Exercise 44. Ring Buffer

Ring buffers are incredibly useful when processing asynchronous I/O. They allow one side to receive data in random intervals of random sizes, but feed cohesive chunks to another side in set sizes or intervals. They are a variant on the Queue data structure but focus on blocks of bytes instead of a list of pointers. In this exercise, I’m going to show you the RingBuffer code, and then have you make a full unit test for it.

ringbuffer.h

  1   #ifndef _lcthw_RingBuffer_h   2   #define _lcthw_RingBuffer_h   3   4   #include <lcthw/bstrlib.h>   5   6   typedef struct {   7       char *buffer;   8       int length;   9       int start;  10       int end;  11   } RingBuffer;  12  13   RingBuffer ...

Get Learn C the Hard Way: A Clear & Direct Introduction To Modern C Programming now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.