Browse Source

Attempt at making insert work

Cameron Weinfurt 5 months ago
parent
commit
c1ce333202
2 changed files with 17 additions and 6 deletions
  1. 1
    1
      main.c
  2. 16
    5
      tree_alloc.c

+ 1
- 1
main.c View File

@@ -129,7 +129,7 @@ int main() {
129 129
   };
130 130
   dummy.next = &dummy;
131 131
   dummy.prev = &dummy;
132
-  for (int ii = 0; ii < 400; ii++) {
132
+  for (int ii = 0; ii < 4000; ii++) {
133 133
     act(&dummy, &arena);
134 134
   }
135 135
 	return 0;

+ 16
- 5
tree_alloc.c View File

@@ -14,14 +14,14 @@ int debug_tree_black_height(TreeAlloc *node) {
14 14
 	if (node == NULL) {
15 15
 		return 1;
16 16
 	}
17
-	return ((node->color == COLOR_BLACK) ? 1 : 0) + debug_tree_black_height(node->left);
17
+	return (IS_BLACK_NODE(node) ? 1 : 0) + debug_tree_black_height(node->left);
18 18
 }
19 19
 
20 20
 void debug_print_node(int indent, TreeAlloc *node, int bad) {
21 21
 	for (int ii = 0; ii < indent; ii++) 
22 22
 		printf("  ");
23 23
 
24
-	if (node->color == COLOR_RED) 
24
+	if (IS_RED_NODE(node)) 
25 25
 		printf("\e[31m");
26 26
 	else if (bad) 
27 27
 		printf("\e[30m]");
@@ -29,7 +29,10 @@ void debug_print_node(int indent, TreeAlloc *node, int bad) {
29 29
 	if (bad)
30 30
 		printf("\e[43m");
31 31
 
32
-	printf("%p %lu\n", node, node->size);
32
+	if (node)
33
+		printf("%p %lu\n", node, node->size);
34
+	else
35
+		printf("(nil) 0\n");
33 36
 	printf("\e[37m");
34 37
 
35 38
 	if (bad) 
@@ -64,7 +67,7 @@ TreeAlloc *insert_node_at(void *address, uintptr_t padding, uintptr_t align, uin
64 67
 TreeAlloc *search_by_address(TreeAlloc *root, void *address) {
65 68
 	TreeAlloc *head = root;
66 69
 	while (1) {
67
-		if (head > (TreeAlloc*) address) {
70
+		if (head >= (TreeAlloc*) address) {
68 71
 			if (head->left == NULL) {
69 72
 				return NULL;
70 73
 			} else {
@@ -97,7 +100,7 @@ TreeAlloc *search_by_size(TreeAlloc *root, uintptr_t padding, uintptr_t align, u
97 100
 	TreeAlloc *head = root;
98 101
 	while (1) {
99 102
 		uintptr_t esize = pessimistic_size(head, padding, align);
100
-		if (esize < size) {
103
+		if (esize <= size) {
101 104
 			if (head->right == NULL) {
102 105
 				return NULL;
103 106
 			} else {
@@ -265,6 +268,8 @@ void insert_right(TreeAlloc** root_ptr, TreeAlloc* to_insert, TreeAlloc* after)
265 268
 	printf("=== PRE-INSERT-RIGHT ===\n");
266 269
 	printf("===== INSERTING =====\n");
267 270
 	debug_print_node(0, to_insert, 0);
271
+	printf("\tafter\n");
272
+	debug_print_node(0, after, 0);
268 273
 	printf("===== CURRENT TREE =====\n");
269 274
 	debug_print_tree(0, *root_ptr, 0);
270 275
 	printf("===== END OF TREES =====\n");
@@ -295,10 +300,15 @@ void insert_left(TreeAlloc** root_ptr, TreeAlloc* to_insert, TreeAlloc* before)
295 300
 	printf("=== PRE-INSERT-LEFT ====\n");
296 301
 	printf("===== INSERTING =====\n");
297 302
 	debug_print_tree(0, to_insert, 0);
303
+	printf("\tbefore\n");
304
+	debug_print_node(0, before, 0);
298 305
 	printf("===== CURRENT TREE =====\n");
299 306
 	debug_print_tree(0, *root_ptr, 0);
300 307
 	printf("===== END OF TREES =====\n");
301 308
 #endif
309
+	if (!before)
310
+		before = *root_ptr;
311
+
302 312
 	if (before->left != NULL) {
303 313
 		before = before->left;
304 314
 		while (before->right != NULL) {
@@ -629,6 +639,7 @@ void *alloc(Arena *arena, uintptr_t size, uintptr_t align) {
629 639
 			FreeSpace *new_free = (FreeSpace*) ((void*) region + new_size);
630 640
 			new_free->left = NULL;
631 641
 			new_free->right = NULL;
642
+			new_free->parent = NULL;
632 643
 			new_free->type = RT_FREESPACE;
633 644
 			new_free->size = new_free_size;
634 645
 			if (arena->root_freespace == NULL) {

Loading…
Cancel
Save