2024-06-23 14:08:57 +02:00
|
|
|
/* SPDX-License-Identifier: MIT */
|
|
|
|
|
2024-06-27 00:05:49 +02:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
|
2024-06-23 14:08:57 +02:00
|
|
|
#include "ringbuf.h"
|
|
|
|
|
2024-06-30 20:31:52 +02:00
|
|
|
#ifdef DEBUG
|
|
|
|
#define DEBUG_PRINT(fmt, ...) printf(fmt, __VA_ARGS__)
|
|
|
|
#else
|
|
|
|
#define DEBUG_PRINT(fmt, ...)
|
|
|
|
#endif
|
|
|
|
|
2024-06-27 00:05:49 +02:00
|
|
|
void
|
2024-06-27 14:06:08 +02:00
|
|
|
rb_init(struct RingBuf *rb, rb_size_t capacity, ALLOC_T malloc_fn,
|
|
|
|
rb_size_t struct_size)
|
2024-06-27 00:05:49 +02:00
|
|
|
{
|
2024-06-23 14:08:57 +02:00
|
|
|
rb->struct_size = struct_size;
|
|
|
|
rb->capacity = capacity;
|
2024-06-27 02:35:39 +02:00
|
|
|
rb->buffer = malloc_fn(capacity * struct_size); /* Calloc? */
|
2024-06-30 04:36:42 +02:00
|
|
|
rb->buffer_end = rb->buffer + (capacity * struct_size);
|
2024-06-30 20:06:30 +02:00
|
|
|
rb->count = 0;
|
|
|
|
rb->write_head = rb->buffer;
|
|
|
|
rb->read_head = rb->buffer;
|
2024-06-27 01:21:25 +02:00
|
|
|
|
2024-06-30 20:31:52 +02:00
|
|
|
DEBUG_PRINT("Reading from buffer at position %d\n",
|
|
|
|
rb->capacity * rb->struct_size);
|
2024-06-30 21:11:49 +02:00
|
|
|
|
|
|
|
// Read from buffer at max position to force a segfault if theres an issue
|
2024-06-30 04:36:42 +02:00
|
|
|
void *top = rb->buffer + (rb->capacity * rb->struct_size);
|
2024-06-30 21:11:49 +02:00
|
|
|
|
2024-06-30 20:31:52 +02:00
|
|
|
DEBUG_PRINT("Buffer top successfully read at virtual address: %p\n", &top);
|
|
|
|
|
|
|
|
DEBUG_PRINT(
|
|
|
|
"Initialized ring buffer. Capacit: %d, struct_size: %d, total: %d\n",
|
|
|
|
rb->capacity, rb->struct_size, rb->capacity * rb->struct_size);
|
2024-06-27 01:21:25 +02:00
|
|
|
|
2024-06-30 20:31:52 +02:00
|
|
|
DEBUG_PRINT("Size of RB: %lu\n", sizeof(struct RingBuf));
|
2024-06-23 14:08:57 +02:00
|
|
|
}
|
|
|
|
|
2024-06-27 00:05:49 +02:00
|
|
|
void
|
2024-06-27 01:21:25 +02:00
|
|
|
rb_destroy(struct RingBuf *rb, void(free)())
|
2024-06-27 00:05:49 +02:00
|
|
|
{
|
2024-06-23 16:27:51 +02:00
|
|
|
free(rb->buffer);
|
|
|
|
}
|
2024-06-23 14:08:57 +02:00
|
|
|
|
2024-06-27 00:05:49 +02:00
|
|
|
enum WriteResult
|
2024-06-30 04:36:42 +02:00
|
|
|
rb_push_back(struct RingBuf *rb, const void *item, MEMCPY_T memcpy_fn)
|
2024-06-27 00:05:49 +02:00
|
|
|
{
|
2024-06-30 05:20:01 +02:00
|
|
|
if(rb->count == rb->capacity)
|
|
|
|
return Full;
|
2024-06-27 14:06:08 +02:00
|
|
|
|
2024-06-30 20:06:30 +02:00
|
|
|
memcpy_fn(rb->write_head, item, rb->struct_size);
|
2024-06-27 14:06:08 +02:00
|
|
|
|
2024-06-30 20:31:52 +02:00
|
|
|
// Advance the write head
|
2024-06-30 20:06:30 +02:00
|
|
|
rb->write_head = (char *)rb->write_head + rb->struct_size;
|
2024-06-27 14:06:08 +02:00
|
|
|
|
2024-06-30 04:36:42 +02:00
|
|
|
if(rb->write_head == rb->buffer_end)
|
|
|
|
rb->write_head = rb->buffer;
|
2024-06-27 14:06:08 +02:00
|
|
|
|
2024-06-30 04:36:42 +02:00
|
|
|
rb->count++;
|
2024-06-30 05:29:07 +02:00
|
|
|
return WriteOk;
|
2024-06-27 00:05:49 +02:00
|
|
|
}
|
2024-06-27 01:21:25 +02:00
|
|
|
|
2024-07-02 05:39:21 +02:00
|
|
|
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: %d\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 %d 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;
|
|
|
|
}
|
|
|
|
|
2024-06-30 05:20:01 +02:00
|
|
|
enum ReadResult
|
2024-06-30 05:08:42 +02:00
|
|
|
rb_pop_front(struct RingBuf *rb, void *item)
|
2024-06-27 14:06:08 +02:00
|
|
|
{
|
2024-06-30 05:20:01 +02:00
|
|
|
if(rb->count == 0)
|
|
|
|
return Empty;
|
|
|
|
|
2024-06-30 20:06:30 +02:00
|
|
|
memcpy(item, rb->read_head, rb->struct_size);
|
2024-06-30 20:31:52 +02:00
|
|
|
|
|
|
|
// Advance the read head
|
2024-06-30 20:06:30 +02:00
|
|
|
rb->read_head = (char *)rb->read_head + rb->struct_size;
|
2024-06-30 05:20:01 +02:00
|
|
|
|
2024-06-30 05:08:42 +02:00
|
|
|
if(rb->read_head == rb->buffer_end)
|
|
|
|
rb->read_head = rb->buffer;
|
2024-06-30 05:20:01 +02:00
|
|
|
|
2024-06-30 05:08:42 +02:00
|
|
|
rb->count--;
|
2024-06-30 05:29:07 +02:00
|
|
|
return ReadOk;
|
2024-06-27 01:21:25 +02:00
|
|
|
}
|