blob: 7a595b949bd329288b79a36103c41b8b025b12b6 [file] [log] [blame]
Willy Tarreaue6d2e4d2007-11-15 23:56:17 +01001/*
2 * Elastic Binary Trees - generic macros and structures.
3 * (C) 2002-2007 - Willy Tarreau <w@1wt.eu>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 *
19 *
20 * Short history :
21 *
22 * 2007/09/28: full support for the duplicates tree => v3
23 * 2007/07/08: merge back cleanups from kernel version.
24 * 2007/07/01: merge into Linux Kernel (try 1).
25 * 2007/05/27: version 2: compact everything into one single struct
26 * 2007/05/18: adapted the structure to support embedded nodes
27 * 2007/05/13: adapted to mempools v2.
28 */
29
30
31
32/*
33 General idea:
34 -------------
35 In a radix binary tree, we may have up to 2N-1 nodes for N keys if all of
36 them are leaves. If we find a way to differentiate intermediate nodes (later
37 called "nodes") and final nodes (later called "leaves"), and we associate
38 them by two, it is possible to build sort of a self-contained radix tree with
39 intermediate nodes always present. It will not be as cheap as the ultree for
40 optimal cases as shown below, but the optimal case almost never happens :
41
42 Eg, to store 8, 10, 12, 13, 14 :
43
44 ultree this theorical tree
45
46 8 8
47 / \ / \
48 10 12 10 12
49 / \ / \
50 13 14 12 14
51 / \
52 12 13
53
54 Note that on real-world tests (with a scheduler), is was verified that the
55 case with data on an intermediate node never happens. This is because the
56 data spectrum is too large for such coincidences to happen. It would require
57 for instance that a task has its expiration time at an exact second, with
58 other tasks sharing that second. This is too rare to try to optimize for it.
59
60 What is interesting is that the node will only be added above the leaf when
61 necessary, which implies that it will always remain somewhere above it. So
62 both the leaf and the node can share the exact value of the leaf, because
63 when going down the node, the bit mask will be applied to comparisons. So we
64 are tempted to have one single key shared between the node and the leaf.
65
66 The bit only serves the nodes, and the dups only serve the leaves. So we can
67 put a lot of information in common. This results in one single entity with
68 two branch pointers and two parent pointers, one for the node part, and one
69 for the leaf part :
70
71 node's leaf's
72 parent parent
73 | |
74 [node] [leaf]
75 / \
76 left right
77 branch branch
78
79 The node may very well refer to its leaf counterpart in one of its branches,
80 indicating that its own leaf is just below it :
81
82 node's
83 parent
84 |
85 [node]
86 / \
87 left [leaf]
88 branch
89
90 Adding keys in such a tree simply consists in inserting nodes between
91 other nodes and/or leaves :
92
93 [root]
94 |
95 [node2]
96 / \
97 [leaf1] [node3]
98 / \
99 [leaf2] [leaf3]
100
101 On this diagram, we notice that [node2] and [leaf2] have been pulled away
102 from each other due to the insertion of [node3], just as if there would be
103 an elastic between both parts. This elastic-like behaviour gave its name to
104 the tree : "Elastic Binary Tree", or "EBtree". The entity which associates a
105 node part and a leaf part will be called an "EB node".
106
107 We also notice on the diagram that there is a root entity required to attach
108 the tree. It only contains two branches and there is nothing above it. This
109 is an "EB root". Some will note that [leaf1] has no [node1]. One property of
110 the EBtree is that all nodes have their branches filled, and that if a node
111 has only one branch, it does not need to exist. Here, [leaf1] was added
112 below [root] and did not need any node.
113
114 An EB node contains :
115 - a pointer to the node's parent (node_p)
116 - a pointer to the leaf's parent (leaf_p)
117 - two branches pointing to lower nodes or leaves (branches)
118 - a bit position (bit)
119 - an optional key.
120
121 The key here is optional because it's used only during insertion, in order
122 to classify the nodes. Nothing else in the tree structure requires knowledge
123 of the key. This makes it possible to write type-agnostic primitives for
124 everything, and type-specific insertion primitives. This has led to consider
125 two types of EB nodes. The type-agnostic ones will serve as a header for the
126 other ones, and will simply be called "struct eb_node". The other ones will
127 have their type indicated in the structure name. Eg: "struct eb32_node" for
128 nodes carrying 32 bit keys.
129
130 We will also node that the two branches in a node serve exactly the same
131 purpose as an EB root. For this reason, a "struct eb_root" will be used as
132 well inside the struct eb_node. In order to ease pointer manipulation and
133 ROOT detection when walking upwards, all the pointers inside an eb_node will
134 point to the eb_root part of the referenced EB nodes, relying on the same
135 principle as the linked lists in Linux.
136
137 Another important point to note, is that when walking inside a tree, it is
138 very convenient to know where a node is attached in its parent, and what
139 type of branch it has below it (leaf or node). In order to simplify the
140 operations and to speed up the processing, it was decided in this specific
141 implementation to use the lowest bit from the pointer to designate the side
142 of the upper pointers (left/right) and the type of a branch (leaf/node).
143 This practise is not mandatory by design, but an implementation-specific
144 optimisation permitted on all platforms on which data must be aligned. All
145 known 32 bit platforms align their integers and pointers to 32 bits, leaving
146 the two lower bits unused. So, we say that the pointers are "tagged". And
147 since they designate pointers to root parts, we simply call them
148 "tagged root pointers", or "eb_troot" in the code.
149
150 Duplicate keys are stored in a special manner. When inserting a key, if
151 the same one is found, then an incremental binary tree is built at this
152 place from these keys. This ensures that no special case has to be written
153 to handle duplicates when walking through the tree or when deleting entries.
154 It also guarantees that duplicates will be walked in the exact same order
155 they were inserted. This is very important when trying to achieve fair
156 processing distribution for instance.
157
158 Algorithmic complexity can be derived from 3 variables :
159 - the number of possible different keys in the tree : P
160 - the number of entries in the tree : N
161 - the number of duplicates for one key : D
162
163 Note that this tree is deliberately NOT balanced. For this reason, the worst
164 case may happen with a small tree (eg: 32 distinct keys of one bit). BUT,
165 the operations required to manage such data are so much cheap that they make
166 it worth using it even under such conditions. For instance, a balanced tree
167 may require only 6 levels to store those 32 keys when this tree will
168 require 32. But if per-level operations are 5 times cheaper, it wins.
169
170 Minimal, Maximal and Average times are specified in number of operations.
171 Minimal is given for best condition, Maximal for worst condition, and the
172 average is reported for a tree containing random keys. An operation
173 generally consists in jumping from one node to the other.
174
175 Complexity :
176 - lookup : min=1, max=log(P), avg=log(N)
177 - insertion from root : min=1, max=log(P), avg=log(N)
178 - insertion of dups : min=1, max=log(D), avg=log(D)/2 after lookup
179 - deletion : min=1, max=1, avg=1
180 - prev/next : min=1, max=log(P), avg=2 :
181 N/2 nodes need 1 hop => 1*N/2
182 N/4 nodes need 2 hops => 2*N/4
183 N/8 nodes need 3 hops => 3*N/8
184 ...
185 N/x nodes need log(x) hops => log2(x)*N/x
186 Total cost for all N nodes : sum[i=1..N](log2(i)*N/i) = N*sum[i=1..N](log2(i)/i)
187 Average cost across N nodes = total / N = sum[i=1..N](log2(i)/i) = 2
188
189 This design is currently limited to only two branches per node. Most of the
190 tree descent algorithm would be compatible with more branches (eg: 4, to cut
191 the height in half), but this would probably require more complex operations
192 and the deletion algorithm would be problematic.
193
194 Useful properties :
195 - a node is always added above the leaf it is tied to, and never can get
196 below nor in another branch. This implies that leaves directly attached
197 to the root do not use their node part, which is indicated by a NULL
198 value in node_p. This also enhances the cache efficiency when walking
199 down the tree, because when the leaf is reached, its node part will
200 already have been visited (unless it's the first leaf in the tree).
201
202 - pointers to lower nodes or leaves are stored in "branch" pointers. Only
203 the root node may have a NULL in either branch, it is not possible for
204 other branches. Since the nodes are attached to the left branch of the
205 root, it is not possible to see a NULL left branch when walking up a
206 tree. Thus, an empty tree is immediately identified by a NULL left
207 branch at the root. Conversely, the one and only way to identify the
208 root node is to check that it right branch is NULL.
209
210 - a node connected to its own leaf will have branch[0|1] pointing to
211 itself, and leaf_p pointing to itself.
212
213 - a node can never have node_p pointing to itself.
214
215 - a node is linked in a tree if and only if it has a non-null leaf_p.
216
217 - a node can never have both branches equal, except for the root which can
218 have them both NULL.
219
220 - deletion only applies to leaves. When a leaf is deleted, its parent must
221 be released too (unless it's the root), and its sibling must attach to
222 the grand-parent, replacing the parent. Also, when a leaf is deleted,
223 the node tied to this leaf will be removed and must be released too. If
224 this node is different from the leaf's parent, the freshly released
225 leaf's parent will be used to replace the node which must go. A released
226 node will never be used anymore, so there's no point in tracking it.
227
228 - the bit index in a node indicates the bit position in the key which is
229 represented by the branches. That means that a node with (bit == 0) is
230 just above two leaves. Negative bit values are used to build a duplicate
231 tree. The first node above two identical leaves gets (bit == -1). This
232 value logarithmically decreases as the duplicate tree grows. During
233 duplicate insertion, a node is inserted above the highest bit value (the
234 lowest absolute value) in the tree during the right-sided walk. If bit
235 -1 is not encountered (highest < -1), we insert above last leaf.
236 Otherwise, we insert above the node with the highest value which was not
237 equal to the one of its parent + 1.
238
239 - the "eb_next" primitive walks from left to right, which means from lower
240 to higher keys. It returns duplicates in the order they were inserted.
241 The "eb_first" primitive returns the left-most entry.
242
243 - the "eb_prev" primitive walks from right to left, which means from
244 higher to lower keys. It returns duplicates in the opposite order they
245 were inserted. The "eb_last" primitive returns the right-most entry.
246
247 */
248
249
250#include <stdlib.h>
251
252/* Note: we never need to run fls on null keys, so we can optimize the fls
253 * function by removing a conditional jump.
254 */
255#if defined(__i386__)
256static inline int flsnz(int x)
257{
258 int r;
259 __asm__("bsrl %1,%0\n"
260 : "=r" (r) : "rm" (x));
261 return r+1;
262}
263#else
264// returns 1 to 32 for 1<<0 to 1<<31. Undefined for 0.
265#define flsnz(___a) ({ \
266 register int ___x, ___bits = 0; \
267 ___x = (___a); \
268 if (___x & 0xffff0000) { ___x &= 0xffff0000; ___bits += 16;} \
269 if (___x & 0xff00ff00) { ___x &= 0xff00ff00; ___bits += 8;} \
270 if (___x & 0xf0f0f0f0) { ___x &= 0xf0f0f0f0; ___bits += 4;} \
271 if (___x & 0xcccccccc) { ___x &= 0xcccccccc; ___bits += 2;} \
272 if (___x & 0xaaaaaaaa) { ___x &= 0xaaaaaaaa; ___bits += 1;} \
273 ___bits + 1; \
274 })
275#endif
276
277static inline int fls64(unsigned long long x)
278{
279 unsigned int h;
280 unsigned int bits = 32;
281
282 h = x >> 32;
283 if (!h) {
284 h = x;
285 bits = 0;
286 }
287 return flsnz(h) + bits;
288}
289
290#define fls_auto(x) ((sizeof(x) > 4) ? fls64(x) : flsnz(x))
291
292/* Linux-like "container_of". It returns a pointer to the structure of type
293 * <type> which has its member <name> stored at address <ptr>.
294 */
295#ifndef container_of
296#define container_of(ptr, type, name) ((type *)(((void *)(ptr)) - ((long)&((type *)0)->name)))
297#endif
298
299/*
300 * Gcc >= 3 provides the ability for the program to give hints to the compiler
301 * about what branch of an if is most likely to be taken. This helps the
302 * compiler produce the most compact critical paths, which is generally better
303 * for the cache and to reduce the number of jumps. Be very careful not to use
304 * this in inline functions, because the code reordering it causes very often
305 * has a negative impact on the calling functions.
306 */
307#if __GNUC__ < 3 && !defined(__builtin_expect)
308#define __builtin_expect(x,y) (x)
309#endif
310
311#ifndef likely
312#define likely(x) (__builtin_expect((x) != 0, 1))
313#define unlikely(x) (__builtin_expect((x) != 0, 0))
314#endif
315
316/* Support passing function parameters in registers. For this, the
317 * CONFIG_EBTREE_REGPARM macro has to be set to the maximal number of registers
318 * allowed. Some functions have intentionally received a regparm lower than
319 * their parameter count, it is in order to avoid register clobbering where
320 * they are called.
321 */
322#ifndef REGPRM1
323#if CONFIG_EBTREE_REGPARM >= 1
324#define REGPRM1 __attribute__((regparm(1)))
325#else
326#define REGPRM1
327#endif
328#endif
329
330#ifndef REGPRM2
331#if CONFIG_EBTREE_REGPARM >= 2
332#define REGPRM2 __attribute__((regparm(2)))
333#else
334#define REGPRM2 REGPRM1
335#endif
336#endif
337
338#ifndef REGPRM3
339#if CONFIG_EBTREE_REGPARM >= 3
340#define REGPRM3 __attribute__((regparm(3)))
341#else
342#define REGPRM3 REGPRM2
343#endif
344#endif
345
346/* Number of bits per node, and number of leaves per node */
347#define EB_NODE_BITS 1
348#define EB_NODE_BRANCHES (1 << EB_NODE_BITS)
349#define EB_NODE_BRANCH_MASK (EB_NODE_BRANCHES - 1)
350
351/* Be careful not to tweak those values. The walking code is optimized for NULL
352 * detection on the assumption that the following values are intact.
353 */
354#define EB_LEFT 0
355#define EB_RGHT 1
356#define EB_LEAF 0
357#define EB_NODE 1
358
359/* This is the same as an eb_node pointer, except that the lower bit embeds
360 * a tag. See eb_dotag()/eb_untag()/eb_gettag(). This tag has two meanings :
361 * - 0=left, 1=right to designate the parent's branch for leaf_p/node_p
362 * - 0=link, 1=leaf to designate the branch's type for branch[]
363 */
364typedef void eb_troot_t;
365
366/* The eb_root connects the node which contains it, to two nodes below it, one
367 * of which may be the same node. At the top of the tree, we use an eb_root
368 * too, which always has its right branch NULL.
369 */
370struct eb_root {
371 eb_troot_t *b[EB_NODE_BRANCHES]; /* left and right branches */
372};
373
374/* The eb_node contains the two parts, one for the leaf, which always exists,
375 * and one for the node, which remains unused in the very first node inserted
376 * into the tree. This structure is 20 bytes per node on 32-bit machines. Do
377 * not change the order, benchmarks have shown that it's optimal this way.
378 */
379struct eb_node {
380 struct eb_root branches; /* branches, must be at the beginning */
381 eb_troot_t *node_p; /* link node's parent */
382 eb_troot_t *leaf_p; /* leaf node's parent */
383 int bit; /* link's bit position. */
384};
385
386/* Return the structure of type <type> whose member <member> points to <ptr> */
387#define eb_entry(ptr, type, member) container_of(ptr, type, member)
388
389/* The root of a tree is an eb_root initialized with both pointers NULL.
390 * During its life, only the left pointer will change. The right one will
391 * always remain NULL, which is the way we detect it.
392 */
393#define EB_ROOT \
394 (struct eb_root) { \
395 .b = {[0] = NULL, [1] = NULL }, \
396 }
397
398#define EB_TREE_HEAD(name) \
399 struct eb_root name = EB_ROOT
400
401
402/***************************************\
403 * Private functions. Not for end-user *
404\***************************************/
405
406/* Converts a root pointer to its equivalent eb_troot_t pointer,
407 * ready to be stored in ->branch[], leaf_p or node_p. NULL is not
408 * conserved. To be used with EB_LEAF, EB_NODE, EB_LEFT or EB_RGHT in <tag>.
409 */
410static inline eb_troot_t *eb_dotag(const struct eb_root *root, const int tag)
411{
412 return (eb_troot_t *)((void *)root + tag);
413}
414
415/* Converts an eb_troot_t pointer pointer to its equivalent eb_root pointer,
416 * for use with pointers from ->branch[], leaf_p or node_p. NULL is conserved
417 * as long as the tree is not corrupted. To be used with EB_LEAF, EB_NODE,
418 * EB_LEFT or EB_RGHT in <tag>.
419 */
420static inline struct eb_root *eb_untag(const eb_troot_t *troot, const int tag)
421{
422 return (struct eb_root *)((void *)troot - tag);
423}
424
425/* returns the tag associated with an eb_troot_t pointer */
426static inline int eb_gettag(eb_troot_t *troot)
427{
428 return (unsigned long)troot & 1;
429}
430
431/* Converts a root pointer to its equivalent eb_troot_t pointer and clears the
432 * tag, no matter what its value was.
433 */
434static inline struct eb_root *eb_clrtag(const eb_troot_t *troot)
435{
436 return (struct eb_root *)((unsigned long)troot & ~1UL);
437}
438
439/* Returns a pointer to the eb_node holding <root> */
440static inline struct eb_node *eb_root_to_node(struct eb_root *root)
441{
442 return container_of(root, struct eb_node, branches);
443}
444
445/* Walks down starting at root pointer <start>, and always walking on side
446 * <side>. It either returns the node hosting the first leaf on that side,
447 * or NULL if no leaf is found. <start> may either be NULL or a branch pointer.
448 * The pointer to the leaf (or NULL) is returned.
449 */
450static inline struct eb_node *eb_walk_down(eb_troot_t *start, unsigned int side)
451{
452 /* A NULL pointer on an empty tree root will be returned as-is */
453 while (eb_gettag(start) == EB_NODE)
454 start = (eb_untag(start, EB_NODE))->b[side];
455 /* NULL is left untouched (root==eb_node, EB_LEAF==0) */
456 return eb_root_to_node(eb_untag(start, EB_LEAF));
457}
458
459/* This function is used to build a tree of duplicates by adding a new node to
460 * a subtree of at least 2 entries. It will probably never be needed inlined,
461 * and it is not for end-user.
462 */
463static inline struct eb_node *
464__eb_insert_dup(struct eb_node *sub, struct eb_node *new)
465{
466 struct eb_node *head = sub;
467
468 struct eb_troot *new_left = eb_dotag(&new->branches, EB_LEFT);
469 struct eb_troot *new_rght = eb_dotag(&new->branches, EB_RGHT);
470 struct eb_troot *new_leaf = eb_dotag(&new->branches, EB_LEAF);
471
472 /* first, identify the deepest hole on the right branch */
473 while (eb_gettag(head->branches.b[EB_RGHT]) != EB_LEAF) {
474 struct eb_node *last = head;
475 head = container_of(eb_untag(head->branches.b[EB_RGHT], EB_NODE),
476 struct eb_node, branches);
477 if (head->bit > last->bit + 1)
478 sub = head; /* there's a hole here */
479 }
480
481 /* Here we have a leaf attached to (head)->b[EB_RGHT] */
482 if (head->bit < -1) {
483 /* A hole exists just before the leaf, we insert there */
484 new->bit = -1;
485 sub = container_of(eb_untag(head->branches.b[EB_RGHT], EB_LEAF),
486 struct eb_node, branches);
487 head->branches.b[EB_RGHT] = eb_dotag(&new->branches, EB_NODE);
488
489 new->node_p = sub->leaf_p;
490 new->leaf_p = new_rght;
491 sub->leaf_p = new_left;
492 new->branches.b[EB_LEFT] = eb_dotag(&sub->branches, EB_LEAF);
493 new->branches.b[EB_RGHT] = new_leaf;
494 return new;
495 } else {
496 int side;
497 /* No hole was found before a leaf. We have to insert above
498 * <sub>. Note that we cannot be certain that <sub> is attached
499 * to the right of its parent, as this is only true if <sub>
500 * is inside the dup tree, not at the head.
501 */
502 new->bit = sub->bit - 1; /* install at the lowest level */
503 side = eb_gettag(sub->node_p);
504 head = container_of(eb_untag(sub->node_p, side), struct eb_node, branches);
505 head->branches.b[side] = eb_dotag(&new->branches, EB_NODE);
506
507 new->node_p = sub->node_p;
508 new->leaf_p = new_rght;
509 sub->node_p = new_left;
510 new->branches.b[EB_LEFT] = eb_dotag(&sub->branches, EB_NODE);
511 new->branches.b[EB_RGHT] = new_leaf;
512 return new;
513 }
514}
515
516
517/**************************************\
518 * Public functions, for the end-user *
519\**************************************/
520
521/* Return the first leaf in the tree starting at <root>, or NULL if none */
522static inline struct eb_node *eb_first(struct eb_root *root)
523{
524 return eb_walk_down(root->b[0], EB_LEFT);
525}
526
527/* Return the last leaf in the tree starting at <root>, or NULL if none */
528static inline struct eb_node *eb_last(struct eb_root *root)
529{
530 return eb_walk_down(root->b[0], EB_RGHT);
531}
532
533/* Return previous leaf node before an existing leaf node, or NULL if none. */
534static inline struct eb_node *eb_prev(struct eb_node *node)
535{
536 eb_troot_t *t = node->leaf_p;
537
538 while (eb_gettag(t) == EB_LEFT) {
539 /* Walking up from left branch. We must ensure that we never
540 * walk beyond root.
541 */
542 if (unlikely((eb_untag(t, EB_LEFT))->b[EB_RGHT] == NULL))
543 return NULL;
544 t = (eb_root_to_node(eb_untag(t, EB_LEFT)))->node_p;
545 }
546 /* Note that <t> cannot be NULL at this stage */
547 t = (eb_untag(t, EB_RGHT))->b[EB_LEFT];
548 return eb_walk_down(t, EB_RGHT);
549}
550
551/* Return next leaf node after an existing leaf node, or NULL if none. */
552static inline struct eb_node *eb_next(struct eb_node *node)
553{
554 eb_troot_t *t = node->leaf_p;
555
556 while (eb_gettag(t) != EB_LEFT)
557 /* Walking up from right branch, so we cannot be below root */
558 t = (eb_root_to_node(eb_untag(t, EB_RGHT)))->node_p;
559
560 /* Note that <t> cannot be NULL at this stage */
561 t = (eb_untag(t, EB_LEFT))->b[EB_RGHT];
562 return eb_walk_down(t, EB_LEFT);
563}
564
565/* Return previous leaf node before an existing leaf node, skipping duplicates,
566 * or NULL if none. */
567static inline struct eb_node *eb_prev_unique(struct eb_node *node)
568{
569 eb_troot_t *t = node->leaf_p;
570
571 while (1) {
572 if (eb_gettag(t) != EB_LEFT) {
573 node = eb_root_to_node(eb_untag(t, EB_RGHT));
574 /* if we're right and not in duplicates, stop here */
575 if (node->bit >= 0)
576 break;
577 t = node->node_p;
578 }
579 else {
580 /* Walking up from left branch. We must ensure that we never
581 * walk beyond root.
582 */
583 if (unlikely((eb_untag(t, EB_LEFT))->b[EB_RGHT] == NULL))
584 return NULL;
585 t = (eb_root_to_node(eb_untag(t, EB_LEFT)))->node_p;
586 }
587 }
588 /* Note that <t> cannot be NULL at this stage */
589 t = (eb_untag(t, EB_RGHT))->b[EB_LEFT];
590 return eb_walk_down(t, EB_RGHT);
591}
592
593/* Return next leaf node after an existing leaf node, skipping duplicates, or
594 * NULL if none.
595 */
596static inline struct eb_node *eb_next_unique(struct eb_node *node)
597{
598 eb_troot_t *t = node->leaf_p;
599
600 while (1) {
601 if (eb_gettag(t) == EB_LEFT) {
602 if (unlikely((eb_untag(t, EB_LEFT))->b[EB_RGHT] == NULL))
603 return NULL; /* we reached root */
604 node = eb_root_to_node(eb_untag(t, EB_LEFT));
605 /* if we're left and not in duplicates, stop here */
606 if (node->bit >= 0)
607 break;
608 t = node->node_p;
609 }
610 else {
611 /* Walking up from right branch, so we cannot be below root */
612 t = (eb_root_to_node(eb_untag(t, EB_RGHT)))->node_p;
613 }
614 }
615
616 /* Note that <t> cannot be NULL at this stage */
617 t = (eb_untag(t, EB_LEFT))->b[EB_RGHT];
618 return eb_walk_down(t, EB_LEFT);
619}
620
621
622/* Removes a leaf node from the tree if it was still in it. Marks the node
623 * as unlinked.
624 */
625static inline void __eb_delete(struct eb_node *node)
626{
627 __label__ delete_unlink;
628 unsigned int pside, gpside, sibtype;
629 struct eb_node *parent;
630 struct eb_root *gparent;
631
632 if (!node->leaf_p)
633 return;
634
635 /* we need the parent, our side, and the grand parent */
636 pside = eb_gettag(node->leaf_p);
637 parent = eb_root_to_node(eb_untag(node->leaf_p, pside));
638
639 /* We likely have to release the parent link, unless it's the root,
640 * in which case we only set our branch to NULL. Note that we can
641 * only be attached to the root by its left branch.
642 */
643
644 if (parent->branches.b[EB_RGHT] == NULL) {
645 /* we're just below the root, it's trivial. */
646 parent->branches.b[EB_LEFT] = NULL;
647 goto delete_unlink;
648 }
649
650 /* To release our parent, we have to identify our sibling, and reparent
651 * it directly to/from the grand parent. Note that the sibling can
652 * either be a link or a leaf.
653 */
654
655 gpside = eb_gettag(parent->node_p);
656 gparent = eb_untag(parent->node_p, gpside);
657
658 gparent->b[gpside] = parent->branches.b[!pside];
659 sibtype = eb_gettag(gparent->b[gpside]);
660
661 if (sibtype == EB_LEAF) {
662 eb_root_to_node(eb_untag(gparent->b[gpside], EB_LEAF))->leaf_p =
663 eb_dotag(gparent, gpside);
664 } else {
665 eb_root_to_node(eb_untag(gparent->b[gpside], EB_NODE))->node_p =
666 eb_dotag(gparent, gpside);
667 }
668 /* Mark the parent unused. Note that we do not check if the parent is
669 * our own node, but that's not a problem because if it is, it will be
670 * marked unused at the same time, which we'll use below to know we can
671 * safely remove it.
672 */
673 parent->node_p = NULL;
674
675 /* The parent node has been detached, and is currently unused. It may
676 * belong to another node, so we cannot remove it that way. Also, our
677 * own node part might still be used. so we can use this spare node
678 * to replace ours if needed.
679 */
680
681 /* If our link part is unused, we can safely exit now */
682 if (!node->node_p)
683 goto delete_unlink;
684
685 /* From now on, <node> and <parent> are necessarily different, and the
686 * <node>'s node part is in use. By definition, <parent> is at least
687 * below <node>, so keeping its key for the bit string is OK.
688 */
689
690 parent->node_p = node->node_p;
691 parent->branches = node->branches;
692 parent->bit = node->bit;
693
694 /* We must now update the new node's parent... */
695 gpside = eb_gettag(parent->node_p);
696 gparent = eb_untag(parent->node_p, gpside);
697 gparent->b[gpside] = eb_dotag(&parent->branches, EB_NODE);
698
699 /* ... and its branches */
700 for (pside = 0; pside <= 1; pside++) {
701 if (eb_gettag(parent->branches.b[pside]) == EB_NODE) {
702 eb_root_to_node(eb_untag(parent->branches.b[pside], EB_NODE))->node_p =
703 eb_dotag(&parent->branches, pside);
704 } else {
705 eb_root_to_node(eb_untag(parent->branches.b[pside], EB_LEAF))->leaf_p =
706 eb_dotag(&parent->branches, pside);
707 }
708 }
709 delete_unlink:
710 /* Now the node has been completely unlinked */
711 node->leaf_p = NULL;
712 return; /* tree is not empty yet */
713}
714
715/* These functions are declared in ebtree.c */
716void eb_delete(struct eb_node *node);
717REGPRM1 struct eb_node *eb_insert_dup(struct eb_node *sub, struct eb_node *new);
718
719
720/*
721 * Local variables:
722 * c-indent-level: 8
723 * c-basic-offset: 8
724 * End:
725 */