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 the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.