ringbuf/ringbuf.h
2024-06-27 02:35:39 +02:00

39 lines
1.2 KiB
C

/* SPDX-License-Identifier: MIT */
#pragma once
#ifndef rb_size_t
#define rb_size_t int
#endif
/** Signatures of generic functions */
typedef void *(*ALLOC_T)(rb_size_t);
typedef void *(*MEMCPY_T)(void *, const void *, rb_size_t);
/**
* Ring buffer, also known as circular buffer.
*/
struct RingBuf {
rb_size_t struct_size; /* Size of the struct */
rb_size_t capacity; /* The physical capacity of the entire ringbuf */
rb_size_t size; /* The current size of the ring buffer */
rb_size_t write_idx; /* The write head */
rb_size_t read_idx; /* THe read head */
void **buffer; /* The actual data */
};
enum WriteResult { CollisionError, Ok };
/** Initialize the ring buffer */
void rb_init(struct RingBuf *rb, rb_size_t capacity, void *(*alloc)(rb_size_t),
rb_size_t struct_size);
/** Insert data to the ring buffer */
enum WriteResult rb_push(struct RingBuf *rb, void *data[], rb_size_t amount,
void *(*memcpy)(void *, const void *, rb_size_t));
/** Read data from the ring buffer */
void rb_read(struct RingBuf *rb, void *dest, int amount);
/** Free the ring buffer */
void rb_destroy(struct RingBuf *rb, void(free)());