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.
This commit is contained in:
Eric-Paul Ickhorn 2024-09-11 11:40:58 +02:00
parent 44885e161d
commit 0408d19a72
1 changed files with 20 additions and 18 deletions

View File

@ -6,19 +6,16 @@
void vx_reset_pool_allocation(vx_pool_s *pool, uint32_t slot_size) void vx_reset_pool_allocation(vx_pool_s *pool, uint32_t slot_size)
{ {
uint32_t slot_index = 0; uint32_t slot_index = 0;
while(slot_index > pool->capacity) while(slot_index < pool->capacity)
{ {
vx_pool_slot_header_s *header = vx_pool_slot_header_s *header = (void *)
pool->allocation + slot_index * slot_size; ((uint8_t *) pool->allocation) + (slot_index * slot_size);
header->next = header + slot_size;
header->next =
(void *) ((uint8_t *) pool->allocation
+ (slot_index + 1) * slot_size);
header->pool = pool; header->pool = pool;
++slot_index; ++slot_index;
} }
vx_pool_slot_header_s *last_header = (void *) 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; 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); uint32_t slot_size = vx_ceil_to(unit_size + len_header, 16);
vx_pool_s *pool = malloc(64 + (slot_size * capacity)); vx_pool_s *pool = malloc(64 + (slot_size * capacity));
pool->allocation = (void *) vx_ceil_to((uint64_t) pool + 32, (uint64_t) 32); pool->allocation = (void *) vx_ceil_to(((uint64_t) pool + 64), 64);
pool->first_free = pool->allocation;
pool->unit_size = unit_size; 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); vx_reset_pool_allocation(pool, slot_size);
return pool; return pool;
@ -49,23 +47,27 @@ void vx_free_pool(vx_pool_s *pool)
void * vx_pool(vx_pool_s *pool) void * vx_pool(vx_pool_s *pool)
{ {
vx_pool_slot_header_s *item = pool->first_free; vx_pool_slot_header_s *item = pool->first_free;
if( ! item) if(item == NULL)
{ {
if( ! pool->continuation) 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; 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) void vx_unpool(void *allocation)
{ {
vx_pool_slot_header_s *slot_header = vx_pool_slot_header_s *slot_header = (void *)
allocation ((uint8_t *) allocation - 16);
- vx_ceil_to(sizeof(vx_pool_slot_header_s), 16);
slot_header->next = slot_header->pool->first_free; slot_header->next = slot_header->pool->first_free;
slot_header->pool->first_free = slot_header; slot_header->pool->first_free = slot_header;
} }