Browse Source

Fixed some bugs

Thomas Johnson 5 months ago
parent
commit
274081c74f
1 changed files with 14 additions and 8 deletions
  1. 14
    8
      tree_alloc.c

+ 14
- 8
tree_alloc.c View File

@@ -7,12 +7,13 @@
7 7
 #ifdef DEBUG
8 8
 #include <stdio.h>
9 9
 
10
-void debug_print_tree(int indent, TreeAlloc *node) {
10
+void debug_print_tree(int indent, void *p) {
11
+  TreeAlloc *node = (TreeAlloc*) p;
11 12
   if (node != NULL) {
12 13
     debug_print_tree(indent + 1, node->left);
13 14
     for (int ii = 0; ii < indent; ii++) { printf("  "); }
14 15
     if (node->color == COLOR_RED) { printf("\e[31"); }
15
-    printf("%p %u\n", node, node->size);
16
+    printf("%p %lu\n", node, node->size);
16 17
     if (node->color == COLOR_RED) { printf("\e[37"); }
17 18
     debug_print_tree(indent + 1, node->right);
18 19
   }
@@ -306,6 +307,7 @@ int add_new_region(Arena *arena, uintptr_t size, uintptr_t padding, uintptr_t al
306 307
     newreg->right = NULL;
307 308
     realsize -= (void*) newreg - (void*) reg;
308 309
     realsize -= realsize % alignof(WatermarkAlloc);
310
+    newreg->size = realsize;
309 311
     if (arena->root_freespace == NULL) {
310 312
       insert_singleton((TreeAlloc**) &arena->root_freespace, (TreeAlloc*) newreg);
311 313
     } else {
@@ -319,6 +321,14 @@ int add_new_region(Arena *arena, uintptr_t size, uintptr_t padding, uintptr_t al
319 321
 }
320 322
 
321 323
 void unalloc(Arena *arena, void *addr) {
324
+#ifdef DEBUG
325
+  printf("==== UNALLOCATING ====\n");
326
+  printf("=== FREESPACE TREE ===\n");
327
+  debug_print_tree(0, arena->root_freespace);
328
+  printf("=== TREEALLOC TREE ===\n");
329
+  debug_print_tree(0, arena->root_treealloc);
330
+  printf("=== END OF TREES ====\n");
331
+#endif
322 332
   if (arena->root_treealloc == NULL) {
323 333
     arena->error("attempt to unallocate when there are no allocations!");
324 334
     return;
@@ -371,10 +381,12 @@ void unalloc(Arena *arena, void *addr) {
371 381
 void *alloc(Arena *arena, uintptr_t size, uintptr_t align) {
372 382
   uintptr_t actual_align = lcm(alignof(struct WatermarkAlloc), align);
373 383
 #ifdef DEBUG
384
+  printf("==== ALLOCATING =====\n");
374 385
   printf("=== FREESPACE TREE ===\n");
375 386
   debug_print_tree(0, arena->root_freespace);
376 387
   printf("=== TREEALLOC TREE ===\n");
377 388
   debug_print_tree(0, arena->root_treealloc);
389
+  printf("=== END OF TREES ====\n");
378 390
 #endif
379 391
   if (arena->root_freespace == NULL) {
380 392
     // Handle being out of freespace.
@@ -406,12 +418,6 @@ void *alloc(Arena *arena, uintptr_t size, uintptr_t align) {
406 418
       TreeAlloc *insert_point = search_by_address((TreeAlloc*) arena->root_treealloc, region);
407 419
       insert_right(&arena->root_treealloc, region, insert_point);
408 420
     }
409
-    if (arena->root_treealloc == NULL) {
410
-      insert_singleton(&arena->root_treealloc, region);
411
-    } else {
412
-      TreeAlloc *insert_point = search_by_address((TreeAlloc*) arena->root_treealloc, region);
413
-      insert_right(&arena->root_treealloc, region, insert_point);
414
-    }
415 421
     if (new_free_size >= sizeof(FreeSpace)) {
416 422
       // If there's enough free space after the allocation, use it!
417 423
       region->size = new_size;  // Safe because the allocated region tree is not sorted by size.

Loading…
Cancel
Save