Browse Source

Added yet more debug prints

tree_alloc
Thomas Johnson 1 year ago
parent
commit
4ab55500b1
  1. 33
      tree_alloc.c

33
tree_alloc.c

@ -323,12 +323,24 @@ void remove_node(TreeAlloc **root_ptr, TreeAlloc *node) {
// Inserts a node into an empty tree.
void insert_singleton(TreeAlloc **root_ptr, TreeAlloc *to_insert) {
#ifdef DEBUG
printf("= PRE-INSERT-SINGLETON =\n");
printf("===== CURRENT TREE =====\n");
debug_print_tree(0, *root_ptr);
printf("===== END OF TREES =====\n");
#endif
*root_ptr = to_insert;
to_insert->parent = NULL;
repair_tree_after_insert(root_ptr, to_insert);
}
void insert_right(TreeAlloc** root_ptr, TreeAlloc* to_insert, TreeAlloc* after) {
#ifdef DEBUG
printf("=== PRE-INSERT-RIGHT ===\n");
printf("===== CURRENT TREE =====\n");
debug_print_tree(0, *root_ptr);
printf("===== END OF TREES =====\n");
#endif
if (after->right != NULL) {
after->right->parent = to_insert;
to_insert->right = after->right;
@ -339,6 +351,12 @@ 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("===== CURRENT TREE =====\n");
debug_print_tree(0, *root_ptr);
printf("===== END OF TREES =====\n");
#endif
if (before->left != NULL) {
before->left->parent = to_insert;
to_insert->left = before->left;
@ -454,6 +472,9 @@ void *alloc(Arena *arena, uintptr_t size, uintptr_t align) {
#endif
if (arena->root_freespace == NULL) {
// Handle being out of freespace.
#ifdef DEBUG
printf("Out of freespace nodes; getting more\n");
#endif
if (!add_new_region(arena, size, sizeof(TreeAlloc), actual_align)) {
return NULL;
}
@ -462,6 +483,9 @@ void *alloc(Arena *arena, uintptr_t size, uintptr_t align) {
TreeAlloc *region = search_by_size((TreeAlloc*) arena->root_freespace, sizeof(TreeAlloc), actual_align, size);
if (region == NULL) {
// Handle insufficient freespace or fragmentation.
#ifdef DEBUG
printf("Out of sufficiently large freespace nodes; getting more\n");
#endif
if (!add_new_region(arena, size, sizeof(TreeAlloc), actual_align)) {
return NULL;
}
@ -483,14 +507,23 @@ void *alloc(Arena *arena, uintptr_t size, uintptr_t align) {
if (arena->root_treealloc == NULL) {
insert_singleton((TreeAlloc**) &arena->root_treealloc, region);
} else {
#ifdef DEBUG
printf("searching for an insert point\n");
#endif
TreeAlloc *insert_point = search_by_address((TreeAlloc*) arena->root_treealloc, region);
if (insert_point == NULL) {
TreeAlloc *head = arena->root_treealloc;
while (head->left != NULL) {
head = head->left;
}
#ifdef DEBUG
printf("none found; inserting before %p\n", head);
#endif
insert_left(&arena->root_treealloc, region, head);
} else {
#ifdef DEBUG
printf("found one: %p\n", insert_point);
#endif
insert_right(&arena->root_treealloc, region, insert_point);
}
}

Loading…
Cancel
Save