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