Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0-or-later */
0002 /*
0003   Red Black Trees
0004   (C) 1999  Andrea Arcangeli <andrea@suse.de>
0005 
0006 
0007   linux/include/linux/rbtree.h
0008 
0009   To use rbtrees you'll have to implement your own insert and search cores.
0010   This will avoid us to use callbacks and to drop drammatically performances.
0011   I know it's not the cleaner way,  but in C (not in C++) to get
0012   performances and genericity...
0013 
0014   See Documentation/core-api/rbtree.rst for documentation and samples.
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     /* The alignment might seem pointless, but allegedly CRIS needs it */
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 /* 'empty' nodes are nodes that are known not to be inserted in an rbtree */
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 /* Find logical next and previous nodes in a tree */
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 /* Postorder iteration - always visit the parent after its children */
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 /* Fast replacement of a single node without remove/rebalance/add/rebalance */
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  * rbtree_postorder_for_each_entry_safe - iterate in post-order over rb_root of
0082  * given type allowing the backing memory of @pos to be invalidated
0083  *
0084  * @pos:    the 'type *' to use as a loop cursor.
0085  * @n:      another 'type *' to use as temporary storage
0086  * @root:   'rb_root *' of the rbtree.
0087  * @field:  the name of the rb_node field within 'type'.
0088  *
0089  * rbtree_postorder_for_each_entry_safe() provides a similar guarantee as
0090  * list_for_each_entry_safe() and allows the iteration to continue independent
0091  * of changes to @pos by the body of the loop.
0092  *
0093  * Note, however, that it cannot handle other modifications that re-order the
0094  * rbtree it is iterating over. This includes calling rb_erase() on @pos, as
0095  * rb_erase() may rebalance the tree, causing us to miss some nodes.
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  * Leftmost-cached rbtrees.
0111  *
0112  * We do not cache the rightmost node based on footprint
0113  * size vs number of potential users that could benefit
0114  * from O(1) rb_last(). Just not worth it, users that want
0115  * this feature can always implement the logic explicitly.
0116  * Furthermore, users that want to cache both pointers may
0117  * find it a bit asymmetric, but that's ok.
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 /* Same as rb_first(), but O(1) */
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  * The below helper functions use 2 operators with 3 different
0157  * calling conventions. The operators are related like:
0158  *
0159  *  comp(a->key,b) < 0  := less(a,b)
0160  *  comp(a->key,b) > 0  := less(b,a)
0161  *  comp(a->key,b) == 0 := !less(a,b) && !less(b,a)
0162  *
0163  * If these operators define a partial order on the elements we make no
0164  * guarantee on which of the elements matching the key is found. See
0165  * rb_find().
0166  *
0167  * The reason for this is to allow the find() interface without requiring an
0168  * on-stack dummy object, which might not be feasible due to object size.
0169  */
0170 
0171 /**
0172  * rb_add_cached() - insert @node into the leftmost cached tree @tree
0173  * @node: node to insert
0174  * @tree: leftmost cached tree to insert @node into
0175  * @less: operator defining the (partial) node order
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  * rb_add() - insert @node into @tree
0201  * @node: node to insert
0202  * @tree: tree to insert @node into
0203  * @less: operator defining the (partial) node order
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  * rb_find_add() - find equivalent @node in @tree, or add @node
0226  * @node: node to look-for / insert
0227  * @tree: tree to search / modify
0228  * @cmp: operator defining the node order
0229  *
0230  * Returns the rb_node matching @node, or NULL when no match is found and @node
0231  * is inserted.
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  * rb_find() - find @key in tree @tree
0260  * @key: key to match
0261  * @tree: tree to search
0262  * @cmp: operator defining the node order
0263  *
0264  * Returns the rb_node matching @key or NULL.
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  * rb_find_first() - find the first @key in @tree
0288  * @key: key to match
0289  * @tree: tree to search
0290  * @cmp: operator defining node order
0291  *
0292  * Returns the leftmost node matching @key, or NULL.
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  * rb_next_match() - find the next @key in @tree
0318  * @key: key to match
0319  * @tree: tree to search
0320  * @cmp: operator defining node order
0321  *
0322  * Returns the next node matching @key, or NULL.
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  * rb_for_each() - iterates a subtree matching @key
0336  * @node: iterator
0337  * @key: key to match
0338  * @tree: tree to search
0339  * @cmp: operator defining node order
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  /* __TOOLS_LINUX_PERF_RBTREE_H */