blob: 76f9eaacb5b73f6efc1e454c080aad2e69532839 [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
30#define EB32_ROOT EB_ROOT
31#define EB32_TREE_HEAD EB_TREE_HEAD
32
33/* These types may sometimes already be defined */
Willy Tarreau8d2b7772020-05-27 10:58:19 +020034#ifndef _EB32SCTREE_H
Willy Tarreauc2186022009-10-26 19:48:54 +010035typedef unsigned int u32;
36typedef signed int s32;
Willy Tarreau8d2b7772020-05-27 10:58:19 +020037#endif
Willy Tarreauc2186022009-10-26 19:48:54 +010038
39/* This structure carries a node, a leaf, and a key. It must start with the
40 * eb_node so that it can be cast into an eb_node. We could also have put some
41 * sort of transparent union here to reduce the indirection level, but the fact
42 * is, the end user is not meant to manipulate internals, so this is pointless.
43 */
44struct eb32_node {
45 struct eb_node node; /* the tree node, must be at the beginning */
Willy Tarreau41136de2020-02-22 15:55:33 +010046 MAYBE_ALIGN(sizeof(u32));
Willy Tarreauc2186022009-10-26 19:48:54 +010047 u32 key;
Willy Tarreau41136de2020-02-22 15:55:33 +010048} ALIGNED(sizeof(void*));
Willy Tarreauc2186022009-10-26 19:48:54 +010049
50/*
51 * Exported functions and macros.
52 * Many of them are always inlined because they are extremely small, and
53 * are generally called at most once or twice in a program.
54 */
55
56/* Return leftmost node in the tree, or NULL if none */
57static inline struct eb32_node *eb32_first(struct eb_root *root)
58{
59 return eb32_entry(eb_first(root), struct eb32_node, node);
60}
61
62/* Return rightmost node in the tree, or NULL if none */
63static inline struct eb32_node *eb32_last(struct eb_root *root)
64{
65 return eb32_entry(eb_last(root), struct eb32_node, node);
66}
67
68/* Return next node in the tree, or NULL if none */
69static inline struct eb32_node *eb32_next(struct eb32_node *eb32)
70{
71 return eb32_entry(eb_next(&eb32->node), struct eb32_node, node);
72}
73
74/* Return previous node in the tree, or NULL if none */
75static inline struct eb32_node *eb32_prev(struct eb32_node *eb32)
76{
77 return eb32_entry(eb_prev(&eb32->node), struct eb32_node, node);
78}
79
Willy Tarreau2b570202013-05-07 15:58:28 +020080/* Return next leaf node within a duplicate sub-tree, or NULL if none. */
81static inline struct eb32_node *eb32_next_dup(struct eb32_node *eb32)
82{
83 return eb32_entry(eb_next_dup(&eb32->node), struct eb32_node, node);
84}
85
86/* Return previous leaf node within a duplicate sub-tree, or NULL if none. */
87static inline struct eb32_node *eb32_prev_dup(struct eb32_node *eb32)
88{
89 return eb32_entry(eb_prev_dup(&eb32->node), struct eb32_node, node);
90}
91
Willy Tarreauc2186022009-10-26 19:48:54 +010092/* Return next node in the tree, skipping duplicates, or NULL if none */
93static inline struct eb32_node *eb32_next_unique(struct eb32_node *eb32)
94{
95 return eb32_entry(eb_next_unique(&eb32->node), struct eb32_node, node);
96}
97
98/* Return previous node in the tree, skipping duplicates, or NULL if none */
99static inline struct eb32_node *eb32_prev_unique(struct eb32_node *eb32)
100{
101 return eb32_entry(eb_prev_unique(&eb32->node), struct eb32_node, node);
102}
103
104/* Delete node from the tree if it was linked in. Mark the node unused. Note
105 * that this function relies on a non-inlined generic function: eb_delete.
106 */
107static inline void eb32_delete(struct eb32_node *eb32)
108{
109 eb_delete(&eb32->node);
110}
111
112/*
113 * The following functions are not inlined by default. They are declared
114 * in eb32tree.c, which simply relies on their inline version.
115 */
Willy Tarreau03e78532020-02-25 07:38:05 +0100116struct eb32_node *eb32_lookup(struct eb_root *root, u32 x);
117struct eb32_node *eb32i_lookup(struct eb_root *root, s32 x);
118struct eb32_node *eb32_lookup_le(struct eb_root *root, u32 x);
119struct eb32_node *eb32_lookup_ge(struct eb_root *root, u32 x);
120struct eb32_node *eb32_insert(struct eb_root *root, struct eb32_node *new);
121struct eb32_node *eb32i_insert(struct eb_root *root, struct eb32_node *new);
Willy Tarreauc2186022009-10-26 19:48:54 +0100122
123/*
124 * The following functions are less likely to be used directly, because their
125 * code is larger. The non-inlined version is preferred.
126 */
127
128/* Delete node from the tree if it was linked in. Mark the node unused. */
129static forceinline void __eb32_delete(struct eb32_node *eb32)
130{
131 __eb_delete(&eb32->node);
132}
133
134/*
Joseph Herlant7c16c0e2018-11-13 19:55:57 -0800135 * Find the first occurrence of a key in the tree <root>. If none can be
Willy Tarreauc2186022009-10-26 19:48:54 +0100136 * found, return NULL.
137 */
138static forceinline struct eb32_node *__eb32_lookup(struct eb_root *root, u32 x)
139{
140 struct eb32_node *node;
141 eb_troot_t *troot;
142 u32 y;
Willy Tarreau3a932442010-05-09 19:29:23 +0200143 int node_bit;
Willy Tarreauc2186022009-10-26 19:48:54 +0100144
145 troot = root->b[EB_LEFT];
146 if (unlikely(troot == NULL))
147 return NULL;
148
149 while (1) {
150 if ((eb_gettag(troot) == EB_LEAF)) {
151 node = container_of(eb_untag(troot, EB_LEAF),
152 struct eb32_node, node.branches);
153 if (node->key == x)
154 return node;
155 else
156 return NULL;
157 }
158 node = container_of(eb_untag(troot, EB_NODE),
159 struct eb32_node, node.branches);
Willy Tarreau3a932442010-05-09 19:29:23 +0200160 node_bit = node->node.bit;
Willy Tarreauc2186022009-10-26 19:48:54 +0100161
162 y = node->key ^ x;
163 if (!y) {
164 /* Either we found the node which holds the key, or
165 * we have a dup tree. In the later case, we have to
166 * walk it down left to get the first entry.
167 */
Willy Tarreau3a932442010-05-09 19:29:23 +0200168 if (node_bit < 0) {
Willy Tarreauc2186022009-10-26 19:48:54 +0100169 troot = node->node.branches.b[EB_LEFT];
170 while (eb_gettag(troot) != EB_LEAF)
171 troot = (eb_untag(troot, EB_NODE))->b[EB_LEFT];
172 node = container_of(eb_untag(troot, EB_LEAF),
173 struct eb32_node, node.branches);
174 }
175 return node;
176 }
177
Willy Tarreau3a932442010-05-09 19:29:23 +0200178 if ((y >> node_bit) >= EB_NODE_BRANCHES)
Willy Tarreauc2186022009-10-26 19:48:54 +0100179 return NULL; /* no more common bits */
180
Willy Tarreau3a932442010-05-09 19:29:23 +0200181 troot = node->node.branches.b[(x >> node_bit) & EB_NODE_BRANCH_MASK];
Willy Tarreauc2186022009-10-26 19:48:54 +0100182 }
183}
184
185/*
Joseph Herlant7c16c0e2018-11-13 19:55:57 -0800186 * Find the first occurrence of a signed key in the tree <root>. If none can
Willy Tarreauc2186022009-10-26 19:48:54 +0100187 * be found, return NULL.
188 */
189static forceinline struct eb32_node *__eb32i_lookup(struct eb_root *root, s32 x)
190{
191 struct eb32_node *node;
192 eb_troot_t *troot;
193 u32 key = x ^ 0x80000000;
194 u32 y;
Willy Tarreau3a932442010-05-09 19:29:23 +0200195 int node_bit;
Willy Tarreauc2186022009-10-26 19:48:54 +0100196
197 troot = root->b[EB_LEFT];
198 if (unlikely(troot == NULL))
199 return NULL;
200
201 while (1) {
202 if ((eb_gettag(troot) == EB_LEAF)) {
203 node = container_of(eb_untag(troot, EB_LEAF),
204 struct eb32_node, node.branches);
Willy Tarreau3a932442010-05-09 19:29:23 +0200205 if (node->key == (u32)x)
Willy Tarreauc2186022009-10-26 19:48:54 +0100206 return node;
207 else
208 return NULL;
209 }
210 node = container_of(eb_untag(troot, EB_NODE),
211 struct eb32_node, node.branches);
Willy Tarreau3a932442010-05-09 19:29:23 +0200212 node_bit = node->node.bit;
Willy Tarreauc2186022009-10-26 19:48:54 +0100213
214 y = node->key ^ x;
215 if (!y) {
216 /* Either we found the node which holds the key, or
217 * we have a dup tree. In the later case, we have to
218 * walk it down left to get the first entry.
219 */
Willy Tarreau3a932442010-05-09 19:29:23 +0200220 if (node_bit < 0) {
Willy Tarreauc2186022009-10-26 19:48:54 +0100221 troot = node->node.branches.b[EB_LEFT];
222 while (eb_gettag(troot) != EB_LEAF)
223 troot = (eb_untag(troot, EB_NODE))->b[EB_LEFT];
224 node = container_of(eb_untag(troot, EB_LEAF),
225 struct eb32_node, node.branches);
226 }
227 return node;
228 }
229
Willy Tarreau3a932442010-05-09 19:29:23 +0200230 if ((y >> node_bit) >= EB_NODE_BRANCHES)
Willy Tarreauc2186022009-10-26 19:48:54 +0100231 return NULL; /* no more common bits */
232
Willy Tarreau3a932442010-05-09 19:29:23 +0200233 troot = node->node.branches.b[(key >> node_bit) & EB_NODE_BRANCH_MASK];
Willy Tarreauc2186022009-10-26 19:48:54 +0100234 }
235}
236
237/* Insert eb32_node <new> into subtree starting at node root <root>.
238 * Only new->key needs be set with the key. The eb32_node is returned.
239 * If root->b[EB_RGHT]==1, the tree may only contain unique keys.
240 */
241static forceinline struct eb32_node *
242__eb32_insert(struct eb_root *root, struct eb32_node *new) {
243 struct eb32_node *old;
244 unsigned int side;
Willy Tarreau3a932442010-05-09 19:29:23 +0200245 eb_troot_t *troot, **up_ptr;
Willy Tarreauc2186022009-10-26 19:48:54 +0100246 u32 newkey; /* caching the key saves approximately one cycle */
Willy Tarreau6258f7b2011-09-19 20:48:00 +0200247 eb_troot_t *root_right;
Willy Tarreau3a932442010-05-09 19:29:23 +0200248 eb_troot_t *new_left, *new_rght;
249 eb_troot_t *new_leaf;
250 int old_node_bit;
Willy Tarreauc2186022009-10-26 19:48:54 +0100251
252 side = EB_LEFT;
253 troot = root->b[EB_LEFT];
254 root_right = root->b[EB_RGHT];
255 if (unlikely(troot == NULL)) {
256 /* Tree is empty, insert the leaf part below the left branch */
257 root->b[EB_LEFT] = eb_dotag(&new->node.branches, EB_LEAF);
258 new->node.leaf_p = eb_dotag(root, EB_LEFT);
259 new->node.node_p = NULL; /* node part unused */
260 return new;
261 }
262
263 /* The tree descent is fairly easy :
264 * - first, check if we have reached a leaf node
265 * - second, check if we have gone too far
266 * - third, reiterate
267 * Everywhere, we use <new> for the node node we are inserting, <root>
268 * for the node we attach it to, and <old> for the node we are
269 * displacing below <new>. <troot> will always point to the future node
270 * (tagged with its type). <side> carries the side the node <new> is
271 * attached to below its parent, which is also where previous node
272 * was attached. <newkey> carries the key being inserted.
273 */
274 newkey = new->key;
275
276 while (1) {
Willy Tarreau3a932442010-05-09 19:29:23 +0200277 if (eb_gettag(troot) == EB_LEAF) {
278 /* insert above a leaf */
Willy Tarreauc2186022009-10-26 19:48:54 +0100279 old = container_of(eb_untag(troot, EB_LEAF),
280 struct eb32_node, node.branches);
Willy Tarreauc2186022009-10-26 19:48:54 +0100281 new->node.node_p = old->node.leaf_p;
Willy Tarreau3a932442010-05-09 19:29:23 +0200282 up_ptr = &old->node.leaf_p;
Willy Tarreauc2186022009-10-26 19:48:54 +0100283 break;
284 }
285
286 /* OK we're walking down this link */
287 old = container_of(eb_untag(troot, EB_NODE),
288 struct eb32_node, node.branches);
Willy Tarreau3a932442010-05-09 19:29:23 +0200289 old_node_bit = old->node.bit;
Willy Tarreauc2186022009-10-26 19:48:54 +0100290
291 /* Stop going down when we don't have common bits anymore. We
292 * also stop in front of a duplicates tree because it means we
293 * have to insert above.
294 */
295
Willy Tarreau3a932442010-05-09 19:29:23 +0200296 if ((old_node_bit < 0) || /* we're above a duplicate tree, stop here */
297 (((new->key ^ old->key) >> old_node_bit) >= EB_NODE_BRANCHES)) {
Willy Tarreauc2186022009-10-26 19:48:54 +0100298 /* The tree did not contain the key, so we insert <new> before the node
299 * <old>, and set ->bit to designate the lowest bit position in <new>
300 * which applies to ->branches.b[].
301 */
Willy Tarreauc2186022009-10-26 19:48:54 +0100302 new->node.node_p = old->node.node_p;
Willy Tarreau3a932442010-05-09 19:29:23 +0200303 up_ptr = &old->node.node_p;
Willy Tarreauc2186022009-10-26 19:48:54 +0100304 break;
305 }
306
307 /* walk down */
308 root = &old->node.branches;
Willy Tarreau3a932442010-05-09 19:29:23 +0200309 side = (newkey >> old_node_bit) & EB_NODE_BRANCH_MASK;
Willy Tarreauc2186022009-10-26 19:48:54 +0100310 troot = root->b[side];
311 }
312
Willy Tarreau3a932442010-05-09 19:29:23 +0200313 new_left = eb_dotag(&new->node.branches, EB_LEFT);
314 new_rght = eb_dotag(&new->node.branches, EB_RGHT);
315 new_leaf = eb_dotag(&new->node.branches, EB_LEAF);
Willy Tarreauc2186022009-10-26 19:48:54 +0100316
317 /* We need the common higher bits between new->key and old->key.
318 * What differences are there between new->key and the node here ?
319 * NOTE that bit(new) is always < bit(root) because highest
320 * bit of new->key and old->key are identical here (otherwise they
321 * would sit on different branches).
322 */
Willy Tarreau3a932442010-05-09 19:29:23 +0200323
Willy Tarreauc2186022009-10-26 19:48:54 +0100324 // note that if EB_NODE_BITS > 1, we should check that it's still >= 0
325 new->node.bit = flsnz(new->key ^ old->key) - EB_NODE_BITS;
Willy Tarreau3a932442010-05-09 19:29:23 +0200326
327 if (new->key == old->key) {
328 new->node.bit = -1; /* mark as new dup tree, just in case */
329
330 if (likely(eb_gettag(root_right))) {
331 /* we refuse to duplicate this key if the tree is
332 * tagged as containing only unique keys.
333 */
334 return old;
335 }
336
337 if (eb_gettag(troot) != EB_LEAF) {
338 /* there was already a dup tree below */
339 struct eb_node *ret;
340 ret = eb_insert_dup(&old->node, &new->node);
341 return container_of(ret, struct eb32_node, node);
342 }
343 /* otherwise fall through */
344 }
Willy Tarreauc2186022009-10-26 19:48:54 +0100345
Willy Tarreau3a932442010-05-09 19:29:23 +0200346 if (new->key >= old->key) {
347 new->node.branches.b[EB_LEFT] = troot;
348 new->node.branches.b[EB_RGHT] = new_leaf;
349 new->node.leaf_p = new_rght;
350 *up_ptr = new_left;
351 }
352 else {
353 new->node.branches.b[EB_LEFT] = new_leaf;
354 new->node.branches.b[EB_RGHT] = troot;
355 new->node.leaf_p = new_left;
356 *up_ptr = new_rght;
357 }
358
359 /* Ok, now we are inserting <new> between <root> and <old>. <old>'s
360 * parent is already set to <new>, and the <root>'s branch is still in
361 * <side>. Update the root's leaf till we have it. Note that we can also
362 * find the side by checking the side of new->node.node_p.
363 */
364
365 root->b[side] = eb_dotag(&new->node.branches, EB_NODE);
Willy Tarreauc2186022009-10-26 19:48:54 +0100366 return new;
367}
368
369/* Insert eb32_node <new> into subtree starting at node root <root>, using
370 * signed keys. Only new->key needs be set with the key. The eb32_node
371 * is returned. If root->b[EB_RGHT]==1, the tree may only contain unique keys.
372 */
373static forceinline struct eb32_node *
374__eb32i_insert(struct eb_root *root, struct eb32_node *new) {
375 struct eb32_node *old;
376 unsigned int side;
Willy Tarreau3a932442010-05-09 19:29:23 +0200377 eb_troot_t *troot, **up_ptr;
Willy Tarreauc2186022009-10-26 19:48:54 +0100378 int newkey; /* caching the key saves approximately one cycle */
Willy Tarreau6258f7b2011-09-19 20:48:00 +0200379 eb_troot_t *root_right;
Willy Tarreau3a932442010-05-09 19:29:23 +0200380 eb_troot_t *new_left, *new_rght;
381 eb_troot_t *new_leaf;
382 int old_node_bit;
Willy Tarreauc2186022009-10-26 19:48:54 +0100383
384 side = EB_LEFT;
385 troot = root->b[EB_LEFT];
386 root_right = root->b[EB_RGHT];
387 if (unlikely(troot == NULL)) {
388 /* Tree is empty, insert the leaf part below the left branch */
389 root->b[EB_LEFT] = eb_dotag(&new->node.branches, EB_LEAF);
390 new->node.leaf_p = eb_dotag(root, EB_LEFT);
391 new->node.node_p = NULL; /* node part unused */
392 return new;
393 }
394
395 /* The tree descent is fairly easy :
396 * - first, check if we have reached a leaf node
397 * - second, check if we have gone too far
398 * - third, reiterate
399 * Everywhere, we use <new> for the node node we are inserting, <root>
400 * for the node we attach it to, and <old> for the node we are
401 * displacing below <new>. <troot> will always point to the future node
402 * (tagged with its type). <side> carries the side the node <new> is
403 * attached to below its parent, which is also where previous node
404 * was attached. <newkey> carries a high bit shift of the key being
405 * inserted in order to have negative keys stored before positive
406 * ones.
407 */
408 newkey = new->key + 0x80000000;
409
410 while (1) {
Willy Tarreau3a932442010-05-09 19:29:23 +0200411 if (eb_gettag(troot) == EB_LEAF) {
Willy Tarreauc2186022009-10-26 19:48:54 +0100412 old = container_of(eb_untag(troot, EB_LEAF),
413 struct eb32_node, node.branches);
Willy Tarreauc2186022009-10-26 19:48:54 +0100414 new->node.node_p = old->node.leaf_p;
Willy Tarreau3a932442010-05-09 19:29:23 +0200415 up_ptr = &old->node.leaf_p;
Willy Tarreauc2186022009-10-26 19:48:54 +0100416 break;
417 }
418
419 /* OK we're walking down this link */
420 old = container_of(eb_untag(troot, EB_NODE),
421 struct eb32_node, node.branches);
Willy Tarreau3a932442010-05-09 19:29:23 +0200422 old_node_bit = old->node.bit;
Willy Tarreauc2186022009-10-26 19:48:54 +0100423
424 /* Stop going down when we don't have common bits anymore. We
425 * also stop in front of a duplicates tree because it means we
426 * have to insert above.
427 */
428
Willy Tarreau3a932442010-05-09 19:29:23 +0200429 if ((old_node_bit < 0) || /* we're above a duplicate tree, stop here */
430 (((new->key ^ old->key) >> old_node_bit) >= EB_NODE_BRANCHES)) {
Willy Tarreauc2186022009-10-26 19:48:54 +0100431 /* The tree did not contain the key, so we insert <new> before the node
432 * <old>, and set ->bit to designate the lowest bit position in <new>
433 * which applies to ->branches.b[].
434 */
Willy Tarreauc2186022009-10-26 19:48:54 +0100435 new->node.node_p = old->node.node_p;
Willy Tarreau3a932442010-05-09 19:29:23 +0200436 up_ptr = &old->node.node_p;
Willy Tarreauc2186022009-10-26 19:48:54 +0100437 break;
438 }
439
440 /* walk down */
441 root = &old->node.branches;
Willy Tarreau3a932442010-05-09 19:29:23 +0200442 side = (newkey >> old_node_bit) & EB_NODE_BRANCH_MASK;
Willy Tarreauc2186022009-10-26 19:48:54 +0100443 troot = root->b[side];
444 }
445
Willy Tarreau3a932442010-05-09 19:29:23 +0200446 new_left = eb_dotag(&new->node.branches, EB_LEFT);
447 new_rght = eb_dotag(&new->node.branches, EB_RGHT);
448 new_leaf = eb_dotag(&new->node.branches, EB_LEAF);
Willy Tarreauc2186022009-10-26 19:48:54 +0100449
450 /* We need the common higher bits between new->key and old->key.
451 * What differences are there between new->key and the node here ?
452 * NOTE that bit(new) is always < bit(root) because highest
453 * bit of new->key and old->key are identical here (otherwise they
454 * would sit on different branches).
455 */
Willy Tarreau3a932442010-05-09 19:29:23 +0200456
Willy Tarreauc2186022009-10-26 19:48:54 +0100457 // note that if EB_NODE_BITS > 1, we should check that it's still >= 0
458 new->node.bit = flsnz(new->key ^ old->key) - EB_NODE_BITS;
Willy Tarreau3a932442010-05-09 19:29:23 +0200459
460 if (new->key == old->key) {
461 new->node.bit = -1; /* mark as new dup tree, just in case */
462
463 if (likely(eb_gettag(root_right))) {
464 /* we refuse to duplicate this key if the tree is
465 * tagged as containing only unique keys.
466 */
467 return old;
468 }
469
470 if (eb_gettag(troot) != EB_LEAF) {
471 /* there was already a dup tree below */
472 struct eb_node *ret;
473 ret = eb_insert_dup(&old->node, &new->node);
474 return container_of(ret, struct eb32_node, node);
475 }
476 /* otherwise fall through */
477 }
Willy Tarreauc2186022009-10-26 19:48:54 +0100478
Willy Tarreau3a932442010-05-09 19:29:23 +0200479 if ((s32)new->key >= (s32)old->key) {
480 new->node.branches.b[EB_LEFT] = troot;
481 new->node.branches.b[EB_RGHT] = new_leaf;
482 new->node.leaf_p = new_rght;
483 *up_ptr = new_left;
484 }
485 else {
486 new->node.branches.b[EB_LEFT] = new_leaf;
487 new->node.branches.b[EB_RGHT] = troot;
488 new->node.leaf_p = new_left;
489 *up_ptr = new_rght;
490 }
491
492 /* Ok, now we are inserting <new> between <root> and <old>. <old>'s
493 * parent is already set to <new>, and the <root>'s branch is still in
494 * <side>. Update the root's leaf till we have it. Note that we can also
495 * find the side by checking the side of new->node.node_p.
496 */
497
498 root->b[side] = eb_dotag(&new->node.branches, EB_NODE);
Willy Tarreauc2186022009-10-26 19:48:54 +0100499 return new;
500}
501
502#endif /* _EB32_TREE_H */