blob: 1bb7ca739b6a432d5d671089ffdf2855f97aeaf4 [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{
31 struct eb_node *head = sub;
32 eb_troot_t *new_left = eb_dotag(&new->branches, EB_LEFT);
33 eb_troot_t *new_rght = eb_dotag(&new->branches, EB_RGHT);
34 eb_troot_t *new_leaf = eb_dotag(&new->branches, EB_LEAF);
35
36 /* first, identify the deepest hole on the right branch */
37 while (eb_gettag(head->branches.b[EB_RGHT]) != EB_LEAF) {
38 struct eb_node *last = head;
39 head = container_of(eb_untag(head->branches.b[EB_RGHT], EB_NODE),
40 struct eb_node, branches);
41 if (head->bit > last->bit + 1)
42 sub = head; /* there's a hole here */
43 }
44
45 /* Here we have a leaf attached to (head)->b[EB_RGHT] */
46 if (head->bit < -1) {
47 /* A hole exists just before the leaf, we insert there */
48 new->bit = -1;
49 sub = container_of(eb_untag(head->branches.b[EB_RGHT], EB_LEAF),
50 struct eb_node, branches);
51 head->branches.b[EB_RGHT] = eb_dotag(&new->branches, EB_NODE);
52
53 new->node_p = sub->leaf_p;
54 new->leaf_p = new_rght;
55 sub->leaf_p = new_left;
56 new->branches.b[EB_LEFT] = eb_dotag(&sub->branches, EB_LEAF);
57 new->branches.b[EB_RGHT] = new_leaf;
58 return container_of(new, struct eb32sc_node, node);
59 } else {
60 int side;
61 /* No hole was found before a leaf. We have to insert above
62 * <sub>. Note that we cannot be certain that <sub> is attached
63 * to the right of its parent, as this is only true if <sub>
64 * is inside the dup tree, not at the head.
65 */
66 new->bit = sub->bit - 1; /* install at the lowest level */
67 side = eb_gettag(sub->node_p);
68 head = container_of(eb_untag(sub->node_p, side), struct eb_node, branches);
69 head->branches.b[side] = eb_dotag(&new->branches, EB_NODE);
70
71 new->node_p = sub->node_p;
72 new->leaf_p = new_rght;
73 sub->node_p = new_left;
74 new->branches.b[EB_LEFT] = eb_dotag(&sub->branches, EB_NODE);
75 new->branches.b[EB_RGHT] = new_leaf;
76 return container_of(new, struct eb32sc_node, node);
77 }
78}
79
80/* Insert eb32sc_node <new> into subtree starting at node root <root>. Only
81 * new->key needs be set with the key. The eb32sc_node is returned. This
82 * implementation does NOT support unique trees.
83 */
84REGPRM2 struct eb32sc_node *eb32sc_insert(struct eb_root *root, struct eb32sc_node *new, unsigned long scope)
85{
86 struct eb32sc_node *old;
87 unsigned int side;
88 eb_troot_t *troot, **up_ptr;
89 u32 newkey; /* caching the key saves approximately one cycle */
90 eb_troot_t *new_left, *new_rght;
91 eb_troot_t *new_leaf;
92 int old_node_bit;
93
94 side = EB_LEFT;
95 troot = root->b[EB_LEFT];
96 if (unlikely(troot == NULL)) {
97 /* Tree is empty, insert the leaf part below the left branch */
98 root->b[EB_LEFT] = eb_dotag(&new->node.branches, EB_LEAF);
99 new->node.leaf_p = eb_dotag(root, EB_LEFT);
100 new->node.node_p = NULL; /* node part unused */
101 return new;
102 }
103
104 /* The tree descent is fairly easy :
105 * - first, check if we have reached a leaf node
106 * - second, check if we have gone too far
107 * - third, reiterate
108 * Everywhere, we use <new> for the node node we are inserting, <root>
109 * for the node we attach it to, and <old> for the node we are
110 * displacing below <new>. <troot> will always point to the future node
111 * (tagged with its type). <side> carries the side the node <new> is
112 * attached to below its parent, which is also where previous node
113 * was attached. <newkey> carries the key being inserted.
114 */
115 newkey = new->key;
116
117 while (1) {
118 if (eb_gettag(troot) == EB_LEAF) {
119 /* insert above a leaf */
120 old = container_of(eb_untag(troot, EB_LEAF),
121 struct eb32sc_node, node.branches);
122 new->node.node_p = old->node.leaf_p;
123 up_ptr = &old->node.leaf_p;
124 break;
125 }
126
127 /* OK we're walking down this link */
128 old = container_of(eb_untag(troot, EB_NODE),
129 struct eb32sc_node, node.branches);
130 old_node_bit = old->node.bit;
131
132 /* Stop going down when we don't have common bits anymore. We
133 * also stop in front of a duplicates tree because it means we
134 * have to insert above.
135 */
136
137 if ((old_node_bit < 0) || /* we're above a duplicate tree, stop here */
138 (((new->key ^ old->key) >> old_node_bit) >= EB_NODE_BRANCHES)) {
139 /* The tree did not contain the key, so we insert <new> before the node
140 * <old>, and set ->bit to designate the lowest bit position in <new>
141 * which applies to ->branches.b[].
142 */
143 new->node.node_p = old->node.node_p;
144 up_ptr = &old->node.node_p;
145 break;
146 }
147
148 /* walk down */
149 root = &old->node.branches;
150 side = (newkey >> old_node_bit) & EB_NODE_BRANCH_MASK;
151 troot = root->b[side];
152 }
153
154 new_left = eb_dotag(&new->node.branches, EB_LEFT);
155 new_rght = eb_dotag(&new->node.branches, EB_RGHT);
156 new_leaf = eb_dotag(&new->node.branches, EB_LEAF);
157
158 /* We need the common higher bits between new->key and old->key.
159 * What differences are there between new->key and the node here ?
160 * NOTE that bit(new) is always < bit(root) because highest
161 * bit of new->key and old->key are identical here (otherwise they
162 * would sit on different branches).
163 */
164
165 // note that if EB_NODE_BITS > 1, we should check that it's still >= 0
166 new->node.bit = flsnz(new->key ^ old->key) - EB_NODE_BITS;
167
168 if (new->key == old->key) {
169 new->node.bit = -1; /* mark as new dup tree, just in case */
170
171 if (eb_gettag(troot) != EB_LEAF) {
172 /* there was already a dup tree below */
173 return eb32sc_insert_dup(&old->node, &new->node, scope);
174 }
175 /* otherwise fall through */
176 }
177
178 if (new->key >= old->key) {
179 new->node.branches.b[EB_LEFT] = troot;
180 new->node.branches.b[EB_RGHT] = new_leaf;
181 new->node.leaf_p = new_rght;
182 *up_ptr = new_left;
183 }
184 else {
185 new->node.branches.b[EB_LEFT] = new_leaf;
186 new->node.branches.b[EB_RGHT] = troot;
187 new->node.leaf_p = new_left;
188 *up_ptr = new_rght;
189 }
190
191 /* Ok, now we are inserting <new> between <root> and <old>. <old>'s
192 * parent is already set to <new>, and the <root>'s branch is still in
193 * <side>. Update the root's leaf till we have it. Note that we can also
194 * find the side by checking the side of new->node.node_p.
195 */
196
197 root->b[side] = eb_dotag(&new->node.branches, EB_NODE);
198 return new;
199}
200
201/*
202 * Find the first occurrence of the lowest key in the tree <root>, which is
203 * equal to or greater than <x>. NULL is returned is no key matches.
204 */
205REGPRM2 struct eb32sc_node *eb32sc_lookup_ge(struct eb_root *root, u32 x, unsigned long scope)
206{
207 struct eb32sc_node *node;
208 eb_troot_t *troot;
209
210 troot = root->b[EB_LEFT];
211 if (unlikely(troot == NULL))
212 return NULL;
213
214 while (1) {
215 if ((eb_gettag(troot) == EB_LEAF)) {
216 /* We reached a leaf, which means that the whole upper
217 * parts were common. We will return either the current
218 * node or its next one if the former is too small.
219 */
220 node = container_of(eb_untag(troot, EB_LEAF),
221 struct eb32sc_node, node.branches);
222 if (node->key >= x)
223 return node;
224 /* return next */
225 troot = node->node.leaf_p;
226 break;
227 }
228 node = container_of(eb_untag(troot, EB_NODE),
229 struct eb32sc_node, node.branches);
230
231 if (node->node.bit < 0) {
232 /* We're at the top of a dup tree. Either we got a
233 * matching value and we return the leftmost node, or
234 * we don't and we skip the whole subtree to return the
235 * next node after the subtree. Note that since we're
236 * at the top of the dup tree, we can simply return the
237 * next node without first trying to escape from the
238 * tree.
239 */
240 if (node->key >= x) {
241 troot = node->node.branches.b[EB_LEFT];
242 while (eb_gettag(troot) != EB_LEAF)
243 troot = (eb_untag(troot, EB_NODE))->b[EB_LEFT];
244 return container_of(eb_untag(troot, EB_LEAF),
245 struct eb32sc_node, node.branches);
246 }
247 /* return next */
248 troot = node->node.node_p;
249 break;
250 }
251
252 if (((x ^ node->key) >> node->node.bit) >= EB_NODE_BRANCHES) {
253 /* No more common bits at all. Either this node is too
254 * large and we need to get its lowest value, or it is too
255 * small, and we need to get the next value.
256 */
257 if ((node->key >> node->node.bit) > (x >> node->node.bit)) {
258 troot = node->node.branches.b[EB_LEFT];
259 return eb32sc_walk_down(troot, EB_LEFT, scope);
260 }
261
262 /* Further values will be too low here, so return the next
263 * unique node (if it exists).
264 */
265 troot = node->node.node_p;
266 break;
267 }
268 troot = node->node.branches.b[(x >> node->node.bit) & EB_NODE_BRANCH_MASK];
269 }
270
271 /* If we get here, it means we want to report next node after the
272 * current one which is not below. <troot> is already initialised
273 * to the parent's branches.
274 */
275 while (eb_gettag(troot) != EB_LEFT)
276 /* Walking up from right branch, so we cannot be below root */
277 troot = (eb_root_to_node(eb_untag(troot, EB_RGHT)))->node_p;
278
279 /* Note that <troot> cannot be NULL at this stage */
280 troot = (eb_untag(troot, EB_LEFT))->b[EB_RGHT];
281 if (eb_clrtag(troot) == NULL)
282 return NULL;
283
284 return eb32sc_walk_down(troot, EB_LEFT, scope);
285}
286
287/* Removes a leaf node from the tree if it was still in it. Marks the node
288 * as unlinked.
289 */
290void eb32sc_delete(struct eb32sc_node *eb32)
291{
292 struct eb_node *node = &eb32->node;
293 unsigned int pside, gpside, sibtype;
294 struct eb_node *parent;
295 struct eb_root *gparent;
296
297 if (!node->leaf_p)
298 return;
299
300 /* we need the parent, our side, and the grand parent */
301 pside = eb_gettag(node->leaf_p);
302 parent = eb_root_to_node(eb_untag(node->leaf_p, pside));
303
304 /* We likely have to release the parent link, unless it's the root,
305 * in which case we only set our branch to NULL. Note that we can
306 * only be attached to the root by its left branch.
307 */
308
309 if (eb_clrtag(parent->branches.b[EB_RGHT]) == NULL) {
310 /* we're just below the root, it's trivial. */
311 parent->branches.b[EB_LEFT] = NULL;
312 goto delete_unlink;
313 }
314
315 /* To release our parent, we have to identify our sibling, and reparent
316 * it directly to/from the grand parent. Note that the sibling can
317 * either be a link or a leaf.
318 */
319
320 gpside = eb_gettag(parent->node_p);
321 gparent = eb_untag(parent->node_p, gpside);
322
323 gparent->b[gpside] = parent->branches.b[!pside];
324 sibtype = eb_gettag(gparent->b[gpside]);
325
326 if (sibtype == EB_LEAF) {
327 eb_root_to_node(eb_untag(gparent->b[gpside], EB_LEAF))->leaf_p =
328 eb_dotag(gparent, gpside);
329 } else {
330 eb_root_to_node(eb_untag(gparent->b[gpside], EB_NODE))->node_p =
331 eb_dotag(gparent, gpside);
332 }
333 /* Mark the parent unused. Note that we do not check if the parent is
334 * our own node, but that's not a problem because if it is, it will be
335 * marked unused at the same time, which we'll use below to know we can
336 * safely remove it.
337 */
338 parent->node_p = NULL;
339
340 /* The parent node has been detached, and is currently unused. It may
341 * belong to another node, so we cannot remove it that way. Also, our
342 * own node part might still be used. so we can use this spare node
343 * to replace ours if needed.
344 */
345
346 /* If our link part is unused, we can safely exit now */
347 if (!node->node_p)
348 goto delete_unlink;
349
350 /* From now on, <node> and <parent> are necessarily different, and the
351 * <node>'s node part is in use. By definition, <parent> is at least
352 * below <node>, so keeping its key for the bit string is OK.
353 */
354
355 parent->node_p = node->node_p;
356 parent->branches = node->branches;
357 parent->bit = node->bit;
358
359 /* We must now update the new node's parent... */
360 gpside = eb_gettag(parent->node_p);
361 gparent = eb_untag(parent->node_p, gpside);
362 gparent->b[gpside] = eb_dotag(&parent->branches, EB_NODE);
363
364 /* ... and its branches */
365 for (pside = 0; pside <= 1; pside++) {
366 if (eb_gettag(parent->branches.b[pside]) == EB_NODE) {
367 eb_root_to_node(eb_untag(parent->branches.b[pside], EB_NODE))->node_p =
368 eb_dotag(&parent->branches, pside);
369 } else {
370 eb_root_to_node(eb_untag(parent->branches.b[pside], EB_LEAF))->leaf_p =
371 eb_dotag(&parent->branches, pside);
372 }
373 }
374 delete_unlink:
375 /* Now the node has been completely unlinked */
376 node->leaf_p = NULL;
377 return; /* tree is not empty yet */
378}