blob: 1c03fc1ed9e7b29f47f06d08e313d44fa97c4397 [file] [log] [blame]
Willy Tarreauc2186022009-10-26 19:48:54 +01001/*
2 * Elastic Binary Trees - macros and structures for operations on 32bit nodes.
Willy Tarreauf3bfede2011-07-25 11:38:17 +02003 * Version 6.0.6
4 * (C) 2002-2011 - Willy Tarreau <w@1wt.eu>
Willy Tarreauc2186022009-10-26 19:48:54 +01005 *
Willy Tarreauf3bfede2011-07-25 11:38:17 +02006 * 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.
Willy Tarreauc2186022009-10-26 19:48:54 +010010 *
Willy Tarreauf3bfede2011-07-25 11:38:17 +020011 * This library is distributed in the hope that it will be useful,
Willy Tarreauc2186022009-10-26 19:48:54 +010012 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Willy Tarreauf3bfede2011-07-25 11:38:17 +020013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
Willy Tarreauc2186022009-10-26 19:48:54 +010015 *
Willy Tarreauf3bfede2011-07-25 11:38:17 +020016 * 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
Willy Tarreauc2186022009-10-26 19:48:54 +010019 */
20
21#ifndef _EB32TREE_H
22#define _EB32TREE_H
23
24#include "ebtree.h"
25
26
27/* Return the structure of type <type> whose member <member> points to <ptr> */
28#define eb32_entry(ptr, type, member) container_of(ptr, type, member)
29
Willy Tarreauc2186022009-10-26 19:48:54 +010030/*
31 * Exported functions and macros.
32 * Many of them are always inlined because they are extremely small, and
33 * are generally called at most once or twice in a program.
34 */
35
36/* Return leftmost node in the tree, or NULL if none */
37static inline struct eb32_node *eb32_first(struct eb_root *root)
38{
39 return eb32_entry(eb_first(root), struct eb32_node, node);
40}
41
42/* Return rightmost node in the tree, or NULL if none */
43static inline struct eb32_node *eb32_last(struct eb_root *root)
44{
45 return eb32_entry(eb_last(root), struct eb32_node, node);
46}
47
48/* Return next node in the tree, or NULL if none */
49static inline struct eb32_node *eb32_next(struct eb32_node *eb32)
50{
51 return eb32_entry(eb_next(&eb32->node), struct eb32_node, node);
52}
53
54/* Return previous node in the tree, or NULL if none */
55static inline struct eb32_node *eb32_prev(struct eb32_node *eb32)
56{
57 return eb32_entry(eb_prev(&eb32->node), struct eb32_node, node);
58}
59
Willy Tarreau2b570202013-05-07 15:58:28 +020060/* Return next leaf node within a duplicate sub-tree, or NULL if none. */
61static inline struct eb32_node *eb32_next_dup(struct eb32_node *eb32)
62{
63 return eb32_entry(eb_next_dup(&eb32->node), struct eb32_node, node);
64}
65
66/* Return previous leaf node within a duplicate sub-tree, or NULL if none. */
67static inline struct eb32_node *eb32_prev_dup(struct eb32_node *eb32)
68{
69 return eb32_entry(eb_prev_dup(&eb32->node), struct eb32_node, node);
70}
71
Willy Tarreauc2186022009-10-26 19:48:54 +010072/* Return next node in the tree, skipping duplicates, or NULL if none */
73static inline struct eb32_node *eb32_next_unique(struct eb32_node *eb32)
74{
75 return eb32_entry(eb_next_unique(&eb32->node), struct eb32_node, node);
76}
77
78/* Return previous node in the tree, skipping duplicates, or NULL if none */
79static inline struct eb32_node *eb32_prev_unique(struct eb32_node *eb32)
80{
81 return eb32_entry(eb_prev_unique(&eb32->node), struct eb32_node, node);
82}
83
84/* Delete node from the tree if it was linked in. Mark the node unused. Note
85 * that this function relies on a non-inlined generic function: eb_delete.
86 */
87static inline void eb32_delete(struct eb32_node *eb32)
88{
89 eb_delete(&eb32->node);
90}
91
92/*
93 * The following functions are not inlined by default. They are declared
94 * in eb32tree.c, which simply relies on their inline version.
95 */
Willy Tarreau03e78532020-02-25 07:38:05 +010096struct eb32_node *eb32_lookup(struct eb_root *root, u32 x);
97struct eb32_node *eb32i_lookup(struct eb_root *root, s32 x);
98struct eb32_node *eb32_lookup_le(struct eb_root *root, u32 x);
99struct eb32_node *eb32_lookup_ge(struct eb_root *root, u32 x);
100struct eb32_node *eb32_insert(struct eb_root *root, struct eb32_node *new);
101struct eb32_node *eb32i_insert(struct eb_root *root, struct eb32_node *new);
Willy Tarreauc2186022009-10-26 19:48:54 +0100102
103/*
104 * The following functions are less likely to be used directly, because their
105 * code is larger. The non-inlined version is preferred.
106 */
107
108/* Delete node from the tree if it was linked in. Mark the node unused. */
109static forceinline void __eb32_delete(struct eb32_node *eb32)
110{
111 __eb_delete(&eb32->node);
112}
113
114/*
Joseph Herlant7c16c0e2018-11-13 19:55:57 -0800115 * Find the first occurrence of a key in the tree <root>. If none can be
Willy Tarreauc2186022009-10-26 19:48:54 +0100116 * found, return NULL.
117 */
118static forceinline struct eb32_node *__eb32_lookup(struct eb_root *root, u32 x)
119{
120 struct eb32_node *node;
121 eb_troot_t *troot;
122 u32 y;
Willy Tarreau3a932442010-05-09 19:29:23 +0200123 int node_bit;
Willy Tarreauc2186022009-10-26 19:48:54 +0100124
125 troot = root->b[EB_LEFT];
126 if (unlikely(troot == NULL))
127 return NULL;
128
129 while (1) {
130 if ((eb_gettag(troot) == EB_LEAF)) {
131 node = container_of(eb_untag(troot, EB_LEAF),
132 struct eb32_node, node.branches);
133 if (node->key == x)
134 return node;
135 else
136 return NULL;
137 }
138 node = container_of(eb_untag(troot, EB_NODE),
139 struct eb32_node, node.branches);
Willy Tarreau3a932442010-05-09 19:29:23 +0200140 node_bit = node->node.bit;
Willy Tarreauc2186022009-10-26 19:48:54 +0100141
142 y = node->key ^ x;
143 if (!y) {
144 /* Either we found the node which holds the key, or
145 * we have a dup tree. In the later case, we have to
146 * walk it down left to get the first entry.
147 */
Willy Tarreau3a932442010-05-09 19:29:23 +0200148 if (node_bit < 0) {
Willy Tarreauc2186022009-10-26 19:48:54 +0100149 troot = node->node.branches.b[EB_LEFT];
150 while (eb_gettag(troot) != EB_LEAF)
151 troot = (eb_untag(troot, EB_NODE))->b[EB_LEFT];
152 node = container_of(eb_untag(troot, EB_LEAF),
153 struct eb32_node, node.branches);
154 }
155 return node;
156 }
157
Willy Tarreau3a932442010-05-09 19:29:23 +0200158 if ((y >> node_bit) >= EB_NODE_BRANCHES)
Willy Tarreauc2186022009-10-26 19:48:54 +0100159 return NULL; /* no more common bits */
160
Willy Tarreau3a932442010-05-09 19:29:23 +0200161 troot = node->node.branches.b[(x >> node_bit) & EB_NODE_BRANCH_MASK];
Willy Tarreauc2186022009-10-26 19:48:54 +0100162 }
163}
164
165/*
Joseph Herlant7c16c0e2018-11-13 19:55:57 -0800166 * Find the first occurrence of a signed key in the tree <root>. If none can
Willy Tarreauc2186022009-10-26 19:48:54 +0100167 * be found, return NULL.
168 */
169static forceinline struct eb32_node *__eb32i_lookup(struct eb_root *root, s32 x)
170{
171 struct eb32_node *node;
172 eb_troot_t *troot;
173 u32 key = x ^ 0x80000000;
174 u32 y;
Willy Tarreau3a932442010-05-09 19:29:23 +0200175 int node_bit;
Willy Tarreauc2186022009-10-26 19:48:54 +0100176
177 troot = root->b[EB_LEFT];
178 if (unlikely(troot == NULL))
179 return NULL;
180
181 while (1) {
182 if ((eb_gettag(troot) == EB_LEAF)) {
183 node = container_of(eb_untag(troot, EB_LEAF),
184 struct eb32_node, node.branches);
Willy Tarreau3a932442010-05-09 19:29:23 +0200185 if (node->key == (u32)x)
Willy Tarreauc2186022009-10-26 19:48:54 +0100186 return node;
187 else
188 return NULL;
189 }
190 node = container_of(eb_untag(troot, EB_NODE),
191 struct eb32_node, node.branches);
Willy Tarreau3a932442010-05-09 19:29:23 +0200192 node_bit = node->node.bit;
Willy Tarreauc2186022009-10-26 19:48:54 +0100193
194 y = node->key ^ x;
195 if (!y) {
196 /* Either we found the node which holds the key, or
197 * we have a dup tree. In the later case, we have to
198 * walk it down left to get the first entry.
199 */
Willy Tarreau3a932442010-05-09 19:29:23 +0200200 if (node_bit < 0) {
Willy Tarreauc2186022009-10-26 19:48:54 +0100201 troot = node->node.branches.b[EB_LEFT];
202 while (eb_gettag(troot) != EB_LEAF)
203 troot = (eb_untag(troot, EB_NODE))->b[EB_LEFT];
204 node = container_of(eb_untag(troot, EB_LEAF),
205 struct eb32_node, node.branches);
206 }
207 return node;
208 }
209
Willy Tarreau3a932442010-05-09 19:29:23 +0200210 if ((y >> node_bit) >= EB_NODE_BRANCHES)
Willy Tarreauc2186022009-10-26 19:48:54 +0100211 return NULL; /* no more common bits */
212
Willy Tarreau3a932442010-05-09 19:29:23 +0200213 troot = node->node.branches.b[(key >> node_bit) & EB_NODE_BRANCH_MASK];
Willy Tarreauc2186022009-10-26 19:48:54 +0100214 }
215}
216
217/* Insert eb32_node <new> into subtree starting at node root <root>.
218 * Only new->key needs be set with the key. The eb32_node is returned.
219 * If root->b[EB_RGHT]==1, the tree may only contain unique keys.
220 */
221static forceinline struct eb32_node *
222__eb32_insert(struct eb_root *root, struct eb32_node *new) {
223 struct eb32_node *old;
224 unsigned int side;
Willy Tarreau3a932442010-05-09 19:29:23 +0200225 eb_troot_t *troot, **up_ptr;
Willy Tarreauc2186022009-10-26 19:48:54 +0100226 u32 newkey; /* caching the key saves approximately one cycle */
Willy Tarreau6258f7b2011-09-19 20:48:00 +0200227 eb_troot_t *root_right;
Willy Tarreau3a932442010-05-09 19:29:23 +0200228 eb_troot_t *new_left, *new_rght;
229 eb_troot_t *new_leaf;
230 int old_node_bit;
Willy Tarreauc2186022009-10-26 19:48:54 +0100231
232 side = EB_LEFT;
233 troot = root->b[EB_LEFT];
234 root_right = root->b[EB_RGHT];
235 if (unlikely(troot == NULL)) {
236 /* Tree is empty, insert the leaf part below the left branch */
237 root->b[EB_LEFT] = eb_dotag(&new->node.branches, EB_LEAF);
238 new->node.leaf_p = eb_dotag(root, EB_LEFT);
239 new->node.node_p = NULL; /* node part unused */
240 return new;
241 }
242
243 /* The tree descent is fairly easy :
244 * - first, check if we have reached a leaf node
245 * - second, check if we have gone too far
246 * - third, reiterate
247 * Everywhere, we use <new> for the node node we are inserting, <root>
248 * for the node we attach it to, and <old> for the node we are
249 * displacing below <new>. <troot> will always point to the future node
250 * (tagged with its type). <side> carries the side the node <new> is
251 * attached to below its parent, which is also where previous node
252 * was attached. <newkey> carries the key being inserted.
253 */
254 newkey = new->key;
255
256 while (1) {
Willy Tarreau3a932442010-05-09 19:29:23 +0200257 if (eb_gettag(troot) == EB_LEAF) {
258 /* insert above a leaf */
Willy Tarreauc2186022009-10-26 19:48:54 +0100259 old = container_of(eb_untag(troot, EB_LEAF),
260 struct eb32_node, node.branches);
Willy Tarreauc2186022009-10-26 19:48:54 +0100261 new->node.node_p = old->node.leaf_p;
Willy Tarreau3a932442010-05-09 19:29:23 +0200262 up_ptr = &old->node.leaf_p;
Willy Tarreauc2186022009-10-26 19:48:54 +0100263 break;
264 }
265
266 /* OK we're walking down this link */
267 old = container_of(eb_untag(troot, EB_NODE),
268 struct eb32_node, node.branches);
Willy Tarreau3a932442010-05-09 19:29:23 +0200269 old_node_bit = old->node.bit;
Willy Tarreauc2186022009-10-26 19:48:54 +0100270
271 /* Stop going down when we don't have common bits anymore. We
272 * also stop in front of a duplicates tree because it means we
273 * have to insert above.
274 */
275
Willy Tarreau3a932442010-05-09 19:29:23 +0200276 if ((old_node_bit < 0) || /* we're above a duplicate tree, stop here */
277 (((new->key ^ old->key) >> old_node_bit) >= EB_NODE_BRANCHES)) {
Willy Tarreauc2186022009-10-26 19:48:54 +0100278 /* The tree did not contain the key, so we insert <new> before the node
279 * <old>, and set ->bit to designate the lowest bit position in <new>
280 * which applies to ->branches.b[].
281 */
Willy Tarreauc2186022009-10-26 19:48:54 +0100282 new->node.node_p = old->node.node_p;
Willy Tarreau3a932442010-05-09 19:29:23 +0200283 up_ptr = &old->node.node_p;
Willy Tarreauc2186022009-10-26 19:48:54 +0100284 break;
285 }
286
287 /* walk down */
288 root = &old->node.branches;
Willy Tarreau3a932442010-05-09 19:29:23 +0200289 side = (newkey >> old_node_bit) & EB_NODE_BRANCH_MASK;
Willy Tarreauc2186022009-10-26 19:48:54 +0100290 troot = root->b[side];
291 }
292
Willy Tarreau3a932442010-05-09 19:29:23 +0200293 new_left = eb_dotag(&new->node.branches, EB_LEFT);
294 new_rght = eb_dotag(&new->node.branches, EB_RGHT);
295 new_leaf = eb_dotag(&new->node.branches, EB_LEAF);
Willy Tarreauc2186022009-10-26 19:48:54 +0100296
297 /* We need the common higher bits between new->key and old->key.
298 * What differences are there between new->key and the node here ?
299 * NOTE that bit(new) is always < bit(root) because highest
300 * bit of new->key and old->key are identical here (otherwise they
301 * would sit on different branches).
302 */
Willy Tarreau3a932442010-05-09 19:29:23 +0200303
Willy Tarreauc2186022009-10-26 19:48:54 +0100304 // note that if EB_NODE_BITS > 1, we should check that it's still >= 0
305 new->node.bit = flsnz(new->key ^ old->key) - EB_NODE_BITS;
Willy Tarreau3a932442010-05-09 19:29:23 +0200306
307 if (new->key == old->key) {
308 new->node.bit = -1; /* mark as new dup tree, just in case */
309
310 if (likely(eb_gettag(root_right))) {
311 /* we refuse to duplicate this key if the tree is
312 * tagged as containing only unique keys.
313 */
314 return old;
315 }
316
317 if (eb_gettag(troot) != EB_LEAF) {
318 /* there was already a dup tree below */
319 struct eb_node *ret;
320 ret = eb_insert_dup(&old->node, &new->node);
321 return container_of(ret, struct eb32_node, node);
322 }
323 /* otherwise fall through */
324 }
Willy Tarreauc2186022009-10-26 19:48:54 +0100325
Willy Tarreau3a932442010-05-09 19:29:23 +0200326 if (new->key >= old->key) {
327 new->node.branches.b[EB_LEFT] = troot;
328 new->node.branches.b[EB_RGHT] = new_leaf;
329 new->node.leaf_p = new_rght;
330 *up_ptr = new_left;
331 }
332 else {
333 new->node.branches.b[EB_LEFT] = new_leaf;
334 new->node.branches.b[EB_RGHT] = troot;
335 new->node.leaf_p = new_left;
336 *up_ptr = new_rght;
337 }
338
339 /* Ok, now we are inserting <new> between <root> and <old>. <old>'s
340 * parent is already set to <new>, and the <root>'s branch is still in
341 * <side>. Update the root's leaf till we have it. Note that we can also
342 * find the side by checking the side of new->node.node_p.
343 */
344
345 root->b[side] = eb_dotag(&new->node.branches, EB_NODE);
Willy Tarreauc2186022009-10-26 19:48:54 +0100346 return new;
347}
348
349/* Insert eb32_node <new> into subtree starting at node root <root>, using
350 * signed keys. Only new->key needs be set with the key. The eb32_node
351 * is returned. If root->b[EB_RGHT]==1, the tree may only contain unique keys.
352 */
353static forceinline struct eb32_node *
354__eb32i_insert(struct eb_root *root, struct eb32_node *new) {
355 struct eb32_node *old;
356 unsigned int side;
Willy Tarreau3a932442010-05-09 19:29:23 +0200357 eb_troot_t *troot, **up_ptr;
Willy Tarreauc2186022009-10-26 19:48:54 +0100358 int newkey; /* caching the key saves approximately one cycle */
Willy Tarreau6258f7b2011-09-19 20:48:00 +0200359 eb_troot_t *root_right;
Willy Tarreau3a932442010-05-09 19:29:23 +0200360 eb_troot_t *new_left, *new_rght;
361 eb_troot_t *new_leaf;
362 int old_node_bit;
Willy Tarreauc2186022009-10-26 19:48:54 +0100363
364 side = EB_LEFT;
365 troot = root->b[EB_LEFT];
366 root_right = root->b[EB_RGHT];
367 if (unlikely(troot == NULL)) {
368 /* Tree is empty, insert the leaf part below the left branch */
369 root->b[EB_LEFT] = eb_dotag(&new->node.branches, EB_LEAF);
370 new->node.leaf_p = eb_dotag(root, EB_LEFT);
371 new->node.node_p = NULL; /* node part unused */
372 return new;
373 }
374
375 /* The tree descent is fairly easy :
376 * - first, check if we have reached a leaf node
377 * - second, check if we have gone too far
378 * - third, reiterate
379 * Everywhere, we use <new> for the node node we are inserting, <root>
380 * for the node we attach it to, and <old> for the node we are
381 * displacing below <new>. <troot> will always point to the future node
382 * (tagged with its type). <side> carries the side the node <new> is
383 * attached to below its parent, which is also where previous node
384 * was attached. <newkey> carries a high bit shift of the key being
385 * inserted in order to have negative keys stored before positive
386 * ones.
387 */
388 newkey = new->key + 0x80000000;
389
390 while (1) {
Willy Tarreau3a932442010-05-09 19:29:23 +0200391 if (eb_gettag(troot) == EB_LEAF) {
Willy Tarreauc2186022009-10-26 19:48:54 +0100392 old = container_of(eb_untag(troot, EB_LEAF),
393 struct eb32_node, node.branches);
Willy Tarreauc2186022009-10-26 19:48:54 +0100394 new->node.node_p = old->node.leaf_p;
Willy Tarreau3a932442010-05-09 19:29:23 +0200395 up_ptr = &old->node.leaf_p;
Willy Tarreauc2186022009-10-26 19:48:54 +0100396 break;
397 }
398
399 /* OK we're walking down this link */
400 old = container_of(eb_untag(troot, EB_NODE),
401 struct eb32_node, node.branches);
Willy Tarreau3a932442010-05-09 19:29:23 +0200402 old_node_bit = old->node.bit;
Willy Tarreauc2186022009-10-26 19:48:54 +0100403
404 /* Stop going down when we don't have common bits anymore. We
405 * also stop in front of a duplicates tree because it means we
406 * have to insert above.
407 */
408
Willy Tarreau3a932442010-05-09 19:29:23 +0200409 if ((old_node_bit < 0) || /* we're above a duplicate tree, stop here */
410 (((new->key ^ old->key) >> old_node_bit) >= EB_NODE_BRANCHES)) {
Willy Tarreauc2186022009-10-26 19:48:54 +0100411 /* The tree did not contain the key, so we insert <new> before the node
412 * <old>, and set ->bit to designate the lowest bit position in <new>
413 * which applies to ->branches.b[].
414 */
Willy Tarreauc2186022009-10-26 19:48:54 +0100415 new->node.node_p = old->node.node_p;
Willy Tarreau3a932442010-05-09 19:29:23 +0200416 up_ptr = &old->node.node_p;
Willy Tarreauc2186022009-10-26 19:48:54 +0100417 break;
418 }
419
420 /* walk down */
421 root = &old->node.branches;
Willy Tarreau3a932442010-05-09 19:29:23 +0200422 side = (newkey >> old_node_bit) & EB_NODE_BRANCH_MASK;
Willy Tarreauc2186022009-10-26 19:48:54 +0100423 troot = root->b[side];
424 }
425
Willy Tarreau3a932442010-05-09 19:29:23 +0200426 new_left = eb_dotag(&new->node.branches, EB_LEFT);
427 new_rght = eb_dotag(&new->node.branches, EB_RGHT);
428 new_leaf = eb_dotag(&new->node.branches, EB_LEAF);
Willy Tarreauc2186022009-10-26 19:48:54 +0100429
430 /* We need the common higher bits between new->key and old->key.
431 * What differences are there between new->key and the node here ?
432 * NOTE that bit(new) is always < bit(root) because highest
433 * bit of new->key and old->key are identical here (otherwise they
434 * would sit on different branches).
435 */
Willy Tarreau3a932442010-05-09 19:29:23 +0200436
Willy Tarreauc2186022009-10-26 19:48:54 +0100437 // note that if EB_NODE_BITS > 1, we should check that it's still >= 0
438 new->node.bit = flsnz(new->key ^ old->key) - EB_NODE_BITS;
Willy Tarreau3a932442010-05-09 19:29:23 +0200439
440 if (new->key == old->key) {
441 new->node.bit = -1; /* mark as new dup tree, just in case */
442
443 if (likely(eb_gettag(root_right))) {
444 /* we refuse to duplicate this key if the tree is
445 * tagged as containing only unique keys.
446 */
447 return old;
448 }
449
450 if (eb_gettag(troot) != EB_LEAF) {
451 /* there was already a dup tree below */
452 struct eb_node *ret;
453 ret = eb_insert_dup(&old->node, &new->node);
454 return container_of(ret, struct eb32_node, node);
455 }
456 /* otherwise fall through */
457 }
Willy Tarreauc2186022009-10-26 19:48:54 +0100458
Willy Tarreau3a932442010-05-09 19:29:23 +0200459 if ((s32)new->key >= (s32)old->key) {
460 new->node.branches.b[EB_LEFT] = troot;
461 new->node.branches.b[EB_RGHT] = new_leaf;
462 new->node.leaf_p = new_rght;
463 *up_ptr = new_left;
464 }
465 else {
466 new->node.branches.b[EB_LEFT] = new_leaf;
467 new->node.branches.b[EB_RGHT] = troot;
468 new->node.leaf_p = new_left;
469 *up_ptr = new_rght;
470 }
471
472 /* Ok, now we are inserting <new> between <root> and <old>. <old>'s
473 * parent is already set to <new>, and the <root>'s branch is still in
474 * <side>. Update the root's leaf till we have it. Note that we can also
475 * find the side by checking the side of new->node.node_p.
476 */
477
478 root->b[side] = eb_dotag(&new->node.branches, EB_NODE);
Willy Tarreauc2186022009-10-26 19:48:54 +0100479 return new;
480}
481
482#endif /* _EB32_TREE_H */