Compare commits

...

7 Commits

Author SHA1 Message Date
Nils O. Selåsdal 48dc3a1e6a Fix larena aligned alloc 2026-05-30 13:58:13 +02:00
Nils O. Selåsdal e5336056db LARENA_ALLOC_ARRAY: protect macro expansion 2026-05-25 00:02:57 +02:00
Nils O. Selåsdal e306a9fed0 Add array allocations to larena 2026-05-24 23:34:34 +02:00
Nils O. Selåsdal 6185de0694 Cleanups 2026-05-14 14:45:29 +02:00
Nils O. Selåsdal d04b151279 Clean up lpool 2026-05-13 00:11:49 +02:00
Nils O. Selåsdal c89b4d1d3c lpool: use offset to next, avoids pointer alignment requirments 2026-05-12 20:18:25 +02:00
Nils O. Selåsdal 0180ce47e5 Rebuild all if Makefile changes 2026-05-12 00:14:30 +02:00
8 changed files with 105 additions and 70 deletions
+9 -12
View File
@@ -1,13 +1,14 @@
CC = gcc
LD = $(CC)
CFLAGS = -Wall -Wextra -Wno-unused-parameter -ggdb
CFLAGS = -std=gnu17 -Wall -Wextra -Wno-unused-parameter -ggdb
CPPFLAGS += -Iinclude
LDFLAGS +=
ifndef DEBUG
CFLAGS += -O2
CPPFLAGS += -DNDEBUG
else
CPPFLAGS += -DDEBUG
CPPFLAGS += -DDEBUG -fsanitize=address,undefined
LDFLAGS += -fsanitize=address,undefined
endif
BUILDDIR = build
@@ -15,25 +16,23 @@ SOURCES = $(wildcard src/*.c)
TEST_SOURCES = $(wildcard test/*.c)
OBJECTS = $(addprefix $(BUILDDIR)/,$(SOURCES:.c=.o))
DEPS = $(addprefix $(BUILDDIR)/,$(SOURCES:.c=.d)) $(addprefix $(BUILDDIR)/,$(TEST_SOURCES:.c=.d))
LIB = liblilalloc
LIB = liblilalloc.a
TEST_BINS = $(patsubst test/%.c,$(BUILDDIR)/test/%,$(TEST_SOURCES))
.PHONY: all
all: $(BUILDDIR)/$(LIB).a
all: $(BUILDDIR)/$(LIB)
$(BUILDDIR)/%.o: %.c
$(BUILDDIR)/%.o: %.c Makefile
mkdir -p $(@D)
$(CC) $(CPPFLAGS) $(CFLAGS) -MMD -MP -c -o $@ $<
$(BUILDDIR)/$(LIB).a: $(OBJECTS)
$(BUILDDIR)/$(LIB): $(OBJECTS)
ar crs $@ $^
$(BUILDDIR)/test/%: test/%.c $(BUILDDIR)/$(LIB).a
$(BUILDDIR)/test/%: test/%.c $(BUILDDIR)/$(LIB)
mkdir -p $(@D)
$(CC) $(CPPFLAGS) $(CFLAGS) -MMD -MP -o $@ $< $(BUILDDIR)/$(LIB).a
$(CC) $(CPPFLAGS) $(CFLAGS) $(LDFLAGS) -MMD -MP -o $@ $< $(BUILDDIR)/$(LIB)
.PHONY: test
test: $(TEST_BINS)
@@ -44,8 +43,6 @@ test: $(TEST_BINS)
echo ; \
done
.PHONY: clean
clean:
rm -rf $(BUILDDIR)
-1
View File
@@ -34,7 +34,6 @@ typedef struct LAlloc LAlloc;
struct LAlloc {
uint8_t *base; // backing store
uint32_t size; // total size
LBlockHeader *heap_start; // first block
LBlockHeader *next_alloc; // start of free block search
};
+7 -2
View File
@@ -94,9 +94,14 @@ struct LArenaTemp {
* struct foo *foo = LARENA_ALLOC_TYPE(&my_allocator, struct foo);
* // Array types can be used too:
* struct Foo *str = LARENA_ALLOC_TYPE(&my_allocator, struct Foo[32]);
* // For arrays with runtime determined size, use:
* LARENA_ALLOC_ARRAY(&my_allocator, struct Foo, cnt_foos);
* @endcode
*/
#define LARENA_ALLOC_TYPE(allocator, type_) larena_alloc_aligned((allocator), sizeof(type_), _Alignof(type_))
#define LARENA_ALLOC_ARRAY(allocator, type_, nr_elements_) larena_alloc_aligned((allocator), (nr_elements_) * sizeof(type_), _Alignof(type_))
// Arena size in bytes
static inline size_t larena_size(const LArena *arena)
{
@@ -158,11 +163,11 @@ static inline void *larena_alloc_aligned(LArena *arena, size_t sz, unsigned int
assert(align != 0);
assert((align & (align - 1)) == 0); // power of 2
uintptr_t curr = (uintptr_t)arena->curr;
uintptr_t curr = (uintptr_t)arena->;
uintptr_t aligned = (curr + (align - (uintptr_t)1)) & ~(align - (uintptr_t)1);
uint8_t *start = (uint8_t *)aligned;
if (sz > (larena_available(arena))) {
if (sz > (size_t)(arena->end - start)) {
return NULL;
}
+4 -4
View File
@@ -9,7 +9,7 @@ extern "C" {
typedef union LPoolBlock LPoolBlock;
union LPoolBlock {
LPoolBlock *next; // next free block (when not allocated)
uint32_t next; // offset from start to next free block (when not allocated)
unsigned char data[0]; // data when allocated
};
@@ -30,10 +30,10 @@ union LPoolBlock {
typedef struct LPool LPool;
struct LPool {
LPoolBlock *free_list; // free list
LPoolBlock *start; // start of user buffer
LPoolBlock *end; // one past end of user buffer
uint8_t *start; // start of user buffer
uint32_t block_size; // aligned size of each block
uint32_t capacity; // number of blocks
uint32_t capacity; // total number of blocks
uint32_t used; // used number of blocks
uint32_t align; // alignment
};
+4 -6
View File
@@ -70,7 +70,6 @@ static void lmaybe_split_block(LAlloc *allocator, LBlockHeader *block, uint32_t
lblock_set_size(block, size);
lblock_set_used(block);
new_block->size_and_flags = new_size | BLOCK_FREE;
new_block->next = block->next;
new_block->prev = lbh_offset(allocator, block);
@@ -116,7 +115,6 @@ void lalloc_init(LAlloc *allocator, void *backing_store, uint32_t size)
first->next = NULL_BLOCK;
first->prev = NULL_BLOCK;
allocator->heap_start = first;
allocator->next_alloc = first;
}
@@ -127,7 +125,7 @@ void *lalloc(LAlloc *allocator, uint32_t size)
}
size = ALIGN_UP(size);
LBlockHeader *start = allocator->next_alloc ? allocator->next_alloc : allocator->heap_start;
LBlockHeader *start = allocator->next_alloc ? allocator->next_alloc : (LBlockHeader *)allocator->base;
LBlockHeader *curr = start;
do {
@@ -135,11 +133,11 @@ void *lalloc(LAlloc *allocator, uint32_t size)
lmaybe_split_block(allocator, curr, size);
lblock_set_used(curr);
LBlockHeader *next = lblock_from_offset(allocator, curr->next);
allocator->next_alloc = next ? next : allocator->heap_start;
allocator->next_alloc = next ? next : (LBlockHeader *)allocator->base;
return (uint8_t *)curr + sizeof(LBlockHeader);
}
LBlockHeader *next = lblock_from_offset(allocator, curr->next);
curr = next ? next : allocator->heap_start;
curr = next ? next : (LBlockHeader *)allocator->base;
} while (curr != start);
return NULL;
@@ -153,7 +151,7 @@ void lfree(LAlloc *allocator, void *ptr)
LBlockHeader *block = (LBlockHeader *)((uint8_t *)ptr - sizeof(LBlockHeader));
assert((uint8_t *)ptr - sizeof(LBlockHeader) >= allocator->base && (uint8_t *)ptr < allocator->base + allocator->size);
assert((uint8_t *)block >= allocator->base && (uint8_t *)ptr < allocator->base + allocator->size);
// double free detection
assert(!lblock_is_free(block));
+59 -37
View File
@@ -3,13 +3,30 @@
#include "lpool.h"
#define ALIGN_UP(n, alignment) (((n) + (__typeof__(n))(alignment) - 1) & ~((__typeof__(n))(alignment) - 1))
#define NULL_BLOCK (0xffffffff)
static inline LPoolBlock *lpool_next(LPool *pool, LPoolBlock *block)
{
if (block->next == NULL_BLOCK) {
return NULL;
}
return (LPoolBlock *)&pool->start[block->next];
}
static inline uint32_t lpool_offset(LPool *pool, LPoolBlock *block)
{
if (block == NULL) {
return NULL_BLOCK;
}
return (uint8_t *)block - pool->start;
}
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
// enforce minimum for storing free list offsets
if (align < _Alignof(LPoolBlock)) {
align = _Alignof(LPoolBlock);
@@ -19,91 +36,96 @@ void lpool_init(LPool *pool, void *buffer, size_t buffer_size, uint32_t block_si
block_size = sizeof(LPoolBlock);
}
// ensure initial alignment
uint32_t stride = ALIGN_UP(block_size, align);
block_size = 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;
uintptr_t buffer_addr = (uintptr_t)buffer;
uintptr_t first_addr = ALIGN_UP(buffer_addr, align);
size_t padding = first_addr - buffer_addr;
if (padding + stride > buffer_size) {
if (padding + block_size > buffer_size) {
pool->capacity = 0;
pool->free_list = NULL;
} else {
size_t usable = buffer_size - padding;
pool->capacity = usable / stride;
pool->capacity = usable / block_size;
// 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));
for (uint32_t i = 1; i < pool->capacity; i++) {
uint32_t next = i * block_size;
block->next = next;
block = next;
block = (LPoolBlock *)(first_addr + next);
}
block->next = NULL; // last element
block->next = NULL_BLOCK; // last element
}
pool->block_size = stride;
pool->block_size = block_size;
pool->align = align;
pool->start = buffer;
pool->end = buffer + buffer_size;
pool->used = 0;
}
void *lpool_alloc(LPool *pool)
{
if (!pool->free_list) {
if (pool->free_list == NULL) {
return NULL;
}
LPoolBlock *block = pool->free_list;
pool->free_list = block->next;
LPoolBlock *next = lpool_next(pool, block);
pool->free_list = next;
#ifdef DEBUG
memset(block, 0xCB, pool->block_size);
#endif
pool->used++;
return block->data;
}
void lpool_free(LPool *pool, void *p)
void lpool_free(LPool *pool, void *ptr)
{
if (p == NULL) {
if (ptr == 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);
assert(pool->used > 0);
uint8_t *end = pool->start + (pool->capacity * pool->block_size);
assert((uint8_t *)ptr >= pool->start && (uint8_t *)ptr < end);
assert((((uintptr_t)ptr - (uintptr_t)pool->start) & (pool->block_size - 1)) == 0);
// double-free detection
for (LPoolBlock *f = pool->free_list; f; f = f->next) {
if (f == p) {
for (LPoolBlock *block = pool->free_list; block != NULL; block = lpool_next(pool, block)) {
if (block == ptr) {
assert(0 && "Double free detected in pool!");
return;
}
}
memset(p, 0xDD, pool->block_size);
memset(ptr, 0xDD, pool->block_size);
#endif
LPoolBlock *block = ptr;
// When not in use, store pointer to next free element inline in the block
block->next = pool->free_list;
pool->free_list = p;
block->next = lpool_offset(pool, pool->free_list);
pool->free_list = block;
pool->used--;
}
void lpool_reset(LPool *pool)
{
if (pool->capacity == 0) return;
if (pool->capacity == 0) {
return;
}
uintptr_t start_addr = (uintptr_t)pool->start;
uintptr_t first_addr = ALIGN_UP(start_addr, pool->align);
uintptr_t first_addr = (uintptr_t)pool->start;
// 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;
LPoolBlock *block = (LPoolBlock *)first_addr;
pool->free_list = block;
for (size_t i = 1; i < pool->capacity; ++i) {
uint32_t next = i * pool->block_size;
block->next = next;
block = (LPoolBlock *)(first_addr + next);
}
p->next = NULL; // last element
pool->free_list = (LPoolBlock *)first_addr;
block->next = NULL_BLOCK; // last element
pool->used = 0;
}
+1 -1
View File
@@ -7,7 +7,7 @@
void debug_alloc(const LAlloc *allocator)
{
puts("--debug start--");
for (LBlockHeader *b = allocator->heap_start; b; b = lblock_from_offset(allocator, b->next)) {
for (LBlockHeader *b = (LBlockHeader *)allocator->base; b; b = lblock_from_offset(allocator, b->next)) {
printf("Block size %u free %d prev %u next %u \n", lblock_size(b), lblock_is_free(b), b->prev, b->next);
}
puts("--debug end--");
+18 -4
View File
@@ -1,4 +1,6 @@
#include <stdio.h>
#include <stdlib.h>
#include "lpool.h"
struct TestData {
@@ -11,8 +13,10 @@ struct TestData {
int main(int argc, char *argv[])
{
_Alignas(16) uint8_t store[2048];
// uint8_t *store = malloc(1024 * 1024 - 17);
LPool pool;
lpool_init(&pool, store, sizeof store, sizeof(struct TestData), _Alignof(struct TestData));
lpool_init(&pool, store, 2048, sizeof(struct TestData), _Alignof(struct TestData));
printf("align=%u capacity=%u block_size=%u\n", pool.align, pool.capacity, pool.block_size);
struct TestData *data = lpool_alloc(&pool);
size_t allocs = 1;
@@ -23,10 +27,20 @@ int main(int argc, char *argv[])
}
allocs++;
}
printf("Total allocs = %zu\n", allocs);
printf("alloc succeded = %d\n", lpool_alloc(&pool) != NULL);
printf("Total allocs = %zu, used = %u\n", allocs, pool.used);
printf("alloc succeded = %d used = %u\n", lpool_alloc(&pool) != NULL, pool.used);
lpool_free(&pool, data);
printf("alloc succeded = %d\n", lpool_alloc(&pool) != NULL);
printf("alloc succeded = %d used = %u\n", lpool_alloc(&pool) != NULL, pool.used);
lpool_reset(&pool);
allocs = 0;
for (;;) {
struct TestData *t = lpool_alloc(&pool);
if (t == NULL) {
break;
}
allocs++;
}
printf("Total allocs = %zu, used = %u\n", allocs, pool.used);
return 0;
}