Driver code, currently segfaulting

This commit is contained in:
Imbus 2024-06-23 16:27:51 +02:00
parent e24b2f7904
commit 7ae336d499
5 changed files with 48 additions and 15 deletions

View file

@ -25,6 +25,12 @@ all: $(OBJECTS)
@$(CC) $(CFLAGS) -S -masm=intel $<
wc -l $@
driver: $(OBJECTS)
@$(CC) $(CFLAGS) $^ -o $@
run: driver
@./driver
clean:
rm -f $(OBJECTS) $(ASMS)

BIN
driver Executable file

Binary file not shown.

19
driver.c Normal file
View file

@ -0,0 +1,19 @@
/* SPDX-License-Identifier: MIT */
#include <stdlib.h>
#include <string.h>
#define rb_size_t size_t
#include "ringbuf.h"
int main(void) {
struct RingBuf rb;
rb_init(&rb, 10, malloc, sizeof(int));
int data[] = {1, 2, 3, 4, 5};
rb_push(&rb, (void **)data, 5, memcpy);
// rb_destroy(&rb, free);
return 0;
}

View file

@ -2,8 +2,8 @@
#include "ringbuf.h"
void rb_init(struct RingBuf *rb, int capacity, void *(*alloc)(int),
int struct_size) {
void rb_init(struct RingBuf *rb, rb_size_t capacity, void *(*alloc)(rb_size_t),
rb_size_t struct_size) {
rb->struct_size = struct_size;
rb->capacity = capacity;
rb->write_idx = 0;
@ -11,15 +11,17 @@ void rb_init(struct RingBuf *rb, int capacity, void *(*alloc)(int),
rb->buffer = alloc(capacity * struct_size); /* Calloc? */
}
void rb_destroy(struct RingBuf *rb, int(free)(void *)) { free(rb->buffer); }
void rb_destroy(struct RingBuf *rb, rb_size_t(free)(void *)) {
free(rb->buffer);
}
enum WriteResult rb_push(struct RingBuf *rb, void *data[], int amount,
int (*memcpy)(void *, const void *, int)) {
enum WriteResult rb_push(struct RingBuf *rb, void *data[], rb_size_t amount,
void *(*memcpy)(void *, const void *, rb_size_t)) {
if (rb->write_idx + amount >= rb->capacity) {
return CollisionError;
}
for (int i = 0; i < amount; i++) {
for (rb_size_t i = 0; i < amount; i++) {
memcpy(rb->buffer + rb->write_idx * rb->struct_size, data[i],
rb->struct_size);
rb->write_idx = (rb->write_idx + 1) % rb->capacity;

View file

@ -1,25 +1,31 @@
/* SPDX-License-Identifier: MIT */
#pragma once
#ifndef rb_size_t
#define rb_size_t int
#endif
/**
* Ring buffer, also known as circular buffer.
*/
struct RingBuf {
int struct_size; /* Size of the struct */
int capacity; /* The physical capacity of the entire ringbuf */
int write_idx; /* The write head */
int read_idx; /* THe read head */
void **buffer; /* The actual data */
rb_size_t struct_size; /* Size of the struct */
rb_size_t capacity; /* The physical capacity of the entire ringbuf */
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, int capacity, void *(*alloc)(int),
int struct_size);
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[], int amount,
int (*memcpy)(void *, const void *, int));
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, int amount);