Shrink block header, restrict allocator to 4Gb

This commit is contained in:
Nils O. Selåsdal
2026-04-09 15:27:40 +02:00
parent 1b61b3c6d0
commit 0ca7f64c83
+106 -38
View File
@@ -6,27 +6,31 @@
/** /**
* General purpose allocator. * General purpose allocator.
* - Allocated pointers are aligned to 16 bytes. * - Allocated pointers are aligned to 16 bytes.
* - An allocation "wastes" 32 bytes for housekeeping * - Backing buffer must be aligned to 16 bytes start
* - Aallocation of > 0 bytes returns 16 bytes as a minimum * - 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 * - Allocation of 0 bytes returns a valid pointer with 0 usable space
* *
* The allocator maintains a doubly linked list of blocks, * The allocator maintains a doubly linked list of blocks,
* when blocks are free'd, adjacent blocks are merged to keep * when blocks are free'd, adjacent blocks are merged to keep
* fragmentation to a minimum * fragmentation to a minimum
*/ */
#define ALIGNMENT 16 #define ALIGNMENT 16
#define ALIGN(n) (((n) + (ALIGNMENT - 1)) & ~(ALIGNMENT - 1)) #define ALIGN(n) (((n) + (ALIGNMENT - 1)) & ~(ALIGNMENT - 1))
#define NULL_BLOCK (0xffffffff)
#define BLOCK_FREE ((size_t)1) #define BLOCK_FREE ((uint32_t)1)
typedef struct BlockHeader { typedef struct BlockHeader {
size_t size_and_flags; // size | free_bit uint32_t next; // offset from base
struct BlockHeader* next; uint32_t prev; // offset from base
struct BlockHeader* prev; uint32_t size_and_flags; // size | free_bit
// Padding to ensure we always return 16 byte aligned pointers. // Padding to ensure we always return 16 byte aligned pointers.
// Remove padding & set ALIGNMENT to 8 to get 8 byte alignmet // Remove padding & set ALIGNMENT to 8 to get 8 byte alignmet
// (for 64 bit systems, 32 bit systems might need other adjustments) // (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; } BlockHeader;
@@ -36,14 +40,14 @@ _Static_assert(sizeof(BlockHeader) % ALIGNMENT == 0,
typedef struct { typedef struct {
uint8_t* base; // backing store uint8_t* base; // backing store
size_t size; // total size uint32_t size; // total size
BlockHeader* heap_start; // first block BlockHeader* heap_start; // first block
BlockHeader* last_alloc; // next-fit pointer BlockHeader* last_alloc; // next-fit pointer
} SmallAllocator; } SmallAllocator;
/* --- Header helpers --- */ /* --- 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; 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; 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; 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 // backing store must be aligned, otherwise all alignment guarantees are lies
assert(((uintptr_t)backing_store & (ALIGNMENT - 1)) == 0); assert(((uintptr_t)backing_store & (ALIGNMENT - 1)) == 0);
assert(size >= sizeof(BlockHeader)); assert(size >= sizeof(BlockHeader));
assert(size < 0xfffffff0);
a->base = backing_store; a->base = backing_store;
a->size = size; a->size = size;
BlockHeader* first = backing_store; BlockHeader* first = backing_store;
size_t usable = ALIGN(size - sizeof(BlockHeader)); uint32_t usable = ALIGN(size - sizeof(BlockHeader));
block_set_free(first); block_set_free(first);
block_set_size(first, usable); block_set_size(first, usable);
first->next = NULL; first->next = NULL_BLOCK;
first->prev = NULL; first->prev = NULL_BLOCK;
a->heap_start = first; a->heap_start = first;
a->last_alloc = 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 = ALIGN(size);
size_t bsize = block_size(block); uint32_t bsize = block_size(block);
if (bsize < size + sizeof(BlockHeader) + ALIGNMENT) if (bsize < size + sizeof(BlockHeader) + ALIGNMENT)
return; return;
@@ -101,39 +125,42 @@ static void split_block(BlockHeader* block, size_t size)
BlockHeader* new_block = BlockHeader* new_block =
(BlockHeader*)(base + sizeof(BlockHeader) + size); (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_size(block, size);
block_set_used(block); block_set_used(block);
new_block->size_and_flags = new_size | BLOCK_FREE; new_block->size_and_flags = new_size | BLOCK_FREE;
new_block->next = block->next; new_block->next = block->next;
new_block->prev = block; new_block->prev = bh_offset(a, block);
if (block->next) BlockHeader *next = bh_from_offset(a, new_block->next);
block->next->prev = new_block; 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)) if (!next || !block_is_free(next))
return; return;
size_t new_size = ALIGN(block_size(block) + uint32_t new_size = ALIGN(block_size(block) +
sizeof(BlockHeader) + sizeof(BlockHeader) +
block_size(next)); block_size(next));
block_set_size(block, new_size); block_set_size(block, new_size);
block->next = next->next; block->next = next->next;
BlockHeader *nextnext = bh_from_offset(a, next->next);
if (next->next) if (nextnext)
next->next->prev = block; 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); size = ALIGN(size);
@@ -142,12 +169,12 @@ void* small_alloc(SmallAllocator* a, size_t size)
do { do {
if (block_is_free(curr) && block_size(curr) >= size) { if (block_is_free(curr) && block_size(curr) >= size) {
split_block(curr, size); split_block(a, curr, size);
block_set_used(curr); block_set_used(curr);
a->last_alloc = curr; a->last_alloc = curr;
return (uint8_t*)curr + sizeof(BlockHeader); 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); } while (curr != start);
return NULL; return NULL;
@@ -161,7 +188,6 @@ void small_free(SmallAllocator* a, void* ptr)
return; return;
} }
BlockHeader* block = (BlockHeader*)((uint8_t*)ptr - sizeof(BlockHeader)); BlockHeader* block = (BlockHeader*)((uint8_t*)ptr - sizeof(BlockHeader));
assert((uint8_t*)ptr >= a->base && (uint8_t*)ptr < a->base + a->size); 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)); assert(!block_is_free(block));
block_set_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 = bh_from_offset(a, block->prev);
BlockHeader* prev = block->prev; if (prev != NULL && block_is_free(prev)) {
merge_with_next(prev); merge_with_next(a, prev);
if (a->last_alloc == block) if (a->last_alloc == block) {
a->last_alloc = prev; a->last_alloc = prev;
}
} }
} }
#if 0
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
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