2
* Elastic Binary Trees - macros and structures for Multi-Byte data nodes.
4
* (C) 2002-2009 - 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
27
/* Return the structure of type <type> whose member <member> points to <ptr> */
28
#define ebmb_entry(ptr, type, member) container_of(ptr, type, member)
30
#define EBMB_ROOT EB_ROOT
31
#define EBMB_TREE_HEAD EB_TREE_HEAD
33
/* This structure carries a node, a leaf, and a key. It must start with the
34
* eb_node so that it can be cast into an eb_node. We could also have put some
35
* sort of transparent union here to reduce the indirection level, but the fact
36
* is, the end user is not meant to manipulate internals, so this is pointless.
37
* The 'node.bit' value here works differently from scalar types, as it contains
38
* the number of identical bits between the two branches.
41
struct eb_node node; /* the tree node, must be at the beginning */
42
unsigned char key[0]; /* the key, its size depends on the application */
46
* Exported functions and macros.
47
* Many of them are always inlined because they are extremely small, and
48
* are generally called at most once or twice in a program.
51
/* Return leftmost node in the tree, or NULL if none */
52
static forceinline struct ebmb_node *ebmb_first(struct eb_root *root)
54
return ebmb_entry(eb_first(root), struct ebmb_node, node);
57
/* Return rightmost node in the tree, or NULL if none */
58
static forceinline struct ebmb_node *ebmb_last(struct eb_root *root)
60
return ebmb_entry(eb_last(root), struct ebmb_node, node);
63
/* Return next node in the tree, or NULL if none */
64
static forceinline struct ebmb_node *ebmb_next(struct ebmb_node *ebmb)
66
return ebmb_entry(eb_next(&ebmb->node), struct ebmb_node, node);
69
/* Return previous node in the tree, or NULL if none */
70
static forceinline struct ebmb_node *ebmb_prev(struct ebmb_node *ebmb)
72
return ebmb_entry(eb_prev(&ebmb->node), struct ebmb_node, node);
75
/* Return next node in the tree, skipping duplicates, or NULL if none */
76
static forceinline struct ebmb_node *ebmb_next_unique(struct ebmb_node *ebmb)
78
return ebmb_entry(eb_next_unique(&ebmb->node), struct ebmb_node, node);
81
/* Return previous node in the tree, skipping duplicates, or NULL if none */
82
static forceinline struct ebmb_node *ebmb_prev_unique(struct ebmb_node *ebmb)
84
return ebmb_entry(eb_prev_unique(&ebmb->node), struct ebmb_node, node);
87
/* Delete node from the tree if it was linked in. Mark the node unused. Note
88
* that this function relies on a non-inlined generic function: eb_delete.
90
static forceinline void ebmb_delete(struct ebmb_node *ebmb)
92
eb_delete(&ebmb->node);
95
/* The following functions are not inlined by default. They are declared
96
* in ebmbtree.c, which simply relies on their inline version.
98
REGPRM3 struct ebmb_node *ebmb_lookup(struct eb_root *root, const void *x, unsigned int len);
99
REGPRM3 struct ebmb_node *ebmb_insert(struct eb_root *root, struct ebmb_node *new, unsigned int len);
101
/* The following functions are less likely to be used directly, because their
102
* code is larger. The non-inlined version is preferred.
105
/* Delete node from the tree if it was linked in. Mark the node unused. */
106
static forceinline void __ebmb_delete(struct ebmb_node *ebmb)
108
__eb_delete(&ebmb->node);
111
/* Find the first occurence of a key of <len> bytes in the tree <root>.
112
* If none can be found, return NULL.
114
static forceinline struct ebmb_node *__ebmb_lookup(struct eb_root *root, const void *x, unsigned int len)
116
struct ebmb_node *node;
120
troot = root->b[EB_LEFT];
121
if (unlikely(troot == NULL))
126
if ((eb_gettag(troot) == EB_LEAF)) {
127
node = container_of(eb_untag(troot, EB_LEAF),
128
struct ebmb_node, node.branches);
129
if (memcmp(node->key, x, len) == 0)
134
node = container_of(eb_untag(troot, EB_NODE),
135
struct ebmb_node, node.branches);
137
if (node->node.bit < 0) {
138
/* We have a dup tree now. Either it's for the same
139
* value, and we walk down left, or it's a different
140
* one and we don't have our key.
142
if (memcmp(node->key, x, len) != 0)
145
troot = node->node.branches.b[EB_LEFT];
146
while (eb_gettag(troot) != EB_LEAF)
147
troot = (eb_untag(troot, EB_NODE))->b[EB_LEFT];
148
node = container_of(eb_untag(troot, EB_LEAF),
149
struct ebmb_node, node.branches);
153
/* OK, normal data node, let's walk down */
154
bit = equal_bits(x, node->key, bit, node->node.bit);
155
if (bit < node->node.bit)
156
return NULL; /* no more common bits */
158
troot = node->node.branches.b[(((unsigned char*)x)[node->node.bit >> 3] >>
159
(~node->node.bit & 7)) & 1];
163
/* Insert ebmb_node <new> into subtree starting at node root <root>.
164
* Only new->key needs be set with the key. The ebmb_node is returned.
165
* If root->b[EB_RGHT]==1, the tree may only contain unique keys. The
166
* len is specified in bytes.
168
static forceinline struct ebmb_node *
169
__ebmb_insert(struct eb_root *root, struct ebmb_node *new, unsigned int len)
171
struct ebmb_node *old;
174
eb_troot_t *root_right = root;
179
troot = root->b[EB_LEFT];
180
root_right = root->b[EB_RGHT];
181
if (unlikely(troot == NULL)) {
182
/* Tree is empty, insert the leaf part below the left branch */
183
root->b[EB_LEFT] = eb_dotag(&new->node.branches, EB_LEAF);
184
new->node.leaf_p = eb_dotag(root, EB_LEFT);
185
new->node.node_p = NULL; /* node part unused */
191
/* The tree descent is fairly easy :
192
* - first, check if we have reached a leaf node
193
* - second, check if we have gone too far
195
* Everywhere, we use <new> for the node node we are inserting, <root>
196
* for the node we attach it to, and <old> for the node we are
197
* displacing below <new>. <troot> will always point to the future node
198
* (tagged with its type). <side> carries the side the node <new> is
199
* attached to below its parent, which is also where previous node
205
if (unlikely(eb_gettag(troot) == EB_LEAF)) {
206
eb_troot_t *new_left, *new_rght;
207
eb_troot_t *new_leaf, *old_leaf;
209
old = container_of(eb_untag(troot, EB_LEAF),
210
struct ebmb_node, node.branches);
212
new_left = eb_dotag(&new->node.branches, EB_LEFT);
213
new_rght = eb_dotag(&new->node.branches, EB_RGHT);
214
new_leaf = eb_dotag(&new->node.branches, EB_LEAF);
215
old_leaf = eb_dotag(&old->node.branches, EB_LEAF);
217
new->node.node_p = old->node.leaf_p;
219
/* Right here, we have 3 possibilities :
220
* - the tree does not contain the key, and we have
221
* new->key < old->key. We insert new above old, on
224
* - the tree does not contain the key, and we have
225
* new->key > old->key. We insert new above old, on
228
* - the tree does contain the key, which implies it
229
* is alone. We add the new key next to it as a
232
* The last two cases can easily be partially merged.
234
bit = equal_bits(new->key, old->key, bit, len);
235
diff = cmp_bits(new->key, old->key, bit);
238
new->node.leaf_p = new_left;
239
old->node.leaf_p = new_rght;
240
new->node.branches.b[EB_LEFT] = new_leaf;
241
new->node.branches.b[EB_RGHT] = old_leaf;
243
/* we may refuse to duplicate this key if the tree is
244
* tagged as containing only unique keys.
246
if (diff == 0 && eb_gettag(root_right))
249
/* new->key >= old->key, new goes the right */
250
old->node.leaf_p = new_left;
251
new->node.leaf_p = new_rght;
252
new->node.branches.b[EB_LEFT] = old_leaf;
253
new->node.branches.b[EB_RGHT] = new_leaf;
257
root->b[side] = eb_dotag(&new->node.branches, EB_NODE);
264
/* OK we're walking down this link */
265
old = container_of(eb_untag(troot, EB_NODE),
266
struct ebmb_node, node.branches);
268
/* Stop going down when we don't have common bits anymore. We
269
* also stop in front of a duplicates tree because it means we
270
* have to insert above. Note: we can compare more bits than
271
* the current node's because as long as they are identical, we
272
* know we descend along the correct side.
274
if (old->node.bit < 0) {
275
/* we're above a duplicate tree, we must compare till the end */
276
bit = equal_bits(new->key, old->key, bit, len);
279
else if (bit < old->node.bit) {
280
bit = equal_bits(new->key, old->key, bit, old->node.bit);
283
if (bit < old->node.bit) { /* we don't have all bits in common */
284
/* The tree did not contain the key, so we insert <new> before the node
285
* <old>, and set ->bit to designate the lowest bit position in <new>
286
* which applies to ->branches.b[].
288
eb_troot_t *new_left, *new_rght;
289
eb_troot_t *new_leaf, *old_node;
292
new_left = eb_dotag(&new->node.branches, EB_LEFT);
293
new_rght = eb_dotag(&new->node.branches, EB_RGHT);
294
new_leaf = eb_dotag(&new->node.branches, EB_LEAF);
295
old_node = eb_dotag(&old->node.branches, EB_NODE);
297
new->node.node_p = old->node.node_p;
299
diff = cmp_bits(new->key, old->key, bit);
301
new->node.leaf_p = new_left;
302
old->node.node_p = new_rght;
303
new->node.branches.b[EB_LEFT] = new_leaf;
304
new->node.branches.b[EB_RGHT] = old_node;
307
old->node.node_p = new_left;
308
new->node.leaf_p = new_rght;
309
new->node.branches.b[EB_LEFT] = old_node;
310
new->node.branches.b[EB_RGHT] = new_leaf;
314
ret = eb_insert_dup(&old->node, &new->node);
315
return container_of(ret, struct ebmb_node, node);
321
root = &old->node.branches;
322
side = (new->key[old->node.bit >> 3] >> (~old->node.bit & 7)) & 1;
323
troot = root->b[side];
326
/* Ok, now we are inserting <new> between <root> and <old>. <old>'s
327
* parent is already set to <new>, and the <root>'s branch is still in
328
* <side>. Update the root's leaf till we have it. Note that we can also
329
* find the side by checking the side of new->node.node_p.
332
/* We need the common higher bits between new->key and old->key.
333
* This number of bits is already in <bit>.
336
root->b[side] = eb_dotag(&new->node.branches, EB_NODE);
340
#endif /* _EBMBTREE_H */