2D Run-and-gun shooter inspired by One Man's Doomsday, Counter-Strike, and Metal Slug.
 
 
 
 
 
 

57 linhas
1.5 KiB

  1. #include "IZ_pool.h"
  2. void IZ_PoolInitialize(IZ_Pool* pool, size_t size) {
  3. IZ_LogInfo(IZ_LOG_CATEGORY_GLOBAL, "memory", "Setting up pool...");
  4. IZ_ListInitialize(&pool->items);
  5. pool->memory = IZ_malloc(size);
  6. IZ_memset(pool->memory, 0, size);
  7. pool->allocated_memory = 0;
  8. pool->next_address = 0;
  9. pool->max_size = size;
  10. }
  11. IZ_PoolItem* IZ_PoolAllocate(IZ_Pool* pool, IZ_PoolAllocationArgs args) {
  12. // 1. check next free allocation for size
  13. // 2. if 1. returns non-null,
  14. // u64 alloc_end = pool->next_address + size;
  15. //
  16. // for (u64 i = 0; i < POOL_MAX_ALLOCATIONS; i += 1) {
  17. // if (pool->allocation[i].length == 0) {
  18. // continue;
  19. // }
  20. // }
  21. if (pool->max_size - pool->allocated_memory < args.size) {
  22. // TODO deallocate memory based from priority
  23. }
  24. void* pointer = &pool->memory[pool->next_address];
  25. IZ_ListNode** new_item = IZ_ListAppendNode(&pool->items, &(IZ_PoolItem) {
  26. .pointer = pointer,
  27. .args = args,
  28. .pool = pool,
  29. });
  30. pool->next_address = (pool->next_address + args.size) % POOL_MAX_SIZE;
  31. pool->allocated_memory += args.size;
  32. return (*new_item)->value;
  33. }
  34. bool IZ_PoolGetSameItem(IZ_ListNode** node, u64 _index, IZ_List* list) {
  35. return (*node)->value == (*list->iterator)->value;
  36. }
  37. void IZ_PoolDeallocate(IZ_PoolItem* item) {
  38. IZ_ListNode** node = IZ_ListFindFirstNode(&item->pool->items, IZ_PoolGetSameItem);
  39. if (node) {
  40. IZ_ListDeleteNode(*node);
  41. }
  42. }
  43. void IZ_PoolTeardown(IZ_Pool* pool) {
  44. IZ_LogInfo(IZ_LOG_CATEGORY_GLOBAL, "memory", "Shutting down pool...");
  45. IZ_free(pool->memory);
  46. pool->memory = NULL;
  47. }