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