ringbuffer.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2013 Freie Universit├Ąt Berlin
3  * Copyright (C) 2013 INRIA
4  *
5  * This file is subject to the terms and conditions of the GNU Lesser
6  * General Public License v2.1. See the file LICENSE in the top level
7  * directory for more details.
8  */
9 
25 #ifndef RINGBUFFER_H
26 #define RINGBUFFER_H
27 
28 #ifdef __cplusplus
29 extern "C" {
30 #endif
31 
36 typedef struct {
37  char *buf;
38  unsigned int size;
39  unsigned int start;
40  unsigned int avail;
41 } ringbuffer_t;
42 
50 #define RINGBUFFER_INIT(BUF) { (BUF), sizeof (BUF), 0, 0 }
51 
58 static inline void ringbuffer_init(ringbuffer_t *__restrict rb, char *buffer, unsigned bufsize)
59 {
60  rb->buf = buffer;
61  rb->size = bufsize;
62  rb->start = 0;
63  rb->avail = 0;
64 }
65 
75 int ringbuffer_add_one(ringbuffer_t *__restrict rb, char c);
76 
87 unsigned ringbuffer_add(ringbuffer_t *__restrict rb, const char *buf, unsigned n);
88 
94 int ringbuffer_get_one(ringbuffer_t *__restrict rb);
95 
103 unsigned ringbuffer_get(ringbuffer_t *__restrict rb, char *buf, unsigned n);
104 
111 unsigned ringbuffer_remove(ringbuffer_t *__restrict rb, unsigned n);
112 
118 static inline int ringbuffer_empty(const ringbuffer_t *__restrict rb)
119 {
120  return rb->avail == 0;
121 }
122 
128 static inline int ringbuffer_full(const ringbuffer_t *__restrict rb)
129 {
130  return rb->avail == rb->size;
131 }
132 
138 static inline unsigned int ringbuffer_get_free(const ringbuffer_t *__restrict rb)
139 {
140  return rb->size - rb->avail;
141 }
142 
148 int ringbuffer_peek_one(const ringbuffer_t *__restrict rb);
149 
157 unsigned ringbuffer_peek(const ringbuffer_t *__restrict rb, char *buf, unsigned n);
158 
159 #ifdef __cplusplus
160 }
161 #endif
162 
163 #endif /* RINGBUFFER_H */
static void ringbuffer_init(ringbuffer_t *__restrict rb, char *buffer, unsigned bufsize)
Initialize a ringbuffer.
Definition: ringbuffer.h:58
static unsigned int ringbuffer_get_free(const ringbuffer_t *__restrict rb)
Return available space in ringbuffer.
Definition: ringbuffer.h:138
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:39
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:38
Ringbuffer.
Definition: ringbuffer.h:36
static int ringbuffer_empty(const ringbuffer_t *__restrict rb)
Test if the ringbuffer is empty.
Definition: ringbuffer.h:118
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:40
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:128
char * buf
Buffer to operate on.
Definition: ringbuffer.h:37