2
* Elastic Binary Trees - macros and structures for operations on pointer nodes.
3
* (C) 2002-2007 - Willy Tarreau <w@1wt.eu>
5
* This program is free software; you can redistribute it and/or modify
6
* it under the terms of the GNU General Public License as published by
7
* the Free Software Foundation; either version 2 of the License, or
8
* (at your option) any later version.
10
* This program is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
* GNU General Public License for more details.
15
* You should have received a copy of the GNU General Public License
16
* along with this program; if not, write to the Free Software
17
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20
#ifndef _COMMON_EBPTTREE_H
21
#define _COMMON_EBPTTREE_H
26
/* Return the structure of type <type> whose member <member> points to <ptr> */
27
#define ebpt_entry(ptr, type, member) container_of(ptr, type, member)
29
#define EBPT_ROOT EB_ROOT
30
#define EBPT_TREE_HEAD EB_TREE_HEAD
32
/* on *almost* all platforms, a pointer can be cast into a size_t which is unsigned */
34
#define PTR_INT_TYPE size_t
37
typedef PTR_INT_TYPE ptr_t;
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.
45
struct eb_node node; /* the tree node, must be at the beginning */
50
* Exported functions and macros.
51
* Many of them are always inlined because they are extremely small, and
52
* are generally called at most once or twice in a program.
55
/* Return leftmost node in the tree, or NULL if none */
56
static inline struct ebpt_node *ebpt_first(struct eb_root *root)
58
return ebpt_entry(eb_first(root), struct ebpt_node, node);
61
/* Return rightmost node in the tree, or NULL if none */
62
static inline struct ebpt_node *ebpt_last(struct eb_root *root)
64
return ebpt_entry(eb_last(root), struct ebpt_node, node);
67
/* Return next node in the tree, or NULL if none */
68
static inline struct ebpt_node *ebpt_next(struct ebpt_node *ebpt)
70
return ebpt_entry(eb_next(&ebpt->node), struct ebpt_node, node);
73
/* Return previous node in the tree, or NULL if none */
74
static inline struct ebpt_node *ebpt_prev(struct ebpt_node *ebpt)
76
return ebpt_entry(eb_prev(&ebpt->node), struct ebpt_node, node);
79
/* Return next node in the tree, skipping duplicates, or NULL if none */
80
static inline struct ebpt_node *ebpt_next_unique(struct ebpt_node *ebpt)
82
return ebpt_entry(eb_next_unique(&ebpt->node), struct ebpt_node, node);
85
/* Return previous node in the tree, skipping duplicates, or NULL if none */
86
static inline struct ebpt_node *ebpt_prev_unique(struct ebpt_node *ebpt)
88
return ebpt_entry(eb_prev_unique(&ebpt->node), struct ebpt_node, node);
91
/* Delete node from the tree if it was linked in. Mark the node unused. Note
92
* that this function relies on a non-inlined generic function: eb_delete.
94
static inline void ebpt_delete(struct ebpt_node *ebpt)
96
eb_delete(&ebpt->node);
100
* The following functions are not inlined by default. They are declared
101
* in ebpttree.c, which simply relies on their inline version.
103
REGPRM2 struct ebpt_node *ebpt_lookup(struct eb_root *root, void *x);
104
REGPRM2 struct ebpt_node *ebpt_insert(struct eb_root *root, struct ebpt_node *new);
107
* The following functions are less likely to be used directly, because their
108
* code is larger. The non-inlined version is preferred.
111
/* Delete node from the tree if it was linked in. Mark the node unused. */
112
static inline void __ebpt_delete(struct ebpt_node *ebpt)
114
__eb_delete(&ebpt->node);
118
* Find the first occurence of a key in the tree <root>. If none can be
119
* found, return NULL.
121
static inline struct ebpt_node *__ebpt_lookup(struct eb_root *root, void *x)
123
struct ebpt_node *node;
126
troot = root->b[EB_LEFT];
127
if (unlikely(troot == NULL))
131
if ((eb_gettag(troot) == EB_LEAF)) {
132
node = container_of(eb_untag(troot, EB_LEAF),
133
struct ebpt_node, node.branches);
139
node = container_of(eb_untag(troot, EB_NODE),
140
struct ebpt_node, node.branches);
142
if (x == node->key) {
143
/* Either we found the node which holds the key, or
144
* we have a dup tree. In the later case, we have to
145
* walk it down left to get the first entry.
147
if (node->node.bit < 0) {
148
troot = node->node.branches.b[EB_LEFT];
149
while (eb_gettag(troot) != EB_LEAF)
150
troot = (eb_untag(troot, EB_NODE))->b[EB_LEFT];
151
node = container_of(eb_untag(troot, EB_LEAF),
152
struct ebpt_node, node.branches);
157
troot = node->node.branches.b[((ptr_t)x >> node->node.bit) & EB_NODE_BRANCH_MASK];
161
/* Insert ebpt_node <new> into subtree starting at node root <root>.
162
* Only new->key needs be set with the key. The ebpt_node is returned.
164
static inline struct ebpt_node *
165
__ebpt_insert(struct eb_root *root, struct ebpt_node *new) {
166
struct ebpt_node *old;
169
void *newkey; /* caching the key saves approximately one cycle */
172
troot = root->b[EB_LEFT];
173
if (unlikely(troot == NULL)) {
174
/* Tree is empty, insert the leaf part below the left branch */
175
root->b[EB_LEFT] = eb_dotag(&new->node.branches, EB_LEAF);
176
new->node.leaf_p = eb_dotag(root, EB_LEFT);
177
new->node.node_p = NULL; /* node part unused */
181
/* The tree descent is fairly easy :
182
* - first, check if we have reached a leaf node
183
* - second, check if we have gone too far
185
* Everywhere, we use <new> for the node node we are inserting, <root>
186
* for the node we attach it to, and <old> for the node we are
187
* displacing below <new>. <troot> will always point to the future node
188
* (tagged with its type). <side> carries the side the node <new> is
189
* attached to below its parent, which is also where previous node
190
* was attached. <newkey> carries the key being inserted.
195
if (unlikely(eb_gettag(troot) == EB_LEAF)) {
196
eb_troot_t *new_left, *new_rght;
197
eb_troot_t *new_leaf, *old_leaf;
199
old = container_of(eb_untag(troot, EB_LEAF),
200
struct ebpt_node, node.branches);
202
new_left = eb_dotag(&new->node.branches, EB_LEFT);
203
new_rght = eb_dotag(&new->node.branches, EB_RGHT);
204
new_leaf = eb_dotag(&new->node.branches, EB_LEAF);
205
old_leaf = eb_dotag(&old->node.branches, EB_LEAF);
207
new->node.node_p = old->node.leaf_p;
209
/* Right here, we have 3 possibilities :
210
- the tree does not contain the key, and we have
211
new->key < old->key. We insert new above old, on
214
- the tree does not contain the key, and we have
215
new->key > old->key. We insert new above old, on
218
- the tree does contain the key, which implies it
219
is alone. We add the new key next to it as a
222
The last two cases can easily be partially merged.
225
if (new->key < old->key) {
226
new->node.leaf_p = new_left;
227
old->node.leaf_p = new_rght;
228
new->node.branches.b[EB_LEFT] = new_leaf;
229
new->node.branches.b[EB_RGHT] = old_leaf;
231
/* new->key >= old->key, new goes the right */
232
old->node.leaf_p = new_left;
233
new->node.leaf_p = new_rght;
234
new->node.branches.b[EB_LEFT] = old_leaf;
235
new->node.branches.b[EB_RGHT] = new_leaf;
237
if (new->key == old->key) {
239
root->b[side] = eb_dotag(&new->node.branches, EB_NODE);
246
/* OK we're walking down this link */
247
old = container_of(eb_untag(troot, EB_NODE),
248
struct ebpt_node, node.branches);
250
/* Stop going down when we don't have common bits anymore. We
251
* also stop in front of a duplicates tree because it means we
252
* have to insert above.
255
if ((old->node.bit < 0) || /* we're above a duplicate tree, stop here */
256
((((ptr_t)new->key ^ (ptr_t)old->key) >> old->node.bit) >= EB_NODE_BRANCHES)) {
257
/* The tree did not contain the key, so we insert <new> before the node
258
* <old>, and set ->bit to designate the lowest bit position in <new>
259
* which applies to ->branches.b[].
261
eb_troot_t *new_left, *new_rght;
262
eb_troot_t *new_leaf, *old_node;
264
new_left = eb_dotag(&new->node.branches, EB_LEFT);
265
new_rght = eb_dotag(&new->node.branches, EB_RGHT);
266
new_leaf = eb_dotag(&new->node.branches, EB_LEAF);
267
old_node = eb_dotag(&old->node.branches, EB_NODE);
269
new->node.node_p = old->node.node_p;
271
if (new->key < old->key) {
272
new->node.leaf_p = new_left;
273
old->node.node_p = new_rght;
274
new->node.branches.b[EB_LEFT] = new_leaf;
275
new->node.branches.b[EB_RGHT] = old_node;
277
else if (new->key > old->key) {
278
old->node.node_p = new_left;
279
new->node.leaf_p = new_rght;
280
new->node.branches.b[EB_LEFT] = old_node;
281
new->node.branches.b[EB_RGHT] = new_leaf;
285
ret = eb_insert_dup(&old->node, &new->node);
286
return container_of(ret, struct ebpt_node, node);
292
root = &old->node.branches;
293
side = ((ptr_t)newkey >> old->node.bit) & EB_NODE_BRANCH_MASK;
294
troot = root->b[side];
297
/* Ok, now we are inserting <new> between <root> and <old>. <old>'s
298
* parent is already set to <new>, and the <root>'s branch is still in
299
* <side>. Update the root's leaf till we have it. Note that we can also
300
* find the side by checking the side of new->node.node_p.
303
/* We need the common higher bits between new->key and old->key.
304
* What differences are there between new->key and the node here ?
305
* NOTE that bit(new) is always < bit(root) because highest
306
* bit of new->key and old->key are identical here (otherwise they
307
* would sit on different branches).
309
// note that if EB_NODE_BITS > 1, we should check that it's still >= 0
311
/* let the compiler choose the best branch based on the pointer size */
312
if (sizeof(ptr_t) == 4)
313
new->node.bit = flsnz((ptr_t)new->key ^ (ptr_t)old->key) - EB_NODE_BITS;
315
new->node.bit = fls64((ptr_t)new->key ^ (ptr_t)old->key) - EB_NODE_BITS;
316
root->b[side] = eb_dotag(&new->node.branches, EB_NODE);
321
#endif /* _COMMON_EBPTTREE_H */