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 
10 #pragma once
11 
27 #ifdef __cplusplus
28 extern "C" {
29 #endif
30 
35 typedef struct {
36  char *buf;
37  unsigned int size;
38  unsigned int start;
39  unsigned int avail;
40 } ringbuffer_t;
41 
49 #define RINGBUFFER_INIT(BUF) { (BUF), sizeof(BUF), 0, 0 }
50 
57 static inline void ringbuffer_init(ringbuffer_t *__restrict rb, char *buffer,
58  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,
88  unsigned n);
89 
95 int ringbuffer_get_one(ringbuffer_t *__restrict rb);
96 
104 unsigned ringbuffer_get(ringbuffer_t *__restrict rb, char *buf, unsigned n);
105 
112 unsigned ringbuffer_remove(ringbuffer_t *__restrict rb, unsigned n);
113 
119 static inline int ringbuffer_empty(const ringbuffer_t *__restrict rb)
120 {
121  return rb->avail == 0;
122 }
123 
129 static inline int ringbuffer_full(const ringbuffer_t *__restrict rb)
130 {
131  return rb->avail == rb->size;
132 }
133 
139 static inline unsigned int ringbuffer_get_free(
140  const ringbuffer_t *__restrict rb)
141 {
142  return rb->size - rb->avail;
143 }
144 
150 int ringbuffer_peek_one(const ringbuffer_t *__restrict rb);
151 
159 unsigned ringbuffer_peek(const ringbuffer_t *__restrict rb, char *buf,
160  unsigned n);
161 
162 #ifdef __cplusplus
163 }
164 #endif
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.
int ringbuffer_get_one(ringbuffer_t *__restrict rb)
Peek and remove oldest element from the ringbuffer.
static void ringbuffer_init(ringbuffer_t *__restrict rb, char *buffer, unsigned bufsize)
Initialize a ringbuffer.
Definition: ringbuffer.h:57
int ringbuffer_add_one(ringbuffer_t *__restrict rb, char c)
Add an element to the ringbuffer.
static unsigned int ringbuffer_get_free(const ringbuffer_t *__restrict rb)
Return available space in ringbuffer.
Definition: ringbuffer.h:139
static int ringbuffer_full(const ringbuffer_t *__restrict rb)
Test if the ringbuffer is full.
Definition: ringbuffer.h:129
int ringbuffer_peek_one(const ringbuffer_t *__restrict rb)
Read, but don't remove, the oldest element in the buffer.
static int ringbuffer_empty(const ringbuffer_t *__restrict rb)
Test if the ringbuffer is empty.
Definition: ringbuffer.h:119
unsigned ringbuffer_get(ringbuffer_t *__restrict rb, char *buf, unsigned n)
Read and remove a number of elements from the ringbuffer.
unsigned ringbuffer_add(ringbuffer_t *__restrict rb, const char *buf, unsigned n)
Add a number of elements to the ringbuffer.
unsigned ringbuffer_remove(ringbuffer_t *__restrict rb, unsigned n)
Remove a number of elements from the ringbuffer.
Ringbuffer.
Definition: ringbuffer.h:35
char * buf
Buffer to operate on.
Definition: ringbuffer.h:36
unsigned int start
Current read position in the ring buffer.
Definition: ringbuffer.h:38
unsigned int size
Size of buf.
Definition: ringbuffer.h:37
unsigned int avail
Number of elements available for reading.
Definition: ringbuffer.h:39