From be3d902be5895a13b34b88c7521ba6106e806cba Mon Sep 17 00:00:00 2001 From: Imbus <> Date: Mon, 8 Sep 2025 07:43:15 +0200 Subject: [PATCH 1/4] freelist: Move align macro fiddling into header --- freelist/freelist.c | 13 ------------- freelist/freelist.h | 13 +++++++++++++ 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/freelist/freelist.c b/freelist/freelist.c index 9b47a8d..7fd946e 100644 --- a/freelist/freelist.c +++ b/freelist/freelist.c @@ -5,19 +5,6 @@ #include #include -/* Fiddle these around according to your need. */ -#ifdef FREELIST_NOALIGN -#define ALIGN(x) (x) -#else // FREELIST_NOALIGN - -/* Align to nearest multiple of sizeof(void*) */ -static inline size_t align_up(size_t n) { - return (n + sizeof(void *) - 1) & ~(sizeof(void *) - 1); -} - -#define ALIGN(x) (align_up(x)) -#endif // FREELIST_NOALIGN - /* Initialize the FreeList */ int fl_init(FreeList *fl, uintptr_t start, uintptr_t end, size_t itemsize) { size_t size = ALIGN(itemsize + sizeof(FreeListBlock)); diff --git a/freelist/freelist.h b/freelist/freelist.h index de12d5d..f0e8ed4 100644 --- a/freelist/freelist.h +++ b/freelist/freelist.h @@ -5,6 +5,19 @@ #include #include +/* Fiddle these around according to your need. Delete or check makefile */ +#ifdef FREELIST_NOALIGN +#define ALIGN(x) (x) +#else // FREELIST_NOALIGN + +/* Align to nearest multiple of sizeof(void*) */ +static inline size_t align_up(size_t n) { + return (n + sizeof(void *) - 1) & ~(sizeof(void *) - 1); +} + +#define ALIGN(x) (align_up(x)) +#endif // FREELIST_NOALIGN + #define FL_FREE ((uint8_t)0x00) #define FL_USED ((uint8_t)0x01) From 8f41b0873dc565e5159b583fff950fa436e1b8cb Mon Sep 17 00:00:00 2001 From: Imbus <> Date: Mon, 8 Sep 2025 07:43:22 +0200 Subject: [PATCH 2/4] Disable align in makefile --- freelist/Makefile | 2 ++ 1 file changed, 2 insertions(+) diff --git a/freelist/Makefile b/freelist/Makefile index 7f6d8a6..9d85953 100644 --- a/freelist/Makefile +++ b/freelist/Makefile @@ -1,6 +1,8 @@ CC = gcc CFLAGS = -Wall -O2 +CFLAGS += -DFREELIST_NOALIGN + TARGET = main.elf SRC = main.c freelist.c From 13a0d42514836a580d61b882962c044dd5bd094d Mon Sep 17 00:00:00 2001 From: Imbus <> Date: Mon, 8 Sep 2025 07:52:41 +0200 Subject: [PATCH 3/4] Remove block header from allocated blocks entirely --- freelist/freelist.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/freelist/freelist.c b/freelist/freelist.c index 7fd946e..380010f 100644 --- a/freelist/freelist.c +++ b/freelist/freelist.c @@ -4,10 +4,11 @@ #include #include #include +#include /* Initialize the FreeList */ int fl_init(FreeList *fl, uintptr_t start, uintptr_t end, size_t itemsize) { - size_t size = ALIGN(itemsize + sizeof(FreeListBlock)); + size_t size = ALIGN(itemsize); if (!fl || end <= start) return EXIT_FAILURE; @@ -39,7 +40,8 @@ void *fl_alloc(FreeList *fl) { fl->free = fl->free->next; fl->allocated++; - return ((void *)m) + sizeof(FreeListBlock); + memset((void *)m, 0, sizeof(FreeListBlock)); + return ((void *)m); } /* Return some memory to the FreeList */ @@ -48,7 +50,7 @@ int fl_free(FreeList *fl, void *ptr) { return EXIT_FAILURE; /* We cant free memory we do not own */ } - FreeListBlock *block = (FreeListBlock *)((uintptr_t)ptr - sizeof(FreeListBlock)); + FreeListBlock *block = (FreeListBlock *)(uintptr_t)ptr; block->next = fl->free; fl->free = block; From a8b7fc75e3a68e6cef133a1f4c371f15e2b5a689 Mon Sep 17 00:00:00 2001 From: Imbus <> Date: Mon, 8 Sep 2025 07:52:54 +0200 Subject: [PATCH 4/4] Some more info in demo main --- freelist/main.c | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/freelist/main.c b/freelist/main.c index 0841d2e..49aa19b 100644 --- a/freelist/main.c +++ b/freelist/main.c @@ -17,16 +17,21 @@ void printvec(const Vec3 *v) { printf("Vec3: (%d, %d, %d)\n", v->x, v->y, v->z); } +#define BUFFER_SIZE (4096) + int main() { - FreeList fl; - char *mem = malloc(4096); + FreeList fl; + const char *mem = malloc(BUFFER_SIZE); + + fl_init(&fl, (uintptr_t)mem, (uintptr_t)mem + BUFFER_SIZE, sizeof(Vec3)); + const size_t cap = fl_capacity(&fl); + + printf("Item size: %lu\n", sizeof(Vec3)); + printf("Buffer size: %d\n", BUFFER_SIZE); + printf("Space utilization internal: %.2f%%\n", 100.0 * fl_utilization(&fl, sizeof(Vec3))); - fl_init(&fl, (uintptr_t)mem, (uintptr_t)mem + 4096, sizeof(Vec3)); - size_t cap = fl_capacity(&fl); assert(fl_available(&fl) == cap); - printf("Space utilization: %.2f%%\n", 100.0 * fl_utilization(&fl, sizeof(Vec3))); - Vec3 *a = fl_alloc(&fl); Vec3 *b = fl_alloc(&fl); Vec3 *c = fl_alloc(&fl);