blob: d863f3ce896b3b3f40b1a31cec285cb46070527b [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;
126
127 troot = root->b[EB_LEFT];
128 if (unlikely(troot == NULL))
129 return NULL;
130
131 while (1) {
132 if ((eb_gettag(troot) == EB_LEAF)) {
133 node = container_of(eb_untag(troot, EB_LEAF),
134 struct ebpt_node, node.branches);
135 if (node->key == x)
136 return node;
137 else
138 return NULL;
139 }
140 node = container_of(eb_untag(troot, EB_NODE),
141 struct ebpt_node, node.branches);
142
143 if (x == node->key) {
144 /* Either we found the node which holds the key, or
145 * we have a dup tree. In the later case, we have to
146 * walk it down left to get the first entry.
147 */
148 if (node->node.bit < 0) {
149 troot = node->node.branches.b[EB_LEFT];
150 while (eb_gettag(troot) != EB_LEAF)
151 troot = (eb_untag(troot, EB_NODE))->b[EB_LEFT];
152 node = container_of(eb_untag(troot, EB_LEAF),
153 struct ebpt_node, node.branches);
154 }
155 return node;
156 }
157
158 troot = node->node.branches.b[((ptr_t)x >> node->node.bit) & EB_NODE_BRANCH_MASK];
159 }
160}
161
162/* Insert ebpt_node <new> into subtree starting at node root <root>.
163 * Only new->key needs be set with the key. The ebpt_node is returned.
Willy Tarreau1fb6c872008-05-16 19:48:20 +0200164 * If root->b[EB_RGHT]==1, the tree may only contain unique keys.
Willy Tarreaue6d2e4d2007-11-15 23:56:17 +0100165 */
Willy Tarreau75cf17e2008-08-29 15:48:49 +0200166static forceinline struct ebpt_node *
Willy Tarreaue6d2e4d2007-11-15 23:56:17 +0100167__ebpt_insert(struct eb_root *root, struct ebpt_node *new) {
168 struct ebpt_node *old;
169 unsigned int side;
170 eb_troot_t *troot;
171 void *newkey; /* caching the key saves approximately one cycle */
Willy Tarreau1fb6c872008-05-16 19:48:20 +0200172 eb_troot_t *root_right = root;
Willy Tarreaue6d2e4d2007-11-15 23:56:17 +0100173
174 side = EB_LEFT;
175 troot = root->b[EB_LEFT];
Willy Tarreau1fb6c872008-05-16 19:48:20 +0200176 root_right = root->b[EB_RGHT];
Willy Tarreaue6d2e4d2007-11-15 23:56:17 +0100177 if (unlikely(troot == NULL)) {
178 /* Tree is empty, insert the leaf part below the left branch */
179 root->b[EB_LEFT] = eb_dotag(&new->node.branches, EB_LEAF);
180 new->node.leaf_p = eb_dotag(root, EB_LEFT);
181 new->node.node_p = NULL; /* node part unused */
182 return new;
183 }
184
185 /* The tree descent is fairly easy :
186 * - first, check if we have reached a leaf node
187 * - second, check if we have gone too far
188 * - third, reiterate
189 * Everywhere, we use <new> for the node node we are inserting, <root>
190 * for the node we attach it to, and <old> for the node we are
191 * displacing below <new>. <troot> will always point to the future node
192 * (tagged with its type). <side> carries the side the node <new> is
193 * attached to below its parent, which is also where previous node
194 * was attached. <newkey> carries the key being inserted.
195 */
196 newkey = new->key;
197
198 while (1) {
199 if (unlikely(eb_gettag(troot) == EB_LEAF)) {
200 eb_troot_t *new_left, *new_rght;
201 eb_troot_t *new_leaf, *old_leaf;
202
203 old = container_of(eb_untag(troot, EB_LEAF),
204 struct ebpt_node, node.branches);
205
206 new_left = eb_dotag(&new->node.branches, EB_LEFT);
207 new_rght = eb_dotag(&new->node.branches, EB_RGHT);
208 new_leaf = eb_dotag(&new->node.branches, EB_LEAF);
209 old_leaf = eb_dotag(&old->node.branches, EB_LEAF);
210
211 new->node.node_p = old->node.leaf_p;
212
213 /* Right here, we have 3 possibilities :
214 - the tree does not contain the key, and we have
215 new->key < old->key. We insert new above old, on
216 the left ;
217
218 - the tree does not contain the key, and we have
219 new->key > old->key. We insert new above old, on
220 the right ;
221
222 - the tree does contain the key, which implies it
223 is alone. We add the new key next to it as a
224 first duplicate.
225
226 The last two cases can easily be partially merged.
227 */
228
229 if (new->key < old->key) {
230 new->node.leaf_p = new_left;
231 old->node.leaf_p = new_rght;
232 new->node.branches.b[EB_LEFT] = new_leaf;
233 new->node.branches.b[EB_RGHT] = old_leaf;
234 } else {
Willy Tarreau1fb6c872008-05-16 19:48:20 +0200235 /* we may refuse to duplicate this key if the tree is
236 * tagged as containing only unique keys.
237 */
238 if ((new->key == old->key) && eb_gettag(root_right))
239 return old;
240
Willy Tarreaue6d2e4d2007-11-15 23:56:17 +0100241 /* new->key >= old->key, new goes the right */
242 old->node.leaf_p = new_left;
243 new->node.leaf_p = new_rght;
244 new->node.branches.b[EB_LEFT] = old_leaf;
245 new->node.branches.b[EB_RGHT] = new_leaf;
246
247 if (new->key == old->key) {
248 new->node.bit = -1;
249 root->b[side] = eb_dotag(&new->node.branches, EB_NODE);
250 return new;
251 }
252 }
253 break;
254 }
255
256 /* OK we're walking down this link */
257 old = container_of(eb_untag(troot, EB_NODE),
258 struct ebpt_node, node.branches);
259
260 /* Stop going down when we don't have common bits anymore. We
261 * also stop in front of a duplicates tree because it means we
262 * have to insert above.
263 */
264
265 if ((old->node.bit < 0) || /* we're above a duplicate tree, stop here */
266 ((((ptr_t)new->key ^ (ptr_t)old->key) >> old->node.bit) >= EB_NODE_BRANCHES)) {
267 /* The tree did not contain the key, so we insert <new> before the node
268 * <old>, and set ->bit to designate the lowest bit position in <new>
269 * which applies to ->branches.b[].
270 */
271 eb_troot_t *new_left, *new_rght;
272 eb_troot_t *new_leaf, *old_node;
273
274 new_left = eb_dotag(&new->node.branches, EB_LEFT);
275 new_rght = eb_dotag(&new->node.branches, EB_RGHT);
276 new_leaf = eb_dotag(&new->node.branches, EB_LEAF);
277 old_node = eb_dotag(&old->node.branches, EB_NODE);
278
279 new->node.node_p = old->node.node_p;
280
281 if (new->key < old->key) {
282 new->node.leaf_p = new_left;
283 old->node.node_p = new_rght;
284 new->node.branches.b[EB_LEFT] = new_leaf;
285 new->node.branches.b[EB_RGHT] = old_node;
286 }
287 else if (new->key > old->key) {
288 old->node.node_p = new_left;
289 new->node.leaf_p = new_rght;
290 new->node.branches.b[EB_LEFT] = old_node;
291 new->node.branches.b[EB_RGHT] = new_leaf;
292 }
293 else {
294 struct eb_node *ret;
295 ret = eb_insert_dup(&old->node, &new->node);
296 return container_of(ret, struct ebpt_node, node);
297 }
298 break;
299 }
300
301 /* walk down */
302 root = &old->node.branches;
303 side = ((ptr_t)newkey >> old->node.bit) & EB_NODE_BRANCH_MASK;
304 troot = root->b[side];
305 }
306
307 /* Ok, now we are inserting <new> between <root> and <old>. <old>'s
308 * parent is already set to <new>, and the <root>'s branch is still in
309 * <side>. Update the root's leaf till we have it. Note that we can also
310 * find the side by checking the side of new->node.node_p.
311 */
312
313 /* We need the common higher bits between new->key and old->key.
314 * What differences are there between new->key and the node here ?
315 * NOTE that bit(new) is always < bit(root) because highest
316 * bit of new->key and old->key are identical here (otherwise they
317 * would sit on different branches).
318 */
319 // note that if EB_NODE_BITS > 1, we should check that it's still >= 0
320
321 /* let the compiler choose the best branch based on the pointer size */
322 if (sizeof(ptr_t) == 4)
323 new->node.bit = flsnz((ptr_t)new->key ^ (ptr_t)old->key) - EB_NODE_BITS;
324 else
325 new->node.bit = fls64((ptr_t)new->key ^ (ptr_t)old->key) - EB_NODE_BITS;
326 root->b[side] = eb_dotag(&new->node.branches, EB_NODE);
327
328 return new;
329}
330
Willy Tarreauf56fd8a2007-11-19 18:43:04 +0100331#endif /* _COMMON_EBPTTREE_H */