Advertisement
tinyevil

Untitled

Aug 17th, 2019
376
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.55 KB | None | 0 0
  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. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement