@@ -44,7 +44,9 @@ struct rb_augment_callbacks {
4444 void (* rotate )(struct rb_node * old , struct rb_node * new );
4545};
4646
47- extern void __rb_insert_augmented (struct rb_node * node , struct rb_root * root ,
47+ extern void __rb_insert_augmented (struct rb_node * node ,
48+ struct rb_root * root ,
49+ bool newleft , struct rb_node * * leftmost ,
4850 void (* augment_rotate )(struct rb_node * old , struct rb_node * new ));
4951/*
5052 * Fixup the rbtree and update the augmented information when rebalancing.
@@ -60,7 +62,16 @@ static inline void
6062rb_insert_augmented (struct rb_node * node , struct rb_root * root ,
6163 const struct rb_augment_callbacks * augment )
6264{
63- __rb_insert_augmented (node , root , augment -> rotate );
65+ __rb_insert_augmented (node , root , false, NULL , augment -> rotate );
66+ }
67+
68+ static inline void
69+ rb_insert_augmented_cached (struct rb_node * node ,
70+ struct rb_root_cached * root , bool newleft ,
71+ const struct rb_augment_callbacks * augment )
72+ {
73+ __rb_insert_augmented (node , & root -> rb_root ,
74+ newleft , & root -> rb_leftmost , augment -> rotate );
6475}
6576
6677#define RB_DECLARE_CALLBACKS (rbstatic , rbname , rbstruct , rbfield , \
@@ -93,7 +104,9 @@ rbname ## _rotate(struct rb_node *rb_old, struct rb_node *rb_new) \
93104 old->rbaugmented = rbcompute(old); \
94105} \
95106rbstatic const struct rb_augment_callbacks rbname = { \
96- rbname ## _propagate, rbname ## _copy, rbname ## _rotate \
107+ .propagate = rbname ## _propagate, \
108+ .copy = rbname ## _copy, \
109+ .rotate = rbname ## _rotate \
97110};
98111
99112
@@ -126,24 +139,29 @@ __rb_change_child(struct rb_node *old, struct rb_node *new,
126139{
127140 if (parent ) {
128141 if (parent -> rb_left == old )
129- parent -> rb_left = new ;
142+ WRITE_ONCE ( parent -> rb_left , new ) ;
130143 else
131- parent -> rb_right = new ;
144+ WRITE_ONCE ( parent -> rb_right , new ) ;
132145 } else
133- root -> rb_node = new ;
146+ WRITE_ONCE ( root -> rb_node , new ) ;
134147}
135148
136149extern void __rb_erase_color (struct rb_node * parent , struct rb_root * root ,
137150 void (* augment_rotate )(struct rb_node * old , struct rb_node * new ));
138151
139152static __always_inline struct rb_node *
140153__rb_erase_augmented (struct rb_node * node , struct rb_root * root ,
154+ struct rb_node * * leftmost ,
141155 const struct rb_augment_callbacks * augment )
142156{
143- struct rb_node * child = node -> rb_right , * tmp = node -> rb_left ;
157+ struct rb_node * child = node -> rb_right ;
158+ struct rb_node * tmp = node -> rb_left ;
144159 struct rb_node * parent , * rebalance ;
145160 unsigned long pc ;
146161
162+ if (leftmost && node == * leftmost )
163+ * leftmost = rb_next (node );
164+
147165 if (!tmp ) {
148166 /*
149167 * Case 1: node to erase has no more than 1 child (easy!)
@@ -170,6 +188,7 @@ __rb_erase_augmented(struct rb_node *node, struct rb_root *root,
170188 tmp = parent ;
171189 } else {
172190 struct rb_node * successor = child , * child2 ;
191+
173192 tmp = child -> rb_left ;
174193 if (!tmp ) {
175194 /*
@@ -183,6 +202,7 @@ __rb_erase_augmented(struct rb_node *node, struct rb_root *root,
183202 */
184203 parent = successor ;
185204 child2 = successor -> rb_right ;
205+
186206 augment -> copy (node , successor );
187207 } else {
188208 /*
@@ -204,19 +224,23 @@ __rb_erase_augmented(struct rb_node *node, struct rb_root *root,
204224 successor = tmp ;
205225 tmp = tmp -> rb_left ;
206226 } while (tmp );
207- parent -> rb_left = child2 = successor -> rb_right ;
208- successor -> rb_right = child ;
227+ child2 = successor -> rb_right ;
228+ WRITE_ONCE (parent -> rb_left , child2 );
229+ WRITE_ONCE (successor -> rb_right , child );
209230 rb_set_parent (child , successor );
231+
210232 augment -> copy (node , successor );
211233 augment -> propagate (parent , successor );
212234 }
213235
214- successor -> rb_left = tmp = node -> rb_left ;
236+ tmp = node -> rb_left ;
237+ WRITE_ONCE (successor -> rb_left , tmp );
215238 rb_set_parent (tmp , successor );
216239
217240 pc = node -> __rb_parent_color ;
218241 tmp = __rb_parent (pc );
219242 __rb_change_child (node , successor , tmp , root );
243+
220244 if (child2 ) {
221245 successor -> __rb_parent_color = pc ;
222246 rb_set_parent_color (child2 , parent , RB_BLACK );
@@ -237,9 +261,21 @@ static __always_inline void
237261rb_erase_augmented (struct rb_node * node , struct rb_root * root ,
238262 const struct rb_augment_callbacks * augment )
239263{
240- struct rb_node * rebalance = __rb_erase_augmented (node , root , augment );
264+ struct rb_node * rebalance = __rb_erase_augmented (node , root ,
265+ NULL , augment );
241266 if (rebalance )
242267 __rb_erase_color (rebalance , root , augment -> rotate );
243268}
244269
245- #endif /* _TOOLS_LINUX_RBTREE_AUGMENTED_H */
270+ static __always_inline void
271+ rb_erase_augmented_cached (struct rb_node * node , struct rb_root_cached * root ,
272+ const struct rb_augment_callbacks * augment )
273+ {
274+ struct rb_node * rebalance = __rb_erase_augmented (node , & root -> rb_root ,
275+ & root -> rb_leftmost ,
276+ augment );
277+ if (rebalance )
278+ __rb_erase_color (rebalance , & root -> rb_root , augment -> rotate );
279+ }
280+
281+ #endif /* _TOOLS_LINUX_RBTREE_AUGMENTED_H */
0 commit comments