This commit is contained in:
Nils O. Selåsdal
2026-05-11 22:37:01 +02:00
commit c9a51a85ea
14 changed files with 809 additions and 0 deletions
+170
View File
@@ -0,0 +1,170 @@
#include <stdint.h>
#include <stddef.h>
#include <assert.h>
#include "lalloc.h"
#define ALIGNMENT sizeof(BlockHeader)
#define ALIGN_UP(n) (((n) + (__typeof__(n))(ALIGNMENT) - 1) & ~((__typeof__(n))(ALIGNMENT) - 1))
#define ALIGN_DOWN(n) ((n) & ~((__typeof__(n))(ALIGNMENT) - 1))
#define NULL_BLOCK (0xffffffff)
#define BLOCK_FREE ((uint32_t)1)
static inline void block_set_free(BlockHeader *block)
{
block->size_and_flags |= BLOCK_FREE;
}
static inline void block_set_used(BlockHeader *block)
{
block->size_and_flags &= ~BLOCK_FREE;
}
static inline void block_set_size(BlockHeader *block, uint32_t size)
{
uint32_t flags = block->size_and_flags & BLOCK_FREE;
block->size_and_flags = size | flags;
}
uint32_t block_size(const BlockHeader *block)
{
return block->size_and_flags & ~BLOCK_FREE;
}
int block_is_free(const BlockHeader *block)
{
return (block->size_and_flags & BLOCK_FREE) != 0;
}
inline BlockHeader *block_from_offset(const LAlloc *allocator, uint32_t offset)
{
if (offset == NULL_BLOCK) {
return NULL;
}
return (BlockHeader *)((uintptr_t)allocator->base + offset);
}
static inline uint32_t bh_offset(LAlloc *allocator, BlockHeader *block)
{
if (block == NULL) {
return NULL_BLOCK;
}
return (uint32_t)((uintptr_t)block - (uintptr_t)allocator->base);
}
static void maybe_split_block(LAlloc *allocator, BlockHeader *block, uint32_t size)
{
// assumes size is already aligned
uint32_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);
uint32_t new_size = 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 = bh_offset(allocator, block);
BlockHeader *next = block_from_offset(allocator, new_block->next);
if (next) {
next->prev = bh_offset(allocator, new_block);
}
block->next = bh_offset(allocator, new_block);
}
static void merge_with_next(LAlloc *allocator, BlockHeader *block)
{
BlockHeader *next = block_from_offset(allocator, block->next);
if (next == NULL || !block_is_free(next)) return;
uint32_t new_size = block_size(block) + sizeof(BlockHeader) + block_size(next);
block_set_size(block, new_size);
block->next = next->next;
BlockHeader *nextnext = block_from_offset(allocator, next->next);
if (nextnext) nextnext->prev = bh_offset(allocator, block);
// note - caller have to adjust next_alloc on return
}
void lalloc_init(LAlloc *allocator, 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);
allocator->base = backing_store;
allocator->size = size;
BlockHeader *first = backing_store;
// allocator logic requires the first block to be properly aligned
// as well as size being eligned.
uint32_t usable = ALIGN_DOWN(size) - sizeof(BlockHeader);
block_set_free(first);
block_set_size(first, usable);
first->next = NULL_BLOCK;
first->prev = NULL_BLOCK;
allocator->heap_start = first;
allocator->next_alloc = first;
}
void *lalloc(LAlloc *allocator, uint32_t size)
{
if (size > UINT32_MAX - (ALIGNMENT - 1)) {
return NULL;
}
size = ALIGN_UP(size);
BlockHeader *start = allocator->next_alloc ? allocator->next_alloc : allocator->heap_start;
BlockHeader *curr = start;
do {
if (block_is_free(curr) && block_size(curr) >= size) {
maybe_split_block(allocator, curr, size);
block_set_used(curr);
BlockHeader *next = block_from_offset(allocator, curr->next);
allocator->next_alloc = next ? next : allocator->heap_start;
return (uint8_t *)curr + sizeof(BlockHeader);
}
BlockHeader *next = block_from_offset(allocator, curr->next);
curr = next ? next : allocator->heap_start;
} while (curr != start);
return NULL;
}
void lfree(LAlloc *allocator, void *ptr)
{
if (!ptr) {
return;
}
BlockHeader *block = (BlockHeader *)((uint8_t *)ptr - sizeof(BlockHeader));
assert((uint8_t *)ptr - sizeof(BlockHeader) >= allocator->base && (uint8_t *)ptr < allocator->base + allocator->size);
// double free detection
assert(!block_is_free(block));
block_set_free(block);
merge_with_next(allocator, block);
BlockHeader *prev = block_from_offset(allocator, block->prev);
if (prev != NULL && block_is_free(prev)) {
merge_with_next(allocator, prev);
allocator->next_alloc = prev;
} else {
allocator->next_alloc = block;
}
}
+109
View File
@@ -0,0 +1,109 @@
#include <assert.h>
#include <string.h>
#include "lpool.h"
#define ALIGN_UP(n, alignment) (((n) + (__typeof__(n))(alignment) - 1) & ~((__typeof__(n))(alignment) - 1))
void lpool_init(LPool *pool, void *buffer, size_t buffer_size, uint32_t block_size, uint32_t align)
{
assert(align > 0);
assert((align & (align - 1)) == 0); // power of 2
// enforce minimum for storing free list pointers
if (align < _Alignof(LPoolBlock)) {
align = _Alignof(LPoolBlock);
}
if (block_size < sizeof(LPoolBlock)) {
block_size = sizeof(LPoolBlock);
}
// ensure initial alignment
uint32_t stride = ALIGN_UP(block_size, align);
// align first usable block
uintptr_t start_addr = (uintptr_t)buffer;
uintptr_t first_addr = ALIGN_UP(start_addr, align);
size_t padding = first_addr - start_addr;
if (padding + stride > buffer_size) {
pool->capacity = 0;
pool->free_list = NULL;
} else {
size_t usable = buffer_size - padding;
pool->capacity = usable / stride;
// build intrusive free list
LPoolBlock *block = (LPoolBlock *)first_addr;
pool->free_list = block;
for (uint32_t i = 0; i < pool->capacity - 1; i++) {
LPoolBlock *next = __builtin_assume_aligned(&block->data[0] + stride, _Alignof(LPoolBlock));
block->next = next;
block = next;
}
block->next = NULL; // last element
}
pool->block_size = stride;
pool->align = align;
pool->start = buffer;
pool->end = buffer + buffer_size;
}
void *lpool_alloc(LPool *pool)
{
if (!pool->free_list) {
return NULL;
}
LPoolBlock *block = pool->free_list;
pool->free_list = block->next;
#ifdef DEBUG
memset(block, 0xCB, pool->block_size);
#endif
return block->data;
}
void lpool_free(LPool *pool, void *p)
{
if (p == NULL) {
return;
}
LPoolBlock *block = p;
#ifdef DEBUG
assert(block >= pool->start && block < pool->end);
assert(((uintptr_t)p - (uintptr_t)pool->start) % pool->block_size == 0);
// double-free detection
for (LPoolBlock *f = pool->free_list; f; f = f->next) {
if (f == p) {
assert(0 && "Double free detected in pool!");
return;
}
}
memset(p, 0xDD, pool->block_size);
#endif
// When not in use, store pointer to next free element inline in the block
block->next = pool->free_list;
pool->free_list = p;
}
void lpool_reset(LPool *pool)
{
if (pool->capacity == 0) return;
uintptr_t start_addr = (uintptr_t)pool->start;
uintptr_t first_addr = ALIGN_UP(start_addr, pool->align);
// build intrusive free list
LPoolBlock *p = (LPoolBlock *)first_addr;
for (size_t i = 0; i < pool->capacity - 1; ++i) {
LPoolBlock *next = __builtin_assume_aligned(&p->data[0] + pool->block_size, _Alignof(LPoolBlock));
p->next = next;
p = next;
}
p->next = NULL; // last element
pool->free_list = (LPoolBlock *)first_addr;
}