0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017 #ifndef __TOOLS_LINUX_PERF_RBTREE_H
0018 #define __TOOLS_LINUX_PERF_RBTREE_H
0019
0020 #include <linux/kernel.h>
0021 #include <linux/stddef.h>
0022
0023 struct rb_node {
0024 unsigned long __rb_parent_color;
0025 struct rb_node *rb_right;
0026 struct rb_node *rb_left;
0027 } __attribute__((aligned(sizeof(long))));
0028
0029
0030 struct rb_root {
0031 struct rb_node *rb_node;
0032 };
0033
0034 #define rb_parent(r) ((struct rb_node *)((r)->__rb_parent_color & ~3))
0035
0036 #define RB_ROOT (struct rb_root) { NULL, }
0037 #define rb_entry(ptr, type, member) container_of(ptr, type, member)
0038
0039 #define RB_EMPTY_ROOT(root) (READ_ONCE((root)->rb_node) == NULL)
0040
0041
0042 #define RB_EMPTY_NODE(node) \
0043 ((node)->__rb_parent_color == (unsigned long)(node))
0044 #define RB_CLEAR_NODE(node) \
0045 ((node)->__rb_parent_color = (unsigned long)(node))
0046
0047
0048 extern void rb_insert_color(struct rb_node *, struct rb_root *);
0049 extern void rb_erase(struct rb_node *, struct rb_root *);
0050
0051
0052
0053 extern struct rb_node *rb_next(const struct rb_node *);
0054 extern struct rb_node *rb_prev(const struct rb_node *);
0055 extern struct rb_node *rb_first(const struct rb_root *);
0056 extern struct rb_node *rb_last(const struct rb_root *);
0057
0058
0059 extern struct rb_node *rb_first_postorder(const struct rb_root *);
0060 extern struct rb_node *rb_next_postorder(const struct rb_node *);
0061
0062
0063 extern void rb_replace_node(struct rb_node *victim, struct rb_node *new,
0064 struct rb_root *root);
0065
0066 static inline void rb_link_node(struct rb_node *node, struct rb_node *parent,
0067 struct rb_node **rb_link)
0068 {
0069 node->__rb_parent_color = (unsigned long)parent;
0070 node->rb_left = node->rb_right = NULL;
0071
0072 *rb_link = node;
0073 }
0074
0075 #define rb_entry_safe(ptr, type, member) \
0076 ({ typeof(ptr) ____ptr = (ptr); \
0077 ____ptr ? rb_entry(____ptr, type, member) : NULL; \
0078 })
0079
0080
0081
0082
0083
0084
0085
0086
0087
0088
0089
0090
0091
0092
0093
0094
0095
0096
0097 #define rbtree_postorder_for_each_entry_safe(pos, n, root, field) \
0098 for (pos = rb_entry_safe(rb_first_postorder(root), typeof(*pos), field); \
0099 pos && ({ n = rb_entry_safe(rb_next_postorder(&pos->field), \
0100 typeof(*pos), field); 1; }); \
0101 pos = n)
0102
0103 static inline void rb_erase_init(struct rb_node *n, struct rb_root *root)
0104 {
0105 rb_erase(n, root);
0106 RB_CLEAR_NODE(n);
0107 }
0108
0109
0110
0111
0112
0113
0114
0115
0116
0117
0118
0119 struct rb_root_cached {
0120 struct rb_root rb_root;
0121 struct rb_node *rb_leftmost;
0122 };
0123
0124 #define RB_ROOT_CACHED (struct rb_root_cached) { {NULL, }, NULL }
0125
0126
0127 #define rb_first_cached(root) (root)->rb_leftmost
0128
0129 static inline void rb_insert_color_cached(struct rb_node *node,
0130 struct rb_root_cached *root,
0131 bool leftmost)
0132 {
0133 if (leftmost)
0134 root->rb_leftmost = node;
0135 rb_insert_color(node, &root->rb_root);
0136 }
0137
0138 static inline void rb_erase_cached(struct rb_node *node,
0139 struct rb_root_cached *root)
0140 {
0141 if (root->rb_leftmost == node)
0142 root->rb_leftmost = rb_next(node);
0143 rb_erase(node, &root->rb_root);
0144 }
0145
0146 static inline void rb_replace_node_cached(struct rb_node *victim,
0147 struct rb_node *new,
0148 struct rb_root_cached *root)
0149 {
0150 if (root->rb_leftmost == victim)
0151 root->rb_leftmost = new;
0152 rb_replace_node(victim, new, &root->rb_root);
0153 }
0154
0155
0156
0157
0158
0159
0160
0161
0162
0163
0164
0165
0166
0167
0168
0169
0170
0171
0172
0173
0174
0175
0176
0177 static __always_inline void
0178 rb_add_cached(struct rb_node *node, struct rb_root_cached *tree,
0179 bool (*less)(struct rb_node *, const struct rb_node *))
0180 {
0181 struct rb_node **link = &tree->rb_root.rb_node;
0182 struct rb_node *parent = NULL;
0183 bool leftmost = true;
0184
0185 while (*link) {
0186 parent = *link;
0187 if (less(node, parent)) {
0188 link = &parent->rb_left;
0189 } else {
0190 link = &parent->rb_right;
0191 leftmost = false;
0192 }
0193 }
0194
0195 rb_link_node(node, parent, link);
0196 rb_insert_color_cached(node, tree, leftmost);
0197 }
0198
0199
0200
0201
0202
0203
0204
0205 static __always_inline void
0206 rb_add(struct rb_node *node, struct rb_root *tree,
0207 bool (*less)(struct rb_node *, const struct rb_node *))
0208 {
0209 struct rb_node **link = &tree->rb_node;
0210 struct rb_node *parent = NULL;
0211
0212 while (*link) {
0213 parent = *link;
0214 if (less(node, parent))
0215 link = &parent->rb_left;
0216 else
0217 link = &parent->rb_right;
0218 }
0219
0220 rb_link_node(node, parent, link);
0221 rb_insert_color(node, tree);
0222 }
0223
0224
0225
0226
0227
0228
0229
0230
0231
0232
0233 static __always_inline struct rb_node *
0234 rb_find_add(struct rb_node *node, struct rb_root *tree,
0235 int (*cmp)(struct rb_node *, const struct rb_node *))
0236 {
0237 struct rb_node **link = &tree->rb_node;
0238 struct rb_node *parent = NULL;
0239 int c;
0240
0241 while (*link) {
0242 parent = *link;
0243 c = cmp(node, parent);
0244
0245 if (c < 0)
0246 link = &parent->rb_left;
0247 else if (c > 0)
0248 link = &parent->rb_right;
0249 else
0250 return parent;
0251 }
0252
0253 rb_link_node(node, parent, link);
0254 rb_insert_color(node, tree);
0255 return NULL;
0256 }
0257
0258
0259
0260
0261
0262
0263
0264
0265
0266 static __always_inline struct rb_node *
0267 rb_find(const void *key, const struct rb_root *tree,
0268 int (*cmp)(const void *key, const struct rb_node *))
0269 {
0270 struct rb_node *node = tree->rb_node;
0271
0272 while (node) {
0273 int c = cmp(key, node);
0274
0275 if (c < 0)
0276 node = node->rb_left;
0277 else if (c > 0)
0278 node = node->rb_right;
0279 else
0280 return node;
0281 }
0282
0283 return NULL;
0284 }
0285
0286
0287
0288
0289
0290
0291
0292
0293
0294 static __always_inline struct rb_node *
0295 rb_find_first(const void *key, const struct rb_root *tree,
0296 int (*cmp)(const void *key, const struct rb_node *))
0297 {
0298 struct rb_node *node = tree->rb_node;
0299 struct rb_node *match = NULL;
0300
0301 while (node) {
0302 int c = cmp(key, node);
0303
0304 if (c <= 0) {
0305 if (!c)
0306 match = node;
0307 node = node->rb_left;
0308 } else if (c > 0) {
0309 node = node->rb_right;
0310 }
0311 }
0312
0313 return match;
0314 }
0315
0316
0317
0318
0319
0320
0321
0322
0323
0324 static __always_inline struct rb_node *
0325 rb_next_match(const void *key, struct rb_node *node,
0326 int (*cmp)(const void *key, const struct rb_node *))
0327 {
0328 node = rb_next(node);
0329 if (node && cmp(key, node))
0330 node = NULL;
0331 return node;
0332 }
0333
0334
0335
0336
0337
0338
0339
0340
0341 #define rb_for_each(node, key, tree, cmp) \
0342 for ((node) = rb_find_first((key), (tree), (cmp)); \
0343 (node); (node) = rb_next_match((key), (node), (cmp)))
0344
0345 #endif