blob: 1fe9fa0acba5caa80f240210f6344cf5dc8eb7ad [file] [log] [blame]
Willy Tarreauc2186022009-10-26 19:48:54 +01001/*
2 * Elastic Binary Trees - macros for Indirect Multi-Byte data nodes.
Willy Tarreauf3bfede2011-07-25 11:38:17 +02003 * Version 6.0.6
Willy Tarreau414c4b22011-01-04 13:21:06 +01004 * (C) 2002-2011 - Willy Tarreau <w@1wt.eu>
Willy Tarreauc2186022009-10-26 19:48:54 +01005 *
Willy Tarreauf3bfede2011-07-25 11:38:17 +02006 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation, version 2.1
9 * exclusively.
Willy Tarreauc2186022009-10-26 19:48:54 +010010 *
Willy Tarreauf3bfede2011-07-25 11:38:17 +020011 * This library is distributed in the hope that it will be useful,
Willy Tarreauc2186022009-10-26 19:48:54 +010012 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Willy Tarreauf3bfede2011-07-25 11:38:17 +020013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
Willy Tarreauc2186022009-10-26 19:48:54 +010015 *
Willy Tarreauf3bfede2011-07-25 11:38:17 +020016 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
Willy Tarreauc2186022009-10-26 19:48:54 +010019 */
20
21#include <string.h>
22#include "ebtree.h"
23#include "ebpttree.h"
24
25/* These functions and macros rely on Pointer nodes and use the <key> entry as
26 * a pointer to an indirect key. Most operations are performed using ebpt_*.
27 */
28
29/* The following functions are not inlined by default. They are declared
30 * in ebimtree.c, which simply relies on their inline version.
31 */
32REGPRM3 struct ebpt_node *ebim_lookup(struct eb_root *root, const void *x, unsigned int len);
33REGPRM3 struct ebpt_node *ebim_insert(struct eb_root *root, struct ebpt_node *new, unsigned int len);
34
Willy Tarreau414c4b22011-01-04 13:21:06 +010035/* Find the first occurence of a key of a least <len> bytes matching <x> in the
36 * tree <root>. The caller is responsible for ensuring that <len> will not exceed
37 * the common parts between the tree's keys and <x>. In case of multiple matches,
38 * the leftmost node is returned. This means that this function can be used to
39 * lookup string keys by prefix if all keys in the tree are zero-terminated. If
40 * no match is found, NULL is returned. Returns first node if <len> is zero.
Willy Tarreauc2186022009-10-26 19:48:54 +010041 */
42static forceinline struct ebpt_node *
43__ebim_lookup(struct eb_root *root, const void *x, unsigned int len)
44{
45 struct ebpt_node *node;
46 eb_troot_t *troot;
Willy Tarreau414c4b22011-01-04 13:21:06 +010047 int pos, side;
Willy Tarreau3a932442010-05-09 19:29:23 +020048 int node_bit;
Willy Tarreauc2186022009-10-26 19:48:54 +010049
50 troot = root->b[EB_LEFT];
51 if (unlikely(troot == NULL))
Willy Tarreauce3d44a2011-01-04 14:07:36 +010052 goto ret_null;
Willy Tarreauc2186022009-10-26 19:48:54 +010053
Willy Tarreau414c4b22011-01-04 13:21:06 +010054 if (unlikely(len == 0))
55 goto walk_down;
56
57 pos = 0;
Willy Tarreauc2186022009-10-26 19:48:54 +010058 while (1) {
Willy Tarreau414c4b22011-01-04 13:21:06 +010059 if (eb_gettag(troot) == EB_LEAF) {
Willy Tarreauc2186022009-10-26 19:48:54 +010060 node = container_of(eb_untag(troot, EB_LEAF),
61 struct ebpt_node, node.branches);
Willy Tarreau414c4b22011-01-04 13:21:06 +010062 if (memcmp(node->key + pos, x, len) != 0)
Willy Tarreauce3d44a2011-01-04 14:07:36 +010063 goto ret_null;
Willy Tarreau414c4b22011-01-04 13:21:06 +010064 else
Willy Tarreauce3d44a2011-01-04 14:07:36 +010065 goto ret_node;
Willy Tarreauc2186022009-10-26 19:48:54 +010066 }
67 node = container_of(eb_untag(troot, EB_NODE),
68 struct ebpt_node, node.branches);
69
Willy Tarreau3a932442010-05-09 19:29:23 +020070 node_bit = node->node.bit;
71 if (node_bit < 0) {
Willy Tarreauc2186022009-10-26 19:48:54 +010072 /* We have a dup tree now. Either it's for the same
73 * value, and we walk down left, or it's a different
74 * one and we don't have our key.
75 */
Willy Tarreau414c4b22011-01-04 13:21:06 +010076 if (memcmp(node->key + pos, x, len) != 0)
Willy Tarreauce3d44a2011-01-04 14:07:36 +010077 goto ret_null;
78 else
79 goto walk_left;
Willy Tarreauc2186022009-10-26 19:48:54 +010080 }
81
Willy Tarreau414c4b22011-01-04 13:21:06 +010082 /* OK, normal data node, let's walk down. We check if all full
83 * bytes are equal, and we start from the last one we did not
84 * completely check. We stop as soon as we reach the last byte,
85 * because we must decide to go left/right or abort.
86 */
87 node_bit = ~node_bit + (pos << 3) + 8; // = (pos<<3) + (7 - node_bit)
88 if (node_bit < 0) {
89 /* This surprizing construction gives better performance
90 * because gcc does not try to reorder the loop. Tested to
91 * be fine with 2.95 to 4.2.
92 */
93 while (1) {
94 if (*(unsigned char*)(node->key + pos++) ^ *(unsigned char*)(x++))
Willy Tarreauce3d44a2011-01-04 14:07:36 +010095 goto ret_null; /* more than one full byte is different */
Willy Tarreau414c4b22011-01-04 13:21:06 +010096 if (--len == 0)
97 goto walk_left; /* return first node if all bytes matched */
98 node_bit += 8;
99 if (node_bit >= 0)
100 break;
101 }
102 }
Willy Tarreauc2186022009-10-26 19:48:54 +0100103
Willy Tarreau414c4b22011-01-04 13:21:06 +0100104 /* here we know that only the last byte differs, so node_bit < 8.
105 * We have 2 possibilities :
106 * - more than the last bit differs => return NULL
107 * - walk down on side = (x[pos] >> node_bit) & 1
108 */
109 side = *(unsigned char *)x >> node_bit;
110 if (((*(unsigned char*)(node->key + pos) >> node_bit) ^ side) > 1)
Willy Tarreauce3d44a2011-01-04 14:07:36 +0100111 goto ret_null;
Willy Tarreau414c4b22011-01-04 13:21:06 +0100112 side &= 1;
113 troot = node->node.branches.b[side];
Willy Tarreauc2186022009-10-26 19:48:54 +0100114 }
Willy Tarreauce3d44a2011-01-04 14:07:36 +0100115 walk_left:
116 troot = node->node.branches.b[EB_LEFT];
117 walk_down:
118 while (eb_gettag(troot) != EB_LEAF)
119 troot = (eb_untag(troot, EB_NODE))->b[EB_LEFT];
120 node = container_of(eb_untag(troot, EB_LEAF),
121 struct ebpt_node, node.branches);
122 ret_node:
123 return node;
124 ret_null:
125 return NULL;
Willy Tarreauc2186022009-10-26 19:48:54 +0100126}
127
128/* Insert ebpt_node <new> into subtree starting at node root <root>.
129 * Only new->key needs be set with the key. The ebpt_node is returned.
130 * If root->b[EB_RGHT]==1, the tree may only contain unique keys. The
131 * len is specified in bytes.
132 */
133static forceinline struct ebpt_node *
134__ebim_insert(struct eb_root *root, struct ebpt_node *new, unsigned int len)
135{
136 struct ebpt_node *old;
137 unsigned int side;
138 eb_troot_t *troot;
Willy Tarreau6258f7b2011-09-19 20:48:00 +0200139 eb_troot_t *root_right;
Willy Tarreauc2186022009-10-26 19:48:54 +0100140 int diff;
141 int bit;
Willy Tarreau3a932442010-05-09 19:29:23 +0200142 int old_node_bit;
Willy Tarreauc2186022009-10-26 19:48:54 +0100143
144 side = EB_LEFT;
145 troot = root->b[EB_LEFT];
146 root_right = root->b[EB_RGHT];
147 if (unlikely(troot == NULL)) {
148 /* Tree is empty, insert the leaf part below the left branch */
149 root->b[EB_LEFT] = eb_dotag(&new->node.branches, EB_LEAF);
150 new->node.leaf_p = eb_dotag(root, EB_LEFT);
151 new->node.node_p = NULL; /* node part unused */
152 return new;
153 }
154
155 len <<= 3;
156
157 /* The tree descent is fairly easy :
158 * - first, check if we have reached a leaf node
159 * - second, check if we have gone too far
160 * - third, reiterate
161 * Everywhere, we use <new> for the node node we are inserting, <root>
162 * for the node we attach it to, and <old> for the node we are
163 * displacing below <new>. <troot> will always point to the future node
164 * (tagged with its type). <side> carries the side the node <new> is
165 * attached to below its parent, which is also where previous node
166 * was attached.
167 */
168
169 bit = 0;
170 while (1) {
171 if (unlikely(eb_gettag(troot) == EB_LEAF)) {
172 eb_troot_t *new_left, *new_rght;
173 eb_troot_t *new_leaf, *old_leaf;
174
175 old = container_of(eb_untag(troot, EB_LEAF),
176 struct ebpt_node, node.branches);
177
178 new_left = eb_dotag(&new->node.branches, EB_LEFT);
179 new_rght = eb_dotag(&new->node.branches, EB_RGHT);
180 new_leaf = eb_dotag(&new->node.branches, EB_LEAF);
181 old_leaf = eb_dotag(&old->node.branches, EB_LEAF);
182
183 new->node.node_p = old->node.leaf_p;
184
185 /* Right here, we have 3 possibilities :
186 * - the tree does not contain the key, and we have
187 * new->key < old->key. We insert new above old, on
188 * the left ;
189 *
190 * - the tree does not contain the key, and we have
191 * new->key > old->key. We insert new above old, on
192 * the right ;
193 *
194 * - the tree does contain the key, which implies it
195 * is alone. We add the new key next to it as a
196 * first duplicate.
197 *
198 * The last two cases can easily be partially merged.
199 */
200 bit = equal_bits(new->key, old->key, bit, len);
Willy Tarreaua4a1cd12012-06-09 15:43:36 +0200201
202 /* Note: we can compare more bits than the current node's because as
203 * long as they are identical, we know we descend along the correct
204 * side. However we don't want to start to compare past the end.
205 */
206 diff = 0;
207 if (((unsigned)bit >> 3) < len)
208 diff = cmp_bits(new->key, old->key, bit);
Willy Tarreauc2186022009-10-26 19:48:54 +0100209
210 if (diff < 0) {
211 new->node.leaf_p = new_left;
212 old->node.leaf_p = new_rght;
213 new->node.branches.b[EB_LEFT] = new_leaf;
214 new->node.branches.b[EB_RGHT] = old_leaf;
215 } else {
216 /* we may refuse to duplicate this key if the tree is
217 * tagged as containing only unique keys.
218 */
219 if (diff == 0 && eb_gettag(root_right))
220 return old;
221
222 /* new->key >= old->key, new goes the right */
223 old->node.leaf_p = new_left;
224 new->node.leaf_p = new_rght;
225 new->node.branches.b[EB_LEFT] = old_leaf;
226 new->node.branches.b[EB_RGHT] = new_leaf;
227
228 if (diff == 0) {
229 new->node.bit = -1;
230 root->b[side] = eb_dotag(&new->node.branches, EB_NODE);
231 return new;
232 }
233 }
234 break;
235 }
236
237 /* OK we're walking down this link */
238 old = container_of(eb_untag(troot, EB_NODE),
239 struct ebpt_node, node.branches);
Willy Tarreau3a932442010-05-09 19:29:23 +0200240 old_node_bit = old->node.bit;
Willy Tarreauc2186022009-10-26 19:48:54 +0100241
242 /* Stop going down when we don't have common bits anymore. We
243 * also stop in front of a duplicates tree because it means we
244 * have to insert above. Note: we can compare more bits than
245 * the current node's because as long as they are identical, we
246 * know we descend along the correct side.
247 */
Willy Tarreau3a932442010-05-09 19:29:23 +0200248 if (old_node_bit < 0) {
Willy Tarreauc2186022009-10-26 19:48:54 +0100249 /* we're above a duplicate tree, we must compare till the end */
250 bit = equal_bits(new->key, old->key, bit, len);
251 goto dup_tree;
252 }
Willy Tarreau3a932442010-05-09 19:29:23 +0200253 else if (bit < old_node_bit) {
254 bit = equal_bits(new->key, old->key, bit, old_node_bit);
Willy Tarreauc2186022009-10-26 19:48:54 +0100255 }
256
Willy Tarreau3a932442010-05-09 19:29:23 +0200257 if (bit < old_node_bit) { /* we don't have all bits in common */
Willy Tarreauc2186022009-10-26 19:48:54 +0100258 /* The tree did not contain the key, so we insert <new> before the node
259 * <old>, and set ->bit to designate the lowest bit position in <new>
260 * which applies to ->branches.b[].
261 */
262 eb_troot_t *new_left, *new_rght;
263 eb_troot_t *new_leaf, *old_node;
264
265 dup_tree:
266 new_left = eb_dotag(&new->node.branches, EB_LEFT);
267 new_rght = eb_dotag(&new->node.branches, EB_RGHT);
268 new_leaf = eb_dotag(&new->node.branches, EB_LEAF);
269 old_node = eb_dotag(&old->node.branches, EB_NODE);
270
271 new->node.node_p = old->node.node_p;
272
Willy Tarreaua4a1cd12012-06-09 15:43:36 +0200273 /* Note: we can compare more bits than the current node's because as
274 * long as they are identical, we know we descend along the correct
275 * side. However we don't want to start to compare past the end.
276 */
277 diff = 0;
278 if (((unsigned)bit >> 3) < len)
279 diff = cmp_bits(new->key, old->key, bit);
280
Willy Tarreauc2186022009-10-26 19:48:54 +0100281 if (diff < 0) {
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 (diff > 0) {
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;
Willy Tarreau3a932442010-05-09 19:29:23 +0200303 side = (((unsigned char *)new->key)[old_node_bit >> 3] >> (~old_node_bit & 7)) & 1;
Willy Tarreauc2186022009-10-26 19:48:54 +0100304 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 * This number of bits is already in <bit>.
315 */
316 new->node.bit = bit;
317 root->b[side] = eb_dotag(&new->node.branches, EB_NODE);
318 return new;
319}