ringbuffer.h
Go to the documentation of this file.
1 
19 #ifndef RINGBUFFER_H
20 #define RINGBUFFER_H
21 
22 #ifdef __cplusplus
23 extern "C" {
24 #endif
25 
30 typedef struct {
31  char *buf;
32  unsigned int size;
33  unsigned int start;
34  unsigned int avail;
35 } ringbuffer_t;
36 
44 #define RINGBUFFER_INIT(BUF) { (BUF), sizeof (BUF), 0, 0 }
45 
52 static inline void ringbuffer_init(ringbuffer_t *__restrict rb, char *buffer, unsigned bufsize)
53 {
54  rb->buf = buffer;
55  rb->size = bufsize;
56  rb->start = 0;
57  rb->avail = 0;
58 }
59 
69 int ringbuffer_add_one(ringbuffer_t *__restrict rb, char c);
70 
81 unsigned ringbuffer_add(ringbuffer_t *__restrict rb, const char *buf, unsigned n);
82 
88 int ringbuffer_get_one(ringbuffer_t *__restrict rb);
89 
97 unsigned ringbuffer_get(ringbuffer_t *__restrict rb, char *buf, unsigned n);
98 
105 unsigned ringbuffer_remove(ringbuffer_t *__restrict rb, unsigned n);
106 
112 static inline int ringbuffer_empty(const ringbuffer_t *__restrict rb)
113 {
114  return rb->avail == 0;
115 }
116 
122 static inline int ringbuffer_full(const ringbuffer_t *__restrict rb)
123 {
124  return rb->avail == rb->size;
125 }
126 
132 static inline unsigned int ringbuffer_get_free(const ringbuffer_t *__restrict rb)
133 {
134  return rb->size - rb->avail;
135 }
136 
142 int ringbuffer_peek_one(const ringbuffer_t *__restrict rb);
143 
151 unsigned ringbuffer_peek(const ringbuffer_t *__restrict rb, char *buf, unsigned n);
152 
153 #ifdef __cplusplus
154 }
155 #endif
156 
157 #endif /* RINGBUFFER_H */
static void ringbuffer_init(ringbuffer_t *__restrict rb, char *buffer, unsigned bufsize)
Initialize a ringbuffer.
Definition: ringbuffer.h:52
static unsigned int ringbuffer_get_free(const ringbuffer_t *__restrict rb)
Return available space in ringbuffer.
Definition: ringbuffer.h:132
int ringbuffer_get_one(ringbuffer_t *__restrict rb)
Peek and remove oldest element from the ringbuffer.
unsigned int start
Current read position in the ring buffer.
Definition: ringbuffer.h:33
int ringbuffer_add_one(ringbuffer_t *__restrict rb, char c)
Add an element to the ringbuffer.
unsigned ringbuffer_peek(const ringbuffer_t *__restrict rb, char *buf, unsigned n)
Read, but don't remove, the a number of element of the buffer.
unsigned int size
Size of buf.
Definition: ringbuffer.h:32
Ringbuffer.
Definition: ringbuffer.h:30
static int ringbuffer_empty(const ringbuffer_t *__restrict rb)
Test if the ringbuffer is empty.
Definition: ringbuffer.h:112
unsigned ringbuffer_add(ringbuffer_t *__restrict rb, const char *buf, unsigned n)
Add a number of elements to the ringbuffer.
unsigned int avail
Number of elements available for reading.
Definition: ringbuffer.h:34
unsigned ringbuffer_remove(ringbuffer_t *__restrict rb, unsigned n)
Remove a number of elements from the ringbuffer.
unsigned ringbuffer_get(ringbuffer_t *__restrict rb, char *buf, unsigned n)
Read and remove a number of elements from the ringbuffer.
int ringbuffer_peek_one(const ringbuffer_t *__restrict rb)
Read, but don't remove, the oldest element in the buffer.
static int ringbuffer_full(const ringbuffer_t *__restrict rb)
Test if the ringbuffer is full.
Definition: ringbuffer.h:122
char * buf
Buffer to operate on.
Definition: ringbuffer.h:31