Ring Buffer Pow2

group Fast Ring Buffer

Bitmask-optimized circular buffer.

group Structures

Structures used by the Fast Ring Buffer.

struct osusat_ring_buffer_pow2_t
#include <ring_buffer_pow2.h>

Fast ring buffer state structure.

Public Members

uint8_t *buffer

Pointer to the underlying storage array

size_t capacity

Total size (MUST be power of two)

size_t mask

Pre-calculated mask (capacity - 1) for fast wrapping

size_t head

Write index

size_t tail

Read index

group Public API

External interface for the Fast Ring Buffer.

Functions

static inline bool osusat_is_pow2(size_t x)

Utility to check if a number is a power of two.

Parameters:

x[in] The number to check.

Returns:

true if x is a power of two, false otherwise.

bool osusat_ring_buffer_pow2_init(osusat_ring_buffer_pow2_t *rb, uint8_t *storage, size_t capacity)

Initialize the optimized ring buffer.

Calculates the bitmask based on the provided capacity.

Warning

The capacity MUST be a power of two (e.g., 32, 64, 128). If a non-power-of-two is provided, initialization will fail.

Parameters:
  • rb[out] Pointer to the ring buffer handle.

  • storage[in] Pointer to the allocated byte array.

  • capacity[in] Size of storage in bytes.

Return values:
  • true – Initialization successful.

  • false – Initialization failed (capacity was not a power of two).

static inline void osusat_ring_buffer_pow2_clear(osusat_ring_buffer_pow2_t *rb)

Reset the buffer to empty.

Resets head and tail indices to zero.

Parameters:

rb[inout] The ring buffer handle.

static inline bool osusat_ring_buffer_pow2_empty(const osusat_ring_buffer_pow2_t *rb)

Check if the buffer is empty.

Parameters:

rb[in] The ring buffer handle.

Return values:
  • true – Buffer is empty.

  • false – Buffer contains data.

static inline bool osusat_ring_buffer_pow2_full(const osusat_ring_buffer_pow2_t *rb)

Check if the buffer is full.

Uses bitwise masking to check the next write position.

Parameters:

rb[in] The ring buffer handle.

Return values:
  • true – Buffer is full.

  • false – Buffer has space available.

bool osusat_ring_buffer_pow2_push(osusat_ring_buffer_pow2_t *rb, uint8_t byte)

Push one byte into the buffer.

Parameters:
  • rb[inout] The ring buffer handle.

  • byte[in] The byte to store.

Return values:
  • true – Success, byte added.

  • false – Buffer was full.

bool osusat_ring_buffer_pow2_pop(osusat_ring_buffer_pow2_t *rb, uint8_t *out)

Pop one byte from the buffer.

Parameters:
  • rb[inout] The ring buffer handle.

  • out[out] Pointer to where the popped byte will be written.

Return values:
  • true – Success, byte written to out.

  • false – Buffer was empty.

bool osusat_ring_buffer_pow2_peek(const osusat_ring_buffer_pow2_t *rb, uint8_t *out)

Peek at the next byte without removing it.

Parameters:
  • rb[in] The ring buffer handle.

  • out[out] Pointer to where the peeked byte will be written.

Return values:
  • true – Success, byte written to out.

  • false – Buffer was empty.