rbtree.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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 __TOOLS_LINUX_PERF_RBTREE_H
  23. #define __TOOLS_LINUX_PERF_RBTREE_H
  24. #include <linux/kernel.h>
  25. #include <linux/stddef.h>
  26. struct rb_node {
  27. unsigned long __rb_parent_color;
  28. struct rb_node *rb_right;
  29. struct rb_node *rb_left;
  30. } __attribute__((aligned(sizeof(long))));
  31. /* The alignment might seem pointless, but allegedly CRIS needs it */
  32. struct rb_root {
  33. struct rb_node *rb_node;
  34. };
  35. #define rb_parent(r) ((struct rb_node *)((r)->__rb_parent_color & ~3))
  36. #define RB_ROOT (struct rb_root) { NULL, }
  37. #define rb_entry(ptr, type, member) container_of(ptr, type, member)
  38. #define RB_EMPTY_ROOT(root) ((root)->rb_node == NULL)
  39. /* 'empty' nodes are nodes that are known not to be inserted in an rbtree */
  40. #define RB_EMPTY_NODE(node) \
  41. ((node)->__rb_parent_color == (unsigned long)(node))
  42. #define RB_CLEAR_NODE(node) \
  43. ((node)->__rb_parent_color = (unsigned long)(node))
  44. extern void rb_insert_color(struct rb_node *, struct rb_root *);
  45. extern void rb_erase(struct rb_node *, struct rb_root *);
  46. /* Find logical next and previous nodes in a tree */
  47. extern struct rb_node *rb_next(const struct rb_node *);
  48. extern struct rb_node *rb_prev(const struct rb_node *);
  49. extern struct rb_node *rb_first(const struct rb_root *);
  50. extern struct rb_node *rb_last(const struct rb_root *);
  51. /* Postorder iteration - always visit the parent after its children */
  52. extern struct rb_node *rb_first_postorder(const struct rb_root *);
  53. extern struct rb_node *rb_next_postorder(const struct rb_node *);
  54. /* Fast replacement of a single node without remove/rebalance/add/rebalance */
  55. extern void rb_replace_node(struct rb_node *victim, struct rb_node *new,
  56. struct rb_root *root);
  57. static inline void rb_link_node(struct rb_node *node, struct rb_node *parent,
  58. struct rb_node **rb_link)
  59. {
  60. node->__rb_parent_color = (unsigned long)parent;
  61. node->rb_left = node->rb_right = NULL;
  62. *rb_link = node;
  63. }
  64. #define rb_entry_safe(ptr, type, member) \
  65. ({ typeof(ptr) ____ptr = (ptr); \
  66. ____ptr ? rb_entry(____ptr, type, member) : NULL; \
  67. })
  68. /*
  69. * Handy for checking that we are not deleting an entry that is
  70. * already in a list, found in block/{blk-throttle,cfq-iosched}.c,
  71. * probably should be moved to lib/rbtree.c...
  72. */
  73. static inline void rb_erase_init(struct rb_node *n, struct rb_root *root)
  74. {
  75. rb_erase(n, root);
  76. RB_CLEAR_NODE(n);
  77. }
  78. #endif /* __TOOLS_LINUX_PERF_RBTREE_H */