1 /* $NetBSD: prop_rb_impl.h,v 1.9 2012/07/27 09:11:00 pooka 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 32 #ifndef _PROP_RB_IMPL_H_ 33 #define _PROP_RB_IMPL_H_ 34 35 #if defined(__NetBSD__) || defined(__minix) 36 #include <sys/rbtree.h> 37 38 /* 39 * Define local names for common rb_tree functions. 40 */ 41 #define _prop_rb_tree_init rb_tree_init 42 #define _prop_rb_tree_insert_node rb_tree_insert_node 43 #define _prop_rb_tree_find rb_tree_find_node 44 #define _prop_rb_tree_remove_node rb_tree_remove_node 45 #define _prop_rb_tree_iterate rb_tree_iterate 46 47 #else /* __NetBSD__ */ 48 49 #include <sys/types.h> 50 #ifdef RBDEBUG 51 #include <sys/queue.h> 52 #endif 53 54 typedef struct rb_node { 55 struct rb_node *rb_nodes[2]; 56 #define RB_DIR_LEFT 0 57 #define RB_DIR_RIGHT 1 58 #define RB_DIR_OTHER 1 59 #define rb_left rb_nodes[RB_DIR_LEFT] 60 #define rb_right rb_nodes[RB_DIR_RIGHT] 61 62 /* 63 * rb_info contains the two flags and the parent back pointer. 64 * We put the two flags in the low two bits since we know that 65 * rb_node will have an alignment of 4 or 8 bytes. 66 */ 67 uintptr_t rb_info; 68 #define RB_FLAG_POSITION 0x2 69 #define RB_FLAG_RED 0x1 70 #define RB_FLAG_MASK (RB_FLAG_POSITION|RB_FLAG_RED) 71 #define RB_FATHER(rb) \ 72 ((struct rb_node *)((rb)->rb_info & ~RB_FLAG_MASK)) 73 #define RB_SET_FATHER(rb, father) \ 74 ((void)((rb)->rb_info = (uintptr_t)(father)|((rb)->rb_info & RB_FLAG_MASK))) 75 76 #define RB_SENTINEL_P(rb) ((rb) == NULL) 77 #define RB_LEFT_SENTINEL_P(rb) RB_SENTINEL_P((rb)->rb_left) 78 #define RB_RIGHT_SENTINEL_P(rb) RB_SENTINEL_P((rb)->rb_right) 79 #define RB_FATHER_SENTINEL_P(rb) RB_SENTINEL_P(RB_FATHER((rb))) 80 #define RB_CHILDLESS_P(rb) \ 81 (RB_SENTINEL_P(rb) || (RB_LEFT_SENTINEL_P(rb) && RB_RIGHT_SENTINEL_P(rb))) 82 #define RB_TWOCHILDREN_P(rb) \ 83 (!RB_SENTINEL_P(rb) && !RB_LEFT_SENTINEL_P(rb) && !RB_RIGHT_SENTINEL_P(rb)) 84 85 #define RB_POSITION(rb) \ 86 (((rb)->rb_info & RB_FLAG_POSITION) ? RB_DIR_RIGHT : RB_DIR_LEFT) 87 #define RB_RIGHT_P(rb) (RB_POSITION(rb) == RB_DIR_RIGHT) 88 #define RB_LEFT_P(rb) (RB_POSITION(rb) == RB_DIR_LEFT) 89 #define RB_RED_P(rb) (!RB_SENTINEL_P(rb) && ((rb)->rb_info & RB_FLAG_RED) != 0) 90 #define RB_BLACK_P(rb) (RB_SENTINEL_P(rb) || ((rb)->rb_info & RB_FLAG_RED) == 0) 91 #define RB_MARK_RED(rb) ((void)((rb)->rb_info |= RB_FLAG_RED)) 92 #define RB_MARK_BLACK(rb) ((void)((rb)->rb_info &= ~RB_FLAG_RED)) 93 #define RB_INVERT_COLOR(rb) ((void)((rb)->rb_info ^= RB_FLAG_RED)) 94 #define RB_ROOT_P(rbt, rb) ((rbt)->rbt_root == (rb)) 95 #define RB_SET_POSITION(rb, position) \ 96 ((void)((position) ? ((rb)->rb_info |= RB_FLAG_POSITION) : \ 97 ((rb)->rb_info &= ~RB_FLAG_POSITION))) 98 #define RB_ZERO_PROPERTIES(rb) ((void)((rb)->rb_info &= ~RB_FLAG_MASK)) 99 #define RB_COPY_PROPERTIES(dst, src) \ 100 ((void)((dst)->rb_info ^= ((dst)->rb_info ^ (src)->rb_info) & RB_FLAG_MASK)) 101 #define RB_SWAP_PROPERTIES(a, b) do { \ 102 uintptr_t xorinfo = ((a)->rb_info ^ (b)->rb_info) & RB_FLAG_MASK; \ 103 (a)->rb_info ^= xorinfo; \ 104 (b)->rb_info ^= xorinfo; \ 105 } while (/*CONSTCOND*/ 0) 106 #ifdef RBDEBUG 107 TAILQ_ENTRY(rb_node) rb_link; 108 #endif 109 } rb_node_t; 110 111 #define RB_TREE_MIN(T) rb_tree_iterate((T), NULL, RB_DIR_LEFT) 112 #define RB_TREE_MAX(T) rb_tree_iterate((T), NULL, RB_DIR_RIGHT) 113 #define RB_TREE_FOREACH(N, T) \ 114 for ((N) = RB_TREE_MIN(T); (N); \ 115 (N) = rb_tree_iterate((T), (N), RB_DIR_RIGHT)) 116 #define RB_TREE_FOREACH_REVERSE(N, T) \ 117 for ((N) = RB_TREE_MAX(T); (N); \ 118 (N) = rb_tree_iterate((T), (N), RB_DIR_LEFT)) 119 120 #ifdef RBDEBUG 121 TAILQ_HEAD(rb_node_qh, rb_node); 122 123 #define RB_TAILQ_REMOVE(a, b, c) TAILQ_REMOVE(a, b, c) 124 #define RB_TAILQ_INIT(a) TAILQ_INIT(a) 125 #define RB_TAILQ_INSERT_HEAD(a, b, c) TAILQ_INSERT_HEAD(a, b, c) 126 #define RB_TAILQ_INSERT_BEFORE(a, b, c) TAILQ_INSERT_BEFORE(a, b, c) 127 #define RB_TAILQ_INSERT_AFTER(a, b, c, d) TAILQ_INSERT_AFTER(a, b, c, d) 128 #else 129 #define RB_TAILQ_REMOVE(a, b, c) do { } while (/*CONSTCOND*/0) 130 #define RB_TAILQ_INIT(a) do { } while (/*CONSTCOND*/0) 131 #define RB_TAILQ_INSERT_HEAD(a, b, c) do { } while (/*CONSTCOND*/0) 132 #define RB_TAILQ_INSERT_BEFORE(a, b, c) do { } while (/*CONSTCOND*/0) 133 #define RB_TAILQ_INSERT_AFTER(a, b, c, d) do { } while (/*CONSTCOND*/0) 134 #endif /* RBDEBUG */ 135 136 /* 137 * rbto_compare_nodes_fn: 138 * return a positive value if the first node > the second node. 139 * return a negative value if the first node < the second node. 140 * return 0 if they are considered same. 141 * 142 * rbto_compare_key_fn: 143 * return a positive value if the node > the key. 144 * return a negative value if the node < the key. 145 * return 0 if they are considered same. 146 */ 147 148 typedef signed int (*rbto_compare_nodes_fn)(void *, const void *, const void *); 149 typedef signed int (*rbto_compare_key_fn)(void *, const void *, const void *); 150 151 typedef struct { 152 rbto_compare_nodes_fn rbto_compare_nodes; 153 rbto_compare_key_fn rbto_compare_key; 154 size_t rbto_node_offset; 155 void *rbto_context; 156 } rb_tree_ops_t; 157 158 typedef struct rb_tree { 159 struct rb_node *rbt_root; 160 const rb_tree_ops_t *rbt_ops; 161 struct rb_node *rbt_minmax[2]; 162 #ifdef RBDEBUG 163 struct rb_node_qh rbt_nodes; 164 #endif 165 #ifdef RBSTATS 166 unsigned int rbt_count; 167 unsigned int rbt_insertions; 168 unsigned int rbt_removals; 169 unsigned int rbt_insertion_rebalance_calls; 170 unsigned int rbt_insertion_rebalance_passes; 171 unsigned int rbt_removal_rebalance_calls; 172 unsigned int rbt_removal_rebalance_passes; 173 #endif 174 } rb_tree_t; 175 176 #ifdef RBSTATS 177 #define RBSTAT_INC(v) ((void)((v)++)) 178 #define RBSTAT_DEC(v) ((void)((v)--)) 179 #else 180 #define RBSTAT_INC(v) do { } while (/*CONSTCOND*/0) 181 #define RBSTAT_DEC(v) do { } while (/*CONSTCOND*/0) 182 #endif 183 184 void _prop_rb_tree_init(rb_tree_t *, const rb_tree_ops_t *); 185 void * _prop_rb_tree_insert_node(rb_tree_t *, void *); 186 void * _prop_rb_tree_find(rb_tree_t *, const void *); 187 void * _prop_rb_tree_find_node(rb_tree_t *, const void *); 188 void _prop_rb_tree_remove_node(rb_tree_t *, void *); 189 void * _prop_rb_tree_iterate(rb_tree_t *, void *, const unsigned int); 190 #ifdef RBDEBUG 191 void _prop_rb_tree_check(const struct rb_tree *, bool); 192 #endif 193 194 #endif /* __NetBSD__ */ 195 196 #endif /* _PROP_RB_IMPL_H_*/ 197