ringbuf/ringbuf.h

113 lines
3.2 KiB
C
Raw Normal View History

2024-06-23 14:08:57 +02:00
/* SPDX-License-Identifier: MIT */
2024-06-23 16:27:51 +02:00
#pragma once
#ifndef rb_size_t
#define rb_size_t int
#endif
2024-06-30 23:57:08 +02:00
/** Signatures of allocators */
2024-06-27 02:35:39 +02:00
typedef void *(*ALLOC_T)(rb_size_t);
2024-06-30 23:57:08 +02:00
/** Signature of memcpy */
2024-06-27 02:35:39 +02:00
typedef void *(*MEMCPY_T)(void *, const void *, rb_size_t);
2024-06-23 14:15:56 +02:00
/**
2024-06-30 23:57:08 +02:00
* @brief Ring buffer, also known as circular buffer.
2024-06-23 14:08:57 +02:00
*/
struct RingBuf {
2024-06-30 23:57:08 +02:00
rb_size_t struct_size; /** Size of the struct */
rb_size_t capacity; /** The physical capacity of the entire ringbuf */
rb_size_t count; /** The number of elements in the buffer */
void *write_head; /** Address of the write head */
void *read_head; /** Address of the read head */
void *buffer; /** The actual data */
void *buffer_end; /** The end of the buffer */
2024-06-30 04:36:42 +02:00
} __attribute__((packed));
2024-06-23 14:08:57 +02:00
/*
* Considerations: One hot encoding for the enum values
* (8 bit each with -fshort-enums)
*/
/** Result of a write */
enum WriteResult {
WriteOk, /** The write was successful */
Full, /** The buffer is full */
InsufficientSpace /** There is not enough space to write */
};
/** Result of a read */
enum ReadResult {
Empty, /** The buffer is empty */
ReadOk, /** The read was successful */
InsufficientData /** There is not enough data to read */
};
2024-06-23 14:08:57 +02:00
2024-06-30 23:57:08 +02:00
/**
* @brief Initialize the ring buffer
* @param rb The ring buffer to initialize
* @param capacity The capacity of the ring buffer
* @param alloc The allocator function
* @param struct_size The size of the struct
* @return void
*/
void rb_init(struct RingBuf *rb, rb_size_t capacity, ALLOC_T alloc,
2024-06-27 01:21:25 +02:00
rb_size_t struct_size);
2024-06-23 14:08:57 +02:00
2024-07-02 05:50:59 +02:00
/**
* @brief Clear the ring buffer
* @details This function will reset the read and write heads to the beginning
* of the buffer, and set the count to 0. It will not free the buffer.
* @param rb The ring buffer
*/
void rb_clear(struct RingBuf *rb);
2024-07-02 05:50:47 +02:00
2024-06-30 23:57:08 +02:00
/**
* @brief Insert data to the ring buffer
* @param rb The ring buffer
* @param item The item to insert
* @param memcpy_fn The memcpy function
* @return WriteResult
*/
2024-06-30 04:36:42 +02:00
enum WriteResult rb_push_back(struct RingBuf *rb, const void *item,
MEMCPY_T memcpy_fn);
2024-07-02 05:39:21 +02:00
/**
* @brief Insert multiple data to the ring buffer
2024-07-02 05:50:47 +02:00
*
2024-07-02 05:39:21 +02:00
* @details This function is more efficient than calling rb_push_back multiple
* times. It only advances the write head once, and attempts to write all the
2024-07-02 05:50:47 +02:00
* memory in one go.
*
2024-07-02 05:39:21 +02:00
* If n is greater than the capacity, it will return Full.
* If the full write will overflow, it will wrap around.
2024-07-02 05:50:47 +02:00
*
2024-07-02 05:39:21 +02:00
* If the buffer is full, it will return Full and not write
* anything.
2024-07-02 05:50:47 +02:00
*
2024-07-02 05:39:21 +02:00
* @param rb The ring buffer
* @param items The items to insert
* @param memcpy_fn The memcpy function
* @param n The number of items to insert
* @return WriteResult
*/
enum WriteResult rb_push_many(struct RingBuf *rb, const void *items,
MEMCPY_T memcpy_fn, rb_size_t n);
2024-06-30 23:57:08 +02:00
/**
* @brief Read data from the ring buffer
* @param rb The ring buffer
* @param item The item to read into
* @return ReadResult
*/
2024-07-02 05:50:47 +02:00
enum ReadResult rb_pop_front(struct RingBuf *rb, void *item,
MEMCPY_T memcpy_fn);
2024-06-27 01:21:25 +02:00
2024-06-30 23:57:08 +02:00
/**
* @brief Free the ring buffer
* @param rb The ring buffer
* @param free The free function
*/
2024-06-27 01:21:25 +02:00
void rb_destroy(struct RingBuf *rb, void(free)());