The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/sys/rb.h

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

    1 /* $NetBSD: rb.h,v 1.12 2008/08/05 04:12:09 matt Exp $ */
    2 
    3 /*-
    4  * Copyright (c) 2001 The NetBSD Foundation, Inc.
    5  * All rights reserved.
    6  *
    7  * This code is derived from software contributed to The NetBSD Foundation
    8  * by Matt Thomas <matt@3am-software.com>.
    9  *
   10  * Redistribution and use in source and binary forms, with or without
   11  * modification, are permitted provided that the following conditions
   12  * are met:
   13  * 1. Redistributions of source code must retain the above copyright
   14  *    notice, this list of conditions and the following disclaimer.
   15  * 2. Redistributions in binary form must reproduce the above copyright
   16  *    notice, this list of conditions and the following disclaimer in the
   17  *    documentation and/or other materials provided with the distribution.
   18  *
   19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
   20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
   21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
   22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
   23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
   24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
   25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
   26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
   27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
   28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
   29  * POSSIBILITY OF SUCH DAMAGE.
   30  */
   31 #ifndef _SYS_RB_H_
   32 #define _SYS_RB_H_
   33 
   34 #if defined(_KERNEL) || defined(_STANDALONE)
   35 #include <sys/types.h>
   36 #else
   37 #include <stdbool.h>
   38 #include <inttypes.h>
   39 #endif
   40 #include <sys/queue.h>
   41 #include <sys/endian.h>
   42 
   43 struct rb_node {
   44         struct rb_node *rb_nodes[2];
   45 #define RB_DIR_LEFT             0
   46 #define RB_DIR_RIGHT            1
   47 #define RB_DIR_OTHER            1
   48 #define rb_left                 rb_nodes[RB_DIR_LEFT]
   49 #define rb_right                rb_nodes[RB_DIR_RIGHT]
   50 
   51         /*
   52          * rb_info contains the two flags and the parent back pointer.
   53          * We put the two flags in the low two bits since we know that
   54          * rb_node will have an alignment of 4 or 8 bytes.
   55          */
   56         uintptr_t rb_info;
   57 #define RB_FLAG_POSITION        0x2
   58 #define RB_FLAG_RED             0x1
   59 #define RB_FLAG_MASK            (RB_FLAG_POSITION|RB_FLAG_RED)
   60 #define RB_FATHER(rb) \
   61     ((struct rb_node *)((rb)->rb_info & ~RB_FLAG_MASK))
   62 #define RB_SET_FATHER(rb, father) \
   63     ((void)((rb)->rb_info = (uintptr_t)(father)|((rb)->rb_info & RB_FLAG_MASK)))
   64 
   65 #define RB_SENTINEL_P(rb)       ((rb) == NULL)
   66 #define RB_LEFT_SENTINEL_P(rb)  RB_SENTINEL_P((rb)->rb_left)
   67 #define RB_RIGHT_SENTINEL_P(rb) RB_SENTINEL_P((rb)->rb_right)
   68 #define RB_FATHER_SENTINEL_P(rb) RB_SENTINEL_P(RB_FATHER((rb)))
   69 #define RB_CHILDLESS_P(rb) \
   70     (RB_SENTINEL_P(rb) || (RB_LEFT_SENTINEL_P(rb) && RB_RIGHT_SENTINEL_P(rb)))
   71 #define RB_TWOCHILDREN_P(rb) \
   72     (!RB_SENTINEL_P(rb) && !RB_LEFT_SENTINEL_P(rb) && !RB_RIGHT_SENTINEL_P(rb))
   73 
   74 #define RB_POSITION(rb) \
   75     (((rb)->rb_info & RB_FLAG_POSITION) ? RB_DIR_RIGHT : RB_DIR_LEFT)
   76 #define RB_RIGHT_P(rb)          (RB_POSITION(rb) == RB_DIR_RIGHT)
   77 #define RB_LEFT_P(rb)           (RB_POSITION(rb) == RB_DIR_LEFT)
   78 #define RB_RED_P(rb)            (!RB_SENTINEL_P(rb) && ((rb)->rb_info & RB_FLAG_RED) != 0)
   79 #define RB_BLACK_P(rb)          (RB_SENTINEL_P(rb) || ((rb)->rb_info & RB_FLAG_RED) == 0)
   80 #define RB_MARK_RED(rb)         ((void)((rb)->rb_info |= RB_FLAG_RED))
   81 #define RB_MARK_BLACK(rb)       ((void)((rb)->rb_info &= ~RB_FLAG_RED))
   82 #define RB_INVERT_COLOR(rb)     ((void)((rb)->rb_info ^= RB_FLAG_RED))
   83 #define RB_ROOT_P(rbt, rb)      ((rbt)->rbt_root == (rb))
   84 #define RB_SET_POSITION(rb, position) \
   85     ((void)((position) ? ((rb)->rb_info |= RB_FLAG_POSITION) : \
   86     ((rb)->rb_info &= ~RB_FLAG_POSITION)))
   87 #define RB_ZERO_PROPERTIES(rb)  ((void)((rb)->rb_info &= ~RB_FLAG_MASK))
   88 #define RB_COPY_PROPERTIES(dst, src) \
   89     ((void)((dst)->rb_info ^= ((dst)->rb_info ^ (src)->rb_info) & RB_FLAG_MASK))
   90 #define RB_SWAP_PROPERTIES(a, b) do { \
   91     uintptr_t xorinfo = ((a)->rb_info ^ (b)->rb_info) & RB_FLAG_MASK; \
   92     (a)->rb_info ^= xorinfo; \
   93     (b)->rb_info ^= xorinfo; \
   94   } while (/*CONSTCOND*/ 0)
   95 #ifdef RBDEBUG
   96         TAILQ_ENTRY(rb_node) rb_link;
   97 #endif
   98 };
   99 
  100 #define RB_TREE_MIN(T) rb_tree_iterate((T), NULL, RB_DIR_LEFT)
  101 #define RB_TREE_MAX(T) rb_tree_iterate((T), NULL, RB_DIR_RIGHT)
  102 #define RB_TREE_FOREACH(N, T) \
  103     for ((N) = RB_TREE_MIN(T); (N); \
  104         (N) = rb_tree_iterate((T), (N), RB_DIR_RIGHT))
  105 #define RB_TREE_FOREACH_REVERSE(N, T) \
  106     for ((N) = RB_TREE_MAX(T); (N); \
  107         (N) = rb_tree_iterate((T), (N), RB_DIR_LEFT))
  108 
  109 #ifdef RBDEBUG
  110 TAILQ_HEAD(rb_node_qh, rb_node);
  111 
  112 #define RB_TAILQ_REMOVE(a, b, c)                TAILQ_REMOVE(a, b, c)
  113 #define RB_TAILQ_INIT(a)                        TAILQ_INIT(a)
  114 #define RB_TAILQ_INSERT_HEAD(a, b, c)           TAILQ_INSERT_HEAD(a, b, c)
  115 #define RB_TAILQ_INSERT_BEFORE(a, b, c)         TAILQ_INSERT_BEFORE(a, b, c)
  116 #define RB_TAILQ_INSERT_AFTER(a, b, c, d)       TAILQ_INSERT_AFTER(a, b, c, d)
  117 #else
  118 #define RB_TAILQ_REMOVE(a, b, c)                do { } while (/*CONSTCOND*/0)
  119 #define RB_TAILQ_INIT(a)                        do { } while (/*CONSTCOND*/0)
  120 #define RB_TAILQ_INSERT_HEAD(a, b, c)           do { } while (/*CONSTCOND*/0)
  121 #define RB_TAILQ_INSERT_BEFORE(a, b, c)         do { } while (/*CONSTCOND*/0)
  122 #define RB_TAILQ_INSERT_AFTER(a, b, c, d)       do { } while (/*CONSTCOND*/0)
  123 #endif /* RBDEBUG */
  124 
  125 typedef signed int (*const rbto_compare_nodes_fn)(const struct rb_node *,
  126     const struct rb_node *);
  127 typedef signed int (*const rbto_compare_key_fn)(const struct rb_node *,
  128     const void *);
  129 
  130 struct rb_tree_ops {
  131         rbto_compare_nodes_fn rbto_compare_nodes;
  132         rbto_compare_key_fn rbto_compare_key;
  133 };
  134 
  135 struct rb_tree {
  136         struct rb_node *rbt_root;
  137         const struct rb_tree_ops *rbt_ops;
  138         struct rb_node *rbt_minmax[2];
  139 #ifdef RBDEBUG
  140         struct rb_node_qh rbt_nodes;
  141 #endif
  142 #ifdef RBSTATS
  143         unsigned int rbt_count;
  144         unsigned int rbt_insertions;
  145         unsigned int rbt_removals;
  146         unsigned int rbt_insertion_rebalance_calls;
  147         unsigned int rbt_insertion_rebalance_passes;
  148         unsigned int rbt_removal_rebalance_calls;
  149         unsigned int rbt_removal_rebalance_passes;
  150 #endif
  151 };
  152 
  153 #ifdef RBSTATS
  154 #define RBSTAT_INC(v)   ((void)((v)++))
  155 #define RBSTAT_DEC(v)   ((void)((v)--))
  156 #else
  157 #define RBSTAT_INC(v)   do { } while (/*CONSTCOND*/0)
  158 #define RBSTAT_DEC(v)   do { } while (/*CONSTCOND*/0)
  159 #endif
  160 
  161 void    rb_tree_init(struct rb_tree *, const struct rb_tree_ops *);
  162 bool    rb_tree_insert_node(struct rb_tree *, struct rb_node *);
  163 struct rb_node  *
  164         rb_tree_find_node(struct rb_tree *, const void *);
  165 struct rb_node  *
  166         rb_tree_find_node_geq(struct rb_tree *, const void *);
  167 struct rb_node  *
  168         rb_tree_find_node_leq(struct rb_tree *, const void *);
  169 void    rb_tree_remove_node(struct rb_tree *, struct rb_node *);
  170 struct rb_node *
  171         rb_tree_iterate(struct rb_tree *, struct rb_node *, const unsigned int);
  172 #ifdef RBDEBUG
  173 void    rb_tree_check(const struct rb_tree *, bool);
  174 #endif
  175 #ifdef RBSTATS
  176 void    rb_tree_depths(const struct rb_tree *, size_t *);
  177 #endif
  178 
  179 #endif  /* _SYS_RB_H_*/

Cache object: 4f898d2b9cc190c839cf50c5c91057c2


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.