blob: 0ae6fb4739c6f041e7208df5a6d55f8c672666d2 [file] [log] [blame]
Willy Tarreaube384c62007-04-28 14:00:17 +02001/*
2 * tree.h : tree manipulation macros and structures.
3 * (C) 2002 - Willy Tarreau - willy@ant-computing.com
4 *
5 */
6
7#ifndef __TREE_H__
8#define __TREE_H__
9
10#include <import/bitops.h>
11#include <common/memory.h>
12
13/* binary tree node : either 32 bits unsigned long int values, or
14 * 64 bits in two 32 bits unsigned long int values
15 */
16struct ultree {
17 unsigned long low; /* 32 bits low value of this node */
18 unsigned long high; /* 32 bits high value of this node, not used in 32 bits */
19 int level; /* bit level of this node */
20 void *data; /* carried data */
21 struct ultree *left, *right; /* children : left and right. NULL = leaf */
22 struct ultree *up; /* parent node. NULL = root */
23};
24
25/* binary tree node : 64 bits unsigned long long values */
26struct ulltree {
27 unsigned long long value; /* 64 bits value of this node */
28 int level; /* bit level of this node */
29 void *data; /* carried data */
30 struct ulltree *left, *right; /* children : left and right. NULL = leaf */
31 struct ulltree *up; /* parent node. NULL = root */
32};
33
34/* binary tree node : 64 bits in either one ull or two 32 bits unsigned long int values. This
35 * is the common type for all the above trees, which should be cast into it. This makes
36 * pool_free() far simpler since all types share a same pool.
37 */
38struct tree64 {
39 union {
40 struct {
41 unsigned long low; /* 32 bits low value of this node */
42 unsigned long high; /* 32 bits high value of this node */
43 } ul;
44 struct {
45 unsigned long long value; /* 64 bits value of this node */
46 } ull;
47 } value;
48 int level; /* bit level of this node */
49 void *data; /* carried data */
50 struct tree64 *left, *right; /* children : left and right. NULL = leaf */
51 struct tree64 *up; /* parent node. NULL = root */
52};
53
54#define sizeof_tree64 (sizeof (struct tree64))
55extern void **pool_tree64;
56
57static int node_right_lookup, node_lookup;
58
59#define ULTREE_HEAD(l) struct ultree (l) = { .left=NULL, .right=NULL, .up=NULL, .low=0, .level=LONGBITS, .data=NULL }
60#define ULTREE_INIT(l) { (l)->data = (l)->left = (l)->right = NULL; }
61#define ULTREE_INIT_ROOT(l) { (l)->left=(l)->right=(l)->up=(l)->data=NULL; (l)->low=0; (l)->level=LONGBITS; }
62
63#define ULLTREE_HEAD(l) struct ulltree (l) = { .left=NULL, .right=NULL, .up=NULL, .value=0, .level=LLONGBITS, .data=NULL }
64#define ULLTREE_INIT(l) { (l)->data = (l)->left = (l)->right = NULL; }
65#define ULLTREE_INIT_ROOT(l) { (l)->left=(l)->right=(l)->up=(l)->data=NULL; (l)->value=0; (l)->level=LLONGBITS; }
66
67#define UL2TREE_HEAD(l) struct ultree (l) = { .left=NULL, .right=NULL, .up=NULL, .high=0, .low=0, .level=LLONGBITS, .data=NULL }
68#define UL2TREE_INIT(l) { (l)->left = (l)->right = (l)->data = NULL; }
69#define UL2TREE_INIT_ROOT(l) { (l)->left=(l)->right=(l)->up=(l)->data=NULL; (l)->high=(l)->low=0; (l)->level=LLONGBITS; }
70
71/*
72 * inserts necessary nodes to reach <x> in tree starting at <root>. The node
73 * is not created if it exists. It is returned.
74 */
75inline static struct ulltree *__ulltree_insert(struct ulltree *root, unsigned long long x) {
76 int m;
77 struct ulltree *next, *new, *node;
78 struct ulltree **branch;
79 int ffs;
80
81 next = root;
82 ffs = ffs_fast64(x);
83
84 do {
85 root = next;
86
87 if (x == next->value) {
88 return next;
89 }
90
91 if (x & (1ULL << (next->level - 1))) { /* right branch */
92 branch = &next->right;
93 next = *branch;
94 } else {
95 branch = &next->left;
96 next = *branch;
97 }
98
99 if (next == NULL) {
100 /* we'll have to insert our node here */
101 *branch = new = (struct ulltree *)pool_alloc(tree64);
102 ULLTREE_INIT(new);
103 new->up = root;
104 new->value = x;
105 new->level = ffs;
106 return new;
107 }
108
109 /* we'll keep walking down as long as we have all bits in common */
110 } while ((x & ~((1ULL << next->level) - 1)) == next->value);
111
112
113 /* ok, now we know that we must insert between both. */
114
115 /* the new interconnect node */
116 *branch = node = (struct ulltree *)pool_alloc(tree64); /* was <next> */
117 ULLTREE_INIT(node);
118 node->up = root;
119 next->up = node;
120
121 /* we need the common higher bits between x and next->value. */
122
123 /* what differences are there between x and the node here ?
124 * NOTE that m is always < level(parent) because highest bit
125 * of x and next-value are identical here (else they would be
126 * on a different branch).
127 */
128 m = fls_fast64(x ^ next->value) + 1; /* m = lowest identical bit */
129 node->value = x & ~((1ULL << m) - 1); /* value of common bits */
130
131 if (node->value == x) { /* <x> is exactly on this node */
132 /* we must set its real position (eg: 8,10 => m=1 => val=8, m=3)*/
133 node->level = ffs;
134
135 if (next->value & (1ULL << (node->level - 1))) /* right branch */
136 node->right = next;
137 else
138 node->left = next;
139 return node;
140 }
141
142 /* the new leaf now */
143 node->level = m; /* set the level to the lowest common bit */
144 new = (struct ulltree *)pool_alloc(tree64);
145 ULLTREE_INIT(new);
146 new->value = x;
147 new->level = ffs;
148
149 if (x > next->value) {
150 node->left = next;
151 node->right = new;
152 }
153 else {
154 node->left = new;
155 node->right = next;
156 }
157 new->up = node;
158 return new;
159}
160
161/*
162 * inserts necessary nodes to reach <x> in tree starting at <root>. The node
163 * is not created if it exists. It is returned.
164 */
165inline static struct ultree *__ultree_insert(struct ultree *root, unsigned long x) {
166 int m;
167 struct ultree *next, *new, *node;
168 struct ultree **branch;
169 int ffs;
170
171 next = root;
172 ffs = ffs_fast32(x);
173
174 do {
175 root = next;
176
177 if (x == next->low) {
178 return next;
179 }
180
181 if ((x >> (next->level - 1)) & 1) { /* right branch */
182 branch = &next->right;
183 next = *branch;
184 } else {
185 branch = &next->left;
186 next = *branch;
187 }
188
189 if (next == NULL) {
190 /* we'll have to insert our node here */
191 *branch = new = (struct ultree *)pool_alloc(tree64);
192 ULTREE_INIT(new);
193 new->up = root;
194 new->low = x;
195 new->level = ffs;
196 return new;
197 }
198
199 /* we'll keep walking down as long as we have all bits in common */
200 } while ((x & ~((1 << next->level) - 1)) == next->low);
201
202 /* ok, now we know that we must insert between both. */
203
204 /* the new interconnect node */
205 *branch = node = (struct ultree *)pool_alloc(tree64); /* was <next> */
206 ULTREE_INIT(node);
207 node->up = root;
208 next->up = node;
209
210 /* we need the common higher bits between x and next->low. */
211
212 /* what differences are there between x and the node here ?
213 * NOTE that m is always < level(parent) because highest bit
214 * of x and next->low are identical here (else they would be
215 * on a different branch).
216 */
217 m = fls_fast32(x ^ next->low) + 1; /* m = lower identical bit */
218 node->low = x & ~((1 << m) - 1); /* value of common bits */
219
220 if (node->low == x) { /* <x> is exactly on this node */
221 /* we must set its real position (eg: 8,10 => m=1 => val=8, m=3)*/
222 node->level = ffs;
223
224 if (next->low & (1 << (node->level - 1))) /* right branch */
225 node->right = next;
226 else
227 node->left = next;
228 return node;
229 }
230
231 /* the new leaf now */
232 node->level = m; /* set the level to the lowest common bit */
233 new = (struct ultree *)pool_alloc(tree64);
234 ULTREE_INIT(new);
235 new->low = x;
236 new->level = ffs;
237
238 if (x > next->low) {
239 node->left = next;
240 node->right = new;
241 }
242 else {
243 node->left = new;
244 node->right = next;
245 }
246 new->up = node;
247 return new;
248}
249
250
251/*
252 * inserts necessary nodes to reach <h:l> in tree starting at <root>. The node
253 * is not created if it exists. It is returned.
254 */
255inline static struct ultree *__ul2tree_insert(struct ultree *root, unsigned long h, unsigned long l) {
256 int m;
257 struct ultree *next, *new, *node;
258 struct ultree **branch;
259
260 next = root;
261
262 do {
263 root = next;
264
265 if (h == next->high && l == next->low) {
266 return next;
267 }
268
269 branch = &next->left;
270 if (next->level >= 33) {
271 if ((h >> (next->level - 33)) & 1) { /* right branch */
272 branch = &next->right;
273 node_right_lookup++;
274 }
275 }
276 else {
277 if ((l >> (next->level - 1)) & 1) { /* right branch */
278 branch = &next->right;
279 node_right_lookup++;
280 }
281 }
282 next = *branch;
283
284 node_lookup++;
285 if (next == NULL) {
286 /* we'll have to insert our node here */
287 *branch = new =(struct ultree *)pool_alloc(tree64);
288 UL2TREE_INIT(new);
289 new->up = root;
290 new->high = h;
291 new->low = l;
292 if (l)
293 new->level = __ffs_fast32(l);
294 else
295 new->level = __ffs_fast32(h) + 32;
296
297 return new;
298 }
299
300 /* we'll keep walking down as long as we have all bits in common */
301 if (next->level >= 32) {
302 if ((h & ~((1 << (next->level-32)) - 1)) != next->high)
303 break;
304 }
305 else {
306 if (h != next->high)
307 break;
308 if ((l & ~((1 << next->level) - 1)) != next->low)
309 break;
310 }
311 } while (1);
312
313 /* ok, now we know that we must insert between both. */
314
315 /* the new interconnect node */
316 *branch = node = (struct ultree *)pool_alloc(tree64); /* was <next> */
317 UL2TREE_INIT(node);
318 node->up = root;
319 next->up = node;
320
321 /* we need the common higher bits between x and next->high:low. */
322
323 /* what differences are there between x and the node here ?
324 * NOTE that m is always < level(parent) because highest bit
325 * of x and next->high:low are identical here (else they would be
326 * on a different branch).
327 */
328 if (h != next->high) {
329 m = fls_fast32(h ^ next->high) + 1; /* m = lower identical bit */
330 node->high = h & ~((1 << m) - 1); /* value of common bits */
331 m += 32;
332 node->low = 0;
333 } else {
334 node->high = h;
335 m = fls_fast32(l ^ next->low) + 1; /* m = lower identical bit */
336 node->low = l & ~((1 << m) - 1); /* value of common bits */
337 }
338
339 if (node->high == h && node->low == l) { /* <h:l> is exactly on this node */
340 /* we must set its real position (eg: 8,10 => m=1 => val=8, m=3)*/
341 if (l) {
342 node->level = ffs_fast32(l);
343 if (next->low & (1 << (node->level - 1))) /* right branch */
344 node->right = next;
345 else
346 node->left = next;
347 }
348 else {
349 node->level = ffs_fast32(h) + 32;
350 if (next->high & (1 << (node->level - 33))) /* right branch */
351 node->right = next;
352 else
353 node->left = next;
354 }
355 return node;
356 }
357
358 /* the new leaf now */
359 node->level = m; /* set the level to the lowest common bit */
360 new = (struct ultree *)pool_alloc(tree64);
361 UL2TREE_INIT(new);
362 new->high = h;
363 new->low = l;
364 if (l)
365 new->level = __ffs_fast32(l);
366 else
367 new->level = __ffs_fast32(h) + 32;
368
369 if (h > next->high || (h == next->high && l > next->low)) {
370 node->left = next;
371 node->right = new;
372 }
373 else {
374 node->left = new;
375 node->right = next;
376 }
377 new->up = node;
378 return new;
379}
380
381
382/*
383 * finds a value in the tree <root>. If it cannot be found, NULL is returned.
384 */
385inline static struct ultree *__ultree_find(struct ultree *root, unsigned long x) {
386 do {
387 if (x == root->low)
388 return root;
389
390 if ((x >> (root->level - 1)) & 1)
391 root = root->right;
392 else
393 root = root->left;
394
395 if (root == NULL)
396 return NULL;
397
398 /* we'll keep walking down as long as we have all bits in common */
399 } while ((x & ~((1 << root->level) - 1)) == root->low);
400
401 /* should be there, but nothing. */
402 return NULL;
403}
404
405/*
406 * finds a value in the tree <root>. If it cannot be found, NULL is returned.
407 */
408inline static struct ulltree *__ulltree_find(struct ulltree *root, unsigned long long x) {
409 do {
410 if (x == root->value)
411 return root;
412
413 if ((x >> (root->level - 1)) & 1)
414 root = root->right;
415 else
416 root = root->left;
417
418 if (root == NULL)
419 return NULL;
420
421 /* we'll keep walking down as long as we have all bits in common */
422 } while ((x & ~((1ULL << root->level) - 1)) == root->value);
423
424 /* should be there, but nothing. */
425 return NULL;
426}
427
428
429/*
430 * walks down the tree <__root> and assigns each of its data to <__data>.
431 * <__stack> is an int array of at least N entries where N is the maximum number
432 * of levels of the tree. <__slen> is an integer variable used as a stack index.
433 * The instruction following the foreach statement is executed for each data,
434 * after the data has been unlinked from the tree.
435 * The nodes are deleted automatically, so it is illegal to manually delete a
436 * node within this loop.
437 */
438#define tree64_foreach_destructive(__root, __data, __stack, __slen) \
439 for (__slen = 0, __stack[0] = __root, __data = NULL; ({ \
440 __label__ __left, __right, __again, __end; \
441 typeof(__root) __ptr = __stack[__slen]; \
442__again: \
443 __data = __ptr->data; \
444 if (__data != NULL) { \
445 __ptr->data = NULL; \
446 goto __end; \
447 } \
448 else if (__ptr->left != NULL) { \
449 __stack[++__slen] = __ptr = __ptr->left; \
450 goto __again; \
451 } \
452 else \
453__left: \
454 if (__ptr->right != NULL) { \
455 __stack[++__slen] = __ptr = __ptr->right; \
456 goto __again; \
457 } \
458 else \
459__right: \
460 if (!__slen--) \
461 goto __end; /* nothing left, don't delete the root node */ \
462 else { \
463 typeof (__root) __old; \
464 pool_free(tree64, __ptr); \
465 __old = __ptr; \
466 __ptr = __stack[__slen]; \
467 if (__ptr->left == __old) { \
468 /* unlink this node from its parent */ \
469 __ptr->left = NULL; \
470 goto __left; \
471 } \
472 else { \
473 /* no need to unlink, the parent will also die */ \
474 goto __right; \
475 } \
476 } \
477__end: \
478 (__slen >= 0); /* nothing after loop */}); )
479
480
481/*
482 * walks down the tree <__root> of type <__type> and assigns each of its data
483 * to <__data>. <__stack> is an int array of at least N entries where N is the
484 * maximum number of levels of the tree. <__slen> is an integer variable used
485 * as a stack index. The instruction following the foreach statement is
486 * executed for each data, after the data has been unlinked from the tree.
487 */
488#define tree_foreach_destructive(__type, __root, __data, __stack, __slen) \
489 for (__slen = 0, __stack[0] = __root, __data = NULL; ({ \
490 __label__ __left, __right, __again, __end; \
491 typeof(__root) __ptr = __stack[__slen]; \
492__again: \
493 __data = __ptr->data; \
494 if (__data != NULL) { \
495 __ptr->data = NULL; \
496 goto __end; \
497 } \
498 else if (__ptr->left != NULL) { \
499 __stack[++__slen] = __ptr = __ptr->left; \
500 goto __again; \
501 } \
502 else \
503__left: \
504 if (__ptr->right != NULL) { \
505 __stack[++__slen] = __ptr = __ptr->right; \
506 goto __again; \
507 } \
508 else \
509__right: \
510 if (!__slen--) \
511 goto __end; /* nothing left, don't delete the root node */ \
512 else { \
513 typeof (__root) __old; \
514 pool_free(__type, __ptr); \
515 __old = __ptr; \
516 __ptr = __stack[__slen]; \
517 if (__ptr->left == __old) { \
518 /* unlink this node from its parent */ \
519 __ptr->left = NULL; \
520 goto __left; \
521 } \
522 else { \
523 /* no need to unlink, the parent will also die */ \
524 goto __right; \
525 } \
526 } \
527__end: \
528 (__slen >= 0); /* nothing after loop */}); )
529
530
531/*
532 * walks down the tree <__root> and assigns <__data> a pointer to each of its
533 * data pointers. <__stack> is an int array of at least N entries where N is the
534 * maximum number of levels of the tree. <__slen> is an integer variable used as
535 * a stack index. The instruction following the foreach statement is executed
536 * for each data.
537 * The tree will walk down only when the data field is empty (NULL), so it
538 * allows inner breaks, and will restart without losing items. The nodes data
539 * will be set to NULL after the inner code, or when the inner code does
540 * '__stack[__slen]->data = NULL';
541 * The nodes are deleted automatically, so it is illegal to manually delete a
542 * node within this loop.
543 */
544#define tree64_foreach(__root, __data, __stack, __slen) \
545 for (__slen = 0, __stack[0] = __root, __data = NULL; ({ \
546 __label__ __left, __right, __again, __end; \
547 typeof(__root) __ptr = __stack[__slen]; \
548__again: \
549 if (__ptr->data != NULL) { \
550 __data = __ptr->data; \
551 goto __end; \
552 } \
553 else if (__ptr->left != NULL) { \
554 __stack[++__slen] = __ptr = __ptr->left; \
555 goto __again; \
556 } \
557 else \
558__left: \
559 if (__ptr->right != NULL) { \
560 __stack[++__slen] = __ptr = __ptr->right; \
561 goto __again; \
562 } \
563 else \
564__right: \
565 if (!__slen--) \
566 goto __end; /* nothing left, don't delete the root node */ \
567 else { \
568 typeof (__root) __old; \
569 pool_free(tree64, __ptr); \
570 __old = __ptr; \
571 __ptr = __stack[__slen]; \
572 if (__ptr->left == __old) { \
573 /* unlink this node from its parent */ \
574 __ptr->left = NULL; \
575 goto __left; \
576 } \
577 else { \
578 /* no need to unlink, the parent will also die */ \
579 goto __right; \
580 } \
581 } \
582__end: \
583 (__slen >= 0); }); ((typeof(__root))__stack[__slen])->data = NULL)
584
585
586
587/*
588 * walks down the tree <__root> and assigns <__node> to each of its nodes.
589 * <__stack> is an int array of at least N entries where N is the
590 * maximum number of levels of the tree. <__slen> is an integer variable used as
591 * a stack index. The instruction following the foreach statement is executed
592 * for each node.
593 * The tree will walk down only when the data field is empty (NULL), so it
594 * allows inner breaks, and will restart without losing items. The nodes data
595 * will be set to NULL after the inner code, or when the inner code does
596 * '__node->data = NULL';
597 * The nodes are deleted automatically, so it is illegal to manually delete a
598 * node within this loop.
599 */
600#define tree64_foreach_node(__root, __node, __stack, __slen) \
601 for (__slen = 0, __stack[0] = __root; ({ \
602 __label__ __left, __right, __again, __end; \
603 typeof(__root) __ptr = __stack[__slen]; \
604__again: \
605 if (__ptr->data != NULL) { \
606 __node = __ptr; \
607 goto __end; \
608 } \
609 else if (__ptr->left != NULL) { \
610 __stack[++__slen] = __ptr = __ptr->left; \
611 goto __again; \
612 } \
613 else \
614__left: \
615 if (__ptr->right != NULL) { \
616 __stack[++__slen] = __ptr = __ptr->right; \
617 goto __again; \
618 } \
619 else \
620__right: \
621 if (!__slen--) \
622 goto __end; /* nothing left, don't delete the root node */ \
623 else { \
624 typeof (__root) __old; \
625 pool_free(tree64, __ptr); \
626 __old = __ptr; \
627 __ptr = __stack[__slen]; \
628 if (__ptr->left == __old) { \
629 /* unlink this node from its parent */ \
630 __ptr->left = NULL; \
631 goto __left; \
632 } \
633 else { \
634 /* no need to unlink, the parent will also die */ \
635 goto __right; \
636 } \
637 } \
638__end: \
639 (__slen >= 0); }); ((typeof(__root))__stack[__slen])->data = NULL)
640
641
642/*
643 * removes the current node if possible, and its parent if it doesn't handle
644 * data. A pointer to the parent or grandparent is returned (the parent of the
645 * last one deleted in fact). This function should be compatible with any
646 * tree struct because of the void types.
647 * WARNING : never call it from within a tree_foreach() because this last one
648 * uses a stack which will not be updated.
649 */
650
651inline static void *__tree_delete_only_one(void *firstnode) {
652 struct tree64 *down, **uplink;
653 struct tree64 *node = firstnode;
654
655 /* don't kill the root or a populated link */
656 if (node->data || node->up == NULL)
657 return node;
658 if (node->left && node->right)
659 return node;
660 /* since we know that at least left or right is null, we can do arithmetics on them */
661 down = (void *)((long)node->left | (long)node->right);
662 /* find where we are linked */
663 if (node == node->up->left)
664 uplink = &node->up->left;
665 else
666 uplink = &node->up->right;
667
668 *uplink = down; /* we relink the lower branch above us or simply cut it */
669 if (down) {
670 down->up = node->up;
671 /* we know that we cannot do more because we kept one branch */
672 }
673 else {
674 /* we'll redo this once for the node above us because there was no branch below us,
675 * so maybe it doesn't need to exist for only one branch
676 */
677 down = node;
678 node = node->up;
679 pool_free(tree64, down);
680 if (node->data || node->up == NULL)
681 return node;
682 /* now we're sure we were sharing this empty node with another branch, let's find it */
683 down = (void *)((long)node->left | (long)node->right);
684 if (node == node->up->left)
685 uplink = &node->up->left;
686 else
687 uplink = &node->up->right;
688 *uplink = down; /* we relink the lower branch above */
689 down->up = node->up;
690 }
691 /* free the last node */
692 pool_free(tree64, node);
693 return down->up;
694}
695
696/*
697 * removes the current node if possible, and all of its parents which do not
698 * carry data. A pointer to the parent of the last one deleted is returned.
699 * This function should be compatible with any tree struct because of the void
700 * types.
701 * WARNING : never call it from within a tree_foreach() because this last one
702 * uses a stack which will not be updated.
703 */
704
705inline static void *__tree_delete(void *firstnode) {
706 struct tree64 *down, **uplink, *up;
707 struct tree64 *node = firstnode;
708
709 while (1) {
710 /* don't kill the root or a populated link */
711 if (node->data || (up = node->up) == NULL)
712 return node;
713 if (node->left && node->right)
714 return node;
715 /* since we know that at least left or right is null, we can do arithmetics on them */
716 down = (void *)((long)node->left | (long)node->right);
717 /* find where we are linked */
718 if (node == up->left)
719 uplink = &up->left;
720 else
721 uplink = &up->right;
722
723 *uplink = down; /* we relink the lower branch above us or simply cut it */
724 pool_free(tree64, node);
725 node = up;
726 if (down)
727 down->up = node;
728 }
729}
730
731#endif /* __TREE_H__ */