blob: 4f48d58358a963f8f8b8612e7f1182df6338aebc [file] [log] [blame]
Willy Tarreauca308392017-11-05 13:31:29 +01001/*
2 * Elastic Binary Trees - exported functions for operations on 32bit nodes.
3 * Version 6.0.6 with backports from v7-dev
4 * (C) 2002-2011 - Willy Tarreau <w@1wt.eu>
5 *
6 * 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.
10 *
11 * This library 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 GNU
14 * Lesser General Public License for more details.
15 *
16 * 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
19 */
20
21/* Consult eb32sctree.h for more details about those functions */
22
23#include "eb32sctree.h"
24
25
26/* This function is used to build a tree of duplicates by adding a new node to
27 * a subtree of at least 2 entries.
28 */
29REGPRM1 struct eb32sc_node *eb32sc_insert_dup(struct eb_node *sub, struct eb_node *new, unsigned long scope)
30{
Willy Tarreau5d19fd42017-11-05 14:06:50 +010031 struct eb32sc_node *eb32;
Willy Tarreauca308392017-11-05 13:31:29 +010032 struct eb_node *head = sub;
33 eb_troot_t *new_left = eb_dotag(&new->branches, EB_LEFT);
34 eb_troot_t *new_rght = eb_dotag(&new->branches, EB_RGHT);
35 eb_troot_t *new_leaf = eb_dotag(&new->branches, EB_LEAF);
36
37 /* first, identify the deepest hole on the right branch */
38 while (eb_gettag(head->branches.b[EB_RGHT]) != EB_LEAF) {
39 struct eb_node *last = head;
Willy Tarreau5d19fd42017-11-05 14:06:50 +010040
Willy Tarreauca308392017-11-05 13:31:29 +010041 head = container_of(eb_untag(head->branches.b[EB_RGHT], EB_NODE),
42 struct eb_node, branches);
Willy Tarreau5d19fd42017-11-05 14:06:50 +010043 eb32 = container_of(head, struct eb32sc_node, node);
44
Willy Tarreauca308392017-11-05 13:31:29 +010045 if (head->bit > last->bit + 1)
46 sub = head; /* there's a hole here */
Willy Tarreau5d19fd42017-11-05 14:06:50 +010047
48 if ((eb32->node_s | scope) != eb32->node_s)
49 eb32->node_s |= scope;
Willy Tarreauca308392017-11-05 13:31:29 +010050 }
51
52 /* Here we have a leaf attached to (head)->b[EB_RGHT] */
53 if (head->bit < -1) {
54 /* A hole exists just before the leaf, we insert there */
55 new->bit = -1;
56 sub = container_of(eb_untag(head->branches.b[EB_RGHT], EB_LEAF),
57 struct eb_node, branches);
58 head->branches.b[EB_RGHT] = eb_dotag(&new->branches, EB_NODE);
59
60 new->node_p = sub->leaf_p;
61 new->leaf_p = new_rght;
62 sub->leaf_p = new_left;
63 new->branches.b[EB_LEFT] = eb_dotag(&sub->branches, EB_LEAF);
64 new->branches.b[EB_RGHT] = new_leaf;
Willy Tarreau5d19fd42017-11-05 14:06:50 +010065 eb32 = container_of(new, struct eb32sc_node, node);
66 eb32->node_s = container_of(sub, struct eb32sc_node, node)->leaf_s | scope;
67 return eb32;
Willy Tarreauca308392017-11-05 13:31:29 +010068 } else {
69 int side;
70 /* No hole was found before a leaf. We have to insert above
71 * <sub>. Note that we cannot be certain that <sub> is attached
72 * to the right of its parent, as this is only true if <sub>
73 * is inside the dup tree, not at the head.
74 */
75 new->bit = sub->bit - 1; /* install at the lowest level */
76 side = eb_gettag(sub->node_p);
77 head = container_of(eb_untag(sub->node_p, side), struct eb_node, branches);
78 head->branches.b[side] = eb_dotag(&new->branches, EB_NODE);
79
80 new->node_p = sub->node_p;
81 new->leaf_p = new_rght;
82 sub->node_p = new_left;
83 new->branches.b[EB_LEFT] = eb_dotag(&sub->branches, EB_NODE);
84 new->branches.b[EB_RGHT] = new_leaf;
Willy Tarreau5d19fd42017-11-05 14:06:50 +010085 eb32 = container_of(new, struct eb32sc_node, node);
86 eb32->node_s = container_of(sub, struct eb32sc_node, node)->node_s | scope;
87 return eb32;
Willy Tarreauca308392017-11-05 13:31:29 +010088 }
89}
90
91/* Insert eb32sc_node <new> into subtree starting at node root <root>. Only
92 * new->key needs be set with the key. The eb32sc_node is returned. This
93 * implementation does NOT support unique trees.
94 */
95REGPRM2 struct eb32sc_node *eb32sc_insert(struct eb_root *root, struct eb32sc_node *new, unsigned long scope)
96{
97 struct eb32sc_node *old;
98 unsigned int side;
99 eb_troot_t *troot, **up_ptr;
100 u32 newkey; /* caching the key saves approximately one cycle */
101 eb_troot_t *new_left, *new_rght;
102 eb_troot_t *new_leaf;
103 int old_node_bit;
Willy Tarreau5d19fd42017-11-05 14:06:50 +0100104 unsigned long old_scope;
Willy Tarreauca308392017-11-05 13:31:29 +0100105
106 side = EB_LEFT;
107 troot = root->b[EB_LEFT];
108 if (unlikely(troot == NULL)) {
109 /* Tree is empty, insert the leaf part below the left branch */
110 root->b[EB_LEFT] = eb_dotag(&new->node.branches, EB_LEAF);
111 new->node.leaf_p = eb_dotag(root, EB_LEFT);
112 new->node.node_p = NULL; /* node part unused */
Willy Tarreau5d19fd42017-11-05 14:06:50 +0100113 new->node_s = scope;
114 new->leaf_s = scope;
Willy Tarreauca308392017-11-05 13:31:29 +0100115 return new;
116 }
117
118 /* The tree descent is fairly easy :
119 * - first, check if we have reached a leaf node
120 * - second, check if we have gone too far
121 * - third, reiterate
122 * Everywhere, we use <new> for the node node we are inserting, <root>
123 * for the node we attach it to, and <old> for the node we are
124 * displacing below <new>. <troot> will always point to the future node
125 * (tagged with its type). <side> carries the side the node <new> is
126 * attached to below its parent, which is also where previous node
127 * was attached. <newkey> carries the key being inserted.
128 */
129 newkey = new->key;
130
131 while (1) {
132 if (eb_gettag(troot) == EB_LEAF) {
133 /* insert above a leaf */
134 old = container_of(eb_untag(troot, EB_LEAF),
135 struct eb32sc_node, node.branches);
136 new->node.node_p = old->node.leaf_p;
137 up_ptr = &old->node.leaf_p;
Willy Tarreau5d19fd42017-11-05 14:06:50 +0100138 old_scope = old->leaf_s;
Willy Tarreauca308392017-11-05 13:31:29 +0100139 break;
140 }
141
142 /* OK we're walking down this link */
143 old = container_of(eb_untag(troot, EB_NODE),
144 struct eb32sc_node, node.branches);
145 old_node_bit = old->node.bit;
146
147 /* Stop going down when we don't have common bits anymore. We
148 * also stop in front of a duplicates tree because it means we
149 * have to insert above.
150 */
151
152 if ((old_node_bit < 0) || /* we're above a duplicate tree, stop here */
153 (((new->key ^ old->key) >> old_node_bit) >= EB_NODE_BRANCHES)) {
154 /* The tree did not contain the key, so we insert <new> before the node
155 * <old>, and set ->bit to designate the lowest bit position in <new>
156 * which applies to ->branches.b[].
157 */
158 new->node.node_p = old->node.node_p;
159 up_ptr = &old->node.node_p;
Willy Tarreau5d19fd42017-11-05 14:06:50 +0100160 old_scope = old->node_s;
Willy Tarreauca308392017-11-05 13:31:29 +0100161 break;
162 }
163
164 /* walk down */
Willy Tarreau5d19fd42017-11-05 14:06:50 +0100165 if ((old->node_s | scope) != old->node_s)
166 old->node_s |= scope;
167
Willy Tarreauca308392017-11-05 13:31:29 +0100168 root = &old->node.branches;
169 side = (newkey >> old_node_bit) & EB_NODE_BRANCH_MASK;
170 troot = root->b[side];
171 }
172
173 new_left = eb_dotag(&new->node.branches, EB_LEFT);
174 new_rght = eb_dotag(&new->node.branches, EB_RGHT);
175 new_leaf = eb_dotag(&new->node.branches, EB_LEAF);
176
177 /* We need the common higher bits between new->key and old->key.
178 * What differences are there between new->key and the node here ?
179 * NOTE that bit(new) is always < bit(root) because highest
180 * bit of new->key and old->key are identical here (otherwise they
181 * would sit on different branches).
182 */
183
184 // note that if EB_NODE_BITS > 1, we should check that it's still >= 0
185 new->node.bit = flsnz(new->key ^ old->key) - EB_NODE_BITS;
Willy Tarreau5d19fd42017-11-05 14:06:50 +0100186 new->leaf_s = scope;
187 new->node_s = old_scope | scope;
Willy Tarreauca308392017-11-05 13:31:29 +0100188
189 if (new->key == old->key) {
190 new->node.bit = -1; /* mark as new dup tree, just in case */
191
192 if (eb_gettag(troot) != EB_LEAF) {
193 /* there was already a dup tree below */
194 return eb32sc_insert_dup(&old->node, &new->node, scope);
195 }
196 /* otherwise fall through */
197 }
198
199 if (new->key >= old->key) {
200 new->node.branches.b[EB_LEFT] = troot;
201 new->node.branches.b[EB_RGHT] = new_leaf;
202 new->node.leaf_p = new_rght;
203 *up_ptr = new_left;
204 }
205 else {
206 new->node.branches.b[EB_LEFT] = new_leaf;
207 new->node.branches.b[EB_RGHT] = troot;
208 new->node.leaf_p = new_left;
209 *up_ptr = new_rght;
210 }
211
212 /* Ok, now we are inserting <new> between <root> and <old>. <old>'s
213 * parent is already set to <new>, and the <root>'s branch is still in
214 * <side>. Update the root's leaf till we have it. Note that we can also
215 * find the side by checking the side of new->node.node_p.
216 */
217
218 root->b[side] = eb_dotag(&new->node.branches, EB_NODE);
219 return new;
220}
221
222/*
223 * Find the first occurrence of the lowest key in the tree <root>, which is
224 * equal to or greater than <x>. NULL is returned is no key matches.
225 */
226REGPRM2 struct eb32sc_node *eb32sc_lookup_ge(struct eb_root *root, u32 x, unsigned long scope)
227{
228 struct eb32sc_node *node;
Willy Tarreaud1d55ac2017-11-05 14:33:01 +0100229 struct eb_root *curr;
Willy Tarreauca308392017-11-05 13:31:29 +0100230 eb_troot_t *troot;
231
232 troot = root->b[EB_LEFT];
233 if (unlikely(troot == NULL))
234 return NULL;
235
236 while (1) {
237 if ((eb_gettag(troot) == EB_LEAF)) {
238 /* We reached a leaf, which means that the whole upper
239 * parts were common. We will return either the current
240 * node or its next one if the former is too small.
241 */
242 node = container_of(eb_untag(troot, EB_LEAF),
243 struct eb32sc_node, node.branches);
Willy Tarreaud1d55ac2017-11-05 14:33:01 +0100244 if ((node->leaf_s & scope) && node->key >= x)
Willy Tarreauca308392017-11-05 13:31:29 +0100245 return node;
246 /* return next */
247 troot = node->node.leaf_p;
248 break;
249 }
250 node = container_of(eb_untag(troot, EB_NODE),
251 struct eb32sc_node, node.branches);
252
253 if (node->node.bit < 0) {
254 /* We're at the top of a dup tree. Either we got a
255 * matching value and we return the leftmost node, or
256 * we don't and we skip the whole subtree to return the
257 * next node after the subtree. Note that since we're
258 * at the top of the dup tree, we can simply return the
259 * next node without first trying to escape from the
260 * tree.
261 */
Willy Tarreaud1d55ac2017-11-05 14:33:01 +0100262 if ((node->node_s & scope) && node->key >= x)
263 troot = eb_dotag(&node->node.branches, EB_LEFT);
264 else
265 troot = node->node.node_p;
Willy Tarreauca308392017-11-05 13:31:29 +0100266 break;
267 }
268
269 if (((x ^ node->key) >> node->node.bit) >= EB_NODE_BRANCHES) {
270 /* No more common bits at all. Either this node is too
271 * large and we need to get its lowest value, or it is too
272 * small, and we need to get the next value.
273 */
Willy Tarreaud1d55ac2017-11-05 14:33:01 +0100274 if ((node->node_s & scope) && (node->key >> node->node.bit) > (x >> node->node.bit))
275 troot = eb_dotag(&node->node.branches, EB_LEFT);
276 else
277 troot = node->node.node_p;
Willy Tarreauca308392017-11-05 13:31:29 +0100278 break;
279 }
280 troot = node->node.branches.b[(x >> node->node.bit) & EB_NODE_BRANCH_MASK];
281 }
282
283 /* If we get here, it means we want to report next node after the
284 * current one which is not below. <troot> is already initialised
285 * to the parent's branches.
286 */
Willy Tarreaud1d55ac2017-11-05 14:33:01 +0100287 for (node = NULL; !node; troot = eb_root_to_node(curr)->node_p) {
288 if (eb_gettag(troot) != EB_LEFT) {
289 curr = eb_untag(troot, EB_RGHT);
290 continue;
291 }
Willy Tarreauca308392017-11-05 13:31:29 +0100292
Willy Tarreaud1d55ac2017-11-05 14:33:01 +0100293 /* troot points to the branch location we're attached to by the
294 * left above, set curr to the corresponding eb_root.
295 */
296 curr = eb_untag(troot, EB_LEFT);
297
298 /* and go down by the right, but stop at the root */
299 troot = curr->b[EB_RGHT];
300 if (!eb_clrtag(troot))
301 break;
Willy Tarreauca308392017-11-05 13:31:29 +0100302
Willy Tarreaud1d55ac2017-11-05 14:33:01 +0100303 node = eb32sc_walk_down_left(troot, scope);
304 }
305 return node;
306 //while (1) {
307 // while (eb_gettag(troot) != EB_LEFT)
308 // /* Walking up from right branch, so we cannot be below root */
309 // troot = (eb_root_to_node(eb_untag(troot, EB_RGHT)))->node_p;
310 //
311 // /* Note that <t> cannot be NULL at this stage */
312 // root = eb_untag(troot, EB_LEFT);
313 // troot = root->b[EB_RGHT];
314 // if (eb_clrtag(troot) == NULL)
315 // return NULL;
316 //
317 // /* we can't be below the root here */
318 // node = eb32sc_walk_down_left(troot, scope);
319 // if (node)
320 // return node;
321 // /* not found below, this means we have to go up */
322 // troot = eb_root_to_node(root)->node_p;
323 //}
Willy Tarreauca308392017-11-05 13:31:29 +0100324}
325
Willy Tarreau8878b6c2017-11-05 21:23:21 +0100326/*
327 * Find the first occurrence of the lowest key in the tree <root> which is
328 * equal to or greater than <x>, matching scope <scope>. If not found, it loops
329 * back to the beginning of the tree. NULL is returned is no key matches.
330 */
331REGPRM2 struct eb32sc_node *eb32sc_lookup_ge_or_first(struct eb_root *root, u32 x, unsigned long scope)
332{
333 struct eb32sc_node *eb32;
334 eb_troot_t *troot;
335 struct eb_root *curr;
336
337 troot = root->b[EB_LEFT];
338 if (unlikely(troot == NULL))
339 return NULL;
340
341 while (1) {
342 if ((eb_gettag(troot) == EB_LEAF)) {
343 /* We reached a leaf, which means that the whole upper
344 * parts were common. We will return either the current
345 * node or its next one if the former is too small.
346 */
347 eb32 = container_of(eb_untag(troot, EB_LEAF),
348 struct eb32sc_node, node.branches);
349 if ((eb32->leaf_s & scope) && eb32->key >= x)
350 return eb32;
351 /* return next */
352 troot = eb32->node.leaf_p;
353 break;
354 }
355 eb32 = container_of(eb_untag(troot, EB_NODE),
356 struct eb32sc_node, node.branches);
357
358 if (eb32->node.bit < 0) {
359 /* We're at the top of a dup tree. Either we got a
360 * matching value and we return the leftmost node, or
361 * we don't and we skip the whole subtree to return the
362 * next node after the subtree. Note that since we're
363 * at the top of the dup tree, we can simply return the
364 * next node without first trying to escape from the
365 * tree.
366 */
367 if ((eb32->node_s & scope) && eb32->key >= x)
368 troot = eb_dotag(&eb32->node.branches, EB_LEFT);
369 else
370 troot = eb32->node.node_p;
371 break;
372 }
373
374 if (((x ^ eb32->key) >> eb32->node.bit) >= EB_NODE_BRANCHES) {
375 /* No more common bits at all. Either this node is too
376 * large and we need to get its lowest value, or it is too
377 * small, and we need to get the next value.
378 */
379 if ((eb32->node_s & scope) && (eb32->key >> eb32->node.bit) > (x >> eb32->node.bit))
380 troot = eb_dotag(&eb32->node.branches, EB_LEFT);
381 else
382 troot = eb32->node.node_p;
383 break;
384 }
385 troot = eb32->node.branches.b[(x >> eb32->node.bit) & EB_NODE_BRANCH_MASK];
386 }
387
388 /* If we get here, it means we want to report next node after the
389 * current one which is not below. <troot> is already initialised
390 * to the parent's branches.
391 */
392 for (eb32 = NULL; !eb32; troot = eb_root_to_node(curr)->node_p) {
393 if (eb_gettag(troot) != EB_LEFT) {
394 curr = eb_untag(troot, EB_RGHT);
395 continue;
396 }
397
398 /* troot points to the branch location we're attached to by the
399 * left above, set curr to the corresponding eb_root.
400 */
401 curr = eb_untag(troot, EB_LEFT);
402
403 /* and go down by the right, but stop at the root */
404 troot = curr->b[EB_RGHT];
405 if (!eb_clrtag(troot))
406 break;
407
408 eb32 = eb32sc_walk_down_left(troot, scope);
409 }
410
411 if (!eb32)
412 eb32 = eb32sc_walk_down_left(root->b[EB_LEFT], scope);
413
414 return eb32;
415}
416
Willy Tarreauca308392017-11-05 13:31:29 +0100417/* Removes a leaf node from the tree if it was still in it. Marks the node
418 * as unlinked.
419 */
420void eb32sc_delete(struct eb32sc_node *eb32)
421{
422 struct eb_node *node = &eb32->node;
423 unsigned int pside, gpside, sibtype;
424 struct eb_node *parent;
425 struct eb_root *gparent;
426
427 if (!node->leaf_p)
428 return;
429
430 /* we need the parent, our side, and the grand parent */
431 pside = eb_gettag(node->leaf_p);
432 parent = eb_root_to_node(eb_untag(node->leaf_p, pside));
433
434 /* We likely have to release the parent link, unless it's the root,
435 * in which case we only set our branch to NULL. Note that we can
436 * only be attached to the root by its left branch.
437 */
438
439 if (eb_clrtag(parent->branches.b[EB_RGHT]) == NULL) {
440 /* we're just below the root, it's trivial. */
441 parent->branches.b[EB_LEFT] = NULL;
442 goto delete_unlink;
443 }
444
445 /* To release our parent, we have to identify our sibling, and reparent
446 * it directly to/from the grand parent. Note that the sibling can
447 * either be a link or a leaf.
448 */
449
450 gpside = eb_gettag(parent->node_p);
451 gparent = eb_untag(parent->node_p, gpside);
452
453 gparent->b[gpside] = parent->branches.b[!pside];
454 sibtype = eb_gettag(gparent->b[gpside]);
455
456 if (sibtype == EB_LEAF) {
457 eb_root_to_node(eb_untag(gparent->b[gpside], EB_LEAF))->leaf_p =
458 eb_dotag(gparent, gpside);
459 } else {
460 eb_root_to_node(eb_untag(gparent->b[gpside], EB_NODE))->node_p =
461 eb_dotag(gparent, gpside);
462 }
463 /* Mark the parent unused. Note that we do not check if the parent is
464 * our own node, but that's not a problem because if it is, it will be
465 * marked unused at the same time, which we'll use below to know we can
466 * safely remove it.
467 */
468 parent->node_p = NULL;
469
470 /* The parent node has been detached, and is currently unused. It may
471 * belong to another node, so we cannot remove it that way. Also, our
472 * own node part might still be used. so we can use this spare node
473 * to replace ours if needed.
474 */
475
476 /* If our link part is unused, we can safely exit now */
477 if (!node->node_p)
478 goto delete_unlink;
479
480 /* From now on, <node> and <parent> are necessarily different, and the
481 * <node>'s node part is in use. By definition, <parent> is at least
Willy Tarreauef8d0dc2017-11-05 18:06:22 +0100482 * below <node>, so keeping its key for the bit string is OK. However
483 * its scope must be enlarged to cover the new branch it absorbs.
Willy Tarreauca308392017-11-05 13:31:29 +0100484 */
485
486 parent->node_p = node->node_p;
487 parent->branches = node->branches;
488 parent->bit = node->bit;
Willy Tarreauef8d0dc2017-11-05 18:06:22 +0100489 container_of(parent, struct eb32sc_node, node)->node_s |= eb32->node_s;
Willy Tarreauca308392017-11-05 13:31:29 +0100490
491 /* We must now update the new node's parent... */
492 gpside = eb_gettag(parent->node_p);
493 gparent = eb_untag(parent->node_p, gpside);
494 gparent->b[gpside] = eb_dotag(&parent->branches, EB_NODE);
495
496 /* ... and its branches */
497 for (pside = 0; pside <= 1; pside++) {
498 if (eb_gettag(parent->branches.b[pside]) == EB_NODE) {
499 eb_root_to_node(eb_untag(parent->branches.b[pside], EB_NODE))->node_p =
500 eb_dotag(&parent->branches, pside);
501 } else {
502 eb_root_to_node(eb_untag(parent->branches.b[pside], EB_LEAF))->leaf_p =
503 eb_dotag(&parent->branches, pside);
504 }
505 }
506 delete_unlink:
507 /* Now the node has been completely unlinked */
508 node->leaf_p = NULL;
509 return; /* tree is not empty yet */
510}