How do you implement a circular buffer in C?

后端 未结 8 2115
滥情空心
滥情空心 2020-11-28 00:53

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

8条回答
  •  囚心锁ツ
    2020-11-28 01:37

    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--;
    }
    

提交回复
热议问题