ringbuf/ringbuf.c

166 lines
3.9 KiB
C

/* SPDX-License-Identifier: MIT */
// #include <stdlib.h>
// #include <string.h>
#include "ringbuf.h"
#include <assert.h>
#include <stdint.h>
#ifdef DEBUG
#include <stdio.h>
#define DEBUG_PRINT(fmt, ...) printf(fmt, __VA_ARGS__)
#else
#define DEBUG_PRINT(fmt, ...)
#endif
void
rb_init(struct RingBuf *rb, rb_size_t capacity, ALLOC_T malloc_fn,
rb_size_t struct_size)
{
rb->struct_size = struct_size;
rb->capacity = capacity;
rb->buffer = malloc_fn(capacity * struct_size); /* Calloc? */
rb->buffer_end = rb->buffer + (capacity * struct_size);
rb->count = 0;
rb->write_head = rb->buffer;
rb->read_head = rb->buffer;
}
void
rb_destroy(struct RingBuf *rb, FREE_T free_fn)
{
if(rb->buffer) { // Prevent double-free
free_fn(rb->buffer);
rb->buffer = NULL;
rb->buffer_end = NULL;
rb->struct_size = 0;
rb->capacity = 0;
rb->count = 0;
rb->write_head = NULL;
rb->read_head = NULL;
}
}
void
rb_clear(struct RingBuf *rb)
{
rb->count = 0;
rb->write_head = rb->buffer;
rb->read_head = rb->buffer;
}
enum WriteResult
rb_push_back(struct RingBuf *rb, const void *item, MEMCPY_T memcpy_fn)
{
if(rb->count == rb->capacity)
return Full;
assert(rb->buffer != NULL);
memcpy_fn(rb->write_head, item, rb->struct_size);
// Advance the write head
rb->write_head = (char *)rb->write_head + rb->struct_size;
if(rb->write_head == rb->buffer_end)
rb->write_head = rb->buffer;
rb->count++;
return WriteOk;
}
enum WriteResult
rb_push_many(struct RingBuf *rb, const void *items, MEMCPY_T memcpy_fn,
rb_size_t n)
{
if(rb->count + n > rb->capacity)
return Full; // Perhaps rename to InsufficientSpace
// If the write head will move past the end of the buffer
// we need to to the write in two steps.
void *end = (char *)rb->write_head + rb->struct_size * n;
if(end > rb->buffer_end) {
// Calculate the number of items that can be written in the first chunk
rb_size_t first_chunk = (char *)rb->buffer_end - (char *)rb->write_head;
DEBUG_PRINT("Multi-chunk write. First chunk: %ld\n", first_chunk);
// Write the first chunk
memcpy_fn(rb->write_head, items, rb->struct_size * first_chunk);
// Set the write head to the beginning of the buffer
rb->write_head = rb->buffer;
rb->count += first_chunk;
n -= first_chunk;
} else {
DEBUG_PRINT("Single-chunk write. No need to wrap around.%s\n", "");
}
DEBUG_PRINT("Writing %ld items\n", n);
memcpy_fn(rb->write_head, items, rb->struct_size * n);
if(rb->write_head == rb->buffer_end)
rb->write_head = rb->buffer;
// Advance the write head
rb->write_head = (char *)rb->write_head + rb->struct_size * n;
rb->count += n;
return WriteOk;
}
enum ReadResult
rb_pop_front(struct RingBuf *rb, void *item, MEMCPY_T memcpy_fn)
{
if(rb->count == 0)
return Empty;
memcpy_fn(item, rb->read_head, rb->struct_size);
// Advance the read head
rb->read_head = (char *)rb->read_head + rb->struct_size;
if(rb->read_head == rb->buffer_end)
rb->read_head = rb->buffer;
rb->count--;
return ReadOk;
}
#ifdef DEBUG
#include <stdio.h>
void
rb_debug_empty(struct RingBuf *rb)
{
int d;
if(rb->count == 0)
return;
printf("Debug Data: [");
while(rb_pop_front(rb, &d, memcpy) == ReadOk)
printf("%d,", d);
printf("\b]\n");
}
void
rb_debug_print(struct RingBuf *rb)
{
DEBUG_PRINT("============%s\n", "");
DEBUG_PRINT("Count %lu\n", rb->count);
DEBUG_PRINT("Capacity: %ld\n", rb->capacity);
DEBUG_PRINT("Left: %ld\n", rb->capacity - rb->count);
DEBUG_PRINT("Base addr:\t%p\n", rb->buffer);
DEBUG_PRINT("Read Head:\t%p (%ld:th position)\n", rb->read_head,
((rb->read_head) - (rb->buffer)) / rb->struct_size);
DEBUG_PRINT("Write Head:\t%p (%ld:th position)\n", rb->write_head,
((rb->write_head) - (rb->buffer)) / rb->struct_size);
DEBUG_PRINT("============%s\n", "");
}
#endif