Willy Tarreau | c218602 | 2009-10-26 19:48:54 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Elastic Binary Trees - macros for Indirect Multi-Byte data nodes. |
Willy Tarreau | f3bfede | 2011-07-25 11:38:17 +0200 | [diff] [blame] | 3 | * Version 6.0.6 |
Willy Tarreau | 414c4b2 | 2011-01-04 13:21:06 +0100 | [diff] [blame] | 4 | * (C) 2002-2011 - Willy Tarreau <w@1wt.eu> |
Willy Tarreau | c218602 | 2009-10-26 19:48:54 +0100 | [diff] [blame] | 5 | * |
Willy Tarreau | f3bfede | 2011-07-25 11:38:17 +0200 | [diff] [blame] | 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. |
Willy Tarreau | c218602 | 2009-10-26 19:48:54 +0100 | [diff] [blame] | 10 | * |
Willy Tarreau | f3bfede | 2011-07-25 11:38:17 +0200 | [diff] [blame] | 11 | * This library is distributed in the hope that it will be useful, |
Willy Tarreau | c218602 | 2009-10-26 19:48:54 +0100 | [diff] [blame] | 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
Willy Tarreau | f3bfede | 2011-07-25 11:38:17 +0200 | [diff] [blame] | 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 | * Lesser General Public License for more details. |
Willy Tarreau | c218602 | 2009-10-26 19:48:54 +0100 | [diff] [blame] | 15 | * |
Willy Tarreau | f3bfede | 2011-07-25 11:38:17 +0200 | [diff] [blame] | 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 |
Willy Tarreau | c218602 | 2009-10-26 19:48:54 +0100 | [diff] [blame] | 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 | */ |
| 32 | REGPRM3 struct ebpt_node *ebim_lookup(struct eb_root *root, const void *x, unsigned int len); |
| 33 | REGPRM3 struct ebpt_node *ebim_insert(struct eb_root *root, struct ebpt_node *new, unsigned int len); |
| 34 | |
Willy Tarreau | 414c4b2 | 2011-01-04 13:21:06 +0100 | [diff] [blame] | 35 | /* 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 Tarreau | c218602 | 2009-10-26 19:48:54 +0100 | [diff] [blame] | 41 | */ |
| 42 | static 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 Tarreau | 414c4b2 | 2011-01-04 13:21:06 +0100 | [diff] [blame] | 47 | int pos, side; |
Willy Tarreau | 3a93244 | 2010-05-09 19:29:23 +0200 | [diff] [blame] | 48 | int node_bit; |
Willy Tarreau | c218602 | 2009-10-26 19:48:54 +0100 | [diff] [blame] | 49 | |
| 50 | troot = root->b[EB_LEFT]; |
| 51 | if (unlikely(troot == NULL)) |
Willy Tarreau | ce3d44a | 2011-01-04 14:07:36 +0100 | [diff] [blame] | 52 | goto ret_null; |
Willy Tarreau | c218602 | 2009-10-26 19:48:54 +0100 | [diff] [blame] | 53 | |
Willy Tarreau | 414c4b2 | 2011-01-04 13:21:06 +0100 | [diff] [blame] | 54 | if (unlikely(len == 0)) |
| 55 | goto walk_down; |
| 56 | |
| 57 | pos = 0; |
Willy Tarreau | c218602 | 2009-10-26 19:48:54 +0100 | [diff] [blame] | 58 | while (1) { |
Willy Tarreau | 414c4b2 | 2011-01-04 13:21:06 +0100 | [diff] [blame] | 59 | if (eb_gettag(troot) == EB_LEAF) { |
Willy Tarreau | c218602 | 2009-10-26 19:48:54 +0100 | [diff] [blame] | 60 | node = container_of(eb_untag(troot, EB_LEAF), |
| 61 | struct ebpt_node, node.branches); |
Willy Tarreau | 414c4b2 | 2011-01-04 13:21:06 +0100 | [diff] [blame] | 62 | if (memcmp(node->key + pos, x, len) != 0) |
Willy Tarreau | ce3d44a | 2011-01-04 14:07:36 +0100 | [diff] [blame] | 63 | goto ret_null; |
Willy Tarreau | 414c4b2 | 2011-01-04 13:21:06 +0100 | [diff] [blame] | 64 | else |
Willy Tarreau | ce3d44a | 2011-01-04 14:07:36 +0100 | [diff] [blame] | 65 | goto ret_node; |
Willy Tarreau | c218602 | 2009-10-26 19:48:54 +0100 | [diff] [blame] | 66 | } |
| 67 | node = container_of(eb_untag(troot, EB_NODE), |
| 68 | struct ebpt_node, node.branches); |
| 69 | |
Willy Tarreau | 3a93244 | 2010-05-09 19:29:23 +0200 | [diff] [blame] | 70 | node_bit = node->node.bit; |
| 71 | if (node_bit < 0) { |
Willy Tarreau | c218602 | 2009-10-26 19:48:54 +0100 | [diff] [blame] | 72 | /* 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 Tarreau | 414c4b2 | 2011-01-04 13:21:06 +0100 | [diff] [blame] | 76 | if (memcmp(node->key + pos, x, len) != 0) |
Willy Tarreau | ce3d44a | 2011-01-04 14:07:36 +0100 | [diff] [blame] | 77 | goto ret_null; |
| 78 | else |
| 79 | goto walk_left; |
Willy Tarreau | c218602 | 2009-10-26 19:48:54 +0100 | [diff] [blame] | 80 | } |
| 81 | |
Willy Tarreau | 414c4b2 | 2011-01-04 13:21:06 +0100 | [diff] [blame] | 82 | /* OK, normal data node, let's walk down. We check if all full |
| 83 | * bytes are equal, and we start from the last one we did not |
| 84 | * completely check. We stop as soon as we reach the last byte, |
| 85 | * because we must decide to go left/right or abort. |
| 86 | */ |
| 87 | node_bit = ~node_bit + (pos << 3) + 8; // = (pos<<3) + (7 - node_bit) |
| 88 | if (node_bit < 0) { |
| 89 | /* This surprizing construction gives better performance |
| 90 | * because gcc does not try to reorder the loop. Tested to |
| 91 | * be fine with 2.95 to 4.2. |
| 92 | */ |
| 93 | while (1) { |
| 94 | if (*(unsigned char*)(node->key + pos++) ^ *(unsigned char*)(x++)) |
Willy Tarreau | ce3d44a | 2011-01-04 14:07:36 +0100 | [diff] [blame] | 95 | goto ret_null; /* more than one full byte is different */ |
Willy Tarreau | 414c4b2 | 2011-01-04 13:21:06 +0100 | [diff] [blame] | 96 | if (--len == 0) |
| 97 | goto walk_left; /* return first node if all bytes matched */ |
| 98 | node_bit += 8; |
| 99 | if (node_bit >= 0) |
| 100 | break; |
| 101 | } |
| 102 | } |
Willy Tarreau | c218602 | 2009-10-26 19:48:54 +0100 | [diff] [blame] | 103 | |
Willy Tarreau | 414c4b2 | 2011-01-04 13:21:06 +0100 | [diff] [blame] | 104 | /* here we know that only the last byte differs, so node_bit < 8. |
| 105 | * We have 2 possibilities : |
| 106 | * - more than the last bit differs => return NULL |
| 107 | * - walk down on side = (x[pos] >> node_bit) & 1 |
| 108 | */ |
| 109 | side = *(unsigned char *)x >> node_bit; |
| 110 | if (((*(unsigned char*)(node->key + pos) >> node_bit) ^ side) > 1) |
Willy Tarreau | ce3d44a | 2011-01-04 14:07:36 +0100 | [diff] [blame] | 111 | goto ret_null; |
Willy Tarreau | 414c4b2 | 2011-01-04 13:21:06 +0100 | [diff] [blame] | 112 | side &= 1; |
| 113 | troot = node->node.branches.b[side]; |
Willy Tarreau | c218602 | 2009-10-26 19:48:54 +0100 | [diff] [blame] | 114 | } |
Willy Tarreau | ce3d44a | 2011-01-04 14:07:36 +0100 | [diff] [blame] | 115 | walk_left: |
| 116 | troot = node->node.branches.b[EB_LEFT]; |
| 117 | walk_down: |
| 118 | while (eb_gettag(troot) != EB_LEAF) |
| 119 | troot = (eb_untag(troot, EB_NODE))->b[EB_LEFT]; |
| 120 | node = container_of(eb_untag(troot, EB_LEAF), |
| 121 | struct ebpt_node, node.branches); |
| 122 | ret_node: |
| 123 | return node; |
| 124 | ret_null: |
| 125 | return NULL; |
Willy Tarreau | c218602 | 2009-10-26 19:48:54 +0100 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | /* Insert ebpt_node <new> into subtree starting at node root <root>. |
| 129 | * Only new->key needs be set with the key. The ebpt_node is returned. |
| 130 | * If root->b[EB_RGHT]==1, the tree may only contain unique keys. The |
| 131 | * len is specified in bytes. |
| 132 | */ |
| 133 | static forceinline struct ebpt_node * |
| 134 | __ebim_insert(struct eb_root *root, struct ebpt_node *new, unsigned int len) |
| 135 | { |
| 136 | struct ebpt_node *old; |
| 137 | unsigned int side; |
| 138 | eb_troot_t *troot; |
Willy Tarreau | 6258f7b | 2011-09-19 20:48:00 +0200 | [diff] [blame] | 139 | eb_troot_t *root_right; |
Willy Tarreau | c218602 | 2009-10-26 19:48:54 +0100 | [diff] [blame] | 140 | int diff; |
| 141 | int bit; |
Willy Tarreau | 3a93244 | 2010-05-09 19:29:23 +0200 | [diff] [blame] | 142 | int old_node_bit; |
Willy Tarreau | c218602 | 2009-10-26 19:48:54 +0100 | [diff] [blame] | 143 | |
| 144 | side = EB_LEFT; |
| 145 | troot = root->b[EB_LEFT]; |
| 146 | root_right = root->b[EB_RGHT]; |
| 147 | if (unlikely(troot == NULL)) { |
| 148 | /* Tree is empty, insert the leaf part below the left branch */ |
| 149 | root->b[EB_LEFT] = eb_dotag(&new->node.branches, EB_LEAF); |
| 150 | new->node.leaf_p = eb_dotag(root, EB_LEFT); |
| 151 | new->node.node_p = NULL; /* node part unused */ |
| 152 | return new; |
| 153 | } |
| 154 | |
| 155 | len <<= 3; |
| 156 | |
| 157 | /* The tree descent is fairly easy : |
| 158 | * - first, check if we have reached a leaf node |
| 159 | * - second, check if we have gone too far |
| 160 | * - third, reiterate |
| 161 | * Everywhere, we use <new> for the node node we are inserting, <root> |
| 162 | * for the node we attach it to, and <old> for the node we are |
| 163 | * displacing below <new>. <troot> will always point to the future node |
| 164 | * (tagged with its type). <side> carries the side the node <new> is |
| 165 | * attached to below its parent, which is also where previous node |
| 166 | * was attached. |
| 167 | */ |
| 168 | |
| 169 | bit = 0; |
| 170 | while (1) { |
| 171 | if (unlikely(eb_gettag(troot) == EB_LEAF)) { |
| 172 | eb_troot_t *new_left, *new_rght; |
| 173 | eb_troot_t *new_leaf, *old_leaf; |
| 174 | |
| 175 | old = container_of(eb_untag(troot, EB_LEAF), |
| 176 | struct ebpt_node, node.branches); |
| 177 | |
| 178 | new_left = eb_dotag(&new->node.branches, EB_LEFT); |
| 179 | new_rght = eb_dotag(&new->node.branches, EB_RGHT); |
| 180 | new_leaf = eb_dotag(&new->node.branches, EB_LEAF); |
| 181 | old_leaf = eb_dotag(&old->node.branches, EB_LEAF); |
| 182 | |
| 183 | new->node.node_p = old->node.leaf_p; |
| 184 | |
| 185 | /* Right here, we have 3 possibilities : |
| 186 | * - the tree does not contain the key, and we have |
| 187 | * new->key < old->key. We insert new above old, on |
| 188 | * the left ; |
| 189 | * |
| 190 | * - the tree does not contain the key, and we have |
| 191 | * new->key > old->key. We insert new above old, on |
| 192 | * the right ; |
| 193 | * |
| 194 | * - the tree does contain the key, which implies it |
| 195 | * is alone. We add the new key next to it as a |
| 196 | * first duplicate. |
| 197 | * |
| 198 | * The last two cases can easily be partially merged. |
| 199 | */ |
| 200 | bit = equal_bits(new->key, old->key, bit, len); |
Willy Tarreau | a4a1cd1 | 2012-06-09 15:43:36 +0200 | [diff] [blame^] | 201 | |
| 202 | /* Note: we can compare more bits than the current node's because as |
| 203 | * long as they are identical, we know we descend along the correct |
| 204 | * side. However we don't want to start to compare past the end. |
| 205 | */ |
| 206 | diff = 0; |
| 207 | if (((unsigned)bit >> 3) < len) |
| 208 | diff = cmp_bits(new->key, old->key, bit); |
Willy Tarreau | c218602 | 2009-10-26 19:48:54 +0100 | [diff] [blame] | 209 | |
| 210 | if (diff < 0) { |
| 211 | new->node.leaf_p = new_left; |
| 212 | old->node.leaf_p = new_rght; |
| 213 | new->node.branches.b[EB_LEFT] = new_leaf; |
| 214 | new->node.branches.b[EB_RGHT] = old_leaf; |
| 215 | } else { |
| 216 | /* we may refuse to duplicate this key if the tree is |
| 217 | * tagged as containing only unique keys. |
| 218 | */ |
| 219 | if (diff == 0 && eb_gettag(root_right)) |
| 220 | return old; |
| 221 | |
| 222 | /* new->key >= old->key, new goes the right */ |
| 223 | old->node.leaf_p = new_left; |
| 224 | new->node.leaf_p = new_rght; |
| 225 | new->node.branches.b[EB_LEFT] = old_leaf; |
| 226 | new->node.branches.b[EB_RGHT] = new_leaf; |
| 227 | |
| 228 | if (diff == 0) { |
| 229 | new->node.bit = -1; |
| 230 | root->b[side] = eb_dotag(&new->node.branches, EB_NODE); |
| 231 | return new; |
| 232 | } |
| 233 | } |
| 234 | break; |
| 235 | } |
| 236 | |
| 237 | /* OK we're walking down this link */ |
| 238 | old = container_of(eb_untag(troot, EB_NODE), |
| 239 | struct ebpt_node, node.branches); |
Willy Tarreau | 3a93244 | 2010-05-09 19:29:23 +0200 | [diff] [blame] | 240 | old_node_bit = old->node.bit; |
Willy Tarreau | c218602 | 2009-10-26 19:48:54 +0100 | [diff] [blame] | 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 | */ |
Willy Tarreau | 3a93244 | 2010-05-09 19:29:23 +0200 | [diff] [blame] | 248 | if (old_node_bit < 0) { |
Willy Tarreau | c218602 | 2009-10-26 19:48:54 +0100 | [diff] [blame] | 249 | /* we're above a duplicate tree, we must compare till the end */ |
| 250 | bit = equal_bits(new->key, old->key, bit, len); |
| 251 | goto dup_tree; |
| 252 | } |
Willy Tarreau | 3a93244 | 2010-05-09 19:29:23 +0200 | [diff] [blame] | 253 | else if (bit < old_node_bit) { |
| 254 | bit = equal_bits(new->key, old->key, bit, old_node_bit); |
Willy Tarreau | c218602 | 2009-10-26 19:48:54 +0100 | [diff] [blame] | 255 | } |
| 256 | |
Willy Tarreau | 3a93244 | 2010-05-09 19:29:23 +0200 | [diff] [blame] | 257 | if (bit < old_node_bit) { /* we don't have all bits in common */ |
Willy Tarreau | c218602 | 2009-10-26 19:48:54 +0100 | [diff] [blame] | 258 | /* The tree did not contain the key, so we insert <new> before the node |
| 259 | * <old>, and set ->bit to designate the lowest bit position in <new> |
| 260 | * which applies to ->branches.b[]. |
| 261 | */ |
| 262 | eb_troot_t *new_left, *new_rght; |
| 263 | eb_troot_t *new_leaf, *old_node; |
| 264 | |
| 265 | dup_tree: |
| 266 | new_left = eb_dotag(&new->node.branches, EB_LEFT); |
| 267 | new_rght = eb_dotag(&new->node.branches, EB_RGHT); |
| 268 | new_leaf = eb_dotag(&new->node.branches, EB_LEAF); |
| 269 | old_node = eb_dotag(&old->node.branches, EB_NODE); |
| 270 | |
| 271 | new->node.node_p = old->node.node_p; |
| 272 | |
Willy Tarreau | a4a1cd1 | 2012-06-09 15:43:36 +0200 | [diff] [blame^] | 273 | /* Note: we can compare more bits than the current node's because as |
| 274 | * long as they are identical, we know we descend along the correct |
| 275 | * side. However we don't want to start to compare past the end. |
| 276 | */ |
| 277 | diff = 0; |
| 278 | if (((unsigned)bit >> 3) < len) |
| 279 | diff = cmp_bits(new->key, old->key, bit); |
| 280 | |
Willy Tarreau | c218602 | 2009-10-26 19:48:54 +0100 | [diff] [blame] | 281 | if (diff < 0) { |
| 282 | new->node.leaf_p = new_left; |
| 283 | old->node.node_p = new_rght; |
| 284 | new->node.branches.b[EB_LEFT] = new_leaf; |
| 285 | new->node.branches.b[EB_RGHT] = old_node; |
| 286 | } |
| 287 | else if (diff > 0) { |
| 288 | old->node.node_p = new_left; |
| 289 | new->node.leaf_p = new_rght; |
| 290 | new->node.branches.b[EB_LEFT] = old_node; |
| 291 | new->node.branches.b[EB_RGHT] = new_leaf; |
| 292 | } |
| 293 | else { |
| 294 | struct eb_node *ret; |
| 295 | ret = eb_insert_dup(&old->node, &new->node); |
| 296 | return container_of(ret, struct ebpt_node, node); |
| 297 | } |
| 298 | break; |
| 299 | } |
| 300 | |
| 301 | /* walk down */ |
| 302 | root = &old->node.branches; |
Willy Tarreau | 3a93244 | 2010-05-09 19:29:23 +0200 | [diff] [blame] | 303 | side = (((unsigned char *)new->key)[old_node_bit >> 3] >> (~old_node_bit & 7)) & 1; |
Willy Tarreau | c218602 | 2009-10-26 19:48:54 +0100 | [diff] [blame] | 304 | troot = root->b[side]; |
| 305 | } |
| 306 | |
| 307 | /* Ok, now we are inserting <new> between <root> and <old>. <old>'s |
| 308 | * parent is already set to <new>, and the <root>'s branch is still in |
| 309 | * <side>. Update the root's leaf till we have it. Note that we can also |
| 310 | * find the side by checking the side of new->node.node_p. |
| 311 | */ |
| 312 | |
| 313 | /* We need the common higher bits between new->key and old->key. |
| 314 | * This number of bits is already in <bit>. |
| 315 | */ |
| 316 | new->node.bit = bit; |
| 317 | root->b[side] = eb_dotag(&new->node.branches, EB_NODE); |
| 318 | return new; |
| 319 | } |