A fast, type agnostic ring buffer that's easy to use.
#include "ringBuffer.h"
Use the -fopenmp
flag with GCC or LLVM/Clang.
rb_create()
rb_destroy()
rb_write()
rb_overWrite()
rb_read()
rb_dump()
rb_reset()
rb_fillLevel()
Create a ringBuffer.
ringBuffer* rb_create(size_t len, size_t size)
Returns pointer to new ringBuffer. Otherwise returns NULL if memory allocation failed.
len : Max number of elements the ringBuffer can hold. If len is not a power of 2, it will be
automatically rounded up to the next greater power of 2 for speed.
size : Size in bytes of each element.
Free memory allocated for a ringBuffer.
void rb_destroy(ringBuffer* r)
r : Pointer returned by rb_create().
Write an element into a ringBuffer, without overwriting. If the ringBuffer is full, rb_write()
does nothing.
void rb_write(ringBuffer* r, void* src)
r : Pointer returned by rb_create().
src : Pointer to data that will be copied into the ringBuffer. r→size number of bytes will be copied.
Write an element into a ringBuffer, with overwriting. If the ringBuffer is full, the oldest element will be overwritten.
void rb_overWrite(ringBuffer* r, void* src)
r : Pointer returned by rb_create().
src : Pointer to data that will be copied into the ringBuffer. r→size number of bytes will be copied.
Read oldest element from a ringBuffer.
void rb_read(ringBuffer* r, void* dest)
r : Pointer returned by rb_create().
dest : Pointer to location where the element will be copied to. r→size number of bytes will be copied.
Parallelized read of all elements stored in a ringBuffer. Unlike after calling rb_read()
, elements remain in the buffer.
void rb_dump(ringBuffer* r, void* dest)
r : Pointer returned by rb_create().
dest : Pointer to location where the elements will be copied to. r→size * rb_fillLevel(r) number of bytes
will be copied. Therefore, r→size determines data alignment at dest.
Remove all elements held in a ringBuffer.
void rb_reset(ringBuffer* r)
r : Pointer returned by rb_create().
Get number of elements held in a ringBuffer.
size_t rb_fillLevel(ringBuffer* r)
Returns size_t x, where 0 ≤ x ≤ r→len.
r : Pointer returned by rb_create().
This project is licenced under the WTFPLv2 — see the COPYING file for details
Copyright © 2021 Andre Ostrovsky