diff --git a/.clang-format b/.clang-format index 017c104..38abf14 100644 --- a/.clang-format +++ b/.clang-format @@ -92,7 +92,7 @@ BreakBeforeTernaryOperators: true BreakConstructorInitializers: BeforeColon BreakInheritanceList: BeforeColon BreakStringLiterals: true -ColumnLimit: 120 +ColumnLimit: 80 CommentPragmas: '^ IWYU pragma:' CompactNamespaces: false ConstructorInitializerIndentWidth: 4 @@ -164,7 +164,7 @@ PackConstructorInitializers: BinPack PenaltyBreakAssignment: 2 PenaltyBreakBeforeFirstCallParameter: 19 PenaltyBreakComment: 300 -PenaltyBreakFirstLessLess: 120 +PenaltyBreakFirstLessLess: 80 PenaltyBreakOpenParenthesis: 0 PenaltyBreakScopeResolution: 500 PenaltyBreakString: 1000 diff --git a/Makefile b/Makefile index e4139f7..5853987 100644 --- a/Makefile +++ b/Makefile @@ -1,13 +1,20 @@ # SPDX-License-Identifier: MIT CC = gcc -CFLAGS = -Wall -Wextra -Werror -Wno-unused-parameter -CFLAGS += -Wno-unused-variable -Wno-unused-function -CFLAGS += -Wno-unused-but-set-variable -Wno-unused-value -Wno-unused-label -CFLAGS += -Wno-unused-result -Wno-unused-const-variable -CFLAGS += -fno-exceptions -fno-asynchronous-unwind-tables -fno-ident -CFLAGS += -fno-unwind-tables -fno-stack-protector -fno-plt -fno-pic -CFLAGS += -O3 -std=c99 -march=native -mtune=native -fomit-frame-pointer +# CFLAGS = -Wall -Wextra -Werror -Wno-unused-parameter +# CFLAGS += -Wno-unused-variable -Wno-unused-function +# CFLAGS += -Wno-unused-but-set-variable -Wno-unused-value -Wno-unused-label +# CFLAGS += -Wno-unused-result -Wno-unused-const-variable + +# ifeq ($(RELEASE), 1) +# CFLAGS += -fno-exceptions -fno-asynchronous-unwind-tables -fno-ident +# CFLAGS += -fno-unwind-tables -fno-stack-protector -fno-plt -fno-pic +# CFLAGS += -O3 -std=c99 -march=native -mtune=native -fomit-frame-pointer +# endif + +# Include debug flags +CFLAGS += -g -O0 -std=c99 -march=native -mtune=native + C_SOURCES = $(wildcard *.c) C_HEADERS = $(wildcard *.h) diff --git a/driver.c b/driver.c index 30fbf78..57d0b44 100644 --- a/driver.c +++ b/driver.c @@ -2,6 +2,7 @@ #include #include +#include #define rb_size_t size_t #include "ringbuf.h" @@ -15,7 +16,14 @@ main(void) int data[] = { 1, 2, 3, 4, 5 }; rb_push(&rb, (void **)data, 5, memcpy); - // rb_destroy(&rb, free); + int dest[5]; + rb_read(&rb, (void *)dest, 5); + + for (int i = 0; i < 5; i++) { + printf("Data: %d\n", dest[i]); + } + + rb_destroy(&rb, free); return 0; } diff --git a/ringbuf.c b/ringbuf.c index 6039967..fa93c4c 100644 --- a/ringbuf.c +++ b/ringbuf.c @@ -17,10 +17,19 @@ rb_init(struct RingBuf *rb, rb_size_t capacity, ALLOC_T, rb_size_t struct_size) rb->write_idx = 0; rb->read_idx = 0; rb->buffer = alloc(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, rb_size_t(free)(void *)) +rb_destroy(struct RingBuf *rb, void(free)()) { free(rb->buffer); } @@ -28,28 +37,30 @@ rb_destroy(struct RingBuf *rb, rb_size_t(free)(void *)) enum WriteResult rb_push(struct RingBuf *rb, void *data[], rb_size_t amount, MEMCPY_T) { - if(rb->write_idx + amount >= rb->capacity) { - return CollisionError; - } - - printf("write_idx: %d\n", rb->write_idx); - printf("amount: %d\n", amount); - + printf("\nWriting %d elements\n", amount); for(rb_size_t i = 0; i < amount; i++) { - printf("memcpy: %d, write_idx: %d\n", i, rb->write_idx); - // write to the buffer - rb->buffer[rb->write_idx] = data[i]; - // memcpy(rb->buffer + rb->write_idx * rb->struct_size, data[i], rb->struct_size); + // printf("Index: %d\n", rb->write_idx); + int position = (rb->write_idx + i) % rb->capacity; + int offset = position * rb->struct_size; + printf("Position: %d\n", position); + printf("Offset: %d\n", offset); + memcpy(rb->buffer + offset, data + offset, rb->struct_size); - rb->write_idx = (rb->write_idx + 1) % rb->capacity; - } - - // Read it back to stdout - for(rb_size_t i = 0; i < amount; i++) { - printf("read_idx: %d\n", rb->read_idx); - printf("data: %d\n", *(int *)rb->buffer[rb->read_idx]); - rb->read_idx = (rb->read_idx + 1) % rb->capacity; + printf("Data at location %d: %d\n", position, *(int *)(rb->buffer + offset)); } 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++) { + int position = (rb->read_idx + i) % rb->capacity; + int offset = position * rb->struct_size; + printf("Position: %d\n", position); + printf("Offset: %d\n", offset); + memcpy(dest + offset, rb->buffer + offset, rb->struct_size); + } +} diff --git a/ringbuf.h b/ringbuf.h index 8ef2569..a8c52d8 100644 --- a/ringbuf.h +++ b/ringbuf.h @@ -20,11 +20,15 @@ struct RingBuf { enum WriteResult { CollisionError, Ok }; /** Initialize the ring buffer */ -void rb_init(struct RingBuf *rb, rb_size_t capacity, void *(*alloc)(rb_size_t), rb_size_t 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[], 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); +void rb_read(struct RingBuf *rb, void *dest, int amount); + +/** Free the ring buffer */ +void rb_destroy(struct RingBuf *rb, void(free)());