/src/haproxy/include/import/ebsttree.h
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Elastic Binary Trees - macros to manipulate String data nodes. |
3 | | * Version 6.0.6 |
4 | | * (C) 2002-2011 - Willy Tarreau <w@1wt.eu> |
5 | | * |
6 | | * 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. |
10 | | * |
11 | | * This library 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 GNU |
14 | | * Lesser General Public License for more details. |
15 | | * |
16 | | * 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 |
19 | | */ |
20 | | |
21 | | /* These functions and macros rely on Multi-Byte nodes */ |
22 | | |
23 | | #ifndef _EBSTTREE_H |
24 | | #define _EBSTTREE_H |
25 | | |
26 | | #include "ebtree.h" |
27 | | #include "ebmbtree.h" |
28 | | |
29 | | /* The following functions are not inlined by default. They are declared |
30 | | * in ebsttree.c, which simply relies on their inline version. |
31 | | */ |
32 | | struct ebmb_node *ebst_lookup(struct eb_root *root, const char *x); |
33 | | struct ebmb_node *ebst_insert(struct eb_root *root, struct ebmb_node *new); |
34 | | |
35 | | /* Find the first occurrence of a length <len> string <x> in the tree <root>. |
36 | | * It's the caller's responsibility to use this function only on trees which |
37 | | * only contain zero-terminated strings, and that no null character is present |
38 | | * in string <x> in the first <len> chars. If none can be found, return NULL. |
39 | | */ |
40 | | static forceinline struct ebmb_node * |
41 | | ebst_lookup_len(struct eb_root *root, const char *x, unsigned int len) |
42 | 0 | { |
43 | 0 | struct ebmb_node *node; |
44 | |
|
45 | 0 | node = ebmb_lookup(root, x, len); |
46 | 0 | if (!node || node->key[len] != 0) |
47 | 0 | return NULL; |
48 | 0 | return node; |
49 | 0 | } Unexecuted instantiation: stick_table.c:ebst_lookup_len Unexecuted instantiation: acl.c:ebst_lookup_len Unexecuted instantiation: ebsttree.c:ebst_lookup_len Unexecuted instantiation: pattern.c:ebst_lookup_len |
50 | | |
51 | | /* Find the first occurrence of a zero-terminated string <x> in the tree <root>. |
52 | | * It's the caller's responsibility to use this function only on trees which |
53 | | * only contain zero-terminated strings. If none can be found, return NULL. |
54 | | */ |
55 | | static forceinline struct ebmb_node *__ebst_lookup(struct eb_root *root, const void *x) |
56 | 0 | { |
57 | 0 | struct ebmb_node *node; |
58 | 0 | eb_troot_t *troot; |
59 | 0 | int bit; |
60 | 0 | int node_bit; |
61 | |
|
62 | 0 | troot = root->b[EB_LEFT]; |
63 | 0 | if (unlikely(troot == NULL)) |
64 | 0 | return NULL; |
65 | | |
66 | 0 | bit = 0; |
67 | 0 | while (1) { |
68 | 0 | if ((eb_gettag(troot) == EB_LEAF)) { |
69 | 0 | node = container_of(eb_untag(troot, EB_LEAF), |
70 | 0 | struct ebmb_node, node.branches); |
71 | 0 | if (strcmp((char *)node->key, x) == 0) |
72 | 0 | return node; |
73 | 0 | else |
74 | 0 | return NULL; |
75 | 0 | } |
76 | 0 | node = container_of(eb_untag(troot, EB_NODE), |
77 | 0 | struct ebmb_node, node.branches); |
78 | 0 | node_bit = node->node.bit; |
79 | |
|
80 | 0 | if (node_bit < 0) { |
81 | | /* We have a dup tree now. Either it's for the same |
82 | | * value, and we walk down left, or it's a different |
83 | | * one and we don't have our key. |
84 | | */ |
85 | 0 | if (strcmp((char *)node->key, x) != 0) |
86 | 0 | return NULL; |
87 | | |
88 | 0 | troot = node->node.branches.b[EB_LEFT]; |
89 | 0 | while (eb_gettag(troot) != EB_LEAF) |
90 | 0 | troot = (eb_untag(troot, EB_NODE))->b[EB_LEFT]; |
91 | 0 | node = container_of(eb_untag(troot, EB_LEAF), |
92 | 0 | struct ebmb_node, node.branches); |
93 | 0 | return node; |
94 | 0 | } |
95 | | |
96 | | /* OK, normal data node, let's walk down but don't compare data |
97 | | * if we already reached the end of the key. |
98 | | */ |
99 | 0 | if (likely(bit >= 0)) { |
100 | 0 | bit = string_equal_bits(x, node->key, bit); |
101 | 0 | if (likely(bit < node_bit)) { |
102 | 0 | if (bit >= 0) |
103 | 0 | return NULL; /* no more common bits */ |
104 | | |
105 | | /* bit < 0 : we reached the end of the key. If we |
106 | | * are in a tree with unique keys, we can return |
107 | | * this node. Otherwise we have to walk it down |
108 | | * and stop comparing bits. |
109 | | */ |
110 | 0 | if (eb_gettag(root->b[EB_RGHT])) |
111 | 0 | return node; |
112 | 0 | } |
113 | | /* if the bit is larger than the node's, we must bound it |
114 | | * because we might have compared too many bytes with an |
115 | | * inappropriate leaf. For a test, build a tree from "0", |
116 | | * "WW", "W", "S" inserted in this exact sequence and lookup |
117 | | * "W" => "S" is returned without this assignment. |
118 | | */ |
119 | 0 | else |
120 | 0 | bit = node_bit; |
121 | 0 | } |
122 | | |
123 | 0 | troot = node->node.branches.b[(((unsigned char*)x)[node_bit >> 3] >> |
124 | 0 | (~node_bit & 7)) & 1]; |
125 | 0 | } |
126 | 0 | } Unexecuted instantiation: stick_table.c:__ebst_lookup Unexecuted instantiation: acl.c:__ebst_lookup Unexecuted instantiation: ebsttree.c:__ebst_lookup Unexecuted instantiation: pattern.c:__ebst_lookup |
127 | | |
128 | | /* Insert ebmb_node <new> into subtree starting at node root <root>. Only |
129 | | * new->key needs be set with the zero-terminated string key. The ebmb_node is |
130 | | * returned. If root->b[EB_RGHT]==1, the tree may only contain unique keys. The |
131 | | * caller is responsible for properly terminating the key with a zero. |
132 | | */ |
133 | | static forceinline struct ebmb_node * |
134 | | __ebst_insert(struct eb_root *root, struct ebmb_node *new) |
135 | 0 | { |
136 | 0 | struct ebmb_node *old; |
137 | 0 | unsigned int side; |
138 | 0 | eb_troot_t *troot; |
139 | 0 | eb_troot_t *root_right; |
140 | 0 | int diff; |
141 | 0 | int bit; |
142 | 0 | int old_node_bit; |
143 | |
|
144 | 0 | side = EB_LEFT; |
145 | 0 | troot = root->b[EB_LEFT]; |
146 | 0 | root_right = root->b[EB_RGHT]; |
147 | 0 | if (unlikely(troot == NULL)) { |
148 | | /* Tree is empty, insert the leaf part below the left branch */ |
149 | 0 | root->b[EB_LEFT] = eb_dotag(&new->node.branches, EB_LEAF); |
150 | 0 | new->node.leaf_p = eb_dotag(root, EB_LEFT); |
151 | 0 | new->node.node_p = NULL; /* node part unused */ |
152 | 0 | return new; |
153 | 0 | } |
154 | | |
155 | | /* The tree descent is fairly easy : |
156 | | * - first, check if we have reached a leaf node |
157 | | * - second, check if we have gone too far |
158 | | * - third, reiterate |
159 | | * Everywhere, we use <new> for the node node we are inserting, <root> |
160 | | * for the node we attach it to, and <old> for the node we are |
161 | | * displacing below <new>. <troot> will always point to the future node |
162 | | * (tagged with its type). <side> carries the side the node <new> is |
163 | | * attached to below its parent, which is also where previous node |
164 | | * was attached. |
165 | | */ |
166 | | |
167 | 0 | bit = 0; |
168 | 0 | while (1) { |
169 | 0 | if (unlikely(eb_gettag(troot) == EB_LEAF)) { |
170 | 0 | eb_troot_t *new_left, *new_rght; |
171 | 0 | eb_troot_t *new_leaf, *old_leaf; |
172 | |
|
173 | 0 | old = container_of(eb_untag(troot, EB_LEAF), |
174 | 0 | struct ebmb_node, node.branches); |
175 | |
|
176 | 0 | new_left = eb_dotag(&new->node.branches, EB_LEFT); |
177 | 0 | new_rght = eb_dotag(&new->node.branches, EB_RGHT); |
178 | 0 | new_leaf = eb_dotag(&new->node.branches, EB_LEAF); |
179 | 0 | old_leaf = eb_dotag(&old->node.branches, EB_LEAF); |
180 | |
|
181 | 0 | new->node.node_p = old->node.leaf_p; |
182 | | |
183 | | /* Right here, we have 3 possibilities : |
184 | | * - the tree does not contain the key, and we have |
185 | | * new->key < old->key. We insert new above old, on |
186 | | * the left ; |
187 | | * |
188 | | * - the tree does not contain the key, and we have |
189 | | * new->key > old->key. We insert new above old, on |
190 | | * the right ; |
191 | | * |
192 | | * - the tree does contain the key, which implies it |
193 | | * is alone. We add the new key next to it as a |
194 | | * first duplicate. |
195 | | * |
196 | | * The last two cases can easily be partially merged. |
197 | | */ |
198 | 0 | if (bit >= 0) |
199 | 0 | bit = string_equal_bits(new->key, old->key, bit); |
200 | |
|
201 | 0 | if (bit < 0) { |
202 | | /* key was already there */ |
203 | | |
204 | | /* we may refuse to duplicate this key if the tree is |
205 | | * tagged as containing only unique keys. |
206 | | */ |
207 | 0 | if (eb_gettag(root_right)) |
208 | 0 | return old; |
209 | | |
210 | | /* new arbitrarily goes to the right and tops the dup tree */ |
211 | 0 | old->node.leaf_p = new_left; |
212 | 0 | new->node.leaf_p = new_rght; |
213 | 0 | new->node.branches.b[EB_LEFT] = old_leaf; |
214 | 0 | new->node.branches.b[EB_RGHT] = new_leaf; |
215 | 0 | new->node.bit = -1; |
216 | 0 | root->b[side] = eb_dotag(&new->node.branches, EB_NODE); |
217 | 0 | return new; |
218 | 0 | } |
219 | | |
220 | 0 | diff = cmp_bits(new->key, old->key, bit); |
221 | 0 | if (diff < 0) { |
222 | | /* new->key < old->key, new takes the left */ |
223 | 0 | new->node.leaf_p = new_left; |
224 | 0 | old->node.leaf_p = new_rght; |
225 | 0 | new->node.branches.b[EB_LEFT] = new_leaf; |
226 | 0 | new->node.branches.b[EB_RGHT] = old_leaf; |
227 | 0 | } else { |
228 | | /* new->key > old->key, new takes the right */ |
229 | 0 | old->node.leaf_p = new_left; |
230 | 0 | new->node.leaf_p = new_rght; |
231 | 0 | new->node.branches.b[EB_LEFT] = old_leaf; |
232 | 0 | new->node.branches.b[EB_RGHT] = new_leaf; |
233 | 0 | } |
234 | 0 | break; |
235 | 0 | } |
236 | | |
237 | | /* OK we're walking down this link */ |
238 | 0 | old = container_of(eb_untag(troot, EB_NODE), |
239 | 0 | struct ebmb_node, node.branches); |
240 | 0 | old_node_bit = old->node.bit; |
241 | | |
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 | | */ |
248 | 0 | if (bit >= 0 && (bit < old_node_bit || old_node_bit < 0)) |
249 | 0 | bit = string_equal_bits(new->key, old->key, bit); |
250 | |
|
251 | 0 | if (unlikely(bit < 0)) { |
252 | | /* Perfect match, we must only stop on head of dup tree |
253 | | * or walk down to a leaf. |
254 | | */ |
255 | 0 | if (old_node_bit < 0) { |
256 | | /* We know here that string_equal_bits matched all |
257 | | * bits and that we're on top of a dup tree, then |
258 | | * we can perform the dup insertion and return. |
259 | | */ |
260 | 0 | struct eb_node *ret; |
261 | 0 | ret = eb_insert_dup(&old->node, &new->node); |
262 | 0 | return container_of(ret, struct ebmb_node, node); |
263 | 0 | } |
264 | | /* OK so let's walk down */ |
265 | 0 | } |
266 | 0 | else if (bit < old_node_bit || old_node_bit < 0) { |
267 | | /* The tree did not contain the key, or we stopped on top of a dup |
268 | | * tree, possibly containing the key. In the former case, we insert |
269 | | * <new> before the node <old>, and set ->bit to designate the lowest |
270 | | * bit position in <new> which applies to ->branches.b[]. In the later |
271 | | * case, we add the key to the existing dup tree. Note that we cannot |
272 | | * enter here if we match an intermediate node's key that is not the |
273 | | * head of a dup tree. |
274 | | */ |
275 | 0 | eb_troot_t *new_left, *new_rght; |
276 | 0 | eb_troot_t *new_leaf, *old_node; |
277 | |
|
278 | 0 | new_left = eb_dotag(&new->node.branches, EB_LEFT); |
279 | 0 | new_rght = eb_dotag(&new->node.branches, EB_RGHT); |
280 | 0 | new_leaf = eb_dotag(&new->node.branches, EB_LEAF); |
281 | 0 | old_node = eb_dotag(&old->node.branches, EB_NODE); |
282 | |
|
283 | 0 | new->node.node_p = old->node.node_p; |
284 | | |
285 | | /* we can never match all bits here */ |
286 | 0 | diff = cmp_bits(new->key, old->key, bit); |
287 | 0 | if (diff < 0) { |
288 | 0 | new->node.leaf_p = new_left; |
289 | 0 | old->node.node_p = new_rght; |
290 | 0 | new->node.branches.b[EB_LEFT] = new_leaf; |
291 | 0 | new->node.branches.b[EB_RGHT] = old_node; |
292 | 0 | } |
293 | 0 | else { |
294 | 0 | old->node.node_p = new_left; |
295 | 0 | new->node.leaf_p = new_rght; |
296 | 0 | new->node.branches.b[EB_LEFT] = old_node; |
297 | 0 | new->node.branches.b[EB_RGHT] = new_leaf; |
298 | 0 | } |
299 | 0 | break; |
300 | 0 | } |
301 | | |
302 | | /* walk down */ |
303 | 0 | root = &old->node.branches; |
304 | 0 | side = (new->key[old_node_bit >> 3] >> (~old_node_bit & 7)) & 1; |
305 | 0 | troot = root->b[side]; |
306 | 0 | } |
307 | | |
308 | | /* Ok, now we are inserting <new> between <root> and <old>. <old>'s |
309 | | * parent is already set to <new>, and the <root>'s branch is still in |
310 | | * <side>. Update the root's leaf till we have it. Note that we can also |
311 | | * find the side by checking the side of new->node.node_p. |
312 | | */ |
313 | | |
314 | | /* We need the common higher bits between new->key and old->key. |
315 | | * This number of bits is already in <bit>. |
316 | | * NOTE: we can't get here whit bit < 0 since we found a dup ! |
317 | | */ |
318 | 0 | new->node.bit = bit; |
319 | 0 | root->b[side] = eb_dotag(&new->node.branches, EB_NODE); |
320 | 0 | return new; |
321 | 0 | } Unexecuted instantiation: stick_table.c:__ebst_insert Unexecuted instantiation: acl.c:__ebst_insert Unexecuted instantiation: ebsttree.c:__ebst_insert Unexecuted instantiation: pattern.c:__ebst_insert |
322 | | |
323 | | #endif /* _EBSTTREE_H */ |
324 | | |