blob: 12b534dda620c2128808da46d2760ceed55300dc [file] [log] [blame]
Willy Tarreauc2186022009-10-26 19:48:54 +01001/*
2 * Elastic Binary Trees - macros and structures for Multi-Byte data nodes.
3 * Version 5.0
4 * (C) 2002-2009 - Willy Tarreau <w@1wt.eu>
5 *
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.
10 *
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.
15 *
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
19 */
20
Willy Tarreauead63a02009-11-02 14:41:23 +010021#ifndef _EBMBTREE_H
22#define _EBMBTREE_H
23
Willy Tarreauc2186022009-10-26 19:48:54 +010024#include <string.h>
25#include "ebtree.h"
26
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)
29
30#define EBMB_ROOT EB_ROOT
31#define EBMB_TREE_HEAD EB_TREE_HEAD
32
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.
39 */
40struct ebmb_node {
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 */
43};
44
45/*
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.
49 */
50
51/* Return leftmost node in the tree, or NULL if none */
52static forceinline struct ebmb_node *ebmb_first(struct eb_root *root)
53{
54 return ebmb_entry(eb_first(root), struct ebmb_node, node);
55}
56
57/* Return rightmost node in the tree, or NULL if none */
58static forceinline struct ebmb_node *ebmb_last(struct eb_root *root)
59{
60 return ebmb_entry(eb_last(root), struct ebmb_node, node);
61}
62
63/* Return next node in the tree, or NULL if none */
64static forceinline struct ebmb_node *ebmb_next(struct ebmb_node *ebmb)
65{
66 return ebmb_entry(eb_next(&ebmb->node), struct ebmb_node, node);
67}
68
69/* Return previous node in the tree, or NULL if none */
70static forceinline struct ebmb_node *ebmb_prev(struct ebmb_node *ebmb)
71{
72 return ebmb_entry(eb_prev(&ebmb->node), struct ebmb_node, node);
73}
74
75/* Return next node in the tree, skipping duplicates, or NULL if none */
76static forceinline struct ebmb_node *ebmb_next_unique(struct ebmb_node *ebmb)
77{
78 return ebmb_entry(eb_next_unique(&ebmb->node), struct ebmb_node, node);
79}
80
81/* Return previous node in the tree, skipping duplicates, or NULL if none */
82static forceinline struct ebmb_node *ebmb_prev_unique(struct ebmb_node *ebmb)
83{
84 return ebmb_entry(eb_prev_unique(&ebmb->node), struct ebmb_node, node);
85}
86
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.
89 */
90static forceinline void ebmb_delete(struct ebmb_node *ebmb)
91{
92 eb_delete(&ebmb->node);
93}
94
95/* The following functions are not inlined by default. They are declared
96 * in ebmbtree.c, which simply relies on their inline version.
97 */
98REGPRM3 struct ebmb_node *ebmb_lookup(struct eb_root *root, const void *x, unsigned int len);
99REGPRM3 struct ebmb_node *ebmb_insert(struct eb_root *root, struct ebmb_node *new, unsigned int len);
100
101/* The following functions are less likely to be used directly, because their
102 * code is larger. The non-inlined version is preferred.
103 */
104
105/* Delete node from the tree if it was linked in. Mark the node unused. */
106static forceinline void __ebmb_delete(struct ebmb_node *ebmb)
107{
108 __eb_delete(&ebmb->node);
109}
110
111/* Find the first occurence of a key of <len> bytes in the tree <root>.
112 * If none can be found, return NULL.
113 */
114static forceinline struct ebmb_node *__ebmb_lookup(struct eb_root *root, const void *x, unsigned int len)
115{
116 struct ebmb_node *node;
117 eb_troot_t *troot;
118 unsigned int bit;
119
120 troot = root->b[EB_LEFT];
121 if (unlikely(troot == NULL))
122 return NULL;
123
124 bit = 0;
125 while (1) {
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)
130 return node;
131 else
132 return NULL;
133 }
134 node = container_of(eb_untag(troot, EB_NODE),
135 struct ebmb_node, node.branches);
136
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.
141 */
142 if (memcmp(node->key, x, len) != 0)
143 return NULL;
144
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);
150 return node;
151 }
152
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 */
157
158 troot = node->node.branches.b[(((unsigned char*)x)[node->node.bit >> 3] >>
159 (~node->node.bit & 7)) & 1];
160 }
161}
162
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.
167 */
168static forceinline struct ebmb_node *
169__ebmb_insert(struct eb_root *root, struct ebmb_node *new, unsigned int len)
170{
171 struct ebmb_node *old;
172 unsigned int side;
173 eb_troot_t *troot;
174 eb_troot_t *root_right = root;
175 int diff;
176 int bit;
177
178 side = EB_LEFT;
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 */
186 return new;
187 }
188
189 len <<= 3;
190
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
194 * - third, reiterate
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
200 * was attached.
201 */
202
203 bit = 0;
204 while (1) {
205 if (unlikely(eb_gettag(troot) == EB_LEAF)) {
206 eb_troot_t *new_left, *new_rght;
207 eb_troot_t *new_leaf, *old_leaf;
208
209 old = container_of(eb_untag(troot, EB_LEAF),
210 struct ebmb_node, node.branches);
211
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);
216
217 new->node.node_p = old->node.leaf_p;
218
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
222 * the left ;
223 *
224 * - the tree does not contain the key, and we have
225 * new->key > old->key. We insert new above old, on
226 * the right ;
227 *
228 * - the tree does contain the key, which implies it
229 * is alone. We add the new key next to it as a
230 * first duplicate.
231 *
232 * The last two cases can easily be partially merged.
233 */
234 bit = equal_bits(new->key, old->key, bit, len);
235 diff = cmp_bits(new->key, old->key, bit);
236
237 if (diff < 0) {
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;
242 } else {
243 /* we may refuse to duplicate this key if the tree is
244 * tagged as containing only unique keys.
245 */
246 if (diff == 0 && eb_gettag(root_right))
247 return old;
248
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;
254
255 if (diff == 0) {
256 new->node.bit = -1;
257 root->b[side] = eb_dotag(&new->node.branches, EB_NODE);
258 return new;
259 }
260 }
261 break;
262 }
263
264 /* OK we're walking down this link */
265 old = container_of(eb_untag(troot, EB_NODE),
266 struct ebmb_node, node.branches);
267
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.
273 */
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);
277 goto dup_tree;
278 }
279 else if (bit < old->node.bit) {
280 bit = equal_bits(new->key, old->key, bit, old->node.bit);
281 }
282
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[].
287 */
288 eb_troot_t *new_left, *new_rght;
289 eb_troot_t *new_leaf, *old_node;
290
291 dup_tree:
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);
296
297 new->node.node_p = old->node.node_p;
298
299 diff = cmp_bits(new->key, old->key, bit);
300 if (diff < 0) {
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;
305 }
306 else if (diff > 0) {
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;
311 }
312 else {
313 struct eb_node *ret;
314 ret = eb_insert_dup(&old->node, &new->node);
315 return container_of(ret, struct ebmb_node, node);
316 }
317 break;
318 }
319
320 /* walk down */
321 root = &old->node.branches;
322 side = (new->key[old->node.bit >> 3] >> (~old->node.bit & 7)) & 1;
323 troot = root->b[side];
324 }
325
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.
330 */
331
332 /* We need the common higher bits between new->key and old->key.
333 * This number of bits is already in <bit>.
334 */
335 new->node.bit = bit;
336 root->b[side] = eb_dotag(&new->node.branches, EB_NODE);
337 return new;
338}
339
Willy Tarreauead63a02009-11-02 14:41:23 +0100340#endif /* _EBMBTREE_H */
341