blob: e7034343504158088bf84002bda1febbf98da15d [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 Tarreau318d0c22017-11-15 19:38:29 +010060 if (!(eb32->node_s & scope))
61 eb32->node_s |= scope;
Willy Tarreauca308392017-11-05 13:31:29 +010062 }
63
64 /* Here we have a leaf attached to (head)->b[EB_RGHT] */
65 if (head->bit < -1) {
66 /* A hole exists just before the leaf, we insert there */
67 new->bit = -1;
68 sub = container_of(eb_untag(head->branches.b[EB_RGHT], EB_LEAF),
69 struct eb_node, branches);
70 head->branches.b[EB_RGHT] = eb_dotag(&new->branches, EB_NODE);
71
72 new->node_p = sub->leaf_p;
73 new->leaf_p = new_rght;
74 sub->leaf_p = new_left;
75 new->branches.b[EB_LEFT] = eb_dotag(&sub->branches, EB_LEAF);
76 new->branches.b[EB_RGHT] = new_leaf;
Willy Tarreau5d19fd42017-11-05 14:06:50 +010077 eb32 = container_of(new, struct eb32sc_node, node);
78 eb32->node_s = container_of(sub, struct eb32sc_node, node)->leaf_s | scope;
79 return eb32;
Willy Tarreauca308392017-11-05 13:31:29 +010080 } else {
81 int side;
82 /* No hole was found before a leaf. We have to insert above
83 * <sub>. Note that we cannot be certain that <sub> is attached
84 * to the right of its parent, as this is only true if <sub>
85 * is inside the dup tree, not at the head.
86 */
87 new->bit = sub->bit - 1; /* install at the lowest level */
88 side = eb_gettag(sub->node_p);
89 head = container_of(eb_untag(sub->node_p, side), struct eb_node, branches);
90 head->branches.b[side] = eb_dotag(&new->branches, EB_NODE);
91
92 new->node_p = sub->node_p;
93 new->leaf_p = new_rght;
94 sub->node_p = new_left;
95 new->branches.b[EB_LEFT] = eb_dotag(&sub->branches, EB_NODE);
96 new->branches.b[EB_RGHT] = new_leaf;
Willy Tarreau5d19fd42017-11-05 14:06:50 +010097 eb32 = container_of(new, struct eb32sc_node, node);
98 eb32->node_s = container_of(sub, struct eb32sc_node, node)->node_s | scope;
99 return eb32;
Willy Tarreauca308392017-11-05 13:31:29 +0100100 }
101}
102
103/* Insert eb32sc_node <new> into subtree starting at node root <root>. Only
104 * new->key needs be set with the key. The eb32sc_node is returned. This
105 * implementation does NOT support unique trees.
106 */
107REGPRM2 struct eb32sc_node *eb32sc_insert(struct eb_root *root, struct eb32sc_node *new, unsigned long scope)
108{
109 struct eb32sc_node *old;
110 unsigned int side;
111 eb_troot_t *troot, **up_ptr;
112 u32 newkey; /* caching the key saves approximately one cycle */
113 eb_troot_t *new_left, *new_rght;
114 eb_troot_t *new_leaf;
115 int old_node_bit;
Willy Tarreau5d19fd42017-11-05 14:06:50 +0100116 unsigned long old_scope;
Willy Tarreauca308392017-11-05 13:31:29 +0100117
118 side = EB_LEFT;
119 troot = root->b[EB_LEFT];
120 if (unlikely(troot == NULL)) {
121 /* Tree is empty, insert the leaf part below the left branch */
122 root->b[EB_LEFT] = eb_dotag(&new->node.branches, EB_LEAF);
123 new->node.leaf_p = eb_dotag(root, EB_LEFT);
124 new->node.node_p = NULL; /* node part unused */
Willy Tarreau5d19fd42017-11-05 14:06:50 +0100125 new->node_s = scope;
126 new->leaf_s = scope;
Willy Tarreauca308392017-11-05 13:31:29 +0100127 return new;
128 }
129
130 /* The tree descent is fairly easy :
131 * - first, check if we have reached a leaf node
132 * - second, check if we have gone too far
133 * - third, reiterate
134 * Everywhere, we use <new> for the node node we are inserting, <root>
135 * for the node we attach it to, and <old> for the node we are
136 * displacing below <new>. <troot> will always point to the future node
137 * (tagged with its type). <side> carries the side the node <new> is
138 * attached to below its parent, which is also where previous node
139 * was attached. <newkey> carries the key being inserted.
140 */
141 newkey = new->key;
142
143 while (1) {
144 if (eb_gettag(troot) == EB_LEAF) {
145 /* insert above a leaf */
146 old = container_of(eb_untag(troot, EB_LEAF),
147 struct eb32sc_node, node.branches);
148 new->node.node_p = old->node.leaf_p;
149 up_ptr = &old->node.leaf_p;
Willy Tarreau5d19fd42017-11-05 14:06:50 +0100150 old_scope = old->leaf_s;
Willy Tarreauca308392017-11-05 13:31:29 +0100151 break;
152 }
153
154 /* OK we're walking down this link */
155 old = container_of(eb_untag(troot, EB_NODE),
156 struct eb32sc_node, node.branches);
157 old_node_bit = old->node.bit;
158
Willy Tarreau318d0c22017-11-15 19:38:29 +0100159 /* our new node will be found through this one, we must mark it */
160 if ((old->node_s | scope) != old->node_s)
161 old->node_s |= scope;
162
Willy Tarreauca308392017-11-05 13:31:29 +0100163 /* Stop going down when we don't have common bits anymore. We
164 * also stop in front of a duplicates tree because it means we
165 * have to insert above.
166 */
167
168 if ((old_node_bit < 0) || /* we're above a duplicate tree, stop here */
169 (((new->key ^ old->key) >> old_node_bit) >= EB_NODE_BRANCHES)) {
170 /* The tree did not contain the key, so we insert <new> before the node
171 * <old>, and set ->bit to designate the lowest bit position in <new>
172 * which applies to ->branches.b[].
173 */
174 new->node.node_p = old->node.node_p;
175 up_ptr = &old->node.node_p;
Willy Tarreau5d19fd42017-11-05 14:06:50 +0100176 old_scope = old->node_s;
Willy Tarreauca308392017-11-05 13:31:29 +0100177 break;
178 }
179
180 /* walk down */
181 root = &old->node.branches;
182 side = (newkey >> old_node_bit) & EB_NODE_BRANCH_MASK;
183 troot = root->b[side];
184 }
185
186 new_left = eb_dotag(&new->node.branches, EB_LEFT);
187 new_rght = eb_dotag(&new->node.branches, EB_RGHT);
188 new_leaf = eb_dotag(&new->node.branches, EB_LEAF);
189
190 /* We need the common higher bits between new->key and old->key.
191 * What differences are there between new->key and the node here ?
192 * NOTE that bit(new) is always < bit(root) because highest
193 * bit of new->key and old->key are identical here (otherwise they
194 * would sit on different branches).
195 */
196
197 // note that if EB_NODE_BITS > 1, we should check that it's still >= 0
198 new->node.bit = flsnz(new->key ^ old->key) - EB_NODE_BITS;
Willy Tarreau5d19fd42017-11-05 14:06:50 +0100199 new->leaf_s = scope;
200 new->node_s = old_scope | scope;
Willy Tarreauca308392017-11-05 13:31:29 +0100201
202 if (new->key == old->key) {
203 new->node.bit = -1; /* mark as new dup tree, just in case */
204
205 if (eb_gettag(troot) != EB_LEAF) {
206 /* there was already a dup tree below */
207 return eb32sc_insert_dup(&old->node, &new->node, scope);
208 }
209 /* otherwise fall through */
210 }
211
212 if (new->key >= old->key) {
213 new->node.branches.b[EB_LEFT] = troot;
214 new->node.branches.b[EB_RGHT] = new_leaf;
215 new->node.leaf_p = new_rght;
216 *up_ptr = new_left;
217 }
218 else {
219 new->node.branches.b[EB_LEFT] = new_leaf;
220 new->node.branches.b[EB_RGHT] = troot;
221 new->node.leaf_p = new_left;
222 *up_ptr = new_rght;
223 }
224
225 /* Ok, now we are inserting <new> between <root> and <old>. <old>'s
226 * parent is already set to <new>, and the <root>'s branch is still in
227 * <side>. Update the root's leaf till we have it. Note that we can also
228 * find the side by checking the side of new->node.node_p.
229 */
230
231 root->b[side] = eb_dotag(&new->node.branches, EB_NODE);
232 return new;
233}
234
235/*
236 * Find the first occurrence of the lowest key in the tree <root>, which is
237 * equal to or greater than <x>. NULL is returned is no key matches.
238 */
239REGPRM2 struct eb32sc_node *eb32sc_lookup_ge(struct eb_root *root, u32 x, unsigned long scope)
240{
241 struct eb32sc_node *node;
242 eb_troot_t *troot;
243
244 troot = root->b[EB_LEFT];
245 if (unlikely(troot == NULL))
246 return NULL;
247
248 while (1) {
249 if ((eb_gettag(troot) == EB_LEAF)) {
250 /* We reached a leaf, which means that the whole upper
251 * parts were common. We will return either the current
252 * node or its next one if the former is too small.
253 */
254 node = container_of(eb_untag(troot, EB_LEAF),
255 struct eb32sc_node, node.branches);
Willy Tarreaud1d55ac2017-11-05 14:33:01 +0100256 if ((node->leaf_s & scope) && node->key >= x)
Willy Tarreauca308392017-11-05 13:31:29 +0100257 return node;
258 /* return next */
259 troot = node->node.leaf_p;
260 break;
261 }
262 node = container_of(eb_untag(troot, EB_NODE),
263 struct eb32sc_node, node.branches);
264
265 if (node->node.bit < 0) {
266 /* We're at the top of a dup tree. Either we got a
267 * matching value and we return the leftmost node, or
268 * we don't and we skip the whole subtree to return the
269 * next node after the subtree. Note that since we're
270 * at the top of the dup tree, we can simply return the
271 * next node without first trying to escape from the
272 * tree.
273 */
Willy Tarreaud1d55ac2017-11-05 14:33:01 +0100274 if ((node->node_s & scope) && node->key >= x)
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
281 if (((x ^ node->key) >> node->node.bit) >= EB_NODE_BRANCHES) {
282 /* No more common bits at all. Either this node is too
283 * large and we need to get its lowest value, or it is too
284 * small, and we need to get the next value.
285 */
Willy Tarreaud1d55ac2017-11-05 14:33:01 +0100286 if ((node->node_s & scope) && (node->key >> node->node.bit) > (x >> node->node.bit))
287 troot = eb_dotag(&node->node.branches, EB_LEFT);
288 else
289 troot = node->node.node_p;
Willy Tarreauca308392017-11-05 13:31:29 +0100290 break;
291 }
292 troot = node->node.branches.b[(x >> node->node.bit) & EB_NODE_BRANCH_MASK];
293 }
294
295 /* If we get here, it means we want to report next node after the
296 * current one which is not below. <troot> is already initialised
297 * to the parent's branches.
298 */
Willy Tarreaucfaa6e72017-11-13 19:17:54 +0100299 return eb32sc_next_with_parent(troot, scope);
Willy Tarreauca308392017-11-05 13:31:29 +0100300}
301
Willy Tarreau8878b6c2017-11-05 21:23:21 +0100302/*
303 * Find the first occurrence of the lowest key in the tree <root> which is
304 * equal to or greater than <x>, matching scope <scope>. If not found, it loops
305 * back to the beginning of the tree. NULL is returned is no key matches.
306 */
307REGPRM2 struct eb32sc_node *eb32sc_lookup_ge_or_first(struct eb_root *root, u32 x, unsigned long scope)
308{
309 struct eb32sc_node *eb32;
310 eb_troot_t *troot;
Willy Tarreau8878b6c2017-11-05 21:23:21 +0100311
312 troot = root->b[EB_LEFT];
313 if (unlikely(troot == NULL))
314 return NULL;
315
316 while (1) {
317 if ((eb_gettag(troot) == EB_LEAF)) {
318 /* We reached a leaf, which means that the whole upper
319 * parts were common. We will return either the current
320 * node or its next one if the former is too small.
321 */
322 eb32 = container_of(eb_untag(troot, EB_LEAF),
323 struct eb32sc_node, node.branches);
324 if ((eb32->leaf_s & scope) && eb32->key >= x)
325 return eb32;
326 /* return next */
327 troot = eb32->node.leaf_p;
328 break;
329 }
330 eb32 = container_of(eb_untag(troot, EB_NODE),
331 struct eb32sc_node, node.branches);
332
333 if (eb32->node.bit < 0) {
334 /* We're at the top of a dup tree. Either we got a
335 * matching value and we return the leftmost node, or
336 * we don't and we skip the whole subtree to return the
337 * next node after the subtree. Note that since we're
338 * at the top of the dup tree, we can simply return the
339 * next node without first trying to escape from the
340 * tree.
341 */
342 if ((eb32->node_s & scope) && eb32->key >= x)
343 troot = eb_dotag(&eb32->node.branches, EB_LEFT);
344 else
345 troot = eb32->node.node_p;
346 break;
347 }
348
349 if (((x ^ eb32->key) >> eb32->node.bit) >= EB_NODE_BRANCHES) {
350 /* No more common bits at all. Either this node is too
351 * large and we need to get its lowest value, or it is too
352 * small, and we need to get the next value.
353 */
354 if ((eb32->node_s & scope) && (eb32->key >> eb32->node.bit) > (x >> eb32->node.bit))
355 troot = eb_dotag(&eb32->node.branches, EB_LEFT);
356 else
357 troot = eb32->node.node_p;
358 break;
359 }
360 troot = eb32->node.branches.b[(x >> eb32->node.bit) & EB_NODE_BRANCH_MASK];
361 }
362
363 /* If we get here, it means we want to report next node after the
364 * current one which is not below. <troot> is already initialised
365 * to the parent's branches.
366 */
Willy Tarreaucfaa6e72017-11-13 19:17:54 +0100367 eb32 = eb32sc_next_with_parent(troot, scope);
Willy Tarreau8878b6c2017-11-05 21:23:21 +0100368 if (!eb32)
369 eb32 = eb32sc_walk_down_left(root->b[EB_LEFT], scope);
370
371 return eb32;
372}
373
Willy Tarreauca308392017-11-05 13:31:29 +0100374/* Removes a leaf node from the tree if it was still in it. Marks the node
375 * as unlinked.
376 */
377void eb32sc_delete(struct eb32sc_node *eb32)
378{
379 struct eb_node *node = &eb32->node;
380 unsigned int pside, gpside, sibtype;
381 struct eb_node *parent;
382 struct eb_root *gparent;
Willy Tarreaud19ec7d2017-11-13 16:16:09 +0100383 unsigned long scope;
Willy Tarreauca308392017-11-05 13:31:29 +0100384
385 if (!node->leaf_p)
386 return;
387
388 /* we need the parent, our side, and the grand parent */
389 pside = eb_gettag(node->leaf_p);
390 parent = eb_root_to_node(eb_untag(node->leaf_p, pside));
391
392 /* We likely have to release the parent link, unless it's the root,
393 * in which case we only set our branch to NULL. Note that we can
394 * only be attached to the root by its left branch.
395 */
396
397 if (eb_clrtag(parent->branches.b[EB_RGHT]) == NULL) {
398 /* we're just below the root, it's trivial. */
399 parent->branches.b[EB_LEFT] = NULL;
400 goto delete_unlink;
401 }
402
403 /* To release our parent, we have to identify our sibling, and reparent
404 * it directly to/from the grand parent. Note that the sibling can
405 * either be a link or a leaf.
406 */
407
408 gpside = eb_gettag(parent->node_p);
409 gparent = eb_untag(parent->node_p, gpside);
410
411 gparent->b[gpside] = parent->branches.b[!pside];
412 sibtype = eb_gettag(gparent->b[gpside]);
413
414 if (sibtype == EB_LEAF) {
415 eb_root_to_node(eb_untag(gparent->b[gpside], EB_LEAF))->leaf_p =
416 eb_dotag(gparent, gpside);
417 } else {
418 eb_root_to_node(eb_untag(gparent->b[gpside], EB_NODE))->node_p =
419 eb_dotag(gparent, gpside);
420 }
421 /* Mark the parent unused. Note that we do not check if the parent is
422 * our own node, but that's not a problem because if it is, it will be
423 * marked unused at the same time, which we'll use below to know we can
424 * safely remove it.
425 */
426 parent->node_p = NULL;
427
428 /* The parent node has been detached, and is currently unused. It may
429 * belong to another node, so we cannot remove it that way. Also, our
430 * own node part might still be used. so we can use this spare node
431 * to replace ours if needed.
432 */
433
434 /* If our link part is unused, we can safely exit now */
435 if (!node->node_p)
436 goto delete_unlink;
437
438 /* From now on, <node> and <parent> are necessarily different, and the
439 * <node>'s node part is in use. By definition, <parent> is at least
Willy Tarreauef8d0dc2017-11-05 18:06:22 +0100440 * below <node>, so keeping its key for the bit string is OK. However
441 * its scope must be enlarged to cover the new branch it absorbs.
Willy Tarreauca308392017-11-05 13:31:29 +0100442 */
443
444 parent->node_p = node->node_p;
445 parent->branches = node->branches;
446 parent->bit = node->bit;
Willy Tarreauca308392017-11-05 13:31:29 +0100447
448 /* We must now update the new node's parent... */
449 gpside = eb_gettag(parent->node_p);
450 gparent = eb_untag(parent->node_p, gpside);
451 gparent->b[gpside] = eb_dotag(&parent->branches, EB_NODE);
452
453 /* ... and its branches */
Willy Tarreaud19ec7d2017-11-13 16:16:09 +0100454 scope = 0;
Willy Tarreauca308392017-11-05 13:31:29 +0100455 for (pside = 0; pside <= 1; pside++) {
456 if (eb_gettag(parent->branches.b[pside]) == EB_NODE) {
457 eb_root_to_node(eb_untag(parent->branches.b[pside], EB_NODE))->node_p =
458 eb_dotag(&parent->branches, pside);
Willy Tarreaud19ec7d2017-11-13 16:16:09 +0100459 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 +0100460 } else {
461 eb_root_to_node(eb_untag(parent->branches.b[pside], EB_LEAF))->leaf_p =
462 eb_dotag(&parent->branches, pside);
Willy Tarreaud19ec7d2017-11-13 16:16:09 +0100463 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 +0100464 }
465 }
Willy Tarreaud19ec7d2017-11-13 16:16:09 +0100466 container_of(parent, struct eb32sc_node, node)->node_s = scope;
467
Willy Tarreauca308392017-11-05 13:31:29 +0100468 delete_unlink:
469 /* Now the node has been completely unlinked */
470 node->leaf_p = NULL;
471 return; /* tree is not empty yet */
472}