From 0408d19a72755f8da177da8c1902308a1c7931d0 Mon Sep 17 00:00:00 2001 From: Eric-Paul Ickhorn Date: Wed, 11 Sep 2024 11:40:58 +0200 Subject: [PATCH] Fix pool allocator The pool allocator would only allocate one item and then stop, its allocation wasn't reset correctly because of a comparison operator being the wrong way around. --- modules/utility/src-c/allocation/pool.c | 38 +++++++++++++------------ 1 file changed, 20 insertions(+), 18 deletions(-) diff --git a/modules/utility/src-c/allocation/pool.c b/modules/utility/src-c/allocation/pool.c index 627847d..d5ef3ab 100644 --- a/modules/utility/src-c/allocation/pool.c +++ b/modules/utility/src-c/allocation/pool.c @@ -6,19 +6,16 @@ void vx_reset_pool_allocation(vx_pool_s *pool, uint32_t slot_size) { uint32_t slot_index = 0; - while(slot_index > pool->capacity) + while(slot_index < pool->capacity) { - vx_pool_slot_header_s *header = - pool->allocation + slot_index * slot_size; - - header->next = - (void *) ((uint8_t *) pool->allocation - + (slot_index + 1) * slot_size); + vx_pool_slot_header_s *header = (void *) + ((uint8_t *) pool->allocation) + (slot_index * slot_size); + header->next = header + slot_size; header->pool = pool; ++slot_index; } vx_pool_slot_header_s *last_header = (void *) - ((uint8_t *) pool->allocation + slot_size * (pool->capacity - 1)); + ( ((uint8_t *) pool->allocation) + (slot_size * (pool->capacity - 1)) ); last_header->next = NULL; } @@ -28,10 +25,11 @@ vx_pool_s * vx_new_pool(uint32_t unit_size, uint32_t capacity) uint32_t slot_size = vx_ceil_to(unit_size + len_header, 16); vx_pool_s *pool = malloc(64 + (slot_size * capacity)); - pool->allocation = (void *) vx_ceil_to((uint64_t) pool + 32, (uint64_t) 32); - pool->first_free = pool->allocation; + pool->allocation = (void *) vx_ceil_to(((uint64_t) pool + 64), 64); pool->unit_size = unit_size; - pool->capacity = capacity; + pool->capacity = capacity - 1; + pool->first_free = pool->allocation; + pool->continuation = NULL; vx_reset_pool_allocation(pool, slot_size); return pool; @@ -49,23 +47,27 @@ void vx_free_pool(vx_pool_s *pool) void * vx_pool(vx_pool_s *pool) { vx_pool_slot_header_s *item = pool->first_free; - if( ! item) + if(item == NULL) { if( ! pool->continuation) { - pool->continuation = vx_new_pool(pool->unit_size, pool->capacity * 2); + uint32_t new_pool_size = pool->capacity * 2; + if(new_pool_size > 262144) + { + new_pool_size = 262144; + } + pool->continuation = vx_new_pool(pool->unit_size, new_pool_size); } - return vx_pool(pool->allocation); + return vx_pool(pool->continuation); } pool->first_free = item->next; - return item + vx_ceil_to(sizeof(vx_pool_slot_header_s), 16); + return ((uint8_t *) item + 16); } void vx_unpool(void *allocation) { - vx_pool_slot_header_s *slot_header = - allocation - - vx_ceil_to(sizeof(vx_pool_slot_header_s), 16); + vx_pool_slot_header_s *slot_header = (void *) + ((uint8_t *) allocation - 16); slot_header->next = slot_header->pool->first_free; slot_header->pool->first_free = slot_header; }