kern

view src/rbtree.c @ 68:0a205396e1a0

- added a generic red-black tree data structure - added a VM map as an red-black tree of vm_pages in the process structure - constructed the vm map of the memory passed by the kernel initially to the first process.
author John Tsiombikas <nuclear@mutantstargoat.com>
date Mon, 10 Oct 2011 04:16:01 +0300
parents
children b45e2d5f0ae1
line source
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include "rbtree.h"
6 #define INT2PTR(x) ((void*)(x))
7 #define PTR2INT(x) ((int)(x))
9 static int cmpaddr(void *ap, void *bp);
10 static int cmpint(void *ap, void *bp);
12 static int count_nodes(struct rbnode *node);
13 static void del_tree(struct rbnode *node, void (*delfunc)(struct rbnode*, void*), void *cls);
14 static struct rbnode *insert(struct rbtree *rb, struct rbnode *tree, void *key, void *data);
15 static struct rbnode *delete(struct rbtree *rb, struct rbnode *tree, void *key);
16 /*static struct rbnode *find(struct rbtree *rb, struct rbnode *node, void *key);*/
17 static void traverse(struct rbnode *node, void (*func)(struct rbnode*, void*), void *cls);
19 struct rbtree *rb_create(rb_cmp_func_t cmp_func)
20 {
21 struct rbtree *rb;
23 if(!(rb = malloc(sizeof *rb))) {
24 return 0;
25 }
26 if(rb_init(rb, cmp_func) == -1) {
27 free(rb);
28 return 0;
29 }
30 return rb;
31 }
33 void rb_free(struct rbtree *rb)
34 {
35 rb_destroy(rb);
36 free(rb);
37 }
40 int rb_init(struct rbtree *rb, rb_cmp_func_t cmp_func)
41 {
42 memset(rb, 0, sizeof *rb);
44 if(cmp_func == RB_KEY_INT) {
45 rb->cmp = cmpint;
46 } else if(cmp_func == RB_KEY_STRING) {
47 rb->cmp = (rb_cmp_func_t)strcmp;
48 } else {
49 rb->cmp = cmpaddr;
50 }
52 rb->alloc = malloc;
53 rb->free = free;
54 return 0;
55 }
57 void rb_destroy(struct rbtree *rb)
58 {
59 del_tree(rb->root, rb->del, rb->del_cls);
60 }
62 void rb_set_allocator(struct rbtree *rb, rb_alloc_func_t alloc, rb_free_func_t free)
63 {
64 rb->alloc = alloc;
65 rb->free = free;
66 }
69 void rb_set_compare_func(struct rbtree *rb, rb_cmp_func_t func)
70 {
71 rb->cmp = func;
72 }
74 void rb_set_delete_func(struct rbtree *rb, rb_del_func_t func, void *cls)
75 {
76 rb->del = func;
77 rb->del_cls = cls;
78 }
80 int rb_size(struct rbtree *rb)
81 {
82 return count_nodes(rb->root);
83 }
85 int rb_insert(struct rbtree *rb, void *key, void *data)
86 {
87 rb->root = insert(rb, rb->root, key, data);
88 rb->root->red = 0;
89 return 0;
90 }
92 int rb_inserti(struct rbtree *rb, int key, void *data)
93 {
94 rb->root = insert(rb, rb->root, INT2PTR(key), data);
95 rb->root->red = 0;
96 return 0;
97 }
100 int rb_delete(struct rbtree *rb, void *key)
101 {
102 rb->root = delete(rb, rb->root, key);
103 rb->root->red = 0;
104 return 0;
105 }
107 int rb_deletei(struct rbtree *rb, int key)
108 {
109 rb->root = delete(rb, rb->root, INT2PTR(key));
110 rb->root->red = 0;
111 return 0;
112 }
115 void *rb_find(struct rbtree *rb, void *key)
116 {
117 struct rbnode *node = rb->root;
119 while(node) {
120 int cmp = rb->cmp(key, node->key);
121 if(cmp == 0) {
122 return node;
123 }
124 node = cmp < 0 ? node->left : node->right;
125 }
126 return 0;
127 }
129 void *rb_findi(struct rbtree *rb, int key)
130 {
131 return rb_find(rb, INT2PTR(key));
132 }
135 void rb_foreach(struct rbtree *rb, void (*func)(struct rbnode*, void*), void *cls)
136 {
137 traverse(rb->root, func, cls);
138 }
141 struct rbnode *rb_root(struct rbtree *rb)
142 {
143 return rb->root;
144 }
146 void rb_begin(struct rbtree *rb)
147 {
148 rb->rstack = 0;
149 rb->iter = rb->root;
150 }
152 #define push(sp, x) ((x)->next = (sp), (sp) = (x))
153 #define pop(sp) ((sp) = (sp)->next)
154 #define top(sp) (sp)
156 struct rbnode *rb_next(struct rbtree *rb)
157 {
158 struct rbnode *res = 0;
160 while(rb->rstack || rb->iter) {
161 if(rb->iter) {
162 push(rb->rstack, rb->iter);
163 rb->iter = rb->iter->left;
164 } else {
165 rb->iter = top(rb->rstack);
166 pop(rb->rstack);
167 res = rb->iter;
168 rb->iter = rb->iter->right;
169 break;
170 }
171 }
172 return res;
173 }
175 void *rb_node_key(struct rbnode *node)
176 {
177 return node ? node->key : 0;
178 }
180 int rb_node_keyi(struct rbnode *node)
181 {
182 return node ? PTR2INT(node->key) : 0;
183 }
185 void *rb_node_data(struct rbnode *node)
186 {
187 return node ? node->data : 0;
188 }
190 static int cmpaddr(void *ap, void *bp)
191 {
192 return ap < bp ? -1 : (ap > bp ? 1 : 0);
193 }
195 static int cmpint(void *ap, void *bp)
196 {
197 return PTR2INT(ap) - PTR2INT(bp);
198 }
201 /* ---- left-leaning 2-3 red-black implementation ---- */
203 /* helper prototypes */
204 static int is_red(struct rbnode *tree);
205 static void color_flip(struct rbnode *tree);
206 static struct rbnode *rot_left(struct rbnode *a);
207 static struct rbnode *rot_right(struct rbnode *a);
208 static struct rbnode *find_min(struct rbnode *tree);
209 static struct rbnode *del_min(struct rbtree *rb, struct rbnode *tree);
210 /*static struct rbnode *move_red_right(struct rbnode *tree);*/
211 static struct rbnode *move_red_left(struct rbnode *tree);
212 static struct rbnode *fix_up(struct rbnode *tree);
214 static int count_nodes(struct rbnode *node)
215 {
216 if(!node)
217 return 0;
219 return 1 + count_nodes(node->left) + count_nodes(node->right);
220 }
222 static void del_tree(struct rbnode *node, rb_del_func_t delfunc, void *cls)
223 {
224 if(!node)
225 return;
227 del_tree(node->left, delfunc, cls);
228 del_tree(node->right, delfunc, cls);
230 delfunc(node, cls);
231 free(node);
232 }
234 static struct rbnode *insert(struct rbtree *rb, struct rbnode *tree, void *key, void *data)
235 {
236 int cmp;
238 if(!tree) {
239 struct rbnode *node = rb->alloc(sizeof *node);
240 node->red = 1;
241 node->key = key;
242 node->data = data;
243 node->left = node->right = 0;
244 return node;
245 }
247 cmp = rb->cmp(key, tree->key);
249 if(cmp < 0) {
250 tree->left = insert(rb, tree->left, key, data);
251 } else if(cmp > 0) {
252 tree->right = insert(rb, tree->right, key, data);
253 } else {
254 tree->data = data;
255 }
257 /* fix right-leaning reds */
258 if(is_red(tree->right)) {
259 tree = rot_left(tree);
260 }
261 /* fix two reds in a row */
262 if(is_red(tree->left) && is_red(tree->left->left)) {
263 tree = rot_right(tree);
264 }
266 /* if 4-node, split it by color inversion */
267 if(is_red(tree->left) && is_red(tree->right)) {
268 color_flip(tree);
269 }
271 return tree;
272 }
274 static struct rbnode *delete(struct rbtree *rb, struct rbnode *tree, void *key)
275 {
276 int cmp;
278 if(!tree) {
279 return 0;
280 }
282 cmp = rb->cmp(key, tree->key);
284 if(cmp < 0) {
285 if(!is_red(tree->left) && !is_red(tree->left->left)) {
286 tree = move_red_left(tree);
287 }
288 tree->left = delete(rb, tree->left, key);
289 } else {
290 /* need reds on the right */
291 if(is_red(tree->left)) {
292 tree = rot_right(tree);
293 }
295 /* found it at the bottom (XXX what certifies left is null?) */
296 if(cmp == 0 && !tree->right) {
297 if(rb->del) {
298 rb->del(tree, rb->del_cls);
299 }
300 rb->free(tree);
301 return 0;
302 }
304 if(!is_red(tree->right) && !is_red(tree->right->left)) {
305 tree = move_red_left(tree);
306 }
308 if(key == tree->key) {
309 struct rbnode *rmin = find_min(tree->right);
310 tree->key = rmin->key;
311 tree->data = rmin->data;
312 tree->right = del_min(rb, tree->right);
313 } else {
314 tree->right = delete(rb, tree->right, key);
315 }
316 }
318 return fix_up(tree);
319 }
321 /*static struct rbnode *find(struct rbtree *rb, struct rbnode *node, void *key)
322 {
323 int cmp;
325 if(!node)
326 return 0;
328 if((cmp = rb->cmp(key, node->key)) == 0) {
329 return node;
330 }
331 return find(rb, cmp < 0 ? node->left : node->right, key);
332 }*/
334 static void traverse(struct rbnode *node, void (*func)(struct rbnode*, void*), void *cls)
335 {
336 if(!node)
337 return;
339 traverse(node->left, func, cls);
340 func(node, cls);
341 traverse(node->right, func, cls);
342 }
344 /* helpers */
346 static int is_red(struct rbnode *tree)
347 {
348 return tree && tree->red;
349 }
351 static void color_flip(struct rbnode *tree)
352 {
353 tree->red = !tree->red;
354 tree->left->red = !tree->left->red;
355 tree->right->red = !tree->right->red;
356 }
358 static struct rbnode *rot_left(struct rbnode *a)
359 {
360 struct rbnode *b = a->right;
361 a->right = b->left;
362 b->left = a;
363 b->red = a->red;
364 a->red = 1;
365 return b;
366 }
368 static struct rbnode *rot_right(struct rbnode *a)
369 {
370 struct rbnode *b = a->left;
371 a->left = b->right;
372 b->right = a;
373 b->red = a->red;
374 a->red = 1;
375 return b;
376 }
378 static struct rbnode *find_min(struct rbnode *tree)
379 {
380 struct rbnode *node;
382 if(!tree)
383 return 0;
385 while(node->left) {
386 node = node->left;
387 }
388 return node;
389 }
391 static struct rbnode *del_min(struct rbtree *rb, struct rbnode *tree)
392 {
393 if(!tree->left) {
394 if(rb->del) {
395 rb->del(tree->left, rb->del_cls);
396 }
397 rb->free(tree->left);
398 return 0;
399 }
401 /* make sure we've got red (3/4-nodes) at the left side so we can delete at the bottom */
402 if(!is_red(tree->left) && !is_red(tree->left->left)) {
403 tree = move_red_left(tree);
404 }
405 tree->left = del_min(rb, tree->left);
407 /* fix right-reds, red-reds, and split 4-nodes on the way up */
408 return fix_up(tree);
409 }
411 #if 0
412 /* push a red link on this node to the right */
413 static struct rbnode *move_red_right(struct rbnode *tree)
414 {
415 /* flipping it makes both children go red, so we have a red to the right */
416 color_flip(tree);
418 /* if after the flip we've got a red-red situation to the left, fix it */
419 if(is_red(tree->left->left)) {
420 tree = rot_right(tree);
421 color_flip(tree);
422 }
423 return tree;
424 }
425 #endif
427 /* push a red link on this node to the left */
428 static struct rbnode *move_red_left(struct rbnode *tree)
429 {
430 /* flipping it makes both children go red, so we have a red to the left */
431 color_flip(tree);
433 /* if after the flip we've got a red-red on the right-left, fix it */
434 if(is_red(tree->right->left)) {
435 tree->right = rot_right(tree->right);
436 tree = rot_left(tree);
437 color_flip(tree);
438 }
439 return tree;
440 }
442 static struct rbnode *fix_up(struct rbnode *tree)
443 {
444 /* fix right-leaning */
445 if(is_red(tree->right)) {
446 tree = rot_left(tree);
447 }
448 /* change invalid red-red pairs into a proper 4-node */
449 if(is_red(tree->left) && is_red(tree->left->left)) {
450 tree = rot_right(tree);
451 }
452 /* split 4-nodes */
453 if(is_red(tree->left) && is_red(tree->right)) {
454 color_flip(tree);
455 }
456 return tree;
457 }