From 5fed6d24d8b33dbc0ca4e47d4c902d555d3e239a Mon Sep 17 00:00:00 2001 From: Ivar Fatland Date: Fri, 3 Oct 2025 20:21:27 +0200 Subject: [PATCH] std_allocator and testing --- allocator.h | 25 +------------------------ makefile | 24 ++++++++++++++++++++++++ std_allocator.c | 32 ++++++++++++++++++++++++++++++++ test_std_allocator.c | 14 ++++++++++++++ 4 files changed, 71 insertions(+), 24 deletions(-) create mode 100644 makefile create mode 100644 std_allocator.c create mode 100644 test_std_allocator.c diff --git a/allocator.h b/allocator.h index 5b63d77..5644d4f 100644 --- a/allocator.h +++ b/allocator.h @@ -31,8 +31,6 @@ extern const allocator_t allocator_stdlib; #ifdef ALLOCATOR_IMPLEMENTATION -// convenience functions /////////////////////////////////////////////////////// - void *allocator_alloc_func(allocator_t this, size_t bytes, const char *file, int line) { return this.vtbl->alloc(this.this, bytes, file, line); } @@ -43,28 +41,7 @@ void allocator_free_func(allocator_t this, void *ptr, const char *file, int line this.vtbl->free(this.this, ptr, file, line); } -// allocator_stdlib //////////////////////////////////////////////////////////// - -static void *stdlib_alloc(void *_, size_t bytes, const char *_, int _) { - return malloc(bytes); -} -static void *stdlib_resize(void *_, void *old_ptr, size_t bytes, const char *_, int _) { - return realloc(old_ptr, bytes); -} -static void stdlib_free(void *_, void *ptr, const char *_, int _) { - free(ptr); -} - -static const allocator_vtbl_t stdlib_vtbl = { - .alloc = stdlib_alloc, - .resize = stdlib_resize, - .free = stdlib_free, -}; - -const allocator_t allocator_stdlib = (allocator_t) { - .this=NULL, - .vtbl=&stdlib_vtbl, -}; +#include "std_allocator.c" #endif // ALLOCATOR_IMPLEMENTATION #endif // ALLOCATOR_H diff --git a/makefile b/makefile new file mode 100644 index 0000000..0e1d55d --- /dev/null +++ b/makefile @@ -0,0 +1,24 @@ +# Makefile + +# Compiler and flags +CC := gcc +CFLAGS := -Wall -Wextra -O2 +LDFLAGS := -lcriterion + +# Output binary +TESTBIN := /tmp/all_tests + +# Phony target +.PHONY: test + +test: + @echo "Discovering test files..." + @files=$$(find . -type f -name 'test*.c'); \ + if [ -z "$$files" ]; then \ + echo "No test files found!"; \ + else \ + echo "Compiling all test files into $(TESTBIN)..."; \ + $(CC) $(CFLAGS) $$files -o $(TESTBIN) $(LDFLAGS); \ + echo "Running tests..."; \ + $(TESTBIN); \ + fi diff --git a/std_allocator.c b/std_allocator.c new file mode 100644 index 0000000..03d6386 --- /dev/null +++ b/std_allocator.c @@ -0,0 +1,32 @@ +#include "allocator.h" + +static void *stdlib_alloc(void *this, size_t bytes, const char *file, int line) { + (void)this; + (void)file; + (void)line; + return malloc(bytes); +} +static void *stdlib_resize(void *this, void *old_ptr, size_t bytes, const char *file, int line) { + (void)this; + (void)file; + (void)line; + return realloc(old_ptr, bytes); +} +static void stdlib_free(void *this, void *ptr, const char *file, int line) { + (void)this; + (void)file; + (void)line; + free(ptr); +} + +static const allocator_vtbl_t stdlib_vtbl = { + .alloc = stdlib_alloc, + .resize = stdlib_resize, + .free = stdlib_free, +}; + +const allocator_t allocator_stdlib = (allocator_t) { + .this=NULL, + .vtbl=&stdlib_vtbl, +}; + diff --git a/test_std_allocator.c b/test_std_allocator.c new file mode 100644 index 0000000..cfde117 --- /dev/null +++ b/test_std_allocator.c @@ -0,0 +1,14 @@ +#include +#define ALLOCATOR_IMPLEMENTATION +#include "allocator.h" + +Test(std_allocator, test) { + allocator_t this = allocator_stdlib; + void *ptr = allocator_alloc(this, 10); + cr_assert(ptr != ((void *)0), "non null from malloc"); + void *new_ptr = allocator_resize(this, ptr, 1024*1024*500); + cr_assert(new_ptr != ((void *)0), "non null from realloc"); + cr_assert(new_ptr != ptr, "realloc is not the same ptr as malloc"); + + allocator_free(this, new_ptr); +}