blob: d756e7a571c7fbe9939dd3fd3b5461ac6bdd32e7 [file] [log] [blame]
Willy Tarreauc2186022009-10-26 19:48:54 +01001/*
2 * Elastic Binary Trees - macros and structures for operations on 64bit nodes.
Willy Tarreau3a932442010-05-09 19:29:23 +02003 * Version 6.0
4 * (C) 2002-2010 - Willy Tarreau <w@1wt.eu>
Willy Tarreauc2186022009-10-26 19:48:54 +01005 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21#ifndef _EB64TREE_H
22#define _EB64TREE_H
23
24#include "ebtree.h"
25
26
27/* Return the structure of type <type> whose member <member> points to <ptr> */
28#define eb64_entry(ptr, type, member) container_of(ptr, type, member)
29
30#define EB64_ROOT EB_ROOT
31#define EB64_TREE_HEAD EB_TREE_HEAD
32
33/* These types may sometimes already be defined */
34typedef unsigned long long u64;
35typedef signed long long s64;
36
37/* This structure carries a node, a leaf, and a key. It must start with the
38 * eb_node so that it can be cast into an eb_node. We could also have put some
39 * sort of transparent union here to reduce the indirection level, but the fact
40 * is, the end user is not meant to manipulate internals, so this is pointless.
41 */
42struct eb64_node {
43 struct eb_node node; /* the tree node, must be at the beginning */
44 u64 key;
45};
46
47/*
48 * Exported functions and macros.
49 * Many of them are always inlined because they are extremely small, and
50 * are generally called at most once or twice in a program.
51 */
52
53/* Return leftmost node in the tree, or NULL if none */
54static inline struct eb64_node *eb64_first(struct eb_root *root)
55{
56 return eb64_entry(eb_first(root), struct eb64_node, node);
57}
58
59/* Return rightmost node in the tree, or NULL if none */
60static inline struct eb64_node *eb64_last(struct eb_root *root)
61{
62 return eb64_entry(eb_last(root), struct eb64_node, node);
63}
64
65/* Return next node in the tree, or NULL if none */
66static inline struct eb64_node *eb64_next(struct eb64_node *eb64)
67{
68 return eb64_entry(eb_next(&eb64->node), struct eb64_node, node);
69}
70
71/* Return previous node in the tree, or NULL if none */
72static inline struct eb64_node *eb64_prev(struct eb64_node *eb64)
73{
74 return eb64_entry(eb_prev(&eb64->node), struct eb64_node, node);
75}
76
77/* Return next node in the tree, skipping duplicates, or NULL if none */
78static inline struct eb64_node *eb64_next_unique(struct eb64_node *eb64)
79{
80 return eb64_entry(eb_next_unique(&eb64->node), struct eb64_node, node);
81}
82
83/* Return previous node in the tree, skipping duplicates, or NULL if none */
84static inline struct eb64_node *eb64_prev_unique(struct eb64_node *eb64)
85{
86 return eb64_entry(eb_prev_unique(&eb64->node), struct eb64_node, node);
87}
88
89/* Delete node from the tree if it was linked in. Mark the node unused. Note
90 * that this function relies on a non-inlined generic function: eb_delete.
91 */
92static inline void eb64_delete(struct eb64_node *eb64)
93{
94 eb_delete(&eb64->node);
95}
96
97/*
98 * The following functions are not inlined by default. They are declared
99 * in eb64tree.c, which simply relies on their inline version.
100 */
101REGPRM2 struct eb64_node *eb64_lookup(struct eb_root *root, u64 x);
102REGPRM2 struct eb64_node *eb64i_lookup(struct eb_root *root, s64 x);
103REGPRM2 struct eb64_node *eb64_lookup_le(struct eb_root *root, u64 x);
104REGPRM2 struct eb64_node *eb64_lookup_ge(struct eb_root *root, u64 x);
105REGPRM2 struct eb64_node *eb64_insert(struct eb_root *root, struct eb64_node *new);
106REGPRM2 struct eb64_node *eb64i_insert(struct eb_root *root, struct eb64_node *new);
107
108/*
109 * The following functions are less likely to be used directly, because their
110 * code is larger. The non-inlined version is preferred.
111 */
112
113/* Delete node from the tree if it was linked in. Mark the node unused. */
114static forceinline void __eb64_delete(struct eb64_node *eb64)
115{
116 __eb_delete(&eb64->node);
117}
118
119/*
120 * Find the first occurence of a key in the tree <root>. If none can be
121 * found, return NULL.
122 */
123static forceinline struct eb64_node *__eb64_lookup(struct eb_root *root, u64 x)
124{
125 struct eb64_node *node;
126 eb_troot_t *troot;
127 u64 y;
Willy Tarreau3a932442010-05-09 19:29:23 +0200128 int node_bit;
Willy Tarreauc2186022009-10-26 19:48:54 +0100129
130 troot = root->b[EB_LEFT];
131 if (unlikely(troot == NULL))
132 return NULL;
133
134 while (1) {
135 if ((eb_gettag(troot) == EB_LEAF)) {
136 node = container_of(eb_untag(troot, EB_LEAF),
137 struct eb64_node, node.branches);
138 if (node->key == x)
139 return node;
140 else
141 return NULL;
142 }
143 node = container_of(eb_untag(troot, EB_NODE),
144 struct eb64_node, node.branches);
Willy Tarreau3a932442010-05-09 19:29:23 +0200145 node_bit = node->node.bit;
Willy Tarreauc2186022009-10-26 19:48:54 +0100146
147 y = node->key ^ x;
148 if (!y) {
149 /* Either we found the node which holds the key, or
150 * we have a dup tree. In the later case, we have to
151 * walk it down left to get the first entry.
152 */
153 if (node->node.bit < 0) {
154 troot = node->node.branches.b[EB_LEFT];
155 while (eb_gettag(troot) != EB_LEAF)
156 troot = (eb_untag(troot, EB_NODE))->b[EB_LEFT];
157 node = container_of(eb_untag(troot, EB_LEAF),
158 struct eb64_node, node.branches);
159 }
160 return node;
161 }
162
163 if ((y >> node->node.bit) >= EB_NODE_BRANCHES)
164 return NULL; /* no more common bits */
165
166 troot = node->node.branches.b[(x >> node->node.bit) & EB_NODE_BRANCH_MASK];
167 }
168}
169
170/*
171 * Find the first occurence of a signed key in the tree <root>. If none can
172 * be found, return NULL.
173 */
174static forceinline struct eb64_node *__eb64i_lookup(struct eb_root *root, s64 x)
175{
176 struct eb64_node *node;
177 eb_troot_t *troot;
178 u64 key = x ^ (1ULL << 63);
179 u64 y;
Willy Tarreau3a932442010-05-09 19:29:23 +0200180 int node_bit;
Willy Tarreauc2186022009-10-26 19:48:54 +0100181
182 troot = root->b[EB_LEFT];
183 if (unlikely(troot == NULL))
184 return NULL;
185
186 while (1) {
187 if ((eb_gettag(troot) == EB_LEAF)) {
188 node = container_of(eb_untag(troot, EB_LEAF),
189 struct eb64_node, node.branches);
Willy Tarreau3a932442010-05-09 19:29:23 +0200190 if (node->key == (u64)x)
Willy Tarreauc2186022009-10-26 19:48:54 +0100191 return node;
192 else
193 return NULL;
194 }
195 node = container_of(eb_untag(troot, EB_NODE),
196 struct eb64_node, node.branches);
Willy Tarreau3a932442010-05-09 19:29:23 +0200197 node_bit = node->node.bit;
Willy Tarreauc2186022009-10-26 19:48:54 +0100198
199 y = node->key ^ x;
200 if (!y) {
201 /* Either we found the node which holds the key, or
202 * we have a dup tree. In the later case, we have to
203 * walk it down left to get the first entry.
204 */
205 if (node->node.bit < 0) {
206 troot = node->node.branches.b[EB_LEFT];
207 while (eb_gettag(troot) != EB_LEAF)
208 troot = (eb_untag(troot, EB_NODE))->b[EB_LEFT];
209 node = container_of(eb_untag(troot, EB_LEAF),
210 struct eb64_node, node.branches);
211 }
212 return node;
213 }
214
215 if ((y >> node->node.bit) >= EB_NODE_BRANCHES)
216 return NULL; /* no more common bits */
217
218 troot = node->node.branches.b[(key >> node->node.bit) & EB_NODE_BRANCH_MASK];
219 }
220}
221
222/* Insert eb64_node <new> into subtree starting at node root <root>.
223 * Only new->key needs be set with the key. The eb64_node is returned.
224 * If root->b[EB_RGHT]==1, the tree may only contain unique keys.
225 */
226static forceinline struct eb64_node *
227__eb64_insert(struct eb_root *root, struct eb64_node *new) {
228 struct eb64_node *old;
229 unsigned int side;
230 eb_troot_t *troot;
231 u64 newkey; /* caching the key saves approximately one cycle */
232 eb_troot_t *root_right = root;
Willy Tarreau3a932442010-05-09 19:29:23 +0200233 int old_node_bit;
Willy Tarreauc2186022009-10-26 19:48:54 +0100234
235 side = EB_LEFT;
236 troot = root->b[EB_LEFT];
237 root_right = root->b[EB_RGHT];
238 if (unlikely(troot == NULL)) {
239 /* Tree is empty, insert the leaf part below the left branch */
240 root->b[EB_LEFT] = eb_dotag(&new->node.branches, EB_LEAF);
241 new->node.leaf_p = eb_dotag(root, EB_LEFT);
242 new->node.node_p = NULL; /* node part unused */
243 return new;
244 }
245
246 /* The tree descent is fairly easy :
247 * - first, check if we have reached a leaf node
248 * - second, check if we have gone too far
249 * - third, reiterate
250 * Everywhere, we use <new> for the node node we are inserting, <root>
251 * for the node we attach it to, and <old> for the node we are
252 * displacing below <new>. <troot> will always point to the future node
253 * (tagged with its type). <side> carries the side the node <new> is
254 * attached to below its parent, which is also where previous node
255 * was attached. <newkey> carries the key being inserted.
256 */
257 newkey = new->key;
258
259 while (1) {
260 if (unlikely(eb_gettag(troot) == EB_LEAF)) {
261 eb_troot_t *new_left, *new_rght;
262 eb_troot_t *new_leaf, *old_leaf;
263
264 old = container_of(eb_untag(troot, EB_LEAF),
265 struct eb64_node, node.branches);
266
267 new_left = eb_dotag(&new->node.branches, EB_LEFT);
268 new_rght = eb_dotag(&new->node.branches, EB_RGHT);
269 new_leaf = eb_dotag(&new->node.branches, EB_LEAF);
270 old_leaf = eb_dotag(&old->node.branches, EB_LEAF);
271
272 new->node.node_p = old->node.leaf_p;
273
274 /* Right here, we have 3 possibilities :
275 - the tree does not contain the key, and we have
276 new->key < old->key. We insert new above old, on
277 the left ;
278
279 - the tree does not contain the key, and we have
280 new->key > old->key. We insert new above old, on
281 the right ;
282
283 - the tree does contain the key, which implies it
284 is alone. We add the new key next to it as a
285 first duplicate.
286
287 The last two cases can easily be partially merged.
288 */
289
290 if (new->key < old->key) {
291 new->node.leaf_p = new_left;
292 old->node.leaf_p = new_rght;
293 new->node.branches.b[EB_LEFT] = new_leaf;
294 new->node.branches.b[EB_RGHT] = old_leaf;
295 } else {
296 /* we may refuse to duplicate this key if the tree is
297 * tagged as containing only unique keys.
298 */
299 if ((new->key == old->key) && eb_gettag(root_right))
300 return old;
301
302 /* new->key >= old->key, new goes the right */
303 old->node.leaf_p = new_left;
304 new->node.leaf_p = new_rght;
305 new->node.branches.b[EB_LEFT] = old_leaf;
306 new->node.branches.b[EB_RGHT] = new_leaf;
307
308 if (new->key == old->key) {
309 new->node.bit = -1;
310 root->b[side] = eb_dotag(&new->node.branches, EB_NODE);
311 return new;
312 }
313 }
314 break;
315 }
316
317 /* OK we're walking down this link */
318 old = container_of(eb_untag(troot, EB_NODE),
319 struct eb64_node, node.branches);
Willy Tarreau3a932442010-05-09 19:29:23 +0200320 old_node_bit = old->node.bit;
Willy Tarreauc2186022009-10-26 19:48:54 +0100321
322 /* Stop going down when we don't have common bits anymore. We
323 * also stop in front of a duplicates tree because it means we
324 * have to insert above.
325 */
326
Willy Tarreau3a932442010-05-09 19:29:23 +0200327 if ((old_node_bit < 0) || /* we're above a duplicate tree, stop here */
328 (((new->key ^ old->key) >> old_node_bit) >= EB_NODE_BRANCHES)) {
Willy Tarreauc2186022009-10-26 19:48:54 +0100329 /* The tree did not contain the key, so we insert <new> before the node
330 * <old>, and set ->bit to designate the lowest bit position in <new>
331 * which applies to ->branches.b[].
332 */
333 eb_troot_t *new_left, *new_rght;
334 eb_troot_t *new_leaf, *old_node;
335
336 new_left = eb_dotag(&new->node.branches, EB_LEFT);
337 new_rght = eb_dotag(&new->node.branches, EB_RGHT);
338 new_leaf = eb_dotag(&new->node.branches, EB_LEAF);
339 old_node = eb_dotag(&old->node.branches, EB_NODE);
340
341 new->node.node_p = old->node.node_p;
342
343 if (new->key < old->key) {
344 new->node.leaf_p = new_left;
345 old->node.node_p = new_rght;
346 new->node.branches.b[EB_LEFT] = new_leaf;
347 new->node.branches.b[EB_RGHT] = old_node;
348 }
349 else if (new->key > old->key) {
350 old->node.node_p = new_left;
351 new->node.leaf_p = new_rght;
352 new->node.branches.b[EB_LEFT] = old_node;
353 new->node.branches.b[EB_RGHT] = new_leaf;
354 }
355 else {
356 struct eb_node *ret;
357 ret = eb_insert_dup(&old->node, &new->node);
358 return container_of(ret, struct eb64_node, node);
359 }
360 break;
361 }
362
363 /* walk down */
364 root = &old->node.branches;
365#if BITS_PER_LONG >= 64
Willy Tarreau3a932442010-05-09 19:29:23 +0200366 side = (newkey >> old_node_bit) & EB_NODE_BRANCH_MASK;
Willy Tarreauc2186022009-10-26 19:48:54 +0100367#else
368 side = newkey;
Willy Tarreau3a932442010-05-09 19:29:23 +0200369 side >>= old_node_bit;
370 if (old_node_bit >= 32) {
Willy Tarreauc2186022009-10-26 19:48:54 +0100371 side = newkey >> 32;
Willy Tarreau3a932442010-05-09 19:29:23 +0200372 side >>= old_node_bit & 0x1F;
Willy Tarreauc2186022009-10-26 19:48:54 +0100373 }
374 side &= EB_NODE_BRANCH_MASK;
375#endif
376 troot = root->b[side];
377 }
378
379 /* Ok, now we are inserting <new> between <root> and <old>. <old>'s
380 * parent is already set to <new>, and the <root>'s branch is still in
381 * <side>. Update the root's leaf till we have it. Note that we can also
382 * find the side by checking the side of new->node.node_p.
383 */
384
385 /* We need the common higher bits between new->key and old->key.
386 * What differences are there between new->key and the node here ?
387 * NOTE that bit(new) is always < bit(root) because highest
388 * bit of new->key and old->key are identical here (otherwise they
389 * would sit on different branches).
390 */
391 // note that if EB_NODE_BITS > 1, we should check that it's still >= 0
392 new->node.bit = fls64(new->key ^ old->key) - EB_NODE_BITS;
393 root->b[side] = eb_dotag(&new->node.branches, EB_NODE);
394
395 return new;
396}
397
398/* Insert eb64_node <new> into subtree starting at node root <root>, using
399 * signed keys. Only new->key needs be set with the key. The eb64_node
400 * is returned. If root->b[EB_RGHT]==1, the tree may only contain unique keys.
401 */
402static forceinline struct eb64_node *
403__eb64i_insert(struct eb_root *root, struct eb64_node *new) {
404 struct eb64_node *old;
405 unsigned int side;
406 eb_troot_t *troot;
407 u64 newkey; /* caching the key saves approximately one cycle */
408 eb_troot_t *root_right = root;
Willy Tarreau3a932442010-05-09 19:29:23 +0200409 int old_node_bit;
Willy Tarreauc2186022009-10-26 19:48:54 +0100410
411 side = EB_LEFT;
412 troot = root->b[EB_LEFT];
413 root_right = root->b[EB_RGHT];
414 if (unlikely(troot == NULL)) {
415 /* Tree is empty, insert the leaf part below the left branch */
416 root->b[EB_LEFT] = eb_dotag(&new->node.branches, EB_LEAF);
417 new->node.leaf_p = eb_dotag(root, EB_LEFT);
418 new->node.node_p = NULL; /* node part unused */
419 return new;
420 }
421
422 /* The tree descent is fairly easy :
423 * - first, check if we have reached a leaf node
424 * - second, check if we have gone too far
425 * - third, reiterate
426 * Everywhere, we use <new> for the node node we are inserting, <root>
427 * for the node we attach it to, and <old> for the node we are
428 * displacing below <new>. <troot> will always point to the future node
429 * (tagged with its type). <side> carries the side the node <new> is
430 * attached to below its parent, which is also where previous node
431 * was attached. <newkey> carries a high bit shift of the key being
432 * inserted in order to have negative keys stored before positive
433 * ones.
434 */
435 newkey = new->key ^ (1ULL << 63);
436
437 while (1) {
438 if (unlikely(eb_gettag(troot) == EB_LEAF)) {
439 eb_troot_t *new_left, *new_rght;
440 eb_troot_t *new_leaf, *old_leaf;
441
442 old = container_of(eb_untag(troot, EB_LEAF),
443 struct eb64_node, node.branches);
444
445 new_left = eb_dotag(&new->node.branches, EB_LEFT);
446 new_rght = eb_dotag(&new->node.branches, EB_RGHT);
447 new_leaf = eb_dotag(&new->node.branches, EB_LEAF);
448 old_leaf = eb_dotag(&old->node.branches, EB_LEAF);
449
450 new->node.node_p = old->node.leaf_p;
451
452 /* Right here, we have 3 possibilities :
453 - the tree does not contain the key, and we have
454 new->key < old->key. We insert new above old, on
455 the left ;
456
457 - the tree does not contain the key, and we have
458 new->key > old->key. We insert new above old, on
459 the right ;
460
461 - the tree does contain the key, which implies it
462 is alone. We add the new key next to it as a
463 first duplicate.
464
465 The last two cases can easily be partially merged.
466 */
467
468 if ((s64)new->key < (s64)old->key) {
469 new->node.leaf_p = new_left;
470 old->node.leaf_p = new_rght;
471 new->node.branches.b[EB_LEFT] = new_leaf;
472 new->node.branches.b[EB_RGHT] = old_leaf;
473 } else {
474 /* we may refuse to duplicate this key if the tree is
475 * tagged as containing only unique keys.
476 */
477 if ((new->key == old->key) && eb_gettag(root_right))
478 return old;
479
480 /* new->key >= old->key, new goes the right */
481 old->node.leaf_p = new_left;
482 new->node.leaf_p = new_rght;
483 new->node.branches.b[EB_LEFT] = old_leaf;
484 new->node.branches.b[EB_RGHT] = new_leaf;
485
486 if (new->key == old->key) {
487 new->node.bit = -1;
488 root->b[side] = eb_dotag(&new->node.branches, EB_NODE);
489 return new;
490 }
491 }
492 break;
493 }
494
495 /* OK we're walking down this link */
496 old = container_of(eb_untag(troot, EB_NODE),
497 struct eb64_node, node.branches);
Willy Tarreau3a932442010-05-09 19:29:23 +0200498 old_node_bit = old->node.bit;
Willy Tarreauc2186022009-10-26 19:48:54 +0100499
500 /* Stop going down when we don't have common bits anymore. We
501 * also stop in front of a duplicates tree because it means we
502 * have to insert above.
503 */
504
Willy Tarreau3a932442010-05-09 19:29:23 +0200505 if ((old_node_bit < 0) || /* we're above a duplicate tree, stop here */
506 (((new->key ^ old->key) >> old_node_bit) >= EB_NODE_BRANCHES)) {
Willy Tarreauc2186022009-10-26 19:48:54 +0100507 /* The tree did not contain the key, so we insert <new> before the node
508 * <old>, and set ->bit to designate the lowest bit position in <new>
509 * which applies to ->branches.b[].
510 */
511 eb_troot_t *new_left, *new_rght;
512 eb_troot_t *new_leaf, *old_node;
513
514 new_left = eb_dotag(&new->node.branches, EB_LEFT);
515 new_rght = eb_dotag(&new->node.branches, EB_RGHT);
516 new_leaf = eb_dotag(&new->node.branches, EB_LEAF);
517 old_node = eb_dotag(&old->node.branches, EB_NODE);
518
519 new->node.node_p = old->node.node_p;
520
521 if ((s64)new->key < (s64)old->key) {
522 new->node.leaf_p = new_left;
523 old->node.node_p = new_rght;
524 new->node.branches.b[EB_LEFT] = new_leaf;
525 new->node.branches.b[EB_RGHT] = old_node;
526 }
527 else if ((s64)new->key > (s64)old->key) {
528 old->node.node_p = new_left;
529 new->node.leaf_p = new_rght;
530 new->node.branches.b[EB_LEFT] = old_node;
531 new->node.branches.b[EB_RGHT] = new_leaf;
532 }
533 else {
534 struct eb_node *ret;
535 ret = eb_insert_dup(&old->node, &new->node);
536 return container_of(ret, struct eb64_node, node);
537 }
538 break;
539 }
540
541 /* walk down */
542 root = &old->node.branches;
543#if BITS_PER_LONG >= 64
Willy Tarreau3a932442010-05-09 19:29:23 +0200544 side = (newkey >> old_node_bit) & EB_NODE_BRANCH_MASK;
Willy Tarreauc2186022009-10-26 19:48:54 +0100545#else
546 side = newkey;
Willy Tarreau3a932442010-05-09 19:29:23 +0200547 side >>= old_node_bit;
548 if (old_node_bit >= 32) {
Willy Tarreauc2186022009-10-26 19:48:54 +0100549 side = newkey >> 32;
Willy Tarreau3a932442010-05-09 19:29:23 +0200550 side >>= old_node_bit & 0x1F;
Willy Tarreauc2186022009-10-26 19:48:54 +0100551 }
552 side &= EB_NODE_BRANCH_MASK;
553#endif
554 troot = root->b[side];
555 }
556
557 /* Ok, now we are inserting <new> between <root> and <old>. <old>'s
558 * parent is already set to <new>, and the <root>'s branch is still in
559 * <side>. Update the root's leaf till we have it. Note that we can also
560 * find the side by checking the side of new->node.node_p.
561 */
562
563 /* We need the common higher bits between new->key and old->key.
564 * What differences are there between new->key and the node here ?
565 * NOTE that bit(new) is always < bit(root) because highest
566 * bit of new->key and old->key are identical here (otherwise they
567 * would sit on different branches).
568 */
569 // note that if EB_NODE_BITS > 1, we should check that it's still >= 0
570 new->node.bit = fls64(new->key ^ old->key) - EB_NODE_BITS;
571 root->b[side] = eb_dotag(&new->node.branches, EB_NODE);
572
573 return new;
574}
575
576#endif /* _EB64_TREE_H */