1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
|
/* $NetBSD: rbtree.h,v 1.18 2022/02/27 14:18:34 riastradh Exp $ */
/*-
* Copyright (c) 2013 The NetBSD Foundation, Inc.
* All rights reserved.
*
* This code is derived from software contributed to The NetBSD Foundation
* by Taylor R. Campbell.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef _LINUX_RBTREE_H_
#define _LINUX_RBTREE_H_
#include <sys/rbtree.h>
#include <lib/libkern/libkern.h>
struct rb_root {
struct rb_tree rbr_tree;
};
struct rb_root_cached {
struct rb_root rb_root; /* Linux API name */
};
#define rb_entry(P, T, F) container_of(P, T, F)
#define rb_entry_safe(P, T, F) \
({ \
__typeof__(P) __p = (P); \
__p ? container_of(__p, T, F) : NULL; \
})
/*
* Several of these functions take const inputs and return non-const
* outputs. That is a deliberate choice. It would be better if these
* functions could be const-polymorphic -- return const if given const,
* return non-const if given non-const -- but C doesn't let us express
* that. We are using them to adapt Linux code that is defined in
* terms of token-substitution macros, without types of their own,
* which happen to work out with both const and non-const variants.
* Presumably the Linux code compiles upstream and has some level of
* const type-checking in Linux, so this abuse of __UNCONST does not
* carry substantial risk over to this code here.
*/
static inline bool
RB_EMPTY_ROOT(const struct rb_root *root)
{
return RB_TREE_MIN(__UNCONST(&root->rbr_tree)) == NULL;
}
static inline struct rb_node *
rb_first(const struct rb_root *root)
{
char *vnode = RB_TREE_MIN(__UNCONST(&root->rbr_tree));
if (vnode)
vnode += root->rbr_tree.rbt_ops->rbto_node_offset;
return (struct rb_node *)vnode;
}
static inline struct rb_node *
rb_next2(const struct rb_root *root, const struct rb_node *rbnode)
{
char *vnode = (char *)__UNCONST(rbnode);
vnode -= root->rbr_tree.rbt_ops->rbto_node_offset;
vnode = RB_TREE_NEXT(__UNCONST(&root->rbr_tree), vnode);
if (vnode)
vnode += root->rbr_tree.rbt_ops->rbto_node_offset;
return (struct rb_node *)vnode;
}
static inline struct rb_node *
rb_last(const struct rb_root *root)
{
char *vnode = RB_TREE_MAX(__UNCONST(&root->rbr_tree));
if (vnode)
vnode += root->rbr_tree.rbt_ops->rbto_node_offset;
return (struct rb_node *)vnode;
}
static inline struct rb_node *
rb_first_cached(const struct rb_root_cached *root)
{
return rb_first(&root->rb_root);
}
static inline void
rb_erase(struct rb_node *rbnode, struct rb_root *root)
{
struct rb_tree *tree = &root->rbr_tree;
void *node = (char *)rbnode - tree->rbt_ops->rbto_node_offset;
rb_tree_remove_node(tree, node);
}
static inline void
rb_erase_cached(struct rb_node *rbnode, struct rb_root_cached *root)
{
rb_erase(rbnode, &root->rb_root);
}
static inline void
rb_replace_node(struct rb_node *old, struct rb_node *new, struct rb_root *root)
{
void *vold = (char *)old - root->rbr_tree.rbt_ops->rbto_node_offset;
void *vnew = (char *)new - root->rbr_tree.rbt_ops->rbto_node_offset;
void *collision __diagused;
rb_tree_remove_node(&root->rbr_tree, vold);
collision = rb_tree_insert_node(&root->rbr_tree, vnew);
KASSERT(collision == vnew);
}
static inline void
rb_replace_node_cached(struct rb_node *old, struct rb_node *new,
struct rb_root_cached *root)
{
rb_replace_node(old, new, &root->rb_root);
}
/*
* This violates the abstraction of rbtree(3) for postorder traversal
* -- children first, then parents -- so it is safe for cleanup code
* that just frees all the nodes without removing them from the tree.
*/
static inline struct rb_node *
rb_first_postorder(const struct rb_root *root)
{
struct rb_node *node, *child;
if ((node = root->rbr_tree.rbt_root) == NULL)
return NULL;
for (;; node = child) {
if ((child = node->rb_left) != NULL)
continue;
if ((child = node->rb_right) != NULL)
continue;
return node;
}
}
static inline struct rb_node *
rb_next2_postorder(const struct rb_root *root, struct rb_node *node)
{
struct rb_node *parent, *child;
if (node == NULL)
return NULL;
/*
* If we're at the root, there are no more siblings and no
* parent, so post-order iteration is done.
*/
if (RB_ROOT_P(&root->rbr_tree, node))
return NULL;
parent = RB_FATHER(node); /* kinda sexist, innit */
KASSERT(parent != NULL);
/*
* If we're the right child, we've already processed the left
* child (which may be gone by now), so just return the parent.
*/
if (RB_RIGHT_P(node))
return parent;
/*
* Otherwise, move down to the leftmost child of our right
* sibling -- or return the parent if there is none.
*/
if ((node = parent->rb_right) == NULL)
return parent;
for (;; node = child) {
if ((child = node->rb_left) != NULL)
continue;
if ((child = node->rb_right) != NULL)
continue;
return node;
}
}
/*
* Extension to Linux API, which allows copying a struct rb_root object
* with `=' or `memcpy' and no additional relocation.
*/
static inline void
rb_move(struct rb_root *to, struct rb_root *from)
{
struct rb_node *root;
*to = *from;
memset(from, 0, sizeof(*from)); /* paranoia */
if ((root = to->rbr_tree.rbt_root) == NULL)
return;
/*
* The root node's `parent' is a strict-aliasing-unsafe hack
* pointing at the root of the tree.
*/
RB_SET_FATHER(root, (struct rb_node *)(void *)&to->rbr_tree.rbt_root);
}
#define rbtree_postorder_for_each_entry_safe(ENTRY, TMP, ROOT, FIELD) \
for ((ENTRY) = rb_entry_safe(rb_first_postorder(ROOT), \
__typeof__(*(ENTRY)), FIELD); \
((ENTRY) != NULL && \
((TMP) = rb_entry_safe(rb_next2_postorder((ROOT), \
&(ENTRY)->FIELD), __typeof__(*(ENTRY)), FIELD), \
1)); \
(ENTRY) = (TMP))
#endif /* _LINUX_RBTREE_H_ */
|