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,
59  unsigned bufsize)
60 {
61  rb->buf = buffer;
62  rb->size = bufsize;
63  rb->start = 0;
64  rb->avail = 0;
65 }
66 
76 int ringbuffer_add_one(ringbuffer_t *__restrict rb, char c);
77 
88 unsigned ringbuffer_add(ringbuffer_t *__restrict rb, const char *buf,
89  unsigned n);
90 
96 int ringbuffer_get_one(ringbuffer_t *__restrict rb);
97 
105 unsigned ringbuffer_get(ringbuffer_t *__restrict rb, char *buf, unsigned n);
106 
113 unsigned ringbuffer_remove(ringbuffer_t *__restrict rb, unsigned n);
114 
120 static inline int ringbuffer_empty(const ringbuffer_t *__restrict rb)
121 {
122  return rb->avail == 0;
123 }
124 
130 static inline int ringbuffer_full(const ringbuffer_t *__restrict rb)
131 {
132  return rb->avail == rb->size;
133 }
134 
140 static inline unsigned int ringbuffer_get_free(
141  const ringbuffer_t *__restrict rb)
142 {
143  return rb->size - rb->avail;
144 }
145 
151 int ringbuffer_peek_one(const ringbuffer_t *__restrict rb);
152 
160 unsigned ringbuffer_peek(const ringbuffer_t *__restrict rb, char *buf,
161  unsigned n);
162 
163 #ifdef __cplusplus
164 }
165 #endif
166 
167 #endif /* RINGBUFFER_H */
ringbuffer_peek
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.
ringbuffer_remove
unsigned ringbuffer_remove(ringbuffer_t *__restrict rb, unsigned n)
Remove a number of elements from the ringbuffer.
ringbuffer_full
static int ringbuffer_full(const ringbuffer_t *__restrict rb)
Test if the ringbuffer is full.
Definition: ringbuffer.h:130
ringbuffer_get
unsigned ringbuffer_get(ringbuffer_t *__restrict rb, char *buf, unsigned n)
Read and remove a number of elements from the ringbuffer.
ringbuffer_init
static void ringbuffer_init(ringbuffer_t *__restrict rb, char *buffer, unsigned bufsize)
Initialize a ringbuffer.
Definition: ringbuffer.h:58
ringbuffer_peek_one
int ringbuffer_peek_one(const ringbuffer_t *__restrict rb)
Read, but don't remove, the oldest element in the buffer.
ringbuffer_t
Ringbuffer.
Definition: ringbuffer.h:36
ringbuffer_t::buf
char * buf
Buffer to operate on.
Definition: ringbuffer.h:37
ringbuffer_get_free
static unsigned int ringbuffer_get_free(const ringbuffer_t *__restrict rb)
Return available space in ringbuffer.
Definition: ringbuffer.h:140
ringbuffer_t::start
unsigned int start
Current read position in the ring buffer.
Definition: ringbuffer.h:39
ringbuffer_add
unsigned ringbuffer_add(ringbuffer_t *__restrict rb, const char *buf, unsigned n)
Add a number of elements to the ringbuffer.
ringbuffer_add_one
int ringbuffer_add_one(ringbuffer_t *__restrict rb, char c)
Add an element to the ringbuffer.
ringbuffer_get_one
int ringbuffer_get_one(ringbuffer_t *__restrict rb)
Peek and remove oldest element from the ringbuffer.
ringbuffer_t::avail
unsigned int avail
Number of elements available for reading.
Definition: ringbuffer.h:40
ringbuffer_empty
static int ringbuffer_empty(const ringbuffer_t *__restrict rb)
Test if the ringbuffer is empty.
Definition: ringbuffer.h:120
ringbuffer_t::size
unsigned int size
Size of buf.
Definition: ringbuffer.h:38