Skip to content

Commit 6d2052d

Browse files
walken-googletorvalds
authored andcommitted
augmented rbtree: rework the RB_DECLARE_CALLBACKS macro definition
Change the definition of the RBCOMPUTE function. The propagate callback repeatedly calls RBCOMPUTE as it moves from leaf to root. it wants to stop recomputing once the augmented subtree information doesn't change. This was previously checked using the == operator, but that only works when the augmented subtree information is a scalar field. This commit modifies the RBCOMPUTE function so that it now sets the augmented subtree information instead of returning it, and returns a boolean value indicating if the propagate callback should stop. The motivation for this change is that I want to introduce augmented rbtree uses where the augmented data for the subtree is a struct instead of a scalar. Link: http://lkml.kernel.org/r/20190703040156.56953-4-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>
1 parent 315cc06 commit 6d2052d

2 files changed

Lines changed: 24 additions & 24 deletions

File tree

‎include/linux/rbtree_augmented.h‎

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -67,22 +67,19 @@ rb_insert_augmented_cached(struct rb_node *node,
6767
* RBNAME: name of the rb_augment_callbacks structure
6868
* RBSTRUCT: struct type of the tree nodes
6969
* RBFIELD: name of struct rb_node field within RBSTRUCT
70-
* RBTYPE: type of the RBAUGMENTED field
71-
* RBAUGMENTED: name of RBTYPE field within RBSTRUCT holding data for subtree
70+
* RBAUGMENTED: name of field within RBSTRUCT holding data for subtree
7271
* RBCOMPUTE: name of function that recomputes the RBAUGMENTED data
7372
*/
7473

75-
#define RB_DECLARE_CALLBACKS(RBSTATIC, RBNAME, RBSTRUCT, RBFIELD, \
76-
RBTYPE, RBAUGMENTED, RBCOMPUTE) \
74+
#define RB_DECLARE_CALLBACKS(RBSTATIC, RBNAME, \
75+
RBSTRUCT, RBFIELD, RBAUGMENTED, RBCOMPUTE) \
7776
static inline void \
7877
RBNAME ## _propagate(struct rb_node *rb, struct rb_node *stop) \
7978
{ \
8079
while (rb != stop) { \
8180
RBSTRUCT *node = rb_entry(rb, RBSTRUCT, RBFIELD); \
82-
RBTYPE augmented = RBCOMPUTE(node); \
83-
if (node->RBAUGMENTED == augmented) \
81+
if (RBCOMPUTE(node, true)) \
8482
break; \
85-
node->RBAUGMENTED = augmented; \
8683
rb = rb_parent(&node->RBFIELD); \
8784
} \
8885
} \
@@ -99,7 +96,7 @@ RBNAME ## _rotate(struct rb_node *rb_old, struct rb_node *rb_new) \
9996
RBSTRUCT *old = rb_entry(rb_old, RBSTRUCT, RBFIELD); \
10097
RBSTRUCT *new = rb_entry(rb_new, RBSTRUCT, RBFIELD); \
10198
new->RBAUGMENTED = old->RBAUGMENTED; \
102-
old->RBAUGMENTED = RBCOMPUTE(old); \
99+
RBCOMPUTE(old, false); \
103100
} \
104101
RBSTATIC const struct rb_augment_callbacks RBNAME = { \
105102
.propagate = RBNAME ## _propagate, \
@@ -122,7 +119,7 @@ RBSTATIC const struct rb_augment_callbacks RBNAME = { \
122119

123120
#define RB_DECLARE_CALLBACKS_MAX(RBSTATIC, RBNAME, RBSTRUCT, RBFIELD, \
124121
RBTYPE, RBAUGMENTED, RBCOMPUTE) \
125-
static inline RBTYPE RBNAME ## _compute_max(RBSTRUCT *node) \
122+
static inline bool RBNAME ## _compute_max(RBSTRUCT *node, bool exit) \
126123
{ \
127124
RBSTRUCT *child; \
128125
RBTYPE max = RBCOMPUTE(node); \
@@ -136,10 +133,13 @@ static inline RBTYPE RBNAME ## _compute_max(RBSTRUCT *node) \
136133
if (child->RBAUGMENTED > max) \
137134
max = child->RBAUGMENTED; \
138135
} \
139-
return max; \
136+
if (exit && node->RBAUGMENTED == max) \
137+
return true; \
138+
node->RBAUGMENTED = max; \
139+
return false; \
140140
} \
141-
RB_DECLARE_CALLBACKS(RBSTATIC, RBNAME, RBSTRUCT, RBFIELD, \
142-
RBTYPE, RBAUGMENTED, RBNAME ## _compute_max)
141+
RB_DECLARE_CALLBACKS(RBSTATIC, RBNAME, \
142+
RBSTRUCT, RBFIELD, RBAUGMENTED, RBNAME ## _compute_max)
143143

144144

145145
#define RB_RED 0

‎tools/include/linux/rbtree_augmented.h‎

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -69,22 +69,19 @@ rb_insert_augmented_cached(struct rb_node *node,
6969
* RBNAME: name of the rb_augment_callbacks structure
7070
* RBSTRUCT: struct type of the tree nodes
7171
* RBFIELD: name of struct rb_node field within RBSTRUCT
72-
* RBTYPE: type of the RBAUGMENTED field
73-
* RBAUGMENTED: name of RBTYPE field within RBSTRUCT holding data for subtree
72+
* RBAUGMENTED: name of field within RBSTRUCT holding data for subtree
7473
* RBCOMPUTE: name of function that recomputes the RBAUGMENTED data
7574
*/
7675

77-
#define RB_DECLARE_CALLBACKS(RBSTATIC, RBNAME, RBSTRUCT, RBFIELD, \
78-
RBTYPE, RBAUGMENTED, RBCOMPUTE) \
76+
#define RB_DECLARE_CALLBACKS(RBSTATIC, RBNAME, \
77+
RBSTRUCT, RBFIELD, RBAUGMENTED, RBCOMPUTE) \
7978
static inline void \
8079
RBNAME ## _propagate(struct rb_node *rb, struct rb_node *stop) \
8180
{ \
8281
while (rb != stop) { \
8382
RBSTRUCT *node = rb_entry(rb, RBSTRUCT, RBFIELD); \
84-
RBTYPE augmented = RBCOMPUTE(node); \
85-
if (node->RBAUGMENTED == augmented) \
83+
if (RBCOMPUTE(node, true)) \
8684
break; \
87-
node->RBAUGMENTED = augmented; \
8885
rb = rb_parent(&node->RBFIELD); \
8986
} \
9087
} \
@@ -101,7 +98,7 @@ RBNAME ## _rotate(struct rb_node *rb_old, struct rb_node *rb_new) \
10198
RBSTRUCT *old = rb_entry(rb_old, RBSTRUCT, RBFIELD); \
10299
RBSTRUCT *new = rb_entry(rb_new, RBSTRUCT, RBFIELD); \
103100
new->RBAUGMENTED = old->RBAUGMENTED; \
104-
old->RBAUGMENTED = RBCOMPUTE(old); \
101+
RBCOMPUTE(old, false); \
105102
} \
106103
RBSTATIC const struct rb_augment_callbacks RBNAME = { \
107104
.propagate = RBNAME ## _propagate, \
@@ -124,7 +121,7 @@ RBSTATIC const struct rb_augment_callbacks RBNAME = { \
124121

125122
#define RB_DECLARE_CALLBACKS_MAX(RBSTATIC, RBNAME, RBSTRUCT, RBFIELD, \
126123
RBTYPE, RBAUGMENTED, RBCOMPUTE) \
127-
static inline RBTYPE RBNAME ## _compute_max(RBSTRUCT *node) \
124+
static inline bool RBNAME ## _compute_max(RBSTRUCT *node, bool exit) \
128125
{ \
129126
RBSTRUCT *child; \
130127
RBTYPE max = RBCOMPUTE(node); \
@@ -138,10 +135,13 @@ static inline RBTYPE RBNAME ## _compute_max(RBSTRUCT *node) \
138135
if (child->RBAUGMENTED > max) \
139136
max = child->RBAUGMENTED; \
140137
} \
141-
return max; \
138+
if (exit && node->RBAUGMENTED == max) \
139+
return true; \
140+
node->RBAUGMENTED = max; \
141+
return false; \
142142
} \
143-
RB_DECLARE_CALLBACKS(RBSTATIC, RBNAME, RBSTRUCT, RBFIELD, \
144-
RBTYPE, RBAUGMENTED, RBNAME ## _compute_max)
143+
RB_DECLARE_CALLBACKS(RBSTATIC, RBNAME, \
144+
RBSTRUCT, RBFIELD, RBAUGMENTED, RBNAME ## _compute_max)
145145

146146

147147
#define RB_RED 0

0 commit comments

Comments
 (0)