TPCircularBuffer.h File Reference
#include <stdbool.h>
#include <string.h>
#include <assert.h>
#include <stdatomic.h>

Go to the source code of this file.

Classes

struct  TPCircularBuffer
 

Macros

#define TPCircularBufferInit(buffer, length)   _TPCircularBufferInit(buffer, length, sizeof(*buffer))
 Initialise buffer.
 

Functions

bool _TPCircularBufferInit (TPCircularBuffer *buffer, int32_t length, size_t structSize)
 
void TPCircularBufferCleanup (TPCircularBuffer *buffer)
 Cleanup buffer.
 
void TPCircularBufferClear (TPCircularBuffer *buffer)
 Clear buffer.
 
void TPCircularBufferSetAtomic (TPCircularBuffer *buffer, bool atomic)
 Set the atomicity.
 
 return (void *)((char *) buffer-> buffer+buffer->tail)
 
 if (buffer->atomic)
 
 assert (buffer->fillCount >=0)
 
 assert (buffer->fillCount<=buffer->length)
 
 if (space< len) return false
 
 memcpy (ptr, src, len)
 
 TPCircularBufferProduce (buffer, len)
 

Variables

static __inline__ int32_t * availableBytes
 
if availableBytes return NULL
 
static __inline__ int32_t amount
 
 else
 
static __inline__ const void * src
 
static __inline__ const void
int32_t 
len
 
void * ptr = TPCircularBufferHead(buffer, &space)
 
return true
 
buffer fillCount = amount
 

Class Documentation

struct TPCircularBuffer
Class Members
void * buffer
int32_t length
int32_t tail
int32_t head
volatile atomic_int fillCount
bool atomic

Macro Definition Documentation

#define TPCircularBufferInit (   buffer,
  length 
)    _TPCircularBufferInit(buffer, length, sizeof(*buffer))

Initialise buffer.

Note that the length is advisory only: Because of the way the memory mirroring technique works, the true buffer length will be multiples of the device page size (e.g. 4096 bytes)

If you intend to use the AudioBufferList utilities, you should always allocate a bit more space than you need for pure audio data, so there's room for the metadata. How much extra is required depends on how many AudioBufferList structures are used, which is a function of how many audio frames each buffer holds. A good rule of thumb is to add 15%, or at least another 2048 bytes or so.

Parameters
bufferCircular buffer
lengthLength of buffer

Function Documentation

bool _TPCircularBufferInit ( TPCircularBuffer buffer,
int32_t  length,
size_t  structSize 
)
void TPCircularBufferCleanup ( TPCircularBuffer buffer)

Cleanup buffer.

Releases buffer resources.

void TPCircularBufferClear ( TPCircularBuffer buffer)

Clear buffer.

Resets buffer to original, empty state.

This is safe for use by consumer while producer is accessing buffer.

void TPCircularBufferSetAtomic ( TPCircularBuffer buffer,
bool  atomic 
)

Set the atomicity.

If you set the atomiticy to false using this method, the buffer will not use atomic operations. This can be used to give the compiler a little more optimisation opportunities when the buffer is only used on one thread.

Important note: Only set this to false if you know what you're doing!

The default value is true (the buffer will use atomic operations)

Parameters
bufferCircular buffer
atomicWhether the buffer is atomic (default true)
return ( void *  ) -> buffer+buffer->tail)
if ( buffer->  atomic)
assert ( buffer->fillCount >=  0)
assert ( buffer->fillCount<=buffer->  length)
if ( )
memcpy ( ptr  ,
src  ,
len   
)
TPCircularBufferProduce ( buffer  ,
len   
)

Variable Documentation

static __inline__ int32_t * availableBytes
Initial value:
{
*availableBytes = buffer->fillCount
static __inline__ int32_t amount
Initial value:
{
buffer->tail = (buffer->tail + amount) % buffer->length
else
Initial value:
{
buffer->fillCount -= amount
__inline__ const void* src
__inline__ const void int32_t len
Initial value:
{
int32_t space
void* ptr = TPCircularBufferHead(buffer, &space)
return true
buffer fillCount = amount