Header-only ring buffer C util.
- single header module;
- extra configuration file to enable features such as custom assert & thread-safe protect callback;
- malloc free;
- generic type supported;
The module must be configured by supplying a project specific RingBuffer_Conf.h
. RingBuffer_ConfTemplate.h
can be copied from template
folder, renamed and modified to supply compile time options.
#include "RingBuffer.h"
/* Generic type. */
typedef struct
{
uint8_t dummy;
uint32_t x;
} Item_t;
static void* ptrMutex;
/* Thread-safe custom protect callback. */
void
ProtectCallback (uint8_t op, void* ptrArg)
{
switch (op)
{
case RING_BUFFER_UNLOCK:
{
/* Exit critical section / unlock mutex / suspend scheduler. */
}
break;
case RING_BUFFER_LOCK:
{
/* Enter critical section / lock mutex / suspend scheduler. */
}
break;
default:
break;
}
}
/*
* Create a static Item_t ring buffer named testBuffer w/ 7 slots using
* ProtectCallback w/ custom argument for thread-safe protection.
*/
RingBuffer_Create (static , Item_t, testBuffer, 7, ProtectCallback, ptrMutex);
Item_t item;
item.x = 10;
RingBuffer_Push (testBuffer, item);
Item_t item;
RingBuffer_Pop (testBuffer, item);
uint8_t i;
Item_t item[4];
for (i = 0; i < 4; i++)
{
item.x = i;
}
RingBuffer_Fill (testBuffer, item, 4);
Item_t item[2];
RingBuffer_Empty (item, testBuffer, 2);
uint32_t used;
used = RingBuffer_GetUsed (testBuffer);
uint32_t free;
free = RingBuffer_GetFree (testBuffer);
RingBuffer_Reset (testBuffer);