Skip to content

File syn_ringbuf.c

FileList > src > syntropic > util > syn_ringbuf.c

Go to the source code of this file

Ring buffer implementation.

  • #include "syn_ringbuf.h"
  • #include "../common/syn_barrier.h"
  • #include "../util/syn_assert.h"
  • #include <string.h>

Public Functions

Type Name
size_t syn_ringbuf_count (const SYN_RingBuf * rb)
Return the number of bytes currently stored in the buffer.
bool syn_ringbuf_empty (const SYN_RingBuf * rb)
Check if the ring buffer is empty.
size_t syn_ringbuf_free (const SYN_RingBuf * rb)
Return the number of free bytes available for writing.
bool syn_ringbuf_full (const SYN_RingBuf * rb)
Check if the ring buffer is full.
bool syn_ringbuf_get (SYN_RingBuf * rb, uint8_t * byte)
Get a byte from the ring buffer.
void syn_ringbuf_init (SYN_RingBuf * rb, uint8_t * buf, size_t size)
Initialize a ring buffer.
bool syn_ringbuf_peek (const SYN_RingBuf * rb, uint8_t * byte)
Peek at the next byte without removing it.
size_t syn_ringbuf_peek_n (const SYN_RingBuf * rb, uint8_t * data, size_t len)
Peek at up to len bytes without removing them.
bool syn_ringbuf_put (SYN_RingBuf * rb, uint8_t byte)
Put a byte into the ring buffer.
size_t syn_ringbuf_read (SYN_RingBuf * rb, uint8_t * data, size_t len)
Read multiple bytes from the ring buffer.
void syn_ringbuf_reset (SYN_RingBuf * rb)
Reset the ring buffer to empty.
size_t syn_ringbuf_write (SYN_RingBuf * rb, const uint8_t * data, size_t len)
Write multiple bytes into the ring buffer.

Public Static Functions

Type Name
size_t advance (size_t idx, size_t size)
Advance a ring buffer index, wrapping at capacity.

Public Functions Documentation

function syn_ringbuf_count

Return the number of bytes currently stored in the buffer.

size_t syn_ringbuf_count (
    const SYN_RingBuf * rb
) 

Parameters:

  • rb Ring buffer.

Returns:

Byte count.


function syn_ringbuf_empty

Check if the ring buffer is empty.

bool syn_ringbuf_empty (
    const SYN_RingBuf * rb
) 

Parameters:

  • rb Ring buffer.

Returns:

true if empty.


function syn_ringbuf_free

Return the number of free bytes available for writing.

size_t syn_ringbuf_free (
    const SYN_RingBuf * rb
) 

Parameters:

  • rb Ring buffer.

Returns:

Free byte count.


function syn_ringbuf_full

Check if the ring buffer is full.

bool syn_ringbuf_full (
    const SYN_RingBuf * rb
) 

Parameters:

  • rb Ring buffer.

Returns:

true if full.


function syn_ringbuf_get

Get a byte from the ring buffer.

bool syn_ringbuf_get (
    SYN_RingBuf * rb,
    uint8_t * byte
) 

Parameters:

  • rb Ring buffer.
  • byte [out] The retrieved byte.

Returns:

true if a byte was available, false if the buffer is empty.


function syn_ringbuf_init

Initialize a ring buffer.

void syn_ringbuf_init (
    SYN_RingBuf * rb,
    uint8_t * buf,
    size_t size
) 

Parameters:

  • rb Pointer to the ring buffer control struct.
  • buf Pointer to the caller-provided backing array.
  • size Size of the backing array in bytes. Must be > 0. One byte is used as a sentinel, so usable capacity is size - 1.

function syn_ringbuf_peek

Peek at the next byte without removing it.

bool syn_ringbuf_peek (
    const SYN_RingBuf * rb,
    uint8_t * byte
) 

Parameters:

  • rb Ring buffer.
  • byte [out] The peeked byte.

Returns:

true if a byte was available, false if the buffer is empty.


function syn_ringbuf_peek_n

Peek at up to len bytes without removing them.

size_t syn_ringbuf_peek_n (
    const SYN_RingBuf * rb,
    uint8_t * data,
    size_t len
) 

Copies bytes starting from the read position into data without advancing the tail. Subsequent calls to syn_ringbuf_read will return the same bytes.

Parameters:

  • rb Ring buffer.
  • data [out] Destination buffer.
  • len Maximum number of bytes to peek.

Returns:

Number of bytes actually copied (may be less than len).


function syn_ringbuf_put

Put a byte into the ring buffer.

bool syn_ringbuf_put (
    SYN_RingBuf * rb,
    uint8_t byte
) 

Parameters:

  • rb Ring buffer.
  • byte Byte to store.

Returns:

true if the byte was stored, false if the buffer is full.


function syn_ringbuf_read

Read multiple bytes from the ring buffer.

size_t syn_ringbuf_read (
    SYN_RingBuf * rb,
    uint8_t * data,
    size_t len
) 

Reads up to len bytes into data. Returns the number of bytes actually read (may be less than len if the buffer doesn't have enough). Uses memcpy internally for efficiency across wrap boundaries.

Parameters:

  • rb Ring buffer.
  • data [out] Destination buffer.
  • len Maximum number of bytes to read.

Returns:

Number of bytes actually read.


function syn_ringbuf_reset

Reset the ring buffer to empty.

void syn_ringbuf_reset (
    SYN_RingBuf * rb
) 

Parameters:

  • rb Ring buffer.

function syn_ringbuf_write

Write multiple bytes into the ring buffer.

size_t syn_ringbuf_write (
    SYN_RingBuf * rb,
    const uint8_t * data,
    size_t len
) 

Writes up to len bytes from data. Returns the number of bytes actually written (may be less than len if the buffer fills up). Uses memcpy internally for efficiency across wrap boundaries.

Parameters:

  • rb Ring buffer.
  • data Source data to write.
  • len Number of bytes to write.

Returns:

Number of bytes actually written.


Public Static Functions Documentation

function advance

Advance a ring buffer index, wrapping at capacity.

static inline size_t advance (
    size_t idx,
    size_t size
) 

Parameters:

  • idx Current index.
  • size Buffer capacity.

Returns:

Next index.



The documentation for this class was generated from the following file src/syntropic/util/syn_ringbuf.c