How do you implement a circular buffer in C?

后端 未结 8 2114
滥情空心
滥情空心 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条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-11-28 01:16

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

提交回复
热议问题