rbtree.h 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. /* SPDX-License-Identifier: GPL-2.0-or-later */
  2. /*
  3. Red Black Trees
  4. (C) 1999 Andrea Arcangeli <[email protected]>
  5. linux/include/linux/rbtree.h
  6. To use rbtrees you'll have to implement your own insert and search cores.
  7. This will avoid us to use callbacks and to drop drammatically performances.
  8. I know it's not the cleaner way, but in C (not in C++) to get
  9. performances and genericity...
  10. See Documentation/core-api/rbtree.rst for documentation and samples.
  11. */
  12. #ifndef _LINUX_RBTREE_H
  13. #define _LINUX_RBTREE_H
  14. #include <linux/container_of.h>
  15. #include <linux/rbtree_types.h>
  16. #include <linux/stddef.h>
  17. #include <linux/rcupdate.h>
  18. #define rb_parent(r) ((struct rb_node *)((r)->__rb_parent_color & ~3))
  19. #define rb_entry(ptr, type, member) container_of(ptr, type, member)
  20. #define RB_EMPTY_ROOT(root) (READ_ONCE((root)->rb_node) == NULL)
  21. /* 'empty' nodes are nodes that are known not to be inserted in an rbtree */
  22. #define RB_EMPTY_NODE(node) \
  23. ((node)->__rb_parent_color == (unsigned long)(node))
  24. #define RB_CLEAR_NODE(node) \
  25. ((node)->__rb_parent_color = (unsigned long)(node))
  26. extern void rb_insert_color(struct rb_node *, struct rb_root *);
  27. extern void rb_erase(struct rb_node *, struct rb_root *);
  28. /* Find logical next and previous nodes in a tree */
  29. extern struct rb_node *rb_next(const struct rb_node *);
  30. extern struct rb_node *rb_prev(const struct rb_node *);
  31. extern struct rb_node *rb_first(const struct rb_root *);
  32. extern struct rb_node *rb_last(const struct rb_root *);
  33. /* Postorder iteration - always visit the parent after its children */
  34. extern struct rb_node *rb_first_postorder(const struct rb_root *);
  35. extern struct rb_node *rb_next_postorder(const struct rb_node *);
  36. /* Fast replacement of a single node without remove/rebalance/add/rebalance */
  37. extern void rb_replace_node(struct rb_node *victim, struct rb_node *new,
  38. struct rb_root *root);
  39. extern void rb_replace_node_rcu(struct rb_node *victim, struct rb_node *new,
  40. struct rb_root *root);
  41. static inline void rb_link_node(struct rb_node *node, struct rb_node *parent,
  42. struct rb_node **rb_link)
  43. {
  44. node->__rb_parent_color = (unsigned long)parent;
  45. node->rb_left = node->rb_right = NULL;
  46. *rb_link = node;
  47. }
  48. static inline void rb_link_node_rcu(struct rb_node *node, struct rb_node *parent,
  49. struct rb_node **rb_link)
  50. {
  51. node->__rb_parent_color = (unsigned long)parent;
  52. node->rb_left = node->rb_right = NULL;
  53. rcu_assign_pointer(*rb_link, node);
  54. }
  55. #define rb_entry_safe(ptr, type, member) \
  56. ({ typeof(ptr) ____ptr = (ptr); \
  57. ____ptr ? rb_entry(____ptr, type, member) : NULL; \
  58. })
  59. /**
  60. * rbtree_postorder_for_each_entry_safe - iterate in post-order over rb_root of
  61. * given type allowing the backing memory of @pos to be invalidated
  62. *
  63. * @pos: the 'type *' to use as a loop cursor.
  64. * @n: another 'type *' to use as temporary storage
  65. * @root: 'rb_root *' of the rbtree.
  66. * @field: the name of the rb_node field within 'type'.
  67. *
  68. * rbtree_postorder_for_each_entry_safe() provides a similar guarantee as
  69. * list_for_each_entry_safe() and allows the iteration to continue independent
  70. * of changes to @pos by the body of the loop.
  71. *
  72. * Note, however, that it cannot handle other modifications that re-order the
  73. * rbtree it is iterating over. This includes calling rb_erase() on @pos, as
  74. * rb_erase() may rebalance the tree, causing us to miss some nodes.
  75. */
  76. #define rbtree_postorder_for_each_entry_safe(pos, n, root, field) \
  77. for (pos = rb_entry_safe(rb_first_postorder(root), typeof(*pos), field); \
  78. pos && ({ n = rb_entry_safe(rb_next_postorder(&pos->field), \
  79. typeof(*pos), field); 1; }); \
  80. pos = n)
  81. /* Same as rb_first(), but O(1) */
  82. #define rb_first_cached(root) (root)->rb_leftmost
  83. static inline void rb_insert_color_cached(struct rb_node *node,
  84. struct rb_root_cached *root,
  85. bool leftmost)
  86. {
  87. if (leftmost)
  88. root->rb_leftmost = node;
  89. rb_insert_color(node, &root->rb_root);
  90. }
  91. static inline struct rb_node *
  92. rb_erase_cached(struct rb_node *node, struct rb_root_cached *root)
  93. {
  94. struct rb_node *leftmost = NULL;
  95. if (root->rb_leftmost == node)
  96. leftmost = root->rb_leftmost = rb_next(node);
  97. rb_erase(node, &root->rb_root);
  98. return leftmost;
  99. }
  100. static inline void rb_replace_node_cached(struct rb_node *victim,
  101. struct rb_node *new,
  102. struct rb_root_cached *root)
  103. {
  104. if (root->rb_leftmost == victim)
  105. root->rb_leftmost = new;
  106. rb_replace_node(victim, new, &root->rb_root);
  107. }
  108. /*
  109. * The below helper functions use 2 operators with 3 different
  110. * calling conventions. The operators are related like:
  111. *
  112. * comp(a->key,b) < 0 := less(a,b)
  113. * comp(a->key,b) > 0 := less(b,a)
  114. * comp(a->key,b) == 0 := !less(a,b) && !less(b,a)
  115. *
  116. * If these operators define a partial order on the elements we make no
  117. * guarantee on which of the elements matching the key is found. See
  118. * rb_find().
  119. *
  120. * The reason for this is to allow the find() interface without requiring an
  121. * on-stack dummy object, which might not be feasible due to object size.
  122. */
  123. /**
  124. * rb_add_cached() - insert @node into the leftmost cached tree @tree
  125. * @node: node to insert
  126. * @tree: leftmost cached tree to insert @node into
  127. * @less: operator defining the (partial) node order
  128. *
  129. * Returns @node when it is the new leftmost, or NULL.
  130. */
  131. static __always_inline struct rb_node *
  132. rb_add_cached(struct rb_node *node, struct rb_root_cached *tree,
  133. bool (*less)(struct rb_node *, const struct rb_node *))
  134. {
  135. struct rb_node **link = &tree->rb_root.rb_node;
  136. struct rb_node *parent = NULL;
  137. bool leftmost = true;
  138. while (*link) {
  139. parent = *link;
  140. if (less(node, parent)) {
  141. link = &parent->rb_left;
  142. } else {
  143. link = &parent->rb_right;
  144. leftmost = false;
  145. }
  146. }
  147. rb_link_node(node, parent, link);
  148. rb_insert_color_cached(node, tree, leftmost);
  149. return leftmost ? node : NULL;
  150. }
  151. /**
  152. * rb_add() - insert @node into @tree
  153. * @node: node to insert
  154. * @tree: tree to insert @node into
  155. * @less: operator defining the (partial) node order
  156. */
  157. static __always_inline void
  158. rb_add(struct rb_node *node, struct rb_root *tree,
  159. bool (*less)(struct rb_node *, const struct rb_node *))
  160. {
  161. struct rb_node **link = &tree->rb_node;
  162. struct rb_node *parent = NULL;
  163. while (*link) {
  164. parent = *link;
  165. if (less(node, parent))
  166. link = &parent->rb_left;
  167. else
  168. link = &parent->rb_right;
  169. }
  170. rb_link_node(node, parent, link);
  171. rb_insert_color(node, tree);
  172. }
  173. /**
  174. * rb_find_add() - find equivalent @node in @tree, or add @node
  175. * @node: node to look-for / insert
  176. * @tree: tree to search / modify
  177. * @cmp: operator defining the node order
  178. *
  179. * Returns the rb_node matching @node, or NULL when no match is found and @node
  180. * is inserted.
  181. */
  182. static __always_inline struct rb_node *
  183. rb_find_add(struct rb_node *node, struct rb_root *tree,
  184. int (*cmp)(struct rb_node *, const struct rb_node *))
  185. {
  186. struct rb_node **link = &tree->rb_node;
  187. struct rb_node *parent = NULL;
  188. int c;
  189. while (*link) {
  190. parent = *link;
  191. c = cmp(node, parent);
  192. if (c < 0)
  193. link = &parent->rb_left;
  194. else if (c > 0)
  195. link = &parent->rb_right;
  196. else
  197. return parent;
  198. }
  199. rb_link_node(node, parent, link);
  200. rb_insert_color(node, tree);
  201. return NULL;
  202. }
  203. /**
  204. * rb_find() - find @key in tree @tree
  205. * @key: key to match
  206. * @tree: tree to search
  207. * @cmp: operator defining the node order
  208. *
  209. * Returns the rb_node matching @key or NULL.
  210. */
  211. static __always_inline struct rb_node *
  212. rb_find(const void *key, const struct rb_root *tree,
  213. int (*cmp)(const void *key, const struct rb_node *))
  214. {
  215. struct rb_node *node = tree->rb_node;
  216. while (node) {
  217. int c = cmp(key, node);
  218. if (c < 0)
  219. node = node->rb_left;
  220. else if (c > 0)
  221. node = node->rb_right;
  222. else
  223. return node;
  224. }
  225. return NULL;
  226. }
  227. /**
  228. * rb_find_first() - find the first @key in @tree
  229. * @key: key to match
  230. * @tree: tree to search
  231. * @cmp: operator defining node order
  232. *
  233. * Returns the leftmost node matching @key, or NULL.
  234. */
  235. static __always_inline struct rb_node *
  236. rb_find_first(const void *key, const struct rb_root *tree,
  237. int (*cmp)(const void *key, const struct rb_node *))
  238. {
  239. struct rb_node *node = tree->rb_node;
  240. struct rb_node *match = NULL;
  241. while (node) {
  242. int c = cmp(key, node);
  243. if (c <= 0) {
  244. if (!c)
  245. match = node;
  246. node = node->rb_left;
  247. } else if (c > 0) {
  248. node = node->rb_right;
  249. }
  250. }
  251. return match;
  252. }
  253. /**
  254. * rb_next_match() - find the next @key in @tree
  255. * @key: key to match
  256. * @tree: tree to search
  257. * @cmp: operator defining node order
  258. *
  259. * Returns the next node matching @key, or NULL.
  260. */
  261. static __always_inline struct rb_node *
  262. rb_next_match(const void *key, struct rb_node *node,
  263. int (*cmp)(const void *key, const struct rb_node *))
  264. {
  265. node = rb_next(node);
  266. if (node && cmp(key, node))
  267. node = NULL;
  268. return node;
  269. }
  270. /**
  271. * rb_for_each() - iterates a subtree matching @key
  272. * @node: iterator
  273. * @key: key to match
  274. * @tree: tree to search
  275. * @cmp: operator defining node order
  276. */
  277. #define rb_for_each(node, key, tree, cmp) \
  278. for ((node) = rb_find_first((key), (tree), (cmp)); \
  279. (node); (node) = rb_next_match((key), (node), (cmp)))
  280. #endif /* _LINUX_RBTREE_H */