blob: d6e5db49c578a8ec6b4134ad91be9c547d6cc6aa [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
Willy Tarreauc2186022009-10-26 19:48:54 +010030/*
31 * Exported functions and macros.
32 * Many of them are always inlined because they are extremely small, and
33 * are generally called at most once or twice in a program.
34 */
35
36/* Return leftmost node in the tree, or NULL if none */
37static inline struct eb64_node *eb64_first(struct eb_root *root)
38{
39 return eb64_entry(eb_first(root), struct eb64_node, node);
40}
41
42/* Return rightmost node in the tree, or NULL if none */
43static inline struct eb64_node *eb64_last(struct eb_root *root)
44{
45 return eb64_entry(eb_last(root), struct eb64_node, node);
46}
47
48/* Return next node in the tree, or NULL if none */
49static inline struct eb64_node *eb64_next(struct eb64_node *eb64)
50{
51 return eb64_entry(eb_next(&eb64->node), struct eb64_node, node);
52}
53
54/* Return previous node in the tree, or NULL if none */
55static inline struct eb64_node *eb64_prev(struct eb64_node *eb64)
56{
57 return eb64_entry(eb_prev(&eb64->node), struct eb64_node, node);
58}
59
Willy Tarreau2b570202013-05-07 15:58:28 +020060/* Return next leaf node within a duplicate sub-tree, or NULL if none. */
61static inline struct eb64_node *eb64_next_dup(struct eb64_node *eb64)
62{
63 return eb64_entry(eb_next_dup(&eb64->node), struct eb64_node, node);
64}
65
66/* Return previous leaf node within a duplicate sub-tree, or NULL if none. */
67static inline struct eb64_node *eb64_prev_dup(struct eb64_node *eb64)
68{
69 return eb64_entry(eb_prev_dup(&eb64->node), struct eb64_node, node);
70}
71
Willy Tarreauc2186022009-10-26 19:48:54 +010072/* Return next node in the tree, skipping duplicates, or NULL if none */
73static inline struct eb64_node *eb64_next_unique(struct eb64_node *eb64)
74{
75 return eb64_entry(eb_next_unique(&eb64->node), struct eb64_node, node);
76}
77
78/* Return previous node in the tree, skipping duplicates, or NULL if none */
79static inline struct eb64_node *eb64_prev_unique(struct eb64_node *eb64)
80{
81 return eb64_entry(eb_prev_unique(&eb64->node), struct eb64_node, node);
82}
83
84/* Delete node from the tree if it was linked in. Mark the node unused. Note
85 * that this function relies on a non-inlined generic function: eb_delete.
86 */
87static inline void eb64_delete(struct eb64_node *eb64)
88{
89 eb_delete(&eb64->node);
90}
91
92/*
93 * The following functions are not inlined by default. They are declared
94 * in eb64tree.c, which simply relies on their inline version.
95 */
Willy Tarreau03e78532020-02-25 07:38:05 +010096struct eb64_node *eb64_lookup(struct eb_root *root, u64 x);
97struct eb64_node *eb64i_lookup(struct eb_root *root, s64 x);
98struct eb64_node *eb64_lookup_le(struct eb_root *root, u64 x);
99struct eb64_node *eb64_lookup_ge(struct eb_root *root, u64 x);
100struct eb64_node *eb64_insert(struct eb_root *root, struct eb64_node *new);
101struct eb64_node *eb64i_insert(struct eb_root *root, struct eb64_node *new);
Willy Tarreauc2186022009-10-26 19:48:54 +0100102
103/*
104 * The following functions are less likely to be used directly, because their
105 * code is larger. The non-inlined version is preferred.
106 */
107
108/* Delete node from the tree if it was linked in. Mark the node unused. */
109static forceinline void __eb64_delete(struct eb64_node *eb64)
110{
111 __eb_delete(&eb64->node);
112}
113
114/*
Joseph Herlant7c16c0e2018-11-13 19:55:57 -0800115 * Find the first occurrence of a key in the tree <root>. If none can be
Willy Tarreauc2186022009-10-26 19:48:54 +0100116 * found, return NULL.
117 */
118static forceinline struct eb64_node *__eb64_lookup(struct eb_root *root, u64 x)
119{
120 struct eb64_node *node;
121 eb_troot_t *troot;
122 u64 y;
123
124 troot = root->b[EB_LEFT];
125 if (unlikely(troot == NULL))
126 return NULL;
127
128 while (1) {
129 if ((eb_gettag(troot) == EB_LEAF)) {
130 node = container_of(eb_untag(troot, EB_LEAF),
131 struct eb64_node, node.branches);
132 if (node->key == x)
133 return node;
134 else
135 return NULL;
136 }
137 node = container_of(eb_untag(troot, EB_NODE),
138 struct eb64_node, node.branches);
139
140 y = node->key ^ x;
141 if (!y) {
142 /* Either we found the node which holds the key, or
143 * we have a dup tree. In the later case, we have to
144 * walk it down left to get the first entry.
145 */
146 if (node->node.bit < 0) {
147 troot = node->node.branches.b[EB_LEFT];
148 while (eb_gettag(troot) != EB_LEAF)
149 troot = (eb_untag(troot, EB_NODE))->b[EB_LEFT];
150 node = container_of(eb_untag(troot, EB_LEAF),
151 struct eb64_node, node.branches);
152 }
153 return node;
154 }
155
156 if ((y >> node->node.bit) >= EB_NODE_BRANCHES)
157 return NULL; /* no more common bits */
158
159 troot = node->node.branches.b[(x >> node->node.bit) & EB_NODE_BRANCH_MASK];
160 }
161}
162
163/*
Joseph Herlant7c16c0e2018-11-13 19:55:57 -0800164 * Find the first occurrence of a signed key in the tree <root>. If none can
Willy Tarreauc2186022009-10-26 19:48:54 +0100165 * be found, return NULL.
166 */
167static forceinline struct eb64_node *__eb64i_lookup(struct eb_root *root, s64 x)
168{
169 struct eb64_node *node;
170 eb_troot_t *troot;
171 u64 key = x ^ (1ULL << 63);
172 u64 y;
173
174 troot = root->b[EB_LEFT];
175 if (unlikely(troot == NULL))
176 return NULL;
177
178 while (1) {
179 if ((eb_gettag(troot) == EB_LEAF)) {
180 node = container_of(eb_untag(troot, EB_LEAF),
181 struct eb64_node, node.branches);
Willy Tarreau3a932442010-05-09 19:29:23 +0200182 if (node->key == (u64)x)
Willy Tarreauc2186022009-10-26 19:48:54 +0100183 return node;
184 else
185 return NULL;
186 }
187 node = container_of(eb_untag(troot, EB_NODE),
188 struct eb64_node, node.branches);
189
190 y = node->key ^ x;
191 if (!y) {
192 /* Either we found the node which holds the key, or
193 * we have a dup tree. In the later case, we have to
194 * walk it down left to get the first entry.
195 */
196 if (node->node.bit < 0) {
197 troot = node->node.branches.b[EB_LEFT];
198 while (eb_gettag(troot) != EB_LEAF)
199 troot = (eb_untag(troot, EB_NODE))->b[EB_LEFT];
200 node = container_of(eb_untag(troot, EB_LEAF),
201 struct eb64_node, node.branches);
202 }
203 return node;
204 }
205
206 if ((y >> node->node.bit) >= EB_NODE_BRANCHES)
207 return NULL; /* no more common bits */
208
209 troot = node->node.branches.b[(key >> node->node.bit) & EB_NODE_BRANCH_MASK];
210 }
211}
212
213/* Insert eb64_node <new> into subtree starting at node root <root>.
214 * Only new->key needs be set with the key. The eb64_node is returned.
215 * If root->b[EB_RGHT]==1, the tree may only contain unique keys.
216 */
217static forceinline struct eb64_node *
218__eb64_insert(struct eb_root *root, struct eb64_node *new) {
219 struct eb64_node *old;
220 unsigned int side;
221 eb_troot_t *troot;
222 u64 newkey; /* caching the key saves approximately one cycle */
Willy Tarreau6258f7b2011-09-19 20:48:00 +0200223 eb_troot_t *root_right;
Willy Tarreau3a932442010-05-09 19:29:23 +0200224 int old_node_bit;
Willy Tarreauc2186022009-10-26 19:48:54 +0100225
226 side = EB_LEFT;
227 troot = root->b[EB_LEFT];
228 root_right = root->b[EB_RGHT];
229 if (unlikely(troot == NULL)) {
230 /* Tree is empty, insert the leaf part below the left branch */
231 root->b[EB_LEFT] = eb_dotag(&new->node.branches, EB_LEAF);
232 new->node.leaf_p = eb_dotag(root, EB_LEFT);
233 new->node.node_p = NULL; /* node part unused */
234 return new;
235 }
236
237 /* The tree descent is fairly easy :
238 * - first, check if we have reached a leaf node
239 * - second, check if we have gone too far
240 * - third, reiterate
241 * Everywhere, we use <new> for the node node we are inserting, <root>
242 * for the node we attach it to, and <old> for the node we are
243 * displacing below <new>. <troot> will always point to the future node
244 * (tagged with its type). <side> carries the side the node <new> is
245 * attached to below its parent, which is also where previous node
246 * was attached. <newkey> carries the key being inserted.
247 */
248 newkey = new->key;
249
250 while (1) {
251 if (unlikely(eb_gettag(troot) == EB_LEAF)) {
252 eb_troot_t *new_left, *new_rght;
253 eb_troot_t *new_leaf, *old_leaf;
254
255 old = container_of(eb_untag(troot, EB_LEAF),
256 struct eb64_node, node.branches);
257
258 new_left = eb_dotag(&new->node.branches, EB_LEFT);
259 new_rght = eb_dotag(&new->node.branches, EB_RGHT);
260 new_leaf = eb_dotag(&new->node.branches, EB_LEAF);
261 old_leaf = eb_dotag(&old->node.branches, EB_LEAF);
262
263 new->node.node_p = old->node.leaf_p;
264
265 /* Right here, we have 3 possibilities :
266 - the tree does not contain the key, and we have
267 new->key < old->key. We insert new above old, on
268 the left ;
269
270 - the tree does not contain the key, and we have
271 new->key > old->key. We insert new above old, on
272 the right ;
273
274 - the tree does contain the key, which implies it
275 is alone. We add the new key next to it as a
276 first duplicate.
277
278 The last two cases can easily be partially merged.
279 */
280
281 if (new->key < old->key) {
282 new->node.leaf_p = new_left;
283 old->node.leaf_p = new_rght;
284 new->node.branches.b[EB_LEFT] = new_leaf;
285 new->node.branches.b[EB_RGHT] = old_leaf;
286 } else {
287 /* we may refuse to duplicate this key if the tree is
288 * tagged as containing only unique keys.
289 */
290 if ((new->key == old->key) && eb_gettag(root_right))
291 return old;
292
293 /* new->key >= old->key, new goes the right */
294 old->node.leaf_p = new_left;
295 new->node.leaf_p = new_rght;
296 new->node.branches.b[EB_LEFT] = old_leaf;
297 new->node.branches.b[EB_RGHT] = new_leaf;
298
299 if (new->key == old->key) {
300 new->node.bit = -1;
301 root->b[side] = eb_dotag(&new->node.branches, EB_NODE);
302 return new;
303 }
304 }
305 break;
306 }
307
308 /* OK we're walking down this link */
309 old = container_of(eb_untag(troot, EB_NODE),
310 struct eb64_node, node.branches);
Willy Tarreau3a932442010-05-09 19:29:23 +0200311 old_node_bit = old->node.bit;
Willy Tarreauc2186022009-10-26 19:48:54 +0100312
313 /* Stop going down when we don't have common bits anymore. We
314 * also stop in front of a duplicates tree because it means we
315 * have to insert above.
316 */
317
Willy Tarreau3a932442010-05-09 19:29:23 +0200318 if ((old_node_bit < 0) || /* we're above a duplicate tree, stop here */
319 (((new->key ^ old->key) >> old_node_bit) >= EB_NODE_BRANCHES)) {
Willy Tarreauc2186022009-10-26 19:48:54 +0100320 /* The tree did not contain the key, so we insert <new> before the node
321 * <old>, and set ->bit to designate the lowest bit position in <new>
322 * which applies to ->branches.b[].
323 */
324 eb_troot_t *new_left, *new_rght;
325 eb_troot_t *new_leaf, *old_node;
326
327 new_left = eb_dotag(&new->node.branches, EB_LEFT);
328 new_rght = eb_dotag(&new->node.branches, EB_RGHT);
329 new_leaf = eb_dotag(&new->node.branches, EB_LEAF);
330 old_node = eb_dotag(&old->node.branches, EB_NODE);
331
332 new->node.node_p = old->node.node_p;
333
334 if (new->key < old->key) {
335 new->node.leaf_p = new_left;
336 old->node.node_p = new_rght;
337 new->node.branches.b[EB_LEFT] = new_leaf;
338 new->node.branches.b[EB_RGHT] = old_node;
339 }
340 else if (new->key > old->key) {
341 old->node.node_p = new_left;
342 new->node.leaf_p = new_rght;
343 new->node.branches.b[EB_LEFT] = old_node;
344 new->node.branches.b[EB_RGHT] = new_leaf;
345 }
346 else {
347 struct eb_node *ret;
348 ret = eb_insert_dup(&old->node, &new->node);
349 return container_of(ret, struct eb64_node, node);
350 }
351 break;
352 }
353
354 /* walk down */
355 root = &old->node.branches;
Willy Tarreaucbdc74b2021-08-28 11:55:53 +0200356
357 if (sizeof(long) >= 8) {
358 side = newkey >> old_node_bit;
359 } else {
360 /* note: provides the best code on low-register count archs
361 * such as i386.
362 */
363 side = newkey;
364 side >>= old_node_bit;
365 if (old_node_bit >= 32) {
366 side = newkey >> 32;
367 side >>= old_node_bit & 0x1F;
368 }
Willy Tarreauc2186022009-10-26 19:48:54 +0100369 }
370 side &= EB_NODE_BRANCH_MASK;
Willy Tarreauc2186022009-10-26 19:48:54 +0100371 troot = root->b[side];
372 }
373
374 /* Ok, now we are inserting <new> between <root> and <old>. <old>'s
375 * parent is already set to <new>, and the <root>'s branch is still in
376 * <side>. Update the root's leaf till we have it. Note that we can also
377 * find the side by checking the side of new->node.node_p.
378 */
379
380 /* We need the common higher bits between new->key and old->key.
381 * What differences are there between new->key and the node here ?
382 * NOTE that bit(new) is always < bit(root) because highest
383 * bit of new->key and old->key are identical here (otherwise they
384 * would sit on different branches).
385 */
386 // note that if EB_NODE_BITS > 1, we should check that it's still >= 0
387 new->node.bit = fls64(new->key ^ old->key) - EB_NODE_BITS;
388 root->b[side] = eb_dotag(&new->node.branches, EB_NODE);
389
390 return new;
391}
392
393/* Insert eb64_node <new> into subtree starting at node root <root>, using
394 * signed keys. Only new->key needs be set with the key. The eb64_node
395 * is returned. If root->b[EB_RGHT]==1, the tree may only contain unique keys.
396 */
397static forceinline struct eb64_node *
398__eb64i_insert(struct eb_root *root, struct eb64_node *new) {
399 struct eb64_node *old;
400 unsigned int side;
401 eb_troot_t *troot;
402 u64 newkey; /* caching the key saves approximately one cycle */
Willy Tarreau6258f7b2011-09-19 20:48:00 +0200403 eb_troot_t *root_right;
Willy Tarreau3a932442010-05-09 19:29:23 +0200404 int old_node_bit;
Willy Tarreauc2186022009-10-26 19:48:54 +0100405
406 side = EB_LEFT;
407 troot = root->b[EB_LEFT];
408 root_right = root->b[EB_RGHT];
409 if (unlikely(troot == NULL)) {
410 /* Tree is empty, insert the leaf part below the left branch */
411 root->b[EB_LEFT] = eb_dotag(&new->node.branches, EB_LEAF);
412 new->node.leaf_p = eb_dotag(root, EB_LEFT);
413 new->node.node_p = NULL; /* node part unused */
414 return new;
415 }
416
417 /* The tree descent is fairly easy :
418 * - first, check if we have reached a leaf node
419 * - second, check if we have gone too far
420 * - third, reiterate
421 * Everywhere, we use <new> for the node node we are inserting, <root>
422 * for the node we attach it to, and <old> for the node we are
423 * displacing below <new>. <troot> will always point to the future node
424 * (tagged with its type). <side> carries the side the node <new> is
425 * attached to below its parent, which is also where previous node
426 * was attached. <newkey> carries a high bit shift of the key being
427 * inserted in order to have negative keys stored before positive
428 * ones.
429 */
430 newkey = new->key ^ (1ULL << 63);
431
432 while (1) {
433 if (unlikely(eb_gettag(troot) == EB_LEAF)) {
434 eb_troot_t *new_left, *new_rght;
435 eb_troot_t *new_leaf, *old_leaf;
436
437 old = container_of(eb_untag(troot, EB_LEAF),
438 struct eb64_node, node.branches);
439
440 new_left = eb_dotag(&new->node.branches, EB_LEFT);
441 new_rght = eb_dotag(&new->node.branches, EB_RGHT);
442 new_leaf = eb_dotag(&new->node.branches, EB_LEAF);
443 old_leaf = eb_dotag(&old->node.branches, EB_LEAF);
444
445 new->node.node_p = old->node.leaf_p;
446
447 /* Right here, we have 3 possibilities :
448 - the tree does not contain the key, and we have
449 new->key < old->key. We insert new above old, on
450 the left ;
451
452 - the tree does not contain the key, and we have
453 new->key > old->key. We insert new above old, on
454 the right ;
455
456 - the tree does contain the key, which implies it
457 is alone. We add the new key next to it as a
458 first duplicate.
459
460 The last two cases can easily be partially merged.
461 */
462
463 if ((s64)new->key < (s64)old->key) {
464 new->node.leaf_p = new_left;
465 old->node.leaf_p = new_rght;
466 new->node.branches.b[EB_LEFT] = new_leaf;
467 new->node.branches.b[EB_RGHT] = old_leaf;
468 } else {
469 /* we may refuse to duplicate this key if the tree is
470 * tagged as containing only unique keys.
471 */
472 if ((new->key == old->key) && eb_gettag(root_right))
473 return old;
474
475 /* new->key >= old->key, new goes the right */
476 old->node.leaf_p = new_left;
477 new->node.leaf_p = new_rght;
478 new->node.branches.b[EB_LEFT] = old_leaf;
479 new->node.branches.b[EB_RGHT] = new_leaf;
480
481 if (new->key == old->key) {
482 new->node.bit = -1;
483 root->b[side] = eb_dotag(&new->node.branches, EB_NODE);
484 return new;
485 }
486 }
487 break;
488 }
489
490 /* OK we're walking down this link */
491 old = container_of(eb_untag(troot, EB_NODE),
492 struct eb64_node, node.branches);
Willy Tarreau3a932442010-05-09 19:29:23 +0200493 old_node_bit = old->node.bit;
Willy Tarreauc2186022009-10-26 19:48:54 +0100494
495 /* Stop going down when we don't have common bits anymore. We
496 * also stop in front of a duplicates tree because it means we
497 * have to insert above.
498 */
499
Willy Tarreau3a932442010-05-09 19:29:23 +0200500 if ((old_node_bit < 0) || /* we're above a duplicate tree, stop here */
501 (((new->key ^ old->key) >> old_node_bit) >= EB_NODE_BRANCHES)) {
Willy Tarreauc2186022009-10-26 19:48:54 +0100502 /* The tree did not contain the key, so we insert <new> before the node
503 * <old>, and set ->bit to designate the lowest bit position in <new>
504 * which applies to ->branches.b[].
505 */
506 eb_troot_t *new_left, *new_rght;
507 eb_troot_t *new_leaf, *old_node;
508
509 new_left = eb_dotag(&new->node.branches, EB_LEFT);
510 new_rght = eb_dotag(&new->node.branches, EB_RGHT);
511 new_leaf = eb_dotag(&new->node.branches, EB_LEAF);
512 old_node = eb_dotag(&old->node.branches, EB_NODE);
513
514 new->node.node_p = old->node.node_p;
515
516 if ((s64)new->key < (s64)old->key) {
517 new->node.leaf_p = new_left;
518 old->node.node_p = new_rght;
519 new->node.branches.b[EB_LEFT] = new_leaf;
520 new->node.branches.b[EB_RGHT] = old_node;
521 }
522 else if ((s64)new->key > (s64)old->key) {
523 old->node.node_p = new_left;
524 new->node.leaf_p = new_rght;
525 new->node.branches.b[EB_LEFT] = old_node;
526 new->node.branches.b[EB_RGHT] = new_leaf;
527 }
528 else {
529 struct eb_node *ret;
530 ret = eb_insert_dup(&old->node, &new->node);
531 return container_of(ret, struct eb64_node, node);
532 }
533 break;
534 }
535
536 /* walk down */
537 root = &old->node.branches;
Willy Tarreaucbdc74b2021-08-28 11:55:53 +0200538
539 if (sizeof(long) >= 8) {
540 side = newkey >> old_node_bit;
541 } else {
542 /* note: provides the best code on low-register count archs
543 * such as i386.
544 */
545 side = newkey;
546 side >>= old_node_bit;
547 if (old_node_bit >= 32) {
548 side = newkey >> 32;
549 side >>= old_node_bit & 0x1F;
550 }
Willy Tarreauc2186022009-10-26 19:48:54 +0100551 }
552 side &= EB_NODE_BRANCH_MASK;
Willy Tarreauc2186022009-10-26 19:48:54 +0100553 troot = root->b[side];
554 }
555
556 /* Ok, now we are inserting <new> between <root> and <old>. <old>'s
557 * parent is already set to <new>, and the <root>'s branch is still in
558 * <side>. Update the root's leaf till we have it. Note that we can also
559 * find the side by checking the side of new->node.node_p.
560 */
561
562 /* We need the common higher bits between new->key and old->key.
563 * What differences are there between new->key and the node here ?
564 * NOTE that bit(new) is always < bit(root) because highest
565 * bit of new->key and old->key are identical here (otherwise they
566 * would sit on different branches).
567 */
568 // note that if EB_NODE_BITS > 1, we should check that it's still >= 0
569 new->node.bit = fls64(new->key ^ old->key) - EB_NODE_BITS;
570 root->b[side] = eb_dotag(&new->node.branches, EB_NODE);
571
572 return new;
573}
574
575#endif /* _EB64_TREE_H */