|
|
@ -80,6 +80,34 @@ TreeAlloc *search_by_size(TreeAlloc *root, uintptr_t padding, uintptr_t align, u |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
TreeAlloc *succ(TreeAlloc *el) { |
|
|
|
if (el->right != NULL) { |
|
|
|
el = el->right; |
|
|
|
while (el->left != NULL) { |
|
|
|
el = el->left; |
|
|
|
} |
|
|
|
return el; |
|
|
|
} |
|
|
|
while (el->parent != NULL && el == el->parent->right) { |
|
|
|
el = el->parent; |
|
|
|
} |
|
|
|
return el->parent; |
|
|
|
} |
|
|
|
|
|
|
|
TreeAlloc *pred(TreeAlloc *el) { |
|
|
|
if (el->left != NULL) { |
|
|
|
el = el->left; |
|
|
|
while (el->right != NULL) { |
|
|
|
el = el->right; |
|
|
|
} |
|
|
|
return el; |
|
|
|
} |
|
|
|
while (el->parent != NULL && el == el->parent->left) { |
|
|
|
el = el->parent; |
|
|
|
} |
|
|
|
return el->parent; |
|
|
|
} |
|
|
|
|
|
|
|
TreeAlloc *get_sibling(TreeAlloc *ta) { |
|
|
|
TreeAlloc *p = ta->parent; |
|
|
|
if (!p) |
|
|
@ -437,6 +465,19 @@ void *alloc(Arena *arena, uintptr_t size, uintptr_t align) { |
|
|
|
FreeSpace *insert_point = (FreeSpace*) search_by_size((TreeAlloc*) arena->root_freespace, 0, 1, new_free_size); |
|
|
|
insert_left((TreeAlloc**) &arena->root_freespace, (TreeAlloc*) new_free, (TreeAlloc*) insert_point); |
|
|
|
} |
|
|
|
// Set the region following this one to be the new free space |
|
|
|
region->after = (TreeAlloc*) new_free; |
|
|
|
} else { |
|
|
|
// There isn't a free space after this one, so put the `next` pointer at the next allocated |
|
|
|
// region. |
|
|
|
region->after = succ(region); |
|
|
|
} |
|
|
|
// Also make sure the `before` pointer is correct. |
|
|
|
TreeAlloc *before_alloc = pred(region); |
|
|
|
if (before_alloc == NULL || ((void*) before_alloc) + before_alloc->size < (void*) region) { |
|
|
|
region->before = search_by_address((TreeAlloc*) arena->root_freespace, region); |
|
|
|
} else { |
|
|
|
region->before = before_alloc; |
|
|
|
} |
|
|
|
return align_after(region + sizeof(TreeAlloc), actual_align); |
|
|
|
} |
|
|
|