Browse Source

Fixed a megaleak caused by the tree node getting lost

Thomas Johnson 5 months ago
parent
commit
357827ca69
2 changed files with 52 additions and 48 deletions
  1. 3
    3
      allocator_internal.h
  2. 49
    45
      tree_alloc.c

+ 3
- 3
allocator_internal.h View File

@@ -69,9 +69,9 @@ uintptr_t gcd(uintptr_t a, uintptr_t b);
69 69
 TreeAlloc *search_by_address(TreeAlloc *root, void *address);
70 70
 TreeAlloc *search_by_size(TreeAlloc *root, uintptr_t padding, uintptr_t align, uintptr_t size);
71 71
 TreeAlloc *get_sibling(TreeAlloc *ta);
72
-void rotate_left(TreeAlloc *ta);
73
-void rotate_right(TreeAlloc *ta);
74
-void repair_tree_after_insert(TreeAlloc *ta);
72
+void rotate_left(TreeAlloc **root_ptr, TreeAlloc *ta);
73
+void rotate_right(TreeAlloc **root_ptr, TreeAlloc *ta);
74
+void repair_tree_after_insert(TreeAlloc **root_ptr, TreeAlloc *ta);
75 75
 void remove_node(TreeAlloc** root_ptr, TreeAlloc* node);
76 76
 void insert_singleton(TreeAlloc **root_ptr, TreeAlloc *to_insert);
77 77
 void insert_right(TreeAlloc** root_ptr, TreeAlloc* to_insert, TreeAlloc* after);

+ 49
- 45
tree_alloc.c View File

@@ -72,51 +72,55 @@ TreeAlloc *get_sibling(TreeAlloc *ta) {
72 72
 		return p->left;
73 73
 }
74 74
 
