Browse Source

did we fix all the bugs yet?

Thomas Johnson 5 months ago
parent
commit
83dcda7743
1 changed files with 44 additions and 25 deletions
  1. 44
    25
      tree_alloc.c

+ 44
- 25
tree_alloc.c View File

@@ -14,14 +14,15 @@ int debug_tree_black_height(TreeAlloc *node) {
14 14
 	return ((node->color == COLOR_BLACK) ? 1 : 0) + debug_tree_black_height(node->left);
15 15
 }
16 16
 
17
-void debug_print_tree(int indent, void *p) {
17
+void debug_print_tree(int indent, void *p, int safe) {
18 18
 	TreeAlloc *node = (TreeAlloc*) p;
19 19
 	if (node != NULL) {
20 20
 		int bad = debug_tree_black_height(node->left) != debug_tree_black_height(node->right);
21 21
 		bad |= node->color == COLOR_RED && ((node->left != NULL && node->left->color == COLOR_RED) ||
22 22
 				(node->right != NULL && node->right->color == COLOR_RED) || (node->parent != NULL &&
23 23
 					node->parent->color == COLOR_RED));
24
-		debug_print_tree(indent + 1, node->left);
24
+    bad &= !safe;
25
+		debug_print_tree(indent + 1, node->left, safe);
25 26
 		for (int ii = 0; ii < indent; ii++) { printf("  "); }
26 27
 		if (node->color == COLOR_RED) { printf("\e[31m"); }
27 28
     else if (bad) { printf("\e[30m]"); }
@@ -29,7 +30,7 @@ void debug_print_tree(int indent, void *p) {
29 30
 		printf("%p %lu\n", node, node->size);
30 31
 		printf("\e[37m");
31 32
 		if (bad) { printf("\e[40m"); }
32
-		debug_print_tree(indent + 1, node->right);
33
+		debug_print_tree(indent + 1, node->right, safe);
33 34
 	}
34 35
 }
35 36
 
@@ -230,7 +231,7 @@ void insert_singleton(TreeAlloc **root_ptr, TreeAlloc *to_insert) {
230 231
 #ifdef DEBUG
231 232
 	printf("= PRE-INSERT-SINGLETON =\n");
232 233
 	printf("===== CURRENT TREE =====\n");
233
-	debug_print_tree(0, *root_ptr);
234
+	debug_print_tree(0, *root_ptr, 0);
234 235
 	printf("===== END OF TREES =====\n");
235 236
 #endif
236 237
 	*root_ptr = to_insert;
@@ -239,7 +240,7 @@ void insert_singleton(TreeAlloc **root_ptr, TreeAlloc *to_insert) {
239 240
 #ifdef DEBUG
240 241
 	printf("= POST-INSERT-SINGLETON =\n");
241 242
 	printf("===== CURRENT TREE =====\n");
242
-	debug_print_tree(0, *root_ptr);
243
+	debug_print_tree(0, *root_ptr, 0);
243 244
 	printf("===== END OF TREES =====\n");
244 245
 #endif
245 246
 }
@@ -248,9 +249,9 @@ void insert_right(TreeAlloc** root_ptr, TreeAlloc* to_insert, TreeAlloc* after)
248 249
 #ifdef DEBUG
249 250
 	printf("=== PRE-INSERT-RIGHT ===\n");
250 251
 	printf("===== INSERTING =====\n");
251
-	debug_print_tree(0, to_insert);
252
+	debug_print_tree(0, to_insert, 0);
252 253
 	printf("===== CURRENT TREE =====\n");
253
-	debug_print_tree(0, *root_ptr);
254
+	debug_print_tree(0, *root_ptr, 0);
254 255
 	printf("===== END OF TREES =====\n");
255 256
 #endif
256 257
   if (after->right != NULL) {
@@ -269,7 +270,7 @@ void insert_right(TreeAlloc** root_ptr, TreeAlloc* to_insert, TreeAlloc* after)
269 270
 #ifdef DEBUG
270 271
 	printf("== POST-INSERT-FIXUP ===\n");
271 272
 	printf("===== CURRENT TREE =====\n");
272
-	debug_print_tree(0, *root_ptr);
273
+	debug_print_tree(0, *root_ptr, 0);
273 274
 	printf("===== END OF TREES =====\n");
274 275
 #endif
275 276
 }
@@ -278,9 +279,9 @@ void insert_left(TreeAlloc** root_ptr, TreeAlloc* to_insert, TreeAlloc* before)
278 279
 #ifdef DEBUG
279 280
 	printf("=== PRE-INSERT-LEFT ====\n");
280 281
 	printf("===== INSERTING =====\n");
281
-	debug_print_tree(0, to_insert);
282
+	debug_print_tree(0, to_insert, 0);
282 283
 	printf("===== CURRENT TREE =====\n");
283
-	debug_print_tree(0, *root_ptr);
284
+	debug_print_tree(0, *root_ptr, 0);
284 285
 	printf("===== END OF TREES =====\n");
285 286
 #endif
286 287
   if (before->left != NULL) {
@@ -299,7 +300,7 @@ void insert_left(TreeAlloc** root_ptr, TreeAlloc* to_insert, TreeAlloc* before)
299 300
 #ifdef DEBUG
300 301
 	printf("== POST-INSERT-FIXUP ===\n");
301 302
 	printf("===== CURRENT TREE =====\n");
302
-	debug_print_tree(0, *root_ptr);
303
+	debug_print_tree(0, *root_ptr, 0);
303 304
 	printf("===== END OF TREES =====\n");
304 305
 #endif
305 306
 }
@@ -318,7 +319,7 @@ void replace_node(TreeAlloc **root_ptr, TreeAlloc *node, TreeAlloc *replace) {
318 319
 
319 320
 void repair_after_remove(TreeAlloc **root_ptr, TreeAlloc *parent, TreeAlloc *node) {
320 321
 #ifdef DEBUG
321
-  printf("delete fixup at %p\n", node);
322
+  printf("delete fixup at %p -> %p\n", parent, node);
322 323
 #endif
323 324
   // In theory, the last two conditions should be the same ...
324 325
 	if (IS_RED_NODE(node) || (node != NULL && node == *root_ptr)) {
@@ -326,7 +327,7 @@ void repair_after_remove(TreeAlloc **root_ptr, TreeAlloc *parent, TreeAlloc *nod
326 327
 	} else {
327 328
 		TreeAlloc *sibling = get_sibling(parent, node);
328 329
 		if (IS_RED_NODE(sibling)) {
329
-			if (node->parent->left == node) {
330
+			if (parent->left == node) {
330 331
 				rotate_left(root_ptr, parent);
331 332
 			} else {
332 333
 				rotate_right(root_ptr, parent);
@@ -377,34 +378,45 @@ void remove_node(TreeAlloc **root_ptr, TreeAlloc *to_remove) {
377 378
   char old_color;
378 379
 #ifdef DEBUG
379 380
 	printf("====== PRE-REMOVE ======\n");
380
-	printf("===== REMOVING =====\n");
381
-	debug_print_tree(0, to_remove);
381
+	printf("======= REMOVING =======\n");
382
+	debug_print_tree(0, to_remove, 0);
382 383
 	printf("===== CURRENT TREE =====\n");
383
-	debug_print_tree(0, *root_ptr);
384
+	debug_print_tree(0, *root_ptr, 0);
384 385
 	printf("===== END OF TREES =====\n");
385 386
 #endif
386 387
 	TreeAlloc *replace, *parent_of_replace;
387 388
 	TreeAlloc *parent = to_remove->parent;
388 389
 	if (!to_remove->left) {
390
+#ifdef DEBUG
391
+    printf("code path 1l\n");
392
+#endif
389 393
 		replace = to_remove->right;
390 394
     parent_of_replace = to_remove->parent;
391 395
 		do_repair = to_remove->color == COLOR_BLACK;
392 396
 		replace_node(root_ptr, to_remove, replace);
393 397
 	} else if (!to_remove->right) {
398
+#ifdef DEBUG
399
+    printf("code path 1r\n");
400
+#endif
394 401
 		replace = to_remove->left;
395 402
     parent_of_replace = to_remove->parent;
396 403
 		do_repair = to_remove->color == COLOR_BLACK;
397 404
 		replace_node(root_ptr, to_remove, replace);
398 405
 	} else {
406
+#ifdef DEBUG
407
+    printf("code path 2\n");
408
+#endif
399 409
 		TreeAlloc *tmp = succ(to_remove);
400 410
 		replace = tmp->right;
401
-    parent_of_replace = tmp->parent;
402 411
 		do_repair = tmp->color == COLOR_BLACK;
403 412
 		if (tmp != to_remove->right) {
404 413
 			replace_node(root_ptr, tmp, replace);
405 414
 			tmp->right = to_remove->right;
406 415
 			to_remove->right->parent = tmp;
407
-		}
416
+      parent_of_replace = tmp->parent;
417
+		} else {
418
+      parent_of_replace = tmp;
419
+    }
408 420
 		replace_node(root_ptr, to_remove, tmp);
409 421
 		tmp->color = to_remove->color;
410 422
 		tmp->left = to_remove->left;
@@ -414,6 +426,13 @@ void remove_node(TreeAlloc **root_ptr, TreeAlloc *to_remove) {
414 426
 	// Make sure that it doesn't have any tree pointers it shouldn't have.
415 427
 	to_remove->parent = to_remove->left = to_remove->right = NULL;
416 428
 
429
+#ifdef DEBUG
430
+	printf("==== PRE-REMOVE-FIXUP ===\n");
431
+	printf("===== CURRENT TREE =====\n");
432
+	debug_print_tree(0, *root_ptr, 1);
433
+	printf("===== END OF TREES =====\n");
434
+  printf("considering fixing up %p -> %p\n", parent_of_replace, replace);
435
+#endif
417 436
 	if (replace && parent_of_replace == NULL) {
418 437
 		replace->color = COLOR_BLACK;
419 438
 	} else if (parent_of_replace != NULL && do_repair) {
@@ -422,7 +441,7 @@ void remove_node(TreeAlloc **root_ptr, TreeAlloc *to_remove) {
422 441
 #ifdef DEBUG
423 442
 	printf("=== POST-REMOVE ===\n");
424 443
 	printf("===== CURRENT TREE =====\n");
425
-	debug_print_tree(0, *root_ptr);
444
+	debug_print_tree(0, *root_ptr, 0);
426 445
 	printf("===== END OF TREES =====\n");
427 446
 #endif
428 447
 }
@@ -455,9 +474,9 @@ int add_new_region(Arena *arena, uintptr_t size, uintptr_t padding, uintptr_t al
455 474
 #ifdef DEBUG
456 475
 	printf("= POST-REGION-CREATION =\n");
457 476
 	printf("==== FREESPACE TREE ====\n");
458
-	debug_print_tree(0, arena->root_freespace);
477
+	debug_print_tree(0, arena->root_freespace, 0);
459 478
 	printf("==== TREEALLOC TREE ====\n");
460
-	debug_print_tree(0, arena->root_treealloc);
479
+	debug_print_tree(0, arena->root_treealloc, 0);
461 480
 	printf("===== END OF TREES =====\n");
462 481
 #endif
463 482
 	return 1;
@@ -467,9 +486,9 @@ void unalloc(Arena *arena, void *addr) {
467 486
 #ifdef DEBUG
468 487
 	printf("==== UNALLOCATING ====\n");
469 488
 	printf("=== FREESPACE TREE ===\n");
470
-	debug_print_tree(0, arena->root_freespace);
489
+	debug_print_tree(0, arena->root_freespace, 0);
471 490
 	printf("=== TREEALLOC TREE ===\n");
472
-	debug_print_tree(0, arena->root_treealloc);
491
+	debug_print_tree(0, arena->root_treealloc, 0);
473 492
 	printf("==== END OF TREES ====\n");
474 493
 #endif
475 494
 	if (arena->root_treealloc == NULL) {
@@ -526,9 +545,9 @@ void *alloc(Arena *arena, uintptr_t size, uintptr_t align) {
526 545
 #ifdef DEBUG
527 546
 	printf("==== ALLOCATING =====\n");
528 547
 	printf("=== FREESPACE TREE ===\n");
529
-	debug_print_tree(0, arena->root_freespace);
548
+	debug_print_tree(0, arena->root_freespace, 0);
530 549
 	printf("=== TREEALLOC TREE ===\n");
531
-	debug_print_tree(0, arena->root_treealloc);
550
+	debug_print_tree(0, arena->root_treealloc, 0);
532 551
 	printf("==== END OF TREES ====\n");
533 552
 #endif
534 553
 	if (arena->root_freespace == NULL) {

Loading…
Cancel
Save