Browse Source

Added debug feature

Thomas Johnson 5 months ago
parent
commit
732d2f9dd2
2 changed files with 23 additions and 1 deletions
  1. 1
    1
      Makefile
  2. 22
    0
      tree_alloc.c

+ 1
- 1
Makefile View File

@@ -4,7 +4,7 @@ CC = clang
4 4
 COMMON_FLAGS = -std=gnu11
5 5
 OUT_NAME = the_alloc
6 6
 
7
-dev: CFLAGS= $(COMMON_FLAGS) -g -D UNIT_TESTS
7
+dev: CFLAGS= $(COMMON_FLAGS) -g -D UNIT_TESTS -D DEBUG=1
8 8
 dev: $(OUT_NAME)
9 9
 	valgrind ./$(OUT_NAME)
10 10
 

+ 22
- 0
tree_alloc.c View File

@@ -4,6 +4,22 @@
4 4
 
5 5
 #include "allocator_internal.h"
6 6
 
7
+#ifdef DEBUG
8
+#include <stdio.h>
9
+
10
+void debug_print_tree(int indent, TreeAlloc *node) {
11
+  if (node != NULL) {
12
+    debug_print_tree(indent + 1, node->left);
13
+    for (int ii = 0; ii < indent; ii++) { printf("  "); }
14
+    if (node->color == COLOR_RED) { printf("\e[31"); }
15
+    printf("%p %u\n", node, node->size);
16
+    if (node->color == COLOR_RED) { printf("\e[37"); }
17
+    debug_print_tree(indent + 1, node->right);
18
+  }
19
+}
20
+
21
+#endif
22
+
7 23
 TreeAlloc *insert_node_at(void *address, uintptr_t padding, uintptr_t align, uintptr_t size) {
8 24
 	return NULL;
9 25
 }
@@ -354,6 +370,12 @@ void unalloc(Arena *arena, void *addr) {
354 370
 
355 371
 void *alloc(Arena *arena, uintptr_t size, uintptr_t align) {
356 372
   uintptr_t actual_align = lcm(alignof(struct WatermarkAlloc), align);
373
+#ifdef DEBUG
374
+  printf("=== FREESPACE TREE ===\n");
375
+  debug_print_tree(0, arena->root_freespace);
376
+  printf("=== TREEALLOC TREE ===\n");
377
+  debug_print_tree(0, arena->root_treealloc);
378
+#endif
357 379
   if (arena->root_freespace == NULL) {
358 380
     // Handle being out of freespace.
359 381
     if (!add_new_region(arena, size, sizeof(TreeAlloc), actual_align)) {

Loading…
Cancel
Save