From d5922a00913240d541cc7a3faa397bffcb9856dd Mon Sep 17 00:00:00 2001 From: Imbus <> Date: Sun, 30 Jun 2024 20:31:52 +0200 Subject: [PATCH] Debug print macro and makefile changes related to asm/size --- Makefile | 8 ++++++-- ringbuf.c | 23 +++++++++++++++++------ 2 files changed, 23 insertions(+), 8 deletions(-) diff --git a/Makefile b/Makefile index 6f0a11f..d27a91c 100644 --- a/Makefile +++ b/Makefile @@ -10,6 +10,9 @@ 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 +else +CFLAGS += -g -O0 -std=c99 -march=native -mtune=native +CFLAGS += -DDEBUG endif # Include debug flags @@ -29,7 +32,6 @@ all: $(OBJECTS) %.s: %.c $(C_HEADERS) @echo "CC $<" @$(CC) $(CFLAGS) -S -masm=intel $< - wc -l $@ driver: $(OBJECTS) @$(CC) $(CFLAGS) $^ -o $@ @@ -40,7 +42,9 @@ run: driver clean: rm -f $(OBJECTS) $(ASMS) driver -asm: $(ASMS) +asm: $(ASMS) $(OBJECTS) + wc -l $(ASMS) + size $(OBJECTS) format: clang-format -i $(C_SOURCES) $(C_HEADERS) diff --git a/ringbuf.c b/ringbuf.c index ae79968..bd9c26e 100644 --- a/ringbuf.c +++ b/ringbuf.c @@ -6,6 +6,12 @@ #include "ringbuf.h" +#ifdef DEBUG +#define DEBUG_PRINT(fmt, ...) printf(fmt, __VA_ARGS__) +#else +#define DEBUG_PRINT(fmt, ...) +#endif + void rb_init(struct RingBuf *rb, rb_size_t capacity, ALLOC_T malloc_fn, rb_size_t struct_size) @@ -19,13 +25,16 @@ rb_init(struct RingBuf *rb, rb_size_t capacity, ALLOC_T malloc_fn, rb->read_head = rb->buffer; // 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); + DEBUG_PRINT("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); + DEBUG_PRINT("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); + DEBUG_PRINT( + "Initialized ring buffer. Capacit: %d, struct_size: %d, total: %d\n", + rb->capacity, rb->struct_size, rb->capacity * rb->struct_size); + + DEBUG_PRINT("Size of RB: %lu\n", sizeof(struct RingBuf)); } void @@ -42,6 +51,7 @@ rb_push_back(struct RingBuf *rb, const void *item, MEMCPY_T memcpy_fn) memcpy_fn(rb->write_head, item, rb->struct_size); + // Advance the write head rb->write_head = (char *)rb->write_head + rb->struct_size; if(rb->write_head == rb->buffer_end) @@ -58,7 +68,8 @@ rb_pop_front(struct RingBuf *rb, void *item) return Empty; memcpy(item, rb->read_head, rb->struct_size); - + + // Advance the read head rb->read_head = (char *)rb->read_head + rb->struct_size; if(rb->read_head == rb->buffer_end)