I have a need for a fixed-size (selectable at run-time when creating it, not compile-time) circular buffer which can hold objects of any type and it needs to be very
Extending adam-rosenfield's solution, i think the following will work for multithreaded single producer - single consumer scenario.
int cb_push_back(circular_buffer *cb, const void *item)
{
void *new_head = (char *)cb->head + cb->sz;
if (new_head == cb>buffer_end) {
new_head = cb->buffer;
}
if (new_head == cb->tail) {
return 1;
}
memcpy(cb->head, item, cb->sz);
cb->head = new_head;
return 0;
}
int cb_pop_front(circular_buffer *cb, void *item)
{
void *new_tail = cb->tail + cb->sz;
if (cb->head == cb->tail) {
return 1;
}
memcpy(item, cb->tail, cb->sz);
if (new_tail == cb->buffer_end) {
new_tail = cb->buffer;
}
cb->tail = new_tail;
return 0;
}