blob: bd7060a2924a92df0ecbf9950100493b8c75db82 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
|
/* Ring Buffer implementation */
/* (C) 2012 by Sylvain Munaut <tnt@246tNt.com>
* All Rights Reserved
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef __OSMO_RINGBUF_H__
#define __OSMO_RINGBUF_H__
struct osmo_ringbuf
{
/* Memory zone */
void *base; /*!< Base pointer */
unsigned int len; /*!< Length in bytes */
/* Pointers */
unsigned int ri; /*!< Read index */
unsigned int wi; /*!< Write index */
};
int osmo_rb_init(struct osmo_ringbuf *rb, unsigned int len);
void osmo_rb_deinit(struct osmo_ringbuf *rb);
struct osmo_ringbuf *osmo_rb_alloc(unsigned int len);
void osmo_rb_free(struct osmo_ringbuf *rb);
static inline void
osmo_rb_clear(struct osmo_ringbuf *rb)
{
rb->wi = 0;
rb->ri = 0;
}
static inline unsigned int
osmo_rb_used_bytes(struct osmo_ringbuf *rb)
{
return (rb->wi - rb->ri + rb->len) % rb->len;
}
static inline unsigned int
osmo_rb_free_bytes(struct osmo_ringbuf *rb)
{
return rb->len - osmo_rb_used_bytes(rb) - 1;
}
static inline void *
osmo_rb_write_ptr(struct osmo_ringbuf *rb)
{
return rb->base + rb->wi;
}
static inline void *
osmo_rb_read_ptr(struct osmo_ringbuf *rb)
{
return rb->base + rb->ri;
}
static inline void
osmo_rb_write_advance(struct osmo_ringbuf *rb, unsigned int bytes)
{
rb->wi = (rb->wi + bytes) % rb->len;
}
static inline void
osmo_rb_read_advance(struct osmo_ringbuf *rb, unsigned int bytes)
{
rb->ri = (rb->ri + bytes) % rb->len;
}
#endif /* __OSMO_RINGBUF_H__ */
|