46 lines
1.2 KiB
C
46 lines
1.2 KiB
C
/* SPDX-License-Identifier: MIT */
|
|
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <stdio.h>
|
|
|
|
#include "ringbuf.h"
|
|
|
|
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->write_idx = 0;
|
|
rb->read_idx = 0;
|
|
rb->buffer = malloc_fn(capacity * struct_size); /* Calloc? */
|
|
|
|
// Read from buffer at max position to force a segfault if theres an issue
|
|
printf("Reading from buffer at position %d\n",
|
|
rb->capacity * rb->struct_size);
|
|
void *top = rb->buffer[rb->capacity * rb->struct_size];
|
|
printf("Buffer top successfully read at virtual address: %p\n", &top);
|
|
|
|
printf("Initialized ring buffer. Capacit: %d, struct_size: %d, total: %d\n",
|
|
rb->capacity, rb->struct_size, rb->capacity * rb->struct_size);
|
|
}
|
|
|
|
void
|
|
rb_destroy(struct RingBuf *rb, void(free)())
|
|
{
|
|
free(rb->buffer);
|
|
}
|
|
|
|
enum WriteResult
|
|
rb_push(struct RingBuf *rb, void *data[], rb_size_t amount, MEMCPY_T memcpy_fn)
|
|
{
|
|
printf("\nWriting %d elements\n", amount);
|
|
return Ok;
|
|
}
|
|
|
|
void
|
|
rb_read(struct RingBuf *rb, void *dest, int amount)
|
|
{
|
|
printf("\nReading %d elements\n", amount);
|
|
for(int i = 0; i < amount; i++) { }
|
|
}
|