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 _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 /* 'empty' nodes are nodes that are known not to be inserted in an rbtree */
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 /* Find logical next and previous nodes in a tree */
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 /* Postorder iteration - always visit the parent after its children */
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 /* Fast replacement of a single node without remove/rebalance/add/rebalance */
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  * rbtree_postorder_for_each_entry_safe - iterate in post-order over rb_root of
0084  * given type allowing the backing memory of @pos to be invalidated
0085  *
0086  * @pos:    the 'type *' to use as a loop cursor.
0087  * @n:      another 'type *' to use as temporary storage
0088  * @root:   'rb_root *' of the rbtree.
0089  * @field:  the name of the rb_node field within 'type'.
0090  *
0091  * rbtree_postorder_for_each_entry_safe() provides a similar guarantee as
0092  * list_for_each_entry_safe() and allows the iteration to continue independent
0093  * of changes to @pos by the body of the loop.
0094  *
0095  * Note, however, that it cannot handle other modifications that re-order the
0096  * rbtree it is iterating over. This includes calling rb_erase() on @pos, as
0097  * rb_erase() may rebalance the tree, causing us to miss some nodes.
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 /* Same as rb_first(), but O(1) */
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  * The below helper functions use 2 operators with 3 different
0142  * calling conventions. The operators are related like:
0143  *
0144  *  comp(a->key,b) < 0  := less(a,b)
0145  *  comp(a->key,b) > 0  := less(b,a)
0146  *  comp(a->key,b) == 0 := !less(a,b) && !less(b,a)
0147  *
0148  * If these operators define a partial order on the elements we make no
0149  * guarantee on which of the elements matching the key is found. See
0150  * rb_find().
0151  *
0152  * The reason for this is to allow the find() interface without requiring an
0153  * on-stack dummy object, which might not be feasible due to object size.
0154  */
0155 
0156 /**
0157  * rb_add_cached() - insert @node into the leftmost cached tree @tree
0158  * @node: node to insert
0159  * @tree: leftmost cached tree to insert @node into
0160  * @less: operator defining the (partial) node order
0161  *
0162  * Returns @node when it is the new leftmost, or NULL.
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  * rb_add() - insert @node into @tree
0190  * @node: node to insert
0191  * @tree: tree to insert @node into
0192  * @less: operator defining the (partial) node order
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  * rb_find_add() - find equivalent @node in @tree, or add @node
0215  * @node: node to look-for / insert
0216  * @tree: tree to search / modify
0217  * @cmp: operator defining the node order
0218  *
0219  * Returns the rb_node matching @node, or NULL when no match is found and @node
0220  * is inserted.
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  * rb_find() - find @key in tree @tree
0249  * @key: key to match
0250  * @tree: tree to search
0251  * @cmp: operator defining the node order
0252  *
0253  * Returns the rb_node matching @key or NULL.
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  * rb_find_first() - find the first @key in @tree
0277  * @key: key to match
0278  * @tree: tree to search
0279  * @cmp: operator defining node order
0280  *
0281  * Returns the leftmost node matching @key, or NULL.
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  * rb_next_match() - find the next @key in @tree
0307  * @key: key to match
0308  * @tree: tree to search
0309  * @cmp: operator defining node order
0310  *
0311  * Returns the next node matching @key, or NULL.
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  * rb_for_each() - iterates a subtree matching @key
0325  * @node: iterator
0326  * @key: key to match
0327  * @tree: tree to search
0328  * @cmp: operator defining node order
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  /* _LINUX_RBTREE_H */