#include "IZ_pool.h" void IZ_PoolInitialize(IZ_Pool* pool, size_t size) { IZ_ListInitialize(&pool->items); pool->memory = SDL_malloc(size); SDL_memset(pool->memory, 0, size); pool->allocated_memory = 0; pool->next_address = 0; pool->max_size = size; } bool IZ_PoolFindItemsWithLowerPrecedence(IZ_ListNode** node, u64 index, IZ_List* list) { IZ_PoolItem* item = (IZ_PoolItem*)(*node)->value; IZ_PoolAllocationArgs* args = (IZ_PoolAllocationArgs*) list->find_predicate_userdata; return item->args.priority < args->priority; } IZ_PoolItem* IZ_PoolAllocate(IZ_Pool* pool, IZ_PoolAllocationArgs args) { // 1. check next free allocation for size // 2. if 1. returns non-null, // u64 alloc_end = pool->next_address + size; // // for (u64 i = 0; i < POOL_MAX_ALLOCATIONS; i += 1) { // if (pool->allocation[i].length == 0) { // continue; // } // } if (pool->max_size < args.size) { // couldn't allocate anything bigger than the pool return NULL; } if (pool->max_size - pool->allocated_memory < args.size) { pool->items.find_predicate_userdata = &args; IZ_ListDeleteFirstNode(&pool->items, IZ_PoolFindItemsWithLowerPrecedence); // TODO deallocate memory based from priority } void* pointer = &pool->memory[pool->next_address]; IZ_ListNode** new_item = IZ_ListAppendNode(&pool->items, &(IZ_PoolItem) { .pointer = pointer, .args = args, .pool = pool, }); pool->next_address = (pool->next_address + args.size) % POOL_MAX_SIZE; pool->allocated_memory += args.size; return (*new_item)->value; } bool IZ_PoolGetSameItem(IZ_ListNode** node, u64 _index, IZ_List* list) { return (*node)->value == (*list->iterator)->value; } void IZ_PoolDeallocate(IZ_PoolItem* item) { IZ_ListDeleteFirstNode(&item->pool->items, IZ_PoolGetSameItem); } void IZ_PoolTeardown(IZ_Pool* pool) { SDL_free(pool->memory); pool->memory = NULL; }