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