From 6b12606d8217048ff21435faef667bf10468cac1 Mon Sep 17 00:00:00 2001 From: Imbus <> Date: Tue, 9 Sep 2025 11:11:28 +0200 Subject: [PATCH] Freelist that seems to behave --- kern/libkern/freelist.c | 99 ++++++++++++++++++++++++++++------------- kern/libkern/freelist.h | 14 +----- 2 files changed, 69 insertions(+), 44 deletions(-) diff --git a/kern/libkern/freelist.c b/kern/libkern/freelist.c index f9dc531..35ffa41 100644 --- a/kern/libkern/freelist.c +++ b/kern/libkern/freelist.c @@ -1,37 +1,55 @@ #include "freelist.h" -#include "stddef.h" -#include -#include -#include -#include -#include -struct FreeListBlock { - struct FreeListBlock *next; +/* Header block used to keep track of all the free blocks */ +struct __attribute__((packed)) FreeListBlock { + uint16_t next_offset; /* May be 8, 16, 32, 64, whatever */ }; -/* 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; +/* Convert pointer -> 1-based offset (0 means NULL) */ +static inline uint32_t ptr_to_offset(FreeList *fl, void *ptr) { + if (!ptr) + return 0; // NULL maps to 0 + uintptr_t diff = (uintptr_t)ptr - fl->start; + return (uint32_t)(diff / fl->size) + 1; +} + +/* Convert 1-based offset -> pointer (0 means NULL) */ +static inline void *offset_to_ptr(FreeList *fl, uint32_t offset) { + if (offset == 0) + return NULL; // 0 = invalid/null + return (void *)(fl->start + (uintptr_t)(offset - 1) * fl->size); +} + +/* 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; FreeListBlock *block = (FreeListBlock *)start; - for (size_t i = 0; i < fl_capacity(fl); i++) { - block->next = (FreeListBlock *)((void *)block + size); - block = block->next; + + for (size_t offset = 1; offset < fl_capacity(fl) + 1; offset++) { + block->next_offset = (int32_t)offset; + block = offset_to_ptr(fl, offset); } - block->next = NULL; - + block->next_offset = 0; /* Last block */ fl->free = (FreeListBlock *)start; - return EXIT_SUCCESS; + + return 1; } /* Allocate some memory from the FreeList */ @@ -41,26 +59,32 @@ void *fl_alloc(FreeList *fl) { FreeListBlock *m = fl->free; - fl->free = fl->free->next; + fl->free = offset_to_ptr(fl, fl->free->next_offset); /* May be null, which is fine */ fl->allocated++; - memset((void *)m, 0, sizeof(FreeListBlock)); + /* Wipe it before sending it out, could use memset + * here, or even wiping the entire block */ + m->next_offset = 0; + + /* For reference: */ + // memset((void *)m, 0, fl->size); /* Wipe entire block */ + // memset((void *)m, 0, sizeof(FreeListBlock)); /* Wipe only header */ + return ((void *)m); } /* 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_offset = ptr_to_offset(fl, 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 +104,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 */ @@ -93,9 +123,16 @@ size_t fl_check(FreeList *fl) { int avail = 0; FreeListBlock *cursor = fl->free; - while (cursor->next != NULL) { + while (cursor) { avail++; - cursor = cursor->next; + + if (!fl_is_managed(fl, cursor) || avail > fl_capacity(fl)) + return 0; + + if (cursor->next_offset == 0) + break; + + cursor = offset_to_ptr(fl, cursor->next_offset); } return avail; diff --git a/kern/libkern/freelist.h b/kern/libkern/freelist.h index 015ee33..ed7949c 100644 --- a/kern/libkern/freelist.h +++ b/kern/libkern/freelist.h @@ -5,18 +5,6 @@ #include #include -/* 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); -} - -/* Fiddle these around according to your need. Delete or check makefile */ -#ifdef FREELIST_ALIGN -#define ALIGN(x) (align_up_to(x, sizeof(void *))) -#else -#define ALIGN(x) (x) -#endif - typedef struct FreeListBlock FreeListBlock; typedef struct { @@ -27,7 +15,7 @@ typedef struct { size_t allocated; } FreeList; -int fl_init(FreeList *fl, uintptr_t start, uintptr_t end, size_t itemsize); +int fl_init(FreeList *fl, uintptr_t start, size_t size_bytes, size_t itemsize); int fl_free(FreeList *fl, void *ptr); int fl_is_managed(FreeList *fl, void *ptr); void *fl_alloc(FreeList *fl);