rbtree.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /*
  2. Red Black Trees
  3. (C) 1999 Andrea Arcangeli <andrea@suse.de>
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  15. linux/include/linux/rbtree.h
  16. To use rbtrees you'll have to implement your own insert and search cores.
  17. This will avoid us to use callbacks and to drop drammatically performances.
  18. I know it's not the cleaner way, but in C (not in C++) to get
  19. performances and genericity...
  20. See Documentation/rbtree.txt for documentation and samples.
  21. */
  22. #ifndef _LINUX_RBTREE_H
  23. #define _LINUX_RBTREE_H
  24. #include <linux/kernel.h>
  25. #include <linux/stddef.h>
  26. #include <linux/rcupdate.h>
  27. struct rb_node {
  28. unsigned long __rb_parent_color;
  29. struct rb_node *rb_right;
  30. struct rb_node *rb_left;
  31. } __attribute__((aligned(sizeof(long))));
  32. /* The alignment might seem pointless, but allegedly CRIS needs it */
  33. struct rb_root {
  34. struct rb_node *rb_node;
  35. };
  36. #define rb_parent(r) ((struct rb_node *)((r)->__rb_parent_color & ~3))
  37. #define RB_ROOT (struct rb_root) { NULL, }
  38. #define rb_entry(ptr, type, member) container_of(ptr, type, member)
  39. #define RB_EMPTY_ROOT(root) (READ_ONCE((root)->rb_node) == NULL)
  40. /* 'empty' nodes are nodes that are known not to be inserted in an rbtree */
  41. #define RB_EMPTY_NODE(node) \
  42. ((node)->__rb_parent_color == (unsigned long)(node))
  43. #define RB_CLEAR_NODE(node) \
  44. ((node)->__rb_parent_color = (unsigned long)(node))
  45. extern void rb_insert_color(struct rb_node *, struct rb_root *);
  46. extern void rb_erase(struct rb_node *, struct rb_root *);
  47. /* Find logical next and previous nodes in a tree */
  48. extern struct rb_node *rb_next(const struct rb_node *);
  49. extern struct rb_node *rb_prev(const struct rb_node *);
  50. extern struct rb_node *rb_first(const struct rb_root *);
  51. extern struct rb_node *rb_last(const struct rb_root *);
  52. /* Postorder iteration - always visit the parent after its children */
  53. extern struct rb_node *rb_first_postorder(const struct rb_root *);
  54. extern struct rb_node *rb_next_postorder(const struct rb_node *);
  55. /* Fast replacement of a single node without remove/rebalance/add/rebalance */
  56. extern void rb_replace_node(struct rb_node *victim, struct rb_node *new,
  57. struct rb_root *root);
  58. extern void rb_replace_node_rcu(struct rb_node *victim, struct rb_node *new,
  59. struct rb_root *root);
  60. static inline void rb_link_node(struct rb_node *node, struct rb_node *parent,
  61. struct rb_node **rb_link)
  62. {
  63. node->__rb_parent_color = (unsigned long)parent;
  64. node->rb_left = node->rb_right = NULL;
  65. *rb_link = node;
  66. }
  67. static inline void rb_link_node_rcu(struct rb_node *node, struct rb_node *parent,
  68. struct rb_node **rb_link)
  69. {
  70. node->__rb_parent_color = (unsigned long)parent;
  71. node->rb_left = node->rb_right = NULL;
  72. rcu_assign_pointer(*rb_link, node);
  73. }
  74. #define rb_entry_safe(ptr, type, member) \
  75. ({ typeof(ptr) ____ptr = (ptr); \
  76. ____ptr ? rb_entry(____ptr, type, member) : NULL; \
  77. })
  78. /**
  79. * rbtree_postorder_for_each_entry_safe - iterate in post-order over rb_root of
  80. * given type allowing the backing memory of @pos to be invalidated
  81. *
  82. * @pos: the 'type *' to use as a loop cursor.
  83. * @n: another 'type *' to use as temporary storage
  84. * @root: 'rb_root *' of the rbtree.
  85. * @field: the name of the rb_node field within 'type'.
  86. *
  87. * rbtree_postorder_for_each_entry_safe() provides a similar guarantee as
  88. * list_for_each_entry_safe() and allows the iteration to continue independent
  89. * of changes to @pos by the body of the loop.
  90. *
  91. * Note, however, that it cannot handle other modifications that re-order the
  92. * rbtree it is iterating over. This includes calling rb_erase() on @pos, as
  93. * rb_erase() may rebalance the tree, causing us to miss some nodes.
  94. */
  95. #define rbtree_postorder_for_each_entry_safe(pos, n, root, field) \
  96. for (pos = rb_entry_safe(rb_first_postorder(root), typeof(*pos), field); \
  97. pos && ({ n = rb_entry_safe(rb_next_postorder(&pos->field), \
  98. typeof(*pos), field); 1; }); \
  99. pos = n)
  100. #endif /* _LINUX_RBTREE_H */