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