2D Run-and-gun shooter inspired by One Man's Doomsday, Counter-Strike, and Metal Slug.
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 
 
 
 

65 řádky
1.8 KiB

  1. #include "IZ_pool.h"
  2. void IZ_PoolInitialize(IZ_Pool* pool, size_t size) {
  3. IZ_ListInitialize(&pool->items);
  4. pool->memory = SDL_malloc(size);
  5. SDL_memset(pool->memory, 0, size);
  6. pool->allocated_memory = 0;
  7. pool->next_address = 0;
  8. pool->max_size = size;
  9. }
  10. bool IZ_PoolFindItemsWithLowerPrecedence(IZ_ListNode** node, u64 index, IZ_List* list) {
  11. IZ_PoolItem* item = (IZ_PoolItem*)(*node)->value;
  12. IZ_PoolAllocationArgs* args = (IZ_PoolAllocationArgs*) list->find_predicate_userdata;
  13. return item->args.priority < args->priority;
  14. }
  15. IZ_PoolItem* IZ_PoolAllocate(IZ_Pool* pool, IZ_PoolAllocationArgs args) {
  16. // 1. check next free allocation for size
  17. // 2. if 1. returns non-null,
  18. // u64 alloc_end = pool->next_address + size;
  19. //
  20. // for (u64 i = 0; i < POOL_MAX_ALLOCATIONS; i += 1) {
  21. // if (pool->allocation[i].length == 0) {
  22. // continue;
  23. // }
  24. // }
  25. if (pool->max_size < args.size) {
  26. // couldn't allocate anything bigger than the pool
  27. return NULL;
  28. }
  29. if (pool->max_size - pool->allocated_memory < args.size) {
  30. pool->items.find_predicate_userdata = &args;
  31. IZ_ListDeleteFirstNode(&pool->items, IZ_PoolFindItemsWithLowerPrecedence);
  32. // TODO deallocate memory based from priority
  33. }
  34. void* pointer = &pool->memory[pool->next_address];
  35. IZ_ListNode** new_item = IZ_ListAppendNode(&pool->items, &(IZ_PoolItem) {
  36. .pointer = pointer,
  37. .args = args,
  38. .pool = pool,
  39. });
  40. pool->next_address = (pool->next_address + args.size) % POOL_MAX_SIZE;
  41. pool->allocated_memory += args.size;
  42. return (*new_item)->value;
  43. }
  44. bool IZ_PoolGetSameItem(IZ_ListNode** node, u64 _index, IZ_List* list) {
  45. return (*node)->value == (*list->iterator)->value;
  46. }
  47. void IZ_PoolDeallocate(IZ_PoolItem* item) {
  48. IZ_ListDeleteFirstNode(&item->pool->items, IZ_PoolGetSameItem);
  49. }
  50. void IZ_PoolTeardown(IZ_Pool* pool) {
  51. SDL_free(pool->memory);
  52. pool->memory = NULL;
  53. }