blob: d1dbcfd669c310a1c527769f9ef83f69fbfd0337 [file] [log] [blame]
Willy Tarreaue6d2e4d2007-11-15 23:56:17 +01001/*
2 * Elastic Binary Trees - macros and structures for operations on pointer nodes.
Willy Tarreau1fb6c872008-05-16 19:48:20 +02003 * Version 4.0
4 * (C) 2002-2008 - Willy Tarreau <w@1wt.eu>
Willy Tarreaue6d2e4d2007-11-15 23:56:17 +01005 *
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 Tarreauf56fd8a2007-11-19 18:43:04 +010021#ifndef _COMMON_EBPTTREE_H
22#define _COMMON_EBPTTREE_H
23
Willy Tarreaue6d2e4d2007-11-15 23:56:17 +010024#include "ebtree.h"
25
26
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)
29
30#define EBPT_ROOT EB_ROOT
31#define EBPT_TREE_HEAD EB_TREE_HEAD
32
33/* on *almost* all platforms, a pointer can be cast into a size_t which is unsigned */
34#ifndef PTR_INT_TYPE
35#define PTR_INT_TYPE size_t
36#endif
37
38typedef PTR_INT_TYPE ptr_t;
39
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.
44 */
45struct ebpt_node {
46 struct eb_node node; /* the tree node, must be at the beginning */
47 void *key;
48};
49
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 ebpt_node *ebpt_first(struct eb_root *root)
58{
59 return ebpt_entry(eb_first(root), struct ebpt_node, node);
60}
61
62/* Return rightmost node in the tree, or NULL if none */
63static inline struct ebpt_node *ebpt_last(struct eb_root *root)
64{
65 return ebpt_entry(eb_last(root), struct ebpt_node, node);
66}
67
68/* Return next node in the tree, or NULL if none */
69static inline struct ebpt_node *ebpt_next(struct ebpt_node *ebpt)
70{
71 return ebpt_entry(eb_next(&ebpt->node), struct ebpt_node, node);
72}
73
74/* Return previous node in the tree, or NULL if none */
75static inline struct ebpt_node *ebpt_prev(struct ebpt_node *ebpt)
76{
77 return ebpt_entry(eb_prev(&ebpt->node), struct ebpt_node, node);
78}
79
80/* Return next node in the tree, skipping duplicates, or NULL if none */
81static inline struct ebpt_node *ebpt_next_unique(struct ebpt_node *ebpt)
82{
83 return ebpt_entry(eb_next_unique(&ebpt->node), struct ebpt_node, node);
84}
85
86/* Return previous node in the tree, skipping duplicates, or NULL if none */
87static inline struct ebpt_node *ebpt_prev_unique(struct ebpt_node *ebpt)
88{
89 return ebpt_entry(eb_prev_unique(&ebpt->node), struct ebpt_node, node);
90}
91
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.
94 */
95static inline void ebpt_delete(struct ebpt_node *ebpt)
96{
97 eb_delete(&ebpt->node);
98}
99
100/*
101 * The following functions are not inlined by default. They are declared
102 * in ebpttree.c, which simply relies on their inline version.
103 */
104REGPRM2 struct ebpt_node *ebpt_lookup(struct eb_root *root, void *x);
105REGPRM2 struct ebpt_node *ebpt_insert(struct eb_root *root, struct ebpt_node *new);
106
107/*
108 * The following functions are less likely to be used directly, because their
109 * code is larger. The non-inlined version is preferred.
110 */
111
112/* Delete node from the tree if it was linked in. Mark the node unused. */
Willy Tarreau75cf17e2008-08-29 15:48:49 +0200113static forceinline void __ebpt_delete(struct ebpt_node *ebpt)
Willy Tarreaue6d2e4d2007-11-15 23:56:17 +0100114{
115 __eb_delete(&ebpt->node);
116}
117
118/*
119 * Find the first occurence of a key in the tree <root>. If none can be
120 * found, return NULL.
121 */
Willy Tarreau75cf17e2008-08-29 15:48:49 +0200122static forceinline struct ebpt_node *__ebpt_lookup(struct eb_root *root, void *x)
Willy Tarreaue6d2e4d2007-11-15 23:56:17 +0100123{
124 struct ebpt_node *node;
125 eb_troot_t *troot;
Willy Tarreau58044342009-03-21 07:40:32 +0100126 ptr_t y;
Willy Tarreaue6d2e4d2007-11-15 23:56:17 +0100127
128 troot = root->b[EB_LEFT];
129 if (unlikely(troot == NULL))
130 return NULL;
131
132 while (1) {
133 if ((eb_gettag(troot) == EB_LEAF)) {
134 node = container_of(eb_untag(troot, EB_LEAF),
135 struct ebpt_node, node.branches);
136 if (node->key == x)
137 return node;
138 else
139 return NULL;
140 }
141 node = container_of(eb_untag(troot, EB_NODE),
142 struct ebpt_node, node.branches);
143
Willy Tarreau58044342009-03-21 07:40:32 +0100144 y = (ptr_t)node->key ^ (ptr_t)x;
145 if (!y) {
Willy Tarreaue6d2e4d2007-11-15 23:56:17 +0100146 /* 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.
149 */
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);
156 }
157 return node;
158 }
159
Willy Tarreau58044342009-03-21 07:40:32 +0100160 if ((y >> node->node.bit) >= EB_NODE_BRANCHES)
161 return NULL; /* no more common bits */
162
Willy Tarreaue6d2e4d2007-11-15 23:56:17 +0100163 troot = node->node.branches.b[((ptr_t)x >> node->node.bit) & EB_NODE_BRANCH_MASK];
164 }
165}
166
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.
Willy Tarreau1fb6c872008-05-16 19:48:20 +0200169 * If root->b[EB_RGHT]==1, the tree may only contain unique keys.
Willy Tarreaue6d2e4d2007-11-15 23:56:17 +0100170 */
Willy Tarreau75cf17e2008-08-29 15:48:49 +0200171static forceinline struct ebpt_node *
Willy Tarreaue6d2e4d2007-11-15 23:56:17 +0100172__ebpt_insert(struct eb_root *root, struct ebpt_node *new) {
173 struct ebpt_node *old;
174 unsigned int side;
175 eb_troot_t *troot;
176 void *newkey; /* caching the key saves approximately one cycle */
Willy Tarreau1fb6c872008-05-16 19:48:20 +0200177 eb_troot_t *root_right = root;
Willy Tarreaue6d2e4d2007-11-15 23:56:17 +0100178
179 side = EB_LEFT;
180 troot = root->b[EB_LEFT];
Willy Tarreau1fb6c872008-05-16 19:48:20 +0200181 root_right = root->b[EB_RGHT];
Willy Tarreaue6d2e4d2007-11-15 23:56:17 +0100182 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 */
187 return new;
188 }
189
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
193 * - third, reiterate
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.
200 */
201 newkey = new->key;
202
203 while (1) {
204 if (unlikely(eb_gettag(troot) == EB_LEAF)) {
205 eb_troot_t *new_left, *new_rght;
206 eb_troot_t *new_leaf, *old_leaf;
207
208 old = container_of(eb_untag(troot, EB_LEAF),
209 struct ebpt_node, node.branches);
210
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);
215
216 new->node.node_p = old->node.leaf_p;
217
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
221 the left ;
222
223 - the tree does not contain the key, and we have
224 new->key > old->key. We insert new above old, on
225 the right ;
226
227 - the tree does contain the key, which implies it
228 is alone. We add the new key next to it as a
229 first duplicate.
230
231 The last two cases can easily be partially merged.
232 */
233
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;
239 } else {
Willy Tarreau1fb6c872008-05-16 19:48:20 +0200240 /* we may refuse to duplicate this key if the tree is
241 * tagged as containing only unique keys.
242 */
243 if ((new->key == old->key) && eb_gettag(root_right))
244 return old;
245
Willy Tarreaue6d2e4d2007-11-15 23:56:17 +0100246 /* 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;
251
252 if (new->key == old->key) {
253 new->node.bit = -1;
254 root->b[side] = eb_dotag(&new->node.branches, EB_NODE);
255 return new;
256 }
257 }
258 break;
259 }
260
261 /* OK we're walking down this link */
262 old = container_of(eb_untag(troot, EB_NODE),
263 struct ebpt_node, node.branches);
264
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.
268 */
269
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[].
275 */
276 eb_troot_t *new_left, *new_rght;
277 eb_troot_t *new_leaf, *old_node;
278
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);
283
284 new->node.node_p = old->node.node_p;
285
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;
291 }
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;
297 }
298 else {
299 struct eb_node *ret;
300 ret = eb_insert_dup(&old->node, &new->node);
301 return container_of(ret, struct ebpt_node, node);
302 }
303 break;
304 }
305
306 /* walk down */
307 root = &old->node.branches;
308 side = ((ptr_t)newkey >> old->node.bit) & EB_NODE_BRANCH_MASK;
309 troot = root->b[side];
310 }
311
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.
316 */
317
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).
323 */
324 // note that if EB_NODE_BITS > 1, we should check that it's still >= 0
325
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;
329 else
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);
332
333 return new;
334}
335
Willy Tarreauf56fd8a2007-11-19 18:43:04 +0100336#endif /* _COMMON_EBPTTREE_H */