BUG: Walking logic in check() is broken. Moving aligning into source, removing macros

This commit is contained in:
Imbus 2025-09-09 08:47:26 +02:00
parent d608a81674
commit 04c8311528
6 changed files with 115 additions and 46 deletions

View file

@ -6,19 +6,27 @@
#include <stdlib.h>
#include <string.h>
struct FreeListBlock {
struct __attribute__((packed)) FreeListBlock {
struct FreeListBlock *next;
};
/* Initialize the FreeList */
int fl_init(FreeList *fl, uintptr_t start, uintptr_t end, size_t itemsize) {
size_t size = ALIGN(itemsize);
/* Align to nearest multiple of align */
static inline size_t align_up_to(size_t n, size_t align) {
return (n + align - 1) & ~(align - 1);
}
if (!fl || end <= start)
return EXIT_FAILURE;
/* Initialize the FreeList */
int fl_init(FreeList *fl, uintptr_t start, size_t size_bytes, size_t itemsize) {
/* Fiddle around according to your need, (void *) alignment seems to be enough,
* but MAX_ALIGN_T is also an option. Especially for allocator implementation. */
size_t size = align_up_to(itemsize, sizeof(void *));
if (size < sizeof(FreeListBlock) || !fl) {
return 0;
}
fl->start = start;
fl->end = end;
fl->end = start + size_bytes;
fl->size = size;
fl->allocated = 0;
@ -28,10 +36,10 @@ int fl_init(FreeList *fl, uintptr_t start, uintptr_t end, size_t itemsize) {
block = block->next;
}
block->next = NULL;
block->next = NULL; /* Last block */
fl->free = (FreeListBlock *)start;
return EXIT_SUCCESS;
return 1;
}
/* Allocate some memory from the FreeList */
@ -41,7 +49,7 @@ void *fl_alloc(FreeList *fl) {
FreeListBlock *m = fl->free;
fl->free = fl->free->next;
fl->free = fl->free->next; /* May be null, which is fine */
fl->allocated++;
memset((void *)m, 0, sizeof(FreeListBlock));
@ -50,17 +58,16 @@ void *fl_alloc(FreeList *fl) {
/* Return some memory to the FreeList */
int fl_free(FreeList *fl, void *ptr) {
if (!fl_is_managed(fl, ptr)) {
return EXIT_FAILURE; /* We cant free memory we do not own */
}
if (!fl_is_managed(fl, ptr))
return 0; /* We cant free memory we do not own */
FreeListBlock *block = (FreeListBlock *)(uintptr_t)ptr;
FreeListBlock *block = (FreeListBlock *)ptr;
block->next = fl->free;
block->next = fl->free; /* May be null, which is fine */
fl->free = block;
fl->allocated--;
return EXIT_SUCCESS;
return 1;
}
/* Returns how many slots are occupied */
@ -80,7 +87,13 @@ size_t fl_capacity(FreeList *fl) {
/* Check if a piece of memory is managed by the FreeList */
int fl_is_managed(FreeList *fl, void *ptr) {
return ((uintptr_t)ptr >= fl->start && (uintptr_t)ptr < fl->end);
uintptr_t p = (uintptr_t)ptr;
if (p < fl->start || p >= fl->end) {
return 0; // outside pool
}
return ((p - fl->start) % fl->size) == 0; // aligned to block
}
/* Returns the ratio of metadata versus data as a scalar in range 0..1 */
@ -94,8 +107,16 @@ size_t fl_check(FreeList *fl) {
FreeListBlock *cursor = fl->free;
while (cursor->next != NULL) {
avail++;
cursor = cursor->next;
if (!fl_is_managed(fl, cursor)) {
printf("Unused memory at: %zX\n", (size_t)cursor->next);
printf("Min memory at: %zX\n", (size_t)fl->start);
printf("Max memory at: %zX\n", (size_t)fl->end);
return 0;
}
avail++;
}
return avail;