Willy Tarreau | c218602 | 2009-10-26 19:48:54 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Elastic Binary Trees - macros and structures for operations on 64bit nodes. |
Willy Tarreau | 3a93244 | 2010-05-09 19:29:23 +0200 | [diff] [blame] | 3 | * Version 6.0 |
| 4 | * (C) 2002-2010 - Willy Tarreau <w@1wt.eu> |
Willy Tarreau | c218602 | 2009-10-26 19:48:54 +0100 | [diff] [blame] | 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 | #ifndef _EB64TREE_H |
| 22 | #define _EB64TREE_H |
| 23 | |
| 24 | #include "ebtree.h" |
| 25 | |
| 26 | |
| 27 | /* Return the structure of type <type> whose member <member> points to <ptr> */ |
| 28 | #define eb64_entry(ptr, type, member) container_of(ptr, type, member) |
| 29 | |
| 30 | #define EB64_ROOT EB_ROOT |
| 31 | #define EB64_TREE_HEAD EB_TREE_HEAD |
| 32 | |
| 33 | /* These types may sometimes already be defined */ |
| 34 | typedef unsigned long long u64; |
| 35 | typedef signed long long s64; |
| 36 | |
| 37 | /* This structure carries a node, a leaf, and a key. It must start with the |
| 38 | * eb_node so that it can be cast into an eb_node. We could also have put some |
| 39 | * sort of transparent union here to reduce the indirection level, but the fact |
| 40 | * is, the end user is not meant to manipulate internals, so this is pointless. |
| 41 | */ |
| 42 | struct eb64_node { |
| 43 | struct eb_node node; /* the tree node, must be at the beginning */ |
| 44 | u64 key; |
| 45 | }; |
| 46 | |
| 47 | /* |
| 48 | * Exported functions and macros. |
| 49 | * Many of them are always inlined because they are extremely small, and |
| 50 | * are generally called at most once or twice in a program. |
| 51 | */ |
| 52 | |
| 53 | /* Return leftmost node in the tree, or NULL if none */ |
| 54 | static inline struct eb64_node *eb64_first(struct eb_root *root) |
| 55 | { |
| 56 | return eb64_entry(eb_first(root), struct eb64_node, node); |
| 57 | } |
| 58 | |
| 59 | /* Return rightmost node in the tree, or NULL if none */ |
| 60 | static inline struct eb64_node *eb64_last(struct eb_root *root) |
| 61 | { |
| 62 | return eb64_entry(eb_last(root), struct eb64_node, node); |
| 63 | } |
| 64 | |
| 65 | /* Return next node in the tree, or NULL if none */ |
| 66 | static inline struct eb64_node *eb64_next(struct eb64_node *eb64) |
| 67 | { |
| 68 | return eb64_entry(eb_next(&eb64->node), struct eb64_node, node); |
| 69 | } |
| 70 | |
| 71 | /* Return previous node in the tree, or NULL if none */ |
| 72 | static inline struct eb64_node *eb64_prev(struct eb64_node *eb64) |
| 73 | { |
| 74 | return eb64_entry(eb_prev(&eb64->node), struct eb64_node, node); |
| 75 | } |
| 76 | |
| 77 | /* Return next node in the tree, skipping duplicates, or NULL if none */ |
| 78 | static inline struct eb64_node *eb64_next_unique(struct eb64_node *eb64) |
| 79 | { |
| 80 | return eb64_entry(eb_next_unique(&eb64->node), struct eb64_node, node); |
| 81 | } |
| 82 | |
| 83 | /* Return previous node in the tree, skipping duplicates, or NULL if none */ |
| 84 | static inline struct eb64_node *eb64_prev_unique(struct eb64_node *eb64) |
| 85 | { |
| 86 | return eb64_entry(eb_prev_unique(&eb64->node), struct eb64_node, node); |
| 87 | } |
| 88 | |
| 89 | /* Delete node from the tree if it was linked in. Mark the node unused. Note |
| 90 | * that this function relies on a non-inlined generic function: eb_delete. |
| 91 | */ |
| 92 | static inline void eb64_delete(struct eb64_node *eb64) |
| 93 | { |
| 94 | eb_delete(&eb64->node); |
| 95 | } |
| 96 | |
| 97 | /* |
| 98 | * The following functions are not inlined by default. They are declared |
| 99 | * in eb64tree.c, which simply relies on their inline version. |
| 100 | */ |
| 101 | REGPRM2 struct eb64_node *eb64_lookup(struct eb_root *root, u64 x); |
| 102 | REGPRM2 struct eb64_node *eb64i_lookup(struct eb_root *root, s64 x); |
| 103 | REGPRM2 struct eb64_node *eb64_lookup_le(struct eb_root *root, u64 x); |
| 104 | REGPRM2 struct eb64_node *eb64_lookup_ge(struct eb_root *root, u64 x); |
| 105 | REGPRM2 struct eb64_node *eb64_insert(struct eb_root *root, struct eb64_node *new); |
| 106 | REGPRM2 struct eb64_node *eb64i_insert(struct eb_root *root, struct eb64_node *new); |
| 107 | |
| 108 | /* |
| 109 | * The following functions are less likely to be used directly, because their |
| 110 | * code is larger. The non-inlined version is preferred. |
| 111 | */ |
| 112 | |
| 113 | /* Delete node from the tree if it was linked in. Mark the node unused. */ |
| 114 | static forceinline void __eb64_delete(struct eb64_node *eb64) |
| 115 | { |
| 116 | __eb_delete(&eb64->node); |
| 117 | } |
| 118 | |
| 119 | /* |
| 120 | * Find the first occurence of a key in the tree <root>. If none can be |
| 121 | * found, return NULL. |
| 122 | */ |
| 123 | static forceinline struct eb64_node *__eb64_lookup(struct eb_root *root, u64 x) |
| 124 | { |
| 125 | struct eb64_node *node; |
| 126 | eb_troot_t *troot; |
| 127 | u64 y; |
| 128 | |
| 129 | troot = root->b[EB_LEFT]; |
| 130 | if (unlikely(troot == NULL)) |
| 131 | return NULL; |
| 132 | |
| 133 | while (1) { |
| 134 | if ((eb_gettag(troot) == EB_LEAF)) { |
| 135 | node = container_of(eb_untag(troot, EB_LEAF), |
| 136 | struct eb64_node, node.branches); |
| 137 | if (node->key == x) |
| 138 | return node; |
| 139 | else |
| 140 | return NULL; |
| 141 | } |
| 142 | node = container_of(eb_untag(troot, EB_NODE), |
| 143 | struct eb64_node, node.branches); |
| 144 | |
| 145 | y = node->key ^ x; |
| 146 | if (!y) { |
| 147 | /* Either we found the node which holds the key, or |
| 148 | * we have a dup tree. In the later case, we have to |
| 149 | * walk it down left to get the first entry. |
| 150 | */ |
| 151 | if (node->node.bit < 0) { |
| 152 | troot = node->node.branches.b[EB_LEFT]; |
| 153 | while (eb_gettag(troot) != EB_LEAF) |
| 154 | troot = (eb_untag(troot, EB_NODE))->b[EB_LEFT]; |
| 155 | node = container_of(eb_untag(troot, EB_LEAF), |
| 156 | struct eb64_node, node.branches); |
| 157 | } |
| 158 | return node; |
| 159 | } |
| 160 | |
| 161 | if ((y >> node->node.bit) >= EB_NODE_BRANCHES) |
| 162 | return NULL; /* no more common bits */ |
| 163 | |
| 164 | troot = node->node.branches.b[(x >> node->node.bit) & EB_NODE_BRANCH_MASK]; |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | /* |
| 169 | * Find the first occurence of a signed key in the tree <root>. If none can |
| 170 | * be found, return NULL. |
| 171 | */ |
| 172 | static forceinline struct eb64_node *__eb64i_lookup(struct eb_root *root, s64 x) |
| 173 | { |
| 174 | struct eb64_node *node; |
| 175 | eb_troot_t *troot; |
| 176 | u64 key = x ^ (1ULL << 63); |
| 177 | u64 y; |
| 178 | |
| 179 | troot = root->b[EB_LEFT]; |
| 180 | if (unlikely(troot == NULL)) |
| 181 | return NULL; |
| 182 | |
| 183 | while (1) { |
| 184 | if ((eb_gettag(troot) == EB_LEAF)) { |
| 185 | node = container_of(eb_untag(troot, EB_LEAF), |
| 186 | struct eb64_node, node.branches); |
Willy Tarreau | 3a93244 | 2010-05-09 19:29:23 +0200 | [diff] [blame] | 187 | if (node->key == (u64)x) |
Willy Tarreau | c218602 | 2009-10-26 19:48:54 +0100 | [diff] [blame] | 188 | return node; |
| 189 | else |
| 190 | return NULL; |
| 191 | } |
| 192 | node = container_of(eb_untag(troot, EB_NODE), |
| 193 | struct eb64_node, node.branches); |
| 194 | |
| 195 | y = node->key ^ x; |
| 196 | if (!y) { |
| 197 | /* Either we found the node which holds the key, or |
| 198 | * we have a dup tree. In the later case, we have to |
| 199 | * walk it down left to get the first entry. |
| 200 | */ |
| 201 | if (node->node.bit < 0) { |
| 202 | troot = node->node.branches.b[EB_LEFT]; |
| 203 | while (eb_gettag(troot) != EB_LEAF) |
| 204 | troot = (eb_untag(troot, EB_NODE))->b[EB_LEFT]; |
| 205 | node = container_of(eb_untag(troot, EB_LEAF), |
| 206 | struct eb64_node, node.branches); |
| 207 | } |
| 208 | return node; |
| 209 | } |
| 210 | |
| 211 | if ((y >> node->node.bit) >= EB_NODE_BRANCHES) |
| 212 | return NULL; /* no more common bits */ |
| 213 | |
| 214 | troot = node->node.branches.b[(key >> node->node.bit) & EB_NODE_BRANCH_MASK]; |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | /* Insert eb64_node <new> into subtree starting at node root <root>. |
| 219 | * Only new->key needs be set with the key. The eb64_node is returned. |
| 220 | * If root->b[EB_RGHT]==1, the tree may only contain unique keys. |
| 221 | */ |
| 222 | static forceinline struct eb64_node * |
| 223 | __eb64_insert(struct eb_root *root, struct eb64_node *new) { |
| 224 | struct eb64_node *old; |
| 225 | unsigned int side; |
| 226 | eb_troot_t *troot; |
| 227 | u64 newkey; /* caching the key saves approximately one cycle */ |
| 228 | eb_troot_t *root_right = root; |
Willy Tarreau | 3a93244 | 2010-05-09 19:29:23 +0200 | [diff] [blame] | 229 | int old_node_bit; |
Willy Tarreau | c218602 | 2009-10-26 19:48:54 +0100 | [diff] [blame] | 230 | |
| 231 | side = EB_LEFT; |
| 232 | troot = root->b[EB_LEFT]; |
| 233 | root_right = root->b[EB_RGHT]; |
| 234 | if (unlikely(troot == NULL)) { |
| 235 | /* Tree is empty, insert the leaf part below the left branch */ |
| 236 | root->b[EB_LEFT] = eb_dotag(&new->node.branches, EB_LEAF); |
| 237 | new->node.leaf_p = eb_dotag(root, EB_LEFT); |
| 238 | new->node.node_p = NULL; /* node part unused */ |
| 239 | return new; |
| 240 | } |
| 241 | |
| 242 | /* The tree descent is fairly easy : |
| 243 | * - first, check if we have reached a leaf node |
| 244 | * - second, check if we have gone too far |
| 245 | * - third, reiterate |
| 246 | * Everywhere, we use <new> for the node node we are inserting, <root> |
| 247 | * for the node we attach it to, and <old> for the node we are |
| 248 | * displacing below <new>. <troot> will always point to the future node |
| 249 | * (tagged with its type). <side> carries the side the node <new> is |
| 250 | * attached to below its parent, which is also where previous node |
| 251 | * was attached. <newkey> carries the key being inserted. |
| 252 | */ |
| 253 | newkey = new->key; |
| 254 | |
| 255 | while (1) { |
| 256 | if (unlikely(eb_gettag(troot) == EB_LEAF)) { |
| 257 | eb_troot_t *new_left, *new_rght; |
| 258 | eb_troot_t *new_leaf, *old_leaf; |
| 259 | |
| 260 | old = container_of(eb_untag(troot, EB_LEAF), |
| 261 | struct eb64_node, node.branches); |
| 262 | |
| 263 | new_left = eb_dotag(&new->node.branches, EB_LEFT); |
| 264 | new_rght = eb_dotag(&new->node.branches, EB_RGHT); |
| 265 | new_leaf = eb_dotag(&new->node.branches, EB_LEAF); |
| 266 | old_leaf = eb_dotag(&old->node.branches, EB_LEAF); |
| 267 | |
| 268 | new->node.node_p = old->node.leaf_p; |
| 269 | |
| 270 | /* Right here, we have 3 possibilities : |
| 271 | - the tree does not contain the key, and we have |
| 272 | new->key < old->key. We insert new above old, on |
| 273 | the left ; |
| 274 | |
| 275 | - the tree does not contain the key, and we have |
| 276 | new->key > old->key. We insert new above old, on |
| 277 | the right ; |
| 278 | |
| 279 | - the tree does contain the key, which implies it |
| 280 | is alone. We add the new key next to it as a |
| 281 | first duplicate. |
| 282 | |
| 283 | The last two cases can easily be partially merged. |
| 284 | */ |
| 285 | |
| 286 | if (new->key < old->key) { |
| 287 | new->node.leaf_p = new_left; |
| 288 | old->node.leaf_p = new_rght; |
| 289 | new->node.branches.b[EB_LEFT] = new_leaf; |
| 290 | new->node.branches.b[EB_RGHT] = old_leaf; |
| 291 | } else { |
| 292 | /* we may refuse to duplicate this key if the tree is |
| 293 | * tagged as containing only unique keys. |
| 294 | */ |
| 295 | if ((new->key == old->key) && eb_gettag(root_right)) |
| 296 | return old; |
| 297 | |
| 298 | /* new->key >= old->key, new goes the right */ |
| 299 | old->node.leaf_p = new_left; |
| 300 | new->node.leaf_p = new_rght; |
| 301 | new->node.branches.b[EB_LEFT] = old_leaf; |
| 302 | new->node.branches.b[EB_RGHT] = new_leaf; |
| 303 | |
| 304 | if (new->key == old->key) { |
| 305 | new->node.bit = -1; |
| 306 | root->b[side] = eb_dotag(&new->node.branches, EB_NODE); |
| 307 | return new; |
| 308 | } |
| 309 | } |
| 310 | break; |
| 311 | } |
| 312 | |
| 313 | /* OK we're walking down this link */ |
| 314 | old = container_of(eb_untag(troot, EB_NODE), |
| 315 | struct eb64_node, node.branches); |
Willy Tarreau | 3a93244 | 2010-05-09 19:29:23 +0200 | [diff] [blame] | 316 | old_node_bit = old->node.bit; |
Willy Tarreau | c218602 | 2009-10-26 19:48:54 +0100 | [diff] [blame] | 317 | |
| 318 | /* Stop going down when we don't have common bits anymore. We |
| 319 | * also stop in front of a duplicates tree because it means we |
| 320 | * have to insert above. |
| 321 | */ |
| 322 | |
Willy Tarreau | 3a93244 | 2010-05-09 19:29:23 +0200 | [diff] [blame] | 323 | if ((old_node_bit < 0) || /* we're above a duplicate tree, stop here */ |
| 324 | (((new->key ^ old->key) >> old_node_bit) >= EB_NODE_BRANCHES)) { |
Willy Tarreau | c218602 | 2009-10-26 19:48:54 +0100 | [diff] [blame] | 325 | /* The tree did not contain the key, so we insert <new> before the node |
| 326 | * <old>, and set ->bit to designate the lowest bit position in <new> |
| 327 | * which applies to ->branches.b[]. |
| 328 | */ |
| 329 | eb_troot_t *new_left, *new_rght; |
| 330 | eb_troot_t *new_leaf, *old_node; |
| 331 | |
| 332 | new_left = eb_dotag(&new->node.branches, EB_LEFT); |
| 333 | new_rght = eb_dotag(&new->node.branches, EB_RGHT); |
| 334 | new_leaf = eb_dotag(&new->node.branches, EB_LEAF); |
| 335 | old_node = eb_dotag(&old->node.branches, EB_NODE); |
| 336 | |
| 337 | new->node.node_p = old->node.node_p; |
| 338 | |
| 339 | if (new->key < old->key) { |
| 340 | new->node.leaf_p = new_left; |
| 341 | old->node.node_p = new_rght; |
| 342 | new->node.branches.b[EB_LEFT] = new_leaf; |
| 343 | new->node.branches.b[EB_RGHT] = old_node; |
| 344 | } |
| 345 | else if (new->key > old->key) { |
| 346 | old->node.node_p = new_left; |
| 347 | new->node.leaf_p = new_rght; |
| 348 | new->node.branches.b[EB_LEFT] = old_node; |
| 349 | new->node.branches.b[EB_RGHT] = new_leaf; |
| 350 | } |
| 351 | else { |
| 352 | struct eb_node *ret; |
| 353 | ret = eb_insert_dup(&old->node, &new->node); |
| 354 | return container_of(ret, struct eb64_node, node); |
| 355 | } |
| 356 | break; |
| 357 | } |
| 358 | |
| 359 | /* walk down */ |
| 360 | root = &old->node.branches; |
| 361 | #if BITS_PER_LONG >= 64 |
Willy Tarreau | 3a93244 | 2010-05-09 19:29:23 +0200 | [diff] [blame] | 362 | side = (newkey >> old_node_bit) & EB_NODE_BRANCH_MASK; |
Willy Tarreau | c218602 | 2009-10-26 19:48:54 +0100 | [diff] [blame] | 363 | #else |
| 364 | side = newkey; |
Willy Tarreau | 3a93244 | 2010-05-09 19:29:23 +0200 | [diff] [blame] | 365 | side >>= old_node_bit; |
| 366 | if (old_node_bit >= 32) { |
Willy Tarreau | c218602 | 2009-10-26 19:48:54 +0100 | [diff] [blame] | 367 | side = newkey >> 32; |
Willy Tarreau | 3a93244 | 2010-05-09 19:29:23 +0200 | [diff] [blame] | 368 | side >>= old_node_bit & 0x1F; |
Willy Tarreau | c218602 | 2009-10-26 19:48:54 +0100 | [diff] [blame] | 369 | } |
| 370 | side &= EB_NODE_BRANCH_MASK; |
| 371 | #endif |
| 372 | troot = root->b[side]; |
| 373 | } |
| 374 | |
| 375 | /* Ok, now we are inserting <new> between <root> and <old>. <old>'s |
| 376 | * parent is already set to <new>, and the <root>'s branch is still in |
| 377 | * <side>. Update the root's leaf till we have it. Note that we can also |
| 378 | * find the side by checking the side of new->node.node_p. |
| 379 | */ |
| 380 | |
| 381 | /* We need the common higher bits between new->key and old->key. |
| 382 | * What differences are there between new->key and the node here ? |
| 383 | * NOTE that bit(new) is always < bit(root) because highest |
| 384 | * bit of new->key and old->key are identical here (otherwise they |
| 385 | * would sit on different branches). |
| 386 | */ |
| 387 | // note that if EB_NODE_BITS > 1, we should check that it's still >= 0 |
| 388 | new->node.bit = fls64(new->key ^ old->key) - EB_NODE_BITS; |
| 389 | root->b[side] = eb_dotag(&new->node.branches, EB_NODE); |
| 390 | |
| 391 | return new; |
| 392 | } |
| 393 | |
| 394 | /* Insert eb64_node <new> into subtree starting at node root <root>, using |
| 395 | * signed keys. Only new->key needs be set with the key. The eb64_node |
| 396 | * is returned. If root->b[EB_RGHT]==1, the tree may only contain unique keys. |
| 397 | */ |
| 398 | static forceinline struct eb64_node * |
| 399 | __eb64i_insert(struct eb_root *root, struct eb64_node *new) { |
| 400 | struct eb64_node *old; |
| 401 | unsigned int side; |
| 402 | eb_troot_t *troot; |
| 403 | u64 newkey; /* caching the key saves approximately one cycle */ |
| 404 | eb_troot_t *root_right = root; |
Willy Tarreau | 3a93244 | 2010-05-09 19:29:23 +0200 | [diff] [blame] | 405 | int old_node_bit; |
Willy Tarreau | c218602 | 2009-10-26 19:48:54 +0100 | [diff] [blame] | 406 | |
| 407 | side = EB_LEFT; |
| 408 | troot = root->b[EB_LEFT]; |
| 409 | root_right = root->b[EB_RGHT]; |
| 410 | if (unlikely(troot == NULL)) { |
| 411 | /* Tree is empty, insert the leaf part below the left branch */ |
| 412 | root->b[EB_LEFT] = eb_dotag(&new->node.branches, EB_LEAF); |
| 413 | new->node.leaf_p = eb_dotag(root, EB_LEFT); |
| 414 | new->node.node_p = NULL; /* node part unused */ |
| 415 | return new; |
| 416 | } |
| 417 | |
| 418 | /* The tree descent is fairly easy : |
| 419 | * - first, check if we have reached a leaf node |
| 420 | * - second, check if we have gone too far |
| 421 | * - third, reiterate |
| 422 | * Everywhere, we use <new> for the node node we are inserting, <root> |
| 423 | * for the node we attach it to, and <old> for the node we are |
| 424 | * displacing below <new>. <troot> will always point to the future node |
| 425 | * (tagged with its type). <side> carries the side the node <new> is |
| 426 | * attached to below its parent, which is also where previous node |
| 427 | * was attached. <newkey> carries a high bit shift of the key being |
| 428 | * inserted in order to have negative keys stored before positive |
| 429 | * ones. |
| 430 | */ |
| 431 | newkey = new->key ^ (1ULL << 63); |
| 432 | |
| 433 | while (1) { |
| 434 | if (unlikely(eb_gettag(troot) == EB_LEAF)) { |
| 435 | eb_troot_t *new_left, *new_rght; |
| 436 | eb_troot_t *new_leaf, *old_leaf; |
| 437 | |
| 438 | old = container_of(eb_untag(troot, EB_LEAF), |
| 439 | struct eb64_node, node.branches); |
| 440 | |
| 441 | new_left = eb_dotag(&new->node.branches, EB_LEFT); |
| 442 | new_rght = eb_dotag(&new->node.branches, EB_RGHT); |
| 443 | new_leaf = eb_dotag(&new->node.branches, EB_LEAF); |
| 444 | old_leaf = eb_dotag(&old->node.branches, EB_LEAF); |
| 445 | |
| 446 | new->node.node_p = old->node.leaf_p; |
| 447 | |
| 448 | /* Right here, we have 3 possibilities : |
| 449 | - the tree does not contain the key, and we have |
| 450 | new->key < old->key. We insert new above old, on |
| 451 | the left ; |
| 452 | |
| 453 | - the tree does not contain the key, and we have |
| 454 | new->key > old->key. We insert new above old, on |
| 455 | the right ; |
| 456 | |
| 457 | - the tree does contain the key, which implies it |
| 458 | is alone. We add the new key next to it as a |
| 459 | first duplicate. |
| 460 | |
| 461 | The last two cases can easily be partially merged. |
| 462 | */ |
| 463 | |
| 464 | if ((s64)new->key < (s64)old->key) { |
| 465 | new->node.leaf_p = new_left; |
| 466 | old->node.leaf_p = new_rght; |
| 467 | new->node.branches.b[EB_LEFT] = new_leaf; |
| 468 | new->node.branches.b[EB_RGHT] = old_leaf; |
| 469 | } else { |
| 470 | /* we may refuse to duplicate this key if the tree is |
| 471 | * tagged as containing only unique keys. |
| 472 | */ |
| 473 | if ((new->key == old->key) && eb_gettag(root_right)) |
| 474 | return old; |
| 475 | |
| 476 | /* new->key >= old->key, new goes the right */ |
| 477 | old->node.leaf_p = new_left; |
| 478 | new->node.leaf_p = new_rght; |
| 479 | new->node.branches.b[EB_LEFT] = old_leaf; |
| 480 | new->node.branches.b[EB_RGHT] = new_leaf; |
| 481 | |
| 482 | if (new->key == old->key) { |
| 483 | new->node.bit = -1; |
| 484 | root->b[side] = eb_dotag(&new->node.branches, EB_NODE); |
| 485 | return new; |
| 486 | } |
| 487 | } |
| 488 | break; |
| 489 | } |
| 490 | |
| 491 | /* OK we're walking down this link */ |
| 492 | old = container_of(eb_untag(troot, EB_NODE), |
| 493 | struct eb64_node, node.branches); |
Willy Tarreau | 3a93244 | 2010-05-09 19:29:23 +0200 | [diff] [blame] | 494 | old_node_bit = old->node.bit; |
Willy Tarreau | c218602 | 2009-10-26 19:48:54 +0100 | [diff] [blame] | 495 | |
| 496 | /* Stop going down when we don't have common bits anymore. We |
| 497 | * also stop in front of a duplicates tree because it means we |
| 498 | * have to insert above. |
| 499 | */ |
| 500 | |
Willy Tarreau | 3a93244 | 2010-05-09 19:29:23 +0200 | [diff] [blame] | 501 | if ((old_node_bit < 0) || /* we're above a duplicate tree, stop here */ |
| 502 | (((new->key ^ old->key) >> old_node_bit) >= EB_NODE_BRANCHES)) { |
Willy Tarreau | c218602 | 2009-10-26 19:48:54 +0100 | [diff] [blame] | 503 | /* The tree did not contain the key, so we insert <new> before the node |
| 504 | * <old>, and set ->bit to designate the lowest bit position in <new> |
| 505 | * which applies to ->branches.b[]. |
| 506 | */ |
| 507 | eb_troot_t *new_left, *new_rght; |
| 508 | eb_troot_t *new_leaf, *old_node; |
| 509 | |
| 510 | new_left = eb_dotag(&new->node.branches, EB_LEFT); |
| 511 | new_rght = eb_dotag(&new->node.branches, EB_RGHT); |
| 512 | new_leaf = eb_dotag(&new->node.branches, EB_LEAF); |
| 513 | old_node = eb_dotag(&old->node.branches, EB_NODE); |
| 514 | |
| 515 | new->node.node_p = old->node.node_p; |
| 516 | |
| 517 | if ((s64)new->key < (s64)old->key) { |
| 518 | new->node.leaf_p = new_left; |
| 519 | old->node.node_p = new_rght; |
| 520 | new->node.branches.b[EB_LEFT] = new_leaf; |
| 521 | new->node.branches.b[EB_RGHT] = old_node; |
| 522 | } |
| 523 | else if ((s64)new->key > (s64)old->key) { |
| 524 | old->node.node_p = new_left; |
| 525 | new->node.leaf_p = new_rght; |
| 526 | new->node.branches.b[EB_LEFT] = old_node; |
| 527 | new->node.branches.b[EB_RGHT] = new_leaf; |
| 528 | } |
| 529 | else { |
| 530 | struct eb_node *ret; |
| 531 | ret = eb_insert_dup(&old->node, &new->node); |
| 532 | return container_of(ret, struct eb64_node, node); |
| 533 | } |
| 534 | break; |
| 535 | } |
| 536 | |
| 537 | /* walk down */ |
| 538 | root = &old->node.branches; |
| 539 | #if BITS_PER_LONG >= 64 |
Willy Tarreau | 3a93244 | 2010-05-09 19:29:23 +0200 | [diff] [blame] | 540 | side = (newkey >> old_node_bit) & EB_NODE_BRANCH_MASK; |
Willy Tarreau | c218602 | 2009-10-26 19:48:54 +0100 | [diff] [blame] | 541 | #else |
| 542 | side = newkey; |
Willy Tarreau | 3a93244 | 2010-05-09 19:29:23 +0200 | [diff] [blame] | 543 | side >>= old_node_bit; |
| 544 | if (old_node_bit >= 32) { |
Willy Tarreau | c218602 | 2009-10-26 19:48:54 +0100 | [diff] [blame] | 545 | side = newkey >> 32; |
Willy Tarreau | 3a93244 | 2010-05-09 19:29:23 +0200 | [diff] [blame] | 546 | side >>= old_node_bit & 0x1F; |
Willy Tarreau | c218602 | 2009-10-26 19:48:54 +0100 | [diff] [blame] | 547 | } |
| 548 | side &= EB_NODE_BRANCH_MASK; |
| 549 | #endif |
| 550 | troot = root->b[side]; |
| 551 | } |
| 552 | |
| 553 | /* Ok, now we are inserting <new> between <root> and <old>. <old>'s |
| 554 | * parent is already set to <new>, and the <root>'s branch is still in |
| 555 | * <side>. Update the root's leaf till we have it. Note that we can also |
| 556 | * find the side by checking the side of new->node.node_p. |
| 557 | */ |
| 558 | |
| 559 | /* We need the common higher bits between new->key and old->key. |
| 560 | * What differences are there between new->key and the node here ? |
| 561 | * NOTE that bit(new) is always < bit(root) because highest |
| 562 | * bit of new->key and old->key are identical here (otherwise they |
| 563 | * would sit on different branches). |
| 564 | */ |
| 565 | // note that if EB_NODE_BITS > 1, we should check that it's still >= 0 |
| 566 | new->node.bit = fls64(new->key ^ old->key) - EB_NODE_BITS; |
| 567 | root->b[side] = eb_dotag(&new->node.branches, EB_NODE); |
| 568 | |
| 569 | return new; |
| 570 | } |
| 571 | |
| 572 | #endif /* _EB64_TREE_H */ |