ptree.c revision 1.3 1 1.3 jnemeth /* $NetBSD: ptree.c,v 1.3 2008/11/21 03:23:43 jnemeth Exp $ */
2 1.1 matt
3 1.1 matt /*-
4 1.1 matt * Copyright (c) 2008 The NetBSD Foundation, Inc.
5 1.1 matt * All rights reserved.
6 1.1 matt *
7 1.1 matt * This code is derived from software contributed to The NetBSD Foundation
8 1.1 matt * by Matt Thomas <matt (at) 3am-software.com>.
9 1.1 matt *
10 1.1 matt * Redistribution and use in source and binary forms, with or without
11 1.1 matt * modification, are permitted provided that the following conditions
12 1.1 matt * are met:
13 1.1 matt * 1. Redistributions of source code must retain the above copyright
14 1.1 matt * notice, this list of conditions and the following disclaimer.
15 1.1 matt * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 matt * notice, this list of conditions and the following disclaimer in the
17 1.1 matt * documentation and/or other materials provided with the distribution.
18 1.1 matt *
19 1.1 matt * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.1 matt * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.1 matt * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.1 matt * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.1 matt * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.1 matt * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.1 matt * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.1 matt * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.1 matt * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.1 matt * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.1 matt * POSSIBILITY OF SUCH DAMAGE.
30 1.1 matt */
31 1.1 matt
32 1.1 matt #define _PT_PRIVATE
33 1.1 matt
34 1.1 matt #if defined(PTCHECK) && !defined(PTDEBUG)
35 1.1 matt #define PTDEBUG
36 1.1 matt #endif
37 1.1 matt
38 1.1 matt #if defined(_KERNEL) || defined(_STANDALONE)
39 1.1 matt #include <sys/param.h>
40 1.1 matt #include <sys/types.h>
41 1.1 matt #include <sys/systm.h>
42 1.3 jnemeth #include <lib/libkern/libkern.h>
43 1.3 jnemeth __KERNEL_RCSID(0, "$NetBSD: ptree.c,v 1.3 2008/11/21 03:23:43 jnemeth Exp $");
44 1.1 matt #else
45 1.1 matt #include <stddef.h>
46 1.1 matt #include <stdint.h>
47 1.1 matt #include <limits.h>
48 1.1 matt #include <stdbool.h>
49 1.1 matt #include <string.h>
50 1.1 matt #ifdef PTDEBUG
51 1.1 matt #include <assert.h>
52 1.1 matt #define KASSERT(e) assert(e)
53 1.1 matt #else
54 1.1 matt #define KASSERT(e) do { } while (/*CONSTCOND*/ 0)
55 1.1 matt #endif
56 1.3 jnemeth __RCSID("$NetBSD: ptree.c,v 1.3 2008/11/21 03:23:43 jnemeth Exp $");
57 1.1 matt #endif /* _KERNEL || _STANDALONE */
58 1.1 matt
59 1.1 matt #ifdef _LIBC
60 1.1 matt #include "namespace.h"
61 1.1 matt #endif
62 1.1 matt
63 1.1 matt #ifdef PTTEST
64 1.1 matt #include "ptree.h"
65 1.1 matt #else
66 1.1 matt #include <sys/ptree.h>
67 1.1 matt #endif
68 1.1 matt
69 1.1 matt /*
70 1.1 matt * This is an implementation of a radix / PATRICIA tree. As in a traditional
71 1.1 matt * patricia tree, all the data is at the leaves of the tree. An N-value
72 1.1 matt * tree would have N leaves, N-1 branching nodes, and a root pointer. Each
73 1.1 matt * branching node would have left(0) and right(1) pointers that either point
74 1.1 matt * to another branching node or a leaf node. The root pointer would also
75 1.1 matt * point to either the first branching node or a leaf node. Leaf nodes
76 1.1 matt * have no need for pointers.
77 1.1 matt *
78 1.1 matt * However, allocation for these branching nodes is problematic since the
79 1.1 matt * allocation could fail. This would cause insertions to fail for reasons
80 1.1 matt * beyond the users control. So to prevent this, in this implementation
81 1.1 matt * each node has two identities: its leaf identity and its branch identity.
82 1.1 matt * Each is separate from the other. Every branch is tagged as to whether
83 1.1 matt * it points to a leaf or a branch. This is not an attribute of the object
84 1.1 matt * but of the pointer to the object. The low bit of the pointer is used as
85 1.1 matt * the tag to determine wether it points to a leaf or branch identity, with
86 1.1 matt * branch identities having the low bit set.
87 1.1 matt *
88 1.1 matt * A node's branch identity has one rule: when traversing the tree from the
89 1.1 matt * root to the node's leaf identity, one of the branches traversed will be via
90 1.1 matt * the node's branch identity. Of course, that has an exception: since to
91 1.1 matt * store N leaves, you need N-1 branches. That one node whose branch identity
92 1.1 matt * isn't used is stored as "oddman"-out in the root.
93 1.1 matt *
94 1.1 matt * Branching nodes also has a bit offset and a bit length which determines
95 1.1 matt * which branch slot is used. The bit length can be zero resulting in a
96 1.1 matt * one-way branch. This is happens in two special cases: the root and
97 1.1 matt * interior mask nodes.
98 1.1 matt *
99 1.1 matt * To support longest match first lookups, when a mask node (one that only
100 1.1 matt * match the first N bits) has children who first N bits match the mask nodes,
101 1.1 matt * that mask node is converted from being a leaf node to being a one-way
102 1.1 matt * branch-node. The mask becomes fixed in position in the tree. The mask
103 1.1 matt * will alaways be the longest mask match for its descendants (unless they
104 1.1 matt * traverse an even longer match).
105 1.1 matt */
106 1.1 matt
107 1.1 matt #define NODETOITEM(pt, ptn) \
108 1.1 matt ((void *)((uintptr_t)(ptn) - (pt)->pt_node_offset))
109 1.1 matt #define NODETOKEY(pt, ptn) \
110 1.1 matt ((void *)((uintptr_t)(ptn) - (pt)->pt_node_offset + pt->pt_key_offset))
111 1.1 matt #define ITEMTONODE(pt, ptn) \
112 1.1 matt ((pt_node_t *)((uintptr_t)(ptn) + (pt)->pt_node_offset))
113 1.1 matt
114 1.1 matt bool ptree_check(const pt_tree_t *);
115 1.1 matt #if PTCHECK > 1
116 1.1 matt #define PTREE_CHECK(pt) ptree_check(pt)
117 1.1 matt #else
118 1.2 matt #define PTREE_CHECK(pt) do { } while (/*CONSTCOND*/ 0)
119 1.1 matt #endif
120 1.1 matt
121 1.1 matt static inline bool
122 1.1 matt ptree_matchnode(const pt_tree_t *pt, const pt_node_t *target,
123 1.1 matt const pt_node_t *ptn, pt_bitoff_t max_bitoff,
124 1.1 matt pt_bitoff_t *bitoff_p, pt_slot_t *slots_p)
125 1.1 matt {
126 1.1 matt return (*pt->pt_ops->ptto_matchnode)(NODETOKEY(pt, target),
127 1.1 matt (ptn != NULL ? NODETOKEY(pt, ptn) : NULL), max_bitoff,
128 1.1 matt bitoff_p, slots_p);
129 1.1 matt }
130 1.1 matt
131 1.1 matt static inline pt_slot_t
132 1.1 matt ptree_testnode(const pt_tree_t *pt, const pt_node_t *target,
133 1.1 matt const pt_node_t *ptn)
134 1.1 matt {
135 1.1 matt const pt_bitlen_t bitlen = PTN_BRANCH_BITLEN(ptn);
136 1.1 matt if (bitlen == 0)
137 1.1 matt return PT_SLOT_ROOT;
138 1.1 matt return (*pt->pt_ops->ptto_testnode)(NODETOKEY(pt, target),
139 1.1 matt PTN_BRANCH_BITOFF(ptn),
140 1.1 matt bitlen);
141 1.1 matt }
142 1.1 matt
143 1.1 matt static inline bool
144 1.1 matt ptree_matchkey(const pt_tree_t *pt, const void *key,
145 1.1 matt const pt_node_t *ptn, pt_bitoff_t bitoff, pt_bitlen_t bitlen)
146 1.1 matt {
147 1.1 matt return (*pt->pt_ops->ptto_matchkey)(key, NODETOKEY(pt, ptn),
148 1.1 matt bitoff, bitlen);
149 1.1 matt }
150 1.1 matt
151 1.1 matt static inline pt_slot_t
152 1.1 matt ptree_testkey(const pt_tree_t *pt, const void *key, const pt_node_t *ptn)
153 1.1 matt {
154 1.1 matt return (*pt->pt_ops->ptto_testkey)(key,
155 1.1 matt PTN_BRANCH_BITOFF(ptn),
156 1.1 matt PTN_BRANCH_BITLEN(ptn));
157 1.1 matt }
158 1.1 matt
159 1.1 matt static inline void
160 1.1 matt ptree_set_position(uintptr_t node, pt_slot_t position)
161 1.1 matt {
162 1.1 matt if (PT_LEAF_P(node))
163 1.1 matt PTN_SET_LEAF_POSITION(PT_NODE(node), position);
164 1.1 matt else
165 1.1 matt PTN_SET_BRANCH_POSITION(PT_NODE(node), position);
166 1.1 matt }
167 1.1 matt
168 1.1 matt void
169 1.1 matt ptree_init(pt_tree_t *pt, const pt_tree_ops_t *ops, size_t node_offset,
170 1.1 matt size_t key_offset)
171 1.1 matt {
172 1.1 matt memset(pt, 0, sizeof(*pt));
173 1.1 matt pt->pt_node_offset = node_offset;
174 1.1 matt pt->pt_key_offset = key_offset;
175 1.1 matt pt->pt_ops = ops;
176 1.1 matt }
177 1.1 matt
178 1.1 matt typedef struct {
179 1.1 matt uintptr_t *id_insertp;
180 1.1 matt pt_node_t *id_parent;
181 1.1 matt uintptr_t id_node;
182 1.1 matt pt_slot_t id_parent_slot;
183 1.1 matt pt_bitoff_t id_bitoff;
184 1.1 matt pt_slot_t id_slot;
185 1.1 matt } pt_insertdata_t;
186 1.1 matt
187 1.1 matt typedef bool (*pt_insertfunc_t)(pt_tree_t *, pt_node_t *, pt_insertdata_t *);
188 1.1 matt
189 1.1 matt /*
190 1.1 matt * Move a branch identify from src to dst. The leaves don't care since
191 1.1 matt * nothing for them has changed.
192 1.1 matt */
193 1.2 matt /*ARGSUSED*/
194 1.1 matt static uintptr_t
195 1.1 matt ptree_move_branch(pt_tree_t * const pt, pt_node_t * const dst,
196 1.1 matt const pt_node_t * const src)
197 1.1 matt {
198 1.1 matt KASSERT(PTN_BRANCH_BITLEN(src) == 1);
199 1.1 matt /* set branch bitlen and bitoff in one step. */
200 1.1 matt dst->ptn_branchdata = src->ptn_branchdata;
201 1.1 matt PTN_SET_BRANCH_POSITION(dst, PTN_BRANCH_POSITION(src));
202 1.1 matt PTN_COPY_BRANCH_SLOTS(dst, src);
203 1.1 matt return PTN_BRANCH(dst);
204 1.1 matt }
205 1.1 matt
206 1.1 matt #ifndef PTNOMASK
207 1.1 matt static inline uintptr_t *
208 1.1 matt ptree_find_branch(pt_tree_t * const pt, uintptr_t branch_node)
209 1.1 matt {
210 1.1 matt pt_node_t * const branch = PT_NODE(branch_node);
211 1.1 matt pt_node_t *parent;
212 1.1 matt
213 1.1 matt for (parent = &pt->pt_rootnode;;) {
214 1.1 matt uintptr_t *nodep =
215 1.1 matt &PTN_BRANCH_SLOT(parent, ptree_testnode(pt, branch, parent));
216 1.1 matt if (*nodep == branch_node)
217 1.1 matt return nodep;
218 1.1 matt if (PT_LEAF_P(*nodep))
219 1.1 matt return NULL;
220 1.1 matt parent = PT_NODE(*nodep);
221 1.1 matt }
222 1.1 matt }
223 1.1 matt
224 1.1 matt static bool
225 1.1 matt ptree_insert_leaf_after_mask(pt_tree_t * const pt, pt_node_t * const target,
226 1.1 matt pt_insertdata_t * const id)
227 1.1 matt {
228 1.1 matt const uintptr_t target_node = PTN_LEAF(target);
229 1.1 matt const uintptr_t mask_node = id->id_node;
230 1.1 matt pt_node_t * const mask = PT_NODE(mask_node);
231 1.1 matt const pt_bitlen_t mask_len = PTN_MASK_BITLEN(mask);
232 1.1 matt
233 1.1 matt KASSERT(PT_LEAF_P(mask_node));
234 1.1 matt KASSERT(PTN_LEAF_POSITION(mask) == id->id_parent_slot);
235 1.1 matt KASSERT(mask_len <= id->id_bitoff);
236 1.1 matt KASSERT(PTN_ISMASK_P(mask));
237 1.1 matt KASSERT(!PTN_ISMASK_P(target) || mask_len < PTN_MASK_BITLEN(target));
238 1.1 matt
239 1.1 matt if (mask_node == PTN_BRANCH_ODDMAN_SLOT(&pt->pt_rootnode)) {
240 1.1 matt KASSERT(id->id_parent != mask);
241 1.1 matt /*
242 1.1 matt * Nice, mask was an oddman. So just set the oddman to target.
243 1.1 matt */
244 1.1 matt PTN_BRANCH_ODDMAN_SLOT(&pt->pt_rootnode) = target_node;
245 1.1 matt } else {
246 1.1 matt /*
247 1.1 matt * We need to find out who's pointing to mask's branch
248 1.1 matt * identity. We know that between root and the leaf identity,
249 1.1 matt * we must traverse the node's branch identity.
250 1.1 matt */
251 1.1 matt uintptr_t * const mask_nodep = ptree_find_branch(pt, PTN_BRANCH(mask));
252 1.1 matt KASSERT(mask_nodep != NULL);
253 1.1 matt KASSERT(*mask_nodep == PTN_BRANCH(mask));
254 1.1 matt KASSERT(PTN_BRANCH_BITLEN(mask) == 1);
255 1.1 matt
256 1.1 matt /*
257 1.1 matt * Alas, mask was used as a branch. Since the mask is becoming
258 1.1 matt * a one-way branch, we need make target take over mask's
259 1.1 matt * branching responsibilities. Only then can we change it.
260 1.1 matt */
261 1.1 matt *mask_nodep = ptree_move_branch(pt, target, mask);
262 1.1 matt
263 1.1 matt /*
264 1.1 matt * However, it's possible that mask's parent is itself. If
265 1.1 matt * that's true, update the insert point to use target since it
266 1.1 matt * has taken over mask's branching duties.
267 1.1 matt */
268 1.1 matt if (id->id_parent == mask)
269 1.1 matt id->id_insertp = &PTN_BRANCH_SLOT(target,
270 1.1 matt id->id_parent_slot);
271 1.1 matt }
272 1.1 matt
273 1.1 matt PTN_SET_BRANCH_BITLEN(mask, 0);
274 1.1 matt PTN_SET_BRANCH_BITOFF(mask, mask_len);
275 1.1 matt
276 1.1 matt PTN_BRANCH_ROOT_SLOT(mask) = target_node;
277 1.1 matt PTN_BRANCH_ODDMAN_SLOT(mask) = PT_NULL;
278 1.1 matt PTN_SET_LEAF_POSITION(target, PT_SLOT_ROOT);
279 1.1 matt PTN_SET_BRANCH_POSITION(mask, id->id_parent_slot);
280 1.1 matt
281 1.1 matt /*
282 1.1 matt * Now that everything is done, to make target visible we need to
283 1.1 matt * change mask from a leaf to a branch.
284 1.1 matt */
285 1.1 matt *id->id_insertp = PTN_BRANCH(mask);
286 1.1 matt PTREE_CHECK(pt);
287 1.1 matt return true;
288 1.1 matt }
289 1.1 matt
290 1.2 matt /*ARGSUSED*/
291 1.1 matt static bool
292 1.1 matt ptree_insert_mask_before_node(pt_tree_t * const pt, pt_node_t * const target,
293 1.1 matt pt_insertdata_t * const id)
294 1.1 matt {
295 1.1 matt const uintptr_t node = id->id_node;
296 1.1 matt pt_node_t * const ptn = PT_NODE(node);
297 1.1 matt const pt_slot_t mask_len = PTN_MASK_BITLEN(target);
298 1.1 matt const pt_bitlen_t node_mask_len = PTN_MASK_BITLEN(ptn);
299 1.1 matt
300 1.1 matt KASSERT(PT_LEAF_P(node) || id->id_parent_slot == PTN_BRANCH_POSITION(ptn));
301 1.1 matt KASSERT(PT_BRANCH_P(node) || id->id_parent_slot == PTN_LEAF_POSITION(ptn));
302 1.1 matt KASSERT(PTN_ISMASK_P(target));
303 1.1 matt
304 1.1 matt /*
305 1.1 matt * If the node we are placing ourself in front is a mask with the
306 1.1 matt * same mask length as us, return failure.
307 1.1 matt */
308 1.1 matt if (PTN_ISMASK_P(ptn) && node_mask_len == mask_len)
309 1.1 matt return false;
310 1.1 matt
311 1.1 matt PTN_SET_BRANCH_BITLEN(target, 0);
312 1.1 matt PTN_SET_BRANCH_BITOFF(target, mask_len);
313 1.1 matt
314 1.1 matt PTN_BRANCH_SLOT(target, PT_SLOT_ROOT) = node;
315 1.1 matt *id->id_insertp = PTN_BRANCH(target);
316 1.1 matt
317 1.1 matt PTN_SET_BRANCH_POSITION(target, id->id_parent_slot);
318 1.1 matt ptree_set_position(node, PT_SLOT_ROOT);
319 1.1 matt
320 1.1 matt PTREE_CHECK(pt);
321 1.1 matt return true;
322 1.1 matt }
323 1.1 matt #endif /* !PTNOMASK */
324 1.1 matt
325 1.2 matt /*ARGSUSED*/
326 1.1 matt static bool
327 1.1 matt ptree_insert_branch_at_node(pt_tree_t * const pt, pt_node_t * const target,
328 1.1 matt pt_insertdata_t * const id)
329 1.1 matt {
330 1.1 matt const uintptr_t target_node = PTN_LEAF(target);
331 1.1 matt const uintptr_t node = id->id_node;
332 1.1 matt const pt_slot_t other_slot = id->id_slot ^ PT_SLOT_OTHER;
333 1.1 matt
334 1.1 matt KASSERT(PT_BRANCH_P(node) || id->id_parent_slot == PTN_LEAF_POSITION(PT_NODE(node)));
335 1.1 matt KASSERT(PT_LEAF_P(node) || id->id_parent_slot == PTN_BRANCH_POSITION(PT_NODE(node)));
336 1.1 matt KASSERT((node == pt->pt_root) == (id->id_parent == &pt->pt_rootnode));
337 1.1 matt #ifndef PTNOMASK
338 1.1 matt KASSERT(!PTN_ISMASK_P(target) || id->id_bitoff <= PTN_MASK_BITLEN(target));
339 1.1 matt #endif
340 1.1 matt KASSERT(node == pt->pt_root || PTN_BRANCH_BITOFF(id->id_parent) + PTN_BRANCH_BITLEN(id->id_parent) <= id->id_bitoff);
341 1.1 matt
342 1.1 matt PTN_SET_BRANCH_BITOFF(target, id->id_bitoff);
343 1.1 matt PTN_SET_BRANCH_BITLEN(target, 1);
344 1.1 matt
345 1.1 matt PTN_BRANCH_SLOT(target, id->id_slot) = target_node;
346 1.1 matt PTN_BRANCH_SLOT(target, other_slot) = node;
347 1.1 matt *id->id_insertp = PTN_BRANCH(target);
348 1.1 matt
349 1.1 matt PTN_SET_LEAF_POSITION(target, id->id_slot);
350 1.1 matt ptree_set_position(node, other_slot);
351 1.1 matt
352 1.1 matt PTN_SET_BRANCH_POSITION(target, id->id_parent_slot);
353 1.1 matt PTREE_CHECK(pt);
354 1.1 matt return true;
355 1.1 matt }
356 1.1 matt
357 1.1 matt static bool
358 1.1 matt ptree_insert_leaf(pt_tree_t * const pt, pt_node_t * const target,
359 1.1 matt pt_insertdata_t * const id)
360 1.1 matt {
361 1.1 matt const uintptr_t leaf_node = id->id_node;
362 1.1 matt pt_node_t * const leaf = PT_NODE(leaf_node);
363 1.1 matt #ifdef PTNOMASK
364 1.1 matt const bool inserting_mask = false;
365 1.1 matt const bool at_mask = false;
366 1.1 matt #else
367 1.1 matt const bool inserting_mask = PTN_ISMASK_P(target);
368 1.1 matt const bool at_mask = PTN_ISMASK_P(leaf);
369 1.1 matt const pt_bitlen_t leaf_masklen = PTN_MASK_BITLEN(leaf);
370 1.1 matt const pt_bitlen_t target_masklen = PTN_MASK_BITLEN(target);
371 1.1 matt #endif
372 1.1 matt pt_insertfunc_t insertfunc = ptree_insert_branch_at_node;
373 1.1 matt bool matched;
374 1.1 matt
375 1.1 matt /*
376 1.1 matt * In all likelyhood we are going simply going to insert a branch
377 1.1 matt * where this leaf is which will point to the old and new leaves.
378 1.1 matt */
379 1.1 matt KASSERT(PT_LEAF_P(leaf_node));
380 1.1 matt KASSERT(PTN_LEAF_POSITION(leaf) == id->id_parent_slot);
381 1.1 matt matched = ptree_matchnode(pt, target, leaf, UINT_MAX,
382 1.1 matt &id->id_bitoff, &id->id_slot);
383 1.1 matt if (__predict_false(!inserting_mask)) {
384 1.1 matt /*
385 1.1 matt * We aren't inserting a mask nor is the leaf a mask, which
386 1.1 matt * means we are trying to insert a duplicate leaf. Can't do
387 1.1 matt * that.
388 1.1 matt */
389 1.1 matt if (!at_mask && matched)
390 1.1 matt return false;
391 1.1 matt
392 1.1 matt #ifndef PTNOMASK
393 1.1 matt /*
394 1.1 matt * We are at a mask and the leaf we are about to insert
395 1.1 matt * is at or beyond the mask, we need to convert the mask
396 1.1 matt * from a leaf to a one-way branch interior mask.
397 1.1 matt */
398 1.1 matt if (at_mask && id->id_bitoff >= leaf_masklen)
399 1.1 matt insertfunc = ptree_insert_leaf_after_mask;
400 1.1 matt #endif /* PTNOMASK */
401 1.1 matt }
402 1.1 matt #ifndef PTNOMASK
403 1.1 matt else {
404 1.1 matt /*
405 1.1 matt * We are inserting a mask.
406 1.1 matt */
407 1.1 matt if (matched) {
408 1.1 matt /*
409 1.1 matt * If the leaf isn't a mask, we obviously have to
410 1.1 matt * insert the new mask before non-mask leaf. If the
411 1.1 matt * leaf is a mask, and the new node has a LEQ mask
412 1.1 matt * length it too needs to inserted before leaf (*).
413 1.1 matt *
414 1.1 matt * In other cases, we place the new mask as leaf after
415 1.1 matt * leaf mask. Which mask comes first will be a one-way
416 1.1 matt * branch interior mask node which has the other mask
417 1.1 matt * node as a child.
418 1.1 matt *
419 1.1 matt * (*) ptree_insert_mask_before_node can detect a
420 1.1 matt * duplicate mask and return failure if needed.
421 1.1 matt */
422 1.1 matt if (!at_mask || target_masklen <= leaf_masklen)
423 1.1 matt insertfunc = ptree_insert_mask_before_node;
424 1.1 matt else
425 1.1 matt insertfunc = ptree_insert_leaf_after_mask;
426 1.1 matt } else if (at_mask && id->id_bitoff >= leaf_masklen) {
427 1.1 matt /*
428 1.1 matt * If the new mask has a bit offset GEQ than the leaf's
429 1.1 matt * mask length, convert the left to a one-way branch
430 1.1 matt * interior mask and make that point to the new [leaf]
431 1.1 matt * mask.
432 1.1 matt */
433 1.1 matt insertfunc = ptree_insert_leaf_after_mask;
434 1.1 matt } else {
435 1.1 matt /*
436 1.1 matt * The new mask has a bit offset less than the leaf's
437 1.1 matt * mask length or if the leaf isn't a mask at all, the
438 1.1 matt * new mask deserves to be its own leaf so we use the
439 1.1 matt * default insertfunc to do that.
440 1.1 matt */
441 1.1 matt }
442 1.1 matt }
443 1.1 matt #endif /* PTNOMASK */
444 1.1 matt
445 1.1 matt return (*insertfunc)(pt, target, id);
446 1.1 matt }
447 1.1 matt
448 1.1 matt static bool
449 1.1 matt ptree_insert_node_common(pt_tree_t *pt, void *item)
450 1.1 matt {
451 1.1 matt pt_node_t * const target = ITEMTONODE(pt, item);
452 1.1 matt #ifndef PTNOMASK
453 1.1 matt const bool inserting_mask = PTN_ISMASK_P(target);
454 1.1 matt const pt_bitlen_t target_masklen = PTN_MASK_BITLEN(target);
455 1.1 matt #endif
456 1.1 matt pt_insertfunc_t insertfunc;
457 1.1 matt pt_insertdata_t id;
458 1.1 matt
459 1.1 matt /*
460 1.1 matt * We need a leaf so we can match against. Until we get a leaf
461 1.1 matt * we having nothing to test against.
462 1.1 matt */
463 1.1 matt if (__predict_false(PT_NULL_P(pt->pt_root))) {
464 1.1 matt PTN_BRANCH_ROOT_SLOT(&pt->pt_rootnode) = PTN_LEAF(target);
465 1.1 matt PTN_BRANCH_ODDMAN_SLOT(&pt->pt_rootnode) = PTN_LEAF(target);
466 1.1 matt PTN_SET_LEAF_POSITION(target, PT_SLOT_ROOT);
467 1.1 matt PTREE_CHECK(pt);
468 1.1 matt return true;
469 1.1 matt }
470 1.1 matt
471 1.1 matt id.id_bitoff = 0;
472 1.1 matt id.id_parent = &pt->pt_rootnode;
473 1.1 matt id.id_parent_slot = PT_SLOT_ROOT;
474 1.1 matt id.id_insertp = &PTN_BRANCH_ROOT_SLOT(id.id_parent);
475 1.1 matt for (;;) {
476 1.1 matt pt_bitoff_t branch_bitoff;
477 1.1 matt pt_node_t * const ptn = PT_NODE(*id.id_insertp);
478 1.1 matt id.id_node = *id.id_insertp;
479 1.1 matt
480 1.1 matt /*
481 1.1 matt * If we hit a leaf, try to insert target at leaf. We could
482 1.1 matt * have inlined ptree_insert_leaf here but that would have
483 1.1 matt * made this routine much harder to understand. Trust the
484 1.1 matt * compiler to optimize this properly.
485 1.1 matt */
486 1.1 matt if (PT_LEAF_P(id.id_node)) {
487 1.1 matt KASSERT(PTN_LEAF_POSITION(ptn) == id.id_parent_slot);
488 1.1 matt insertfunc = ptree_insert_leaf;
489 1.1 matt break;
490 1.1 matt }
491 1.1 matt
492 1.1 matt /*
493 1.1 matt * If we aren't a leaf, we must be a branch. Make sure we are
494 1.1 matt * in the slot we think we are.
495 1.1 matt */
496 1.1 matt KASSERT(PT_BRANCH_P(id.id_node));
497 1.1 matt KASSERT(PTN_BRANCH_POSITION(ptn) == id.id_parent_slot);
498 1.1 matt
499 1.1 matt /*
500 1.1 matt * Where is this branch?
501 1.1 matt */
502 1.1 matt branch_bitoff = PTN_BRANCH_BITOFF(ptn);
503 1.1 matt
504 1.1 matt #ifndef PTNOMASK
505 1.1 matt /*
506 1.1 matt * If this is a one-way mask node, its offset must equal
507 1.1 matt * its mask's bitlen.
508 1.1 matt */
509 1.1 matt KASSERT(!(PTN_ISMASK_P(ptn) && PTN_BRANCH_BITLEN(ptn) == 0) || PTN_MASK_BITLEN(ptn) == branch_bitoff);
510 1.1 matt
511 1.1 matt /*
512 1.1 matt * If we are inserting a mask, and we know that at this point
513 1.1 matt * all bits before the current bit offset match both the target
514 1.1 matt * and the branch. If the target's mask length is LEQ than
515 1.1 matt * this branch's bit offset, then this is where the mask needs
516 1.1 matt * to added to the tree.
517 1.1 matt */
518 1.1 matt if (__predict_false(inserting_mask)
519 1.1 matt && (PTN_ISROOT_P(pt, id.id_parent)
520 1.1 matt || id.id_bitoff < target_masklen)
521 1.1 matt && target_masklen <= branch_bitoff) {
522 1.1 matt /*
523 1.1 matt * We don't know about the bits (if any) between
524 1.1 matt * id.id_bitoff and the target's mask length match
525 1.1 matt * both the target and the branch. If the target's
526 1.1 matt * mask length is greater than the current bit offset
527 1.1 matt * make sure the untested bits match both the target
528 1.1 matt * and the branch.
529 1.1 matt */
530 1.1 matt if (target_masklen == id.id_bitoff
531 1.1 matt || ptree_matchnode(pt, target, ptn, target_masklen,
532 1.1 matt &id.id_bitoff, &id.id_slot)) {
533 1.1 matt /*
534 1.1 matt * The bits matched, so insert the mask as a
535 1.1 matt * one-way branch.
536 1.1 matt */
537 1.1 matt insertfunc = ptree_insert_mask_before_node;
538 1.1 matt break;
539 1.1 matt } else if (id.id_bitoff < branch_bitoff) {
540 1.1 matt /*
541 1.1 matt * They didn't match, so create a normal branch
542 1.1 matt * because this mask needs to a be a new leaf.
543 1.1 matt */
544 1.1 matt insertfunc = ptree_insert_branch_at_node;
545 1.1 matt break;
546 1.1 matt }
547 1.1 matt }
548 1.1 matt #endif /* PTNOMASK */
549 1.1 matt
550 1.1 matt /*
551 1.1 matt * If we are skipping some bits, verify they match the node.
552 1.1 matt * If they don't match, it means we have a leaf to insert.
553 1.1 matt * Note that if we are advancing bit by bit, we'll skip
554 1.1 matt * doing matchnode and walk the tree bit by bit via testnode.
555 1.1 matt */
556 1.1 matt if (id.id_bitoff < branch_bitoff
557 1.1 matt && !ptree_matchnode(pt, target, ptn, branch_bitoff,
558 1.1 matt &id.id_bitoff, &id.id_slot)) {
559 1.1 matt KASSERT(id.id_bitoff < branch_bitoff);
560 1.1 matt insertfunc = ptree_insert_branch_at_node;
561 1.1 matt break;
562 1.1 matt }
563 1.1 matt
564 1.1 matt /*
565 1.1 matt * At this point, all bits before branch_bitoff are known
566 1.1 matt * to match the target.
567 1.1 matt */
568 1.1 matt KASSERT(id.id_bitoff >= branch_bitoff);
569 1.1 matt
570 1.1 matt /*
571 1.1 matt * Decend the tree one level.
572 1.1 matt */
573 1.1 matt id.id_parent = ptn;
574 1.1 matt id.id_parent_slot = ptree_testnode(pt, target, id.id_parent);
575 1.1 matt id.id_bitoff += PTN_BRANCH_BITLEN(id.id_parent);
576 1.1 matt id.id_insertp = &PTN_BRANCH_SLOT(id.id_parent, id.id_parent_slot);
577 1.1 matt }
578 1.1 matt
579 1.1 matt /*
580 1.1 matt * Do the actual insertion.
581 1.1 matt */
582 1.1 matt return (*insertfunc)(pt, target, &id);
583 1.1 matt }
584 1.1 matt
585 1.1 matt bool
586 1.1 matt ptree_insert_node(pt_tree_t *pt, void *item)
587 1.1 matt {
588 1.1 matt pt_node_t * const target = ITEMTONODE(pt, item);
589 1.1 matt
590 1.1 matt memset(target, 0, sizeof(*target));
591 1.1 matt return ptree_insert_node_common(pt, target);
592 1.1 matt }
593 1.1 matt
594 1.1 matt #ifndef PTNOMASK
595 1.1 matt bool
596 1.1 matt ptree_insert_mask_node(pt_tree_t *pt, void *item, pt_bitlen_t mask_len)
597 1.1 matt {
598 1.1 matt pt_node_t * const target = ITEMTONODE(pt, item);
599 1.1 matt pt_bitoff_t bitoff = mask_len;
600 1.1 matt pt_slot_t slot;
601 1.1 matt
602 1.1 matt memset(target, 0, sizeof(*target));
603 1.1 matt KASSERT(mask_len == 0 || (~PT__MASK(PTN_MASK_BITLEN) & mask_len) == 0);
604 1.1 matt /*
605 1.1 matt * Only the first <mask_len> bits can be non-zero.
606 1.1 matt * All other bits must be 0.
607 1.1 matt */
608 1.1 matt if (!ptree_matchnode(pt, target, NULL, UINT_MAX, &bitoff, &slot))
609 1.1 matt return false;
610 1.1 matt PTN_SET_MASK_BITLEN(target, mask_len);
611 1.1 matt PTN_MARK_MASK(target);
612 1.1 matt return ptree_insert_node_common(pt, target);
613 1.1 matt }
614 1.1 matt #endif /* !PTNOMASH */
615 1.1 matt
616 1.1 matt void *
617 1.1 matt ptree_find_filtered_node(pt_tree_t *pt, void *key, pt_filter_t filter,
618 1.1 matt void *filter_arg)
619 1.1 matt {
620 1.1 matt #ifndef PTNOMASK
621 1.1 matt pt_node_t *mask = NULL;
622 1.1 matt #endif
623 1.1 matt bool at_mask = false;
624 1.1 matt pt_node_t *ptn, *parent;
625 1.1 matt pt_bitoff_t bitoff;
626 1.1 matt pt_slot_t parent_slot;
627 1.1 matt
628 1.1 matt if (PT_NULL_P(PTN_BRANCH_ROOT_SLOT(&pt->pt_rootnode)))
629 1.1 matt return NULL;
630 1.1 matt
631 1.1 matt bitoff = 0;
632 1.1 matt parent = &pt->pt_rootnode;
633 1.1 matt parent_slot = PT_SLOT_ROOT;
634 1.1 matt for (;;) {
635 1.1 matt const uintptr_t node = PTN_BRANCH_SLOT(parent, parent_slot);
636 1.1 matt const pt_slot_t branch_bitoff = PTN_BRANCH_BITOFF(PT_NODE(node));
637 1.1 matt ptn = PT_NODE(node);
638 1.1 matt
639 1.1 matt if (PT_LEAF_P(node)) {
640 1.1 matt #ifndef PTNOMASK
641 1.1 matt at_mask = PTN_ISMASK_P(ptn);
642 1.1 matt #endif
643 1.1 matt break;
644 1.1 matt }
645 1.1 matt
646 1.1 matt if (bitoff < branch_bitoff) {
647 1.1 matt if (!ptree_matchkey(pt, key, ptn, bitoff, branch_bitoff - bitoff)) {
648 1.1 matt #ifndef PTNOMASK
649 1.1 matt if (mask != NULL)
650 1.1 matt return NODETOITEM(pt, mask);
651 1.1 matt #endif
652 1.1 matt return NULL;
653 1.1 matt }
654 1.1 matt bitoff = branch_bitoff;
655 1.1 matt }
656 1.1 matt
657 1.1 matt #ifndef PTNOMASK
658 1.1 matt if (PTN_ISMASK_P(ptn) && PTN_BRANCH_BITLEN(ptn) == 0
659 1.1 matt && (!filter
660 1.1 matt || (*filter)(filter_arg, NODETOITEM(pt, ptn),
661 1.1 matt PT_FILTER_MASK)))
662 1.1 matt mask = ptn;
663 1.1 matt #endif
664 1.1 matt
665 1.1 matt parent = ptn;
666 1.1 matt parent_slot = ptree_testkey(pt, key, parent);
667 1.1 matt bitoff += PTN_BRANCH_BITLEN(parent);
668 1.1 matt }
669 1.1 matt
670 1.1 matt KASSERT(PTN_ISROOT_P(pt, parent) || PTN_BRANCH_BITOFF(parent) + PTN_BRANCH_BITLEN(parent) == bitoff);
671 1.1 matt if (!filter || (*filter)(filter_arg, NODETOITEM(pt, ptn), at_mask ? PT_FILTER_MASK : 0)) {
672 1.1 matt #ifndef PTNOMASK
673 1.1 matt if (PTN_ISMASK_P(ptn)) {
674 1.1 matt const pt_bitlen_t mask_len = PTN_MASK_BITLEN(ptn);
675 1.1 matt if (bitoff == PTN_MASK_BITLEN(ptn))
676 1.1 matt return NODETOITEM(pt, ptn);
677 1.1 matt if (ptree_matchkey(pt, key, ptn, bitoff, mask_len - bitoff))
678 1.1 matt return NODETOITEM(pt, ptn);
679 1.1 matt } else
680 1.1 matt #endif /* !PTNOMASK */
681 1.1 matt if (ptree_matchkey(pt, key, ptn, bitoff, UINT_MAX))
682 1.1 matt return NODETOITEM(pt, ptn);
683 1.1 matt }
684 1.1 matt
685 1.1 matt #ifndef PTNOMASK
686 1.1 matt /*
687 1.1 matt * By virtue of how the mask was placed in the tree,
688 1.1 matt * all nodes descended from it will match it. But the bits
689 1.1 matt * before the mask still need to be checked and since the
690 1.1 matt * mask was a branch, that was done implicitly.
691 1.1 matt */
692 1.1 matt if (mask != NULL) {
693 1.1 matt KASSERT(ptree_matchkey(pt, key, mask, 0, PTN_MASK_BITLEN(mask)));
694 1.1 matt return NODETOITEM(pt, mask);
695 1.1 matt }
696 1.1 matt #endif /* !PTNOMASK */
697 1.1 matt
698 1.1 matt /*
699 1.1 matt * Nothing matched.
700 1.1 matt */
701 1.1 matt return NULL;
702 1.1 matt }
703 1.1 matt
704 1.1 matt void *
705 1.1 matt ptree_iterate(pt_tree_t *pt, const void *item, pt_direction_t direction)
706 1.1 matt {
707 1.1 matt const pt_node_t * const target = ITEMTONODE(pt, item);
708 1.1 matt uintptr_t node, next_node;
709 1.1 matt
710 1.1 matt if (direction != PT_ASCENDING && direction != PT_DESCENDING)
711 1.1 matt return NULL;
712 1.1 matt
713 1.1 matt node = PTN_BRANCH_ROOT_SLOT(&pt->pt_rootnode);
714 1.1 matt if (PT_NULL_P(node))
715 1.1 matt return NULL;
716 1.1 matt
717 1.1 matt if (item == NULL) {
718 1.1 matt pt_node_t * const ptn = PT_NODE(node);
719 1.1 matt if (direction == PT_ASCENDING
720 1.1 matt && PTN_ISMASK_P(ptn) && PTN_BRANCH_BITLEN(ptn) == 0)
721 1.1 matt return NODETOITEM(pt, ptn);
722 1.1 matt next_node = node;
723 1.1 matt } else {
724 1.1 matt #ifndef PTNOMASK
725 1.1 matt uintptr_t mask_node = PT_NULL;
726 1.1 matt #endif /* !PTNOMASK */
727 1.1 matt next_node = PT_NULL;
728 1.1 matt while (!PT_LEAF_P(node)) {
729 1.1 matt pt_node_t * const ptn = PT_NODE(node);
730 1.1 matt pt_slot_t slot;
731 1.1 matt #ifndef PTNOMASK
732 1.1 matt if (PTN_ISMASK_P(ptn) && PTN_BRANCH_BITLEN(ptn) == 0) {
733 1.1 matt if (ptn == target)
734 1.1 matt break;
735 1.1 matt if (direction == PT_DESCENDING) {
736 1.1 matt mask_node = node;
737 1.1 matt next_node = PT_NULL;
738 1.1 matt }
739 1.1 matt }
740 1.1 matt #endif /* !PTNOMASK */
741 1.1 matt slot = ptree_testnode(pt, target, ptn);
742 1.1 matt node = PTN_BRANCH_SLOT(ptn, slot);
743 1.1 matt if (direction == PT_ASCENDING) {
744 1.1 matt if (slot != (1 << PTN_BRANCH_BITLEN(ptn)) - 1)
745 1.1 matt next_node = PTN_BRANCH_SLOT(ptn, slot + 1);
746 1.1 matt } else {
747 1.1 matt if (slot > 0) {
748 1.1 matt #ifndef PTNOMASK
749 1.1 matt mask_node = PT_NULL;
750 1.1 matt #endif /* !PTNOMASK */
751 1.1 matt next_node = PTN_BRANCH_SLOT(ptn, slot - 1);
752 1.1 matt }
753 1.1 matt }
754 1.1 matt }
755 1.1 matt if (PT_NODE(node) != target)
756 1.1 matt return NULL;
757 1.1 matt #ifndef PTNOMASK
758 1.1 matt if (PT_BRANCH_P(node)) {
759 1.1 matt pt_node_t *ptn = PT_NODE(node);
760 1.1 matt KASSERT(PTN_ISMASK_P(PT_NODE(node)) && PTN_BRANCH_BITLEN(PT_NODE(node)) == 0);
761 1.1 matt if (direction == PT_ASCENDING) {
762 1.1 matt next_node = PTN_BRANCH_ROOT_SLOT(ptn);
763 1.1 matt ptn = PT_NODE(next_node);
764 1.1 matt }
765 1.1 matt }
766 1.1 matt /*
767 1.1 matt * When descending, if we countered a mask node then that's
768 1.1 matt * we want to return.
769 1.1 matt */
770 1.1 matt if (direction == PT_DESCENDING && !PT_NULL_P(mask_node)) {
771 1.1 matt KASSERT(PT_NULL_P(next_node));
772 1.1 matt return NODETOITEM(pt, PT_NODE(mask_node));
773 1.1 matt }
774 1.1 matt #endif /* !PTNOMASK */
775 1.1 matt }
776 1.1 matt
777 1.1 matt node = next_node;
778 1.1 matt if (PT_NULL_P(node))
779 1.1 matt return NULL;
780 1.1 matt
781 1.1 matt while (!PT_LEAF_P(node)) {
782 1.1 matt pt_node_t * const ptn = PT_NODE(node);
783 1.1 matt pt_slot_t slot;
784 1.1 matt if (direction == PT_ASCENDING) {
785 1.1 matt #ifndef PTNOMASK
786 1.1 matt if (PT_BRANCH_P(node)
787 1.1 matt && PTN_ISMASK_P(ptn)
788 1.1 matt && PTN_BRANCH_BITLEN(ptn) == 0)
789 1.1 matt return NODETOITEM(pt, ptn);
790 1.1 matt #endif /* !PTNOMASK */
791 1.1 matt slot = PT_SLOT_LEFT;
792 1.1 matt } else {
793 1.1 matt slot = (1 << PTN_BRANCH_BITLEN(ptn)) - 1;
794 1.1 matt }
795 1.1 matt node = PTN_BRANCH_SLOT(ptn, slot);
796 1.1 matt }
797 1.1 matt return NODETOITEM(pt, PT_NODE(node));
798 1.1 matt }
799 1.1 matt
800 1.1 matt void
801 1.1 matt ptree_remove_node(pt_tree_t *pt, void *item)
802 1.1 matt {
803 1.1 matt pt_node_t * const target = ITEMTONODE(pt, item);
804 1.1 matt const pt_slot_t leaf_slot = PTN_LEAF_POSITION(target);
805 1.1 matt const pt_slot_t branch_slot = PTN_BRANCH_POSITION(target);
806 1.1 matt pt_node_t *ptn, *parent;
807 1.1 matt uintptr_t node;
808 1.1 matt uintptr_t *removep;
809 1.1 matt uintptr_t *nodep;
810 1.1 matt pt_bitoff_t bitoff;
811 1.1 matt pt_slot_t parent_slot;
812 1.1 matt #ifndef PTNOMASK
813 1.1 matt bool at_mask;
814 1.1 matt #endif
815 1.1 matt
816 1.1 matt if (PT_NULL_P(PTN_BRANCH_ROOT_SLOT(&pt->pt_rootnode))) {
817 1.1 matt KASSERT(!PT_NULL_P(PTN_BRANCH_ROOT_SLOT(&pt->pt_rootnode)));
818 1.1 matt return;
819 1.1 matt }
820 1.1 matt
821 1.1 matt bitoff = 0;
822 1.1 matt removep = NULL;
823 1.1 matt nodep = NULL;
824 1.1 matt parent = &pt->pt_rootnode;
825 1.1 matt parent_slot = PT_SLOT_ROOT;
826 1.1 matt for (;;) {
827 1.1 matt node = PTN_BRANCH_SLOT(parent, parent_slot);
828 1.1 matt ptn = PT_NODE(node);
829 1.1 matt #ifndef PTNOMASK
830 1.1 matt at_mask = PTN_ISMASK_P(ptn);
831 1.1 matt #endif
832 1.1 matt
833 1.1 matt if (PT_LEAF_P(node))
834 1.1 matt break;
835 1.1 matt
836 1.1 matt /*
837 1.1 matt * If we are at the target, then we are looking at its branch
838 1.1 matt * identity. We need to remember who's pointing at it so we
839 1.1 matt * stop them from doing that.
840 1.1 matt */
841 1.1 matt if (__predict_false(ptn == target)) {
842 1.1 matt KASSERT(nodep == NULL);
843 1.1 matt #ifndef PTNOMASK
844 1.1 matt /*
845 1.1 matt * Interior mask nodes are trivial to get rid of.
846 1.1 matt */
847 1.1 matt if (at_mask && PTN_BRANCH_BITLEN(ptn) == 0) {
848 1.1 matt PTN_BRANCH_SLOT(parent, parent_slot) =
849 1.1 matt PTN_BRANCH_ROOT_SLOT(ptn);
850 1.1 matt KASSERT(PT_NULL_P(PTN_BRANCH_ODDMAN_SLOT(ptn)));
851 1.1 matt PTREE_CHECK(pt);
852 1.1 matt return;
853 1.1 matt }
854 1.1 matt #endif /* !PTNOMASK */
855 1.1 matt nodep = &PTN_BRANCH_SLOT(parent, parent_slot);
856 1.1 matt KASSERT(*nodep == PTN_BRANCH(target));
857 1.1 matt }
858 1.1 matt /*
859 1.1 matt * We need also need to know who's pointing at our parent.
860 1.1 matt * After we remove ourselves from our parent, he'll only
861 1.1 matt * have one child and that's unacceptable. So we replace
862 1.1 matt * the pointer to the parent with our abadoned sibling.
863 1.1 matt */
864 1.1 matt removep = &PTN_BRANCH_SLOT(parent, parent_slot);
865 1.1 matt
866 1.1 matt /*
867 1.1 matt * Descend into the tree.
868 1.1 matt */
869 1.1 matt parent = ptn;
870 1.1 matt parent_slot = ptree_testnode(pt, target, parent);
871 1.1 matt bitoff += PTN_BRANCH_BITLEN(parent);
872 1.1 matt }
873 1.1 matt
874 1.1 matt /*
875 1.1 matt * We better have found that the leaf we are looking for is target.
876 1.1 matt */
877 1.1 matt if (target != ptn) {
878 1.1 matt KASSERT(target == ptn);
879 1.1 matt return;
880 1.1 matt }
881 1.1 matt
882 1.1 matt /*
883 1.1 matt * If we didn't encounter target as branch, then target must be the
884 1.1 matt * oddman-out.
885 1.1 matt */
886 1.1 matt if (nodep == NULL) {
887 1.1 matt KASSERT(PTN_BRANCH_ODDMAN_SLOT(&pt->pt_rootnode) == PTN_LEAF(target));
888 1.1 matt KASSERT(nodep == NULL);
889 1.1 matt nodep = &PTN_BRANCH_ODDMAN_SLOT(&pt->pt_rootnode);
890 1.1 matt }
891 1.1 matt
892 1.1 matt KASSERT((removep == NULL) == (parent == &pt->pt_rootnode));
893 1.1 matt
894 1.1 matt /*
895 1.1 matt * We have to special remove the last leaf from the root since
896 1.1 matt * the only time the tree can a PT_NULL node is when it's empty.
897 1.1 matt */
898 1.1 matt if (__predict_false(PTN_ISROOT_P(pt, parent))) {
899 1.1 matt KASSERT(removep == NULL);
900 1.1 matt KASSERT(parent == &pt->pt_rootnode);
901 1.1 matt KASSERT(nodep == &PTN_BRANCH_ODDMAN_SLOT(&pt->pt_rootnode));
902 1.1 matt KASSERT(*nodep == PTN_LEAF(target));
903 1.1 matt PTN_BRANCH_ROOT_SLOT(&pt->pt_rootnode) = PT_NULL;
904 1.1 matt PTN_BRANCH_ODDMAN_SLOT(&pt->pt_rootnode) = PT_NULL;
905 1.1 matt return;
906 1.1 matt }
907 1.1 matt
908 1.1 matt KASSERT((parent == target) == (removep == nodep));
909 1.1 matt if (PTN_BRANCH(parent) == PTN_BRANCH_SLOT(target, PTN_BRANCH_POSITION(parent))) {
910 1.1 matt /*
911 1.1 matt * The pointer to the parent actually lives in the target's
912 1.1 matt * branch identity. We can't just move the target's branch
913 1.1 matt * identity since that would result in the parent pointing
914 1.1 matt * to its own branch identity and that's fobidden.
915 1.1 matt */
916 1.1 matt const pt_slot_t slot = PTN_BRANCH_POSITION(parent);
917 1.1 matt const pt_slot_t other_slot = slot ^ PT_SLOT_OTHER;
918 1.1 matt const pt_bitlen_t parent_bitlen = PTN_BRANCH_BITLEN(parent);
919 1.1 matt
920 1.1 matt KASSERT(PTN_BRANCH_BITOFF(target) < PTN_BRANCH_BITOFF(parent));
921 1.1 matt
922 1.1 matt /*
923 1.1 matt * This gets so confusing. The target's branch identity
924 1.1 matt * points to the branch identity of the parent of the target's
925 1.1 matt * leaf identity:
926 1.1 matt *
927 1.1 matt * TB = { X, PB = { TL, Y } }
928 1.1 matt * or TB = { X, PB = { TL } }
929 1.1 matt *
930 1.1 matt * So we can't move the target's branch identity to the parent
931 1.1 matt * because that would corrupt the tree.
932 1.1 matt */
933 1.1 matt if (__predict_true(parent_bitlen > 0)) {
934 1.1 matt /*
935 1.1 matt * The parent is a two-way branch. We have to have
936 1.1 matt * do to this chang in two steps to keep internally
937 1.1 matt * consistent. First step is to copy our sibling from
938 1.1 matt * our parent to where we are pointing to parent's
939 1.1 matt * branch identiy. This remove all references to his
940 1.1 matt * branch identity from the tree. We then simply make
941 1.1 matt * the parent assume the target's branching duties.
942 1.1 matt *
943 1.1 matt * TB = { X, PB = { Y, TL } } --> PB = { X, Y }.
944 1.1 matt * TB = { X, PB = { TL, Y } } --> PB = { X, Y }.
945 1.1 matt * TB = { PB = { Y, TL }, X } --> PB = { Y, X }.
946 1.1 matt * TB = { PB = { TL, Y }, X } --> PB = { Y, X }.
947 1.1 matt */
948 1.1 matt PTN_BRANCH_SLOT(target, slot) =
949 1.1 matt PTN_BRANCH_SLOT(parent, parent_slot ^ PT_SLOT_OTHER);
950 1.1 matt *nodep = ptree_move_branch(pt, parent, target);
951 1.1 matt PTREE_CHECK(pt);
952 1.1 matt return;
953 1.1 matt } else {
954 1.1 matt /*
955 1.1 matt * If parent was a one-way branch, it must have been
956 1.1 matt * mask which pointed to a single leaf which we are
957 1.1 matt * removing. This means we have to convert the
958 1.1 matt * parent back to a leaf node. So in the same
959 1.1 matt * position that target pointed to parent, we place
960 1.1 matt * leaf pointer to parent. In the other position,
961 1.1 matt * we just put the other node from target.
962 1.1 matt *
963 1.1 matt * TB = { X, PB = { TL } } --> PB = { X, PL }
964 1.1 matt */
965 1.1 matt KASSERT(PTN_ISMASK_P(parent));
966 1.1 matt KASSERT(slot == ptree_testnode(pt, parent, target));
967 1.1 matt PTN_BRANCH_SLOT(parent, slot) = PTN_LEAF(parent);
968 1.1 matt PTN_BRANCH_SLOT(parent, other_slot) =
969 1.1 matt PTN_BRANCH_SLOT(target, other_slot);
970 1.1 matt PTN_SET_LEAF_POSITION(parent,slot);
971 1.1 matt PTN_SET_BRANCH_BITLEN(parent, 1);
972 1.1 matt }
973 1.1 matt PTN_SET_BRANCH_BITOFF(parent, PTN_BRANCH_BITOFF(target));
974 1.1 matt PTN_SET_BRANCH_POSITION(parent, PTN_BRANCH_POSITION(target));
975 1.1 matt
976 1.1 matt *nodep = PTN_BRANCH(parent);
977 1.1 matt PTREE_CHECK(pt);
978 1.1 matt return;
979 1.1 matt }
980 1.1 matt
981 1.1 matt #ifndef PTNOMASK
982 1.1 matt if (__predict_false(PTN_BRANCH_BITLEN(parent) == 0)) {
983 1.1 matt /*
984 1.1 matt * Parent was a one-way branch which is changing back to a leaf.
985 1.1 matt * Since parent is no longer a one-way branch, it can take over
986 1.1 matt * target's branching duties.
987 1.1 matt *
988 1.1 matt * GB = { PB = { TL } } --> GB = { PL }
989 1.1 matt * TB = { X, Y } --> PB = { X, Y }
990 1.1 matt */
991 1.1 matt KASSERT(PTN_ISMASK_P(parent));
992 1.1 matt KASSERT(parent != target);
993 1.1 matt *removep = PTN_LEAF(parent);
994 1.1 matt } else
995 1.1 matt #endif /* !PTNOMASK */
996 1.1 matt {
997 1.1 matt /*
998 1.1 matt * Now we are the normal removal case. Since after the
999 1.1 matt * target's leaf identity is removed from the its parent,
1000 1.1 matt * that parent will only have one decendent. So we can
1001 1.1 matt * just as easily replace the node that has the parent's
1002 1.1 matt * branch identity with the surviving node. This freeing
1003 1.1 matt * parent from its branching duties which means it can
1004 1.1 matt * take over target's branching duties.
1005 1.1 matt *
1006 1.1 matt * GB = { PB = { X, TL } } --> GB = { X }
1007 1.1 matt * TB = { V, W } --> PB = { V, W }
1008 1.1 matt */
1009 1.1 matt const pt_slot_t other_slot = parent_slot ^ PT_SLOT_OTHER;
1010 1.1 matt uintptr_t other_node = PTN_BRANCH_SLOT(parent, other_slot);
1011 1.1 matt const pt_slot_t target_slot = (parent == target ? branch_slot : leaf_slot);
1012 1.1 matt
1013 1.1 matt *removep = other_node;
1014 1.1 matt
1015 1.1 matt ptree_set_position(other_node, target_slot);
1016 1.1 matt
1017 1.1 matt /*
1018 1.1 matt * If target's branch identity contained its leaf identity, we
1019 1.1 matt * have nothing left to do. We've already moved 'X' so there
1020 1.1 matt * is no longer anything in the target's branch identiy that
1021 1.1 matt * has to be preserved.
1022 1.1 matt */
1023 1.1 matt if (parent == target) {
1024 1.1 matt /*
1025 1.1 matt * GB = { TB = { X, TL } } --> GB = { X }
1026 1.1 matt * TB = { X, TL } --> don't care
1027 1.1 matt */
1028 1.1 matt PTREE_CHECK(pt);
1029 1.1 matt return;
1030 1.1 matt }
1031 1.1 matt }
1032 1.1 matt
1033 1.1 matt /*
1034 1.1 matt * If target wasn't used as a branch, then it must have been the
1035 1.1 matt * oddman-out of the tree (the one node that doesn't have a branch
1036 1.1 matt * identity). This makes parent the new oddman-out.
1037 1.1 matt */
1038 1.1 matt if (*nodep == PTN_LEAF(target)) {
1039 1.1 matt KASSERT(nodep == &PTN_BRANCH_ODDMAN_SLOT(&pt->pt_rootnode));
1040 1.1 matt PTN_BRANCH_ODDMAN_SLOT(&pt->pt_rootnode) = PTN_LEAF(parent);
1041 1.1 matt PTREE_CHECK(pt);
1042 1.1 matt return;
1043 1.1 matt }
1044 1.1 matt
1045 1.1 matt /*
1046 1.1 matt * Finally move the target's branching duties to the parent.
1047 1.1 matt */
1048 1.1 matt KASSERT(PTN_BRANCH_BITOFF(parent) > PTN_BRANCH_BITOFF(target));
1049 1.1 matt *nodep = ptree_move_branch(pt, parent, target);
1050 1.1 matt PTREE_CHECK(pt);
1051 1.1 matt }
1052 1.1 matt
1053 1.1 matt #ifdef PTCHECK
1054 1.1 matt static const pt_node_t *
1055 1.1 matt ptree_check_find_node2(const pt_tree_t *pt, const pt_node_t *parent,
1056 1.1 matt uintptr_t target)
1057 1.1 matt {
1058 1.1 matt const pt_bitlen_t slots = 1 << PTN_BRANCH_BITLEN(parent);
1059 1.1 matt pt_slot_t slot;
1060 1.1 matt
1061 1.1 matt for (slot = 0; slot < slots; slot++) {
1062 1.1 matt const uintptr_t node = PTN_BRANCH_SLOT(parent, slot);
1063 1.1 matt if (PTN_BRANCH_SLOT(parent, slot) == node)
1064 1.1 matt return parent;
1065 1.1 matt }
1066 1.1 matt for (slot = 0; slot < slots; slot++) {
1067 1.1 matt const uintptr_t node = PTN_BRANCH_SLOT(parent, slot);
1068 1.1 matt const pt_node_t *branch;
1069 1.1 matt if (!PT_BRANCH_P(node))
1070 1.1 matt continue;
1071 1.1 matt branch = ptree_check_find_node2(pt, PT_NODE(node), target);
1072 1.1 matt if (branch != NULL)
1073 1.1 matt return branch;
1074 1.1 matt }
1075 1.1 matt
1076 1.1 matt return NULL;
1077 1.1 matt }
1078 1.1 matt
1079 1.1 matt static bool
1080 1.1 matt ptree_check_leaf(const pt_tree_t *pt, const pt_node_t *parent,
1081 1.1 matt const pt_node_t *ptn)
1082 1.1 matt {
1083 1.1 matt const pt_bitoff_t leaf_position = PTN_LEAF_POSITION(ptn);
1084 1.1 matt const pt_bitlen_t bitlen = PTN_BRANCH_BITLEN(ptn);
1085 1.1 matt const pt_bitlen_t mask_len = PTN_MASK_BITLEN(ptn);
1086 1.1 matt const uintptr_t leaf_node = PTN_LEAF(ptn);
1087 1.1 matt const bool is_parent_root = (parent == &pt->pt_rootnode);
1088 1.1 matt const bool is_mask = PTN_ISMASK_P(ptn);
1089 1.1 matt bool ok = true;
1090 1.1 matt
1091 1.1 matt if (is_parent_root) {
1092 1.1 matt ok = ok && PTN_BRANCH_ODDMAN_SLOT(parent) == leaf_node;
1093 1.1 matt KASSERT(ok);
1094 1.1 matt return ok;
1095 1.1 matt }
1096 1.1 matt
1097 1.1 matt if (is_mask && PTN_ISMASK_P(parent) && PTN_BRANCH_BITLEN(parent) == 0) {
1098 1.1 matt ok = ok && PTN_MASK_BITLEN(parent) < mask_len;
1099 1.1 matt KASSERT(ok);
1100 1.1 matt ok = ok && PTN_BRANCH_BITOFF(parent) < mask_len;
1101 1.1 matt KASSERT(ok);
1102 1.1 matt }
1103 1.1 matt ok = ok && PTN_BRANCH_SLOT(parent, leaf_position) == leaf_node;
1104 1.1 matt KASSERT(ok);
1105 1.1 matt ok = ok && leaf_position == ptree_testnode(pt, ptn, parent);
1106 1.1 matt KASSERT(ok);
1107 1.1 matt if (PTN_BRANCH_ODDMAN_SLOT(&pt->pt_rootnode) != leaf_node) {
1108 1.1 matt ok = ok && bitlen > 0;
1109 1.1 matt KASSERT(ok);
1110 1.1 matt ok = ok && ptn == ptree_check_find_node2(pt, ptn, PTN_LEAF(ptn));
1111 1.1 matt KASSERT(ok);
1112 1.1 matt }
1113 1.1 matt return ok;
1114 1.1 matt }
1115 1.1 matt
1116 1.1 matt static bool
1117 1.1 matt ptree_check_branch(const pt_tree_t *pt, const pt_node_t *parent,
1118 1.1 matt const pt_node_t *ptn)
1119 1.1 matt {
1120 1.1 matt const bool is_parent_root = (parent == &pt->pt_rootnode);
1121 1.1 matt const pt_slot_t branch_slot = PTN_BRANCH_POSITION(ptn);
1122 1.1 matt const pt_bitoff_t bitoff = PTN_BRANCH_BITOFF(ptn);
1123 1.1 matt const pt_bitoff_t bitlen = PTN_BRANCH_BITLEN(ptn);
1124 1.1 matt const pt_bitoff_t parent_bitoff = PTN_BRANCH_BITOFF(parent);
1125 1.1 matt const pt_bitoff_t parent_bitlen = PTN_BRANCH_BITLEN(parent);
1126 1.1 matt const bool is_parent_mask = PTN_ISMASK_P(parent) && parent_bitlen == 0;
1127 1.1 matt const bool is_mask = PTN_ISMASK_P(ptn) && bitlen == 0;
1128 1.1 matt const pt_bitoff_t parent_mask_len = PTN_MASK_BITLEN(parent);
1129 1.1 matt const pt_bitoff_t mask_len = PTN_MASK_BITLEN(ptn);
1130 1.1 matt const pt_bitlen_t slots = 1 << bitlen;
1131 1.1 matt pt_slot_t slot;
1132 1.1 matt bool ok = true;
1133 1.1 matt
1134 1.1 matt ok = ok && PTN_BRANCH_SLOT(parent, branch_slot) == PTN_BRANCH(ptn);
1135 1.1 matt KASSERT(ok);
1136 1.1 matt ok = ok && branch_slot == ptree_testnode(pt, ptn, parent);
1137 1.1 matt KASSERT(ok);
1138 1.1 matt
1139 1.1 matt if (is_mask) {
1140 1.1 matt ok = ok && bitoff == mask_len;
1141 1.1 matt KASSERT(ok);
1142 1.1 matt if (is_parent_mask) {
1143 1.1 matt ok = ok && parent_mask_len < mask_len;
1144 1.1 matt KASSERT(ok);
1145 1.1 matt ok = ok && parent_bitoff < bitoff;
1146 1.1 matt KASSERT(ok);
1147 1.1 matt }
1148 1.1 matt } else {
1149 1.1 matt if (is_parent_mask) {
1150 1.1 matt ok = ok && parent_bitoff <= bitoff;
1151 1.1 matt } else if (!is_parent_root) {
1152 1.1 matt ok = ok && parent_bitoff < bitoff;
1153 1.1 matt }
1154 1.1 matt KASSERT(ok);
1155 1.1 matt }
1156 1.1 matt
1157 1.1 matt for (slot = 0; slot < slots; slot++) {
1158 1.1 matt const uintptr_t node = PTN_BRANCH_SLOT(ptn, slot);
1159 1.1 matt pt_bitoff_t tmp_bitoff = 0;
1160 1.1 matt pt_slot_t tmp_slot;
1161 1.1 matt ok = ok && node != PTN_BRANCH(ptn);
1162 1.1 matt KASSERT(ok);
1163 1.1 matt if (bitlen > 0) {
1164 1.1 matt ok = ok && ptree_matchnode(pt, PT_NODE(node), ptn, bitoff, &tmp_bitoff, &tmp_slot);
1165 1.1 matt KASSERT(ok);
1166 1.1 matt tmp_slot = ptree_testnode(pt, PT_NODE(node), ptn);
1167 1.1 matt ok = ok && slot == tmp_slot;
1168 1.1 matt KASSERT(ok);
1169 1.1 matt }
1170 1.1 matt if (PT_LEAF_P(node))
1171 1.1 matt ok = ok && ptree_check_leaf(pt, ptn, PT_NODE(node));
1172 1.1 matt else
1173 1.1 matt ok = ok && ptree_check_branch(pt, ptn, PT_NODE(node));
1174 1.1 matt }
1175 1.1 matt
1176 1.1 matt return ok;
1177 1.1 matt }
1178 1.1 matt #endif /* PTCHECK */
1179 1.1 matt
1180 1.2 matt /*ARGSUSED*/
1181 1.1 matt bool
1182 1.1 matt ptree_check(const pt_tree_t *pt)
1183 1.1 matt {
1184 1.1 matt bool ok = true;
1185 1.1 matt #ifdef PTCHECK
1186 1.1 matt const pt_node_t * const parent = &pt->pt_rootnode;
1187 1.1 matt const uintptr_t node = pt->pt_root;
1188 1.1 matt const pt_node_t * const ptn = PT_NODE(node);
1189 1.1 matt
1190 1.1 matt ok = ok && PTN_BRANCH_BITOFF(parent) == 0;
1191 1.1 matt ok = ok && !PTN_ISMASK_P(parent);
1192 1.1 matt
1193 1.1 matt if (PT_NULL_P(node))
1194 1.1 matt return ok;
1195 1.1 matt
1196 1.1 matt if (PT_LEAF_P(node))
1197 1.1 matt ok = ok && ptree_check_leaf(pt, parent, ptn);
1198 1.1 matt else
1199 1.1 matt ok = ok && ptree_check_branch(pt, parent, ptn);
1200 1.1 matt #endif
1201 1.1 matt return ok;
1202 1.1 matt }
1203