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