Willy Tarreau | c218602 | 2009-10-26 19:48:54 +0100 | [diff] [blame^] | 1 | /* |
| 2 | * Elastic Binary Trees - macros and structures for 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 | |
| 24 | /* Return the structure of type <type> whose member <member> points to <ptr> */ |
| 25 | #define ebmb_entry(ptr, type, member) container_of(ptr, type, member) |
| 26 | |
| 27 | #define EBMB_ROOT EB_ROOT |
| 28 | #define EBMB_TREE_HEAD EB_TREE_HEAD |
| 29 | |
| 30 | /* This structure carries a node, a leaf, and a key. It must start with the |
| 31 | * eb_node so that it can be cast into an eb_node. We could also have put some |
| 32 | * sort of transparent union here to reduce the indirection level, but the fact |
| 33 | * is, the end user is not meant to manipulate internals, so this is pointless. |
| 34 | * The 'node.bit' value here works differently from scalar types, as it contains |
| 35 | * the number of identical bits between the two branches. |
| 36 | */ |
| 37 | struct ebmb_node { |
| 38 | struct eb_node node; /* the tree node, must be at the beginning */ |
| 39 | unsigned char key[0]; /* the key, its size depends on the application */ |
| 40 | }; |
| 41 | |
| 42 | /* |
| 43 | * Exported functions and macros. |
| 44 | * Many of them are always inlined because they are extremely small, and |
| 45 | * are generally called at most once or twice in a program. |
| 46 | */ |
| 47 | |
| 48 | /* Return leftmost node in the tree, or NULL if none */ |
| 49 | static forceinline struct ebmb_node *ebmb_first(struct eb_root *root) |
| 50 | { |
| 51 | return ebmb_entry(eb_first(root), struct ebmb_node, node); |
| 52 | } |
| 53 | |
| 54 | /* Return rightmost node in the tree, or NULL if none */ |
| 55 | static forceinline struct ebmb_node *ebmb_last(struct eb_root *root) |
| 56 | { |
| 57 | return ebmb_entry(eb_last(root), struct ebmb_node, node); |
| 58 | } |
| 59 | |
| 60 | /* Return next node in the tree, or NULL if none */ |
| 61 | static forceinline struct ebmb_node *ebmb_next(struct ebmb_node *ebmb) |
| 62 | { |
| 63 | return ebmb_entry(eb_next(&ebmb->node), struct ebmb_node, node); |
| 64 | } |
| 65 | |
| 66 | /* Return previous node in the tree, or NULL if none */ |
| 67 | static forceinline struct ebmb_node *ebmb_prev(struct ebmb_node *ebmb) |
| 68 | { |
| 69 | return ebmb_entry(eb_prev(&ebmb->node), struct ebmb_node, node); |
| 70 | } |
| 71 | |
| 72 | /* Return next node in the tree, skipping duplicates, or NULL if none */ |
| 73 | static forceinline struct ebmb_node *ebmb_next_unique(struct ebmb_node *ebmb) |
| 74 | { |
| 75 | return ebmb_entry(eb_next_unique(&ebmb->node), struct ebmb_node, node); |
| 76 | } |
| 77 | |
| 78 | /* Return previous node in the tree, skipping duplicates, or NULL if none */ |
| 79 | static forceinline struct ebmb_node *ebmb_prev_unique(struct ebmb_node *ebmb) |
| 80 | { |
| 81 | return ebmb_entry(eb_prev_unique(&ebmb->node), struct ebmb_node, node); |
| 82 | } |
| 83 | |
| 84 | /* Delete node from the tree if it was linked in. Mark the node unused. Note |
| 85 | * that this function relies on a non-inlined generic function: eb_delete. |
| 86 | */ |
| 87 | static forceinline void ebmb_delete(struct ebmb_node *ebmb) |
| 88 | { |
| 89 | eb_delete(&ebmb->node); |
| 90 | } |
| 91 | |
| 92 | /* The following functions are not inlined by default. They are declared |
| 93 | * in ebmbtree.c, which simply relies on their inline version. |
| 94 | */ |
| 95 | REGPRM3 struct ebmb_node *ebmb_lookup(struct eb_root *root, const void *x, unsigned int len); |
| 96 | REGPRM3 struct ebmb_node *ebmb_insert(struct eb_root *root, struct ebmb_node *new, unsigned int len); |
| 97 | |
| 98 | /* The following functions are less likely to be used directly, because their |
| 99 | * code is larger. The non-inlined version is preferred. |
| 100 | */ |
| 101 | |
| 102 | /* Delete node from the tree if it was linked in. Mark the node unused. */ |
| 103 | static forceinline void __ebmb_delete(struct ebmb_node *ebmb) |
| 104 | { |
| 105 | __eb_delete(&ebmb->node); |
| 106 | } |
| 107 | |
| 108 | /* Find the first occurence of a key of <len> bytes in the tree <root>. |
| 109 | * If none can be found, return NULL. |
| 110 | */ |
| 111 | static forceinline struct ebmb_node *__ebmb_lookup(struct eb_root *root, const void *x, unsigned int len) |
| 112 | { |
| 113 | struct ebmb_node *node; |
| 114 | eb_troot_t *troot; |
| 115 | unsigned int bit; |
| 116 | |
| 117 | troot = root->b[EB_LEFT]; |
| 118 | if (unlikely(troot == NULL)) |
| 119 | return NULL; |
| 120 | |
| 121 | bit = 0; |
| 122 | while (1) { |
| 123 | if ((eb_gettag(troot) == EB_LEAF)) { |
| 124 | node = container_of(eb_untag(troot, EB_LEAF), |
| 125 | struct ebmb_node, node.branches); |
| 126 | if (memcmp(node->key, x, len) == 0) |
| 127 | return node; |
| 128 | else |
| 129 | return NULL; |
| 130 | } |
| 131 | node = container_of(eb_untag(troot, EB_NODE), |
| 132 | struct ebmb_node, node.branches); |
| 133 | |
| 134 | if (node->node.bit < 0) { |
| 135 | /* We have a dup tree now. Either it's for the same |
| 136 | * value, and we walk down left, or it's a different |
| 137 | * one and we don't have our key. |
| 138 | */ |
| 139 | if (memcmp(node->key, x, len) != 0) |
| 140 | return NULL; |
| 141 | |
| 142 | troot = node->node.branches.b[EB_LEFT]; |
| 143 | while (eb_gettag(troot) != EB_LEAF) |
| 144 | troot = (eb_untag(troot, EB_NODE))->b[EB_LEFT]; |
| 145 | node = container_of(eb_untag(troot, EB_LEAF), |
| 146 | struct ebmb_node, node.branches); |
| 147 | return node; |
| 148 | } |
| 149 | |
| 150 | /* OK, normal data node, let's walk down */ |
| 151 | bit = equal_bits(x, node->key, bit, node->node.bit); |
| 152 | if (bit < node->node.bit) |
| 153 | return NULL; /* no more common bits */ |
| 154 | |
| 155 | troot = node->node.branches.b[(((unsigned char*)x)[node->node.bit >> 3] >> |
| 156 | (~node->node.bit & 7)) & 1]; |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | /* Insert ebmb_node <new> into subtree starting at node root <root>. |
| 161 | * Only new->key needs be set with the key. The ebmb_node is returned. |
| 162 | * If root->b[EB_RGHT]==1, the tree may only contain unique keys. The |
| 163 | * len is specified in bytes. |
| 164 | */ |
| 165 | static forceinline struct ebmb_node * |
| 166 | __ebmb_insert(struct eb_root *root, struct ebmb_node *new, unsigned int len) |
| 167 | { |
| 168 | struct ebmb_node *old; |
| 169 | unsigned int side; |
| 170 | eb_troot_t *troot; |
| 171 | eb_troot_t *root_right = root; |
| 172 | int diff; |
| 173 | int bit; |
| 174 | |
| 175 | side = EB_LEFT; |
| 176 | troot = root->b[EB_LEFT]; |
| 177 | root_right = root->b[EB_RGHT]; |
| 178 | if (unlikely(troot == NULL)) { |
| 179 | /* Tree is empty, insert the leaf part below the left branch */ |
| 180 | root->b[EB_LEFT] = eb_dotag(&new->node.branches, EB_LEAF); |
| 181 | new->node.leaf_p = eb_dotag(root, EB_LEFT); |
| 182 | new->node.node_p = NULL; /* node part unused */ |
| 183 | return new; |
| 184 | } |
| 185 | |
| 186 | len <<= 3; |
| 187 | |
| 188 | /* The tree descent is fairly easy : |
| 189 | * - first, check if we have reached a leaf node |
| 190 | * - second, check if we have gone too far |
| 191 | * - third, reiterate |
| 192 | * Everywhere, we use <new> for the node node we are inserting, <root> |
| 193 | * for the node we attach it to, and <old> for the node we are |
| 194 | * displacing below <new>. <troot> will always point to the future node |
| 195 | * (tagged with its type). <side> carries the side the node <new> is |
| 196 | * attached to below its parent, which is also where previous node |
| 197 | * was attached. |
| 198 | */ |
| 199 | |
| 200 | bit = 0; |
| 201 | while (1) { |
| 202 | if (unlikely(eb_gettag(troot) == EB_LEAF)) { |
| 203 | eb_troot_t *new_left, *new_rght; |
| 204 | eb_troot_t *new_leaf, *old_leaf; |
| 205 | |
| 206 | old = container_of(eb_untag(troot, EB_LEAF), |
| 207 | struct ebmb_node, node.branches); |
| 208 | |
| 209 | new_left = eb_dotag(&new->node.branches, EB_LEFT); |
| 210 | new_rght = eb_dotag(&new->node.branches, EB_RGHT); |
| 211 | new_leaf = eb_dotag(&new->node.branches, EB_LEAF); |
| 212 | old_leaf = eb_dotag(&old->node.branches, EB_LEAF); |
| 213 | |
| 214 | new->node.node_p = old->node.leaf_p; |
| 215 | |
| 216 | /* Right here, we have 3 possibilities : |
| 217 | * - the tree does not contain the key, and we have |
| 218 | * new->key < old->key. We insert new above old, on |
| 219 | * the left ; |
| 220 | * |
| 221 | * - the tree does not contain the key, and we have |
| 222 | * new->key > old->key. We insert new above old, on |
| 223 | * the right ; |
| 224 | * |
| 225 | * - the tree does contain the key, which implies it |
| 226 | * is alone. We add the new key next to it as a |
| 227 | * first duplicate. |
| 228 | * |
| 229 | * The last two cases can easily be partially merged. |
| 230 | */ |
| 231 | bit = equal_bits(new->key, old->key, bit, len); |
| 232 | diff = cmp_bits(new->key, old->key, bit); |
| 233 | |
| 234 | if (diff < 0) { |
| 235 | new->node.leaf_p = new_left; |
| 236 | old->node.leaf_p = new_rght; |
| 237 | new->node.branches.b[EB_LEFT] = new_leaf; |
| 238 | new->node.branches.b[EB_RGHT] = old_leaf; |
| 239 | } else { |
| 240 | /* we may refuse to duplicate this key if the tree is |
| 241 | * tagged as containing only unique keys. |
| 242 | */ |
| 243 | if (diff == 0 && eb_gettag(root_right)) |
| 244 | return old; |
| 245 | |
| 246 | /* new->key >= old->key, new goes the right */ |
| 247 | old->node.leaf_p = new_left; |
| 248 | new->node.leaf_p = new_rght; |
| 249 | new->node.branches.b[EB_LEFT] = old_leaf; |
| 250 | new->node.branches.b[EB_RGHT] = new_leaf; |
| 251 | |
| 252 | if (diff == 0) { |
| 253 | new->node.bit = -1; |
| 254 | root->b[side] = eb_dotag(&new->node.branches, EB_NODE); |
| 255 | return new; |
| 256 | } |
| 257 | } |
| 258 | break; |
| 259 | } |
| 260 | |
| 261 | /* OK we're walking down this link */ |
| 262 | old = container_of(eb_untag(troot, EB_NODE), |
| 263 | struct ebmb_node, node.branches); |
| 264 | |
| 265 | /* Stop going down when we don't have common bits anymore. We |
| 266 | * also stop in front of a duplicates tree because it means we |
| 267 | * have to insert above. Note: we can compare more bits than |
| 268 | * the current node's because as long as they are identical, we |
| 269 | * know we descend along the correct side. |
| 270 | */ |
| 271 | if (old->node.bit < 0) { |
| 272 | /* we're above a duplicate tree, we must compare till the end */ |
| 273 | bit = equal_bits(new->key, old->key, bit, len); |
| 274 | goto dup_tree; |
| 275 | } |
| 276 | else if (bit < old->node.bit) { |
| 277 | bit = equal_bits(new->key, old->key, bit, old->node.bit); |
| 278 | } |
| 279 | |
| 280 | if (bit < old->node.bit) { /* we don't have all bits in common */ |
| 281 | /* The tree did not contain the key, so we insert <new> before the node |
| 282 | * <old>, and set ->bit to designate the lowest bit position in <new> |
| 283 | * which applies to ->branches.b[]. |
| 284 | */ |
| 285 | eb_troot_t *new_left, *new_rght; |
| 286 | eb_troot_t *new_leaf, *old_node; |
| 287 | |
| 288 | dup_tree: |
| 289 | new_left = eb_dotag(&new->node.branches, EB_LEFT); |
| 290 | new_rght = eb_dotag(&new->node.branches, EB_RGHT); |
| 291 | new_leaf = eb_dotag(&new->node.branches, EB_LEAF); |
| 292 | old_node = eb_dotag(&old->node.branches, EB_NODE); |
| 293 | |
| 294 | new->node.node_p = old->node.node_p; |
| 295 | |
| 296 | diff = cmp_bits(new->key, old->key, bit); |
| 297 | if (diff < 0) { |
| 298 | new->node.leaf_p = new_left; |
| 299 | old->node.node_p = new_rght; |
| 300 | new->node.branches.b[EB_LEFT] = new_leaf; |
| 301 | new->node.branches.b[EB_RGHT] = old_node; |
| 302 | } |
| 303 | else if (diff > 0) { |
| 304 | old->node.node_p = new_left; |
| 305 | new->node.leaf_p = new_rght; |
| 306 | new->node.branches.b[EB_LEFT] = old_node; |
| 307 | new->node.branches.b[EB_RGHT] = new_leaf; |
| 308 | } |
| 309 | else { |
| 310 | struct eb_node *ret; |
| 311 | ret = eb_insert_dup(&old->node, &new->node); |
| 312 | return container_of(ret, struct ebmb_node, node); |
| 313 | } |
| 314 | break; |
| 315 | } |
| 316 | |
| 317 | /* walk down */ |
| 318 | root = &old->node.branches; |
| 319 | side = (new->key[old->node.bit >> 3] >> (~old->node.bit & 7)) & 1; |
| 320 | troot = root->b[side]; |
| 321 | } |
| 322 | |
| 323 | /* Ok, now we are inserting <new> between <root> and <old>. <old>'s |
| 324 | * parent is already set to <new>, and the <root>'s branch is still in |
| 325 | * <side>. Update the root's leaf till we have it. Note that we can also |
| 326 | * find the side by checking the side of new->node.node_p. |
| 327 | */ |
| 328 | |
| 329 | /* We need the common higher bits between new->key and old->key. |
| 330 | * This number of bits is already in <bit>. |
| 331 | */ |
| 332 | new->node.bit = bit; |
| 333 | root->b[side] = eb_dotag(&new->node.branches, EB_NODE); |
| 334 | return new; |
| 335 | } |
| 336 | |