blob: 4d2eea0f05e251c4eda0dbb3c9fc872d523ce9a9 [file] [log] [blame]
Willy Tarreauc2186022009-10-26 19:48:54 +01001/*
2 * Elastic Binary Trees - macros for Indirect Multi-Byte data nodes.
Willy Tarreau3a932442010-05-09 19:29:23 +02003 * Version 6.0
4 * (C) 2002-2010 - 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
35/* Find the first occurence of a key of <len> bytes in the tree <root>.
36 * If none can be found, return NULL.
37 */
38static forceinline struct ebpt_node *
39__ebim_lookup(struct eb_root *root, const void *x, unsigned int len)
40{
41 struct ebpt_node *node;
42 eb_troot_t *troot;
Willy Tarreau3a932442010-05-09 19:29:23 +020043 int bit;
44 int node_bit;
Willy Tarreauc2186022009-10-26 19:48:54 +010045
46 troot = root->b[EB_LEFT];
47 if (unlikely(troot == NULL))
48 return NULL;
49
50 bit = 0;
51 while (1) {
52 if ((eb_gettag(troot) == EB_LEAF)) {
53 node = container_of(eb_untag(troot, EB_LEAF),
54 struct ebpt_node, node.branches);
55 if (memcmp(node->key, x, len) == 0)
56 return node;
57 else
58 return NULL;
59 }
60 node = container_of(eb_untag(troot, EB_NODE),
61 struct ebpt_node, node.branches);
62
Willy Tarreau3a932442010-05-09 19:29:23 +020063 node_bit = node->node.bit;
64 if (node_bit < 0) {
Willy Tarreauc2186022009-10-26 19:48:54 +010065 /* We have a dup tree now. Either it's for the same
66 * value, and we walk down left, or it's a different
67 * one and we don't have our key.
68 */
69 if (memcmp(node->key, x, len) != 0)
70 return NULL;
71
72 troot = node->node.branches.b[EB_LEFT];
73 while (eb_gettag(troot) != EB_LEAF)
74 troot = (eb_untag(troot, EB_NODE))->b[EB_LEFT];
75 node = container_of(eb_untag(troot, EB_LEAF),
76 struct ebpt_node, node.branches);
77 return node;
78 }
79
80 /* OK, normal data node, let's walk down */
Willy Tarreau3a932442010-05-09 19:29:23 +020081 bit = equal_bits(x, node->key, bit, node_bit);
82 if (bit < node_bit)
Willy Tarreauc2186022009-10-26 19:48:54 +010083 return NULL; /* no more common bits */
84
Willy Tarreau3a932442010-05-09 19:29:23 +020085 troot = node->node.branches.b[(((unsigned char*)x)[node_bit >> 3] >>
86 (~node_bit & 7)) & 1];
Willy Tarreauc2186022009-10-26 19:48:54 +010087 }
88}
89
90/* Insert ebpt_node <new> into subtree starting at node root <root>.
91 * Only new->key needs be set with the key. The ebpt_node is returned.
92 * If root->b[EB_RGHT]==1, the tree may only contain unique keys. The
93 * len is specified in bytes.
94 */
95static forceinline struct ebpt_node *
96__ebim_insert(struct eb_root *root, struct ebpt_node *new, unsigned int len)
97{
98 struct ebpt_node *old;
99 unsigned int side;
100 eb_troot_t *troot;
101 eb_troot_t *root_right = root;
102 int diff;
103 int bit;
Willy Tarreau3a932442010-05-09 19:29:23 +0200104 int old_node_bit;
Willy Tarreauc2186022009-10-26 19:48:54 +0100105
106 side = EB_LEFT;
107 troot = root->b[EB_LEFT];
108 root_right = root->b[EB_RGHT];
109 if (unlikely(troot == NULL)) {
110 /* Tree is empty, insert the leaf part below the left branch */
111 root->b[EB_LEFT] = eb_dotag(&new->node.branches, EB_LEAF);
112 new->node.leaf_p = eb_dotag(root, EB_LEFT);
113 new->node.node_p = NULL; /* node part unused */
114 return new;
115 }
116
117 len <<= 3;
118
119 /* The tree descent is fairly easy :
120 * - first, check if we have reached a leaf node
121 * - second, check if we have gone too far
122 * - third, reiterate
123 * Everywhere, we use <new> for the node node we are inserting, <root>
124 * for the node we attach it to, and <old> for the node we are
125 * displacing below <new>. <troot> will always point to the future node
126 * (tagged with its type). <side> carries the side the node <new> is
127 * attached to below its parent, which is also where previous node
128 * was attached.
129 */
130
131 bit = 0;
132 while (1) {
133 if (unlikely(eb_gettag(troot) == EB_LEAF)) {
134 eb_troot_t *new_left, *new_rght;
135 eb_troot_t *new_leaf, *old_leaf;
136
137 old = container_of(eb_untag(troot, EB_LEAF),
138 struct ebpt_node, node.branches);
139
140 new_left = eb_dotag(&new->node.branches, EB_LEFT);
141 new_rght = eb_dotag(&new->node.branches, EB_RGHT);
142 new_leaf = eb_dotag(&new->node.branches, EB_LEAF);
143 old_leaf = eb_dotag(&old->node.branches, EB_LEAF);
144
145 new->node.node_p = old->node.leaf_p;
146
147 /* Right here, we have 3 possibilities :
148 * - the tree does not contain the key, and we have
149 * new->key < old->key. We insert new above old, on
150 * the left ;
151 *
152 * - the tree does not contain the key, and we have
153 * new->key > old->key. We insert new above old, on
154 * the right ;
155 *
156 * - the tree does contain the key, which implies it
157 * is alone. We add the new key next to it as a
158 * first duplicate.
159 *
160 * The last two cases can easily be partially merged.
161 */
162 bit = equal_bits(new->key, old->key, bit, len);
163 diff = cmp_bits(new->key, old->key, bit);
164
165 if (diff < 0) {
166 new->node.leaf_p = new_left;
167 old->node.leaf_p = new_rght;
168 new->node.branches.b[EB_LEFT] = new_leaf;
169 new->node.branches.b[EB_RGHT] = old_leaf;
170 } else {
171 /* we may refuse to duplicate this key if the tree is
172 * tagged as containing only unique keys.
173 */
174 if (diff == 0 && eb_gettag(root_right))
175 return old;
176
177 /* new->key >= old->key, new goes the right */
178 old->node.leaf_p = new_left;
179 new->node.leaf_p = new_rght;
180 new->node.branches.b[EB_LEFT] = old_leaf;
181 new->node.branches.b[EB_RGHT] = new_leaf;
182
183 if (diff == 0) {
184 new->node.bit = -1;
185 root->b[side] = eb_dotag(&new->node.branches, EB_NODE);
186 return new;
187 }
188 }
189 break;
190 }
191
192 /* OK we're walking down this link */
193 old = container_of(eb_untag(troot, EB_NODE),
194 struct ebpt_node, node.branches);
Willy Tarreau3a932442010-05-09 19:29:23 +0200195 old_node_bit = old->node.bit;
Willy Tarreauc2186022009-10-26 19:48:54 +0100196
197 /* Stop going down when we don't have common bits anymore. We
198 * also stop in front of a duplicates tree because it means we
199 * have to insert above. Note: we can compare more bits than
200 * the current node's because as long as they are identical, we
201 * know we descend along the correct side.
202 */
Willy Tarreau3a932442010-05-09 19:29:23 +0200203 if (old_node_bit < 0) {
Willy Tarreauc2186022009-10-26 19:48:54 +0100204 /* we're above a duplicate tree, we must compare till the end */
205 bit = equal_bits(new->key, old->key, bit, len);
206 goto dup_tree;
207 }
Willy Tarreau3a932442010-05-09 19:29:23 +0200208 else if (bit < old_node_bit) {
209 bit = equal_bits(new->key, old->key, bit, old_node_bit);
Willy Tarreauc2186022009-10-26 19:48:54 +0100210 }
211
Willy Tarreau3a932442010-05-09 19:29:23 +0200212 if (bit < old_node_bit) { /* we don't have all bits in common */
Willy Tarreauc2186022009-10-26 19:48:54 +0100213 /* The tree did not contain the key, so we insert <new> before the node
214 * <old>, and set ->bit to designate the lowest bit position in <new>
215 * which applies to ->branches.b[].
216 */
217 eb_troot_t *new_left, *new_rght;
218 eb_troot_t *new_leaf, *old_node;
219
220 dup_tree:
221 new_left = eb_dotag(&new->node.branches, EB_LEFT);
222 new_rght = eb_dotag(&new->node.branches, EB_RGHT);
223 new_leaf = eb_dotag(&new->node.branches, EB_LEAF);
224 old_node = eb_dotag(&old->node.branches, EB_NODE);
225
226 new->node.node_p = old->node.node_p;
227
228 diff = cmp_bits(new->key, old->key, bit);
229 if (diff < 0) {
230 new->node.leaf_p = new_left;
231 old->node.node_p = new_rght;
232 new->node.branches.b[EB_LEFT] = new_leaf;
233 new->node.branches.b[EB_RGHT] = old_node;
234 }
235 else if (diff > 0) {
236 old->node.node_p = new_left;
237 new->node.leaf_p = new_rght;
238 new->node.branches.b[EB_LEFT] = old_node;
239 new->node.branches.b[EB_RGHT] = new_leaf;
240 }
241 else {
242 struct eb_node *ret;
243 ret = eb_insert_dup(&old->node, &new->node);
244 return container_of(ret, struct ebpt_node, node);
245 }
246 break;
247 }
248
249 /* walk down */
250 root = &old->node.branches;
Willy Tarreau3a932442010-05-09 19:29:23 +0200251 side = (((unsigned char *)new->key)[old_node_bit >> 3] >> (~old_node_bit & 7)) & 1;
Willy Tarreauc2186022009-10-26 19:48:54 +0100252 troot = root->b[side];
253 }
254
255 /* Ok, now we are inserting <new> between <root> and <old>. <old>'s
256 * parent is already set to <new>, and the <root>'s branch is still in
257 * <side>. Update the root's leaf till we have it. Note that we can also
258 * find the side by checking the side of new->node.node_p.
259 */
260
261 /* We need the common higher bits between new->key and old->key.
262 * This number of bits is already in <bit>.
263 */
264 new->node.bit = bit;
265 root->b[side] = eb_dotag(&new->node.branches, EB_NODE);
266 return new;
267}