Browse Source

Fixed some bugs

Thomas Johnson 5 months ago
parent
commit
fb70ac29e7
1 changed files with 35 additions and 7 deletions
  1. 35
    7
      tree_alloc.c

+ 35
- 7
tree_alloc.c View File

@@ -12,9 +12,9 @@ void debug_print_tree(int indent, void *p) {
12 12
   if (node != NULL) {
13 13
     debug_print_tree(indent + 1, node->left);
14 14
     for (int ii = 0; ii < indent; ii++) { printf("  "); }
15
-    if (node->color == COLOR_RED) { printf("\e[31"); }
15
+    if (node->color == COLOR_RED) { printf("\e[31m"); }
16 16
     printf("%p %lu\n", node, node->size);
17
-    if (node->color == COLOR_RED) { printf("\e[37"); }
17
+    if (node->color == COLOR_RED) { printf("\e[37m"); }
18 18
     debug_print_tree(indent + 1, node->right);
19 19
   }
20 20
 }
@@ -173,7 +173,6 @@ void repair_tree_after_insert(TreeAlloc **root_ptr, TreeAlloc *ta) {
173 173
 		ta->color = COLOR_BLACK;
174 174
 		return;
175 175
 	}
176
-	
177 176
 	TreeAlloc *grandparent = parent->parent;
178 177
 	TreeAlloc *uncle = get_sibling(parent);
179 178
 
@@ -187,12 +186,13 @@ void repair_tree_after_insert(TreeAlloc **root_ptr, TreeAlloc *ta) {
187 186
 				if (ta == parent->left && parent == grandparent->left) {
188 187
 						rotate_left(root_ptr, parent);
189 188
 						ta = ta->left;
189
+            parent = parent->left;
190 190
 				} else {
191 191
 						rotate_right(root_ptr, parent);
192 192
 						ta = ta->right;	
193
+            parent = parent->right;
193 194
 				}
194 195
 
195
-				parent = ta->parent;
196 196
 				grandparent = parent->parent;
197 197
 				if (ta == parent->left) {
198 198
 						rotate_right(root_ptr, grandparent);
@@ -222,7 +222,7 @@ void repair_after_remove(TreeAlloc **root_ptr, TreeAlloc *node) {
222 222
 		node->color = COLOR_BLACK;
223 223
 	} else {
224 224
 		TreeAlloc *sibling = get_sibling(node);
225
-		if (sibling->color == COLOR_RED) {
225
+		if (sibling != NULL && sibling->color == COLOR_RED) {
226 226
 			if (node->parent->left == node)
227 227
 				rotate_left(root_ptr, node->parent);
228 228
 			else
@@ -261,6 +261,12 @@ void repair_after_remove(TreeAlloc **root_ptr, TreeAlloc *node) {
261 261
 
262 262
 void remove_node(TreeAlloc **root_ptr, TreeAlloc *node) {
263 263
 	char do_repair = 0;
264
+#ifdef DEBUG
265
+      printf("====== PRE-REMOVE ======\n");
266
+      printf("===== CURRENT TREE =====\n");
267
+      debug_print_tree(0, *root_ptr);
268
+      printf("===== END OF TREES =====\n");
269
+#endif
264 270
 	TreeAlloc *replace;
265 271
 	TreeAlloc *parent = node->parent;
266 272
 	if (!node->left) {
@@ -287,7 +293,13 @@ void remove_node(TreeAlloc **root_ptr, TreeAlloc *node) {
287 293
 		node->left->parent = tmp;
288 294
 	}
289 295
 
290
-	if (do_repair && replace) {
296
+	if (do_repair && replace && replace->parent != NULL) {
297
+#ifdef DEBUG
298
+      printf("=== PRE-REMOVE-FIXUP ===\n");
299
+      printf("===== CURRENT TREE =====\n");
300
+      debug_print_tree(0, *root_ptr);
301
+      printf("===== END OF TREES =====\n");
302
+#endif
291 303
 		repair_after_remove(root_ptr, replace);
292 304
 	}
293 305
 }
@@ -456,7 +468,15 @@ void *alloc(Arena *arena, uintptr_t size, uintptr_t align) {
456 468
       insert_singleton((TreeAlloc**) &arena->root_treealloc, region);
457 469
     } else {
458 470
       TreeAlloc *insert_point = search_by_address((TreeAlloc*) arena->root_treealloc, region);
459
-      insert_right(&arena->root_treealloc, region, insert_point);
471
+      if (insert_point == NULL) {
472
+        TreeAlloc *head = arena->root_treealloc;
473
+        while (head->left != NULL) {
474
+          head = head->left;
475
+        }
476
+        insert_left(&arena->root_treealloc, region, head);
477
+      } else {
478
+        insert_right(&arena->root_treealloc, region, insert_point);
479
+      }
460 480
     }
461 481
     if (new_free_size >= sizeof(FreeSpace)) {
462 482
       // If there's enough free space after the allocation, use it!
@@ -479,6 +499,10 @@ void *alloc(Arena *arena, uintptr_t size, uintptr_t align) {
479 499
       // region.
480 500
       region->after = succ(region);
481 501
     }
502
+    // I seem to have forgotten about the fact that memory may not be contiguous
503
+    if (region->after != NULL && region->after != (void*) region + region->size) {
504
+      region->after = NULL;
505
+    }
482 506
     // Also make sure the `before` pointer is correct.
483 507
     TreeAlloc *before_alloc = pred(region);
484 508
     if (before_alloc == NULL || ((void*) before_alloc) + before_alloc->size < (void*) region) {
@@ -486,6 +510,10 @@ void *alloc(Arena *arena, uintptr_t size, uintptr_t align) {
486 510
     } else {
487 511
       region->before = before_alloc;
488 512
     }
513
+    // I seem to have forgotten about the fact that memory may not be contiguous
514
+    if (region->before != NULL && region->before != (void*) region->before + region->before->size) {
515
+      region->before = NULL;
516
+    }
489 517
 #ifdef DEBUG
490 518
     printf("region is still at %p\n", region);
491 519
 #endif

Loading…
Cancel
Save