How do you implement a circular buffer in C? How do you implement a circular buffer in C? c c

How do you implement a circular buffer in C?


The simplest solution would be to keep track of the item size and the number of items, and then create a buffer of the appropriate number of bytes:

typedef struct circular_buffer{    void *buffer;     // data buffer    void *buffer_end; // end of data buffer    size_t capacity;  // maximum number of items in the buffer    size_t count;     // number of items in the buffer    size_t sz;        // size of each item in the buffer    void *head;       // pointer to head    void *tail;       // pointer to tail} circular_buffer;void cb_init(circular_buffer *cb, size_t capacity, size_t sz){    cb->buffer = malloc(capacity * sz);    if(cb->buffer == NULL)        // handle error    cb->buffer_end = (char *)cb->buffer + capacity * sz;    cb->capacity = capacity;    cb->count = 0;    cb->sz = sz;    cb->head = cb->buffer;    cb->tail = cb->buffer;}void cb_free(circular_buffer *cb){    free(cb->buffer);    // clear out other fields too, just to be safe}void cb_push_back(circular_buffer *cb, const void *item){    if(cb->count == cb->capacity){        // handle error    }    memcpy(cb->head, item, cb->sz);    cb->head = (char*)cb->head + cb->sz;    if(cb->head == cb->buffer_end)        cb->head = cb->buffer;    cb->count++;}void cb_pop_front(circular_buffer *cb, void *item){    if(cb->count == 0){        // handle error    }    memcpy(item, cb->tail, cb->sz);    cb->tail = (char*)cb->tail + cb->sz;    if(cb->tail == cb->buffer_end)        cb->tail = cb->buffer;    cb->count--;}


// Note power of two buffer size#define kNumPointsInMyBuffer 1024 typedef struct _ringBuffer {    UInt32 currentIndex;    UInt32 sizeOfBuffer;    double data[kNumPointsInMyBuffer];} ringBuffer;// Initialize the ring bufferringBuffer *myRingBuffer = (ringBuffer *)calloc(1, sizeof(ringBuffer));myRingBuffer->sizeOfBuffer = kNumPointsInMyBuffer;myRingBuffer->currentIndex = 0;// A little function to write into the buffer// N.B. First argument of writeIntoBuffer() just happens to have the// same as the one calloc'ed above. It will only point to the same// space in memory if the calloc'ed pointer is passed to// writeIntoBuffer() as an arg when the function is called. Consider// using another name for clarityvoid writeIntoBuffer(ringBuffer *myRingBuffer, double *myData, int numsamples) {    // -1 for our binary modulo in a moment    int buffLen = myRingBuffer->sizeOfBuffer - 1;    int lastWrittenSample = myRingBuffer->currentIndex;    int idx;    for (int i=0; i < numsamples; ++i) {        // modulo will automagically wrap around our index        idx = (i + lastWrittenSample) & buffLen;         myRingBuffer->data[idx] = myData[i];    }    // Update the current index of our ring buffer.    myRingBuffer->currentIndex += numsamples;    myRingBuffer->currentIndex &= myRingBuffer->sizeOfBuffer - 1;}

As long as your ring buffer's length is a power of two, the incredibly fast binary "&" operation will wrap around your index for you.For my application, I'm displaying a segment of audio to the user from a ring buffer of audio acquired from a microphone.

I always make sure that the maximum amount of audio that can be displayed on screen is much less than the size of the ring buffer. Otherwise you might be reading and writing from the same chunk. This would likely give you weird display artifacts.


Can you enumerate the types needed at the time you code up the buffer, or do you need to be able to add types at run time via dynamic calls? If the former, then I would create the buffer as a heap-allocated array of n structs, where each struct consists of two elements: an enum tag identifying the data type, and a union of all the data types. What you lose in terms of extra storage for small elements, you make up in terms of not having to deal with allocation/deallocation and the resulting memory fragmentation. Then you just need to keep track of the start and end indices that define the head and tail elements of the buffer, and make sure to compute mod n when incrementing/decrementing the indices.