at master 12 kB view raw
1/* SPDX-License-Identifier: GPL-2.0-or-later */ 2/* 3 Red Black Trees 4 (C) 1999 Andrea Arcangeli <andrea@suse.de> 5 6 7 linux/include/linux/rbtree.h 8 9 To use rbtrees you'll have to implement your own insert and search cores. 10 This will avoid us to use callbacks and to drop drammatically performances. 11 I know it's not the cleaner way, but in C (not in C++) to get 12 performances and genericity... 13 14 See Documentation/core-api/rbtree.rst for documentation and samples. 15*/ 16 17#ifndef _LINUX_RBTREE_H 18#define _LINUX_RBTREE_H 19 20#include <linux/container_of.h> 21#include <linux/rbtree_types.h> 22 23#include <linux/stddef.h> 24#include <linux/rcupdate.h> 25 26#define rb_parent(r) ((struct rb_node *)((r)->__rb_parent_color & ~3)) 27 28#define rb_entry(ptr, type, member) container_of(ptr, type, member) 29 30#define RB_EMPTY_ROOT(root) (READ_ONCE((root)->rb_node) == NULL) 31 32/* 'empty' nodes are nodes that are known not to be inserted in an rbtree */ 33#define RB_EMPTY_NODE(node) \ 34 ((node)->__rb_parent_color == (unsigned long)(node)) 35#define RB_CLEAR_NODE(node) \ 36 ((node)->__rb_parent_color = (unsigned long)(node)) 37 38 39extern void rb_insert_color(struct rb_node *, struct rb_root *); 40extern void rb_erase(struct rb_node *, struct rb_root *); 41 42 43/* Find logical next and previous nodes in a tree */ 44extern struct rb_node *rb_next(const struct rb_node *); 45extern struct rb_node *rb_prev(const struct rb_node *); 46 47/* 48 * This function returns the first node (in sort order) of the tree. 49 */ 50static inline struct rb_node *rb_first(const struct rb_root *root) 51{ 52 struct rb_node *n; 53 54 n = root->rb_node; 55 if (!n) 56 return NULL; 57 while (n->rb_left) 58 n = n->rb_left; 59 return n; 60} 61 62/* 63 * This function returns the last node (in sort order) of the tree. 64 */ 65static inline struct rb_node *rb_last(const struct rb_root *root) 66{ 67 struct rb_node *n; 68 69 n = root->rb_node; 70 if (!n) 71 return NULL; 72 while (n->rb_right) 73 n = n->rb_right; 74 return n; 75} 76 77/* Postorder iteration - always visit the parent after its children */ 78extern struct rb_node *rb_first_postorder(const struct rb_root *); 79extern struct rb_node *rb_next_postorder(const struct rb_node *); 80 81/* Fast replacement of a single node without remove/rebalance/add/rebalance */ 82extern void rb_replace_node(struct rb_node *victim, struct rb_node *new, 83 struct rb_root *root); 84extern void rb_replace_node_rcu(struct rb_node *victim, struct rb_node *new, 85 struct rb_root *root); 86 87static inline void rb_link_node(struct rb_node *node, struct rb_node *parent, 88 struct rb_node **rb_link) 89{ 90 node->__rb_parent_color = (unsigned long)parent; 91 node->rb_left = node->rb_right = NULL; 92 93 *rb_link = node; 94} 95 96static inline void rb_link_node_rcu(struct rb_node *node, struct rb_node *parent, 97 struct rb_node **rb_link) 98{ 99 node->__rb_parent_color = (unsigned long)parent; 100 node->rb_left = node->rb_right = NULL; 101 102 rcu_assign_pointer(*rb_link, node); 103} 104 105#define rb_entry_safe(ptr, type, member) \ 106 ({ typeof(ptr) ____ptr = (ptr); \ 107 ____ptr ? rb_entry(____ptr, type, member) : NULL; \ 108 }) 109 110/** 111 * rbtree_postorder_for_each_entry_safe - iterate in post-order over rb_root of 112 * given type allowing the backing memory of @pos to be invalidated 113 * 114 * @pos: the 'type *' to use as a loop cursor. 115 * @n: another 'type *' to use as temporary storage 116 * @root: 'rb_root *' of the rbtree. 117 * @field: the name of the rb_node field within 'type'. 118 * 119 * rbtree_postorder_for_each_entry_safe() provides a similar guarantee as 120 * list_for_each_entry_safe() and allows the iteration to continue independent 121 * of changes to @pos by the body of the loop. 122 * 123 * Note, however, that it cannot handle other modifications that re-order the 124 * rbtree it is iterating over. This includes calling rb_erase() on @pos, as 125 * rb_erase() may rebalance the tree, causing us to miss some nodes. 126 */ 127#define rbtree_postorder_for_each_entry_safe(pos, n, root, field) \ 128 for (pos = rb_entry_safe(rb_first_postorder(root), typeof(*pos), field); \ 129 pos && ({ n = rb_entry_safe(rb_next_postorder(&pos->field), \ 130 typeof(*pos), field); 1; }); \ 131 pos = n) 132 133/* Same as rb_first(), but O(1) */ 134#define rb_first_cached(root) (root)->rb_leftmost 135 136static inline void rb_insert_color_cached(struct rb_node *node, 137 struct rb_root_cached *root, 138 bool leftmost) 139{ 140 if (leftmost) 141 root->rb_leftmost = node; 142 rb_insert_color(node, &root->rb_root); 143} 144 145 146static inline struct rb_node * 147rb_erase_cached(struct rb_node *node, struct rb_root_cached *root) 148{ 149 struct rb_node *leftmost = NULL; 150 151 if (root->rb_leftmost == node) 152 leftmost = root->rb_leftmost = rb_next(node); 153 154 rb_erase(node, &root->rb_root); 155 156 return leftmost; 157} 158 159static inline void rb_replace_node_cached(struct rb_node *victim, 160 struct rb_node *new, 161 struct rb_root_cached *root) 162{ 163 if (root->rb_leftmost == victim) 164 root->rb_leftmost = new; 165 rb_replace_node(victim, new, &root->rb_root); 166} 167 168/* 169 * The below helper functions use 2 operators with 3 different 170 * calling conventions. The operators are related like: 171 * 172 * comp(a->key,b) < 0 := less(a,b) 173 * comp(a->key,b) > 0 := less(b,a) 174 * comp(a->key,b) == 0 := !less(a,b) && !less(b,a) 175 * 176 * If these operators define a partial order on the elements we make no 177 * guarantee on which of the elements matching the key is found. See 178 * rb_find(). 179 * 180 * The reason for this is to allow the find() interface without requiring an 181 * on-stack dummy object, which might not be feasible due to object size. 182 */ 183 184/** 185 * rb_add_cached() - insert @node into the leftmost cached tree @tree 186 * @node: node to insert 187 * @tree: leftmost cached tree to insert @node into 188 * @less: operator defining the (partial) node order 189 * 190 * Returns @node when it is the new leftmost, or NULL. 191 */ 192static __always_inline struct rb_node * 193rb_add_cached(struct rb_node *node, struct rb_root_cached *tree, 194 bool (*less)(struct rb_node *, const struct rb_node *)) 195{ 196 struct rb_node **link = &tree->rb_root.rb_node; 197 struct rb_node *parent = NULL; 198 bool leftmost = true; 199 200 while (*link) { 201 parent = *link; 202 if (less(node, parent)) { 203 link = &parent->rb_left; 204 } else { 205 link = &parent->rb_right; 206 leftmost = false; 207 } 208 } 209 210 rb_link_node(node, parent, link); 211 rb_insert_color_cached(node, tree, leftmost); 212 213 return leftmost ? node : NULL; 214} 215 216/** 217 * rb_add() - insert @node into @tree 218 * @node: node to insert 219 * @tree: tree to insert @node into 220 * @less: operator defining the (partial) node order 221 */ 222static __always_inline void 223rb_add(struct rb_node *node, struct rb_root *tree, 224 bool (*less)(struct rb_node *, const struct rb_node *)) 225{ 226 struct rb_node **link = &tree->rb_node; 227 struct rb_node *parent = NULL; 228 229 while (*link) { 230 parent = *link; 231 if (less(node, parent)) 232 link = &parent->rb_left; 233 else 234 link = &parent->rb_right; 235 } 236 237 rb_link_node(node, parent, link); 238 rb_insert_color(node, tree); 239} 240 241/** 242 * rb_find_add_cached() - find equivalent @node in @tree, or add @node 243 * @node: node to look-for / insert 244 * @tree: tree to search / modify 245 * @cmp: operator defining the node order 246 * 247 * Returns the rb_node matching @node, or NULL when no match is found and @node 248 * is inserted. 249 */ 250static __always_inline struct rb_node * 251rb_find_add_cached(struct rb_node *node, struct rb_root_cached *tree, 252 int (*cmp)(const struct rb_node *new, const struct rb_node *exist)) 253{ 254 bool leftmost = true; 255 struct rb_node **link = &tree->rb_root.rb_node; 256 struct rb_node *parent = NULL; 257 int c; 258 259 while (*link) { 260 parent = *link; 261 c = cmp(node, parent); 262 263 if (c < 0) { 264 link = &parent->rb_left; 265 } else if (c > 0) { 266 link = &parent->rb_right; 267 leftmost = false; 268 } else { 269 return parent; 270 } 271 } 272 273 rb_link_node(node, parent, link); 274 rb_insert_color_cached(node, tree, leftmost); 275 return NULL; 276} 277 278/** 279 * rb_find_add() - find equivalent @node in @tree, or add @node 280 * @node: node to look-for / insert 281 * @tree: tree to search / modify 282 * @cmp: operator defining the node order 283 * 284 * Returns the rb_node matching @node, or NULL when no match is found and @node 285 * is inserted. 286 */ 287static __always_inline struct rb_node * 288rb_find_add(struct rb_node *node, struct rb_root *tree, 289 int (*cmp)(struct rb_node *, const struct rb_node *)) 290{ 291 struct rb_node **link = &tree->rb_node; 292 struct rb_node *parent = NULL; 293 int c; 294 295 while (*link) { 296 parent = *link; 297 c = cmp(node, parent); 298 299 if (c < 0) 300 link = &parent->rb_left; 301 else if (c > 0) 302 link = &parent->rb_right; 303 else 304 return parent; 305 } 306 307 rb_link_node(node, parent, link); 308 rb_insert_color(node, tree); 309 return NULL; 310} 311 312/** 313 * rb_find_add_rcu() - find equivalent @node in @tree, or add @node 314 * @node: node to look-for / insert 315 * @tree: tree to search / modify 316 * @cmp: operator defining the node order 317 * 318 * Adds a Store-Release for link_node. 319 * 320 * Returns the rb_node matching @node, or NULL when no match is found and @node 321 * is inserted. 322 */ 323static __always_inline struct rb_node * 324rb_find_add_rcu(struct rb_node *node, struct rb_root *tree, 325 int (*cmp)(struct rb_node *, const struct rb_node *)) 326{ 327 struct rb_node **link = &tree->rb_node; 328 struct rb_node *parent = NULL; 329 int c; 330 331 while (*link) { 332 parent = *link; 333 c = cmp(node, parent); 334 335 if (c < 0) 336 link = &parent->rb_left; 337 else if (c > 0) 338 link = &parent->rb_right; 339 else 340 return parent; 341 } 342 343 rb_link_node_rcu(node, parent, link); 344 rb_insert_color(node, tree); 345 return NULL; 346} 347 348/** 349 * rb_find() - find @key in tree @tree 350 * @key: key to match 351 * @tree: tree to search 352 * @cmp: operator defining the node order 353 * 354 * Returns the rb_node matching @key or NULL. 355 */ 356static __always_inline struct rb_node * 357rb_find(const void *key, const struct rb_root *tree, 358 int (*cmp)(const void *key, const struct rb_node *)) 359{ 360 struct rb_node *node = tree->rb_node; 361 362 while (node) { 363 int c = cmp(key, node); 364 365 if (c < 0) 366 node = node->rb_left; 367 else if (c > 0) 368 node = node->rb_right; 369 else 370 return node; 371 } 372 373 return NULL; 374} 375 376/** 377 * rb_find_rcu() - find @key in tree @tree 378 * @key: key to match 379 * @tree: tree to search 380 * @cmp: operator defining the node order 381 * 382 * Notably, tree descent vs concurrent tree rotations is unsound and can result 383 * in false-negatives. 384 * 385 * Returns the rb_node matching @key or NULL. 386 */ 387static __always_inline struct rb_node * 388rb_find_rcu(const void *key, const struct rb_root *tree, 389 int (*cmp)(const void *key, const struct rb_node *)) 390{ 391 struct rb_node *node = tree->rb_node; 392 393 while (node) { 394 int c = cmp(key, node); 395 396 if (c < 0) 397 node = rcu_dereference_raw(node->rb_left); 398 else if (c > 0) 399 node = rcu_dereference_raw(node->rb_right); 400 else 401 return node; 402 } 403 404 return NULL; 405} 406 407/** 408 * rb_find_first() - find the first @key in @tree 409 * @key: key to match 410 * @tree: tree to search 411 * @cmp: operator defining node order 412 * 413 * Returns the leftmost node matching @key, or NULL. 414 */ 415static __always_inline struct rb_node * 416rb_find_first(const void *key, const struct rb_root *tree, 417 int (*cmp)(const void *key, const struct rb_node *)) 418{ 419 struct rb_node *node = tree->rb_node; 420 struct rb_node *match = NULL; 421 422 while (node) { 423 int c = cmp(key, node); 424 425 if (c <= 0) { 426 if (!c) 427 match = node; 428 node = node->rb_left; 429 } else if (c > 0) { 430 node = node->rb_right; 431 } 432 } 433 434 return match; 435} 436 437/** 438 * rb_next_match() - find the next @key in @tree 439 * @key: key to match 440 * @tree: tree to search 441 * @cmp: operator defining node order 442 * 443 * Returns the next node matching @key, or NULL. 444 */ 445static __always_inline struct rb_node * 446rb_next_match(const void *key, struct rb_node *node, 447 int (*cmp)(const void *key, const struct rb_node *)) 448{ 449 node = rb_next(node); 450 if (node && cmp(key, node)) 451 node = NULL; 452 return node; 453} 454 455/** 456 * rb_for_each() - iterates a subtree matching @key 457 * @node: iterator 458 * @key: key to match 459 * @tree: tree to search 460 * @cmp: operator defining node order 461 */ 462#define rb_for_each(node, key, tree, cmp) \ 463 for ((node) = rb_find_first((key), (tree), (cmp)); \ 464 (node); (node) = rb_next_match((key), (node), (cmp))) 465 466#endif /* _LINUX_RBTREE_H */