Small general purpose allocator

This commit is contained in:
Nils O. Selåsdal
2026-04-08 22:11:18 +02:00
parent a4547f6736
commit 88e9f95988
+162
View File
@@ -0,0 +1,162 @@
#include <stdint.h>
#include <stddef.h>
#include <stdint.h>
#include <stddef.h>
#define ALIGNMENT 16
#define ALIGN(n) (((n) + (ALIGNMENT - 1)) & ~(ALIGNMENT - 1))
#define BLOCK_FREE ((size_t)1)
typedef struct BlockHeader {
size_t size_and_flags; // size | free_bit
struct BlockHeader* next;
} BlockHeader;
//Note:
// On 64bit systems, allocated pointers are 16byte aligned.
// On 32bit systems, allocated pointers are 8byte aligned.
_Static_assert(sizeof(BlockHeader) % ALIGNMENT == 0,
"BlockHeader size must be a multiple of ALIGNMENT");
typedef struct {
uint8_t* base; // backing store
size_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) {
return b->size_and_flags & ~BLOCK_FREE;
}
static inline int block_is_free(BlockHeader* b) {
return (b->size_and_flags & BLOCK_FREE) != 0;
}
static inline void block_set_free(BlockHeader* b) {
b->size_and_flags |= BLOCK_FREE;
}
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) {
size_t flags = b->size_and_flags & BLOCK_FREE;
b->size_and_flags = size | flags;
}
/* --- Allocator initialization --- */
void small_allocator_init(SmallAllocator* a, void* backing_store, size_t size) {
a->base = (uint8_t*)backing_store;
a->size = size;
BlockHeader* first = (BlockHeader*)a->base;
size_t usable = size - sizeof(BlockHeader);
usable = ALIGN(usable);
first->size_and_flags = usable | BLOCK_FREE;
first->next = NULL;
a->heap_start = first;
a->last_alloc = first;
}
/* --- Block splitting --- */
static void split_block(BlockHeader* block, size_t size) {
size = ALIGN(size);
size_t bsize = block_size(block);
if (bsize < size + sizeof(BlockHeader) + ALIGNMENT)
return;
uint8_t* base = (uint8_t*)block;
BlockHeader* new_block =
(BlockHeader*)(base + sizeof(BlockHeader) + size);
size_t new_size = bsize - size - sizeof(BlockHeader);
new_size = ALIGN(new_size);
block_set_size(block, size);
block_set_used(block);
new_block->size_and_flags = new_size | BLOCK_FREE;
new_block->next = block->next;
block->next = new_block;
}
/* --- Adjacent-only coalescing --- */
static void merge_with_next(BlockHeader* block) {
BlockHeader* next = block->next;
if (!next || !block_is_free(next))
return;
size_t new_size = block_size(block) +
sizeof(BlockHeader) +
block_size(next);
new_size = ALIGN(new_size);
block_set_size(block, new_size);
block->next = next->next;
}
/* --- Allocation (next-fit) --- */
void* small_alloc(SmallAllocator* a, size_t size) {
if (size == 0)
return NULL;
size = ALIGN(size);
BlockHeader* start = a->last_alloc ? a->last_alloc : a->heap_start;
BlockHeader* curr = start;
do {
if (block_is_free(curr) && block_size(curr) >= size) {
split_block(curr, size);
block_set_used(curr);
a->last_alloc = curr;
return (uint8_t*)curr + sizeof(BlockHeader);
}
curr = curr->next ? curr->next : a->heap_start;
} while (curr != start);
return NULL;
}
/* --- Free --- */
void small_free(SmallAllocator* a, void* ptr) {
if (!ptr)
return;
BlockHeader* block =
(BlockHeader*)((uint8_t*)ptr - sizeof(BlockHeader));
block_set_free(block);
merge_with_next(block);
BlockHeader* prev = NULL;
BlockHeader* curr = a->heap_start;
while (curr && curr != block) {
prev = curr;
curr = curr->next;
}
if (prev && block_is_free(prev)) {
merge_with_next(prev);
if (a->last_alloc == block)
a->last_alloc = prev;
}
}