blob: be164ad4c8eec1fc22de650552e1d7383ad84929 [file] [log] [blame]
Willy Tarreaue6d2e4d2007-11-15 23:56:17 +01001/*
2 * Elastic Binary Trees - macros and structures for operations on pointer nodes.
3 * (C) 2002-2007 - Willy Tarreau <w@1wt.eu>
4 *
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.
9 *
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.
14 *
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
18 */
19
Willy Tarreauf56fd8a2007-11-19 18:43:04 +010020#ifndef _COMMON_EBPTTREE_H
21#define _COMMON_EBPTTREE_H
22
Willy Tarreaue6d2e4d2007-11-15 23:56:17 +010023#include "ebtree.h"
24
25
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)
28
29#define EBPT_ROOT EB_ROOT
30#define EBPT_TREE_HEAD EB_TREE_HEAD
31
32/* on *almost* all platforms, a pointer can be cast into a size_t which is unsigned */
33#ifndef PTR_INT_TYPE
34#define PTR_INT_TYPE size_t
35#endif
36
37typedef PTR_INT_TYPE ptr_t;
38
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.
43 */
44struct ebpt_node {
45 struct eb_node node; /* the tree node, must be at the beginning */
46 void *key;
47};
48
49/*
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.
53 */
54
55/* Return leftmost node in the tree, or NULL if none */
56static inline struct ebpt_node *ebpt_first(struct eb_root *root)
57{
58 return ebpt_entry(eb_first(root), struct ebpt_node, node);
59}
60
61/* Return rightmost node in the tree, or NULL if none */
62static inline struct ebpt_node *ebpt_last(struct eb_root *root)
63{
64 return ebpt_entry(eb_last(root), struct ebpt_node, node);
65}
66
67/* Return next node in the tree, or NULL if none */
68static inline struct ebpt_node *ebpt_next(struct ebpt_node *ebpt)
69{
70 return ebpt_entry(eb_next(&ebpt->node), struct ebpt_node, node);
71}
72
73/* Return previous node in the tree, or NULL if none */
74static inline struct ebpt_node *ebpt_prev(struct ebpt_node *ebpt)
75{
76 return ebpt_entry(eb_prev(&ebpt->node), struct ebpt_node, node);
77}
78
79/* Return next node in the tree, skipping duplicates, or NULL if none */
80static inline struct ebpt_node *ebpt_next_unique(struct ebpt_node *ebpt)
81{
82 return ebpt_entry(eb_next_unique(&ebpt->node), struct ebpt_node, node);
83}
84
85/* Return previous node in the tree, skipping duplicates, or NULL if none */
86static inline struct ebpt_node *ebpt_prev_unique(struct ebpt_node *ebpt)
87{
88 return ebpt_entry(eb_prev_unique(&ebpt->node), struct ebpt_node, node);
89}
90
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.
93 */
94static inline void ebpt_delete(struct ebpt_node *ebpt)
95{
96 eb_delete(&ebpt->node);
97}
98
99/*
100 * The following functions are not inlined by default. They are declared
101 * in ebpttree.c, which simply relies on their inline version.
102 */
103REGPRM2 struct ebpt_node *ebpt_lookup(struct eb_root *root, void *x);
104REGPRM2 struct ebpt_node *ebpt_insert(struct eb_root *root, struct ebpt_node *new);
105
106/*
107 * The following functions are less likely to be used directly, because their
108 * code is larger. The non-inlined version is preferred.
109 */
110
111/* Delete node from the tree if it was linked in. Mark the node unused. */
112static inline void __ebpt_delete(struct ebpt_node *ebpt)
113{
114 __eb_delete(&ebpt->node);
115}
116
117/*
118 * Find the first occurence of a key in the tree <root>. If none can be
119 * found, return NULL.
120 */
121static inline struct ebpt_node *__ebpt_lookup(struct eb_root *root, void *x)
122{
123 struct ebpt_node *node;
124 eb_troot_t *troot;
125
126 troot = root->b[EB_LEFT];
127 if (unlikely(troot == NULL))
128 return NULL;
129
130 while (1) {
131 if ((eb_gettag(troot) == EB_LEAF)) {
132 node = container_of(eb_untag(troot, EB_LEAF),
133 struct ebpt_node, node.branches);
134 if (node->key == x)
135 return node;
136 else
137 return NULL;
138 }
139 node = container_of(eb_untag(troot, EB_NODE),
140 struct ebpt_node, node.branches);
141
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.
146 */
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);
153 }
154 return node;
155 }
156
157 troot = node->node.branches.b[((ptr_t)x >> node->node.bit) & EB_NODE_BRANCH_MASK];
158 }
159}
160
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.
163 */
164static inline struct ebpt_node *
165__ebpt_insert(struct eb_root *root, struct ebpt_node *new) {
166 struct ebpt_node *old;
167 unsigned int side;
168 eb_troot_t *troot;
169 void *newkey; /* caching the key saves approximately one cycle */
170
171 side = EB_LEFT;
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 */
178 return new;
179 }
180
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
184 * - third, reiterate
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.
191 */
192 newkey = new->key;
193
194 while (1) {
195 if (unlikely(eb_gettag(troot) == EB_LEAF)) {
196 eb_troot_t *new_left, *new_rght;
197 eb_troot_t *new_leaf, *old_leaf;
198
199 old = container_of(eb_untag(troot, EB_LEAF),
200 struct ebpt_node, node.branches);
201
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);
206
207 new->node.node_p = old->node.leaf_p;
208
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
212 the left ;
213
214 - the tree does not contain the key, and we have
215 new->key > old->key. We insert new above old, on
216 the right ;
217
218 - the tree does contain the key, which implies it
219 is alone. We add the new key next to it as a
220 first duplicate.
221
222 The last two cases can easily be partially merged.
223 */
224
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;
230 } else {
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;
236
237 if (new->key == old->key) {
238 new->node.bit = -1;
239 root->b[side] = eb_dotag(&new->node.branches, EB_NODE);
240 return new;
241 }
242 }
243 break;
244 }
245
246 /* OK we're walking down this link */
247 old = container_of(eb_untag(troot, EB_NODE),
248 struct ebpt_node, node.branches);
249
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.
253 */
254
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[].
260 */
261 eb_troot_t *new_left, *new_rght;
262 eb_troot_t *new_leaf, *old_node;
263
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);
268
269 new->node.node_p = old->node.node_p;
270
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;
276 }
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;
282 }
283 else {
284 struct eb_node *ret;
285 ret = eb_insert_dup(&old->node, &new->node);
286 return container_of(ret, struct ebpt_node, node);
287 }
288 break;
289 }
290
291 /* walk down */
292 root = &old->node.branches;
293 side = ((ptr_t)newkey >> old->node.bit) & EB_NODE_BRANCH_MASK;
294 troot = root->b[side];
295 }
296
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.
301 */
302
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).
308 */
309 // note that if EB_NODE_BITS > 1, we should check that it's still >= 0
310
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;
314 else
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);
317
318 return new;
319}
320
Willy Tarreauf56fd8a2007-11-19 18:43:04 +0100321#endif /* _COMMON_EBPTTREE_H */