blob: a8a38f8ce506816fc85fb21b4dc294620702850e [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 Tarreaud19ec7d2017-11-13 16:16:09 +010043
44 if (unlikely(head->bit > last->bit + 1)) {
45 /* there's a hole here, we must assign the top of the
46 * following sub-tree to <sub> and mark all intermediate
47 * nodes with the scope mask.
48 */
49 do {
50 eb32 = container_of(sub, struct eb32sc_node, node);
51 if (!(eb32->node_s & scope))
52 eb32->node_s |= scope;
Willy Tarreau5d19fd42017-11-05 14:06:50 +010053
Willy Tarreaud19ec7d2017-11-13 16:16:09 +010054 sub = container_of(eb_untag(sub->branches.b[EB_RGHT], EB_NODE),
55 struct eb_node, branches);
56 } while (sub != head);
57 }
Willy Tarreau5d19fd42017-11-05 14:06:50 +010058
Willy Tarreaud19ec7d2017-11-13 16:16:09 +010059 eb32 = container_of(head, struct eb32sc_node, node);
Willy Tarreauca308392017-11-05 13:31:29 +010060 }
61
62 /* Here we have a leaf attached to (head)->b[EB_RGHT] */
63 if (head->bit < -1) {
64 /* A hole exists just before the leaf, we insert there */
65 new->bit = -1;
66 sub = container_of(eb_untag(head->branches.b[EB_RGHT], EB_LEAF),
67 struct eb_node, branches);
68 head->branches.b[EB_RGHT] = eb_dotag(&new->branches, EB_NODE);
69
70 new->node_p = sub->leaf_p;
71 new->leaf_p = new_rght;
72 sub->leaf_p = new_left;
73 new->branches.b[EB_LEFT] = eb_dotag(&sub->branches, EB_LEAF);
74 new->branches.b[EB_RGHT] = new_leaf;
Willy Tarreau5d19fd42017-11-05 14:06:50 +010075 eb32 = container_of(new, struct eb32sc_node, node);
76 eb32->node_s = container_of(sub, struct eb32sc_node, node)->leaf_s | scope;
77 return eb32;
Willy Tarreauca308392017-11-05 13:31:29 +010078 } else {
79 int side;
80 /* No hole was found before a leaf. We have to insert above
81 * <sub>. Note that we cannot be certain that <sub> is attached
82 * to the right of its parent, as this is only true if <sub>
83 * is inside the dup tree, not at the head.
84 */
85 new->bit = sub->bit - 1; /* install at the lowest level */
86 side = eb_gettag(sub->node_p);
87 head = container_of(eb_untag(sub->node_p, side), struct eb_node, branches);
88 head->branches.b[side] = eb_dotag(&new->branches, EB_NODE);
89
90 new->node_p = sub->node_p;
91 new->leaf_p = new_rght;
92 sub->node_p = new_left;
93 new->branches.b[EB_LEFT] = eb_dotag(&sub->branches, EB_NODE);
94 new->branches.b[EB_RGHT] = new_leaf;
Willy Tarreau5d19fd42017-11-05 14:06:50 +010095 eb32 = container_of(new, struct eb32sc_node, node);
96 eb32->node_s = container_of(sub, struct eb32sc_node, node)->node_s | scope;
97 return eb32;
Willy Tarreauca308392017-11-05 13:31:29 +010098 }
99}
100
101/* Insert eb32sc_node <new> into subtree starting at node root <root>. Only
102 * new->key needs be set with the key. The eb32sc_node is returned. This
103 * implementation does NOT support unique trees.
104 */
105REGPRM2 struct eb32sc_node *eb32sc_insert(struct eb_root *root, struct eb32sc_node *new, unsigned long scope)
106{
107 struct eb32sc_node *old;
108 unsigned int side;
109 eb_troot_t *troot, **up_ptr;
110 u32 newkey; /* caching the key saves approximately one cycle */
111 eb_troot_t *new_left, *new_rght;
112 eb_troot_t *new_leaf;
113 int old_node_bit;
Willy Tarreau5d19fd42017-11-05 14:06:50 +0100114 unsigned long old_scope;
Willy Tarreauca308392017-11-05 13:31:29 +0100115
116 side = EB_LEFT;
117 troot = root->b[EB_LEFT];
118 if (unlikely(troot == NULL)) {
119 /* Tree is empty, insert the leaf part below the left branch */
120 root->b[EB_LEFT] = eb_dotag(&new->node.branches, EB_LEAF);
121 new->node.leaf_p = eb_dotag(root, EB_LEFT);
122 new->node.node_p = NULL; /* node part unused */
Willy Tarreau5d19fd42017-11-05 14:06:50 +0100123 new->node_s = scope;
124 new->leaf_s = scope;
Willy Tarreauca308392017-11-05 13:31:29 +0100125 return new;
126 }
127
128 /* The tree descent is fairly easy :
129 * - first, check if we have reached a leaf node
130 * - second, check if we have gone too far
131 * - third, reiterate
132 * Everywhere, we use <new> for the node node we are inserting, <root>
133 * for the node we attach it to, and <old> for the node we are
134 * displacing below <new>. <troot> will always point to the future node
135 * (tagged with its type). <side> carries the side the node <new> is
136 * attached to below its parent, which is also where previous node
137 * was attached. <newkey> carries the key being inserted.
138 */
139 newkey = new->key;
140
141 while (1) {
142 if (eb_gettag(troot) == EB_LEAF) {
143 /* insert above a leaf */
144 old = container_of(eb_untag(troot, EB_LEAF),
145 struct eb32sc_node, node.branches);
146 new->node.node_p = old->node.leaf_p;
147 up_ptr = &old->node.leaf_p;
Willy Tarreau5d19fd42017-11-05 14:06:50 +0100148 old_scope = old->leaf_s;
Willy Tarreauca308392017-11-05 13:31:29 +0100149 break;
150 }
151
152 /* OK we're walking down this link */
153 old = container_of(eb_untag(troot, EB_NODE),
154 struct eb32sc_node, node.branches);
155 old_node_bit = old->node.bit;
156
157 /* Stop going down when we don't have common bits anymore. We
158 * also stop in front of a duplicates tree because it means we
159 * have to insert above.
160 */
161
162 if ((old_node_bit < 0) || /* we're above a duplicate tree, stop here */
163 (((new->key ^ old->key) >> old_node_bit) >= EB_NODE_BRANCHES)) {
164 /* The tree did not contain the key, so we insert <new> before the node
165 * <old>, and set ->bit to designate the lowest bit position in <new>
166 * which applies to ->branches.b[].
167 */
168 new->node.node_p = old->node.node_p;
169 up_ptr = &old->node.node_p;
Willy Tarreau5d19fd42017-11-05 14:06:50 +0100170 old_scope = old->node_s;
Willy Tarreauca308392017-11-05 13:31:29 +0100171 break;
172 }
173
174 /* walk down */
Willy Tarreau5d19fd42017-11-05 14:06:50 +0100175 if ((old->node_s | scope) != old->node_s)
176 old->node_s |= scope;
177
Willy Tarreauca308392017-11-05 13:31:29 +0100178 root = &old->node.branches;
179 side = (newkey >> old_node_bit) & EB_NODE_BRANCH_MASK;
180 troot = root->b[side];
181 }
182
183 new_left = eb_dotag(&new->node.branches, EB_LEFT);
184 new_rght = eb_dotag(&new->node.branches, EB_RGHT);
185 new_leaf = eb_dotag(&new->node.branches, EB_LEAF);
186
187 /* We need the common higher bits between new->key and old->key.
188 * What differences are there between new->key and the node here ?
189 * NOTE that bit(new) is always < bit(root) because highest
190 * bit of new->key and old->key are identical here (otherwise they
191 * would sit on different branches).
192 */
193
194 // note that if EB_NODE_BITS > 1, we should check that it's still >= 0
195 new->node.bit = flsnz(new->key ^ old->key) - EB_NODE_BITS;
Willy Tarreau5d19fd42017-11-05 14:06:50 +0100196 new->leaf_s = scope;
197 new->node_s = old_scope | scope;
Willy Tarreauca308392017-11-05 13:31:29 +0100198
199 if (new->key == old->key) {
200 new->node.bit = -1; /* mark as new dup tree, just in case */
201
202 if (eb_gettag(troot) != EB_LEAF) {
203 /* there was already a dup tree below */
204 return eb32sc_insert_dup(&old->node, &new->node, scope);
205 }
206 /* otherwise fall through */
207 }
208
209 if (new->key >= old->key) {
210 new->node.branches.b[EB_LEFT] = troot;
211 new->node.branches.b[EB_RGHT] = new_leaf;
212 new->node.leaf_p = new_rght;
213 *up_ptr = new_left;
214 }
215 else {
216 new->node.branches.b[EB_LEFT] = new_leaf;
217 new->node.branches.b[EB_RGHT] = troot;
218 new->node.leaf_p = new_left;
219 *up_ptr = new_rght;
220 }
221
222 /* Ok, now we are inserting <new> between <root> and <old>. <old>'s
223 * parent is already set to <new>, and the <root>'s branch is still in
224 * <side>. Update the root's leaf till we have it. Note that we can also
225 * find the side by checking the side of new->node.node_p.
226 */
227
228 root->b[side] = eb_dotag(&new->node.branches, EB_NODE);
229 return new;
230}
231
232/*
233 * Find the first occurrence of the lowest key in the tree <root>, which is
234 * equal to or greater than <x>. NULL is returned is no key matches.
235 */
236REGPRM2 struct eb32sc_node *eb32sc_lookup_ge(struct eb_root *root, u32 x, unsigned long scope)
237{
238 struct eb32sc_node *node;
239 eb_troot_t *troot;
240
241 troot = root->b[EB_LEFT];
242 if (unlikely(troot == NULL))
243 return NULL;
244
245 while (1) {
246 if ((eb_gettag(troot) == EB_LEAF)) {
247 /* We reached a leaf, which means that the whole upper
248 * parts were common. We will return either the current
249 * node or its next one if the former is too small.
250 */
251 node = container_of(eb_untag(troot, EB_LEAF),
252 struct eb32sc_node, node.branches);
Willy Tarreaud1d55ac2017-11-05 14:33:01 +0100253 if ((node->leaf_s & scope) && node->key >= x)
Willy Tarreauca308392017-11-05 13:31:29 +0100254 return node;
255 /* return next */
256 troot = node->node.leaf_p;
257 break;
258 }
259 node = container_of(eb_untag(troot, EB_NODE),
260 struct eb32sc_node, node.branches);
261
262 if (node->node.bit < 0) {
263 /* We're at the top of a dup tree. Either we got a
264 * matching value and we return the leftmost node, or
265 * we don't and we skip the whole subtree to return the
266 * next node after the subtree. Note that since we're
267 * at the top of the dup tree, we can simply return the
268 * next node without first trying to escape from the
269 * tree.
270 */
Willy Tarreaud1d55ac2017-11-05 14:33:01 +0100271 if ((node->node_s & scope) && node->key >= x)
272 troot = eb_dotag(&node->node.branches, EB_LEFT);
273 else
274 troot = node->node.node_p;
Willy Tarreauca308392017-11-05 13:31:29 +0100275 break;
276 }
277
278 if (((x ^ node->key) >> node->node.bit) >= EB_NODE_BRANCHES) {
279 /* No more common bits at all. Either this node is too
280 * large and we need to get its lowest value, or it is too
281 * small, and we need to get the next value.
282 */
Willy Tarreaud1d55ac2017-11-05 14:33:01 +0100283 if ((node->node_s & scope) && (node->key >> node->node.bit) > (x >> node->node.bit))
284 troot = eb_dotag(&node->node.branches, EB_LEFT);
285 else
286 troot = node->node.node_p;
Willy Tarreauca308392017-11-05 13:31:29 +0100287 break;
288 }
289 troot = node->node.branches.b[(x >> node->node.bit) & EB_NODE_BRANCH_MASK];
290 }
291
292 /* If we get here, it means we want to report next node after the
293 * current one which is not below. <troot> is already initialised
294 * to the parent's branches.
295 */
Willy Tarreaucfaa6e72017-11-13 19:17:54 +0100296 return eb32sc_next_with_parent(troot, scope);
Willy Tarreauca308392017-11-05 13:31:29 +0100297}
298
Willy Tarreau8878b6c2017-11-05 21:23:21 +0100299/*
300 * Find the first occurrence of the lowest key in the tree <root> which is
301 * equal to or greater than <x>, matching scope <scope>. If not found, it loops
302 * back to the beginning of the tree. NULL is returned is no key matches.
303 */
304REGPRM2 struct eb32sc_node *eb32sc_lookup_ge_or_first(struct eb_root *root, u32 x, unsigned long scope)
305{
306 struct eb32sc_node *eb32;
307 eb_troot_t *troot;
Willy Tarreau8878b6c2017-11-05 21:23:21 +0100308
309 troot = root->b[EB_LEFT];
310 if (unlikely(troot == NULL))
311 return NULL;
312
313 while (1) {
314 if ((eb_gettag(troot) == EB_LEAF)) {
315 /* We reached a leaf, which means that the whole upper
316 * parts were common. We will return either the current
317 * node or its next one if the former is too small.
318 */
319 eb32 = container_of(eb_untag(troot, EB_LEAF),
320 struct eb32sc_node, node.branches);
321 if ((eb32->leaf_s & scope) && eb32->key >= x)
322 return eb32;
323 /* return next */
324 troot = eb32->node.leaf_p;
325 break;
326 }
327 eb32 = container_of(eb_untag(troot, EB_NODE),
328 struct eb32sc_node, node.branches);
329
330 if (eb32->node.bit < 0) {
331 /* We're at the top of a dup tree. Either we got a
332 * matching value and we return the leftmost node, or
333 * we don't and we skip the whole subtree to return the
334 * next node after the subtree. Note that since we're
335 * at the top of the dup tree, we can simply return the
336 * next node without first trying to escape from the
337 * tree.
338 */
339 if ((eb32->node_s & scope) && eb32->key >= x)
340 troot = eb_dotag(&eb32->node.branches, EB_LEFT);
341 else
342 troot = eb32->node.node_p;
343 break;
344 }
345
346 if (((x ^ eb32->key) >> eb32->node.bit) >= EB_NODE_BRANCHES) {
347 /* No more common bits at all. Either this node is too
348 * large and we need to get its lowest value, or it is too
349 * small, and we need to get the next value.
350 */
351 if ((eb32->node_s & scope) && (eb32->key >> eb32->node.bit) > (x >> eb32->node.bit))
352 troot = eb_dotag(&eb32->node.branches, EB_LEFT);
353 else
354 troot = eb32->node.node_p;
355 break;
356 }
357 troot = eb32->node.branches.b[(x >> eb32->node.bit) & EB_NODE_BRANCH_MASK];
358 }
359
360 /* If we get here, it means we want to report next node after the
361 * current one which is not below. <troot> is already initialised
362 * to the parent's branches.
363 */
Willy Tarreaucfaa6e72017-11-13 19:17:54 +0100364 eb32 = eb32sc_next_with_parent(troot, scope);
Willy Tarreau8878b6c2017-11-05 21:23:21 +0100365 if (!eb32)
366 eb32 = eb32sc_walk_down_left(root->b[EB_LEFT], scope);
367
368 return eb32;
369}
370
Willy Tarreauca308392017-11-05 13:31:29 +0100371/* Removes a leaf node from the tree if it was still in it. Marks the node
372 * as unlinked.
373 */
374void eb32sc_delete(struct eb32sc_node *eb32)
375{
376 struct eb_node *node = &eb32->node;
377 unsigned int pside, gpside, sibtype;
378 struct eb_node *parent;
379 struct eb_root *gparent;
Willy Tarreaud19ec7d2017-11-13 16:16:09 +0100380 unsigned long scope;
Willy Tarreauca308392017-11-05 13:31:29 +0100381
382 if (!node->leaf_p)
383 return;
384
385 /* we need the parent, our side, and the grand parent */
386 pside = eb_gettag(node->leaf_p);
387 parent = eb_root_to_node(eb_untag(node->leaf_p, pside));
388
389 /* We likely have to release the parent link, unless it's the root,
390 * in which case we only set our branch to NULL. Note that we can
391 * only be attached to the root by its left branch.
392 */
393
394 if (eb_clrtag(parent->branches.b[EB_RGHT]) == NULL) {
395 /* we're just below the root, it's trivial. */
396 parent->branches.b[EB_LEFT] = NULL;
397 goto delete_unlink;
398 }
399
400 /* To release our parent, we have to identify our sibling, and reparent
401 * it directly to/from the grand parent. Note that the sibling can
402 * either be a link or a leaf.
403 */
404
405 gpside = eb_gettag(parent->node_p);
406 gparent = eb_untag(parent->node_p, gpside);
407
408 gparent->b[gpside] = parent->branches.b[!pside];
409 sibtype = eb_gettag(gparent->b[gpside]);
410
411 if (sibtype == EB_LEAF) {
412 eb_root_to_node(eb_untag(gparent->b[gpside], EB_LEAF))->leaf_p =
413 eb_dotag(gparent, gpside);
414 } else {
415 eb_root_to_node(eb_untag(gparent->b[gpside], EB_NODE))->node_p =
416 eb_dotag(gparent, gpside);
417 }
418 /* Mark the parent unused. Note that we do not check if the parent is
419 * our own node, but that's not a problem because if it is, it will be
420 * marked unused at the same time, which we'll use below to know we can
421 * safely remove it.
422 */
423 parent->node_p = NULL;
424
425 /* The parent node has been detached, and is currently unused. It may
426 * belong to another node, so we cannot remove it that way. Also, our
427 * own node part might still be used. so we can use this spare node
428 * to replace ours if needed.
429 */
430
431 /* If our link part is unused, we can safely exit now */
432 if (!node->node_p)
433 goto delete_unlink;
434
435 /* From now on, <node> and <parent> are necessarily different, and the
436 * <node>'s node part is in use. By definition, <parent> is at least
Willy Tarreauef8d0dc2017-11-05 18:06:22 +0100437 * below <node>, so keeping its key for the bit string is OK. However
438 * its scope must be enlarged to cover the new branch it absorbs.
Willy Tarreauca308392017-11-05 13:31:29 +0100439 */
440
441 parent->node_p = node->node_p;
442 parent->branches = node->branches;
443 parent->bit = node->bit;
Willy Tarreauca308392017-11-05 13:31:29 +0100444
445 /* We must now update the new node's parent... */
446 gpside = eb_gettag(parent->node_p);
447 gparent = eb_untag(parent->node_p, gpside);
448 gparent->b[gpside] = eb_dotag(&parent->branches, EB_NODE);
449
450 /* ... and its branches */
Willy Tarreaud19ec7d2017-11-13 16:16:09 +0100451 scope = 0;
Willy Tarreauca308392017-11-05 13:31:29 +0100452 for (pside = 0; pside <= 1; pside++) {
453 if (eb_gettag(parent->branches.b[pside]) == EB_NODE) {
454 eb_root_to_node(eb_untag(parent->branches.b[pside], EB_NODE))->node_p =
455 eb_dotag(&parent->branches, pside);
Willy Tarreaud19ec7d2017-11-13 16:16:09 +0100456 scope |= container_of(eb_untag(parent->branches.b[pside], EB_NODE), struct eb32sc_node, node.branches)->node_s;
Willy Tarreauca308392017-11-05 13:31:29 +0100457 } else {
458 eb_root_to_node(eb_untag(parent->branches.b[pside], EB_LEAF))->leaf_p =
459 eb_dotag(&parent->branches, pside);
Willy Tarreaud19ec7d2017-11-13 16:16:09 +0100460 scope |= container_of(eb_untag(parent->branches.b[pside], EB_LEAF), struct eb32sc_node, node.branches)->leaf_s;
Willy Tarreauca308392017-11-05 13:31:29 +0100461 }
462 }
Willy Tarreaud19ec7d2017-11-13 16:16:09 +0100463 container_of(parent, struct eb32sc_node, node)->node_s = scope;
464
Willy Tarreauca308392017-11-05 13:31:29 +0100465 delete_unlink:
466 /* Now the node has been completely unlinked */
467 node->leaf_p = NULL;
468 return; /* tree is not empty yet */
469}