Advertisement
Guest User

memory arena sample

a guest
Apr 26th, 2021
411
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.48 KB | None | 0 0
  1. // A arena might look like:
  2.  
  3. struct Arena {
  4.     void * ptr;
  5.     size_t total_size;
  6.     size_t filled_size;
  7.     Arena *next;
  8.     Arena *prev;
  9. }
  10.  
  11. // malloc() would be implemented like:
  12.  
  13. void* malloc (Arena *a, size_t s)
  14. {
  15.     if (a->filled_size + s > a->total_size) { // arena is full
  16.         // allocate new arena
  17.         // insert it in the linked list
  18.         // blah blah
  19.     }
  20.  
  21.     void *result = a->ptr;
  22.     a->ptr += s;
  23.     a->filled_size += s;
  24.  
  25.     return result;
  26. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement