Willy Tarreau | e6d2e4d | 2007-11-15 23:56:17 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Elastic Binary Trees - macros and structures for operations on 64bit nodes. |
Willy Tarreau | 1fb6c87 | 2008-05-16 19:48:20 +0200 | [diff] [blame] | 3 | * Version 4.0 |
| 4 | * (C) 2002-2008 - Willy Tarreau <w@1wt.eu> |
Willy Tarreau | e6d2e4d | 2007-11-15 23:56:17 +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 | |
Willy Tarreau | f56fd8a | 2007-11-19 18:43:04 +0100 | [diff] [blame] | 21 | #ifndef _COMMON_EB64TREE_H |
| 22 | #define _COMMON_EB64TREE_H |
| 23 | |
Willy Tarreau | e6d2e4d | 2007-11-15 23:56:17 +0100 | [diff] [blame] | 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_insert(struct eb_root *root, struct eb64_node *new); |
| 104 | REGPRM2 struct eb64_node *eb64i_insert(struct eb_root *root, struct eb64_node *new); |
| 105 | |
| 106 | /* |
| 107 | * The following functions are less likely to be used directly, because their |
| 108 | * code is larger. The non-inlined version is preferred. |
| 109 | */ |
| 110 | |
| 111 | /* Delete node from the tree if it was linked in. Mark the node unused. */ |
Willy Tarreau | 75cf17e | 2008-08-29 15:48:49 +0200 | [diff] [blame] | 112 | static forceinline void __eb64_delete(struct eb64_node *eb64) |
Willy Tarreau | e6d2e4d | 2007-11-15 23:56:17 +0100 | [diff] [blame] | 113 | { |
| 114 | __eb_delete(&eb64->node); |
| 115 | } |
| 116 | |
| 117 | /* |
| 118 | * Find the first occurence of a key in the tree <root>. If none can be |
| 119 | * found, return NULL. |
| 120 | */ |
Willy Tarreau | 75cf17e | 2008-08-29 15:48:49 +0200 | [diff] [blame] | 121 | static forceinline struct eb64_node *__eb64_lookup(struct eb_root *root, u64 x) |
Willy Tarreau | e6d2e4d | 2007-11-15 23:56:17 +0100 | [diff] [blame] | 122 | { |
| 123 | struct eb64_node *node; |
| 124 | eb_troot_t *troot; |
| 125 | |
| 126 | troot = root->b[EB_LEFT]; |
| 127 | if (unlikely(troot == NULL)) |
| 128 | return NULL; |
| 129 | |
| 130 | while (1) { |
| 131 | if ((eb_gettag(troot) == EB_LEAF)) { |
| 132 | node = container_of(eb_untag(troot, EB_LEAF), |
| 133 | struct eb64_node, node.branches); |
| 134 | if (node->key == x) |
| 135 | return node; |
| 136 | else |
| 137 | return NULL; |
| 138 | } |
| 139 | node = container_of(eb_untag(troot, EB_NODE), |
| 140 | struct eb64_node, node.branches); |
| 141 | |
| 142 | if (x == node->key) { |
| 143 | /* Either we found the node which holds the key, or |
| 144 | * we have a dup tree. In the later case, we have to |
| 145 | * walk it down left to get the first entry. |
| 146 | */ |
| 147 | if (node->node.bit < 0) { |
| 148 | troot = node->node.branches.b[EB_LEFT]; |
| 149 | while (eb_gettag(troot) != EB_LEAF) |
| 150 | troot = (eb_untag(troot, EB_NODE))->b[EB_LEFT]; |
| 151 | node = container_of(eb_untag(troot, EB_LEAF), |
| 152 | struct eb64_node, node.branches); |
| 153 | } |
| 154 | return node; |
| 155 | } |
| 156 | |
| 157 | troot = node->node.branches.b[(x >> node->node.bit) & EB_NODE_BRANCH_MASK]; |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | /* |
| 162 | * Find the first occurence of a signed key in the tree <root>. If none can |
| 163 | * be found, return NULL. |
| 164 | */ |
Willy Tarreau | 75cf17e | 2008-08-29 15:48:49 +0200 | [diff] [blame] | 165 | static forceinline struct eb64_node *__eb64i_lookup(struct eb_root *root, s64 x) |
Willy Tarreau | e6d2e4d | 2007-11-15 23:56:17 +0100 | [diff] [blame] | 166 | { |
| 167 | struct eb64_node *node; |
| 168 | eb_troot_t *troot; |
| 169 | u64 key = x ^ (1ULL << 63); |
| 170 | |
| 171 | troot = root->b[EB_LEFT]; |
| 172 | if (unlikely(troot == NULL)) |
| 173 | return NULL; |
| 174 | |
| 175 | while (1) { |
| 176 | if ((eb_gettag(troot) == EB_LEAF)) { |
| 177 | node = container_of(eb_untag(troot, EB_LEAF), |
| 178 | struct eb64_node, node.branches); |
| 179 | if (node->key == x) |
| 180 | return node; |
| 181 | else |
| 182 | return NULL; |
| 183 | } |
| 184 | node = container_of(eb_untag(troot, EB_NODE), |
| 185 | struct eb64_node, node.branches); |
| 186 | |
| 187 | if (x == node->key) { |
| 188 | /* Either we found the node which holds the key, or |
| 189 | * we have a dup tree. In the later case, we have to |
| 190 | * walk it down left to get the first entry. |
| 191 | */ |
| 192 | if (node->node.bit < 0) { |
| 193 | troot = node->node.branches.b[EB_LEFT]; |
| 194 | while (eb_gettag(troot) != EB_LEAF) |
| 195 | troot = (eb_untag(troot, EB_NODE))->b[EB_LEFT]; |
| 196 | node = container_of(eb_untag(troot, EB_LEAF), |
| 197 | struct eb64_node, node.branches); |
| 198 | } |
| 199 | return node; |
| 200 | } |
| 201 | |
| 202 | troot = node->node.branches.b[(key >> node->node.bit) & EB_NODE_BRANCH_MASK]; |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | /* Insert eb64_node <new> into subtree starting at node root <root>. |
| 207 | * Only new->key needs be set with the key. The eb64_node is returned. |
Willy Tarreau | 1fb6c87 | 2008-05-16 19:48:20 +0200 | [diff] [blame] | 208 | * If root->b[EB_RGHT]==1, the tree may only contain unique keys. |
Willy Tarreau | e6d2e4d | 2007-11-15 23:56:17 +0100 | [diff] [blame] | 209 | */ |
Willy Tarreau | 75cf17e | 2008-08-29 15:48:49 +0200 | [diff] [blame] | 210 | static forceinline struct eb64_node * |
Willy Tarreau | e6d2e4d | 2007-11-15 23:56:17 +0100 | [diff] [blame] | 211 | __eb64_insert(struct eb_root *root, struct eb64_node *new) { |
| 212 | struct eb64_node *old; |
| 213 | unsigned int side; |
| 214 | eb_troot_t *troot; |
| 215 | u64 newkey; /* caching the key saves approximately one cycle */ |
Willy Tarreau | 1fb6c87 | 2008-05-16 19:48:20 +0200 | [diff] [blame] | 216 | eb_troot_t *root_right = root; |
Willy Tarreau | e6d2e4d | 2007-11-15 23:56:17 +0100 | [diff] [blame] | 217 | |
| 218 | side = EB_LEFT; |
| 219 | troot = root->b[EB_LEFT]; |
Willy Tarreau | 1fb6c87 | 2008-05-16 19:48:20 +0200 | [diff] [blame] | 220 | root_right = root->b[EB_RGHT]; |
Willy Tarreau | e6d2e4d | 2007-11-15 23:56:17 +0100 | [diff] [blame] | 221 | if (unlikely(troot == NULL)) { |
| 222 | /* Tree is empty, insert the leaf part below the left branch */ |
| 223 | root->b[EB_LEFT] = eb_dotag(&new->node.branches, EB_LEAF); |
| 224 | new->node.leaf_p = eb_dotag(root, EB_LEFT); |
| 225 | new->node.node_p = NULL; /* node part unused */ |
| 226 | return new; |
| 227 | } |
| 228 | |
| 229 | /* The tree descent is fairly easy : |
| 230 | * - first, check if we have reached a leaf node |
| 231 | * - second, check if we have gone too far |
| 232 | * - third, reiterate |
| 233 | * Everywhere, we use <new> for the node node we are inserting, <root> |
| 234 | * for the node we attach it to, and <old> for the node we are |
| 235 | * displacing below <new>. <troot> will always point to the future node |
| 236 | * (tagged with its type). <side> carries the side the node <new> is |
| 237 | * attached to below its parent, which is also where previous node |
| 238 | * was attached. <newkey> carries the key being inserted. |
| 239 | */ |
| 240 | newkey = new->key; |
| 241 | |
| 242 | while (1) { |
| 243 | if (unlikely(eb_gettag(troot) == EB_LEAF)) { |
| 244 | eb_troot_t *new_left, *new_rght; |
| 245 | eb_troot_t *new_leaf, *old_leaf; |
| 246 | |
| 247 | old = container_of(eb_untag(troot, EB_LEAF), |
| 248 | struct eb64_node, node.branches); |
| 249 | |
| 250 | new_left = eb_dotag(&new->node.branches, EB_LEFT); |
| 251 | new_rght = eb_dotag(&new->node.branches, EB_RGHT); |
| 252 | new_leaf = eb_dotag(&new->node.branches, EB_LEAF); |
| 253 | old_leaf = eb_dotag(&old->node.branches, EB_LEAF); |
| 254 | |
| 255 | new->node.node_p = old->node.leaf_p; |
| 256 | |
| 257 | /* Right here, we have 3 possibilities : |
| 258 | - the tree does not contain the key, and we have |
| 259 | new->key < old->key. We insert new above old, on |
| 260 | the left ; |
| 261 | |
| 262 | - the tree does not contain the key, and we have |
| 263 | new->key > old->key. We insert new above old, on |
| 264 | the right ; |
| 265 | |
| 266 | - the tree does contain the key, which implies it |
| 267 | is alone. We add the new key next to it as a |
| 268 | first duplicate. |
| 269 | |
| 270 | The last two cases can easily be partially merged. |
| 271 | */ |
| 272 | |
| 273 | if (new->key < old->key) { |
| 274 | new->node.leaf_p = new_left; |
| 275 | old->node.leaf_p = new_rght; |
| 276 | new->node.branches.b[EB_LEFT] = new_leaf; |
| 277 | new->node.branches.b[EB_RGHT] = old_leaf; |
| 278 | } else { |
Willy Tarreau | 1fb6c87 | 2008-05-16 19:48:20 +0200 | [diff] [blame] | 279 | /* we may refuse to duplicate this key if the tree is |
| 280 | * tagged as containing only unique keys. |
| 281 | */ |
| 282 | if ((new->key == old->key) && eb_gettag(root_right)) |
| 283 | return old; |
| 284 | |
Willy Tarreau | e6d2e4d | 2007-11-15 23:56:17 +0100 | [diff] [blame] | 285 | /* new->key >= old->key, new goes the right */ |
| 286 | old->node.leaf_p = new_left; |
| 287 | new->node.leaf_p = new_rght; |
| 288 | new->node.branches.b[EB_LEFT] = old_leaf; |
| 289 | new->node.branches.b[EB_RGHT] = new_leaf; |
| 290 | |
| 291 | if (new->key == old->key) { |
| 292 | new->node.bit = -1; |
| 293 | root->b[side] = eb_dotag(&new->node.branches, EB_NODE); |
| 294 | return new; |
| 295 | } |
| 296 | } |
| 297 | break; |
| 298 | } |
| 299 | |
| 300 | /* OK we're walking down this link */ |
| 301 | old = container_of(eb_untag(troot, EB_NODE), |
| 302 | struct eb64_node, node.branches); |
| 303 | |
| 304 | /* Stop going down when we don't have common bits anymore. We |
| 305 | * also stop in front of a duplicates tree because it means we |
| 306 | * have to insert above. |
| 307 | */ |
| 308 | |
| 309 | if ((old->node.bit < 0) || /* we're above a duplicate tree, stop here */ |
| 310 | (((new->key ^ old->key) >> old->node.bit) >= EB_NODE_BRANCHES)) { |
| 311 | /* The tree did not contain the key, so we insert <new> before the node |
| 312 | * <old>, and set ->bit to designate the lowest bit position in <new> |
| 313 | * which applies to ->branches.b[]. |
| 314 | */ |
| 315 | eb_troot_t *new_left, *new_rght; |
| 316 | eb_troot_t *new_leaf, *old_node; |
| 317 | |
| 318 | new_left = eb_dotag(&new->node.branches, EB_LEFT); |
| 319 | new_rght = eb_dotag(&new->node.branches, EB_RGHT); |
| 320 | new_leaf = eb_dotag(&new->node.branches, EB_LEAF); |
| 321 | old_node = eb_dotag(&old->node.branches, EB_NODE); |
| 322 | |
| 323 | new->node.node_p = old->node.node_p; |
| 324 | |
| 325 | if (new->key < old->key) { |
| 326 | new->node.leaf_p = new_left; |
| 327 | old->node.node_p = new_rght; |
| 328 | new->node.branches.b[EB_LEFT] = new_leaf; |
| 329 | new->node.branches.b[EB_RGHT] = old_node; |
| 330 | } |
| 331 | else if (new->key > old->key) { |
| 332 | old->node.node_p = new_left; |
| 333 | new->node.leaf_p = new_rght; |
| 334 | new->node.branches.b[EB_LEFT] = old_node; |
| 335 | new->node.branches.b[EB_RGHT] = new_leaf; |
| 336 | } |
| 337 | else { |
| 338 | struct eb_node *ret; |
| 339 | ret = eb_insert_dup(&old->node, &new->node); |
| 340 | return container_of(ret, struct eb64_node, node); |
| 341 | } |
| 342 | break; |
| 343 | } |
| 344 | |
| 345 | /* walk down */ |
| 346 | root = &old->node.branches; |
| 347 | #if BITS_PER_LONG >= 64 |
| 348 | side = (newkey >> old->node.bit) & EB_NODE_BRANCH_MASK; |
| 349 | #else |
| 350 | side = newkey; |
| 351 | side >>= old->node.bit; |
| 352 | if (old->node.bit >= 32) { |
| 353 | side = newkey >> 32; |
| 354 | side >>= old->node.bit & 0x1F; |
| 355 | } |
| 356 | side &= EB_NODE_BRANCH_MASK; |
| 357 | #endif |
| 358 | troot = root->b[side]; |
| 359 | } |
| 360 | |
| 361 | /* Ok, now we are inserting <new> between <root> and <old>. <old>'s |
| 362 | * parent is already set to <new>, and the <root>'s branch is still in |
| 363 | * <side>. Update the root's leaf till we have it. Note that we can also |
| 364 | * find the side by checking the side of new->node.node_p. |
| 365 | */ |
| 366 | |
| 367 | /* We need the common higher bits between new->key and old->key. |
| 368 | * What differences are there between new->key and the node here ? |
| 369 | * NOTE that bit(new) is always < bit(root) because highest |
| 370 | * bit of new->key and old->key are identical here (otherwise they |
| 371 | * would sit on different branches). |
| 372 | */ |
| 373 | // note that if EB_NODE_BITS > 1, we should check that it's still >= 0 |
| 374 | new->node.bit = fls64(new->key ^ old->key) - EB_NODE_BITS; |
| 375 | root->b[side] = eb_dotag(&new->node.branches, EB_NODE); |
| 376 | |
| 377 | return new; |
| 378 | } |
| 379 | |
| 380 | /* Insert eb64_node <new> into subtree starting at node root <root>, using |
| 381 | * signed keys. Only new->key needs be set with the key. The eb64_node |
Willy Tarreau | 1fb6c87 | 2008-05-16 19:48:20 +0200 | [diff] [blame] | 382 | * is returned. If root->b[EB_RGHT]==1, the tree may only contain unique keys. |
Willy Tarreau | e6d2e4d | 2007-11-15 23:56:17 +0100 | [diff] [blame] | 383 | */ |
Willy Tarreau | 75cf17e | 2008-08-29 15:48:49 +0200 | [diff] [blame] | 384 | static forceinline struct eb64_node * |
Willy Tarreau | e6d2e4d | 2007-11-15 23:56:17 +0100 | [diff] [blame] | 385 | __eb64i_insert(struct eb_root *root, struct eb64_node *new) { |
| 386 | struct eb64_node *old; |
| 387 | unsigned int side; |
| 388 | eb_troot_t *troot; |
| 389 | u64 newkey; /* caching the key saves approximately one cycle */ |
Willy Tarreau | 1fb6c87 | 2008-05-16 19:48:20 +0200 | [diff] [blame] | 390 | eb_troot_t *root_right = root; |
Willy Tarreau | e6d2e4d | 2007-11-15 23:56:17 +0100 | [diff] [blame] | 391 | |
| 392 | side = EB_LEFT; |
| 393 | troot = root->b[EB_LEFT]; |
Willy Tarreau | 1fb6c87 | 2008-05-16 19:48:20 +0200 | [diff] [blame] | 394 | root_right = root->b[EB_RGHT]; |
Willy Tarreau | e6d2e4d | 2007-11-15 23:56:17 +0100 | [diff] [blame] | 395 | if (unlikely(troot == NULL)) { |
| 396 | /* Tree is empty, insert the leaf part below the left branch */ |
| 397 | root->b[EB_LEFT] = eb_dotag(&new->node.branches, EB_LEAF); |
| 398 | new->node.leaf_p = eb_dotag(root, EB_LEFT); |
| 399 | new->node.node_p = NULL; /* node part unused */ |
| 400 | return new; |
| 401 | } |
| 402 | |
| 403 | /* The tree descent is fairly easy : |
| 404 | * - first, check if we have reached a leaf node |
| 405 | * - second, check if we have gone too far |
| 406 | * - third, reiterate |
| 407 | * Everywhere, we use <new> for the node node we are inserting, <root> |
| 408 | * for the node we attach it to, and <old> for the node we are |
| 409 | * displacing below <new>. <troot> will always point to the future node |
| 410 | * (tagged with its type). <side> carries the side the node <new> is |
| 411 | * attached to below its parent, which is also where previous node |
| 412 | * was attached. <newkey> carries a high bit shift of the key being |
| 413 | * inserted in order to have negative keys stored before positive |
| 414 | * ones. |
| 415 | */ |
| 416 | newkey = new->key ^ (1ULL << 63); |
| 417 | |
| 418 | while (1) { |
| 419 | if (unlikely(eb_gettag(troot) == EB_LEAF)) { |
| 420 | eb_troot_t *new_left, *new_rght; |
| 421 | eb_troot_t *new_leaf, *old_leaf; |
| 422 | |
| 423 | old = container_of(eb_untag(troot, EB_LEAF), |
| 424 | struct eb64_node, node.branches); |
| 425 | |
| 426 | new_left = eb_dotag(&new->node.branches, EB_LEFT); |
| 427 | new_rght = eb_dotag(&new->node.branches, EB_RGHT); |
| 428 | new_leaf = eb_dotag(&new->node.branches, EB_LEAF); |
| 429 | old_leaf = eb_dotag(&old->node.branches, EB_LEAF); |
| 430 | |
| 431 | new->node.node_p = old->node.leaf_p; |
| 432 | |
| 433 | /* Right here, we have 3 possibilities : |
| 434 | - the tree does not contain the key, and we have |
| 435 | new->key < old->key. We insert new above old, on |
| 436 | the left ; |
| 437 | |
| 438 | - the tree does not contain the key, and we have |
| 439 | new->key > old->key. We insert new above old, on |
| 440 | the right ; |
| 441 | |
| 442 | - the tree does contain the key, which implies it |
| 443 | is alone. We add the new key next to it as a |
| 444 | first duplicate. |
| 445 | |
| 446 | The last two cases can easily be partially merged. |
| 447 | */ |
| 448 | |
| 449 | if ((s64)new->key < (s64)old->key) { |
| 450 | new->node.leaf_p = new_left; |
| 451 | old->node.leaf_p = new_rght; |
| 452 | new->node.branches.b[EB_LEFT] = new_leaf; |
| 453 | new->node.branches.b[EB_RGHT] = old_leaf; |
| 454 | } else { |
Willy Tarreau | 1fb6c87 | 2008-05-16 19:48:20 +0200 | [diff] [blame] | 455 | /* we may refuse to duplicate this key if the tree is |
| 456 | * tagged as containing only unique keys. |
| 457 | */ |
| 458 | if ((new->key == old->key) && eb_gettag(root_right)) |
| 459 | return old; |
| 460 | |
Willy Tarreau | e6d2e4d | 2007-11-15 23:56:17 +0100 | [diff] [blame] | 461 | /* new->key >= old->key, new goes the right */ |
| 462 | old->node.leaf_p = new_left; |
| 463 | new->node.leaf_p = new_rght; |
| 464 | new->node.branches.b[EB_LEFT] = old_leaf; |
| 465 | new->node.branches.b[EB_RGHT] = new_leaf; |
| 466 | |
| 467 | if (new->key == old->key) { |
| 468 | new->node.bit = -1; |
| 469 | root->b[side] = eb_dotag(&new->node.branches, EB_NODE); |
| 470 | return new; |
| 471 | } |
| 472 | } |
| 473 | break; |
| 474 | } |
| 475 | |
| 476 | /* OK we're walking down this link */ |
| 477 | old = container_of(eb_untag(troot, EB_NODE), |
| 478 | struct eb64_node, node.branches); |
| 479 | |
| 480 | /* Stop going down when we don't have common bits anymore. We |
| 481 | * also stop in front of a duplicates tree because it means we |
| 482 | * have to insert above. |
| 483 | */ |
| 484 | |
| 485 | if ((old->node.bit < 0) || /* we're above a duplicate tree, stop here */ |
| 486 | (((new->key ^ old->key) >> old->node.bit) >= EB_NODE_BRANCHES)) { |
| 487 | /* The tree did not contain the key, so we insert <new> before the node |
| 488 | * <old>, and set ->bit to designate the lowest bit position in <new> |
| 489 | * which applies to ->branches.b[]. |
| 490 | */ |
| 491 | eb_troot_t *new_left, *new_rght; |
| 492 | eb_troot_t *new_leaf, *old_node; |
| 493 | |
| 494 | new_left = eb_dotag(&new->node.branches, EB_LEFT); |
| 495 | new_rght = eb_dotag(&new->node.branches, EB_RGHT); |
| 496 | new_leaf = eb_dotag(&new->node.branches, EB_LEAF); |
| 497 | old_node = eb_dotag(&old->node.branches, EB_NODE); |
| 498 | |
| 499 | new->node.node_p = old->node.node_p; |
| 500 | |
| 501 | if ((s64)new->key < (s64)old->key) { |
| 502 | new->node.leaf_p = new_left; |
| 503 | old->node.node_p = new_rght; |
| 504 | new->node.branches.b[EB_LEFT] = new_leaf; |
| 505 | new->node.branches.b[EB_RGHT] = old_node; |
| 506 | } |
| 507 | else if ((s64)new->key > (s64)old->key) { |
| 508 | old->node.node_p = new_left; |
| 509 | new->node.leaf_p = new_rght; |
| 510 | new->node.branches.b[EB_LEFT] = old_node; |
| 511 | new->node.branches.b[EB_RGHT] = new_leaf; |
| 512 | } |
| 513 | else { |
| 514 | struct eb_node *ret; |
| 515 | ret = eb_insert_dup(&old->node, &new->node); |
| 516 | return container_of(ret, struct eb64_node, node); |
| 517 | } |
| 518 | break; |
| 519 | } |
| 520 | |
| 521 | /* walk down */ |
| 522 | root = &old->node.branches; |
| 523 | #if BITS_PER_LONG >= 64 |
| 524 | side = (newkey >> old->node.bit) & EB_NODE_BRANCH_MASK; |
| 525 | #else |
| 526 | side = newkey; |
| 527 | side >>= old->node.bit; |
| 528 | if (old->node.bit >= 32) { |
| 529 | side = newkey >> 32; |
| 530 | side >>= old->node.bit & 0x1F; |
| 531 | } |
| 532 | side &= EB_NODE_BRANCH_MASK; |
| 533 | #endif |
| 534 | troot = root->b[side]; |
| 535 | } |
| 536 | |
| 537 | /* Ok, now we are inserting <new> between <root> and <old>. <old>'s |
| 538 | * parent is already set to <new>, and the <root>'s branch is still in |
| 539 | * <side>. Update the root's leaf till we have it. Note that we can also |
| 540 | * find the side by checking the side of new->node.node_p. |
| 541 | */ |
| 542 | |
| 543 | /* We need the common higher bits between new->key and old->key. |
| 544 | * What differences are there between new->key and the node here ? |
| 545 | * NOTE that bit(new) is always < bit(root) because highest |
| 546 | * bit of new->key and old->key are identical here (otherwise they |
| 547 | * would sit on different branches). |
| 548 | */ |
| 549 | // note that if EB_NODE_BITS > 1, we should check that it's still >= 0 |
| 550 | new->node.bit = fls64(new->key ^ old->key) - EB_NODE_BITS; |
| 551 | root->b[side] = eb_dotag(&new->node.branches, EB_NODE); |
| 552 | |
| 553 | return new; |
| 554 | } |
| 555 | |
Willy Tarreau | f56fd8a | 2007-11-19 18:43:04 +0100 | [diff] [blame] | 556 | #endif /* _COMMON_EB64TREE_H */ |