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