ringbuffer.h¶
A utility for storing and retrieving byte data using a ring buffer.
The ringbuffer is useful for buffering data in the same thread context but it is not thread-safe. For a thread-safe ring buffer, see Thread safe ringbuffer in the System library.
-
RINGBUFFER_INIT
( BUF)¶ Initialize a ringbuffer.
1
{ (BUF), sizeof (BUF), 0, 0 }
This macro is meant for static ringbuffers.
Parameters
BUF: Buffer to use for the ringbuffer. The size is deduced through sizeof (BUF)
.Return values
- The static initializer.
-
void
ringbuffer_init
(ringbuffer_t *__restrict rb, char * buffer, unsigned bufsize)¶ Initialize a ringbuffer.
Parameters
rb: Datum to initialize. buffer: Buffer to use by rb. bufsize: sizeof (buffer)
-
int
ringbuffer_add_one
(ringbuffer_t *__restrict rb, char c)¶ Add an element to the ringbuffer.
If rb is full, then the oldest element gets overwritten. Test
ringbuffer.h::ringbuffer_full()
first if overwriting is not intended.Parameters
rb: Ringbuffer to operate on. c: Element to add. Return values
- The element that was dropped, iff the buffer was full. -1 iff the buffer was not full.
-
unsigned
ringbuffer_add
(ringbuffer_t *__restrict rb, const char * buf, unsigned n)¶ Add a number of elements to the ringbuffer.
Only so many elements are added as fit in the ringbuffer. No elements get overwritten. If this is not the intended behavior, then use
ringbuffer.h::ringbuffer_add_one()
in a loop instead.Parameters
rb: Ringbuffer to operate on. buf: Buffer to add elements from. n: Maximum number of elements to add. Return values
- Number of elements actually added. 0 if rb is full.
-
int
ringbuffer_get_one
(ringbuffer_t *__restrict rb)¶ Peek and remove oldest element from the ringbuffer.
Parameters
rb: Ringbuffer to operate on. Return values
- The oldest element that was added, or
-1
if rb is empty.
- The oldest element that was added, or
-
unsigned
ringbuffer_get
(ringbuffer_t *__restrict rb, char * buf, unsigned n)¶ Read and remove a number of elements from the ringbuffer.
Parameters
rb: Ringbuffer to operate on. buf: Buffer to write into. n: Read at most n elements. Return values
- Number of elements actually read.
-
unsigned
ringbuffer_remove
(ringbuffer_t *__restrict rb, unsigned n)¶ Remove a number of elements from the ringbuffer.
Parameters
rb: Ringbuffer to operate on. n: Read at most n elements. Return values
- Number of elements actually removed.
-
int
ringbuffer_empty
(const ringbuffer_t *__restrict rb)¶ Test if the ringbuffer is empty.
Parameters
rb: Ringbuffer to operate on. Return values
- 0 iff not empty
-
int
ringbuffer_full
(const ringbuffer_t *__restrict rb)¶ Test if the ringbuffer is full.
Parameters
rb: Ringbuffer to operate on. Return values
- 0 iff not full
-
unsigned int
ringbuffer_get_free
(const ringbuffer_t *__restrict rb)¶ Return available space in ringbuffer.
Parameters
rb: Ringbuffer to query. Return values
- number of available bytes
-
int
ringbuffer_peek_one
(const ringbuffer_t *__restrict rb)¶ Read, but don’t remove, the oldest element in the buffer.
Parameters
rb: Ringbuffer to operate on. Return values
- Same as
ringbuffer.h::ringbuffer_get_one()
- Same as
-
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.
Parameters
rb: Ringbuffer to operate on. buf: Buffer to write into. n: Read at most n elements. Return values
- Same as
ringbuffer.h::ringbuffer_get()
- Same as