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