paraslash Paraslash Audio Streaming
About   News   Download   Documentation   Development

Functions
ringbuffer.c File Reference

Simple ringbuffer implementation. More...

#include <regex.h>
#include "para.h"
#include "ringbuffer.h"
#include "string.h"

Functions

struct ringbuffer * ringbuffer_new (unsigned size)
 Initialize a new ringbuffer. More...
 
void * ringbuffer_add (struct ringbuffer *rb, void *data)
 Add one entry to a ringbuffer. More...
 
void * ringbuffer_get (struct ringbuffer *rb, int num)
 Get one entry from a ringbuffer. More...
 
unsigned ringbuffer_filled (struct ringbuffer *rb)
 Get the number of entries in the ring buffer. More...
 

Detailed Description

Simple ringbuffer implementation.

Function Documentation

◆ ringbuffer_new()

struct ringbuffer* ringbuffer_new ( unsigned  size)

Initialize a new ringbuffer.

Parameters
sizeThe number of entries the ringbuffer holds.

This function initializes a circular ring buffer which can hold up to size entries of arbitrary type. If performance is an issue, size should be a power of two to make the underlying modulo operations cheap. Arbitrary many ringbuffers may be initialized via this function. Each ringbuffer is identified by a 'cookie'.

Returns
A 'cookie' which identifies the ringbuffer just created and which must be passed to ringbuffer_add() and ringbuffer_get().

References zalloc().

◆ ringbuffer_add()

void* ringbuffer_add ( struct ringbuffer *  rb,
void *  data 
)

Add one entry to a ringbuffer.

Parameters
rbThe ringbuffer identifier.
dataThe data to be inserted.

Insert data into the ringbuffer associated with cookie. As soon as the ringbuffer fills up, its oldest entry is disregarded and replaced by data.

Returns
The old data pointer which is going to be disregarded, or NULL if the ringbuffer is not yet full.

◆ ringbuffer_get()

void* ringbuffer_get ( struct ringbuffer *  rb,
int  num 
)

Get one entry from a ringbuffer.

Parameters
rbThe ringbuffer identifier.
numThe number of the entry.
Returns
A pointer to data previously added, or NULL if entry number num is not available. num counts backwards from zero, i.e. ringbuffer_get_entry(0) gets the entry which was added most recently.

◆ ringbuffer_filled()

unsigned ringbuffer_filled ( struct ringbuffer *  rb)

Get the number of entries in the ring buffer.

Parameters
rbThe ringbuffer identifier
Returns
This function always succeeds. It returns a number less than the size of the ring buffer.