linux/tools/include/linux/rbtree.h
<<
>>
Prefs
   1/*
   2  Red Black Trees
   3  (C) 1999  Andrea Arcangeli <andrea@suse.de>
   4
   5  This program is free software; you can redistribute it and/or modify
   6  it under the terms of the GNU General Public License as published by
   7  the Free Software Foundation; either version 2 of the License, or
   8  (at your option) any later version.
   9
  10  This program is distributed in the hope that it will be useful,
  11  but WITHOUT ANY WARRANTY; without even the implied warranty of
  12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13  GNU General Public License for more details.
  14
  15  You should have received a copy of the GNU General Public License
  16  along with this program; if not, write to the Free Software
  17  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  18
  19  linux/include/linux/rbtree.h
  20
  21  To use rbtrees you'll have to implement your own insert and search cores.
  22  This will avoid us to use callbacks and to drop drammatically performances.
  23  I know it's not the cleaner way,  but in C (not in C++) to get
  24  performances and genericity...
  25
  26  See Documentation/rbtree.txt for documentation and samples.
  27*/
  28
  29#ifndef __TOOLS_LINUX_PERF_RBTREE_H
  30#define __TOOLS_LINUX_PERF_RBTREE_H
  31
  32#include <linux/kernel.h>
  33#include <linux/stddef.h>
  34
  35struct rb_node {
  36        unsigned long  __rb_parent_color;
  37        struct rb_node *rb_right;
  38        struct rb_node *rb_left;
  39} __attribute__((aligned(sizeof(long))));
  40    /* The alignment might seem pointless, but allegedly CRIS needs it */
  41
  42struct rb_root {
  43        struct rb_node *rb_node;
  44};
  45
  46#define rb_parent(r)   ((struct rb_node *)((r)->__rb_parent_color & ~3))
  47
  48#define RB_ROOT (struct rb_root) { NULL, }
  49#define rb_entry(ptr, type, member) container_of(ptr, type, member)
  50
  51#define RB_EMPTY_ROOT(root)  (READ_ONCE((root)->rb_node) == NULL)
  52
  53/* 'empty' nodes are nodes that are known not to be inserted in an rbtree */
  54#define RB_EMPTY_NODE(node)  \
  55        ((node)->__rb_parent_color == (unsigned long)(node))
  56#define RB_CLEAR_NODE(node)  \
  57        ((node)->__rb_parent_color = (unsigned long)(node))
  58
  59
  60extern void rb_insert_color(struct rb_node *, struct rb_root *);
  61extern void rb_erase(struct rb_node *, struct rb_root *);
  62
  63
  64/* Find logical next and previous nodes in a tree */
  65extern struct rb_node *rb_next(const struct rb_node *);
  66extern struct rb_node *rb_prev(const struct rb_node *);
  67extern struct rb_node *rb_first(const struct rb_root *);
  68extern struct rb_node *rb_last(const struct rb_root *);
  69
  70/* Postorder iteration - always visit the parent after its children */
  71extern struct rb_node *rb_first_postorder(const struct rb_root *);
  72extern struct rb_node *rb_next_postorder(const struct rb_node *);
  73
  74/* Fast replacement of a single node without remove/rebalance/add/rebalance */
  75extern void rb_replace_node(struct rb_node *victim, struct rb_node *new,
  76                            struct rb_root *root);
  77
  78static inline void rb_link_node(struct rb_node *node, struct rb_node *parent,
  79                                struct rb_node **rb_link)
  80{
  81        node->__rb_parent_color = (unsigned long)parent;
  82        node->rb_left = node->rb_right = NULL;
  83
  84        *rb_link = node;
  85}
  86
  87#define rb_entry_safe(ptr, type, member) \
  88        ({ typeof(ptr) ____ptr = (ptr); \
  89           ____ptr ? rb_entry(____ptr, type, member) : NULL; \
  90        })
  91
  92/**
  93 * rbtree_postorder_for_each_entry_safe - iterate in post-order over rb_root of
  94 * given type allowing the backing memory of @pos to be invalidated
  95 *
  96 * @pos:        the 'type *' to use as a loop cursor.
  97 * @n:          another 'type *' to use as temporary storage
  98 * @root:       'rb_root *' of the rbtree.
  99 * @field:      the name of the rb_node field within 'type'.
 100 *
 101 * rbtree_postorder_for_each_entry_safe() provides a similar guarantee as
 102 * list_for_each_entry_safe() and allows the iteration to continue independent
 103 * of changes to @pos by the body of the loop.
 104 *
 105 * Note, however, that it cannot handle other modifications that re-order the
 106 * rbtree it is iterating over. This includes calling rb_erase() on @pos, as
 107 * rb_erase() may rebalance the tree, causing us to miss some nodes.
 108 */
 109#define rbtree_postorder_for_each_entry_safe(pos, n, root, field) \
 110        for (pos = rb_entry_safe(rb_first_postorder(root), typeof(*pos), field); \
 111             pos && ({ n = rb_entry_safe(rb_next_postorder(&pos->field), \
 112                        typeof(*pos), field); 1; }); \
 113             pos = n)
 114
 115static inline void rb_erase_init(struct rb_node *n, struct rb_root *root)
 116{
 117        rb_erase(n, root);
 118        RB_CLEAR_NODE(n);
 119}
 120
 121/*
 122 * Leftmost-cached rbtrees.
 123 *
 124 * We do not cache the rightmost node based on footprint
 125 * size vs number of potential users that could benefit
 126 * from O(1) rb_last(). Just not worth it, users that want
 127 * this feature can always implement the logic explicitly.
 128 * Furthermore, users that want to cache both pointers may
 129 * find it a bit asymmetric, but that's ok.
 130 */
 131struct rb_root_cached {
 132        struct rb_root rb_root;
 133        struct rb_node *rb_leftmost;
 134};
 135
 136#define RB_ROOT_CACHED (struct rb_root_cached) { {NULL, }, NULL }
 137
 138/* Same as rb_first(), but O(1) */
 139#define rb_first_cached(root) (root)->rb_leftmost
 140
 141static inline void rb_insert_color_cached(struct rb_node *node,
 142                                          struct rb_root_cached *root,
 143                                          bool leftmost)
 144{
 145        if (leftmost)
 146                root->rb_leftmost = node;
 147        rb_insert_color(node, &root->rb_root);
 148}
 149
 150static inline void rb_erase_cached(struct rb_node *node,
 151                                   struct rb_root_cached *root)
 152{
 153        if (root->rb_leftmost == node)
 154                root->rb_leftmost = rb_next(node);
 155        rb_erase(node, &root->rb_root);
 156}
 157
 158static inline void rb_replace_node_cached(struct rb_node *victim,
 159                                          struct rb_node *new,
 160                                          struct rb_root_cached *root)
 161{
 162        if (root->rb_leftmost == victim)
 163                root->rb_leftmost = new;
 164        rb_replace_node(victim, new, &root->rb_root);
 165}
 166
 167/*
 168 * The below helper functions use 2 operators with 3 different
 169 * calling conventions. The operators are related like:
 170 *
 171 *      comp(a->key,b) < 0  := less(a,b)
 172 *      comp(a->key,b) > 0  := less(b,a)
 173 *      comp(a->key,b) == 0 := !less(a,b) && !less(b,a)
 174 *
 175 * If these operators define a partial order on the elements we make no
 176 * guarantee on which of the elements matching the key is found. See
 177 * rb_find().
 178 *
 179 * The reason for this is to allow the find() interface without requiring an
 180 * on-stack dummy object, which might not be feasible due to object size.
 181 */
 182
 183/**
 184 * rb_add_cached() - insert @node into the leftmost cached tree @tree
 185 * @node: node to insert
 186 * @tree: leftmost cached tree to insert @node into
 187 * @less: operator defining the (partial) node order
 188 */
 189static __always_inline void
 190rb_add_cached(struct rb_node *node, struct rb_root_cached *tree,
 191              bool (*less)(struct rb_node *, const struct rb_node *))
 192{
 193        struct rb_node **link = &tree->rb_root.rb_node;
 194        struct rb_node *parent = NULL;
 195        bool leftmost = true;
 196
 197        while (*link) {
 198                parent = *link;
 199                if (less(node, parent)) {
 200                        link = &parent->rb_left;
 201                } else {
 202                        link = &parent->rb_right;
 203                        leftmost = false;
 204                }
 205        }
 206
 207        rb_link_node(node, parent, link);
 208        rb_insert_color_cached(node, tree, leftmost);
 209}
 210
 211/**
 212 * rb_add() - insert @node into @tree
 213 * @node: node to insert
 214 * @tree: tree to insert @node into
 215 * @less: operator defining the (partial) node order
 216 */
 217static __always_inline void
 218rb_add(struct rb_node *node, struct rb_root *tree,
 219       bool (*less)(struct rb_node *, const struct rb_node *))
 220{
 221        struct rb_node **link = &tree->rb_node;
 222        struct rb_node *parent = NULL;
 223
 224        while (*link) {
 225                parent = *link;
 226                if (less(node, parent))
 227                        link = &parent->rb_left;
 228                else
 229                        link = &parent->rb_right;
 230        }
 231
 232        rb_link_node(node, parent, link);
 233        rb_insert_color(node, tree);
 234}
 235
 236/**
 237 * rb_find_add() - find equivalent @node in @tree, or add @node
 238 * @node: node to look-for / insert
 239 * @tree: tree to search / modify
 240 * @cmp: operator defining the node order
 241 *
 242 * Returns the rb_node matching @node, or NULL when no match is found and @node
 243 * is inserted.
 244 */
 245static __always_inline struct rb_node *
 246rb_find_add(struct rb_node *node, struct rb_root *tree,
 247            int (*cmp)(struct rb_node *, const struct rb_node *))
 248{
 249        struct rb_node **link = &tree->rb_node;
 250        struct rb_node *parent = NULL;
 251        int c;
 252
 253        while (*link) {
 254                parent = *link;
 255                c = cmp(node, parent);
 256
 257                if (c < 0)
 258                        link = &parent->rb_left;
 259                else if (c > 0)
 260                        link = &parent->rb_right;
 261                else
 262                        return parent;
 263        }
 264
 265        rb_link_node(node, parent, link);
 266        rb_insert_color(node, tree);
 267        return NULL;
 268}
 269
 270/**
 271 * rb_find() - find @key in tree @tree
 272 * @key: key to match
 273 * @tree: tree to search
 274 * @cmp: operator defining the node order
 275 *
 276 * Returns the rb_node matching @key or NULL.
 277 */
 278static __always_inline struct rb_node *
 279rb_find(const void *key, const struct rb_root *tree,
 280        int (*cmp)(const void *key, const struct rb_node *))
 281{
 282        struct rb_node *node = tree->rb_node;
 283
 284        while (node) {
 285                int c = cmp(key, node);
 286
 287                if (c < 0)
 288                        node = node->rb_left;
 289                else if (c > 0)
 290                        node = node->rb_right;
 291                else
 292                        return node;
 293        }
 294
 295        return NULL;
 296}
 297
 298/**
 299 * rb_find_first() - find the first @key in @tree
 300 * @key: key to match
 301 * @tree: tree to search
 302 * @cmp: operator defining node order
 303 *
 304 * Returns the leftmost node matching @key, or NULL.
 305 */
 306static __always_inline struct rb_node *
 307rb_find_first(const void *key, const struct rb_root *tree,
 308              int (*cmp)(const void *key, const struct rb_node *))
 309{
 310        struct rb_node *node = tree->rb_node;
 311        struct rb_node *match = NULL;
 312
 313        while (node) {
 314                int c = cmp(key, node);
 315
 316                if (c <= 0) {
 317                        if (!c)
 318                                match = node;
 319                        node = node->rb_left;
 320                } else if (c > 0) {
 321                        node = node->rb_right;
 322                }
 323        }
 324
 325        return match;
 326}
 327
 328/**
 329 * rb_next_match() - find the next @key in @tree
 330 * @key: key to match
 331 * @tree: tree to search
 332 * @cmp: operator defining node order
 333 *
 334 * Returns the next node matching @key, or NULL.
 335 */
 336static __always_inline struct rb_node *
 337rb_next_match(const void *key, struct rb_node *node,
 338              int (*cmp)(const void *key, const struct rb_node *))
 339{
 340        node = rb_next(node);
 341        if (node && cmp(key, node))
 342                node = NULL;
 343        return node;
 344}
 345
 346/**
 347 * rb_for_each() - iterates a subtree matching @key
 348 * @node: iterator
 349 * @key: key to match
 350 * @tree: tree to search
 351 * @cmp: operator defining node order
 352 */
 353#define rb_for_each(node, key, tree, cmp) \
 354        for ((node) = rb_find_first((key), (tree), (cmp)); \
 355             (node); (node) = rb_next_match((key), (node), (cmp)))
 356
 357#endif  /* __TOOLS_LINUX_PERF_RBTREE_H */
 358