blob: 205a2dbfa16ae85c66ad1d67027c426635e28440 [file] [log] [blame]
Willy Tarreauc2186022009-10-26 19:48:54 +01001/*
2 * Elastic Binary Trees - macros for Indirect Multi-Byte data nodes.
Willy Tarreau414c4b22011-01-04 13:21:06 +01003 * Version 6.0.5
4 * (C) 2002-2011 - Willy Tarreau <w@1wt.eu>
Willy Tarreauc2186022009-10-26 19:48:54 +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
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))
52 return NULL;
53
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 Tarreauc2186022009-10-26 19:48:54 +010063 return NULL;
Willy Tarreau414c4b22011-01-04 13:21:06 +010064 else
65 return 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 Tarreauc2186022009-10-26 19:48:54 +010077 return NULL;
Willy Tarreau414c4b22011-01-04 13:21:06 +010078 walk_left:
Willy Tarreauc2186022009-10-26 19:48:54 +010079 troot = node->node.branches.b[EB_LEFT];
Willy Tarreau414c4b22011-01-04 13:21:06 +010080 walk_down:
Willy Tarreauc2186022009-10-26 19:48:54 +010081 while (eb_gettag(troot) != EB_LEAF)
82 troot = (eb_untag(troot, EB_NODE))->b[EB_LEFT];
83 node = container_of(eb_untag(troot, EB_LEAF),
84 struct ebpt_node, node.branches);
85 return node;
86 }
87
Willy Tarreau414c4b22011-01-04 13:21:06 +010088 /* OK, normal data node, let's walk down. We check if all full
89 * bytes are equal, and we start from the last one we did not
90 * completely check. We stop as soon as we reach the last byte,
91 * because we must decide to go left/right or abort.
92 */
93 node_bit = ~node_bit + (pos << 3) + 8; // = (pos<<3) + (7 - node_bit)
94 if (node_bit < 0) {
95 /* This surprizing construction gives better performance
96 * because gcc does not try to reorder the loop. Tested to
97 * be fine with 2.95 to 4.2.
98 */
99 while (1) {
100 if (*(unsigned char*)(node->key + pos++) ^ *(unsigned char*)(x++))
101 return NULL; /* more than one full byte is different */
102 if (--len == 0)
103 goto walk_left; /* return first node if all bytes matched */
104 node_bit += 8;
105 if (node_bit >= 0)
106 break;
107 }
108 }
Willy Tarreauc2186022009-10-26 19:48:54 +0100109
Willy Tarreau414c4b22011-01-04 13:21:06 +0100110 /* here we know that only the last byte differs, so node_bit < 8.
111 * We have 2 possibilities :
112 * - more than the last bit differs => return NULL
113 * - walk down on side = (x[pos] >> node_bit) & 1
114 */
115 side = *(unsigned char *)x >> node_bit;
116 if (((*(unsigned char*)(node->key + pos) >> node_bit) ^ side) > 1)
117 return NULL;
118 side &= 1;
119 troot = node->node.branches.b[side];
Willy Tarreauc2186022009-10-26 19:48:54 +0100120 }
121}
122
123/* Insert ebpt_node <new> into subtree starting at node root <root>.
124 * Only new->key needs be set with the key. The ebpt_node is returned.
125 * If root->b[EB_RGHT]==1, the tree may only contain unique keys. The
126 * len is specified in bytes.
127 */
128static forceinline struct ebpt_node *
129__ebim_insert(struct eb_root *root, struct ebpt_node *new, unsigned int len)
130{
131 struct ebpt_node *old;
132 unsigned int side;
133 eb_troot_t *troot;
134 eb_troot_t *root_right = root;
135 int diff;
136 int bit;
Willy Tarreau3a932442010-05-09 19:29:23 +0200137 int old_node_bit;
Willy Tarreauc2186022009-10-26 19:48:54 +0100138
139 side = EB_LEFT;
140 troot = root->b[EB_LEFT];
141 root_right = root->b[EB_RGHT];
142 if (unlikely(troot == NULL)) {
143 /* Tree is empty, insert the leaf part below the left branch */
144 root->b[EB_LEFT] = eb_dotag(&new->node.branches, EB_LEAF);
145 new->node.leaf_p = eb_dotag(root, EB_LEFT);
146 new->node.node_p = NULL; /* node part unused */
147 return new;
148 }
149
150 len <<= 3;
151
152 /* The tree descent is fairly easy :
153 * - first, check if we have reached a leaf node
154 * - second, check if we have gone too far
155 * - third, reiterate
156 * Everywhere, we use <new> for the node node we are inserting, <root>
157 * for the node we attach it to, and <old> for the node we are
158 * displacing below <new>. <troot> will always point to the future node
159 * (tagged with its type). <side> carries the side the node <new> is
160 * attached to below its parent, which is also where previous node
161 * was attached.
162 */
163
164 bit = 0;
165 while (1) {
166 if (unlikely(eb_gettag(troot) == EB_LEAF)) {
167 eb_troot_t *new_left, *new_rght;
168 eb_troot_t *new_leaf, *old_leaf;
169
170 old = container_of(eb_untag(troot, EB_LEAF),
171 struct ebpt_node, node.branches);
172
173 new_left = eb_dotag(&new->node.branches, EB_LEFT);
174 new_rght = eb_dotag(&new->node.branches, EB_RGHT);
175 new_leaf = eb_dotag(&new->node.branches, EB_LEAF);
176 old_leaf = eb_dotag(&old->node.branches, EB_LEAF);
177
178 new->node.node_p = old->node.leaf_p;
179
180 /* Right here, we have 3 possibilities :
181 * - the tree does not contain the key, and we have
182 * new->key < old->key. We insert new above old, on
183 * the left ;
184 *
185 * - the tree does not contain the key, and we have
186 * new->key > old->key. We insert new above old, on
187 * the right ;
188 *
189 * - the tree does contain the key, which implies it
190 * is alone. We add the new key next to it as a
191 * first duplicate.
192 *
193 * The last two cases can easily be partially merged.
194 */
195 bit = equal_bits(new->key, old->key, bit, len);
196 diff = cmp_bits(new->key, old->key, bit);
197
198 if (diff < 0) {
199 new->node.leaf_p = new_left;
200 old->node.leaf_p = new_rght;
201 new->node.branches.b[EB_LEFT] = new_leaf;
202 new->node.branches.b[EB_RGHT] = old_leaf;
203 } else {
204 /* we may refuse to duplicate this key if the tree is
205 * tagged as containing only unique keys.
206 */
207 if (diff == 0 && eb_gettag(root_right))
208 return old;
209
210 /* new->key >= old->key, new goes the right */
211 old->node.leaf_p = new_left;
212 new->node.leaf_p = new_rght;
213 new->node.branches.b[EB_LEFT] = old_leaf;
214 new->node.branches.b[EB_RGHT] = new_leaf;
215
216 if (diff == 0) {
217 new->node.bit = -1;
218 root->b[side] = eb_dotag(&new->node.branches, EB_NODE);
219 return new;
220 }
221 }
222 break;
223 }
224
225 /* OK we're walking down this link */
226 old = container_of(eb_untag(troot, EB_NODE),
227 struct ebpt_node, node.branches);
Willy Tarreau3a932442010-05-09 19:29:23 +0200228 old_node_bit = old->node.bit;
Willy Tarreauc2186022009-10-26 19:48:54 +0100229
230 /* Stop going down when we don't have common bits anymore. We
231 * also stop in front of a duplicates tree because it means we
232 * have to insert above. Note: we can compare more bits than
233 * the current node's because as long as they are identical, we
234 * know we descend along the correct side.
235 */
Willy Tarreau3a932442010-05-09 19:29:23 +0200236 if (old_node_bit < 0) {
Willy Tarreauc2186022009-10-26 19:48:54 +0100237 /* we're above a duplicate tree, we must compare till the end */
238 bit = equal_bits(new->key, old->key, bit, len);
239 goto dup_tree;
240 }
Willy Tarreau3a932442010-05-09 19:29:23 +0200241 else if (bit < old_node_bit) {
242 bit = equal_bits(new->key, old->key, bit, old_node_bit);
Willy Tarreauc2186022009-10-26 19:48:54 +0100243 }
244
Willy Tarreau3a932442010-05-09 19:29:23 +0200245 if (bit < old_node_bit) { /* we don't have all bits in common */
Willy Tarreauc2186022009-10-26 19:48:54 +0100246 /* The tree did not contain the key, so we insert <new> before the node
247 * <old>, and set ->bit to designate the lowest bit position in <new>
248 * which applies to ->branches.b[].
249 */
250 eb_troot_t *new_left, *new_rght;
251 eb_troot_t *new_leaf, *old_node;
252
253 dup_tree:
254 new_left = eb_dotag(&new->node.branches, EB_LEFT);
255 new_rght = eb_dotag(&new->node.branches, EB_RGHT);
256 new_leaf = eb_dotag(&new->node.branches, EB_LEAF);
257 old_node = eb_dotag(&old->node.branches, EB_NODE);
258
259 new->node.node_p = old->node.node_p;
260
261 diff = cmp_bits(new->key, old->key, bit);
262 if (diff < 0) {
263 new->node.leaf_p = new_left;
264 old->node.node_p = new_rght;
265 new->node.branches.b[EB_LEFT] = new_leaf;
266 new->node.branches.b[EB_RGHT] = old_node;
267 }
268 else if (diff > 0) {
269 old->node.node_p = new_left;
270 new->node.leaf_p = new_rght;
271 new->node.branches.b[EB_LEFT] = old_node;
272 new->node.branches.b[EB_RGHT] = new_leaf;
273 }
274 else {
275 struct eb_node *ret;
276 ret = eb_insert_dup(&old->node, &new->node);
277 return container_of(ret, struct ebpt_node, node);
278 }
279 break;
280 }
281
282 /* walk down */
283 root = &old->node.branches;
Willy Tarreau3a932442010-05-09 19:29:23 +0200284 side = (((unsigned char *)new->key)[old_node_bit >> 3] >> (~old_node_bit & 7)) & 1;
Willy Tarreauc2186022009-10-26 19:48:54 +0100285 troot = root->b[side];
286 }
287
288 /* Ok, now we are inserting <new> between <root> and <old>. <old>'s
289 * parent is already set to <new>, and the <root>'s branch is still in
290 * <side>. Update the root's leaf till we have it. Note that we can also
291 * find the side by checking the side of new->node.node_p.
292 */
293
294 /* We need the common higher bits between new->key and old->key.
295 * This number of bits is already in <bit>.
296 */
297 new->node.bit = bit;
298 root->b[side] = eb_dotag(&new->node.branches, EB_NODE);
299 return new;
300}