75
-void rotate_left(TreeAlloc *ta) {
76
-		TreeAlloc *parent, *tmp;
77
-		
78
-		parent = ta->parent;
79
-		tmp = ta->right;
80
-		if (!tmp) return;
81
-		
82
-		ta->right = tmp->left;
83
-		tmp->left = ta;
84
-		ta->parent = tmp;
75
+void rotate_left(TreeAlloc **root_ptr, TreeAlloc *ta) {
76
+  TreeAlloc *parent, *tmp;
85 77
 
86
-		if (ta->right) ta->right->parent = ta;
78
+  parent = ta->parent;
79
+  tmp = ta->right;
80
+  if (!tmp) return;
87 81
 
88
-		if (parent) {
89
-				if (ta == parent->left)
90
-						parent->left = tmp;
91
-				else
92
-						parent->right = tmp;
93
-		}
94
-		tmp->parent = parent;
82
+  ta->right = tmp->left;
83
+  tmp->left = ta;
84
+  ta->parent = tmp;
85
+
86
+  if (ta->right) ta->right->parent = ta;
87
+
88
+  if (parent == NULL) {
89
+    *root_ptr = tmp;
90
+  } else {
91
+    if (ta == parent->left)
92
+      parent->left = tmp;
93
+    else
94
+      parent->right = tmp;
95
+  }
96
+  tmp->parent = parent;
95 97
 }
96 98
 
97
-void rotate_right(TreeAlloc *ta) {
98
-		TreeAlloc *parent, *tmp;
99
-		
100
-		parent = ta->parent;
101
-		tmp = ta->left;
102
-		if (!tmp) return;
103
-		
104
-		ta->left = tmp->right;
105
-		tmp->right = ta;
106
-		ta->parent = tmp;
99
+void rotate_right(TreeAlloc **root_ptr, TreeAlloc *ta) {
100
+  TreeAlloc *parent, *tmp;
107 101
 
108
-		if (ta->left) ta->left->parent = ta;
102
+  parent = ta->parent;
103
+  tmp = ta->left;
104
+  if (!tmp) return;
109 105
 
110
-		if (parent) {
111
-				if (ta == parent->left)
112
-						parent->left = tmp;
113
-				else
114
-						parent->right = tmp;
115
-		}
116
-		tmp->parent = parent;
106
+  ta->left = tmp->right;
107
+  tmp->right = ta;
108
+  ta->parent = tmp;
109
+
110
+  if (ta->left) ta->left->parent = ta;
111
+
112
+  if (parent == NULL) {
113
+    *root_ptr = tmp;
114
+  } else {
115
+    if (ta == parent->left)
116
+      parent->left = tmp;
117
+    else
118
+      parent->right = tmp;
119
+  }
120
+  tmp->parent = parent;
117 121
 }
118 122
 
119
-void repair_tree_after_insert(TreeAlloc *ta) {
123
+void repair_tree_after_insert(TreeAlloc **root_ptr, TreeAlloc *ta) {
120 124
 	TreeAlloc *parent = ta->parent;
121 125
 	
122 126
 	if (parent == NULL) {
@@ -132,22 +136,22 @@ void repair_tree_after_insert(TreeAlloc *ta) {
132 136
 			parent->color = COLOR_BLACK;
133 137
 			uncle->color = COLOR_BLACK;
134 138
 			grandparent->color = COLOR_RED;
135
-			repair_tree_after_insert(grandparent);
139
+			repair_tree_after_insert(root_ptr, grandparent);
136 140
 		} else {
137 141
 				if (ta == parent->left && parent == grandparent->left) {
138
-						rotate_left(parent);
142
+						rotate_left(root_ptr, parent);
139 143
 						ta = ta->left;
140 144
 				} else {
141
-						rotate_right(parent);
145
+						rotate_right(root_ptr, parent);
142 146
 						ta = ta->right;	
143 147
 				}
144 148
 
145 149
 				parent = ta->parent;
146 150
 				grandparent = parent->parent;
147 151
 				if (ta == parent->left) {
148
-						rotate_right(grandparent);
152
+						rotate_right(root_ptr, grandparent);
149 153
 				} else {
150
-						rotate_left(grandparent);
154
+						rotate_left(root_ptr, grandparent);
151 155
 				}
152 156
 				parent->color = COLOR_BLACK;
153 157
 				grandparent->color = COLOR_RED;
@@ -186,7 +190,7 @@ void remove_node(TreeAlloc** root_ptr, TreeAlloc* node) {
186 190
 void insert_singleton(TreeAlloc **root_ptr, TreeAlloc *to_insert) {
187 191
   *root_ptr = to_insert;
188 192
   to_insert->parent = NULL;
189
-  repair_tree_after_insert(to_insert);
193
+  repair_tree_after_insert(root_ptr, to_insert);
190 194
 }
191 195
 
192 196
 void insert_right(TreeAlloc** root_ptr, TreeAlloc* to_insert, TreeAlloc* after) {
@@ -196,7 +200,7 @@ void insert_right(TreeAlloc** root_ptr, TreeAlloc* to_insert, TreeAlloc* after)
196 200
   }
197 201
   after->right = to_insert;
198 202
   to_insert->parent = after;
199
-  repair_tree_after_insert(to_insert);
203
+  repair_tree_after_insert(root_ptr, to_insert);
200 204
 }
201 205
 
202 206
 void insert_left(TreeAlloc** root_ptr, TreeAlloc* to_insert, TreeAlloc* before) {
@@ -206,7 +210,7 @@ void insert_left(TreeAlloc** root_ptr, TreeAlloc* to_insert, TreeAlloc* before)
206 210
   }
207 211
   before->left = to_insert;
208 212
   to_insert->parent = before;
209
-	repair_tree_after_insert(to_insert);
213
+	repair_tree_after_insert(root_ptr, to_insert);
210 214
 }
211 215
 
212 216
 int add_new_region(Arena *arena, uintptr_t size, uintptr_t padding, uintptr_t align) {

Loading…
Cancel
Save