Thread safe ringbuffer

Thread-safe ringbuffer implementation. More...

Detailed Description

Thread-safe ringbuffer implementation.

Attention
Buffer size must be a power of two!

Files

file  tsrb.h
 Thread-safe ringbuffer interface definition.
 

Data Structures

struct  tsrb
 thread-safe ringbuffer struct More...
 

Macros

#define TSRB_INIT(BUF)   { (BUF), sizeof (BUF), 0, 0 }
 Static initializer.
 

Typedefs

typedef struct tsrb tsrb_t
 thread-safe ringbuffer struct
 

Functions

static void tsrb_init (tsrb_t *rb, uint8_t *buffer, unsigned bufsize)
 Initialize a tsrb. More...
 
static int tsrb_empty (const tsrb_t *rb)
 Test if the tsrb is empty. More...
 
static unsigned int tsrb_avail (const tsrb_t *rb)
 Get number of bytes available for reading. More...
 
static int tsrb_full (const tsrb_t *rb)
 Test if the tsrb is full. More...
 
static unsigned int tsrb_free (const tsrb_t *rb)
 Get free space in ringbuffer. More...
 
int tsrb_get_one (tsrb_t *rb)
 Get a byte from ringbuffer. More...
 
int tsrb_get (tsrb_t *rb, uint8_t *dst, size_t n)
 Get bytes from ringbuffer. More...
 
int tsrb_drop (tsrb_t *rb, size_t n)
 Drop bytes from ringbuffer. More...
 
int tsrb_add_one (tsrb_t *rb, uint8_t c)
 Add a byte to ringbuffer. More...
 
int tsrb_add (tsrb_t *rb, const uint8_t *src, size_t n)
 Add bytes to ringbuffer. More...
 

Function Documentation

◆ tsrb_add()

int tsrb_add ( tsrb_t rb,
const uint8_t *  src,
size_t  n 
)

Add bytes to ringbuffer.

Parameters
[in]rbRingbuffer to operate on
[in]srcbuffer to read from
[in]nmax number of bytes to read from src
Returns
nr of bytes read from src

◆ tsrb_add_one()

int tsrb_add_one ( tsrb_t rb,
uint8_t  c 
)

Add a byte to ringbuffer.

Parameters
[in]rbRingbuffer to operate on
[in]cCharacter to add to ringbuffer
Returns
0 on success
-1 if no space available

◆ tsrb_avail()

static unsigned int tsrb_avail ( const tsrb_t rb)
inlinestatic

Get number of bytes available for reading.

Parameters
[in]rbRingbuffer to operate on
Returns
nr of available bytes

Definition at line 90 of file tsrb.h.

◆ tsrb_drop()

int tsrb_drop ( tsrb_t rb,
size_t  n 
)

Drop bytes from ringbuffer.

Parameters
[in]rbRingbuffer to operate on
[in]nmax number of bytes to drop
Returns
nr of bytes dropped

◆ tsrb_empty()

static int tsrb_empty ( const tsrb_t rb)
inlinestatic

Test if the tsrb is empty.

Parameters
[in]rbRingbuffer to operate on
Returns
0 if not empty
1 otherwise

Definition at line 76 of file tsrb.h.

◆ tsrb_free()

static unsigned int tsrb_free ( const tsrb_t rb)
inlinestatic

Get free space in ringbuffer.

Parameters
[in]rbRingbuffer to operate on
Returns
nr of available bytes

Definition at line 117 of file tsrb.h.

◆ tsrb_full()

static int tsrb_full ( const tsrb_t rb)
inlinestatic

Test if the tsrb is full.

Parameters
[in]rbRingbuffer to operate on
Returns
0 if not full
1 otherwise

Definition at line 104 of file tsrb.h.

◆ tsrb_get()

int tsrb_get ( tsrb_t rb,
uint8_t *  dst,
size_t  n 
)

Get bytes from ringbuffer.

Parameters
[in]rbRingbuffer to operate on
[out]dstbuffer to write to
[in]nmax number of bytes to write to dst
Returns
nr of bytes written to dst

◆ tsrb_get_one()

int tsrb_get_one ( tsrb_t rb)

Get a byte from ringbuffer.

Parameters
[in]rbRingbuffer to operate on
Returns
>=0 byte that has been read
-1 if no byte available

◆ tsrb_init()

static void tsrb_init ( tsrb_t rb,
uint8_t *  buffer,
unsigned  bufsize 
)
inlinestatic

Initialize a tsrb.

Parameters
[out]rbDatum to initialize.
[in]bufferBuffer to use by tsrb.
[in]bufsizesizeof (buffer), must be power of 2.

Definition at line 57 of file tsrb.h.