blob: 04f57ec926374b4e63ff256efbf8942048ce0f13 [file] [log] [blame]
Willy Tarreaue6d2e4d2007-11-15 23:56:17 +01001/*
2 * Elastic Binary Trees - macros and structures for operations on 64bit nodes.
Willy Tarreau1fb6c872008-05-16 19:48:20 +02003 * Version 4.0
4 * (C) 2002-2008 - Willy Tarreau <w@1wt.eu>
Willy Tarreaue6d2e4d2007-11-15 23:56:17 +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
Willy Tarreauf56fd8a2007-11-19 18:43:04 +010021#ifndef _COMMON_EB64TREE_H
22#define _COMMON_EB64TREE_H
23
Willy Tarreaue6d2e4d2007-11-15 23:56:17 +010024#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_insert(struct eb_root *root, struct eb64_node *new);
104REGPRM2 struct eb64_node *eb64i_insert(struct eb_root *root, struct eb64_node *new);
105
106/*
107 * The following functions are less likely to be used directly, because their
108 * code is larger. The non-inlined version is preferred.
109 */
110
111/* Delete node from the tree if it was linked in. Mark the node unused. */
Willy Tarreau75cf17e2008-08-29 15:48:49 +0200112static forceinline void __eb64_delete(struct eb64_node *eb64)
Willy Tarreaue6d2e4d2007-11-15 23:56:17 +0100113{
114 __eb_delete(&eb64->node);
115}
116
117/*
118 * Find the first occurence of a key in the tree <root>. If none can be
119 * found, return NULL.
120 */
Willy Tarreau75cf17e2008-08-29 15:48:49 +0200121static forceinline struct eb64_node *__eb64_lookup(struct eb_root *root, u64 x)
Willy Tarreaue6d2e4d2007-11-15 23:56:17 +0100122{
123 struct eb64_node *node;
124 eb_troot_t *troot;
Willy Tarreau58044342009-03-21 07:40:32 +0100125 u64 y;
Willy Tarreaue6d2e4d2007-11-15 23:56:17 +0100126
127 troot = root->b[EB_LEFT];
128 if (unlikely(troot == NULL))
129 return NULL;
130
131 while (1) {
132 if ((eb_gettag(troot) == EB_LEAF)) {
133 node = container_of(eb_untag(troot, EB_LEAF),
134 struct eb64_node, node.branches);
135 if (node->key == x)
136 return node;
137 else
138 return NULL;
139 }
140 node = container_of(eb_untag(troot, EB_NODE),
141 struct eb64_node, node.branches);
142
Willy Tarreau58044342009-03-21 07:40:32 +0100143 y = node->key ^ x;
144 if (!y) {
Willy Tarreaue6d2e4d2007-11-15 23:56:17 +0100145 /* Either we found the node which holds the key, or
146 * we have a dup tree. In the later case, we have to
147 * walk it down left to get the first entry.
148 */
149 if (node->node.bit < 0) {
150 troot = node->node.branches.b[EB_LEFT];
151 while (eb_gettag(troot) != EB_LEAF)
152 troot = (eb_untag(troot, EB_NODE))->b[EB_LEFT];
153 node = container_of(eb_untag(troot, EB_LEAF),
154 struct eb64_node, node.branches);
155 }
156 return node;
157 }
158
Willy Tarreau58044342009-03-21 07:40:32 +0100159 if ((y >> node->node.bit) >= EB_NODE_BRANCHES)
160 return NULL; /* no more common bits */
161
Willy Tarreaue6d2e4d2007-11-15 23:56:17 +0100162 troot = node->node.branches.b[(x >> node->node.bit) & EB_NODE_BRANCH_MASK];
163 }
164}
165
166/*
167 * Find the first occurence of a signed key in the tree <root>. If none can
168 * be found, return NULL.
169 */
Willy Tarreau75cf17e2008-08-29 15:48:49 +0200170static forceinline struct eb64_node *__eb64i_lookup(struct eb_root *root, s64 x)
Willy Tarreaue6d2e4d2007-11-15 23:56:17 +0100171{
172 struct eb64_node *node;
173 eb_troot_t *troot;
174 u64 key = x ^ (1ULL << 63);
Willy Tarreau58044342009-03-21 07:40:32 +0100175 u64 y;
Willy Tarreaue6d2e4d2007-11-15 23:56:17 +0100176
177 troot = root->b[EB_LEFT];
178 if (unlikely(troot == NULL))
179 return NULL;
180
181 while (1) {
182 if ((eb_gettag(troot) == EB_LEAF)) {
183 node = container_of(eb_untag(troot, EB_LEAF),
184 struct eb64_node, node.branches);
185 if (node->key == x)
186 return node;
187 else
188 return NULL;
189 }
190 node = container_of(eb_untag(troot, EB_NODE),
191 struct eb64_node, node.branches);
192
Willy Tarreau58044342009-03-21 07:40:32 +0100193 y = node->key ^ x;
194 if (!y) {
Willy Tarreaue6d2e4d2007-11-15 23:56:17 +0100195 /* Either we found the node which holds the key, or
196 * we have a dup tree. In the later case, we have to
197 * walk it down left to get the first entry.
198 */
199 if (node->node.bit < 0) {
200 troot = node->node.branches.b[EB_LEFT];
201 while (eb_gettag(troot) != EB_LEAF)
202 troot = (eb_untag(troot, EB_NODE))->b[EB_LEFT];
203 node = container_of(eb_untag(troot, EB_LEAF),
204 struct eb64_node, node.branches);
205 }
206 return node;
207 }
208
Willy Tarreau58044342009-03-21 07:40:32 +0100209 if ((y >> node->node.bit) >= EB_NODE_BRANCHES)
210 return NULL; /* no more common bits */
211
Willy Tarreaue6d2e4d2007-11-15 23:56:17 +0100212 troot = node->node.branches.b[(key >> node->node.bit) & EB_NODE_BRANCH_MASK];
213 }
214}
215
216/* Insert eb64_node <new> into subtree starting at node root <root>.
217 * Only new->key needs be set with the key. The eb64_node is returned.
Willy Tarreau1fb6c872008-05-16 19:48:20 +0200218 * If root->b[EB_RGHT]==1, the tree may only contain unique keys.
Willy Tarreaue6d2e4d2007-11-15 23:56:17 +0100219 */
Willy Tarreau75cf17e2008-08-29 15:48:49 +0200220static forceinline struct eb64_node *
Willy Tarreaue6d2e4d2007-11-15 23:56:17 +0100221__eb64_insert(struct eb_root *root, struct eb64_node *new) {
222 struct eb64_node *old;
223 unsigned int side;
224 eb_troot_t *troot;
225 u64 newkey; /* caching the key saves approximately one cycle */
Willy Tarreau1fb6c872008-05-16 19:48:20 +0200226 eb_troot_t *root_right = root;
Willy Tarreaue6d2e4d2007-11-15 23:56:17 +0100227
228 side = EB_LEFT;
229 troot = root->b[EB_LEFT];
Willy Tarreau1fb6c872008-05-16 19:48:20 +0200230 root_right = root->b[EB_RGHT];
Willy Tarreaue6d2e4d2007-11-15 23:56:17 +0100231 if (unlikely(troot == NULL)) {
232 /* Tree is empty, insert the leaf part below the left branch */
233 root->b[EB_LEFT] = eb_dotag(&new->node.branches, EB_LEAF);
234 new->node.leaf_p = eb_dotag(root, EB_LEFT);
235 new->node.node_p = NULL; /* node part unused */
236 return new;
237 }
238
239 /* The tree descent is fairly easy :
240 * - first, check if we have reached a leaf node
241 * - second, check if we have gone too far
242 * - third, reiterate
243 * Everywhere, we use <new> for the node node we are inserting, <root>
244 * for the node we attach it to, and <old> for the node we are
245 * displacing below <new>. <troot> will always point to the future node
246 * (tagged with its type). <side> carries the side the node <new> is
247 * attached to below its parent, which is also where previous node
248 * was attached. <newkey> carries the key being inserted.
249 */
250 newkey = new->key;
251
252 while (1) {
253 if (unlikely(eb_gettag(troot) == EB_LEAF)) {
254 eb_troot_t *new_left, *new_rght;
255 eb_troot_t *new_leaf, *old_leaf;
256
257 old = container_of(eb_untag(troot, EB_LEAF),
258 struct eb64_node, node.branches);
259
260 new_left = eb_dotag(&new->node.branches, EB_LEFT);
261 new_rght = eb_dotag(&new->node.branches, EB_RGHT);
262 new_leaf = eb_dotag(&new->node.branches, EB_LEAF);
263 old_leaf = eb_dotag(&old->node.branches, EB_LEAF);
264
265 new->node.node_p = old->node.leaf_p;
266
267 /* Right here, we have 3 possibilities :
268 - the tree does not contain the key, and we have
269 new->key < old->key. We insert new above old, on
270 the left ;
271
272 - the tree does not contain the key, and we have
273 new->key > old->key. We insert new above old, on
274 the right ;
275
276 - the tree does contain the key, which implies it
277 is alone. We add the new key next to it as a
278 first duplicate.
279
280 The last two cases can easily be partially merged.
281 */
282
283 if (new->key < old->key) {
284 new->node.leaf_p = new_left;
285 old->node.leaf_p = new_rght;
286 new->node.branches.b[EB_LEFT] = new_leaf;
287 new->node.branches.b[EB_RGHT] = old_leaf;
288 } else {
Willy Tarreau1fb6c872008-05-16 19:48:20 +0200289 /* we may refuse to duplicate this key if the tree is
290 * tagged as containing only unique keys.
291 */
292 if ((new->key == old->key) && eb_gettag(root_right))
293 return old;
294
Willy Tarreaue6d2e4d2007-11-15 23:56:17 +0100295 /* new->key >= old->key, new goes the right */
296 old->node.leaf_p = new_left;
297 new->node.leaf_p = new_rght;
298 new->node.branches.b[EB_LEFT] = old_leaf;
299 new->node.branches.b[EB_RGHT] = new_leaf;
300
301 if (new->key == old->key) {
302 new->node.bit = -1;
303 root->b[side] = eb_dotag(&new->node.branches, EB_NODE);
304 return new;
305 }
306 }
307 break;
308 }
309
310 /* OK we're walking down this link */
311 old = container_of(eb_untag(troot, EB_NODE),
312 struct eb64_node, node.branches);
313
314 /* Stop going down when we don't have common bits anymore. We
315 * also stop in front of a duplicates tree because it means we
316 * have to insert above.
317 */
318
319 if ((old->node.bit < 0) || /* we're above a duplicate tree, stop here */
320 (((new->key ^ old->key) >> old->node.bit) >= EB_NODE_BRANCHES)) {
321 /* The tree did not contain the key, so we insert <new> before the node
322 * <old>, and set ->bit to designate the lowest bit position in <new>
323 * which applies to ->branches.b[].
324 */
325 eb_troot_t *new_left, *new_rght;
326 eb_troot_t *new_leaf, *old_node;
327
328 new_left = eb_dotag(&new->node.branches, EB_LEFT);
329 new_rght = eb_dotag(&new->node.branches, EB_RGHT);
330 new_leaf = eb_dotag(&new->node.branches, EB_LEAF);
331 old_node = eb_dotag(&old->node.branches, EB_NODE);
332
333 new->node.node_p = old->node.node_p;
334
335 if (new->key < old->key) {
336 new->node.leaf_p = new_left;
337 old->node.node_p = new_rght;
338 new->node.branches.b[EB_LEFT] = new_leaf;
339 new->node.branches.b[EB_RGHT] = old_node;
340 }
341 else if (new->key > old->key) {
342 old->node.node_p = new_left;
343 new->node.leaf_p = new_rght;
344 new->node.branches.b[EB_LEFT] = old_node;
345 new->node.branches.b[EB_RGHT] = new_leaf;
346 }
347 else {
348 struct eb_node *ret;
349 ret = eb_insert_dup(&old->node, &new->node);
350 return container_of(ret, struct eb64_node, node);
351 }
352 break;
353 }
354
355 /* walk down */
356 root = &old->node.branches;
357#if BITS_PER_LONG >= 64
358 side = (newkey >> old->node.bit) & EB_NODE_BRANCH_MASK;
359#else
360 side = newkey;
361 side >>= old->node.bit;
362 if (old->node.bit >= 32) {
363 side = newkey >> 32;
364 side >>= old->node.bit & 0x1F;
365 }
366 side &= EB_NODE_BRANCH_MASK;
367#endif
368 troot = root->b[side];
369 }
370
371 /* Ok, now we are inserting <new> between <root> and <old>. <old>'s
372 * parent is already set to <new>, and the <root>'s branch is still in
373 * <side>. Update the root's leaf till we have it. Note that we can also
374 * find the side by checking the side of new->node.node_p.
375 */
376
377 /* We need the common higher bits between new->key and old->key.
378 * What differences are there between new->key and the node here ?
379 * NOTE that bit(new) is always < bit(root) because highest
380 * bit of new->key and old->key are identical here (otherwise they
381 * would sit on different branches).
382 */
383 // note that if EB_NODE_BITS > 1, we should check that it's still >= 0
384 new->node.bit = fls64(new->key ^ old->key) - EB_NODE_BITS;
385 root->b[side] = eb_dotag(&new->node.branches, EB_NODE);
386
387 return new;
388}
389
390/* Insert eb64_node <new> into subtree starting at node root <root>, using
391 * signed keys. Only new->key needs be set with the key. The eb64_node
Willy Tarreau1fb6c872008-05-16 19:48:20 +0200392 * is returned. If root->b[EB_RGHT]==1, the tree may only contain unique keys.
Willy Tarreaue6d2e4d2007-11-15 23:56:17 +0100393 */
Willy Tarreau75cf17e2008-08-29 15:48:49 +0200394static forceinline struct eb64_node *
Willy Tarreaue6d2e4d2007-11-15 23:56:17 +0100395__eb64i_insert(struct eb_root *root, struct eb64_node *new) {
396 struct eb64_node *old;
397 unsigned int side;
398 eb_troot_t *troot;
399 u64 newkey; /* caching the key saves approximately one cycle */
Willy Tarreau1fb6c872008-05-16 19:48:20 +0200400 eb_troot_t *root_right = root;
Willy Tarreaue6d2e4d2007-11-15 23:56:17 +0100401
402 side = EB_LEFT;
403 troot = root->b[EB_LEFT];
Willy Tarreau1fb6c872008-05-16 19:48:20 +0200404 root_right = root->b[EB_RGHT];
Willy Tarreaue6d2e4d2007-11-15 23:56:17 +0100405 if (unlikely(troot == NULL)) {
406 /* Tree is empty, insert the leaf part below the left branch */
407 root->b[EB_LEFT] = eb_dotag(&new->node.branches, EB_LEAF);
408 new->node.leaf_p = eb_dotag(root, EB_LEFT);
409 new->node.node_p = NULL; /* node part unused */
410 return new;
411 }
412
413 /* The tree descent is fairly easy :
414 * - first, check if we have reached a leaf node
415 * - second, check if we have gone too far
416 * - third, reiterate
417 * Everywhere, we use <new> for the node node we are inserting, <root>
418 * for the node we attach it to, and <old> for the node we are
419 * displacing below <new>. <troot> will always point to the future node
420 * (tagged with its type). <side> carries the side the node <new> is
421 * attached to below its parent, which is also where previous node
422 * was attached. <newkey> carries a high bit shift of the key being
423 * inserted in order to have negative keys stored before positive
424 * ones.
425 */
426 newkey = new->key ^ (1ULL << 63);
427
428 while (1) {
429 if (unlikely(eb_gettag(troot) == EB_LEAF)) {
430 eb_troot_t *new_left, *new_rght;
431 eb_troot_t *new_leaf, *old_leaf;
432
433 old = container_of(eb_untag(troot, EB_LEAF),
434 struct eb64_node, node.branches);
435
436 new_left = eb_dotag(&new->node.branches, EB_LEFT);
437 new_rght = eb_dotag(&new->node.branches, EB_RGHT);
438 new_leaf = eb_dotag(&new->node.branches, EB_LEAF);
439 old_leaf = eb_dotag(&old->node.branches, EB_LEAF);
440
441 new->node.node_p = old->node.leaf_p;
442
443 /* Right here, we have 3 possibilities :
444 - the tree does not contain the key, and we have
445 new->key < old->key. We insert new above old, on
446 the left ;
447
448 - the tree does not contain the key, and we have
449 new->key > old->key. We insert new above old, on
450 the right ;
451
452 - the tree does contain the key, which implies it
453 is alone. We add the new key next to it as a
454 first duplicate.
455
456 The last two cases can easily be partially merged.
457 */
458
459 if ((s64)new->key < (s64)old->key) {
460 new->node.leaf_p = new_left;
461 old->node.leaf_p = new_rght;
462 new->node.branches.b[EB_LEFT] = new_leaf;
463 new->node.branches.b[EB_RGHT] = old_leaf;
464 } else {
Willy Tarreau1fb6c872008-05-16 19:48:20 +0200465 /* we may refuse to duplicate this key if the tree is
466 * tagged as containing only unique keys.
467 */
468 if ((new->key == old->key) && eb_gettag(root_right))
469 return old;
470
Willy Tarreaue6d2e4d2007-11-15 23:56:17 +0100471 /* new->key >= old->key, new goes the right */
472 old->node.leaf_p = new_left;
473 new->node.leaf_p = new_rght;
474 new->node.branches.b[EB_LEFT] = old_leaf;
475 new->node.branches.b[EB_RGHT] = new_leaf;
476
477 if (new->key == old->key) {
478 new->node.bit = -1;
479 root->b[side] = eb_dotag(&new->node.branches, EB_NODE);
480 return new;
481 }
482 }
483 break;
484 }
485
486 /* OK we're walking down this link */
487 old = container_of(eb_untag(troot, EB_NODE),
488 struct eb64_node, node.branches);
489
490 /* Stop going down when we don't have common bits anymore. We
491 * also stop in front of a duplicates tree because it means we
492 * have to insert above.
493 */
494
495 if ((old->node.bit < 0) || /* we're above a duplicate tree, stop here */
496 (((new->key ^ old->key) >> old->node.bit) >= EB_NODE_BRANCHES)) {
497 /* The tree did not contain the key, so we insert <new> before the node
498 * <old>, and set ->bit to designate the lowest bit position in <new>
499 * which applies to ->branches.b[].
500 */
501 eb_troot_t *new_left, *new_rght;
502 eb_troot_t *new_leaf, *old_node;
503
504 new_left = eb_dotag(&new->node.branches, EB_LEFT);
505 new_rght = eb_dotag(&new->node.branches, EB_RGHT);
506 new_leaf = eb_dotag(&new->node.branches, EB_LEAF);
507 old_node = eb_dotag(&old->node.branches, EB_NODE);
508
509 new->node.node_p = old->node.node_p;
510
511 if ((s64)new->key < (s64)old->key) {
512 new->node.leaf_p = new_left;
513 old->node.node_p = new_rght;
514 new->node.branches.b[EB_LEFT] = new_leaf;
515 new->node.branches.b[EB_RGHT] = old_node;
516 }
517 else if ((s64)new->key > (s64)old->key) {
518 old->node.node_p = new_left;
519 new->node.leaf_p = new_rght;
520 new->node.branches.b[EB_LEFT] = old_node;
521 new->node.branches.b[EB_RGHT] = new_leaf;
522 }
523 else {
524 struct eb_node *ret;
525 ret = eb_insert_dup(&old->node, &new->node);
526 return container_of(ret, struct eb64_node, node);
527 }
528 break;
529 }
530
531 /* walk down */
532 root = &old->node.branches;
533#if BITS_PER_LONG >= 64
534 side = (newkey >> old->node.bit) & EB_NODE_BRANCH_MASK;
535#else
536 side = newkey;
537 side >>= old->node.bit;
538 if (old->node.bit >= 32) {
539 side = newkey >> 32;
540 side >>= old->node.bit & 0x1F;
541 }
542 side &= EB_NODE_BRANCH_MASK;
543#endif
544 troot = root->b[side];
545 }
546
547 /* Ok, now we are inserting <new> between <root> and <old>. <old>'s
548 * parent is already set to <new>, and the <root>'s branch is still in
549 * <side>. Update the root's leaf till we have it. Note that we can also
550 * find the side by checking the side of new->node.node_p.
551 */
552
553 /* We need the common higher bits between new->key and old->key.
554 * What differences are there between new->key and the node here ?
555 * NOTE that bit(new) is always < bit(root) because highest
556 * bit of new->key and old->key are identical here (otherwise they
557 * would sit on different branches).
558 */
559 // note that if EB_NODE_BITS > 1, we should check that it's still >= 0
560 new->node.bit = fls64(new->key ^ old->key) - EB_NODE_BITS;
561 root->b[side] = eb_dotag(&new->node.branches, EB_NODE);
562
563 return new;
564}
565
Willy Tarreauf56fd8a2007-11-19 18:43:04 +0100566#endif /* _COMMON_EB64TREE_H */