blob: 6ffa58650a26cc45ee2795728c8843fa084f8910 [file] [log] [blame]
Willy Tarreauc2186022009-10-26 19:48:54 +01001/*
2 * Elastic Binary Trees - macros and structures for operations on 64bit nodes.
3 * Version 5.0
4 * (C) 2002-2009 - Willy Tarreau <w@1wt.eu>
5 *
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;
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);
187 if (node->key == x)
188 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 */
228 eb_troot_t *root_right = root;
229
230 side = EB_LEFT;
231 troot = root->b[EB_LEFT];
232 root_right = root->b[EB_RGHT];
233 if (unlikely(troot == NULL)) {
234 /* Tree is empty, insert the leaf part below the left branch */
235 root->b[EB_LEFT] = eb_dotag(&new->node.branches, EB_LEAF);
236 new->node.leaf_p = eb_dotag(root, EB_LEFT);
237 new->node.node_p = NULL; /* node part unused */
238 return new;
239 }
240
241 /* The tree descent is fairly easy :
242 * - first, check if we have reached a leaf node
243 * - second, check if we have gone too far
244 * - third, reiterate
245 * Everywhere, we use <new> for the node node we are inserting, <root>
246 * for the node we attach it to, and <old> for the node we are
247 * displacing below <new>. <troot> will always point to the future node
248 * (tagged with its type). <side> carries the side the node <new> is
249 * attached to below its parent, which is also where previous node
250 * was attached. <newkey> carries the key being inserted.
251 */
252 newkey = new->key;
253
254 while (1) {
255 if (unlikely(eb_gettag(troot) == EB_LEAF)) {
256 eb_troot_t *new_left, *new_rght;
257 eb_troot_t *new_leaf, *old_leaf;
258
259 old = container_of(eb_untag(troot, EB_LEAF),
260 struct eb64_node, node.branches);
261
262 new_left = eb_dotag(&new->node.branches, EB_LEFT);
263 new_rght = eb_dotag(&new->node.branches, EB_RGHT);
264 new_leaf = eb_dotag(&new->node.branches, EB_LEAF);
265 old_leaf = eb_dotag(&old->node.branches, EB_LEAF);
266
267 new->node.node_p = old->node.leaf_p;
268
269 /* Right here, we have 3 possibilities :
270 - the tree does not contain the key, and we have
271 new->key < old->key. We insert new above old, on
272 the left ;
273
274 - the tree does not contain the key, and we have
275 new->key > old->key. We insert new above old, on
276 the right ;
277
278 - the tree does contain the key, which implies it
279 is alone. We add the new key next to it as a
280 first duplicate.
281
282 The last two cases can easily be partially merged.
283 */
284
285 if (new->key < old->key) {
286 new->node.leaf_p = new_left;
287 old->node.leaf_p = new_rght;
288 new->node.branches.b[EB_LEFT] = new_leaf;
289 new->node.branches.b[EB_RGHT] = old_leaf;
290 } else {
291 /* we may refuse to duplicate this key if the tree is
292 * tagged as containing only unique keys.
293 */
294 if ((new->key == old->key) && eb_gettag(root_right))
295 return old;
296
297 /* new->key >= old->key, new goes the right */
298 old->node.leaf_p = new_left;
299 new->node.leaf_p = new_rght;
300 new->node.branches.b[EB_LEFT] = old_leaf;
301 new->node.branches.b[EB_RGHT] = new_leaf;
302
303 if (new->key == old->key) {
304 new->node.bit = -1;
305 root->b[side] = eb_dotag(&new->node.branches, EB_NODE);
306 return new;
307 }
308 }
309 break;
310 }
311
312 /* OK we're walking down this link */
313 old = container_of(eb_untag(troot, EB_NODE),
314 struct eb64_node, node.branches);
315
316 /* Stop going down when we don't have common bits anymore. We
317 * also stop in front of a duplicates tree because it means we
318 * have to insert above.
319 */
320
321 if ((old->node.bit < 0) || /* we're above a duplicate tree, stop here */
322 (((new->key ^ old->key) >> old->node.bit) >= EB_NODE_BRANCHES)) {
323 /* The tree did not contain the key, so we insert <new> before the node
324 * <old>, and set ->bit to designate the lowest bit position in <new>
325 * which applies to ->branches.b[].
326 */
327 eb_troot_t *new_left, *new_rght;
328 eb_troot_t *new_leaf, *old_node;
329
330 new_left = eb_dotag(&new->node.branches, EB_LEFT);
331 new_rght = eb_dotag(&new->node.branches, EB_RGHT);
332 new_leaf = eb_dotag(&new->node.branches, EB_LEAF);
333 old_node = eb_dotag(&old->node.branches, EB_NODE);
334
335 new->node.node_p = old->node.node_p;
336
337 if (new->key < old->key) {
338 new->node.leaf_p = new_left;
339 old->node.node_p = new_rght;
340 new->node.branches.b[EB_LEFT] = new_leaf;
341 new->node.branches.b[EB_RGHT] = old_node;
342 }
343 else if (new->key > old->key) {
344 old->node.node_p = new_left;
345 new->node.leaf_p = new_rght;
346 new->node.branches.b[EB_LEFT] = old_node;
347 new->node.branches.b[EB_RGHT] = new_leaf;
348 }
349 else {
350 struct eb_node *ret;
351 ret = eb_insert_dup(&old->node, &new->node);
352 return container_of(ret, struct eb64_node, node);
353 }
354 break;
355 }
356
357 /* walk down */
358 root = &old->node.branches;
359#if BITS_PER_LONG >= 64
360 side = (newkey >> old->node.bit) & EB_NODE_BRANCH_MASK;
361#else
362 side = newkey;
363 side >>= old->node.bit;
364 if (old->node.bit >= 32) {
365 side = newkey >> 32;
366 side >>= old->node.bit & 0x1F;
367 }
368 side &= EB_NODE_BRANCH_MASK;
369#endif
370 troot = root->b[side];
371 }
372
373 /* Ok, now we are inserting <new> between <root> and <old>. <old>'s
374 * parent is already set to <new>, and the <root>'s branch is still in
375 * <side>. Update the root's leaf till we have it. Note that we can also
376 * find the side by checking the side of new->node.node_p.
377 */
378
379 /* We need the common higher bits between new->key and old->key.
380 * What differences are there between new->key and the node here ?
381 * NOTE that bit(new) is always < bit(root) because highest
382 * bit of new->key and old->key are identical here (otherwise they
383 * would sit on different branches).
384 */
385 // note that if EB_NODE_BITS > 1, we should check that it's still >= 0
386 new->node.bit = fls64(new->key ^ old->key) - EB_NODE_BITS;
387 root->b[side] = eb_dotag(&new->node.branches, EB_NODE);
388
389 return new;
390}
391
392/* Insert eb64_node <new> into subtree starting at node root <root>, using
393 * signed keys. Only new->key needs be set with the key. The eb64_node
394 * is returned. If root->b[EB_RGHT]==1, the tree may only contain unique keys.
395 */
396static forceinline struct eb64_node *
397__eb64i_insert(struct eb_root *root, struct eb64_node *new) {
398 struct eb64_node *old;
399 unsigned int side;
400 eb_troot_t *troot;
401 u64 newkey; /* caching the key saves approximately one cycle */
402 eb_troot_t *root_right = root;
403
404 side = EB_LEFT;
405 troot = root->b[EB_LEFT];
406 root_right = root->b[EB_RGHT];
407 if (unlikely(troot == NULL)) {
408 /* Tree is empty, insert the leaf part below the left branch */
409 root->b[EB_LEFT] = eb_dotag(&new->node.branches, EB_LEAF);
410 new->node.leaf_p = eb_dotag(root, EB_LEFT);
411 new->node.node_p = NULL; /* node part unused */
412 return new;
413 }
414
415 /* The tree descent is fairly easy :
416 * - first, check if we have reached a leaf node
417 * - second, check if we have gone too far
418 * - third, reiterate
419 * Everywhere, we use <new> for the node node we are inserting, <root>
420 * for the node we attach it to, and <old> for the node we are
421 * displacing below <new>. <troot> will always point to the future node
422 * (tagged with its type). <side> carries the side the node <new> is
423 * attached to below its parent, which is also where previous node
424 * was attached. <newkey> carries a high bit shift of the key being
425 * inserted in order to have negative keys stored before positive
426 * ones.
427 */
428 newkey = new->key ^ (1ULL << 63);
429
430 while (1) {
431 if (unlikely(eb_gettag(troot) == EB_LEAF)) {
432 eb_troot_t *new_left, *new_rght;
433 eb_troot_t *new_leaf, *old_leaf;
434
435 old = container_of(eb_untag(troot, EB_LEAF),
436 struct eb64_node, node.branches);
437
438 new_left = eb_dotag(&new->node.branches, EB_LEFT);
439 new_rght = eb_dotag(&new->node.branches, EB_RGHT);
440 new_leaf = eb_dotag(&new->node.branches, EB_LEAF);
441 old_leaf = eb_dotag(&old->node.branches, EB_LEAF);
442
443 new->node.node_p = old->node.leaf_p;
444
445 /* Right here, we have 3 possibilities :
446 - the tree does not contain the key, and we have
447 new->key < old->key. We insert new above old, on
448 the left ;
449
450 - the tree does not contain the key, and we have
451 new->key > old->key. We insert new above old, on
452 the right ;
453
454 - the tree does contain the key, which implies it
455 is alone. We add the new key next to it as a
456 first duplicate.
457
458 The last two cases can easily be partially merged.
459 */
460
461 if ((s64)new->key < (s64)old->key) {
462 new->node.leaf_p = new_left;
463 old->node.leaf_p = new_rght;
464 new->node.branches.b[EB_LEFT] = new_leaf;
465 new->node.branches.b[EB_RGHT] = old_leaf;
466 } else {
467 /* we may refuse to duplicate this key if the tree is
468 * tagged as containing only unique keys.
469 */
470 if ((new->key == old->key) && eb_gettag(root_right))
471 return old;
472
473 /* new->key >= old->key, new goes the right */
474 old->node.leaf_p = new_left;
475 new->node.leaf_p = new_rght;
476 new->node.branches.b[EB_LEFT] = old_leaf;
477 new->node.branches.b[EB_RGHT] = new_leaf;
478
479 if (new->key == old->key) {
480 new->node.bit = -1;
481 root->b[side] = eb_dotag(&new->node.branches, EB_NODE);
482 return new;
483 }
484 }
485 break;
486 }
487
488 /* OK we're walking down this link */
489 old = container_of(eb_untag(troot, EB_NODE),
490 struct eb64_node, node.branches);
491
492 /* Stop going down when we don't have common bits anymore. We
493 * also stop in front of a duplicates tree because it means we
494 * have to insert above.
495 */
496
497 if ((old->node.bit < 0) || /* we're above a duplicate tree, stop here */
498 (((new->key ^ old->key) >> old->node.bit) >= EB_NODE_BRANCHES)) {
499 /* The tree did not contain the key, so we insert <new> before the node
500 * <old>, and set ->bit to designate the lowest bit position in <new>
501 * which applies to ->branches.b[].
502 */
503 eb_troot_t *new_left, *new_rght;
504 eb_troot_t *new_leaf, *old_node;
505
506 new_left = eb_dotag(&new->node.branches, EB_LEFT);
507 new_rght = eb_dotag(&new->node.branches, EB_RGHT);
508 new_leaf = eb_dotag(&new->node.branches, EB_LEAF);
509 old_node = eb_dotag(&old->node.branches, EB_NODE);
510
511 new->node.node_p = old->node.node_p;
512
513 if ((s64)new->key < (s64)old->key) {
514 new->node.leaf_p = new_left;
515 old->node.node_p = new_rght;
516 new->node.branches.b[EB_LEFT] = new_leaf;
517 new->node.branches.b[EB_RGHT] = old_node;
518 }
519 else if ((s64)new->key > (s64)old->key) {
520 old->node.node_p = new_left;
521 new->node.leaf_p = new_rght;
522 new->node.branches.b[EB_LEFT] = old_node;
523 new->node.branches.b[EB_RGHT] = new_leaf;
524 }
525 else {
526 struct eb_node *ret;
527 ret = eb_insert_dup(&old->node, &new->node);
528 return container_of(ret, struct eb64_node, node);
529 }
530 break;
531 }
532
533 /* walk down */
534 root = &old->node.branches;
535#if BITS_PER_LONG >= 64
536 side = (newkey >> old->node.bit) & EB_NODE_BRANCH_MASK;
537#else
538 side = newkey;
539 side >>= old->node.bit;
540 if (old->node.bit >= 32) {
541 side = newkey >> 32;
542 side >>= old->node.bit & 0x1F;
543 }
544 side &= EB_NODE_BRANCH_MASK;
545#endif
546 troot = root->b[side];
547 }
548
549 /* Ok, now we are inserting <new> between <root> and <old>. <old>'s
550 * parent is already set to <new>, and the <root>'s branch is still in
551 * <side>. Update the root's leaf till we have it. Note that we can also
552 * find the side by checking the side of new->node.node_p.
553 */
554
555 /* We need the common higher bits between new->key and old->key.
556 * What differences are there between new->key and the node here ?
557 * NOTE that bit(new) is always < bit(root) because highest
558 * bit of new->key and old->key are identical here (otherwise they
559 * would sit on different branches).
560 */
561 // note that if EB_NODE_BITS > 1, we should check that it's still >= 0
562 new->node.bit = fls64(new->key ^ old->key) - EB_NODE_BITS;
563 root->b[side] = eb_dotag(&new->node.branches, EB_NODE);
564
565 return new;
566}
567
568#endif /* _EB64_TREE_H */