This commit is contained in:
Nils O. Selåsdal
2026-05-14 14:45:29 +02:00
parent d04b151279
commit 6185de0694
6 changed files with 15 additions and 16 deletions
+3 -2
View File
@@ -7,7 +7,8 @@ ifndef DEBUG
CFLAGS += -O2
CPPFLAGS += -DNDEBUG
else
CPPFLAGS += -DDEBUG
CPPFLAGS += -DDEBUG -fsanitize=address,undefined
LDFLAGS += -fsanitize=address,undefined
endif
BUILDDIR = build
@@ -31,7 +32,7 @@ $(BUILDDIR)/$(LIB): $(OBJECTS)
$(BUILDDIR)/test/%: test/%.c $(BUILDDIR)/$(LIB)
mkdir -p $(@D)
$(CC) $(CPPFLAGS) $(CFLAGS) -MMD -MP -o $@ $< $(BUILDDIR)/$(LIB)
$(CC) $(CPPFLAGS) $(CFLAGS) $(LDFLAGS) -MMD -MP -o $@ $< $(BUILDDIR)/$(LIB)
.PHONY: test
test: $(TEST_BINS)
-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
};
+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));
+5 -3
View File
@@ -10,7 +10,7 @@ static inline LPoolBlock *lpool_next(LPool *pool, LPoolBlock *block)
if (block->next == NULL_BLOCK) {
return NULL;
}
return (LPoolBlock *)(pool->start + block->next);
return (LPoolBlock *)&pool->start[block->next];
}
static inline uint32_t lpool_offset(LPool *pool, LPoolBlock *block)
@@ -26,7 +26,7 @@ void lpool_init(LPool *pool, void *buffer, size_t buffer_size, uint32_t block_si
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);
@@ -111,7 +111,9 @@ void lpool_free(LPool *pool, void *ptr)
void lpool_reset(LPool *pool)
{
if (pool->capacity == 0) return;
if (pool->capacity == 0) {
return;
}
uintptr_t first_addr = (uintptr_t)pool->start;
+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--");
+2 -3
View File
@@ -12,8 +12,8 @@ struct TestData {
int main(int argc, char *argv[])
{
//_Alignas(16) uint8_t store[2048];
uint8_t *store = malloc(2048);
_Alignas(16) uint8_t store[2048];
// uint8_t *store = malloc(1024 * 1024 - 17);
LPool pool;
lpool_init(&pool, store, 2048, sizeof(struct TestData), _Alignof(struct TestData));
@@ -42,6 +42,5 @@ int main(int argc, char *argv[])
allocs++;
}
printf("Total allocs = %zu, used = %u\n", allocs, pool.used);
return 0;
}