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
+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;