summaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorMichel Lespinasse <walken@google.com>2019-09-25 19:46:07 -0400
committerLinus Torvalds <torvalds@linux-foundation.org>2019-09-25 20:51:39 -0400
commit315cc066b8ae8349a27887ad7a34e1916e9797fe (patch)
tree3c7dba850cee3ec6991228ae425db0de78fbb252 /lib
parent444b8a83f1e01584ff2d53f5951d8e836c0070b5 (diff)
augmented rbtree: add new RB_DECLARE_CALLBACKS_MAX macro
Add RB_DECLARE_CALLBACKS_MAX, which generates augmented rbtree callbacks for the case where the augmented value is a scalar whose definition follows a max(f(node)) pattern. This actually covers all present uses of RB_DECLARE_CALLBACKS, and saves some (source) code duplication in the various RBCOMPUTE function definitions. [walken@google.com: fix mm/vmalloc.c] Link: http://lkml.kernel.org/r/CANN689FXgK13wDYNh1zKxdipeTuALG4eKvKpsdZqKFJ-rvtGiQ@mail.gmail.com [walken@google.com: re-add check to check_augmented()] Link: http://lkml.kernel.org/r/20190727022027.GA86863@google.com Link: http://lkml.kernel.org/r/20190703040156.56953-3-walken@google.com Signed-off-by: Michel Lespinasse <walken@google.com> Acked-by: Peter Zijlstra (Intel) <peterz@infradead.org> Cc: David Howells <dhowells@redhat.com> Cc: Davidlohr Bueso <dbueso@suse.de> Cc: Uladzislau Rezki <urezki@gmail.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Diffstat (limited to 'lib')
-rw-r--r--lib/rbtree_test.c37
1 files changed, 17 insertions, 20 deletions
diff --git a/lib/rbtree_test.c b/lib/rbtree_test.c
index 62b8ee92643d..41ae3c7570d3 100644
--- a/lib/rbtree_test.c
+++ b/lib/rbtree_test.c
@@ -77,26 +77,10 @@ static inline void erase_cached(struct test_node *node, struct rb_root_cached *r
77} 77}
78 78
79 79
80static inline u32 augment_recompute(struct test_node *node) 80#define NODE_VAL(node) ((node)->val)
81{
82 u32 max = node->val, child_augmented;
83 if (node->rb.rb_left) {
84 child_augmented = rb_entry(node->rb.rb_left, struct test_node,
85 rb)->augmented;
86 if (max < child_augmented)
87 max = child_augmented;
88 }
89 if (node->rb.rb_right) {
90 child_augmented = rb_entry(node->rb.rb_right, struct test_node,
91 rb)->augmented;
92 if (max < child_augmented)
93 max = child_augmented;
94 }
95 return max;
96}
97 81
98RB_DECLARE_CALLBACKS(static, augment_callbacks, struct test_node, rb, 82RB_DECLARE_CALLBACKS_MAX(static, augment_callbacks,
99 u32, augmented, augment_recompute) 83 struct test_node, rb, u32, augmented, NODE_VAL)
100 84
101static void insert_augmented(struct test_node *node, 85static void insert_augmented(struct test_node *node,
102 struct rb_root_cached *root) 86 struct rb_root_cached *root)
@@ -238,7 +222,20 @@ static void check_augmented(int nr_nodes)
238 check(nr_nodes); 222 check(nr_nodes);
239 for (rb = rb_first(&root.rb_root); rb; rb = rb_next(rb)) { 223 for (rb = rb_first(&root.rb_root); rb; rb = rb_next(rb)) {
240 struct test_node *node = rb_entry(rb, struct test_node, rb); 224 struct test_node *node = rb_entry(rb, struct test_node, rb);
241 WARN_ON_ONCE(node->augmented != augment_recompute(node)); 225 u32 subtree, max = node->val;
226 if (node->rb.rb_left) {
227 subtree = rb_entry(node->rb.rb_left, struct test_node,
228 rb)->augmented;
229 if (max < subtree)
230 max = subtree;
231 }
232 if (node->rb.rb_right) {
233 subtree = rb_entry(node->rb.rb_right, struct test_node,
234 rb)->augmented;
235 if (max < subtree)
236 max = subtree;
237 }
238 WARN_ON_ONCE(node->augmented != max);
242 } 239 }
243} 240}
244 241