From 0ca7f64c83dd28986b7eaebd6e370b87aaaf581a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nils=20O=2E=20Sel=C3=A5sdal?= Date: Thu, 9 Apr 2026 15:27:40 +0200 Subject: [PATCH] Shrink block header, restrict allocator to 4Gb --- src/small_allocator.c | 144 +++++++++++++++++++++++++++++++----------- 1 file changed, 106 insertions(+), 38 deletions(-) diff --git a/src/small_allocator.c b/src/small_allocator.c index 5e64353..c92d1c1 100644 --- a/src/small_allocator.c +++ b/src/small_allocator.c @@ -6,27 +6,31 @@ /** * General purpose allocator. * - Allocated pointers are aligned to 16 bytes. - * - An allocation "wastes" 32 bytes for housekeeping - * - Aallocation of > 0 bytes returns 16 bytes as a minimum + * - Backing buffer must be aligned to 16 bytes start + * - 4Gb - 16 is max for the allocator backing buffer. + * - An allocation "wastes" 16 bytes for the block header + * - Allocation of > 0 bytes returns a size aligned up to 16 bytes * - Allocation of 0 bytes returns a valid pointer with 0 usable space * * The allocator maintains a doubly linked list of blocks, * when blocks are free'd, adjacent blocks are merged to keep * fragmentation to a minimum */ + + #define ALIGNMENT 16 #define ALIGN(n) (((n) + (ALIGNMENT - 1)) & ~(ALIGNMENT - 1)) - -#define BLOCK_FREE ((size_t)1) +#define NULL_BLOCK (0xffffffff) +#define BLOCK_FREE ((uint32_t)1) typedef struct BlockHeader { - size_t size_and_flags; // size | free_bit - struct BlockHeader* next; - struct BlockHeader* prev; + uint32_t next; // offset from base + uint32_t prev; // offset from base + uint32_t size_and_flags; // size | free_bit // Padding to ensure we always return 16 byte aligned pointers. // Remove padding & set ALIGNMENT to 8 to get 8 byte alignmet // (for 64 bit systems, 32 bit systems might need other adjustments) - uint8_t pad[32 - (sizeof(size_t) + 2*sizeof(void*))]; + uint8_t pad[4]; } BlockHeader; @@ -36,14 +40,14 @@ _Static_assert(sizeof(BlockHeader) % ALIGNMENT == 0, typedef struct { uint8_t* base; // backing store - size_t size; // total size + uint32_t size; // total size BlockHeader* heap_start; // first block BlockHeader* last_alloc; // next-fit pointer } SmallAllocator; /* --- Header helpers --- */ -static inline size_t block_size(BlockHeader* b) +static inline uint32_t block_size(BlockHeader* b) { return b->size_and_flags & ~BLOCK_FREE; } @@ -63,36 +67,56 @@ static inline void block_set_used(BlockHeader* b) b->size_and_flags &= ~BLOCK_FREE; } -static inline void block_set_size(BlockHeader* b, size_t size) + +static inline void block_set_size(BlockHeader* b, uint32_t size) { - size_t flags = b->size_and_flags & BLOCK_FREE; + uint32_t flags = b->size_and_flags & BLOCK_FREE; b->size_and_flags = size | flags; } -void small_allocator_init(SmallAllocator* a, void* backing_store, size_t size) +static inline BlockHeader * bh_from_offset(const SmallAllocator *a, uint32_t off) +{ + if (off == NULL_BLOCK) { + return NULL; + } + + return (BlockHeader *)((uintptr_t)a->base + off); +} + +static inline uint32_t bh_offset(SmallAllocator *a, BlockHeader *bh) +{ + if (bh == NULL) { + return NULL_BLOCK; + } + + return (uint32_t)((uintptr_t)bh - (uintptr_t)a->base); +} + +void small_allocator_init(SmallAllocator* a, void* backing_store, uint32_t size) { // backing store must be aligned, otherwise all alignment guarantees are lies assert(((uintptr_t)backing_store & (ALIGNMENT - 1)) == 0); assert(size >= sizeof(BlockHeader)); + assert(size < 0xfffffff0); a->base = backing_store; a->size = size; BlockHeader* first = backing_store; - size_t usable = ALIGN(size - sizeof(BlockHeader)); + uint32_t usable = ALIGN(size - sizeof(BlockHeader)); block_set_free(first); block_set_size(first, usable); - first->next = NULL; - first->prev = NULL; + first->next = NULL_BLOCK; + first->prev = NULL_BLOCK; a->heap_start = first; a->last_alloc = first; } -static void split_block(BlockHeader* block, size_t size) +static void split_block(SmallAllocator *a, BlockHeader* block, uint32_t size) { size = ALIGN(size); - size_t bsize = block_size(block); + uint32_t bsize = block_size(block); if (bsize < size + sizeof(BlockHeader) + ALIGNMENT) return; @@ -101,39 +125,42 @@ static void split_block(BlockHeader* block, size_t size) BlockHeader* new_block = (BlockHeader*)(base + sizeof(BlockHeader) + size); - size_t new_size = ALIGN(bsize - size - sizeof(BlockHeader)); + uint32_t new_size = ALIGN(bsize - size - sizeof(BlockHeader)); block_set_size(block, size); block_set_used(block); new_block->size_and_flags = new_size | BLOCK_FREE; new_block->next = block->next; - new_block->prev = block; + new_block->prev = bh_offset(a, block); - if (block->next) - block->next->prev = new_block; + BlockHeader *next = bh_from_offset(a, new_block->next); + if (next) { + next->prev = bh_offset(a, new_block); + //block->next->prev = new_block; + } - block->next = new_block; + block->next = bh_offset(a, new_block); } -static void merge_with_next(BlockHeader* block) +static void merge_with_next(SmallAllocator *a, BlockHeader* block) { - BlockHeader* next = block->next; + BlockHeader* next = bh_from_offset(a, block->next); if (!next || !block_is_free(next)) return; - size_t new_size = ALIGN(block_size(block) + + uint32_t new_size = ALIGN(block_size(block) + sizeof(BlockHeader) + block_size(next)); block_set_size(block, new_size); block->next = next->next; - - if (next->next) - next->next->prev = block; + BlockHeader *nextnext = bh_from_offset(a, next->next); + if (nextnext) + nextnext->prev = bh_offset(a, block); } -void* small_alloc(SmallAllocator* a, size_t size) +void* small_alloc(SmallAllocator* a, uint32_t size) { size = ALIGN(size); @@ -142,12 +169,12 @@ void* small_alloc(SmallAllocator* a, size_t size) do { if (block_is_free(curr) && block_size(curr) >= size) { - split_block(curr, size); + split_block(a, curr, size); block_set_used(curr); a->last_alloc = curr; return (uint8_t*)curr + sizeof(BlockHeader); } - curr = curr->next ? curr->next : a->heap_start; + curr = bh_from_offset(a, curr->next) ? bh_from_offset(a, curr->next) : a->heap_start; } while (curr != start); return NULL; @@ -161,7 +188,6 @@ void small_free(SmallAllocator* a, void* ptr) return; } - BlockHeader* block = (BlockHeader*)((uint8_t*)ptr - sizeof(BlockHeader)); assert((uint8_t*)ptr >= a->base && (uint8_t*)ptr < a->base + a->size); @@ -169,13 +195,55 @@ void small_free(SmallAllocator* a, void* ptr) assert(!block_is_free(block)); block_set_free(block); - merge_with_next(block); + merge_with_next(a, block); - if (block->prev && block_is_free(block->prev)) { - BlockHeader* prev = block->prev; - merge_with_next(prev); + BlockHeader* prev = bh_from_offset(a, block->prev); + if (prev != NULL && block_is_free(prev)) { + merge_with_next(a, prev); - if (a->last_alloc == block) + if (a->last_alloc == block) { a->last_alloc = prev; + } } } + +#if 0 +#include +#include +#include + +void debug_alloc(const SmallAllocator *a) +{ + puts("--debug start--"); + for (BlockHeader *b = a->heap_start ; b ; b = bh_from_offset(a, b->next)) { + printf("Block size %u free %d prev %u next %u \n", block_size(b), block_is_free(b), b->prev, b->next); + } + puts("--debug end--"); +} + +char buff[1024*1024]; +int main(void) +{ + printf("bh size %zu\n", sizeof(BlockHeader)); + #define SZ 64 + SmallAllocator a; + char *ptrs[SZ]; + + small_allocator_init(&a, buff, sizeof buff); + ptrs[0] = small_alloc(&a, 16); + debug_alloc(&a); + small_free(&a, ptrs[0]); + + for (int i = 0; i < SZ; i++) { + ptrs[i] = small_alloc(&a, rand() % 256 + 1); + } + + for (int i = 0; i < SZ; i++) { + // if (rand() % 2 == 0) { + small_free(&a, ptrs[i]); + // } + } + debug_alloc(&a); +} + +#endif