Willy Tarreau | c218602 | 2009-10-26 19:48:54 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Elastic Binary Trees - macros and structures for operations on 32bit nodes. |
Willy Tarreau | f3bfede | 2011-07-25 11:38:17 +0200 | [diff] [blame] | 3 | * Version 6.0.6 |
| 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 | #ifndef _EB32TREE_H |
| 22 | #define _EB32TREE_H |
| 23 | |
| 24 | #include "ebtree.h" |
| 25 | |
| 26 | |
| 27 | /* Return the structure of type <type> whose member <member> points to <ptr> */ |
| 28 | #define eb32_entry(ptr, type, member) container_of(ptr, type, member) |
| 29 | |
Willy Tarreau | c218602 | 2009-10-26 19:48:54 +0100 | [diff] [blame] | 30 | /* |
| 31 | * Exported functions and macros. |
| 32 | * Many of them are always inlined because they are extremely small, and |
| 33 | * are generally called at most once or twice in a program. |
| 34 | */ |
| 35 | |
| 36 | /* Return leftmost node in the tree, or NULL if none */ |
| 37 | static inline struct eb32_node *eb32_first(struct eb_root *root) |
| 38 | { |
| 39 | return eb32_entry(eb_first(root), struct eb32_node, node); |
| 40 | } |
| 41 | |
| 42 | /* Return rightmost node in the tree, or NULL if none */ |
| 43 | static inline struct eb32_node *eb32_last(struct eb_root *root) |
| 44 | { |
| 45 | return eb32_entry(eb_last(root), struct eb32_node, node); |
| 46 | } |
| 47 | |
| 48 | /* Return next node in the tree, or NULL if none */ |
| 49 | static inline struct eb32_node *eb32_next(struct eb32_node *eb32) |
| 50 | { |
| 51 | return eb32_entry(eb_next(&eb32->node), struct eb32_node, node); |
| 52 | } |
| 53 | |
| 54 | /* Return previous node in the tree, or NULL if none */ |
| 55 | static inline struct eb32_node *eb32_prev(struct eb32_node *eb32) |
| 56 | { |
| 57 | return eb32_entry(eb_prev(&eb32->node), struct eb32_node, node); |
| 58 | } |
| 59 | |
Willy Tarreau | 2b57020 | 2013-05-07 15:58:28 +0200 | [diff] [blame] | 60 | /* Return next leaf node within a duplicate sub-tree, or NULL if none. */ |
| 61 | static inline struct eb32_node *eb32_next_dup(struct eb32_node *eb32) |
| 62 | { |
| 63 | return eb32_entry(eb_next_dup(&eb32->node), struct eb32_node, node); |
| 64 | } |
| 65 | |
| 66 | /* Return previous leaf node within a duplicate sub-tree, or NULL if none. */ |
| 67 | static inline struct eb32_node *eb32_prev_dup(struct eb32_node *eb32) |
| 68 | { |
| 69 | return eb32_entry(eb_prev_dup(&eb32->node), struct eb32_node, node); |
| 70 | } |
| 71 | |
Willy Tarreau | c218602 | 2009-10-26 19:48:54 +0100 | [diff] [blame] | 72 | /* Return next node in the tree, skipping duplicates, or NULL if none */ |
| 73 | static inline struct eb32_node *eb32_next_unique(struct eb32_node *eb32) |
| 74 | { |
| 75 | return eb32_entry(eb_next_unique(&eb32->node), struct eb32_node, node); |
| 76 | } |
| 77 | |
| 78 | /* Return previous node in the tree, skipping duplicates, or NULL if none */ |
| 79 | static inline struct eb32_node *eb32_prev_unique(struct eb32_node *eb32) |
| 80 | { |
| 81 | return eb32_entry(eb_prev_unique(&eb32->node), struct eb32_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 inline void eb32_delete(struct eb32_node *eb32) |
| 88 | { |
| 89 | eb_delete(&eb32->node); |
| 90 | } |
| 91 | |
| 92 | /* |
| 93 | * The following functions are not inlined by default. They are declared |
| 94 | * in eb32tree.c, which simply relies on their inline version. |
| 95 | */ |
Willy Tarreau | 03e7853 | 2020-02-25 07:38:05 +0100 | [diff] [blame] | 96 | struct eb32_node *eb32_lookup(struct eb_root *root, u32 x); |
| 97 | struct eb32_node *eb32i_lookup(struct eb_root *root, s32 x); |
| 98 | struct eb32_node *eb32_lookup_le(struct eb_root *root, u32 x); |
| 99 | struct eb32_node *eb32_lookup_ge(struct eb_root *root, u32 x); |
| 100 | struct eb32_node *eb32_insert(struct eb_root *root, struct eb32_node *new); |
| 101 | struct eb32_node *eb32i_insert(struct eb_root *root, struct eb32_node *new); |
Willy Tarreau | c218602 | 2009-10-26 19:48:54 +0100 | [diff] [blame] | 102 | |
| 103 | /* |
| 104 | * The following functions are less likely to be used directly, because their |
| 105 | * code is larger. The non-inlined version is preferred. |
| 106 | */ |
| 107 | |
| 108 | /* Delete node from the tree if it was linked in. Mark the node unused. */ |
| 109 | static forceinline void __eb32_delete(struct eb32_node *eb32) |
| 110 | { |
| 111 | __eb_delete(&eb32->node); |
| 112 | } |
| 113 | |
| 114 | /* |
Joseph Herlant | 7c16c0e | 2018-11-13 19:55:57 -0800 | [diff] [blame] | 115 | * Find the first occurrence of a key in the tree <root>. If none can be |
Willy Tarreau | c218602 | 2009-10-26 19:48:54 +0100 | [diff] [blame] | 116 | * found, return NULL. |
| 117 | */ |
| 118 | static forceinline struct eb32_node *__eb32_lookup(struct eb_root *root, u32 x) |
| 119 | { |
| 120 | struct eb32_node *node; |
| 121 | eb_troot_t *troot; |
| 122 | u32 y; |
Willy Tarreau | 3a93244 | 2010-05-09 19:29:23 +0200 | [diff] [blame] | 123 | int node_bit; |
Willy Tarreau | c218602 | 2009-10-26 19:48:54 +0100 | [diff] [blame] | 124 | |
| 125 | troot = root->b[EB_LEFT]; |
| 126 | if (unlikely(troot == NULL)) |
| 127 | return NULL; |
| 128 | |
| 129 | while (1) { |
| 130 | if ((eb_gettag(troot) == EB_LEAF)) { |
| 131 | node = container_of(eb_untag(troot, EB_LEAF), |
| 132 | struct eb32_node, node.branches); |
| 133 | if (node->key == x) |
| 134 | return node; |
| 135 | else |
| 136 | return NULL; |
| 137 | } |
| 138 | node = container_of(eb_untag(troot, EB_NODE), |
| 139 | struct eb32_node, node.branches); |
Willy Tarreau | 3a93244 | 2010-05-09 19:29:23 +0200 | [diff] [blame] | 140 | node_bit = node->node.bit; |
Willy Tarreau | c218602 | 2009-10-26 19:48:54 +0100 | [diff] [blame] | 141 | |
| 142 | y = node->key ^ x; |
| 143 | if (!y) { |
| 144 | /* Either we found the node which holds the key, or |
| 145 | * we have a dup tree. In the later case, we have to |
| 146 | * walk it down left to get the first entry. |
| 147 | */ |
Willy Tarreau | 3a93244 | 2010-05-09 19:29:23 +0200 | [diff] [blame] | 148 | if (node_bit < 0) { |
Willy Tarreau | c218602 | 2009-10-26 19:48:54 +0100 | [diff] [blame] | 149 | troot = node->node.branches.b[EB_LEFT]; |
| 150 | while (eb_gettag(troot) != EB_LEAF) |
| 151 | troot = (eb_untag(troot, EB_NODE))->b[EB_LEFT]; |
| 152 | node = container_of(eb_untag(troot, EB_LEAF), |
| 153 | struct eb32_node, node.branches); |
| 154 | } |
| 155 | return node; |
| 156 | } |
| 157 | |
Willy Tarreau | 3a93244 | 2010-05-09 19:29:23 +0200 | [diff] [blame] | 158 | if ((y >> node_bit) >= EB_NODE_BRANCHES) |
Willy Tarreau | c218602 | 2009-10-26 19:48:54 +0100 | [diff] [blame] | 159 | return NULL; /* no more common bits */ |
| 160 | |
Willy Tarreau | 3a93244 | 2010-05-09 19:29:23 +0200 | [diff] [blame] | 161 | troot = node->node.branches.b[(x >> node_bit) & EB_NODE_BRANCH_MASK]; |
Willy Tarreau | c218602 | 2009-10-26 19:48:54 +0100 | [diff] [blame] | 162 | } |
| 163 | } |
| 164 | |
| 165 | /* |
Joseph Herlant | 7c16c0e | 2018-11-13 19:55:57 -0800 | [diff] [blame] | 166 | * Find the first occurrence of a signed key in the tree <root>. If none can |
Willy Tarreau | c218602 | 2009-10-26 19:48:54 +0100 | [diff] [blame] | 167 | * be found, return NULL. |
| 168 | */ |
| 169 | static forceinline struct eb32_node *__eb32i_lookup(struct eb_root *root, s32 x) |
| 170 | { |
| 171 | struct eb32_node *node; |
| 172 | eb_troot_t *troot; |
| 173 | u32 key = x ^ 0x80000000; |
| 174 | u32 y; |
Willy Tarreau | 3a93244 | 2010-05-09 19:29:23 +0200 | [diff] [blame] | 175 | int node_bit; |
Willy Tarreau | c218602 | 2009-10-26 19:48:54 +0100 | [diff] [blame] | 176 | |
| 177 | troot = root->b[EB_LEFT]; |
| 178 | if (unlikely(troot == NULL)) |
| 179 | return NULL; |
| 180 | |
| 181 | while (1) { |
| 182 | if ((eb_gettag(troot) == EB_LEAF)) { |
| 183 | node = container_of(eb_untag(troot, EB_LEAF), |
| 184 | struct eb32_node, node.branches); |
Willy Tarreau | 3a93244 | 2010-05-09 19:29:23 +0200 | [diff] [blame] | 185 | if (node->key == (u32)x) |
Willy Tarreau | c218602 | 2009-10-26 19:48:54 +0100 | [diff] [blame] | 186 | return node; |
| 187 | else |
| 188 | return NULL; |
| 189 | } |
| 190 | node = container_of(eb_untag(troot, EB_NODE), |
| 191 | struct eb32_node, node.branches); |
Willy Tarreau | 3a93244 | 2010-05-09 19:29:23 +0200 | [diff] [blame] | 192 | node_bit = node->node.bit; |
Willy Tarreau | c218602 | 2009-10-26 19:48:54 +0100 | [diff] [blame] | 193 | |
| 194 | y = node->key ^ x; |
| 195 | if (!y) { |
| 196 | /* Either we found the node which holds the key, or |
| 197 | * we have a dup tree. In the later case, we have to |
| 198 | * walk it down left to get the first entry. |
| 199 | */ |
Willy Tarreau | 3a93244 | 2010-05-09 19:29:23 +0200 | [diff] [blame] | 200 | if (node_bit < 0) { |
Willy Tarreau | c218602 | 2009-10-26 19:48:54 +0100 | [diff] [blame] | 201 | troot = node->node.branches.b[EB_LEFT]; |
| 202 | while (eb_gettag(troot) != EB_LEAF) |
| 203 | troot = (eb_untag(troot, EB_NODE))->b[EB_LEFT]; |
| 204 | node = container_of(eb_untag(troot, EB_LEAF), |
| 205 | struct eb32_node, node.branches); |
| 206 | } |
| 207 | return node; |
| 208 | } |
| 209 | |
Willy Tarreau | 3a93244 | 2010-05-09 19:29:23 +0200 | [diff] [blame] | 210 | if ((y >> node_bit) >= EB_NODE_BRANCHES) |
Willy Tarreau | c218602 | 2009-10-26 19:48:54 +0100 | [diff] [blame] | 211 | return NULL; /* no more common bits */ |
| 212 | |
Willy Tarreau | 3a93244 | 2010-05-09 19:29:23 +0200 | [diff] [blame] | 213 | troot = node->node.branches.b[(key >> node_bit) & EB_NODE_BRANCH_MASK]; |
Willy Tarreau | c218602 | 2009-10-26 19:48:54 +0100 | [diff] [blame] | 214 | } |
| 215 | } |
| 216 | |
| 217 | /* Insert eb32_node <new> into subtree starting at node root <root>. |
| 218 | * Only new->key needs be set with the key. The eb32_node is returned. |
| 219 | * If root->b[EB_RGHT]==1, the tree may only contain unique keys. |
| 220 | */ |
| 221 | static forceinline struct eb32_node * |
| 222 | __eb32_insert(struct eb_root *root, struct eb32_node *new) { |
| 223 | struct eb32_node *old; |
| 224 | unsigned int side; |
Willy Tarreau | 3a93244 | 2010-05-09 19:29:23 +0200 | [diff] [blame] | 225 | eb_troot_t *troot, **up_ptr; |
Willy Tarreau | c218602 | 2009-10-26 19:48:54 +0100 | [diff] [blame] | 226 | u32 newkey; /* caching the key saves approximately one cycle */ |
Willy Tarreau | 6258f7b | 2011-09-19 20:48:00 +0200 | [diff] [blame] | 227 | eb_troot_t *root_right; |
Willy Tarreau | 3a93244 | 2010-05-09 19:29:23 +0200 | [diff] [blame] | 228 | eb_troot_t *new_left, *new_rght; |
| 229 | eb_troot_t *new_leaf; |
| 230 | int old_node_bit; |
Willy Tarreau | c218602 | 2009-10-26 19:48:54 +0100 | [diff] [blame] | 231 | |
| 232 | side = EB_LEFT; |
| 233 | troot = root->b[EB_LEFT]; |
| 234 | root_right = root->b[EB_RGHT]; |
| 235 | if (unlikely(troot == NULL)) { |
| 236 | /* Tree is empty, insert the leaf part below the left branch */ |
| 237 | root->b[EB_LEFT] = eb_dotag(&new->node.branches, EB_LEAF); |
| 238 | new->node.leaf_p = eb_dotag(root, EB_LEFT); |
| 239 | new->node.node_p = NULL; /* node part unused */ |
| 240 | return new; |
| 241 | } |
| 242 | |
| 243 | /* The tree descent is fairly easy : |
| 244 | * - first, check if we have reached a leaf node |
| 245 | * - second, check if we have gone too far |
| 246 | * - third, reiterate |
| 247 | * Everywhere, we use <new> for the node node we are inserting, <root> |
| 248 | * for the node we attach it to, and <old> for the node we are |
| 249 | * displacing below <new>. <troot> will always point to the future node |
| 250 | * (tagged with its type). <side> carries the side the node <new> is |
| 251 | * attached to below its parent, which is also where previous node |
| 252 | * was attached. <newkey> carries the key being inserted. |
| 253 | */ |
| 254 | newkey = new->key; |
| 255 | |
| 256 | while (1) { |
Willy Tarreau | 3a93244 | 2010-05-09 19:29:23 +0200 | [diff] [blame] | 257 | if (eb_gettag(troot) == EB_LEAF) { |
| 258 | /* insert above a leaf */ |
Willy Tarreau | c218602 | 2009-10-26 19:48:54 +0100 | [diff] [blame] | 259 | old = container_of(eb_untag(troot, EB_LEAF), |
| 260 | struct eb32_node, node.branches); |
Willy Tarreau | c218602 | 2009-10-26 19:48:54 +0100 | [diff] [blame] | 261 | new->node.node_p = old->node.leaf_p; |
Willy Tarreau | 3a93244 | 2010-05-09 19:29:23 +0200 | [diff] [blame] | 262 | up_ptr = &old->node.leaf_p; |
Willy Tarreau | c218602 | 2009-10-26 19:48:54 +0100 | [diff] [blame] | 263 | break; |
| 264 | } |
| 265 | |
| 266 | /* OK we're walking down this link */ |
| 267 | old = container_of(eb_untag(troot, EB_NODE), |
| 268 | struct eb32_node, node.branches); |
Willy Tarreau | 3a93244 | 2010-05-09 19:29:23 +0200 | [diff] [blame] | 269 | old_node_bit = old->node.bit; |
Willy Tarreau | c218602 | 2009-10-26 19:48:54 +0100 | [diff] [blame] | 270 | |
| 271 | /* Stop going down when we don't have common bits anymore. We |
| 272 | * also stop in front of a duplicates tree because it means we |
| 273 | * have to insert above. |
| 274 | */ |
| 275 | |
Willy Tarreau | 3a93244 | 2010-05-09 19:29:23 +0200 | [diff] [blame] | 276 | if ((old_node_bit < 0) || /* we're above a duplicate tree, stop here */ |
| 277 | (((new->key ^ old->key) >> old_node_bit) >= EB_NODE_BRANCHES)) { |
Willy Tarreau | c218602 | 2009-10-26 19:48:54 +0100 | [diff] [blame] | 278 | /* The tree did not contain the key, so we insert <new> before the node |
| 279 | * <old>, and set ->bit to designate the lowest bit position in <new> |
| 280 | * which applies to ->branches.b[]. |
| 281 | */ |
Willy Tarreau | c218602 | 2009-10-26 19:48:54 +0100 | [diff] [blame] | 282 | new->node.node_p = old->node.node_p; |
Willy Tarreau | 3a93244 | 2010-05-09 19:29:23 +0200 | [diff] [blame] | 283 | up_ptr = &old->node.node_p; |
Willy Tarreau | c218602 | 2009-10-26 19:48:54 +0100 | [diff] [blame] | 284 | break; |
| 285 | } |
| 286 | |
| 287 | /* walk down */ |
| 288 | root = &old->node.branches; |
Willy Tarreau | 3a93244 | 2010-05-09 19:29:23 +0200 | [diff] [blame] | 289 | side = (newkey >> old_node_bit) & EB_NODE_BRANCH_MASK; |
Willy Tarreau | c218602 | 2009-10-26 19:48:54 +0100 | [diff] [blame] | 290 | troot = root->b[side]; |
| 291 | } |
| 292 | |
Willy Tarreau | 3a93244 | 2010-05-09 19:29:23 +0200 | [diff] [blame] | 293 | new_left = eb_dotag(&new->node.branches, EB_LEFT); |
| 294 | new_rght = eb_dotag(&new->node.branches, EB_RGHT); |
| 295 | new_leaf = eb_dotag(&new->node.branches, EB_LEAF); |
Willy Tarreau | c218602 | 2009-10-26 19:48:54 +0100 | [diff] [blame] | 296 | |
| 297 | /* We need the common higher bits between new->key and old->key. |
| 298 | * What differences are there between new->key and the node here ? |
| 299 | * NOTE that bit(new) is always < bit(root) because highest |
| 300 | * bit of new->key and old->key are identical here (otherwise they |
| 301 | * would sit on different branches). |
| 302 | */ |
Willy Tarreau | 3a93244 | 2010-05-09 19:29:23 +0200 | [diff] [blame] | 303 | |
Willy Tarreau | c218602 | 2009-10-26 19:48:54 +0100 | [diff] [blame] | 304 | // note that if EB_NODE_BITS > 1, we should check that it's still >= 0 |
| 305 | new->node.bit = flsnz(new->key ^ old->key) - EB_NODE_BITS; |
Willy Tarreau | 3a93244 | 2010-05-09 19:29:23 +0200 | [diff] [blame] | 306 | |
| 307 | if (new->key == old->key) { |
| 308 | new->node.bit = -1; /* mark as new dup tree, just in case */ |
| 309 | |
| 310 | if (likely(eb_gettag(root_right))) { |
| 311 | /* we refuse to duplicate this key if the tree is |
| 312 | * tagged as containing only unique keys. |
| 313 | */ |
| 314 | return old; |
| 315 | } |
| 316 | |
| 317 | if (eb_gettag(troot) != EB_LEAF) { |
| 318 | /* there was already a dup tree below */ |
| 319 | struct eb_node *ret; |
| 320 | ret = eb_insert_dup(&old->node, &new->node); |
| 321 | return container_of(ret, struct eb32_node, node); |
| 322 | } |
| 323 | /* otherwise fall through */ |
| 324 | } |
Willy Tarreau | c218602 | 2009-10-26 19:48:54 +0100 | [diff] [blame] | 325 | |
Willy Tarreau | 3a93244 | 2010-05-09 19:29:23 +0200 | [diff] [blame] | 326 | if (new->key >= old->key) { |
| 327 | new->node.branches.b[EB_LEFT] = troot; |
| 328 | new->node.branches.b[EB_RGHT] = new_leaf; |
| 329 | new->node.leaf_p = new_rght; |
| 330 | *up_ptr = new_left; |
| 331 | } |
| 332 | else { |
| 333 | new->node.branches.b[EB_LEFT] = new_leaf; |
| 334 | new->node.branches.b[EB_RGHT] = troot; |
| 335 | new->node.leaf_p = new_left; |
| 336 | *up_ptr = new_rght; |
| 337 | } |
| 338 | |
| 339 | /* Ok, now we are inserting <new> between <root> and <old>. <old>'s |
| 340 | * parent is already set to <new>, and the <root>'s branch is still in |
| 341 | * <side>. Update the root's leaf till we have it. Note that we can also |
| 342 | * find the side by checking the side of new->node.node_p. |
| 343 | */ |
| 344 | |
| 345 | root->b[side] = eb_dotag(&new->node.branches, EB_NODE); |
Willy Tarreau | c218602 | 2009-10-26 19:48:54 +0100 | [diff] [blame] | 346 | return new; |
| 347 | } |
| 348 | |
| 349 | /* Insert eb32_node <new> into subtree starting at node root <root>, using |
| 350 | * signed keys. Only new->key needs be set with the key. The eb32_node |
| 351 | * is returned. If root->b[EB_RGHT]==1, the tree may only contain unique keys. |
| 352 | */ |
| 353 | static forceinline struct eb32_node * |
| 354 | __eb32i_insert(struct eb_root *root, struct eb32_node *new) { |
| 355 | struct eb32_node *old; |
| 356 | unsigned int side; |
Willy Tarreau | 3a93244 | 2010-05-09 19:29:23 +0200 | [diff] [blame] | 357 | eb_troot_t *troot, **up_ptr; |
Willy Tarreau | c218602 | 2009-10-26 19:48:54 +0100 | [diff] [blame] | 358 | int newkey; /* caching the key saves approximately one cycle */ |
Willy Tarreau | 6258f7b | 2011-09-19 20:48:00 +0200 | [diff] [blame] | 359 | eb_troot_t *root_right; |
Willy Tarreau | 3a93244 | 2010-05-09 19:29:23 +0200 | [diff] [blame] | 360 | eb_troot_t *new_left, *new_rght; |
| 361 | eb_troot_t *new_leaf; |
| 362 | int old_node_bit; |
Willy Tarreau | c218602 | 2009-10-26 19:48:54 +0100 | [diff] [blame] | 363 | |
| 364 | side = EB_LEFT; |
| 365 | troot = root->b[EB_LEFT]; |
| 366 | root_right = root->b[EB_RGHT]; |
| 367 | if (unlikely(troot == NULL)) { |
| 368 | /* Tree is empty, insert the leaf part below the left branch */ |
| 369 | root->b[EB_LEFT] = eb_dotag(&new->node.branches, EB_LEAF); |
| 370 | new->node.leaf_p = eb_dotag(root, EB_LEFT); |
| 371 | new->node.node_p = NULL; /* node part unused */ |
| 372 | return new; |
| 373 | } |
| 374 | |
| 375 | /* The tree descent is fairly easy : |
| 376 | * - first, check if we have reached a leaf node |
| 377 | * - second, check if we have gone too far |
| 378 | * - third, reiterate |
| 379 | * Everywhere, we use <new> for the node node we are inserting, <root> |
| 380 | * for the node we attach it to, and <old> for the node we are |
| 381 | * displacing below <new>. <troot> will always point to the future node |
| 382 | * (tagged with its type). <side> carries the side the node <new> is |
| 383 | * attached to below its parent, which is also where previous node |
| 384 | * was attached. <newkey> carries a high bit shift of the key being |
| 385 | * inserted in order to have negative keys stored before positive |
| 386 | * ones. |
| 387 | */ |
| 388 | newkey = new->key + 0x80000000; |
| 389 | |
| 390 | while (1) { |
Willy Tarreau | 3a93244 | 2010-05-09 19:29:23 +0200 | [diff] [blame] | 391 | if (eb_gettag(troot) == EB_LEAF) { |
Willy Tarreau | c218602 | 2009-10-26 19:48:54 +0100 | [diff] [blame] | 392 | old = container_of(eb_untag(troot, EB_LEAF), |
| 393 | struct eb32_node, node.branches); |
Willy Tarreau | c218602 | 2009-10-26 19:48:54 +0100 | [diff] [blame] | 394 | new->node.node_p = old->node.leaf_p; |
Willy Tarreau | 3a93244 | 2010-05-09 19:29:23 +0200 | [diff] [blame] | 395 | up_ptr = &old->node.leaf_p; |
Willy Tarreau | c218602 | 2009-10-26 19:48:54 +0100 | [diff] [blame] | 396 | break; |
| 397 | } |
| 398 | |
| 399 | /* OK we're walking down this link */ |
| 400 | old = container_of(eb_untag(troot, EB_NODE), |
| 401 | struct eb32_node, node.branches); |
Willy Tarreau | 3a93244 | 2010-05-09 19:29:23 +0200 | [diff] [blame] | 402 | old_node_bit = old->node.bit; |
Willy Tarreau | c218602 | 2009-10-26 19:48:54 +0100 | [diff] [blame] | 403 | |
| 404 | /* Stop going down when we don't have common bits anymore. We |
| 405 | * also stop in front of a duplicates tree because it means we |
| 406 | * have to insert above. |
| 407 | */ |
| 408 | |
Willy Tarreau | 3a93244 | 2010-05-09 19:29:23 +0200 | [diff] [blame] | 409 | if ((old_node_bit < 0) || /* we're above a duplicate tree, stop here */ |
| 410 | (((new->key ^ old->key) >> old_node_bit) >= EB_NODE_BRANCHES)) { |
Willy Tarreau | c218602 | 2009-10-26 19:48:54 +0100 | [diff] [blame] | 411 | /* The tree did not contain the key, so we insert <new> before the node |
| 412 | * <old>, and set ->bit to designate the lowest bit position in <new> |
| 413 | * which applies to ->branches.b[]. |
| 414 | */ |
Willy Tarreau | c218602 | 2009-10-26 19:48:54 +0100 | [diff] [blame] | 415 | new->node.node_p = old->node.node_p; |
Willy Tarreau | 3a93244 | 2010-05-09 19:29:23 +0200 | [diff] [blame] | 416 | up_ptr = &old->node.node_p; |
Willy Tarreau | c218602 | 2009-10-26 19:48:54 +0100 | [diff] [blame] | 417 | break; |
| 418 | } |
| 419 | |
| 420 | /* walk down */ |
| 421 | root = &old->node.branches; |
Willy Tarreau | 3a93244 | 2010-05-09 19:29:23 +0200 | [diff] [blame] | 422 | side = (newkey >> old_node_bit) & EB_NODE_BRANCH_MASK; |
Willy Tarreau | c218602 | 2009-10-26 19:48:54 +0100 | [diff] [blame] | 423 | troot = root->b[side]; |
| 424 | } |
| 425 | |
Willy Tarreau | 3a93244 | 2010-05-09 19:29:23 +0200 | [diff] [blame] | 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); |
Willy Tarreau | c218602 | 2009-10-26 19:48:54 +0100 | [diff] [blame] | 429 | |
| 430 | /* We need the common higher bits between new->key and old->key. |
| 431 | * What differences are there between new->key and the node here ? |
| 432 | * NOTE that bit(new) is always < bit(root) because highest |
| 433 | * bit of new->key and old->key are identical here (otherwise they |
| 434 | * would sit on different branches). |
| 435 | */ |
Willy Tarreau | 3a93244 | 2010-05-09 19:29:23 +0200 | [diff] [blame] | 436 | |
Willy Tarreau | c218602 | 2009-10-26 19:48:54 +0100 | [diff] [blame] | 437 | // note that if EB_NODE_BITS > 1, we should check that it's still >= 0 |
| 438 | new->node.bit = flsnz(new->key ^ old->key) - EB_NODE_BITS; |
Willy Tarreau | 3a93244 | 2010-05-09 19:29:23 +0200 | [diff] [blame] | 439 | |
| 440 | if (new->key == old->key) { |
| 441 | new->node.bit = -1; /* mark as new dup tree, just in case */ |
| 442 | |
| 443 | if (likely(eb_gettag(root_right))) { |
| 444 | /* we refuse to duplicate this key if the tree is |
| 445 | * tagged as containing only unique keys. |
| 446 | */ |
| 447 | return old; |
| 448 | } |
| 449 | |
| 450 | if (eb_gettag(troot) != EB_LEAF) { |
| 451 | /* there was already a dup tree below */ |
| 452 | struct eb_node *ret; |
| 453 | ret = eb_insert_dup(&old->node, &new->node); |
| 454 | return container_of(ret, struct eb32_node, node); |
| 455 | } |
| 456 | /* otherwise fall through */ |
| 457 | } |
Willy Tarreau | c218602 | 2009-10-26 19:48:54 +0100 | [diff] [blame] | 458 | |
Willy Tarreau | 3a93244 | 2010-05-09 19:29:23 +0200 | [diff] [blame] | 459 | if ((s32)new->key >= (s32)old->key) { |
| 460 | new->node.branches.b[EB_LEFT] = troot; |
| 461 | new->node.branches.b[EB_RGHT] = new_leaf; |
| 462 | new->node.leaf_p = new_rght; |
| 463 | *up_ptr = new_left; |
| 464 | } |
| 465 | else { |
| 466 | new->node.branches.b[EB_LEFT] = new_leaf; |
| 467 | new->node.branches.b[EB_RGHT] = troot; |
| 468 | new->node.leaf_p = new_left; |
| 469 | *up_ptr = new_rght; |
| 470 | } |
| 471 | |
| 472 | /* Ok, now we are inserting <new> between <root> and <old>. <old>'s |
| 473 | * parent is already set to <new>, and the <root>'s branch is still in |
| 474 | * <side>. Update the root's leaf till we have it. Note that we can also |
| 475 | * find the side by checking the side of new->node.node_p. |
| 476 | */ |
| 477 | |
| 478 | root->b[side] = eb_dotag(&new->node.branches, EB_NODE); |
Willy Tarreau | c218602 | 2009-10-26 19:48:54 +0100 | [diff] [blame] | 479 | return new; |
| 480 | } |
| 481 | |
| 482 | #endif /* _EB32_TREE_H */ |