O'Reilly logo

Learn C the Hard Way: A Clear & Direct Introduction To Modern C Programming by Zed A. Shaw

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required