1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24#ifndef _LINUX_RBTREE_AUGMENTED_H
25#define _LINUX_RBTREE_AUGMENTED_H
26
27#include <linux/compiler.h>
28#include <linux/rbtree.h>
29
30
31
32
33
34
35
36
37
38struct rb_augment_callbacks {
39 void (*propagate)(struct rb_node *node, struct rb_node *stop);
40 void (*copy)(struct rb_node *old, struct rb_node *new);
41 void (*rotate)(struct rb_node *old, struct rb_node *new);
42};
43
44extern void __rb_insert_augmented(struct rb_node *node, struct rb_root *root,
45 void (*augment_rotate)(struct rb_node *old, struct rb_node *new));
46
47
48
49
50
51
52
53
54
55
56static inline void
57rb_insert_augmented(struct rb_node *node, struct rb_root *root,
58 const struct rb_augment_callbacks *augment)
59{
60 __rb_insert_augmented(node, root, augment->rotate);
61}
62
63#define RB_DECLARE_CALLBACKS(rbstatic, rbname, rbstruct, rbfield, \
64 rbtype, rbaugmented, rbcompute) \
65static inline void \
66rbname ## _propagate(struct rb_node *rb, struct rb_node *stop) \
67{ \
68 while (rb != stop) { \
69 rbstruct *node = rb_entry(rb, rbstruct, rbfield); \
70 rbtype augmented = rbcompute(node); \
71 if (node->rbaugmented == augmented) \
72 break; \
73 node->rbaugmented = augmented; \
74 rb = rb_parent(&node->rbfield); \
75 } \
76} \
77static inline void \
78rbname ## _copy(struct rb_node *rb_old, struct rb_node *rb_new) \
79{ \
80 rbstruct *old = rb_entry(rb_old, rbstruct, rbfield); \
81 rbstruct *new = rb_entry(rb_new, rbstruct, rbfield); \
82 new->rbaugmented = old->rbaugmented; \
83} \
84static void \
85rbname ## _rotate(struct rb_node *rb_old, struct rb_node *rb_new) \
86{ \
87 rbstruct *old = rb_entry(rb_old, rbstruct, rbfield); \
88 rbstruct *new = rb_entry(rb_new, rbstruct, rbfield); \
89 new->rbaugmented = old->rbaugmented; \
90 old->rbaugmented = rbcompute(old); \
91} \
92rbstatic const struct rb_augment_callbacks rbname = { \
93 rbname ## _propagate, rbname ## _copy, rbname ## _rotate \
94};
95
96
97#define RB_RED 0
98#define RB_BLACK 1
99
100#define __rb_parent(pc) ((struct rb_node *)(pc & ~3))
101
102#define __rb_color(pc) ((pc) & 1)
103#define __rb_is_black(pc) __rb_color(pc)
104#define __rb_is_red(pc) (!__rb_color(pc))
105#define rb_color(rb) __rb_color((rb)->__rb_parent_color)
106#define rb_is_red(rb) __rb_is_red((rb)->__rb_parent_color)
107#define rb_is_black(rb) __rb_is_black((rb)->__rb_parent_color)
108
109static inline void rb_set_parent(struct rb_node *rb, struct rb_node *p)
110{
111 rb->__rb_parent_color = rb_color(rb) | (unsigned long)p;
112}
113
114static inline void rb_set_parent_color(struct rb_node *rb,
115 struct rb_node *p, int color)
116{
117 rb->__rb_parent_color = (unsigned long)p | color;
118}
119
120static inline void
121__rb_change_child(struct rb_node *old, struct rb_node *new,
122 struct rb_node *parent, struct rb_root *root)
123{
124 if (parent) {
125 if (parent->rb_left == old)
126 WRITE_ONCE(parent->rb_left, new);
127 else
128 WRITE_ONCE(parent->rb_right, new);
129 } else
130 WRITE_ONCE(root->rb_node, new);
131}
132
133static inline void
134__rb_change_child_rcu(struct rb_node *old, struct rb_node *new,
135 struct rb_node *parent, struct rb_root *root)
136{
137 if (parent) {
138 if (parent->rb_left == old)
139 rcu_assign_pointer(parent->rb_left, new);
140 else
141 rcu_assign_pointer(parent->rb_right, new);
142 } else
143 rcu_assign_pointer(root->rb_node, new);
144}
145
146extern void __rb_erase_color(struct rb_node *parent, struct rb_root *root,
147 void (*augment_rotate)(struct rb_node *old, struct rb_node *new));
148
149static __always_inline struct rb_node *
150__rb_erase_augmented(struct rb_node *node, struct rb_root *root,
151 const struct rb_augment_callbacks *augment)
152{
153 struct rb_node *child = node->rb_right;
154 struct rb_node *tmp = node->rb_left;
155 struct rb_node *parent, *rebalance;
156 unsigned long pc;
157
158 if (!tmp) {
159
160
161
162
163
164
165
166 pc = node->__rb_parent_color;
167 parent = __rb_parent(pc);
168 __rb_change_child(node, child, parent, root);
169 if (child) {
170 child->__rb_parent_color = pc;
171 rebalance = NULL;
172 } else
173 rebalance = __rb_is_black(pc) ? parent : NULL;
174 tmp = parent;
175 } else if (!child) {
176
177 tmp->__rb_parent_color = pc = node->__rb_parent_color;
178 parent = __rb_parent(pc);
179 __rb_change_child(node, tmp, parent, root);
180 rebalance = NULL;
181 tmp = parent;
182 } else {
183 struct rb_node *successor = child, *child2;
184
185 tmp = child->rb_left;
186 if (!tmp) {
187
188
189
190
191
192
193
194
195
196 parent = successor;
197 child2 = successor->rb_right;
198
199 augment->copy(node, successor);
200 } else {
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215 do {
216 parent = successor;
217 successor = tmp;
218 tmp = tmp->rb_left;
219 } while (tmp);
220 child2 = successor->rb_right;
221 WRITE_ONCE(parent->rb_left, child2);
222 WRITE_ONCE(successor->rb_right, child);
223 rb_set_parent(child, successor);
224
225 augment->copy(node, successor);
226 augment->propagate(parent, successor);
227 }
228
229 tmp = node->rb_left;
230 WRITE_ONCE(successor->rb_left, tmp);
231 rb_set_parent(tmp, successor);
232
233 pc = node->__rb_parent_color;
234 tmp = __rb_parent(pc);
235 __rb_change_child(node, successor, tmp, root);
236
237 if (child2) {
238 successor->__rb_parent_color = pc;
239 rb_set_parent_color(child2, parent, RB_BLACK);
240 rebalance = NULL;
241 } else {
242 unsigned long pc2 = successor->__rb_parent_color;
243 successor->__rb_parent_color = pc;
244 rebalance = __rb_is_black(pc2) ? parent : NULL;
245 }
246 tmp = successor;
247 }
248
249 augment->propagate(tmp, NULL);
250 return rebalance;
251}
252
253static __always_inline void
254rb_erase_augmented(struct rb_node *node, struct rb_root *root,
255 const struct rb_augment_callbacks *augment)
256{
257 struct rb_node *rebalance = __rb_erase_augmented(node, root, augment);
258 if (rebalance)
259 __rb_erase_color(rebalance, root, augment->rotate);
260}
261
262#endif
263