From: "Uladzislau Rezki (Sony)" <urezki@gmail.com>
To: Andrew Morton <akpm@linux-foundation.org>,
Michel Lespinasse <walken@google.com>
Cc: linux-mm@kvack.org, LKML <linux-kernel@vger.kernel.org>,
Peter Zijlstra <peterz@infradead.org>,
Roman Gushchin <guro@fb.com>, Uladzislau Rezki <urezki@gmail.com>,
Hillf Danton <hdanton@sina.com>, Michal Hocko <mhocko@suse.com>,
Matthew Wilcox <willy@infradead.org>,
Oleksiy Avramchenko <oleksiy.avramchenko@sonymobile.com>,
Steven Rostedt <rostedt@goodmis.org>
Subject: [PATCH 1/2] augmented rbtree: use max3() in the *_compute_max() function
Date: Sun, 11 Aug 2019 20:46:12 +0200 [thread overview]
Message-ID: <20190811184613.20463-2-urezki@gmail.com> (raw)
In-Reply-To: <20190811184613.20463-1-urezki@gmail.com>
Recently there was introduced RB_DECLARE_CALLBACKS_MAX template.
One of the callback, to be more specific *_compute_max(), calculates
a maximum scalar value of node against its left/right sub-tree.
To simplify the code and improve readability we can switch and
make use of max3() macro that makes the code more transparent.
Signed-off-by: Uladzislau Rezki (Sony) <urezki@gmail.com>
---
include/linux/rbtree_augmented.h | 40 +++++++++++++++++-----------------
tools/include/linux/rbtree_augmented.h | 40 +++++++++++++++++-----------------
2 files changed, 40 insertions(+), 40 deletions(-)
diff --git a/include/linux/rbtree_augmented.h b/include/linux/rbtree_augmented.h
index fdd421b8d9ae..fb29d6627646 100644
--- a/include/linux/rbtree_augmented.h
+++ b/include/linux/rbtree_augmented.h
@@ -119,26 +119,26 @@ RBSTATIC const struct rb_augment_callbacks RBNAME = { \
#define RB_DECLARE_CALLBACKS_MAX(RBSTATIC, RBNAME, RBSTRUCT, RBFIELD, \
RBTYPE, RBAUGMENTED, RBCOMPUTE) \
-static inline bool RBNAME ## _compute_max(RBSTRUCT *node, bool exit) \
-{ \
- RBSTRUCT *child; \
- RBTYPE max = RBCOMPUTE(node); \
- if (node->RBFIELD.rb_left) { \
- child = rb_entry(node->RBFIELD.rb_left, RBSTRUCT, RBFIELD); \
- if (child->RBAUGMENTED > max) \
- max = child->RBAUGMENTED; \
- } \
- if (node->RBFIELD.rb_right) { \
- child = rb_entry(node->RBFIELD.rb_right, RBSTRUCT, RBFIELD); \
- if (child->RBAUGMENTED > max) \
- max = child->RBAUGMENTED; \
- } \
- if (exit && node->RBAUGMENTED == max) \
- return true; \
- node->RBAUGMENTED = max; \
- return false; \
-} \
-RB_DECLARE_CALLBACKS(RBSTATIC, RBNAME, \
+static inline RBTYPE RBNAME ## _get_max(struct rb_node *node) \
+{ \
+ RBSTRUCT *tmp; \
+ \
+ tmp = rb_entry_safe(node, RBSTRUCT, RBFIELD); \
+ return tmp ? tmp->RBAUGMENTED : 0; \
+} \
+ \
+static inline bool RBNAME ## _compute_max(RBSTRUCT *node, bool exit) \
+{ \
+ RBTYPE max = max3(RBCOMPUTE(node), \
+ RBNAME ## _get_max(node->RBFIELD.rb_left), \
+ RBNAME ## _get_max(node->RBFIELD.rb_right)); \
+ \
+ if (exit && node->RBAUGMENTED == max) \
+ return true; \
+ node->RBAUGMENTED = max; \
+ return false; \
+} \
+RB_DECLARE_CALLBACKS(RBSTATIC, RBNAME, \
RBSTRUCT, RBFIELD, RBAUGMENTED, RBNAME ## _compute_max)
diff --git a/tools/include/linux/rbtree_augmented.h b/tools/include/linux/rbtree_augmented.h
index 381aa948610d..3b8284479e98 100644
--- a/tools/include/linux/rbtree_augmented.h
+++ b/tools/include/linux/rbtree_augmented.h
@@ -121,26 +121,26 @@ RBSTATIC const struct rb_augment_callbacks RBNAME = { \
#define RB_DECLARE_CALLBACKS_MAX(RBSTATIC, RBNAME, RBSTRUCT, RBFIELD, \
RBTYPE, RBAUGMENTED, RBCOMPUTE) \
-static inline bool RBNAME ## _compute_max(RBSTRUCT *node, bool exit) \
-{ \
- RBSTRUCT *child; \
- RBTYPE max = RBCOMPUTE(node); \
- if (node->RBFIELD.rb_left) { \
- child = rb_entry(node->RBFIELD.rb_left, RBSTRUCT, RBFIELD); \
- if (child->RBAUGMENTED > max) \
- max = child->RBAUGMENTED; \
- } \
- if (node->RBFIELD.rb_right) { \
- child = rb_entry(node->RBFIELD.rb_right, RBSTRUCT, RBFIELD); \
- if (child->RBAUGMENTED > max) \
- max = child->RBAUGMENTED; \
- } \
- if (exit && node->RBAUGMENTED == max) \
- return true; \
- node->RBAUGMENTED = max; \
- return false; \
-} \
-RB_DECLARE_CALLBACKS(RBSTATIC, RBNAME, \
+static inline RBTYPE RBNAME ## _get_max(struct rb_node *node) \
+{ \
+ RBSTRUCT *tmp; \
+ \
+ tmp = rb_entry_safe(node, RBSTRUCT, RBFIELD); \
+ return tmp ? tmp->RBAUGMENTED : 0; \
+} \
+ \
+static inline bool RBNAME ## _compute_max(RBSTRUCT *node, bool exit) \
+{ \
+ RBTYPE max = max3(RBCOMPUTE(node), \
+ RBNAME ## _get_max(node->RBFIELD.rb_left), \
+ RBNAME ## _get_max(node->RBFIELD.rb_right)); \
+ \
+ if (exit && node->RBAUGMENTED == max) \
+ return true; \
+ node->RBAUGMENTED = max; \
+ return false; \
+} \
+RB_DECLARE_CALLBACKS(RBSTATIC, RBNAME, \
RBSTRUCT, RBFIELD, RBAUGMENTED, RBNAME ## _compute_max)
--
2.11.0
next prev parent reply other threads:[~2019-08-11 18:46 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2019-08-11 18:46 [PATCH 0/2] some cleanups related to RB_DECLARE_CALLBACKS_MAX Uladzislau Rezki (Sony)
2019-08-11 18:46 ` Uladzislau Rezki (Sony) [this message]
2019-08-12 1:37 ` [PATCH 1/2] augmented rbtree: use max3() in the *_compute_max() function Michel Lespinasse
2019-08-13 9:00 ` Uladzislau Rezki
2019-08-11 18:46 ` [PATCH 2/2] mm/vmalloc: use generated callback to populate subtree_max_size Uladzislau Rezki (Sony)
2019-08-12 0:39 ` Michel Lespinasse
2019-08-13 9:02 ` Uladzislau Rezki
2019-08-12 7:12 ` [PATCH 0/2] some cleanups related to RB_DECLARE_CALLBACKS_MAX Michel Lespinasse
2019-08-13 9:29 ` Uladzislau Rezki
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20190811184613.20463-2-urezki@gmail.com \
--to=urezki@gmail.com \
--cc=akpm@linux-foundation.org \
--cc=guro@fb.com \
--cc=hdanton@sina.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=mhocko@suse.com \
--cc=oleksiy.avramchenko@sonymobile.com \
--cc=peterz@infradead.org \
--cc=rostedt@goodmis.org \
--cc=walken@google.com \
--cc=willy@infradead.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).