Debug print macro and makefile changes related to asm/size

This commit is contained in:
Imbus 2024-06-30 20:31:52 +02:00
parent ec6cc83bd1
commit d5922a0091
2 changed files with 23 additions and 8 deletions

View file

@ -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)

View file

@ -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)
@ -59,6 +69,7 @@ rb_pop_front(struct RingBuf *rb, void *item)
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)