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