From 46a341e5371ffd87df6fc9be3b2312eb387e74d9 Mon Sep 17 00:00:00 2001 From: Imbus <> Date: Sun, 30 Jun 2024 05:08:42 +0200 Subject: [PATCH] Cleaning --- driver.c | 22 +++++----------------- ringbuf.c | 41 +++++++++++------------------------------ ringbuf.h | 12 +++--------- 3 files changed, 19 insertions(+), 56 deletions(-) diff --git a/driver.c b/driver.c index 51e2af2..2350cc4 100644 --- a/driver.c +++ b/driver.c @@ -13,26 +13,14 @@ main(void) struct RingBuf rb; rb_init(&rb, 10, malloc, sizeof(int)); - int data[] = { 5, 6, 7, 8 }; - printf("Address at data first element: %p\n", &data[0]); - printf("Address at internal first element: %p\n", &rb.buffer); - // rb_push(&rb, data, 4, memcpy); - rb_push_back(&rb, data, memcpy); + int data = 5; - // Print addresses of data - for(int i = 0; i < 4; i++) { - printf("Address: %u\n", &data[i]); - } + rb_push_back(&rb, &data, memcpy); - print_seq(data, 4); - print_seq(rb.buffer, 4); + int d; + rb_pop_front(&rb, &d); - int dest[5]; - // rb_read(&rb, (void *)dest, 5); - - // for (int i = 0; i < 5; i++) { - // printf("Data: %d\n", dest[i]); - // } + printf("Data: %d\n", d); rb_destroy(&rb, free); diff --git a/ringbuf.c b/ringbuf.c index 4b6c50e..dbe9142 100644 --- a/ringbuf.c +++ b/ringbuf.c @@ -12,8 +12,6 @@ rb_init(struct RingBuf *rb, rb_size_t capacity, ALLOC_T malloc_fn, { rb->struct_size = struct_size; rb->capacity = capacity; - rb->write_idx = 0; - rb->read_idx = 0; rb->buffer = malloc_fn(capacity * struct_size); /* Calloc? */ rb->buffer_end = rb->buffer + (capacity * struct_size); @@ -36,9 +34,9 @@ rb_destroy(struct RingBuf *rb, void(free)()) enum WriteResult rb_push_back(struct RingBuf *rb, const void *item, MEMCPY_T memcpy_fn) { - // if(rb->count == rb->capacity){ - // // handle error - // } + if(rb->count == rb->capacity) { + // handle error + } memcpy_fn(rb->write_head, item, rb->size); @@ -50,32 +48,15 @@ rb_push_back(struct RingBuf *rb, const void *item, MEMCPY_T memcpy_fn) rb->count++; } -// void rb_pop_front(struct RingBuf *rb, void *item) -// { -// if(cb->count == 0){ -// // handle error -// } -// memcpy(item, cb->tail, cb->sz); -// cb->tail = (char*)cb->tail + cb->sz; -// if(cb->tail == cb->buffer_end) -// cb->tail = cb->buffer; -// cb->count--; -// } - void -rb_read(struct RingBuf *rb, void *dest, int amount) +rb_pop_front(struct RingBuf *rb, void *item) { - printf("\nReading %d elements\n", amount); - for(int i = 0; i < amount; i++) { - } -} - -void -print_seq(void *begin, int amount) -{ - printf("\nPrinting sequence\n"); - for(int i = 0; i < amount; i++) { - int *val = (int *)(begin + 4 * i); - printf("V: %d, A: %u\n", *val, val); + if(rb->count == 0) { + // handle error } + memcpy(item, rb->read_head, rb->size); + rb->read_head = (char *)rb->read_head + rb->size; + if(rb->read_head == rb->buffer_end) + rb->read_head = rb->buffer; + rb->count--; } diff --git a/ringbuf.h b/ringbuf.h index be4a7db..aaea871 100644 --- a/ringbuf.h +++ b/ringbuf.h @@ -17,10 +17,9 @@ struct RingBuf { rb_size_t struct_size; /* Size of the struct */ rb_size_t capacity; /* The physical capacity of the entire ringbuf */ rb_size_t size; /* The current size of the ring buffer */ - void *write_head; /* Address of the write head */ - rb_size_t write_idx; /* The write head */ - rb_size_t read_idx; /* THe read head */ rb_size_t count; /* The number of elements in the buffer */ + void *write_head; /* Address of the write head */ + void *read_head; /* Address of the read head */ void *buffer; /* The actual data */ void *buffer_end; /* The end of the buffer */ } __attribute__((packed)); @@ -32,16 +31,11 @@ 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, rb_size_t amount, - void *(*memcpy)(void *, const void *, rb_size_t)); - enum WriteResult rb_push_back(struct RingBuf *rb, const void *item, MEMCPY_T memcpy_fn); /** Read data from the ring buffer */ -void rb_read(struct RingBuf *rb, void *dest, int amount); +void rb_pop_front(struct RingBuf *rb, void *item); /** Free the ring buffer */ void rb_destroy(struct RingBuf *rb, void(free)()); - -void print_seq(void *begin, int amount);