#include #include #include void vx_reset_pool_allocation(vx_pool_s *pool, uint32_t slot_size) { uint32_t slot_index = 0; while(slot_index < pool->capacity) { 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)) ); 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)); pool->allocation = (void *) vx_ceil_to(((uint64_t) pool + 64), 64); pool->unit_size = unit_size; pool->capacity = capacity - 1; pool->first_free = pool->allocation; pool->continuation = NULL; 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; if(item == NULL) { if( ! pool->continuation) { 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->continuation); } pool->first_free = item->next; return ((uint8_t *) item + 16); } void vx_unpool(void *allocation) { 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; }