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

52 line
1.4 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. IZ_PoolItem* IZ_PoolAllocate(IZ_Pool* pool, IZ_PoolAllocationArgs args) {
  11. // 1. check next free allocation for size
  12. // 2. if 1. returns non-null,
  13. // u64 alloc_end = pool->next_address + size;
  14. //
  15. // for (u64 i = 0; i < POOL_MAX_ALLOCATIONS; i += 1) {
  16. // if (pool->allocation[i].length == 0) {
  17. // continue;
  18. // }
  19. // }
  20. if (pool->max_size - pool->allocated_memory < args.size) {
  21. // TODO deallocate memory based from priority
  22. }
  23. void* pointer = &pool->memory[pool->next_address];
  24. IZ_ListNode* new_item = IZ_ListAppendNode(&pool->items, &(IZ_PoolItem) {
  25. .pointer = pointer,
  26. .args = args,
  27. .pool = pool,
  28. });
  29. pool->next_address = (pool->next_address + args.size) % POOL_MAX_SIZE;
  30. pool->allocated_memory += args.size;
  31. return new_item->value;
  32. }
  33. IZ_ListFilterFunctionArgs(IZ_PoolItem* __current_item) bool IZ_PoolGetSameItem(IZ_ListNode* node, u64 _index) {
  34. return node->value == __current_item;
  35. }
  36. void IZ_PoolDeallocate(IZ_PoolItem* item) {
  37. IZ_ListDeleteFirstNode(__current_item, item)(&item->pool->items, IZ_PoolGetSameItem);
  38. }
  39. void IZ_PoolTeardown(IZ_Pool* pool) {
  40. SDL_free(pool->memory);
  41. pool->memory = NULL;
  42. }