A simple, fast circular buffer implementation
Switch branches/tags
Nothing to show
Clone or download
Pull request Compare This branch is 92 commits behind michaeltyson:master.
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.


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: TPCircularBufferInit and TPCircularBufferCleanup to allocate and free resources.

Producing: Use 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.

Consuming: Use TPCircularBufferTail to get a pointer to the next data to read, followed by TPCircularBufferConsume to free up the space once processed.

Thread safety

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.

Virtual memory technique originally proposed by Philip Howard, and adapted to Darwin by Kurt Revis

See more info at atastypixel.com

Michael Tyson
A Tasty Pixel