Advertisement
Tkap1

Untitled

Jul 16th, 2023
1,588
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.23 KB | None | 0 0
  1. struct s_free_list_node
  2. {
  3.     b8 used;
  4.     size_t size;
  5.     s_free_list_node* next;
  6. };
  7.  
  8.  
  9. struct s_free_list
  10. {
  11.     size_t capacity;
  12.     u8* memory;
  13.     s_free_list_node* first;
  14.  
  15.     void* alloc(size_t in_size);
  16.     void* realloc(void* ptr, size_t in_size);
  17.     void free(void* ptr);
  18. };
  19.  
  20.  
  21. void* s_free_list::alloc(size_t in_size)
  22. {
  23.     assert(in_size > 0);
  24.     size_t aligned_size = (in_size + 7) & ~7;
  25.  
  26.     void* result = null;
  27.     for(s_free_list_node* node = first; node; node = node->next)
  28.     {
  29.         if(!node->used && node->size >= aligned_size)
  30.         {
  31.             node->used = true;
  32.             result = (u8*)node + sizeof(s_free_list_node);
  33.             if(!node->next)
  34.             {
  35.                 s_free_list_node* new_node = (s_free_list_node*)((u8*)result + aligned_size);
  36.                 *new_node = zero;
  37.                 new_node->size = node->size - aligned_size - sizeof(s_free_list_node);
  38.                 node->next = new_node;
  39.                 node->size = aligned_size;
  40.             }
  41.             break;
  42.         }
  43.     }
  44.     assert(result);
  45.     return result;
  46. }
  47.  
  48. void* s_free_list::realloc(void* ptr, size_t in_size)
  49. {
  50.     s_free_list_node* node = (s_free_list_node*)((u8*)ptr - sizeof(s_free_list_node));
  51.     size_t aligned_size = (in_size + 7) & ~7;
  52.  
  53.     assert(in_size > 0);
  54.     assert(node->used);
  55.     assert(node->size > 0);
  56.     assert(node->size <= capacity - sizeof(s_free_list_node));
  57.  
  58.     if(aligned_size <= node->size)
  59.     {
  60.         return ptr;
  61.     }
  62.     free(ptr);
  63.     return alloc(aligned_size);
  64. }
  65.  
  66. void s_free_list::free(void* ptr)
  67. {
  68.     s_free_list_node* node = (s_free_list_node*)((u8*)ptr - sizeof(s_free_list_node));
  69.     s_free_list_node* temp = node;
  70.  
  71.     assert(node->used);
  72.     assert(node->size > 0);
  73.     assert(node->size <= capacity - sizeof(s_free_list_node));
  74.  
  75.     node->used = false;
  76.  
  77.     // @Note(tkap, 15/07/2023): Merge the following nodes if possible
  78.     while(true)
  79.     {
  80.         temp = temp->next;
  81.         if(!temp) { break; }
  82.         if(temp->used) { break; }
  83.         node->size += temp->size + sizeof(s_free_list_node);
  84.         node->next = temp->next;
  85.     }
  86. }
  87.  
  88. func s_free_list make_free_list(void* memory, size_t capacity)
  89. {
  90.     assert(capacity >= 1024);
  91.     s_free_list result = zero;
  92.     result.capacity = capacity;
  93.     result.memory = (u8*)memory;
  94.  
  95.     s_free_list_node first = zero;
  96.     first.size = capacity - sizeof(s_free_list_node);
  97.     result.first = (s_free_list_node*)result.memory;
  98.     *result.first = first;
  99.     return result;
  100. }
  101.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement