source: GPL/branches/uniaud32-next/lib32/rbtree.c@ 625

Last change on this file since 625 was 625, checked in by Paul Smedley, 5 years ago

Code cleanups to simplify future maintenance, update regmap/regcache/rbtree to linux 4.19.163 level

File size: 19.4 KB
Line 
1/*
2 Red Black Trees
3 (C) 1999 Andrea Arcangeli <andrea@suse.de>
4 (C) 2002 David Woodhouse <dwmw2@infradead.org>
5 (C) 2012 Michel Lespinasse <walken@google.com>
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20
21 linux/lib/rbtree.c
22*/
23/* from 4.19.163 */
24
25#include <linux/rbtree_augmented.h>
26#include <linux/export.h>
27#include <linux/module.h>
28#include <linux/printk.h>
29
30/*
31 * red-black trees properties: http://en.wikipedia.org/wiki/Rbtree
32 *
33 * 1) A node is either red or black
34 * 2) The root is black
35 * 3) All leaves (NULL) are black
36 * 4) Both children of every red node are black
37 * 5) Every simple path from root to leaves contains the same number
38 * of black nodes.
39 *
40 * 4 and 5 give the O(log n) guarantee, since 4 implies you cannot have two
41 * consecutive red nodes in a path and every red node is therefore followed by
42 * a black. So if B is the number of black nodes on every simple path (as per
43 * 5), then the longest possible path due to 4 is 2B.
44 *
45 * We shall indicate color with case, where black nodes are uppercase and red
46 * nodes will be lowercase. Unknown color nodes shall be drawn as red within
47 * parentheses and have some accompanying text comment.
48 */
49
50/*
51 * Notes on lockless lookups:
52 *
53 * All stores to the tree structure (rb_left and rb_right) must be done using
54 * WRITE_ONCE(). And we must not inadvertently cause (temporary) loops in the
55 * tree structure as seen in program order.
56 *
57 * These two requirements will allow lockless iteration of the tree -- not
58 * correct iteration mind you, tree rotations are not atomic so a lookup might
59 * miss entire subtrees.
60 *
61 * But they do guarantee that any such traversal will only see valid elements
62 * and that it will indeed complete -- does not get stuck in a loop.
63 *
64 * It also guarantees that if the lookup returns an element it is the 'correct'
65 * one. But not returning an element does _NOT_ mean it's not present.
66 *
67 * NOTE:
68 *
69 * Stores to __rb_parent_color are not important for simple lookups so those
70 * are left undone as of now. Nor did I check for loops involving parent
71 * pointers.
72 */
73
74/*static inline*/ void rb_set_black(struct rb_node *rb)
75{
76 rb->__rb_parent_color |= RB_BLACK;
77}
78
79/*static inline*/ struct rb_node *rb_red_parent(struct rb_node *red)
80{
81 return (struct rb_node *)red->__rb_parent_color;
82}
83
84/*
85 * Helper function for rotations:
86 * - old's parent and color get assigned to new
87 * - old gets assigned new as a parent and 'color' as a color.
88 */
89/*static inline*/ void
90__rb_rotate_set_parents(struct rb_node *old, struct rb_node *new,
91 struct rb_root *root, int color)
92{
93 struct rb_node *parent = rb_parent(old);
94 new->__rb_parent_color = old->__rb_parent_color;
95 rb_set_parent_color(old, new, color);
96 __rb_change_child(old, new, parent, root);
97}
98
99/*static inline*/ void
100__rb_insert(struct rb_node *node, struct rb_root *root,
101 bool newleft, struct rb_node **leftmost,
102 void (*augment_rotate)(struct rb_node *old, struct rb_node *new))
103{
104 struct rb_node *parent = rb_red_parent(node), *gparent, *tmp;
105
106 if (newleft)
107 *leftmost = node;
108
109 while (true) {
110 /*
111 * Loop invariant: node is red.
112 */
113 if (!parent) {
114 /*
115 * The inserted node is root. Either this is the
116 * first node, or we recursed at Case 1 below and
117 * are no longer violating 4).
118 */
119 rb_set_parent_color(node, NULL, RB_BLACK);
120 break;
121 }
122
123 /*
124 * If there is a black parent, we are done.
125 * Otherwise, take some corrective action as,
126 * per 4), we don't want a red root or two
127 * consecutive red nodes.
128 */
129 if(rb_is_black(parent))
130 break;
131
132 gparent = rb_red_parent(parent);
133
134 tmp = gparent->rb_right;
135 if (parent != tmp) { /* parent == gparent->rb_left */
136 if (tmp && rb_is_red(tmp)) {
137 /*
138 * Case 1 - node's uncle is red (color flips).
139 *
140 * G g
141 * / \ / \
142 * p u --> P U
143 * / /
144 * n n
145 *
146 * However, since g's parent might be red, and
147 * 4) does not allow this, we need to recurse
148 * at g.
149 */
150 rb_set_parent_color(tmp, gparent, RB_BLACK);
151 rb_set_parent_color(parent, gparent, RB_BLACK);
152 node = gparent;
153 parent = rb_parent(node);
154 rb_set_parent_color(node, parent, RB_RED);
155 continue;
156 }
157
158 tmp = parent->rb_right;
159 if (node == tmp) {
160 /*
161 * Case 2 - node's uncle is black and node is
162 * the parent's right child (left rotate at parent).
163 *
164 * G G
165 * / \ / \
166 * p U --> n U
167 * \ /
168 * n p
169 *
170 * This still leaves us in violation of 4), the
171 * continuation into Case 3 will fix that.
172 */
173 tmp = node->rb_left;
174 WRITE_ONCE(parent->rb_right, tmp);
175 WRITE_ONCE(node->rb_left, parent);
176 if (tmp)
177 rb_set_parent_color(tmp, parent,
178 RB_BLACK);
179 rb_set_parent_color(parent, node, RB_RED);
180 augment_rotate(parent, node);
181 parent = node;
182 tmp = node->rb_right;
183 }
184
185 /*
186 * Case 3 - node's uncle is black and node is
187 * the parent's left child (right rotate at gparent).
188 *
189 * G P
190 * / \ / \
191 * p U --> n g
192 * / \
193 * n U
194 */
195 WRITE_ONCE(gparent->rb_left, tmp); /* == parent->rb_right */
196 WRITE_ONCE(parent->rb_right, gparent);
197 if (tmp)
198 rb_set_parent_color(tmp, gparent, RB_BLACK);
199 __rb_rotate_set_parents(gparent, parent, root, RB_RED);
200 augment_rotate(gparent, parent);
201 break;
202 } else {
203 tmp = gparent->rb_left;
204 if (tmp && rb_is_red(tmp)) {
205 /* Case 1 - color flips */
206 rb_set_parent_color(tmp, gparent, RB_BLACK);
207 rb_set_parent_color(parent, gparent, RB_BLACK);
208 node = gparent;
209 parent = rb_parent(node);
210 rb_set_parent_color(node, parent, RB_RED);
211 continue;
212 }
213
214 tmp = parent->rb_left;
215 if (node == tmp) {
216 /* Case 2 - right rotate at parent */
217 tmp = node->rb_right;
218 WRITE_ONCE(parent->rb_left, tmp);
219 WRITE_ONCE(node->rb_right, parent);
220 if (tmp)
221 rb_set_parent_color(tmp, parent,
222 RB_BLACK);
223 rb_set_parent_color(parent, node, RB_RED);
224 augment_rotate(parent, node);
225 parent = node;
226 tmp = node->rb_left;
227 }
228
229 /* Case 3 - left rotate at gparent */
230 WRITE_ONCE(gparent->rb_right, tmp); /* == parent->rb_left */
231 WRITE_ONCE(parent->rb_left, gparent);
232 if (tmp)
233 rb_set_parent_color(tmp, gparent, RB_BLACK);
234 __rb_rotate_set_parents(gparent, parent, root, RB_RED);
235 augment_rotate(gparent, parent);
236 break;
237 }
238 }
239}
240
241/*
242 * Inline version for rb_erase() use - we want to be able to inline
243 * and eliminate the dummy_rotate callback there
244 */
245/*static inline*/ void
246____rb_erase_color(struct rb_node *parent, struct rb_root *root,
247 void (*augment_rotate)(struct rb_node *old, struct rb_node *new))
248{
249 struct rb_node *node = NULL, *sibling, *tmp1, *tmp2;
250
251 while (true) {
252 /*
253 * Loop invariants:
254 * - node is black (or NULL on first iteration)
255 * - node is not the root (parent is not NULL)
256 * - All leaf paths going through parent and node have a
257 * black node count that is 1 lower than other leaf paths.
258 */
259 sibling = parent->rb_right;
260 if (node != sibling) { /* node == parent->rb_left */
261 if (rb_is_red(sibling)) {
262 /*
263 * Case 1 - left rotate at parent
264 *
265 * P S
266 * / \ / \
267 * N s --> p Sr
268 * / \ / \
269 * Sl Sr N Sl
270 */
271 tmp1 = sibling->rb_left;
272 WRITE_ONCE(parent->rb_right, tmp1);
273 WRITE_ONCE(sibling->rb_left, parent);
274 rb_set_parent_color(tmp1, parent, RB_BLACK);
275 __rb_rotate_set_parents(parent, sibling, root,
276 RB_RED);
277 augment_rotate(parent, sibling);
278 sibling = tmp1;
279 }
280 tmp1 = sibling->rb_right;
281 if (!tmp1 || rb_is_black(tmp1)) {
282 tmp2 = sibling->rb_left;
283 if (!tmp2 || rb_is_black(tmp2)) {
284 /*
285 * Case 2 - sibling color flip
286 * (p could be either color here)
287 *
288 * (p) (p)
289 * / \ / \
290 * N S --> N s
291 * / \ / \
292 * Sl Sr Sl Sr
293 *
294 * This leaves us violating 5) which
295 * can be fixed by flipping p to black
296 * if it was red, or by recursing at p.
297 * p is red when coming from Case 1.
298 */
299 rb_set_parent_color(sibling, parent,
300 RB_RED);
301 if (rb_is_red(parent))
302 rb_set_black(parent);
303 else {
304 node = parent;
305 parent = rb_parent(node);
306 if (parent)
307 continue;
308 }
309 break;
310 }
311 /*
312 * Case 3 - right rotate at sibling
313 * (p could be either color here)
314 *
315 * (p) (p)
316 * / \ / \
317 * N S --> N sl
318 * / \ \
319 * sl Sr S
320 * \
321 * Sr
322 *
323 * Note: p might be red, and then both
324 * p and sl are red after rotation(which
325 * breaks property 4). This is fixed in
326 * Case 4 (in __rb_rotate_set_parents()
327 * which set sl the color of p
328 * and set p RB_BLACK)
329 *
330 * (p) (sl)
331 * / \ / \
332 * N sl --> P S
333 * \ / \
334 * S N Sr
335 * \
336 * Sr
337 */
338 tmp1 = tmp2->rb_right;
339 WRITE_ONCE(sibling->rb_left, tmp1);
340 WRITE_ONCE(tmp2->rb_right, sibling);
341 WRITE_ONCE(parent->rb_right, tmp2);
342 if (tmp1)
343 rb_set_parent_color(tmp1, sibling,
344 RB_BLACK);
345 augment_rotate(sibling, tmp2);
346 tmp1 = sibling;
347 sibling = tmp2;
348 }
349 /*
350 * Case 4 - left rotate at parent + color flips
351 * (p and sl could be either color here.
352 * After rotation, p becomes black, s acquires
353 * p's color, and sl keeps its color)
354 *
355 * (p) (s)
356 * / \ / \
357 * N S --> P Sr
358 * / \ / \
359 * (sl) sr N (sl)
360 */
361 tmp2 = sibling->rb_left;
362 WRITE_ONCE(parent->rb_right, tmp2);
363 WRITE_ONCE(sibling->rb_left, parent);
364 rb_set_parent_color(tmp1, sibling, RB_BLACK);
365 if (tmp2)
366 rb_set_parent(tmp2, parent);
367 __rb_rotate_set_parents(parent, sibling, root,
368 RB_BLACK);
369 augment_rotate(parent, sibling);
370 break;
371 } else {
372 sibling = parent->rb_left;
373 if (rb_is_red(sibling)) {
374 /* Case 1 - right rotate at parent */
375 tmp1 = sibling->rb_right;
376 WRITE_ONCE(parent->rb_left, tmp1);
377 WRITE_ONCE(sibling->rb_right, parent);
378 rb_set_parent_color(tmp1, parent, RB_BLACK);
379 __rb_rotate_set_parents(parent, sibling, root,
380 RB_RED);
381 augment_rotate(parent, sibling);
382 sibling = tmp1;
383 }
384 tmp1 = sibling->rb_left;
385 if (!tmp1 || rb_is_black(tmp1)) {
386 tmp2 = sibling->rb_right;
387 if (!tmp2 || rb_is_black(tmp2)) {
388 /* Case 2 - sibling color flip */
389 rb_set_parent_color(sibling, parent,
390 RB_RED);
391 if (rb_is_red(parent))
392 rb_set_black(parent);
393 else {
394 node = parent;
395 parent = rb_parent(node);
396 if (parent)
397 continue;
398 }
399 break;
400 }
401 /* Case 3 - left rotate at sibling */
402 tmp1 = tmp2->rb_left;
403 WRITE_ONCE(sibling->rb_right, tmp1);
404 WRITE_ONCE(tmp2->rb_left, sibling);
405 WRITE_ONCE(parent->rb_left, tmp2);
406 if (tmp1)
407 rb_set_parent_color(tmp1, sibling,
408 RB_BLACK);
409 augment_rotate(sibling, tmp2);
410 tmp1 = sibling;
411 sibling = tmp2;
412 }
413 /* Case 4 - right rotate at parent + color flips */
414 tmp2 = sibling->rb_right;
415 WRITE_ONCE(parent->rb_left, tmp2);
416 WRITE_ONCE(sibling->rb_right, parent);
417 rb_set_parent_color(tmp1, sibling, RB_BLACK);
418 if (tmp2)
419 rb_set_parent(tmp2, parent);
420 __rb_rotate_set_parents(parent, sibling, root,
421 RB_BLACK);
422 augment_rotate(parent, sibling);
423 break;
424 }
425 }
426}
427
428/* Non-inline version for rb_erase_augmented() use */
429void __rb_erase_color(struct rb_node *parent, struct rb_root *root,
430 void (*augment_rotate)(struct rb_node *old, struct rb_node *new))
431{
432 ____rb_erase_color(parent, root, augment_rotate);
433}
434EXPORT_SYMBOL(__rb_erase_color);
435
436/*
437 * Non-augmented rbtree manipulation functions.
438 *
439 * We use dummy augmented callbacks here, and have the compiler optimize them
440 * out of the rb_insert_color() and rb_erase() function definitions.
441 */
442
443/*static inline*/ void dummy_propagate(struct rb_node *node, struct rb_node *stop) {}
444/*static inline*/ void dummy_copy(struct rb_node *old, struct rb_node *new) {}
445/*static inline*/ void dummy_rotate(struct rb_node *old, struct rb_node *new) {}
446
447static const struct rb_augment_callbacks dummy_callbacks = {
448 .propagate = dummy_propagate,
449 .copy = dummy_copy,
450 .rotate = dummy_rotate
451};
452
453void rb_insert_color(struct rb_node *node, struct rb_root *root)
454{
455 __rb_insert(node, root, false, NULL, dummy_rotate);
456}
457EXPORT_SYMBOL(rb_insert_color);
458
459void rb_erase(struct rb_node *node, struct rb_root *root)
460{
461 struct rb_node *rebalance;
462 rebalance = __rb_erase_augmented(node, root,
463 NULL, &dummy_callbacks);
464 if (rebalance)
465 ____rb_erase_color(rebalance, root, dummy_rotate);
466}
467EXPORT_SYMBOL(rb_erase);
468
469void rb_insert_color_cached(struct rb_node *node,
470 struct rb_root_cached *root, bool leftmost)
471{
472 __rb_insert(node, &root->rb_root, leftmost,
473 &root->rb_leftmost, dummy_rotate);
474}
475EXPORT_SYMBOL(rb_insert_color_cached);
476
477void rb_erase_cached(struct rb_node *node, struct rb_root_cached *root)
478{
479 struct rb_node *rebalance;
480 rebalance = __rb_erase_augmented(node, &root->rb_root,
481 &root->rb_leftmost, &dummy_callbacks);
482 if (rebalance)
483 ____rb_erase_color(rebalance, &root->rb_root, dummy_rotate);
484}
485EXPORT_SYMBOL(rb_erase_cached);
486
487/*
488 * Augmented rbtree manipulation functions.
489 *
490 * This instantiates the same inline functions as in the non-augmented
491 * case, but this time with user-defined callbacks.
492 */
493
494void __rb_insert_augmented(struct rb_node *node, struct rb_root *root,
495 bool newleft, struct rb_node **leftmost,
496 void (*augment_rotate)(struct rb_node *old, struct rb_node *new))
497{
498 __rb_insert(node, root, newleft, leftmost, augment_rotate);
499}
500EXPORT_SYMBOL(__rb_insert_augmented);
501
502/*
503 * This function returns the first node (in sort order) of the tree.
504 */
505struct rb_node *rb_first(const struct rb_root *root)
506{
507 struct rb_node *n;
508
509 n = root->rb_node;
510 if (!n)
511 return NULL;
512 while (n->rb_left)
513 n = n->rb_left;
514 return n;
515}
516EXPORT_SYMBOL(rb_first);
517
518struct rb_node *rb_last(const struct rb_root *root)
519{
520 struct rb_node *n;
521
522 n = root->rb_node;
523 if (!n)
524 return NULL;
525 while (n->rb_right)
526 n = n->rb_right;
527 return n;
528}
529EXPORT_SYMBOL(rb_last);
530
531struct rb_node *rb_next(const struct rb_node *node)
532{
533 struct rb_node *parent;
534
535 if (RB_EMPTY_NODE(node))
536 return NULL;
537
538 /*
539 * If we have a right-hand child, go down and then left as far
540 * as we can.
541 */
542 if (node->rb_right) {
543 node = node->rb_right;
544 while (node->rb_left)
545 node=node->rb_left;
546 return (struct rb_node *)node;
547 }
548
549 /*
550 * No right-hand children. Everything down and left is smaller than us,
551 * so any 'next' node must be in the general direction of our parent.
552 * Go up the tree; any time the ancestor is a right-hand child of its
553 * parent, keep going up. First time it's a left-hand child of its
554 * parent, said parent is our 'next' node.
555 */
556 while ((parent = rb_parent(node)) && node == parent->rb_right)
557 node = parent;
558
559 return parent;
560}
561EXPORT_SYMBOL(rb_next);
562
563struct rb_node *rb_prev(const struct rb_node *node)
564{
565 struct rb_node *parent;
566
567 if (RB_EMPTY_NODE(node))
568 return NULL;
569
570 /*
571 * If we have a left-hand child, go down and then right as far
572 * as we can.
573 */
574 if (node->rb_left) {
575 node = node->rb_left;
576 while (node->rb_right)
577 node=node->rb_right;
578 return (struct rb_node *)node;
579 }
580
581 /*
582 * No left-hand children. Go up till we find an ancestor which
583 * is a right-hand child of its parent.
584 */
585 while ((parent = rb_parent(node)) && node == parent->rb_left)
586 node = parent;
587
588 return parent;
589}
590EXPORT_SYMBOL(rb_prev);
591
592void rb_replace_node(struct rb_node *victim, struct rb_node *new,
593 struct rb_root *root)
594{
595 struct rb_node *parent = rb_parent(victim);
596
597 /* Copy the pointers/colour from the victim to the replacement */
598 *new = *victim;
599
600 /* Set the surrounding nodes to point to the replacement */
601 if (victim->rb_left)
602 rb_set_parent(victim->rb_left, new);
603 if (victim->rb_right)
604 rb_set_parent(victim->rb_right, new);
605 __rb_change_child(victim, new, parent, root);
606}
607EXPORT_SYMBOL(rb_replace_node);
608
609void rb_replace_node_cached(struct rb_node *victim, struct rb_node *new,
610 struct rb_root_cached *root)
611{
612 rb_replace_node(victim, new, &root->rb_root);
613
614 if (root->rb_leftmost == victim)
615 root->rb_leftmost = new;
616}
617EXPORT_SYMBOL(rb_replace_node_cached);
618
619#ifndef TARGET_OS2
620void rb_replace_node_rcu(struct rb_node *victim, struct rb_node *new,
621 struct rb_root *root)
622{
623 struct rb_node *parent = rb_parent(victim);
624
625 /* Copy the pointers/colour from the victim to the replacement */
626 *new = *victim;
627
628 /* Set the surrounding nodes to point to the replacement */
629 if (victim->rb_left)
630 rb_set_parent(victim->rb_left, new);
631 if (victim->rb_right)
632 rb_set_parent(victim->rb_right, new);
633
634 /* Set the parent's pointer to the new node last after an RCU barrier
635 * so that the pointers onwards are seen to be set correctly when doing
636 * an RCU walk over the tree.
637 */
638 __rb_change_child_rcu(victim, new, parent, root);
639}
640EXPORT_SYMBOL(rb_replace_node_rcu);
641#endif
642
643static struct rb_node *rb_left_deepest_node(const struct rb_node *node)
644{
645 for (;;) {
646 if (node->rb_left)
647 node = node->rb_left;
648 else if (node->rb_right)
649 node = node->rb_right;
650 else
651 return (struct rb_node *)node;
652 }
653}
654
655struct rb_node *rb_next_postorder(const struct rb_node *node)
656{
657 const struct rb_node *parent;
658 if (!node)
659 return NULL;
660 parent = rb_parent(node);
661
662 /* If we're sitting on node, we've already seen our children */
663 if (parent && node == parent->rb_left && parent->rb_right) {
664 /* If we are the parent's left node, go to the parent's right
665 * node then all the way down to the left */
666 return rb_left_deepest_node(parent->rb_right);
667 } else
668 /* Otherwise we are the parent's right node, and the parent
669 * should be next */
670 return (struct rb_node *)parent;
671}
672EXPORT_SYMBOL(rb_next_postorder);
673
674struct rb_node *rb_first_postorder(const struct rb_root *root)
675{
676 if (!root->rb_node)
677 return NULL;
678
679 return rb_left_deepest_node(root->rb_node);
680}
681EXPORT_SYMBOL(rb_first_postorder);
Note: See TracBrowser for help on using the repository browser.