/src/haproxy/include/import/ebsttree.h
Line | Count | Source |
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: stats.c:ebst_lookup_len 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 Unexecuted instantiation: stats-file.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 | |
|
79 | 0 | eb_prefetch(node->node.branches.b[0], 0); |
80 | 0 | eb_prefetch(node->node.branches.b[1], 0); |
81 | |
|
82 | 0 | node_bit = node->node.bit; |
83 | |
|
84 | 0 | if (node_bit < 0) { |
85 | | /* We have a dup tree now. Either it's for the same |
86 | | * value, and we walk down left, or it's a different |
87 | | * one and we don't have our key. |
88 | | */ |
89 | 0 | if (strcmp((char *)node->key, x) != 0) |
90 | 0 | return NULL; |
91 | | |
92 | 0 | troot = node->node.branches.b[EB_LEFT]; |
93 | 0 | while (eb_gettag(troot) != EB_LEAF) |
94 | 0 | troot = (eb_untag(troot, EB_NODE))->b[EB_LEFT]; |
95 | 0 | node = container_of(eb_untag(troot, EB_LEAF), |
96 | 0 | struct ebmb_node, node.branches); |
97 | 0 | return node; |
98 | 0 | } |
99 | | |
100 | | /* OK, normal data node, let's walk down but don't compare data |
101 | | * if we already reached the end of the key. |
102 | | */ |
103 | 0 | if (likely(bit >= 0)) { |
104 | 0 | bit = string_equal_bits(x, node->key, bit); |
105 | 0 | if (likely(bit < node_bit)) { |
106 | 0 | if (bit >= 0) |
107 | 0 | return NULL; /* no more common bits */ |
108 | | |
109 | | /* bit < 0 : we reached the end of the key. If we |
110 | | * are in a tree with unique keys, we can return |
111 | | * this node. Otherwise we have to walk it down |
112 | | * and stop comparing bits. |
113 | | */ |
114 | 0 | if (eb_gettag(root->b[EB_RGHT])) |
115 | 0 | return node; |
116 | 0 | } |
117 | | /* if the bit is larger than the node's, we must bound it |
118 | | * because we might have compared too many bytes with an |
119 | | * inappropriate leaf. For a test, build a tree from "0", |
120 | | * "WW", "W", "S" inserted in this exact sequence and lookup |
121 | | * "W" => "S" is returned without this assignment. |
122 | | */ |
123 | 0 | else |
124 | 0 | bit = node_bit; |
125 | 0 | } |
126 | | |
127 | 0 | troot = node->node.branches.b[(((unsigned char*)x)[node_bit >> 3] >> |
128 | 0 | (~node_bit & 7)) & 1]; |
129 | 0 | } |
130 | 0 | } Unexecuted instantiation: stats.c:__ebst_lookup 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 Unexecuted instantiation: stats-file.c:__ebst_lookup |
131 | | |
132 | | /* Insert ebmb_node <new> into subtree starting at node root <root>. Only |
133 | | * new->key needs be set with the zero-terminated string key. The ebmb_node is |
134 | | * returned. If root->b[EB_RGHT]==1, the tree may only contain unique keys. The |
135 | | * caller is responsible for properly terminating the key with a zero. |
136 | | */ |
137 | | static forceinline struct ebmb_node * |
138 | | __ebst_insert(struct eb_root *root, struct ebmb_node *new) |
139 | 0 | { |
140 | 0 | struct ebmb_node *old; |
141 | 0 | unsigned int side; |
142 | 0 | eb_troot_t *troot; |
143 | 0 | eb_troot_t *root_right; |
144 | 0 | int diff; |
145 | 0 | int bit; |
146 | 0 | int old_node_bit; |
147 | |
|
148 | 0 | side = EB_LEFT; |
149 | 0 | troot = root->b[EB_LEFT]; |
150 | 0 | root_right = root->b[EB_RGHT]; |
151 | 0 | if (unlikely(troot == NULL)) { |
152 | | /* Tree is empty, insert the leaf part below the left branch */ |
153 | 0 | root->b[EB_LEFT] = eb_dotag(&new->node.branches, EB_LEAF); |
154 | 0 | new->node.leaf_p = eb_dotag(root, EB_LEFT); |
155 | 0 | new->node.node_p = NULL; /* node part unused */ |
156 | 0 | return new; |
157 | 0 | } |
158 | | |
159 | | /* The tree descent is fairly easy : |
160 | | * - first, check if we have reached a leaf node |
161 | | * - second, check if we have gone too far |
162 | | * - third, reiterate |
163 | | * Everywhere, we use <new> for the node node we are inserting, <root> |
164 | | * for the node we attach it to, and <old> for the node we are |
165 | | * displacing below <new>. <troot> will always point to the future node |
166 | | * (tagged with its type). <side> carries the side the node <new> is |
167 | | * attached to below its parent, which is also where previous node |
168 | | * was attached. |
169 | | */ |
170 | | |
171 | 0 | bit = 0; |
172 | 0 | while (1) { |
173 | 0 | if (unlikely(eb_gettag(troot) == EB_LEAF)) { |
174 | 0 | eb_troot_t *new_left, *new_rght; |
175 | 0 | eb_troot_t *new_leaf, *old_leaf; |
176 | |
|
177 | 0 | old = container_of(eb_untag(troot, EB_LEAF), |
178 | 0 | struct ebmb_node, node.branches); |
179 | |
|
180 | 0 | new_left = eb_dotag(&new->node.branches, EB_LEFT); |
181 | 0 | new_rght = eb_dotag(&new->node.branches, EB_RGHT); |
182 | 0 | new_leaf = eb_dotag(&new->node.branches, EB_LEAF); |
183 | 0 | old_leaf = eb_dotag(&old->node.branches, EB_LEAF); |
184 | |
|
185 | 0 | new->node.node_p = old->node.leaf_p; |
186 | | |
187 | | /* Right here, we have 3 possibilities : |
188 | | * - the tree does not contain the key, and we have |
189 | | * new->key < old->key. We insert new above old, on |
190 | | * the left ; |
191 | | * |
192 | | * - the tree does not contain the key, and we have |
193 | | * new->key > old->key. We insert new above old, on |
194 | | * the right ; |
195 | | * |
196 | | * - the tree does contain the key, which implies it |
197 | | * is alone. We add the new key next to it as a |
198 | | * first duplicate. |
199 | | * |
200 | | * The last two cases can easily be partially merged. |
201 | | */ |
202 | 0 | if (bit >= 0) |
203 | 0 | bit = string_equal_bits(new->key, old->key, bit); |
204 | |
|
205 | 0 | if (bit < 0) { |
206 | | /* key was already there */ |
207 | | |
208 | | /* we may refuse to duplicate this key if the tree is |
209 | | * tagged as containing only unique keys. |
210 | | */ |
211 | 0 | if (eb_gettag(root_right)) |
212 | 0 | return old; |
213 | | |
214 | | /* new arbitrarily goes to the right and tops the dup tree */ |
215 | 0 | old->node.leaf_p = new_left; |
216 | 0 | new->node.leaf_p = new_rght; |
217 | 0 | new->node.branches.b[EB_LEFT] = old_leaf; |
218 | 0 | new->node.branches.b[EB_RGHT] = new_leaf; |
219 | 0 | new->node.bit = -1; |
220 | 0 | root->b[side] = eb_dotag(&new->node.branches, EB_NODE); |
221 | 0 | return new; |
222 | 0 | } |
223 | | |
224 | 0 | diff = cmp_bits(new->key, old->key, bit); |
225 | 0 | if (diff < 0) { |
226 | | /* new->key < old->key, new takes the left */ |
227 | 0 | new->node.leaf_p = new_left; |
228 | 0 | old->node.leaf_p = new_rght; |
229 | 0 | new->node.branches.b[EB_LEFT] = new_leaf; |
230 | 0 | new->node.branches.b[EB_RGHT] = old_leaf; |
231 | 0 | } else { |
232 | | /* new->key > old->key, new takes the right */ |
233 | 0 | old->node.leaf_p = new_left; |
234 | 0 | new->node.leaf_p = new_rght; |
235 | 0 | new->node.branches.b[EB_LEFT] = old_leaf; |
236 | 0 | new->node.branches.b[EB_RGHT] = new_leaf; |
237 | 0 | } |
238 | 0 | break; |
239 | 0 | } |
240 | | |
241 | | /* OK we're walking down this link */ |
242 | 0 | old = container_of(eb_untag(troot, EB_NODE), |
243 | 0 | struct ebmb_node, node.branches); |
244 | |
|
245 | 0 | eb_prefetch(old->node.branches.b[0], 0); |
246 | 0 | eb_prefetch(old->node.branches.b[1], 0); |
247 | |
|
248 | 0 | old_node_bit = old->node.bit; |
249 | | |
250 | | /* Stop going down when we don't have common bits anymore. We |
251 | | * also stop in front of a duplicates tree because it means we |
252 | | * have to insert above. Note: we can compare more bits than |
253 | | * the current node's because as long as they are identical, we |
254 | | * know we descend along the correct side. |
255 | | */ |
256 | 0 | if (bit >= 0 && (bit < old_node_bit || old_node_bit < 0)) |
257 | 0 | bit = string_equal_bits(new->key, old->key, bit); |
258 | |
|
259 | 0 | if (unlikely(bit < 0)) { |
260 | | /* Perfect match, we must only stop on head of dup tree |
261 | | * or walk down to a leaf. |
262 | | */ |
263 | 0 | if (old_node_bit < 0) { |
264 | | /* We know here that string_equal_bits matched all |
265 | | * bits and that we're on top of a dup tree, then |
266 | | * we can perform the dup insertion and return. |
267 | | */ |
268 | 0 | struct eb_node *ret; |
269 | 0 | ret = eb_insert_dup(&old->node, &new->node); |
270 | 0 | return container_of(ret, struct ebmb_node, node); |
271 | 0 | } |
272 | | /* OK so let's walk down */ |
273 | 0 | } |
274 | 0 | else if (bit < old_node_bit || old_node_bit < 0) { |
275 | | /* The tree did not contain the key, or we stopped on top of a dup |
276 | | * tree, possibly containing the key. In the former case, we insert |
277 | | * <new> before the node <old>, and set ->bit to designate the lowest |
278 | | * bit position in <new> which applies to ->branches.b[]. In the later |
279 | | * case, we add the key to the existing dup tree. Note that we cannot |
280 | | * enter here if we match an intermediate node's key that is not the |
281 | | * head of a dup tree. |
282 | | */ |
283 | 0 | eb_troot_t *new_left, *new_rght; |
284 | 0 | eb_troot_t *new_leaf, *old_node; |
285 | |
|
286 | 0 | new_left = eb_dotag(&new->node.branches, EB_LEFT); |
287 | 0 | new_rght = eb_dotag(&new->node.branches, EB_RGHT); |
288 | 0 | new_leaf = eb_dotag(&new->node.branches, EB_LEAF); |
289 | 0 | old_node = eb_dotag(&old->node.branches, EB_NODE); |
290 | |
|
291 | 0 | new->node.node_p = old->node.node_p; |
292 | | |
293 | | /* we can never match all bits here */ |
294 | 0 | diff = cmp_bits(new->key, old->key, bit); |
295 | 0 | if (diff < 0) { |
296 | 0 | new->node.leaf_p = new_left; |
297 | 0 | old->node.node_p = new_rght; |
298 | 0 | new->node.branches.b[EB_LEFT] = new_leaf; |
299 | 0 | new->node.branches.b[EB_RGHT] = old_node; |
300 | 0 | } |
301 | 0 | else { |
302 | 0 | old->node.node_p = new_left; |
303 | 0 | new->node.leaf_p = new_rght; |
304 | 0 | new->node.branches.b[EB_LEFT] = old_node; |
305 | 0 | new->node.branches.b[EB_RGHT] = new_leaf; |
306 | 0 | } |
307 | 0 | break; |
308 | 0 | } |
309 | | |
310 | | /* walk down */ |
311 | 0 | root = &old->node.branches; |
312 | 0 | side = (new->key[old_node_bit >> 3] >> (~old_node_bit & 7)) & 1; |
313 | 0 | troot = root->b[side]; |
314 | 0 | } |
315 | | |
316 | | /* Ok, now we are inserting <new> between <root> and <old>. <old>'s |
317 | | * parent is already set to <new>, and the <root>'s branch is still in |
318 | | * <side>. Update the root's leaf till we have it. Note that we can also |
319 | | * find the side by checking the side of new->node.node_p. |
320 | | */ |
321 | | |
322 | | /* We need the common higher bits between new->key and old->key. |
323 | | * This number of bits is already in <bit>. |
324 | | * NOTE: we can't get here with bit < 0 since we found a dup ! |
325 | | */ |
326 | 0 | new->node.bit = bit; |
327 | 0 | root->b[side] = eb_dotag(&new->node.branches, EB_NODE); |
328 | 0 | return new; |
329 | 0 | } Unexecuted instantiation: stats.c:__ebst_insert 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 Unexecuted instantiation: stats-file.c:__ebst_insert |
330 | | |
331 | | #endif /* _EBSTTREE_H */ |
332 | | |