2024-09-08 15:12:42 +00:00
|
|
|
#include <voxula/internals/utility/allocation.h>
|
|
|
|
#include <voxula/internals/utility/math.h>
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
void vx_reset_pool_allocation(vx_pool_s *pool, uint32_t slot_size)
|
|
|
|
{
|
|
|
|
uint32_t slot_index = 0;
|
2024-09-11 09:40:58 +00:00
|
|
|
while(slot_index < pool->capacity)
|
2024-09-08 15:12:42 +00:00
|
|
|
{
|
2024-09-11 09:40:58 +00:00
|
|
|
vx_pool_slot_header_s *header = (void *)
|
|
|
|
((uint8_t *) pool->allocation) + (slot_index * slot_size);
|
|
|
|
header->next = header + slot_size;
|
2024-09-08 15:12:42 +00:00
|
|
|
header->pool = pool;
|
|
|
|
++slot_index;
|
|
|
|
}
|
|
|
|
vx_pool_slot_header_s *last_header = (void *)
|
2024-09-11 09:40:58 +00:00
|
|
|
( ((uint8_t *) pool->allocation) + (slot_size * (pool->capacity - 1)) );
|
2024-09-08 15:12:42 +00:00
|
|
|
last_header->next = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
vx_pool_s * vx_new_pool(uint32_t unit_size, uint32_t capacity)
|
|
|
|
{
|
|
|
|
uint32_t len_header = vx_ceil_to(sizeof(vx_pool_slot_header_s), 16);
|
|
|
|
uint32_t slot_size = vx_ceil_to(unit_size + len_header, 16);
|
|
|
|
|
|
|
|
vx_pool_s *pool = malloc(64 + (slot_size * capacity));
|
2024-09-11 09:40:58 +00:00
|
|
|
pool->allocation = (void *) vx_ceil_to(((uint64_t) pool + 64), 64);
|
2024-09-08 15:12:42 +00:00
|
|
|
pool->unit_size = unit_size;
|
2024-09-11 09:40:58 +00:00
|
|
|
pool->capacity = capacity - 1;
|
|
|
|
pool->first_free = pool->allocation;
|
|
|
|
pool->continuation = NULL;
|
2024-09-08 15:12:42 +00:00
|
|
|
|
|
|
|
vx_reset_pool_allocation(pool, slot_size);
|
|
|
|
return pool;
|
|
|
|
}
|
|
|
|
|
|
|
|
void vx_free_pool(vx_pool_s *pool)
|
|
|
|
{
|
|
|
|
if(pool->continuation)
|
|
|
|
{
|
|
|
|
vx_free_pool(pool->continuation);
|
|
|
|
}
|
|
|
|
free(pool);
|
|
|
|
}
|
|
|
|
|
|
|
|
void * vx_pool(vx_pool_s *pool)
|
|
|
|
{
|
|
|
|
vx_pool_slot_header_s *item = pool->first_free;
|
2024-09-11 09:40:58 +00:00
|
|
|
if(item == NULL)
|
2024-09-08 15:12:42 +00:00
|
|
|
{
|
|
|
|
if( ! pool->continuation)
|
|
|
|
{
|
2024-09-11 09:40:58 +00:00
|
|
|
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);
|
2024-09-08 15:12:42 +00:00
|
|
|
}
|
2024-09-11 09:40:58 +00:00
|
|
|
return vx_pool(pool->continuation);
|
2024-09-08 15:12:42 +00:00
|
|
|
}
|
|
|
|
pool->first_free = item->next;
|
2024-09-11 09:40:58 +00:00
|
|
|
return ((uint8_t *) item + 16);
|
2024-09-08 15:12:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void vx_unpool(void *allocation)
|
|
|
|
{
|
2024-09-11 09:40:58 +00:00
|
|
|
vx_pool_slot_header_s *slot_header = (void *)
|
|
|
|
((uint8_t *) allocation - 16);
|
2024-09-08 15:12:42 +00:00
|
|
|
slot_header->next = slot_header->pool->first_free;
|
|
|
|
slot_header->pool->first_free = slot_header;
|
|
|
|
}
|