Browse Source

Fixed size underflow

tree_alloc
Cameron Weinfurt 1 year ago
parent
commit
2e23ecc609
  1. 2
      main.c
  2. 186
      tree_alloc.c

2
main.c

@ -129,7 +129,7 @@ int main() {
};
dummy.next = &dummy;
dummy.prev = &dummy;
for (int ii = 0; ii < 100; ii++) {
for (int ii = 0; ii < 400; ii++) {
act(&dummy, &arena);
}
return 0;

186
tree_alloc.c

@ -7,6 +7,9 @@
#ifdef DEBUG
#include <stdio.h>
#define IS_BLACK_NODE(n) (n == NULL || n->color == COLOR_BLACK)
#define IS_RED_NODE(n) (n != NULL && n->color == COLOR_RED)
int debug_tree_black_height(TreeAlloc *node) {
if (node == NULL) {
return 1;
@ -14,22 +17,37 @@ int debug_tree_black_height(TreeAlloc *node) {
return ((node->color == COLOR_BLACK) ? 1 : 0) + debug_tree_black_height(node->left);
}
void debug_print_node(int indent, TreeAlloc *node, int bad) {
for (int ii = 0; ii < indent; ii++)
printf(" ");
if (node->color == COLOR_RED)
printf("\e[31m");
else if (bad)
printf("\e[30m]");
if (bad)
printf("\e[43m");
printf("%p %lu\n", node, node->size);
printf("\e[37m");
if (bad)
printf("\e[40m");
}
void debug_print_tree(int indent, void *p, int safe) {
TreeAlloc *node = (TreeAlloc*) p;
if (node != NULL) {
int bad = debug_tree_black_height(node->left) != debug_tree_black_height(node->right);
bad |= node->color == COLOR_RED && ((node->left != NULL && node->left->color == COLOR_RED) ||
(node->right != NULL && node->right->color == COLOR_RED) || (node->parent != NULL &&
node->parent->color == COLOR_RED));
bad &= !safe;
bad |= IS_RED_NODE(node) && (
IS_RED_NODE(node->left) ||
IS_RED_NODE(node->right) ||
IS_RED_NODE(node->parent) );
bad &= !safe;
debug_print_tree(indent + 1, node->left, safe);
for (int ii = 0; ii < indent; ii++) { printf(" "); }
if (node->color == COLOR_RED) { printf("\e[31m"); }
else if (bad) { printf("\e[30m]"); }
if (bad) { printf("\e[43m"); }
printf("%p %lu\n", node, node->size);
printf("\e[37m");
if (bad) { printf("\e[40m"); }
debug_print_node(indent, node, bad);
debug_print_tree(indent + 1, node->right, safe);
}
}
@ -180,9 +198,6 @@ void rotate_right(TreeAlloc **root_ptr, TreeAlloc *ta) {
}
}
#define IS_BLACK_NODE(n) (n == NULL || n->color == COLOR_BLACK)
#define IS_RED_NODE(n) (n != NULL && n->color == COLOR_RED)
void repair_tree_after_insert(TreeAlloc **root_ptr, TreeAlloc *ta) {
TreeAlloc *parent = ta->parent;
if (ta == *root_ptr) {
@ -249,24 +264,24 @@ void insert_right(TreeAlloc** root_ptr, TreeAlloc* to_insert, TreeAlloc* after)
#ifdef DEBUG
printf("=== PRE-INSERT-RIGHT ===\n");
printf("===== INSERTING =====\n");
debug_print_tree(0, to_insert, 0);
debug_print_node(0, to_insert, 0);
printf("===== CURRENT TREE =====\n");
debug_print_tree(0, *root_ptr, 0);
printf("===== END OF TREES =====\n");
#endif
if (after->right != NULL) {
after = after->right;
while (after->left != NULL) {
after = after->left;
}
after->left = to_insert;
to_insert->parent = after;
} else {
after->right = to_insert;
to_insert->parent = after;
}
to_insert->color = COLOR_RED;
repair_tree_after_insert(root_ptr, to_insert);
if (after->right != NULL) {
after = after->right;
while (after->left != NULL) {
after = after->left;
}
after->left = to_insert;
to_insert->parent = after;
} else {
after->right = to_insert;
to_insert->parent = after;
}
to_insert->color = COLOR_RED;
repair_tree_after_insert(root_ptr, to_insert);
#ifdef DEBUG
printf("== POST-INSERT-FIXUP ===\n");
printf("===== CURRENT TREE =====\n");
@ -284,19 +299,19 @@ void insert_left(TreeAlloc** root_ptr, TreeAlloc* to_insert, TreeAlloc* before)
debug_print_tree(0, *root_ptr, 0);
printf("===== END OF TREES =====\n");
#endif
if (before->left != NULL) {
before = before->left;
while (before->right != NULL) {
before = before->right;
}
before->right = to_insert;
to_insert->parent = before;
} else {
before->left = to_insert;
to_insert->parent = before;
}
to_insert->color = COLOR_RED;
repair_tree_after_insert(root_ptr, to_insert);
if (before->left != NULL) {
before = before->left;
while (before->right != NULL) {
before = before->right;
}
before->right = to_insert;
to_insert->parent = before;
} else {
before->left = to_insert;
to_insert->parent = before;
}
to_insert->color = COLOR_RED;
repair_tree_after_insert(root_ptr, to_insert);
#ifdef DEBUG
printf("== POST-INSERT-FIXUP ===\n");
printf("===== CURRENT TREE =====\n");
@ -319,9 +334,9 @@ void replace_node(TreeAlloc **root_ptr, TreeAlloc *node, TreeAlloc *replace) {
void repair_after_remove(TreeAlloc **root_ptr, TreeAlloc *parent, TreeAlloc *node) {
#ifdef DEBUG
printf("delete fixup at %p -> %p\n", parent, node);
printf("delete fixup at %p -> %p\n", parent, node);
#endif
// In theory, the last two conditions should be the same ...
// In theory, the last two conditions should be the same ...
if (IS_RED_NODE(node) || (node != NULL && node == *root_ptr)) {
node->color = COLOR_BLACK;
} else {
@ -332,54 +347,54 @@ void repair_after_remove(TreeAlloc **root_ptr, TreeAlloc *parent, TreeAlloc *nod
} else {
rotate_right(root_ptr, parent);
}
// The rotate shouldn't touch the parent relationship of `node`
// The rotate shouldn't touch the parent relationship of `node`
parent->parent->color = COLOR_BLACK;
parent->color = COLOR_RED;
sibling = get_sibling(parent, node);
parent->color = COLOR_RED;
sibling = get_sibling(parent, node);
}
if (IS_BLACK_NODE(sibling->left) && IS_BLACK_NODE(sibling->right)) {
if (node != NULL)
node->color = COLOR_BLACK;
if (node != NULL)
node->color = COLOR_BLACK;
sibling->color = COLOR_RED;
repair_after_remove(root_ptr, parent->parent, parent);
} else {
if (parent->left == node && IS_BLACK_NODE(sibling->right)) {
rotate_right(root_ptr, sibling);
sibling->color = COLOR_RED;
sibling = parent->right;
sibling->color = COLOR_BLACK;
}
if (parent->right == node && IS_BLACK_NODE(sibling->left)) {
rotate_left(root_ptr, sibling);
sibling->color = COLOR_RED;
sibling = parent->left;
sibling->color = COLOR_BLACK;
}
if (parent->left == node) {
rotate_left(root_ptr, parent);
} else {
rotate_right(root_ptr, parent);
}
if (node != NULL)
node->color = COLOR_BLACK;
TreeAlloc *uncle = get_sibling(parent->parent, parent);
if (uncle != NULL)
uncle->color = COLOR_BLACK;
char swap = parent->color;
parent->color = parent->parent->color;
parent->parent->color = swap;
if (parent->left == node && IS_BLACK_NODE(sibling->right)) {
rotate_right(root_ptr, sibling);
sibling->color = COLOR_RED;
sibling = parent->right;
sibling->color = COLOR_BLACK;
}
if (parent->right == node && IS_BLACK_NODE(sibling->left)) {
rotate_left(root_ptr, sibling);
sibling->color = COLOR_RED;
sibling = parent->left;
sibling->color = COLOR_BLACK;
}
if (parent->left == node) {
rotate_left(root_ptr, parent);
} else {
rotate_right(root_ptr, parent);
}
if (node != NULL)
node->color = COLOR_BLACK;
TreeAlloc *uncle = get_sibling(parent->parent, parent);
if (uncle != NULL)
uncle->color = COLOR_BLACK;
char swap = parent->color;
parent->color = parent->parent->color;
parent->parent->color = swap;
}
}
}
void remove_node(TreeAlloc **root_ptr, TreeAlloc *to_remove) {
char do_repair = 0;
char old_color;
char old_color;
#ifdef DEBUG
printf("====== PRE-REMOVE ======\n");
printf("======= REMOVING =======\n");
debug_print_tree(0, to_remove, 0);
debug_print_node(0, to_remove, 0);
printf("===== CURRENT TREE =====\n");
debug_print_tree(0, *root_ptr, 0);
printf("===== END OF TREES =====\n");
@ -388,23 +403,23 @@ void remove_node(TreeAlloc **root_ptr, TreeAlloc *to_remove) {
TreeAlloc *parent = to_remove->parent;
if (!to_remove->left) {
#ifdef DEBUG
printf("code path 1l\n");
printf("code path 1l\n");
#endif
replace = to_remove->right;
parent_of_replace = to_remove->parent;
parent_of_replace = to_remove->parent;
do_repair = to_remove->color == COLOR_BLACK;
replace_node(root_ptr, to_remove, replace);
} else if (!to_remove->right) {
#ifdef DEBUG
printf("code path 1r\n");
printf("code path 1r\n");
#endif
replace = to_remove->left;
parent_of_replace = to_remove->parent;
parent_of_replace = to_remove->parent;
do_repair = to_remove->color == COLOR_BLACK;
replace_node(root_ptr, to_remove, replace);
} else {
#ifdef DEBUG
printf("code path 2\n");
printf("code path 2\n");
#endif
TreeAlloc *tmp = succ(to_remove);
replace = tmp->right;
@ -413,10 +428,10 @@ void remove_node(TreeAlloc **root_ptr, TreeAlloc *to_remove) {
replace_node(root_ptr, tmp, replace);
tmp->right = to_remove->right;
to_remove->right->parent = tmp;
parent_of_replace = tmp->parent;
parent_of_replace = tmp->parent;
} else {
parent_of_replace = tmp;
}
parent_of_replace = tmp;
}
replace_node(root_ptr, to_remove, tmp);
tmp->color = to_remove->color;
tmp->left = to_remove->left;
@ -431,7 +446,7 @@ void remove_node(TreeAlloc **root_ptr, TreeAlloc *to_remove) {
printf("===== CURRENT TREE =====\n");
debug_print_tree(0, *root_ptr, 1);
printf("===== END OF TREES =====\n");
printf("considering fixing up %p -> %p\n", parent_of_replace, replace);
printf("considering fixing up %p -> %p\n", parent_of_replace, replace);
#endif
if (replace && parent_of_replace == NULL) {
replace->color = COLOR_BLACK;
@ -583,6 +598,7 @@ void *alloc(Arena *arena, uintptr_t size, uintptr_t align) {
#ifdef DEBUG
printf("start: %p, end: %p, adjusted end: %p\n", region, ((void*) region) + size, true_end);
printf("size: %lu -> %lu\n", size, new_size);
printf("new_free_size: %lu\n", new_free_size);
#endif
if (arena->root_treealloc == NULL) {
insert_singleton((TreeAlloc**) &arena->root_treealloc, region);
@ -607,7 +623,7 @@ void *alloc(Arena *arena, uintptr_t size, uintptr_t align) {
insert_right(&arena->root_treealloc, region, insert_point);
}
}
if (new_free_size >= sizeof(FreeSpace)) {
if (region->size >= new_size + sizeof(FreeSpace)) {
// If there's enough free space after the allocation, use it!
region->size = new_size; // Safe because the allocated region tree is not sorted by size.
FreeSpace *new_free = (FreeSpace*) ((void*) region + new_size);

Loading…
Cancel
Save