A simple, fast circular buffer implementation for audio processing
A simple C implementation for a circular (ring) buffer. Thread-safe with a single producer and a single consumer, using OSAtomic.h primitives, and avoids any need for buffer wrapping logic by using a virtual memory map technique to place a virtual copy of the buffer straight after the end of the real buffer.
Distributed under the MIT license
Initialisation and cleanup:
TPCircularBufferCleanup to allocate and free resources.
TPCircularBufferHead to get a pointer to write to the buffer, followed by
TPCircularBufferProduce to submit the written data.
TPCircularBufferProduceBytes is a convenience routine for writing data straight to the buffer.
TPCircularBufferTail to get a pointer to the next data to read, followed by
TPCircularBufferConsume to free up the space once processed.
As long as you restrict multithreaded access to just one producer, and just one consumer, this utility should be thread safe.
Only one shared variable is used (the buffer fill count), and OSAtomic primitives are used to write to this value to ensure atomicity.
See more info at atastypixel.com
A Tasty Pixel