1
2
3
4
5
6
7
8
9
10
11
12#include <linux/rbtree_augmented.h>
13#include <linux/export.h>
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59static inline void rb_set_black(struct rb_node *rb)
60{
61 rb->__rb_parent_color |= RB_BLACK;
62}
63
64static inline struct rb_node *rb_red_parent(struct rb_node *red)
65{
66 return (struct rb_node *)red->__rb_parent_color;
67}
68
69
70
71
72
73
74static inline void
75__rb_rotate_set_parents(struct rb_node *old, struct rb_node *new,
76 struct rb_root *root, int color)
77{
78 struct rb_node *parent = rb_parent(old);
79 new->__rb_parent_color = old->__rb_parent_color;
80 rb_set_parent_color(old, new, color);
81 __rb_change_child(old, new, parent, root);
82}
83
84static __always_inline void
85__rb_insert(struct rb_node *node, struct rb_root *root,
86 bool newleft, struct rb_node **leftmost,
87 void (*augment_rotate)(struct rb_node *old, struct rb_node *new))
88{
89 struct rb_node *parent = rb_red_parent(node), *gparent, *tmp;
90
91 if (newleft)
92 *leftmost = node;
93
94 while (true) {
95
96
97
98 if (unlikely(!parent)) {
99
100
101
102
103
104 rb_set_parent_color(node, NULL, RB_BLACK);
105 break;
106 }
107
108
109
110
111
112
113
114 if(rb_is_black(parent))
115 break;
116
117 gparent = rb_red_parent(parent);
118
119 tmp = gparent->rb_right;
120 if (parent != tmp) {
121 if (tmp && rb_is_red(tmp)) {
122
123
124
125
126
127
128
129
130
131
132
133
134
135 rb_set_parent_color(tmp, gparent, RB_BLACK);
136 rb_set_parent_color(parent, gparent, RB_BLACK);
137 node = gparent;
138 parent = rb_parent(node);
139 rb_set_parent_color(node, parent, RB_RED);
140 continue;
141 }
142
143 tmp = parent->rb_right;
144 if (node == tmp) {
145
146
147
148
149
150
151
152
153
154
155
156
157
158 tmp = node->rb_left;
159 WRITE_ONCE(parent->rb_right, tmp);
160 WRITE_ONCE(node->rb_left, parent);
161 if (tmp)
162 rb_set_parent_color(tmp, parent,
163 RB_BLACK);
164 rb_set_parent_color(parent, node, RB_RED);
165 augment_rotate(parent, node);
166 parent = node;
167 tmp = node->rb_right;
168 }
169
170
171
172
173
174
175
176
177
178
179
180 WRITE_ONCE(gparent->rb_left, tmp);
181 WRITE_ONCE(parent->rb_right, gparent);
182 if (tmp)
183 rb_set_parent_color(tmp, gparent, RB_BLACK);
184 __rb_rotate_set_parents(gparent, parent, root, RB_RED);
185 augment_rotate(gparent, parent);
186 break;
187 } else {
188 tmp = gparent->rb_left;
189 if (tmp && rb_is_red(tmp)) {
190
191 rb_set_parent_color(tmp, gparent, RB_BLACK);
192 rb_set_parent_color(parent, gparent, RB_BLACK);
193 node = gparent;
194 parent = rb_parent(node);
195 rb_set_parent_color(node, parent, RB_RED);
196 continue;
197 }
198
199 tmp = parent->rb_left;
200 if (node == tmp) {
201
202 tmp = node->rb_right;
203 WRITE_ONCE(parent->rb_left, tmp);
204 WRITE_ONCE(node->rb_right, parent);
205 if (tmp)
206 rb_set_parent_color(tmp, parent,
207 RB_BLACK);
208 rb_set_parent_color(parent, node, RB_RED);
209 augment_rotate(parent, node);
210 parent = node;
211 tmp = node->rb_left;
212 }
213
214
215 WRITE_ONCE(gparent->rb_right, tmp);
216 WRITE_ONCE(parent->rb_left, gparent);
217 if (tmp)
218 rb_set_parent_color(tmp, gparent, RB_BLACK);
219 __rb_rotate_set_parents(gparent, parent, root, RB_RED);
220 augment_rotate(gparent, parent);
221 break;
222 }
223 }
224}
225
226
227
228
229
230static __always_inline void
231____rb_erase_color(struct rb_node *parent, struct rb_root *root,
232 void (*augment_rotate)(struct rb_node *old, struct rb_node *new))
233{
234 struct rb_node *node = NULL, *sibling, *tmp1, *tmp2;
235
236 while (true) {
237
238
239
240
241
242
243
244 sibling = parent->rb_right;
245 if (node != sibling) {
246 if (rb_is_red(sibling)) {
247
248
249
250
251
252
253
254
255
256 tmp1 = sibling->rb_left;
257 WRITE_ONCE(parent->rb_right, tmp1);
258 WRITE_ONCE(sibling->rb_left, parent);
259 rb_set_parent_color(tmp1, parent, RB_BLACK);
260 __rb_rotate_set_parents(parent, sibling, root,
261 RB_RED);
262 augment_rotate(parent, sibling);
263 sibling = tmp1;
264 }
265 tmp1 = sibling->rb_right;
266 if (!tmp1 || rb_is_black(tmp1)) {
267 tmp2 = sibling->rb_left;
268 if (!tmp2 || rb_is_black(tmp2)) {
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284 rb_set_parent_color(sibling, parent,
285 RB_RED);
286 if (rb_is_red(parent))
287 rb_set_black(parent);
288 else {
289 node = parent;
290 parent = rb_parent(node);
291 if (parent)
292 continue;
293 }
294 break;
295 }
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323 tmp1 = tmp2->rb_right;
324 WRITE_ONCE(sibling->rb_left, tmp1);
325 WRITE_ONCE(tmp2->rb_right, sibling);
326 WRITE_ONCE(parent->rb_right, tmp2);
327 if (tmp1)
328 rb_set_parent_color(tmp1, sibling,
329 RB_BLACK);
330 augment_rotate(sibling, tmp2);
331 tmp1 = sibling;
332 sibling = tmp2;
333 }
334
335
336
337
338
339
340
341
342
343
344
345
346 tmp2 = sibling->rb_left;
347 WRITE_ONCE(parent->rb_right, tmp2);
348 WRITE_ONCE(sibling->rb_left, parent);
349 rb_set_parent_color(tmp1, sibling, RB_BLACK);
350 if (tmp2)
351 rb_set_parent(tmp2, parent);
352 __rb_rotate_set_parents(parent, sibling, root,
353 RB_BLACK);
354 augment_rotate(parent, sibling);
355 break;
356 } else {
357 sibling = parent->rb_left;
358 if (rb_is_red(sibling)) {
359
360 tmp1 = sibling->rb_right;
361 WRITE_ONCE(parent->rb_left, tmp1);
362 WRITE_ONCE(sibling->rb_right, parent);
363 rb_set_parent_color(tmp1, parent, RB_BLACK);
364 __rb_rotate_set_parents(parent, sibling, root,
365 RB_RED);
366 augment_rotate(parent, sibling);
367 sibling = tmp1;
368 }
369 tmp1 = sibling->rb_left;
370 if (!tmp1 || rb_is_black(tmp1)) {
371 tmp2 = sibling->rb_right;
372 if (!tmp2 || rb_is_black(tmp2)) {
373
374 rb_set_parent_color(sibling, parent,
375 RB_RED);
376 if (rb_is_red(parent))
377 rb_set_black(parent);
378 else {
379 node = parent;
380 parent = rb_parent(node);
381 if (parent)
382 continue;
383 }
384 break;
385 }
386
387 tmp1 = tmp2->rb_left;
388 WRITE_ONCE(sibling->rb_right, tmp1);
389 WRITE_ONCE(tmp2->rb_left, sibling);
390 WRITE_ONCE(parent->rb_left, tmp2);
391 if (tmp1)
392 rb_set_parent_color(tmp1, sibling,
393 RB_BLACK);
394 augment_rotate(sibling, tmp2);
395 tmp1 = sibling;
396 sibling = tmp2;
397 }
398
399 tmp2 = sibling->rb_right;
400 WRITE_ONCE(parent->rb_left, tmp2);
401 WRITE_ONCE(sibling->rb_right, parent);
402 rb_set_parent_color(tmp1, sibling, RB_BLACK);
403 if (tmp2)
404 rb_set_parent(tmp2, parent);
405 __rb_rotate_set_parents(parent, sibling, root,
406 RB_BLACK);
407 augment_rotate(parent, sibling);
408 break;
409 }
410 }
411}
412
413
414void __rb_erase_color(struct rb_node *parent, struct rb_root *root,
415 void (*augment_rotate)(struct rb_node *old, struct rb_node *new))
416{
417 ____rb_erase_color(parent, root, augment_rotate);
418}
419EXPORT_SYMBOL(__rb_erase_color);
420
421
422
423
424
425
426
427
428static inline void dummy_propagate(struct rb_node *node, struct rb_node *stop) {}
429static inline void dummy_copy(struct rb_node *old, struct rb_node *new) {}
430static inline void dummy_rotate(struct rb_node *old, struct rb_node *new) {}
431
432static const struct rb_augment_callbacks dummy_callbacks = {
433 .propagate = dummy_propagate,
434 .copy = dummy_copy,
435 .rotate = dummy_rotate
436};
437
438void rb_insert_color(struct rb_node *node, struct rb_root *root)
439{
440 __rb_insert(node, root, false, NULL, dummy_rotate);
441}
442EXPORT_SYMBOL(rb_insert_color);
443
444void rb_erase(struct rb_node *node, struct rb_root *root)
445{
446 struct rb_node *rebalance;
447 rebalance = __rb_erase_augmented(node, root,
448 NULL, &dummy_callbacks);
449 if (rebalance)
450 ____rb_erase_color(rebalance, root, dummy_rotate);
451}
452EXPORT_SYMBOL(rb_erase);
453
454void rb_insert_color_cached(struct rb_node *node,
455 struct rb_root_cached *root, bool leftmost)
456{
457 __rb_insert(node, &root->rb_root, leftmost,
458 &root->rb_leftmost, dummy_rotate);
459}
460EXPORT_SYMBOL(rb_insert_color_cached);
461
462void rb_erase_cached(struct rb_node *node, struct rb_root_cached *root)
463{
464 struct rb_node *rebalance;
465 rebalance = __rb_erase_augmented(node, &root->rb_root,
466 &root->rb_leftmost, &dummy_callbacks);
467 if (rebalance)
468 ____rb_erase_color(rebalance, &root->rb_root, dummy_rotate);
469}
470EXPORT_SYMBOL(rb_erase_cached);
471
472
473
474
475
476
477
478
479void __rb_insert_augmented(struct rb_node *node, struct rb_root *root,
480 bool newleft, struct rb_node **leftmost,
481 void (*augment_rotate)(struct rb_node *old, struct rb_node *new))
482{
483 __rb_insert(node, root, newleft, leftmost, augment_rotate);
484}
485EXPORT_SYMBOL(__rb_insert_augmented);
486
487
488
489
490struct rb_node *rb_first(const struct rb_root *root)
491{
492 struct rb_node *n;
493
494 n = root->rb_node;
495 if (!n)
496 return NULL;
497 while (n->rb_left)
498 n = n->rb_left;
499 return n;
500}
501EXPORT_SYMBOL(rb_first);
502
503struct rb_node *rb_last(const struct rb_root *root)
504{
505 struct rb_node *n;
506
507 n = root->rb_node;
508 if (!n)
509 return NULL;
510 while (n->rb_right)
511 n = n->rb_right;
512 return n;
513}
514EXPORT_SYMBOL(rb_last);
515
516struct rb_node *rb_next(const struct rb_node *node)
517{
518 struct rb_node *parent;
519
520 if (RB_EMPTY_NODE(node))
521 return NULL;
522
523
524
525
526
527 if (node->rb_right) {
528 node = node->rb_right;
529 while (node->rb_left)
530 node=node->rb_left;
531 return (struct rb_node *)node;
532 }
533
534
535
536
537
538
539
540
541 while ((parent = rb_parent(node)) && node == parent->rb_right)
542 node = parent;
543
544 return parent;
545}
546EXPORT_SYMBOL(rb_next);
547
548struct rb_node *rb_prev(const struct rb_node *node)
549{
550 struct rb_node *parent;
551
552 if (RB_EMPTY_NODE(node))
553 return NULL;
554
555
556
557
558
559 if (node->rb_left) {
560 node = node->rb_left;
561 while (node->rb_right)
562 node=node->rb_right;
563 return (struct rb_node *)node;
564 }
565
566
567
568
569
570 while ((parent = rb_parent(node)) && node == parent->rb_left)
571 node = parent;
572
573 return parent;
574}
575EXPORT_SYMBOL(rb_prev);
576
577void rb_replace_node(struct rb_node *victim, struct rb_node *new,
578 struct rb_root *root)
579{
580 struct rb_node *parent = rb_parent(victim);
581
582
583 *new = *victim;
584
585
586 if (victim->rb_left)
587 rb_set_parent(victim->rb_left, new);
588 if (victim->rb_right)
589 rb_set_parent(victim->rb_right, new);
590 __rb_change_child(victim, new, parent, root);
591}
592EXPORT_SYMBOL(rb_replace_node);
593
594void rb_replace_node_cached(struct rb_node *victim, struct rb_node *new,
595 struct rb_root_cached *root)
596{
597 rb_replace_node(victim, new, &root->rb_root);
598
599 if (root->rb_leftmost == victim)
600 root->rb_leftmost = new;
601}
602EXPORT_SYMBOL(rb_replace_node_cached);
603
604void rb_replace_node_rcu(struct rb_node *victim, struct rb_node *new,
605 struct rb_root *root)
606{
607 struct rb_node *parent = rb_parent(victim);
608
609
610 *new = *victim;
611
612
613 if (victim->rb_left)
614 rb_set_parent(victim->rb_left, new);
615 if (victim->rb_right)
616 rb_set_parent(victim->rb_right, new);
617
618
619
620
621
622 __rb_change_child_rcu(victim, new, parent, root);
623}
624EXPORT_SYMBOL(rb_replace_node_rcu);
625
626static struct rb_node *rb_left_deepest_node(const struct rb_node *node)
627{
628 for (;;) {
629 if (node->rb_left)
630 node = node->rb_left;
631 else if (node->rb_right)
632 node = node->rb_right;
633 else
634 return (struct rb_node *)node;
635 }
636}
637
638struct rb_node *rb_next_postorder(const struct rb_node *node)
639{
640 const struct rb_node *parent;
641 if (!node)
642 return NULL;
643 parent = rb_parent(node);
644
645
646 if (parent && node == parent->rb_left && parent->rb_right) {
647
648
649 return rb_left_deepest_node(parent->rb_right);
650 } else
651
652
653 return (struct rb_node *)parent;
654}
655EXPORT_SYMBOL(rb_next_postorder);
656
657struct rb_node *rb_first_postorder(const struct rb_root *root)
658{
659 if (!root->rb_node)
660 return NULL;
661
662 return rb_left_deepest_node(root->rb_node);
663}
664EXPORT_SYMBOL(rb_first_postorder);
665