Browse Source

Added yet more debug prints

Thomas Johnson 5 months ago
parent
commit
4ab55500b1
1 changed files with 33 additions and 0 deletions
  1. 33
    0
      tree_alloc.c

+ 33
- 0
tree_alloc.c View File

@@ -323,12 +323,24 @@ void remove_node(TreeAlloc **root_ptr, TreeAlloc *node) {
323 323
 
324 324
 // Inserts a node into an empty tree.
325 325
 void insert_singleton(TreeAlloc **root_ptr, TreeAlloc *to_insert) {
326
+#ifdef DEBUG
327
+  printf("= PRE-INSERT-SINGLETON =\n");
328
+  printf("===== CURRENT TREE =====\n");
329
+  debug_print_tree(0, *root_ptr);
330
+  printf("===== END OF TREES =====\n");
331
+#endif
326 332
   *root_ptr = to_insert;
327 333
   to_insert->parent = NULL;
328 334
   repair_tree_after_insert(root_ptr, to_insert);
329 335
 }
330 336
 
331 337
 void insert_right(TreeAlloc** root_ptr, TreeAlloc* to_insert, TreeAlloc* after) {
338
+#ifdef DEBUG
339
+  printf("=== PRE-INSERT-RIGHT ===\n");
340
+  printf("===== CURRENT TREE =====\n");
341
+  debug_print_tree(0, *root_ptr);
342
+  printf("===== END OF TREES =====\n");
343
+#endif
332 344
   if (after->right != NULL) {
333 345
     after->right->parent = to_insert;
334 346
     to_insert->right = after->right;
@@ -339,6 +351,12 @@ void insert_right(TreeAlloc** root_ptr, TreeAlloc* to_insert, TreeAlloc* after)
339 351
 }
340 352
 
341 353
 void insert_left(TreeAlloc** root_ptr, TreeAlloc* to_insert, TreeAlloc* before) {
354
+#ifdef DEBUG
355
+  printf("=== PRE-INSERT-LEFT ====\n");
356
+  printf("===== CURRENT TREE =====\n");
357
+  debug_print_tree(0, *root_ptr);
358
+  printf("===== END OF TREES =====\n");
359
+#endif
342 360
   if (before->left != NULL) {
343 361
     before->left->parent = to_insert;
344 362
     to_insert->left = before->left;
@@ -454,6 +472,9 @@ void *alloc(Arena *arena, uintptr_t size, uintptr_t align) {
454 472
 #endif
455 473
   if (arena->root_freespace == NULL) {
456 474
     // Handle being out of freespace.
475
+#ifdef DEBUG
476
+    printf("Out of freespace nodes; getting more\n");
477
+#endif
457 478
     if (!add_new_region(arena, size, sizeof(TreeAlloc), actual_align)) {
458 479
       return NULL;
459 480
     }
@@ -462,6 +483,9 @@ void *alloc(Arena *arena, uintptr_t size, uintptr_t align) {
462 483
     TreeAlloc *region = search_by_size((TreeAlloc*) arena->root_freespace, sizeof(TreeAlloc), actual_align, size);
463 484
     if (region == NULL) {
464 485
       // Handle insufficient freespace or fragmentation.
486
+#ifdef DEBUG
487
+      printf("Out of sufficiently large freespace nodes; getting more\n");
488
+#endif
465 489
       if (!add_new_region(arena, size, sizeof(TreeAlloc), actual_align)) {
466 490
         return NULL;
467 491
       }
@@ -483,14 +507,23 @@ void *alloc(Arena *arena, uintptr_t size, uintptr_t align) {
483 507
     if (arena->root_treealloc == NULL) {
484 508
       insert_singleton((TreeAlloc**) &arena->root_treealloc, region);
485 509
     } else {
510
+#ifdef DEBUG
511
+      printf("searching for an insert point\n");
512
+#endif
486 513
       TreeAlloc *insert_point = search_by_address((TreeAlloc*) arena->root_treealloc, region);
487 514
       if (insert_point == NULL) {
488 515
         TreeAlloc *head = arena->root_treealloc;
489 516
         while (head->left != NULL) {
490 517
           head = head->left;
491 518
         }
519
+#ifdef DEBUG
520
+        printf("none found; inserting before %p\n", head);
521
+#endif
492 522
         insert_left(&arena->root_treealloc, region, head);
493 523
       } else {
524
+#ifdef DEBUG
525
+        printf("found one: %p\n", insert_point);
526
+#endif
494 527
         insert_right(&arena->root_treealloc, region, insert_point);
495 528
       }
496 529
     }

Loading…
Cancel
Save