Browse Source

Fixed a megaleak caused by the tree node getting lost

tree_alloc
Thomas Johnson 1 year ago
parent
commit
357827ca69
  1. 6
      allocator_internal.h
  2. 94
      tree_alloc.c

6
allocator_internal.h

@ -69,9 +69,9 @@ uintptr_t gcd(uintptr_t a, uintptr_t b);
TreeAlloc *search_by_address(TreeAlloc *root, void *address);
TreeAlloc *search_by_size(TreeAlloc *root, uintptr_t padding, uintptr_t align, uintptr_t size);
TreeAlloc *get_sibling(TreeAlloc *ta);
void rotate_left(TreeAlloc *ta);
void rotate_right(TreeAlloc *ta);
void repair_tree_after_insert(TreeAlloc *ta);
void rotate_left(TreeAlloc **root_ptr, TreeAlloc *ta);
void rotate_right(TreeAlloc **root_ptr, TreeAlloc *ta);
void repair_tree_after_insert(TreeAlloc **root_ptr, TreeAlloc *ta);
void remove_node(TreeAlloc** root_ptr, TreeAlloc* node);
void insert_singleton(TreeAlloc **root_ptr, TreeAlloc *to_insert);
void insert_right(TreeAlloc** root_ptr, TreeAlloc* to_insert, TreeAlloc* after);

94
tree_alloc.c

@ -72,51 +72,55 @@ TreeAlloc *get_sibling(TreeAlloc *ta) {
return p->left;
}
void rotate_left(TreeAlloc *ta) {
TreeAlloc *parent, *tmp;
parent = ta->parent;
tmp = ta->right;
if (!tmp) return;
ta->right = tmp->left;
tmp->left = ta;
ta->parent = tmp;
void rotate_left(TreeAlloc **root_ptr, TreeAlloc *ta) {
TreeAlloc *parent, *tmp;
if (ta->right) ta->right->parent = ta;
parent = ta->parent;
tmp = ta->right;
if (!tmp) return;
if (parent) {
if (ta == parent->left)
parent->left = tmp;
else
parent->right = tmp;
}
tmp->parent = parent;
ta->right = tmp->left;
tmp->left = ta;
ta->parent = tmp;
if (ta->right) ta->right->parent = ta;
if (parent == NULL) {
*root_ptr = tmp;
} else {
if (ta == parent->left)
parent->left = tmp;
else
parent->right = tmp;
}
tmp->parent = parent;
}
void rotate_right(TreeAlloc *ta) {
TreeAlloc *parent, *tmp;
parent = ta->parent;
tmp = ta->left;
if (!tmp) return;
ta->left = tmp->right;
tmp->right = ta;
ta->parent = tmp;
void rotate_right(TreeAlloc **root_ptr, TreeAlloc *ta) {
TreeAlloc *parent, *tmp;
if (ta->left) ta->left->parent = ta;
parent = ta->parent;
tmp = ta->left;
if (!tmp) return;
if (parent) {
if (ta == parent->left)
parent->left = tmp;
else
parent->right = tmp;
}
tmp->parent = parent;
ta->left = tmp->right;
tmp->right = ta;
ta->parent = tmp;
if (ta->left) ta->left->parent = ta;
if (parent == NULL) {
*root_ptr = tmp;
} else {
if (ta == parent->left)
parent->left = tmp;
else
parent->right = tmp;
}
tmp->parent = parent;
}
void repair_tree_after_insert(TreeAlloc *ta) {
void repair_tree_after_insert(TreeAlloc **root_ptr, TreeAlloc *ta) {
TreeAlloc *parent = ta->parent;
if (parent == NULL) {
@ -132,22 +136,22 @@ void repair_tree_after_insert(TreeAlloc *ta) {
parent->color = COLOR_BLACK;
uncle->color = COLOR_BLACK;
grandparent->color = COLOR_RED;
repair_tree_after_insert(grandparent);
repair_tree_after_insert(root_ptr, grandparent);
} else {
if (ta == parent->left && parent == grandparent->left) {
rotate_left(parent);
rotate_left(root_ptr, parent);
ta = ta->left;
} else {
rotate_right(parent);
rotate_right(root_ptr, parent);
ta = ta->right;
}
parent = ta->parent;
grandparent = parent->parent;
if (ta == parent->left) {
rotate_right(grandparent);
rotate_right(root_ptr, grandparent);
} else {
rotate_left(grandparent);
rotate_left(root_ptr, grandparent);
}
parent->color = COLOR_BLACK;
grandparent->color = COLOR_RED;
@ -186,7 +190,7 @@ void remove_node(TreeAlloc** root_ptr, TreeAlloc* node) {
void insert_singleton(TreeAlloc **root_ptr, TreeAlloc *to_insert) {
*root_ptr = to_insert;
to_insert->parent = NULL;
repair_tree_after_insert(to_insert);
repair_tree_after_insert(root_ptr, to_insert);
}
void insert_right(TreeAlloc** root_ptr, TreeAlloc* to_insert, TreeAlloc* after) {
@ -196,7 +200,7 @@ void insert_right(TreeAlloc** root_ptr, TreeAlloc* to_insert, TreeAlloc* after)
}
after->right = to_insert;
to_insert->parent = after;
repair_tree_after_insert(to_insert);
repair_tree_after_insert(root_ptr, to_insert);
}
void insert_left(TreeAlloc** root_ptr, TreeAlloc* to_insert, TreeAlloc* before) {
@ -206,7 +210,7 @@ void insert_left(TreeAlloc** root_ptr, TreeAlloc* to_insert, TreeAlloc* before)
}
before->left = to_insert;
to_insert->parent = before;
repair_tree_after_insert(to_insert);
repair_tree_after_insert(root_ptr, to_insert);
}
int add_new_region(Arena *arena, uintptr_t size, uintptr_t padding, uintptr_t align) {

Loading…
Cancel
Save