Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- struct FreeBlock {
- FreeBlock* next;
- size_t size;
- };
- (void*, size_t) allocate(size_t sz) {
- FreeBlock** prev = &root;
- FreeBlock* block = root;
- while (block) {
- if (block->size >= sz) {
- size_t tail = block->size - sz;
- if (tail >= sizeof(FreeBlock)) {
- FreeBlock* next = (FreeBlock*)((char*)block + sz);
- next->size = block->size - sz;
- next->next = block->next;
- block->next = next;
- block->size = sz;
- }
- *prev = block->next;
- return block, block->size;
- }
- prev = &block->next;
- block = *prev;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement