Browse Source

More debug info

tree_alloc
Cameron Weinfurt 1 year ago
parent
commit
5a87521bf3
  1. 1
      .gitignore
  2. 2
      Makefile
  3. 44
      tree_alloc.c

1
.gitignore

@ -1,3 +1,4 @@
*.o
the_alloc
vgcore*
debug_log.txt

2
Makefile

@ -7,7 +7,7 @@ OUT_NAME = the_alloc
dev: CFLAGS= $(COMMON_FLAGS) -g -D UNIT_TESTS -D DEBUG=1
dev: $(OUT_NAME)
rm -f vgcore.*
valgrind ./$(OUT_NAME)
valgrind ./$(OUT_NAME) 2&> debug_log.txt
release: CFLAGS= $(COMMON_FLAGS) -O2
release: clean $(OUT_NAME)

44
tree_alloc.c

@ -250,6 +250,8 @@ void insert_singleton(TreeAlloc **root_ptr, TreeAlloc *to_insert) {
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);
printf("===== CURRENT TREE =====\n");
debug_print_tree(0, *root_ptr);
printf("===== END OF TREES =====\n");
@ -273,6 +275,8 @@ void insert_right(TreeAlloc** root_ptr, TreeAlloc* to_insert, TreeAlloc* after)
void insert_left(TreeAlloc** root_ptr, TreeAlloc* to_insert, TreeAlloc* before) {
#ifdef DEBUG
printf("=== PRE-INSERT-LEFT ====\n");
printf("===== INSERTING =====\n");
debug_print_tree(0, to_insert);
printf("===== CURRENT TREE =====\n");
debug_print_tree(0, *root_ptr);
printf("===== END OF TREES =====\n");
@ -348,42 +352,44 @@ void repair_after_remove(TreeAlloc **root_ptr, TreeAlloc *node) {
}
}
void remove_node(TreeAlloc **root_ptr, TreeAlloc *node) {
void remove_node(TreeAlloc **root_ptr, TreeAlloc *to_remove) {
char do_repair = 0;
#ifdef DEBUG
printf("====== PRE-REMOVE ======\n");
printf("===== REMOVING =====\n");
debug_print_tree(0, to_remove);
printf("===== CURRENT TREE =====\n");
debug_print_tree(0, *root_ptr);
printf("===== END OF TREES =====\n");
#endif
TreeAlloc *replace;
TreeAlloc *parent = node->parent;
if (!node->left) {
replace = node->right;
do_repair = node->color == COLOR_BLACK;
replace_node(root_ptr, node, replace);
} else if (!node->right) {
replace = node->left;
do_repair = node->color == COLOR_BLACK;
replace_node(root_ptr, node, replace);
TreeAlloc *parent = to_remove->parent;
if (!to_remove->left) {
replace = to_remove->right;
do_repair = to_remove->color == COLOR_BLACK;
replace_node(root_ptr, to_remove, replace);
} else if (!to_remove->right) {
replace = to_remove->left;
do_repair = to_remove->color == COLOR_BLACK;
replace_node(root_ptr, to_remove, replace);
} else {
TreeAlloc *tmp = node->right;
TreeAlloc *tmp = to_remove->right;
while (tmp->left) tmp = tmp->left;
replace = tmp->right;
do_repair = tmp->color == COLOR_BLACK;
if (tmp != node->right) {
if (tmp != to_remove->right) {
replace_node(root_ptr, tmp, replace);
tmp->right = node->right;
node->right->parent = tmp;
tmp->right = to_remove->right;
to_remove->right->parent = tmp;
}
replace_node(root_ptr, node, tmp);
tmp->color = node->color;
tmp->left = node->left;
node->left->parent = tmp;
replace_node(root_ptr, to_remove, tmp);
tmp->color = to_remove->color;
tmp->left = to_remove->left;
to_remove->left->parent = tmp;
}
// Make sure that it doesn't have any tree pointers it shouldn't have.
node->parent = node->left = node->right = NULL;
to_remove->parent = to_remove->left = to_remove->right = NULL;
if (replace && replace->parent == NULL) {
#ifdef DEBUG

Loading…
Cancel
Save