2
* Elastic Binary Trees - macros and structures for operations on pointer nodes.
4
* (C) 2002-2008 - Willy Tarreau <w@1wt.eu>
6
* This program is free software; you can redistribute it and/or modify
7
* it under the terms of the GNU General Public License as published by
8
* the Free Software Foundation; either version 2 of the License, or
9
* (at your option) any later version.
11
* This program 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
14
* GNU General Public License for more details.
16
* You should have received a copy of the GNU General Public License
17
* along with this program; if not, write to the Free Software
18
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21
#ifndef _COMMON_EBPTTREE_H
22
#define _COMMON_EBPTTREE_H
27
/* Return the structure of type <type> whose member <member> points to <ptr> */
28
#define ebpt_entry(ptr, type, member) container_of(ptr, type, member)
30
#define EBPT_ROOT EB_ROOT
31
#define EBPT_TREE_HEAD EB_TREE_HEAD
33
/* on *almost* all platforms, a pointer can be cast into a size_t which is unsigned */
35
#define PTR_INT_TYPE size_t
38
typedef PTR_INT_TYPE ptr_t;
40
/* This structure carries a node, a leaf, and a key. It must start with the
41
* eb_node so that it can be cast into an eb_node. We could also have put some
42
* sort of transparent union here to reduce the indirection level, but the fact
43
* is, the end user is not meant to manipulate internals, so this is pointless.
46
struct eb_node node; /* the tree node, must be at the beginning */
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.
56
/* Return leftmost node in the tree, or NULL if none */
57
static inline struct ebpt_node *ebpt_first(struct eb_root *root)
59
return ebpt_entry(eb_first(root), struct ebpt_node, node);
62
/* Return rightmost node in the tree, or NULL if none */
63
static inline struct ebpt_node *ebpt_last(struct eb_root *root)
65
return ebpt_entry(eb_last(root), struct ebpt_node, node);
68
/* Return next node in the tree, or NULL if none */
69
static inline struct ebpt_node *ebpt_next(struct ebpt_node *ebpt)
71
return ebpt_entry(eb_next(&ebpt->node), struct ebpt_node, node);
74
/* Return previous node in the tree, or NULL if none */
75
static inline struct ebpt_node *ebpt_prev(struct ebpt_node *ebpt)
77
return ebpt_entry(eb_prev(&ebpt->node), struct ebpt_node, node);
80
/* Return next node in the tree, skipping duplicates, or NULL if none */
81
static inline struct ebpt_node *ebpt_next_unique(struct ebpt_node *ebpt)
83
return ebpt_entry(eb_next_unique(&ebpt->node), struct ebpt_node, node);
86
/* Return previous node in the tree, skipping duplicates, or NULL if none */
87
static inline struct ebpt_node *ebpt_prev_unique(struct ebpt_node *ebpt)
89
return ebpt_entry(eb_prev_unique(&ebpt->node), struct ebpt_node, node);
92
/* Delete node from the tree if it was linked in. Mark the node unused. Note
93
* that this function relies on a non-inlined generic function: eb_delete.
95
static inline void ebpt_delete(struct ebpt_node *ebpt)
97
eb_delete(&ebpt->node);
101
* The following functions are not inlined by default. They are declared
102
* in ebpttree.c, which simply relies on their inline version.
104
REGPRM2 struct ebpt_node *ebpt_lookup(struct eb_root *root, void *x);
105
REGPRM2 struct ebpt_node *ebpt_insert(struct eb_root *root, struct ebpt_node *new);
108
* The following functions are less likely to be used directly, because their
109
* code is larger. The non-inlined version is preferred.
112
/* Delete node from the tree if it was linked in. Mark the node unused. */
113
static forceinline void __ebpt_delete(struct ebpt_node *ebpt)
115
__eb_delete(&ebpt->node);
119
* Find the first occurence of a key in the tree <root>. If none can be
120
* found, return NULL.
122
static forceinline struct ebpt_node *__ebpt_lookup(struct eb_root *root, void *x)
124
struct ebpt_node *node;
128
troot = root->b[EB_LEFT];
129
if (unlikely(troot == NULL))
133
if ((eb_gettag(troot) == EB_LEAF)) {
134
node = container_of(eb_untag(troot, EB_LEAF),
135
struct ebpt_node, node.branches);
141
node = container_of(eb_untag(troot, EB_NODE),
142
struct ebpt_node, node.branches);
144
y = (ptr_t)node->key ^ (ptr_t)x;
146
/* Either we found the node which holds the key, or
147
* we have a dup tree. In the later case, we have to
148
* walk it down left to get the first entry.
150
if (node->node.bit < 0) {
151
troot = node->node.branches.b[EB_LEFT];
152
while (eb_gettag(troot) != EB_LEAF)
153
troot = (eb_untag(troot, EB_NODE))->b[EB_LEFT];
154
node = container_of(eb_untag(troot, EB_LEAF),
155
struct ebpt_node, node.branches);
160
if ((y >> node->node.bit) >= EB_NODE_BRANCHES)
161
return NULL; /* no more common bits */
163
troot = node->node.branches.b[((ptr_t)x >> node->node.bit) & EB_NODE_BRANCH_MASK];
167
/* Insert ebpt_node <new> into subtree starting at node root <root>.
168
* Only new->key needs be set with the key. The ebpt_node is returned.
169
* If root->b[EB_RGHT]==1, the tree may only contain unique keys.
171
static forceinline struct ebpt_node *
172
__ebpt_insert(struct eb_root *root, struct ebpt_node *new) {
173
struct ebpt_node *old;
176
void *newkey; /* caching the key saves approximately one cycle */
177
eb_troot_t *root_right = root;
180
troot = root->b[EB_LEFT];
181
root_right = root->b[EB_RGHT];
182
if (unlikely(troot == NULL)) {
183
/* Tree is empty, insert the leaf part below the left branch */
184
root->b[EB_LEFT] = eb_dotag(&new->node.branches, EB_LEAF);
185
new->node.leaf_p = eb_dotag(root, EB_LEFT);
186
new->node.node_p = NULL; /* node part unused */
190
/* The tree descent is fairly easy :
191
* - first, check if we have reached a leaf node
192
* - second, check if we have gone too far
194
* Everywhere, we use <new> for the node node we are inserting, <root>
195
* for the node we attach it to, and <old> for the node we are
196
* displacing below <new>. <troot> will always point to the future node
197
* (tagged with its type). <side> carries the side the node <new> is
198
* attached to below its parent, which is also where previous node
199
* was attached. <newkey> carries the key being inserted.
204
if (unlikely(eb_gettag(troot) == EB_LEAF)) {
205
eb_troot_t *new_left, *new_rght;
206
eb_troot_t *new_leaf, *old_leaf;
208
old = container_of(eb_untag(troot, EB_LEAF),
209
struct ebpt_node, node.branches);
211
new_left = eb_dotag(&new->node.branches, EB_LEFT);
212
new_rght = eb_dotag(&new->node.branches, EB_RGHT);
213
new_leaf = eb_dotag(&new->node.branches, EB_LEAF);
214
old_leaf = eb_dotag(&old->node.branches, EB_LEAF);
216
new->node.node_p = old->node.leaf_p;
218
/* Right here, we have 3 possibilities :
219
- the tree does not contain the key, and we have
220
new->key < old->key. We insert new above old, on
223
- the tree does not contain the key, and we have
224
new->key > old->key. We insert new above old, on
227
- the tree does contain the key, which implies it
228
is alone. We add the new key next to it as a
231
The last two cases can easily be partially merged.
234
if (new->key < old->key) {
235
new->node.leaf_p = new_left;
236
old->node.leaf_p = new_rght;
237
new->node.branches.b[EB_LEFT] = new_leaf;
238
new->node.branches.b[EB_RGHT] = old_leaf;
240
/* we may refuse to duplicate this key if the tree is
241
* tagged as containing only unique keys.
243
if ((new->key == old->key) && eb_gettag(root_right))
246
/* new->key >= old->key, new goes the right */
247
old->node.leaf_p = new_left;
248
new->node.leaf_p = new_rght;
249
new->node.branches.b[EB_LEFT] = old_leaf;
250
new->node.branches.b[EB_RGHT] = new_leaf;
252
if (new->key == old->key) {
254
root->b[side] = eb_dotag(&new->node.branches, EB_NODE);
261
/* OK we're walking down this link */
262
old = container_of(eb_untag(troot, EB_NODE),
263
struct ebpt_node, node.branches);
265
/* Stop going down when we don't have common bits anymore. We
266
* also stop in front of a duplicates tree because it means we
267
* have to insert above.
270
if ((old->node.bit < 0) || /* we're above a duplicate tree, stop here */
271
((((ptr_t)new->key ^ (ptr_t)old->key) >> old->node.bit) >= EB_NODE_BRANCHES)) {
272
/* The tree did not contain the key, so we insert <new> before the node
273
* <old>, and set ->bit to designate the lowest bit position in <new>
274
* which applies to ->branches.b[].
276
eb_troot_t *new_left, *new_rght;
277
eb_troot_t *new_leaf, *old_node;
279
new_left = eb_dotag(&new->node.branches, EB_LEFT);
280
new_rght = eb_dotag(&new->node.branches, EB_RGHT);
281
new_leaf = eb_dotag(&new->node.branches, EB_LEAF);
282
old_node = eb_dotag(&old->node.branches, EB_NODE);
284
new->node.node_p = old->node.node_p;
286
if (new->key < old->key) {
287
new->node.leaf_p = new_left;
288
old->node.node_p = new_rght;
289
new->node.branches.b[EB_LEFT] = new_leaf;
290
new->node.branches.b[EB_RGHT] = old_node;
292
else if (new->key > old->key) {
293
old->node.node_p = new_left;
294
new->node.leaf_p = new_rght;
295
new->node.branches.b[EB_LEFT] = old_node;
296
new->node.branches.b[EB_RGHT] = new_leaf;
300
ret = eb_insert_dup(&old->node, &new->node);
301
return container_of(ret, struct ebpt_node, node);
307
root = &old->node.branches;
308
side = ((ptr_t)newkey >> old->node.bit) & EB_NODE_BRANCH_MASK;
309
troot = root->b[side];
312
/* Ok, now we are inserting <new> between <root> and <old>. <old>'s
313
* parent is already set to <new>, and the <root>'s branch is still in
314
* <side>. Update the root's leaf till we have it. Note that we can also
315
* find the side by checking the side of new->node.node_p.
318
/* We need the common higher bits between new->key and old->key.
319
* What differences are there between new->key and the node here ?
320
* NOTE that bit(new) is always < bit(root) because highest
321
* bit of new->key and old->key are identical here (otherwise they
322
* would sit on different branches).
324
// note that if EB_NODE_BITS > 1, we should check that it's still >= 0
326
/* let the compiler choose the best branch based on the pointer size */
327
if (sizeof(ptr_t) == 4)
328
new->node.bit = flsnz((ptr_t)new->key ^ (ptr_t)old->key) - EB_NODE_BITS;
330
new->node.bit = fls64((ptr_t)new->key ^ (ptr_t)old->key) - EB_NODE_BITS;
331
root->b[side] = eb_dotag(&new->node.branches, EB_NODE);
336
#endif /* _COMMON_EBPTTREE_H */