From 75b992f52e5e40f14765b0ce80e94a405aed90a2 Mon Sep 17 00:00:00 2001 From: Luke Benstead Date: Sat, 14 Sep 2024 07:51:13 +0100 Subject: [PATCH] Fix multiple bugs in the allocator --- GL/alloc/alloc.c | 32 +++++++++++++++++--------------- 1 file changed, 17 insertions(+), 15 deletions(-) diff --git a/GL/alloc/alloc.c b/GL/alloc/alloc.c index cde5588..0058baf 100644 --- a/GL/alloc/alloc.c +++ b/GL/alloc/alloc.c @@ -244,13 +244,17 @@ int alloc_init(void* pool, size_t size) { memset(pool_header.block_usage, 0, BLOCK_COUNT); pool_header.pool = pool; - pool_header.pool_size = size; intptr_t base_address = (intptr_t) pool_header.pool; base_address = round_up(base_address, 2048); pool_header.base_address = (uint8_t*) base_address; pool_header.block_count = ((p + size) - pool_header.base_address) / 2048; + + /* The pool size might be less than the passed size if the memory + * wasn't aligned to 2048 */ + pool_header.pool_size = pool_header.block_count * 2048; + pool_header.allocations = NULL; assert(((uintptr_t) pool_header.base_address) % 2048 == 0); @@ -490,30 +494,24 @@ static inline uint8_t count_ones(uint8_t byte) { size_t alloc_count_free(void* pool) { (void) pool; - uint8_t* it = pool_header.block_usage; - uint8_t* end = it + pool_header.block_count; + size_t total_used = 0; - size_t total_free = 0; - - while(it < end) { - total_free += count_ones(*it) * 256; - ++it; + for(size_t i = 0; i < pool_header.block_count; ++i) { + total_used += count_ones(pool_header.block_usage[i]) * 256; } - return total_free; + return pool_header.pool_size - total_used; } size_t alloc_count_continuous(void* pool) { (void) pool; size_t largest_block = 0; - - uint8_t* it = pool_header.block_usage; - uint8_t* end = it + pool_header.block_count; - size_t current_block = 0; - while(it < end) { - uint8_t t = *it++; + + for(size_t i = 0; i < pool_header.block_count; ++i) { + uint8_t t = pool_header.block_usage[i]; + if(!t) { current_block += 2048; } else { @@ -530,5 +528,9 @@ size_t alloc_count_continuous(void* pool) { } } + if(largest_block < current_block) { + largest_block = current_block; + } + return largest_block; }