diff --git a/Makefile b/Makefile index e117a77..8bc2486 100644 --- a/Makefile +++ b/Makefile @@ -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) diff --git a/driver b/driver new file mode 100755 index 0000000..bf016f1 Binary files /dev/null and b/driver differ diff --git a/driver.c b/driver.c new file mode 100644 index 0000000..2bfd934 --- /dev/null +++ b/driver.c @@ -0,0 +1,19 @@ +/* SPDX-License-Identifier: MIT */ + +#include +#include + +#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; +} \ No newline at end of file diff --git a/ringbuf.c b/ringbuf.c index 375b5d5..bf9b85b 100644 --- a/ringbuf.c +++ b/ringbuf.c @@ -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; diff --git a/ringbuf.h b/ringbuf.h index 94c5795..9c28559 100644 --- a/ringbuf.h +++ b/ringbuf.h @@ -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); \ No newline at end of file