dtringfifo
Lock-free byte ring buffer for Single Producer Single Consumer use
This group of functions provides byte-oriented FIFO buffering for a single producer and a single consumer. It implements for low-level data transfer between concurrent contexts without external locking. The implementation uses simple memcpy-based transfers.
Mini-guide
- Configure the ring over caller-provided storage by requiring explicit initialization and configuration.
- Use exactly one producer and one consumer to preserve the lock-free concurrency model.
- If necessary to reset the ring, do it only when no concurrent access is happening by enforcing external synchronization.
Example
uint8_t storage[128];
dtringfifo_t fifo;
dtringfifo_config_t cfg = {
.buffer = storage,
.capacity = sizeof(storage),
};
dtringfifo_init(&fifo);
dtringfifo_configure(&fifo, &cfg);
dtringfifo_push(&fifo, data, data_len);
count = dtringfifo_pop(&fifo, out, out_len);
Data structures
dtringfifo_config_t
Describes caller-provided storage used to configure a ring.
Members:
uint8_t* bufferPointer to caller-owned storage backing the ring.
int32_t capacityTotal number of bytes in the storage buffer.
dtringfifo_t
Holds ring state and indices for single-producer single-consumer access.
Members:
uint8_t* bufferPointer to caller-owned storage backing the ring.
int32_t capacityTotal number of bytes in the storage buffer.
_Atomic int32_t headIndex of the next write position.
_Atomic int32_t tailIndex of the next read position.
Functions
dtringfifo_configure
Associates the ring with caller-provided storage and initializes indices.
Params:
dtringfifo_t* selfRing instance to configure.
const dtringfifo_config_t* cfgConfiguration describing storage and capacity.
Return: dterr_t* Error object on failure, or NULL on success.
dtringfifo_init
Initializes the ring structure to a known empty state.
Params:
dtringfifo_t* selfRing instance to initialize.
Return: dterr_t* Error object on failure, or NULL on success.
dtringfifo_pop
Copies available bytes from the ring into the destination buffer.
Params:
dtringfifo_t* selfRing instance to read from.
uint8_t* destDestination buffer to receive data.
int32_t dest_lenMaximum number of bytes to copy.
Return: int32_t Number of bytes actually copied.
dtringfifo_push
Copies bytes from the source buffer into the ring until full.
Params:
dtringfifo_t* selfRing instance to write to.
const uint8_t* srcSource buffer providing data.
int32_t src_lenNumber of bytes available in the source buffer.
Return: int32_t Number of bytes actually stored.
dtringfifo_reset
Resets read and write indices while preserving configured storage.
Params:
dtringfifo_t* selfRing instance to reset.
Return: void No return value.