SHARE
TWEET

Untitled

tinyevil Aug 17th, 2019 72 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. struct FreeBlock {
  2.     FreeBlock* next;
  3.     size_t size;
  4. };
  5.  
  6. (void*, size_t) allocate(size_t sz) {
  7.     FreeBlock** prev = &root;
  8.     FreeBlock* block = root;
  9.     while (block) {
  10.         if (block->size >= sz) {
  11.             size_t tail = block->size - sz;
  12.             if (tail >= sizeof(FreeBlock)) {               
  13.                 FreeBlock* next = (FreeBlock*)((char*)block + sz);
  14.                 next->size = block->size - sz;
  15.                 next->next = block->next;
  16.                 block->next = next;
  17.                 block->size = sz;
  18.             }
  19.             *prev = block->next;
  20.             return block, block->size;
  21.         }
  22.         prev = &block->next;
  23.         block = *prev;
  24.     }
  25. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top