aboutsummaryrefslogtreecommitdiffstats
path: root/include/linux/rbtree.h
diff options
context:
space:
mode:
Diffstat (limited to 'include/linux/rbtree.h')
-rw-r--r--include/linux/rbtree.h70
1 files changed, 46 insertions, 24 deletions
diff --git a/include/linux/rbtree.h b/include/linux/rbtree.h
index e6337fce08f2..1fd61a9af45c 100644
--- a/include/linux/rbtree.h
+++ b/include/linux/rbtree.h
@@ -32,25 +32,9 @@ struct rb_root {
32 struct rb_node *rb_node; 32 struct rb_node *rb_node;
33}; 33};
34 34
35/*
36 * Leftmost-cached rbtrees.
37 *
38 * We do not cache the rightmost node based on footprint
39 * size vs number of potential users that could benefit
40 * from O(1) rb_last(). Just not worth it, users that want
41 * this feature can always implement the logic explicitly.
42 * Furthermore, users that want to cache both pointers may
43 * find it a bit asymmetric, but that's ok.
44 */
45struct rb_root_cached {
46 struct rb_root rb_root;
47 struct rb_node *rb_leftmost;
48};
49
50#define rb_parent(r) ((struct rb_node *)((r)->__rb_parent_color & ~3)) 35#define rb_parent(r) ((struct rb_node *)((r)->__rb_parent_color & ~3))
51 36
52#define RB_ROOT (struct rb_root) { NULL, } 37#define RB_ROOT (struct rb_root) { NULL, }
53#define RB_ROOT_CACHED (struct rb_root_cached) { {NULL, }, NULL }
54#define rb_entry(ptr, type, member) container_of(ptr, type, member) 38#define rb_entry(ptr, type, member) container_of(ptr, type, member)
55 39
56#define RB_EMPTY_ROOT(root) (READ_ONCE((root)->rb_node) == NULL) 40#define RB_EMPTY_ROOT(root) (READ_ONCE((root)->rb_node) == NULL)
@@ -72,12 +56,6 @@ extern struct rb_node *rb_prev(const struct rb_node *);
72extern struct rb_node *rb_first(const struct rb_root *); 56extern struct rb_node *rb_first(const struct rb_root *);
73extern struct rb_node *rb_last(const struct rb_root *); 57extern struct rb_node *rb_last(const struct rb_root *);
74 58
75extern void rb_insert_color_cached(struct rb_node *,
76 struct rb_root_cached *, bool);
77extern void rb_erase_cached(struct rb_node *node, struct rb_root_cached *);
78/* Same as rb_first(), but O(1) */
79#define rb_first_cached(root) (root)->rb_leftmost
80
81/* Postorder iteration - always visit the parent after its children */ 59/* Postorder iteration - always visit the parent after its children */
82extern struct rb_node *rb_first_postorder(const struct rb_root *); 60extern struct rb_node *rb_first_postorder(const struct rb_root *);
83extern struct rb_node *rb_next_postorder(const struct rb_node *); 61extern struct rb_node *rb_next_postorder(const struct rb_node *);
@@ -87,8 +65,6 @@ extern void rb_replace_node(struct rb_node *victim, struct rb_node *new,
87 struct rb_root *root); 65 struct rb_root *root);
88extern void rb_replace_node_rcu(struct rb_node *victim, struct rb_node *new, 66extern void rb_replace_node_rcu(struct rb_node *victim, struct rb_node *new,
89 struct rb_root *root); 67 struct rb_root *root);
90extern void rb_replace_node_cached(struct rb_node *victim, struct rb_node *new,
91 struct rb_root_cached *root);
92 68
93static inline void rb_link_node(struct rb_node *node, struct rb_node *parent, 69static inline void rb_link_node(struct rb_node *node, struct rb_node *parent,
94 struct rb_node **rb_link) 70 struct rb_node **rb_link)
@@ -136,4 +112,50 @@ static inline void rb_link_node_rcu(struct rb_node *node, struct rb_node *parent
136 typeof(*pos), field); 1; }); \ 112 typeof(*pos), field); 1; }); \
137 pos = n) 113 pos = n)
138 114
115/*
116 * Leftmost-cached rbtrees.
117 *
118 * We do not cache the rightmost node based on footprint
119 * size vs number of potential users that could benefit
120 * from O(1) rb_last(). Just not worth it, users that want
121 * this feature can always implement the logic explicitly.
122 * Furthermore, users that want to cache both pointers may
123 * find it a bit asymmetric, but that's ok.
124 */
125struct rb_root_cached {
126 struct rb_root rb_root;
127 struct rb_node *rb_leftmost;
128};
129
130#define RB_ROOT_CACHED (struct rb_root_cached) { {NULL, }, NULL }
131
132/* Same as rb_first(), but O(1) */
133#define rb_first_cached(root) (root)->rb_leftmost
134
135static inline void rb_insert_color_cached(struct rb_node *node,
136 struct rb_root_cached *root,
137 bool leftmost)
138{
139 if (leftmost)
140 root->rb_leftmost = node;
141 rb_insert_color(node, &root->rb_root);
142}
143
144static inline void rb_erase_cached(struct rb_node *node,
145 struct rb_root_cached *root)
146{
147 if (root->rb_leftmost == node)
148 root->rb_leftmost = rb_next(node);
149 rb_erase(node, &root->rb_root);
150}
151
152static inline void rb_replace_node_cached(struct rb_node *victim,
153 struct rb_node *new,
154 struct rb_root_cached *root)
155{
156 if (root->rb_leftmost == victim)
157 root->rb_leftmost = new;
158 rb_replace_node(victim, new, &root->rb_root);
159}
160
139#endif /* _LINUX_RBTREE_H */ 161#endif /* _LINUX_RBTREE_H */