blob: 4908f810529c7582bbadccc7b4dbb36b6ab63a3c [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
20#include "ebtree.h"
21
22
23/* Return the structure of type <type> whose member <member> points to <ptr> */
24#define ebpt_entry(ptr, type, member) container_of(ptr, type, member)
25
26#define EBPT_ROOT EB_ROOT
27#define EBPT_TREE_HEAD EB_TREE_HEAD
28
29/* on *almost* all platforms, a pointer can be cast into a size_t which is unsigned */
30#ifndef PTR_INT_TYPE
31#define PTR_INT_TYPE size_t
32#endif
33
34typedef PTR_INT_TYPE ptr_t;
35
36/* This structure carries a node, a leaf, and a key. It must start with the
37 * eb_node so that it can be cast into an eb_node. We could also have put some
38 * sort of transparent union here to reduce the indirection level, but the fact
39 * is, the end user is not meant to manipulate internals, so this is pointless.
40 */
41struct ebpt_node {
42 struct eb_node node; /* the tree node, must be at the beginning */
43 void *key;
44};
45
46/*
47 * Exported functions and macros.
48 * Many of them are always inlined because they are extremely small, and
49 * are generally called at most once or twice in a program.
50 */
51
52/* Return leftmost node in the tree, or NULL if none */
53static inline struct ebpt_node *ebpt_first(struct eb_root *root)
54{
55 return ebpt_entry(eb_first(root), struct ebpt_node, node);
56}
57
58/* Return rightmost node in the tree, or NULL if none */
59static inline struct ebpt_node *ebpt_last(struct eb_root *root)
60{
61 return ebpt_entry(eb_last(root), struct ebpt_node, node);
62}
63
64/* Return next node in the tree, or NULL if none */
65static inline struct ebpt_node *ebpt_next(struct ebpt_node *ebpt)
66{
67 return ebpt_entry(eb_next(&ebpt->node), struct ebpt_node, node);
68}
69
70/* Return previous node in the tree, or NULL if none */
71static inline struct ebpt_node *ebpt_prev(struct ebpt_node *ebpt)
72{
73 return ebpt_entry(eb_prev(&ebpt->node), struct ebpt_node, node);
74}
75
76/* Return next node in the tree, skipping duplicates, or NULL if none */
77static inline struct ebpt_node *ebpt_next_unique(struct ebpt_node *ebpt)
78{
79 return ebpt_entry(eb_next_unique(&ebpt->node), struct ebpt_node, node);
80}
81
82/* Return previous node in the tree, skipping duplicates, or NULL if none */
83static inline struct ebpt_node *ebpt_prev_unique(struct ebpt_node *ebpt)
84{
85 return ebpt_entry(eb_prev_unique(&ebpt->node), struct ebpt_node, node);
86}
87
88/* Delete node from the tree if it was linked in. Mark the node unused. Note
89 * that this function relies on a non-inlined generic function: eb_delete.
90 */
91static inline void ebpt_delete(struct ebpt_node *ebpt)
92{
93 eb_delete(&ebpt->node);
94}
95
96/*
97 * The following functions are not inlined by default. They are declared
98 * in ebpttree.c, which simply relies on their inline version.
99 */
100REGPRM2 struct ebpt_node *ebpt_lookup(struct eb_root *root, void *x);
101REGPRM2 struct ebpt_node *ebpt_insert(struct eb_root *root, struct ebpt_node *new);
102
103/*
104 * The following functions are less likely to be used directly, because their
105 * code is larger. The non-inlined version is preferred.
106 */
107
108/* Delete node from the tree if it was linked in. Mark the node unused. */
109static inline void __ebpt_delete(struct ebpt_node *ebpt)
110{
111 __eb_delete(&ebpt->node);
112}
113
114/*
115 * Find the first occurence of a key in the tree <root>. If none can be
116 * found, return NULL.
117 */
118static inline struct ebpt_node *__ebpt_lookup(struct eb_root *root, void *x)
119{
120 struct ebpt_node *node;
121 eb_troot_t *troot;
122
123 troot = root->b[EB_LEFT];
124 if (unlikely(troot == NULL))
125 return NULL;
126
127 while (1) {
128 if ((eb_gettag(troot) == EB_LEAF)) {
129 node = container_of(eb_untag(troot, EB_LEAF),
130 struct ebpt_node, node.branches);
131 if (node->key == x)
132 return node;
133 else
134 return NULL;
135 }
136 node = container_of(eb_untag(troot, EB_NODE),
137 struct ebpt_node, node.branches);
138
139 if (x == node->key) {
140 /* Either we found the node which holds the key, or
141 * we have a dup tree. In the later case, we have to
142 * walk it down left to get the first entry.
143 */
144 if (node->node.bit < 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 ebpt_node, node.branches);
150 }
151 return node;
152 }
153
154 troot = node->node.branches.b[((ptr_t)x >> node->node.bit) & EB_NODE_BRANCH_MASK];
155 }
156}
157
158/* Insert ebpt_node <new> into subtree starting at node root <root>.
159 * Only new->key needs be set with the key. The ebpt_node is returned.
160 */
161static inline struct ebpt_node *
162__ebpt_insert(struct eb_root *root, struct ebpt_node *new) {
163 struct ebpt_node *old;
164 unsigned int side;
165 eb_troot_t *troot;
166 void *newkey; /* caching the key saves approximately one cycle */
167
168 side = EB_LEFT;
169 troot = root->b[EB_LEFT];
170 if (unlikely(troot == NULL)) {
171 /* Tree is empty, insert the leaf part below the left branch */
172 root->b[EB_LEFT] = eb_dotag(&new->node.branches, EB_LEAF);
173 new->node.leaf_p = eb_dotag(root, EB_LEFT);
174 new->node.node_p = NULL; /* node part unused */
175 return new;
176 }
177
178 /* The tree descent is fairly easy :
179 * - first, check if we have reached a leaf node
180 * - second, check if we have gone too far
181 * - third, reiterate
182 * Everywhere, we use <new> for the node node we are inserting, <root>
183 * for the node we attach it to, and <old> for the node we are
184 * displacing below <new>. <troot> will always point to the future node
185 * (tagged with its type). <side> carries the side the node <new> is
186 * attached to below its parent, which is also where previous node
187 * was attached. <newkey> carries the key being inserted.
188 */
189 newkey = new->key;
190
191 while (1) {
192 if (unlikely(eb_gettag(troot) == EB_LEAF)) {
193 eb_troot_t *new_left, *new_rght;
194 eb_troot_t *new_leaf, *old_leaf;
195
196 old = container_of(eb_untag(troot, EB_LEAF),
197 struct ebpt_node, node.branches);
198
199 new_left = eb_dotag(&new->node.branches, EB_LEFT);
200 new_rght = eb_dotag(&new->node.branches, EB_RGHT);
201 new_leaf = eb_dotag(&new->node.branches, EB_LEAF);
202 old_leaf = eb_dotag(&old->node.branches, EB_LEAF);
203
204 new->node.node_p = old->node.leaf_p;
205
206 /* Right here, we have 3 possibilities :
207 - the tree does not contain the key, and we have
208 new->key < old->key. We insert new above old, on
209 the left ;
210
211 - the tree does not contain the key, and we have
212 new->key > old->key. We insert new above old, on
213 the right ;
214
215 - the tree does contain the key, which implies it
216 is alone. We add the new key next to it as a
217 first duplicate.
218
219 The last two cases can easily be partially merged.
220 */
221
222 if (new->key < old->key) {
223 new->node.leaf_p = new_left;
224 old->node.leaf_p = new_rght;
225 new->node.branches.b[EB_LEFT] = new_leaf;
226 new->node.branches.b[EB_RGHT] = old_leaf;
227 } else {
228 /* new->key >= old->key, new goes the right */
229 old->node.leaf_p = new_left;
230 new->node.leaf_p = new_rght;
231 new->node.branches.b[EB_LEFT] = old_leaf;
232 new->node.branches.b[EB_RGHT] = new_leaf;
233
234 if (new->key == old->key) {
235 new->node.bit = -1;
236 root->b[side] = eb_dotag(&new->node.branches, EB_NODE);
237 return new;
238 }
239 }
240 break;
241 }
242
243 /* OK we're walking down this link */
244 old = container_of(eb_untag(troot, EB_NODE),
245 struct ebpt_node, node.branches);
246
247 /* Stop going down when we don't have common bits anymore. We
248 * also stop in front of a duplicates tree because it means we
249 * have to insert above.
250 */
251
252 if ((old->node.bit < 0) || /* we're above a duplicate tree, stop here */
253 ((((ptr_t)new->key ^ (ptr_t)old->key) >> old->node.bit) >= EB_NODE_BRANCHES)) {
254 /* The tree did not contain the key, so we insert <new> before the node
255 * <old>, and set ->bit to designate the lowest bit position in <new>
256 * which applies to ->branches.b[].
257 */
258 eb_troot_t *new_left, *new_rght;
259 eb_troot_t *new_leaf, *old_node;
260
261 new_left = eb_dotag(&new->node.branches, EB_LEFT);
262 new_rght = eb_dotag(&new->node.branches, EB_RGHT);
263 new_leaf = eb_dotag(&new->node.branches, EB_LEAF);
264 old_node = eb_dotag(&old->node.branches, EB_NODE);
265
266 new->node.node_p = old->node.node_p;
267
268 if (new->key < old->key) {
269 new->node.leaf_p = new_left;
270 old->node.node_p = new_rght;
271 new->node.branches.b[EB_LEFT] = new_leaf;
272 new->node.branches.b[EB_RGHT] = old_node;
273 }
274 else if (new->key > old->key) {
275 old->node.node_p = new_left;
276 new->node.leaf_p = new_rght;
277 new->node.branches.b[EB_LEFT] = old_node;
278 new->node.branches.b[EB_RGHT] = new_leaf;
279 }
280 else {
281 struct eb_node *ret;
282 ret = eb_insert_dup(&old->node, &new->node);
283 return container_of(ret, struct ebpt_node, node);
284 }
285 break;
286 }
287
288 /* walk down */
289 root = &old->node.branches;
290 side = ((ptr_t)newkey >> old->node.bit) & EB_NODE_BRANCH_MASK;
291 troot = root->b[side];
292 }
293
294 /* Ok, now we are inserting <new> between <root> and <old>. <old>'s
295 * parent is already set to <new>, and the <root>'s branch is still in
296 * <side>. Update the root's leaf till we have it. Note that we can also
297 * find the side by checking the side of new->node.node_p.
298 */
299
300 /* We need the common higher bits between new->key and old->key.
301 * What differences are there between new->key and the node here ?
302 * NOTE that bit(new) is always < bit(root) because highest
303 * bit of new->key and old->key are identical here (otherwise they
304 * would sit on different branches).
305 */
306 // note that if EB_NODE_BITS > 1, we should check that it's still >= 0
307
308 /* let the compiler choose the best branch based on the pointer size */
309 if (sizeof(ptr_t) == 4)
310 new->node.bit = flsnz((ptr_t)new->key ^ (ptr_t)old->key) - EB_NODE_BITS;
311 else
312 new->node.bit = fls64((ptr_t)new->key ^ (ptr_t)old->key) - EB_NODE_BITS;
313 root->b[side] = eb_dotag(&new->node.branches, EB_NODE);
314
315 return new;
316}
317