ringbuffer.h
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: 2013 Freie Universität Berlin
3  * SPDX-FileCopyrightText: 2013 INRIA
4  * SPDX-License-Identifier: LGPL-2.1-only
5  */
6 
7 #pragma once
8 
24 #ifdef __cplusplus
25 extern "C" {
26 #endif
27 
32 typedef struct {
33  char *buf;
34  unsigned int size;
35  unsigned int start;
36  unsigned int avail;
37 } ringbuffer_t;
38 
46 #define RINGBUFFER_INIT(BUF) { (BUF), sizeof(BUF), 0, 0 }
47 
54 static inline void ringbuffer_init(ringbuffer_t *__restrict rb, char *buffer,
55  unsigned bufsize)
56 {
57  rb->buf = buffer;
58  rb->size = bufsize;
59  rb->start = 0;
60  rb->avail = 0;
61 }
62 
72 int ringbuffer_add_one(ringbuffer_t *__restrict rb, char c);
73 
84 unsigned ringbuffer_add(ringbuffer_t *__restrict rb, const char *buf,
85  unsigned n);
86 
92 int ringbuffer_get_one(ringbuffer_t *__restrict rb);
93 
101 unsigned ringbuffer_get(ringbuffer_t *__restrict rb, char *buf, unsigned n);
102 
109 unsigned ringbuffer_remove(ringbuffer_t *__restrict rb, unsigned n);
110 
116 static inline int ringbuffer_empty(const ringbuffer_t *__restrict rb)
117 {
118  return rb->avail == 0;
119 }
120 
126 static inline int ringbuffer_full(const ringbuffer_t *__restrict rb)
127 {
128  return rb->avail == rb->size;
129 }
130 
136 static inline unsigned int ringbuffer_get_free(
137  const ringbuffer_t *__restrict rb)
138 {
139  return rb->size - rb->avail;
140 }
141 
147 int ringbuffer_peek_one(const ringbuffer_t *__restrict rb);
148 
156 unsigned ringbuffer_peek(const ringbuffer_t *__restrict rb, char *buf,
157  unsigned n);
158 
159 #ifdef __cplusplus
160 }
161 #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:54
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:136
static int ringbuffer_full(const ringbuffer_t *__restrict rb)
Test if the ringbuffer is full.
Definition: ringbuffer.h:126
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:116
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:32
char * buf
Buffer to operate on.
Definition: ringbuffer.h:33
unsigned int start
Current read position in the ring buffer.
Definition: ringbuffer.h:35
unsigned int size
Size of buf.
Definition: ringbuffer.h:34
unsigned int avail
Number of elements available for reading.
Definition: ringbuffer.h:36