Loading...
Searching...
No Matches
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.
 
static void tsrb_clear (tsrb_t *rb)
 Clear a tsrb.
 
static int tsrb_empty (const tsrb_t *rb)
 Test if the tsrb is empty.
 
static unsigned int tsrb_avail (const tsrb_t *rb)
 Get number of bytes available for reading.
 
static int tsrb_full (const tsrb_t *rb)
 Test if the tsrb is full.
 
static unsigned int tsrb_free (const tsrb_t *rb)
 Get free space in ringbuffer.
 
int tsrb_get_one (tsrb_t *rb)
 Get a byte from ringbuffer.
 
int tsrb_peek_one (tsrb_t *rb)
 Get a byte from ringbuffer, without removing it.
 
int tsrb_get (tsrb_t *rb, uint8_t *dst, size_t n)
 Get bytes from ringbuffer.
 
int tsrb_peek (tsrb_t *rb, uint8_t *dst, size_t n)
 Get bytes from ringbuffer, without removing them.
 
int tsrb_drop (tsrb_t *rb, size_t n)
 Drop bytes from ringbuffer.
 
int tsrb_add_one (tsrb_t *rb, uint8_t c)
 Add a byte to ringbuffer.
 
int tsrb_add (tsrb_t *rb, const uint8_t *src, size_t n)
 Add bytes to ringbuffer.
 

Macro Definition Documentation

◆ TSRB_INIT

#define TSRB_INIT (   BUF)    { (BUF), sizeof (BUF), 0, 0 }

Static initializer.

Definition at line 49 of file tsrb.h.

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 100 of file tsrb.h.

◆ tsrb_clear()

static void tsrb_clear ( tsrb_t rb)
inlinestatic

Clear a tsrb.

Parameters
[out]rbRingbuffer to operate on

Definition at line 74 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 87 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 127 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 114 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.

◆ tsrb_peek()

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

Get bytes from ringbuffer, without removing them.

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_peek_one()

int tsrb_peek_one ( tsrb_t rb)

Get a byte from ringbuffer, without removing it.

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