blob: b97e0c41e6e1571c581e968c303e99f6331b9b1e [file] [log] [blame]
Willy Tarreauc2186022009-10-26 19:48:54 +01001/*
2 * Elastic Binary Trees - macros and structures for operations on 64bit nodes.
Willy Tarreauf3bfede2011-07-25 11:38:17 +02003 * Version 6.0.6
4 * (C) 2002-2011 - Willy Tarreau <w@1wt.eu>
Willy Tarreauc2186022009-10-26 19:48:54 +01005 *
Willy Tarreauf3bfede2011-07-25 11:38:17 +02006 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation, version 2.1
9 * exclusively.
Willy Tarreauc2186022009-10-26 19:48:54 +010010 *
Willy Tarreauf3bfede2011-07-25 11:38:17 +020011 * This library is distributed in the hope that it will be useful,
Willy Tarreauc2186022009-10-26 19:48:54 +010012 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Willy Tarreauf3bfede2011-07-25 11:38:17 +020013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
Willy Tarreauc2186022009-10-26 19:48:54 +010015 *
Willy Tarreauf3bfede2011-07-25 11:38:17 +020016 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
Willy Tarreauc2186022009-10-26 19:48:54 +010019 */
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;
128
129 troot = root->b[EB_LEFT];
130 if (unlikely(troot == NULL))
131 return NULL;
132
133 while (1) {
134 if ((eb_gettag(troot) == EB_LEAF)) {
135 node = container_of(eb_untag(troot, EB_LEAF),
136 struct eb64_node, node.branches);
137 if (node->key == x)
138 return node;
139 else
140 return NULL;
141 }
142 node = container_of(eb_untag(troot, EB_NODE),
143 struct eb64_node, node.branches);
144
145 y = node->key ^ x;
146 if (!y) {
147 /* Either we found the node which holds the key, or
148 * we have a dup tree. In the later case, we have to
149 * walk it down left to get the first entry.
150 */
151 if (node->node.bit < 0) {
152 troot = node->node.branches.b[EB_LEFT];
153 while (eb_gettag(troot) != EB_LEAF)
154 troot = (eb_untag(troot, EB_NODE))->b[EB_LEFT];
155 node = container_of(eb_untag(troot, EB_LEAF),
156 struct eb64_node, node.branches);
157 }
158 return node;
159 }
160
161 if ((y >> node->node.bit) >= EB_NODE_BRANCHES)
162 return NULL; /* no more common bits */
163
164 troot = node->node.branches.b[(x >> node->node.bit) & EB_NODE_BRANCH_MASK];
165 }
166}
167
168/*
169 * Find the first occurence of a signed key in the tree <root>. If none can
170 * be found, return NULL.
171 */
172static forceinline struct eb64_node *__eb64i_lookup(struct eb_root *root, s64 x)
173{
174 struct eb64_node *node;
175 eb_troot_t *troot;
176 u64 key = x ^ (1ULL << 63);
177 u64 y;
178
179 troot = root->b[EB_LEFT];
180 if (unlikely(troot == NULL))
181 return NULL;
182
183 while (1) {
184 if ((eb_gettag(troot) == EB_LEAF)) {
185 node = container_of(eb_untag(troot, EB_LEAF),
186 struct eb64_node, node.branches);
Willy Tarreau3a932442010-05-09 19:29:23 +0200187 if (node->key == (u64)x)
Willy Tarreauc2186022009-10-26 19:48:54 +0100188 return node;
189 else
190 return NULL;
191 }
192 node = container_of(eb_untag(troot, EB_NODE),
193 struct eb64_node, node.branches);
194
195 y = node->key ^ x;
196 if (!y) {
197 /* Either we found the node which holds the key, or
198 * we have a dup tree. In the later case, we have to
199 * walk it down left to get the first entry.
200 */
201 if (node->node.bit < 0) {
202 troot = node->node.branches.b[EB_LEFT];
203 while (eb_gettag(troot) != EB_LEAF)
204 troot = (eb_untag(troot, EB_NODE))->b[EB_LEFT];
205 node = container_of(eb_untag(troot, EB_LEAF),
206 struct eb64_node, node.branches);
207 }
208 return node;
209 }
210
211 if ((y >> node->node.bit) >= EB_NODE_BRANCHES)
212 return NULL; /* no more common bits */
213
214 troot = node->node.branches.b[(key >> node->node.bit) & EB_NODE_BRANCH_MASK];
215 }
216}
217
218/* Insert eb64_node <new> into subtree starting at node root <root>.
219 * Only new->key needs be set with the key. The eb64_node is returned.
220 * If root->b[EB_RGHT]==1, the tree may only contain unique keys.
221 */
222static forceinline struct eb64_node *
223__eb64_insert(struct eb_root *root, struct eb64_node *new) {
224 struct eb64_node *old;
225 unsigned int side;
226 eb_troot_t *troot;
227 u64 newkey; /* caching the key saves approximately one cycle */
Willy Tarreau6258f7b2011-09-19 20:48:00 +0200228 eb_troot_t *root_right;
Willy Tarreau3a932442010-05-09 19:29:23 +0200229 int old_node_bit;
Willy Tarreauc2186022009-10-26 19:48:54 +0100230
231 side = EB_LEFT;
232 troot = root->b[EB_LEFT];
233 root_right = root->b[EB_RGHT];
234 if (unlikely(troot == NULL)) {
235 /* Tree is empty, insert the leaf part below the left branch */
236 root->b[EB_LEFT] = eb_dotag(&new->node.branches, EB_LEAF);
237 new->node.leaf_p = eb_dotag(root, EB_LEFT);
238 new->node.node_p = NULL; /* node part unused */
239 return new;
240 }
241
242 /* The tree descent is fairly easy :
243 * - first, check if we have reached a leaf node
244 * - second, check if we have gone too far
245 * - third, reiterate
246 * Everywhere, we use <new> for the node node we are inserting, <root>
247 * for the node we attach it to, and <old> for the node we are
248 * displacing below <new>. <troot> will always point to the future node
249 * (tagged with its type). <side> carries the side the node <new> is
250 * attached to below its parent, which is also where previous node
251 * was attached. <newkey> carries the key being inserted.
252 */
253 newkey = new->key;
254
255 while (1) {
256 if (unlikely(eb_gettag(troot) == EB_LEAF)) {
257 eb_troot_t *new_left, *new_rght;
258 eb_troot_t *new_leaf, *old_leaf;
259
260 old = container_of(eb_untag(troot, EB_LEAF),
261 struct eb64_node, node.branches);
262
263 new_left = eb_dotag(&new->node.branches, EB_LEFT);
264 new_rght = eb_dotag(&new->node.branches, EB_RGHT);
265 new_leaf = eb_dotag(&new->node.branches, EB_LEAF);
266 old_leaf = eb_dotag(&old->node.branches, EB_LEAF);
267
268 new->node.node_p = old->node.leaf_p;
269
270 /* Right here, we have 3 possibilities :
271 - the tree does not contain the key, and we have
272 new->key < old->key. We insert new above old, on
273 the left ;
274
275 - the tree does not contain the key, and we have
276 new->key > old->key. We insert new above old, on
277 the right ;
278
279 - the tree does contain the key, which implies it
280 is alone. We add the new key next to it as a
281 first duplicate.
282
283 The last two cases can easily be partially merged.
284 */
285
286 if (new->key < old->key) {
287 new->node.leaf_p = new_left;
288 old->node.leaf_p = new_rght;
289 new->node.branches.b[EB_LEFT] = new_leaf;
290 new->node.branches.b[EB_RGHT] = old_leaf;
291 } else {
292 /* we may refuse to duplicate this key if the tree is
293 * tagged as containing only unique keys.
294 */
295 if ((new->key == old->key) && eb_gettag(root_right))
296 return old;
297
298 /* new->key >= old->key, new goes the right */
299 old->node.leaf_p = new_left;
300 new->node.leaf_p = new_rght;
301 new->node.branches.b[EB_LEFT] = old_leaf;
302 new->node.branches.b[EB_RGHT] = new_leaf;
303
304 if (new->key == old->key) {
305 new->node.bit = -1;
306 root->b[side] = eb_dotag(&new->node.branches, EB_NODE);
307 return new;
308 }
309 }
310 break;
311 }
312
313 /* OK we're walking down this link */
314 old = container_of(eb_untag(troot, EB_NODE),
315 struct eb64_node, node.branches);
Willy Tarreau3a932442010-05-09 19:29:23 +0200316 old_node_bit = old->node.bit;
Willy Tarreauc2186022009-10-26 19:48:54 +0100317
318 /* Stop going down when we don't have common bits anymore. We
319 * also stop in front of a duplicates tree because it means we
320 * have to insert above.
321 */
322
Willy Tarreau3a932442010-05-09 19:29:23 +0200323 if ((old_node_bit < 0) || /* we're above a duplicate tree, stop here */
324 (((new->key ^ old->key) >> old_node_bit) >= EB_NODE_BRANCHES)) {
Willy Tarreauc2186022009-10-26 19:48:54 +0100325 /* The tree did not contain the key, so we insert <new> before the node
326 * <old>, and set ->bit to designate the lowest bit position in <new>
327 * which applies to ->branches.b[].
328 */
329 eb_troot_t *new_left, *new_rght;
330 eb_troot_t *new_leaf, *old_node;
331
332 new_left = eb_dotag(&new->node.branches, EB_LEFT);
333 new_rght = eb_dotag(&new->node.branches, EB_RGHT);
334 new_leaf = eb_dotag(&new->node.branches, EB_LEAF);
335 old_node = eb_dotag(&old->node.branches, EB_NODE);
336
337 new->node.node_p = old->node.node_p;
338
339 if (new->key < old->key) {
340 new->node.leaf_p = new_left;
341 old->node.node_p = new_rght;
342 new->node.branches.b[EB_LEFT] = new_leaf;
343 new->node.branches.b[EB_RGHT] = old_node;
344 }
345 else if (new->key > old->key) {
346 old->node.node_p = new_left;
347 new->node.leaf_p = new_rght;
348 new->node.branches.b[EB_LEFT] = old_node;
349 new->node.branches.b[EB_RGHT] = new_leaf;
350 }
351 else {
352 struct eb_node *ret;
353 ret = eb_insert_dup(&old->node, &new->node);
354 return container_of(ret, struct eb64_node, node);
355 }
356 break;
357 }
358
359 /* walk down */
360 root = &old->node.branches;
361#if BITS_PER_LONG >= 64
Willy Tarreau3a932442010-05-09 19:29:23 +0200362 side = (newkey >> old_node_bit) & EB_NODE_BRANCH_MASK;
Willy Tarreauc2186022009-10-26 19:48:54 +0100363#else
364 side = newkey;
Willy Tarreau3a932442010-05-09 19:29:23 +0200365 side >>= old_node_bit;
366 if (old_node_bit >= 32) {
Willy Tarreauc2186022009-10-26 19:48:54 +0100367 side = newkey >> 32;
Willy Tarreau3a932442010-05-09 19:29:23 +0200368 side >>= old_node_bit & 0x1F;
Willy Tarreauc2186022009-10-26 19:48:54 +0100369 }
370 side &= EB_NODE_BRANCH_MASK;
371#endif
372 troot = root->b[side];
373 }
374
375 /* Ok, now we are inserting <new> between <root> and <old>. <old>'s
376 * parent is already set to <new>, and the <root>'s branch is still in
377 * <side>. Update the root's leaf till we have it. Note that we can also
378 * find the side by checking the side of new->node.node_p.
379 */
380
381 /* We need the common higher bits between new->key and old->key.
382 * What differences are there between new->key and the node here ?
383 * NOTE that bit(new) is always < bit(root) because highest
384 * bit of new->key and old->key are identical here (otherwise they
385 * would sit on different branches).
386 */
387 // note that if EB_NODE_BITS > 1, we should check that it's still >= 0
388 new->node.bit = fls64(new->key ^ old->key) - EB_NODE_BITS;
389 root->b[side] = eb_dotag(&new->node.branches, EB_NODE);
390
391 return new;
392}
393
394/* Insert eb64_node <new> into subtree starting at node root <root>, using
395 * signed keys. Only new->key needs be set with the key. The eb64_node
396 * is returned. If root->b[EB_RGHT]==1, the tree may only contain unique keys.
397 */
398static forceinline struct eb64_node *
399__eb64i_insert(struct eb_root *root, struct eb64_node *new) {
400 struct eb64_node *old;
401 unsigned int side;
402 eb_troot_t *troot;
403 u64 newkey; /* caching the key saves approximately one cycle */
Willy Tarreau6258f7b2011-09-19 20:48:00 +0200404 eb_troot_t *root_right;
Willy Tarreau3a932442010-05-09 19:29:23 +0200405 int old_node_bit;
Willy Tarreauc2186022009-10-26 19:48:54 +0100406
407 side = EB_LEFT;
408 troot = root->b[EB_LEFT];
409 root_right = root->b[EB_RGHT];
410 if (unlikely(troot == NULL)) {
411 /* Tree is empty, insert the leaf part below the left branch */
412 root->b[EB_LEFT] = eb_dotag(&new->node.branches, EB_LEAF);
413 new->node.leaf_p = eb_dotag(root, EB_LEFT);
414 new->node.node_p = NULL; /* node part unused */
415 return new;
416 }
417
418 /* The tree descent is fairly easy :
419 * - first, check if we have reached a leaf node
420 * - second, check if we have gone too far
421 * - third, reiterate
422 * Everywhere, we use <new> for the node node we are inserting, <root>
423 * for the node we attach it to, and <old> for the node we are
424 * displacing below <new>. <troot> will always point to the future node
425 * (tagged with its type). <side> carries the side the node <new> is
426 * attached to below its parent, which is also where previous node
427 * was attached. <newkey> carries a high bit shift of the key being
428 * inserted in order to have negative keys stored before positive
429 * ones.
430 */
431 newkey = new->key ^ (1ULL << 63);
432
433 while (1) {
434 if (unlikely(eb_gettag(troot) == EB_LEAF)) {
435 eb_troot_t *new_left, *new_rght;
436 eb_troot_t *new_leaf, *old_leaf;
437
438 old = container_of(eb_untag(troot, EB_LEAF),
439 struct eb64_node, node.branches);
440
441 new_left = eb_dotag(&new->node.branches, EB_LEFT);
442 new_rght = eb_dotag(&new->node.branches, EB_RGHT);
443 new_leaf = eb_dotag(&new->node.branches, EB_LEAF);
444 old_leaf = eb_dotag(&old->node.branches, EB_LEAF);
445
446 new->node.node_p = old->node.leaf_p;
447
448 /* Right here, we have 3 possibilities :
449 - the tree does not contain the key, and we have
450 new->key < old->key. We insert new above old, on
451 the left ;
452
453 - the tree does not contain the key, and we have
454 new->key > old->key. We insert new above old, on
455 the right ;
456
457 - the tree does contain the key, which implies it
458 is alone. We add the new key next to it as a
459 first duplicate.
460
461 The last two cases can easily be partially merged.
462 */
463
464 if ((s64)new->key < (s64)old->key) {
465 new->node.leaf_p = new_left;
466 old->node.leaf_p = new_rght;
467 new->node.branches.b[EB_LEFT] = new_leaf;
468 new->node.branches.b[EB_RGHT] = old_leaf;
469 } else {
470 /* we may refuse to duplicate this key if the tree is
471 * tagged as containing only unique keys.
472 */
473 if ((new->key == old->key) && eb_gettag(root_right))
474 return old;
475
476 /* new->key >= old->key, new goes the right */
477 old->node.leaf_p = new_left;
478 new->node.leaf_p = new_rght;
479 new->node.branches.b[EB_LEFT] = old_leaf;
480 new->node.branches.b[EB_RGHT] = new_leaf;
481
482 if (new->key == old->key) {
483 new->node.bit = -1;
484 root->b[side] = eb_dotag(&new->node.branches, EB_NODE);
485 return new;
486 }
487 }
488 break;
489 }
490
491 /* OK we're walking down this link */
492 old = container_of(eb_untag(troot, EB_NODE),
493 struct eb64_node, node.branches);
Willy Tarreau3a932442010-05-09 19:29:23 +0200494 old_node_bit = old->node.bit;
Willy Tarreauc2186022009-10-26 19:48:54 +0100495
496 /* Stop going down when we don't have common bits anymore. We
497 * also stop in front of a duplicates tree because it means we
498 * have to insert above.
499 */
500
Willy Tarreau3a932442010-05-09 19:29:23 +0200501 if ((old_node_bit < 0) || /* we're above a duplicate tree, stop here */
502 (((new->key ^ old->key) >> old_node_bit) >= EB_NODE_BRANCHES)) {
Willy Tarreauc2186022009-10-26 19:48:54 +0100503 /* The tree did not contain the key, so we insert <new> before the node
504 * <old>, and set ->bit to designate the lowest bit position in <new>
505 * which applies to ->branches.b[].
506 */
507 eb_troot_t *new_left, *new_rght;
508 eb_troot_t *new_leaf, *old_node;
509
510 new_left = eb_dotag(&new->node.branches, EB_LEFT);
511 new_rght = eb_dotag(&new->node.branches, EB_RGHT);
512 new_leaf = eb_dotag(&new->node.branches, EB_LEAF);
513 old_node = eb_dotag(&old->node.branches, EB_NODE);
514
515 new->node.node_p = old->node.node_p;
516
517 if ((s64)new->key < (s64)old->key) {
518 new->node.leaf_p = new_left;
519 old->node.node_p = new_rght;
520 new->node.branches.b[EB_LEFT] = new_leaf;
521 new->node.branches.b[EB_RGHT] = old_node;
522 }
523 else if ((s64)new->key > (s64)old->key) {
524 old->node.node_p = new_left;
525 new->node.leaf_p = new_rght;
526 new->node.branches.b[EB_LEFT] = old_node;
527 new->node.branches.b[EB_RGHT] = new_leaf;
528 }
529 else {
530 struct eb_node *ret;
531 ret = eb_insert_dup(&old->node, &new->node);
532 return container_of(ret, struct eb64_node, node);
533 }
534 break;
535 }
536
537 /* walk down */
538 root = &old->node.branches;
539#if BITS_PER_LONG >= 64
Willy Tarreau3a932442010-05-09 19:29:23 +0200540 side = (newkey >> old_node_bit) & EB_NODE_BRANCH_MASK;
Willy Tarreauc2186022009-10-26 19:48:54 +0100541#else
542 side = newkey;
Willy Tarreau3a932442010-05-09 19:29:23 +0200543 side >>= old_node_bit;
544 if (old_node_bit >= 32) {
Willy Tarreauc2186022009-10-26 19:48:54 +0100545 side = newkey >> 32;
Willy Tarreau3a932442010-05-09 19:29:23 +0200546 side >>= old_node_bit & 0x1F;
Willy Tarreauc2186022009-10-26 19:48:54 +0100547 }
548 side &= EB_NODE_BRANCH_MASK;
549#endif
550 troot = root->b[side];
551 }
552
553 /* Ok, now we are inserting <new> between <root> and <old>. <old>'s
554 * parent is already set to <new>, and the <root>'s branch is still in
555 * <side>. Update the root's leaf till we have it. Note that we can also
556 * find the side by checking the side of new->node.node_p.
557 */
558
559 /* We need the common higher bits between new->key and old->key.
560 * What differences are there between new->key and the node here ?
561 * NOTE that bit(new) is always < bit(root) because highest
562 * bit of new->key and old->key are identical here (otherwise they
563 * would sit on different branches).
564 */
565 // note that if EB_NODE_BITS > 1, we should check that it's still >= 0
566 new->node.bit = fls64(new->key ^ old->key) - EB_NODE_BITS;
567 root->b[side] = eb_dotag(&new->node.branches, EB_NODE);
568
569 return new;
570}
571
572#endif /* _EB64_TREE_H */