]>
git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blob - zfs/module/avl/avl.c
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
22 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
27 * Copyright (c) 2014 by Delphix. All rights reserved.
31 * AVL - generic AVL tree implementation for kernel use
33 * A complete description of AVL trees can be found in many CS textbooks.
35 * Here is a very brief overview. An AVL tree is a binary search tree that is
36 * almost perfectly balanced. By "almost" perfectly balanced, we mean that at
37 * any given node, the left and right subtrees are allowed to differ in height
40 * This relaxation from a perfectly balanced binary tree allows doing
41 * insertion and deletion relatively efficiently. Searching the tree is
42 * still a fast operation, roughly O(log(N)).
44 * The key to insertion and deletion is a set of tree manipulations called
45 * rotations, which bring unbalanced subtrees back into the semi-balanced state.
47 * This implementation of AVL trees has the following peculiarities:
49 * - The AVL specific data structures are physically embedded as fields
50 * in the "using" data structures. To maintain generality the code
51 * must constantly translate between "avl_node_t *" and containing
52 * data structure "void *"s by adding/subtracting the avl_offset.
54 * - Since the AVL data is always embedded in other structures, there is
55 * no locking or memory allocation in the AVL routines. This must be
56 * provided for by the enclosing data structure's semantics. Typically,
57 * avl_insert()/_add()/_remove()/avl_insert_here() require some kind of
58 * exclusive write lock. Other operations require a read lock.
60 * - The implementation uses iteration instead of explicit recursion,
61 * since it is intended to run on limited size kernel stacks. Since
62 * there is no recursion stack present to move "up" in the tree,
63 * there is an explicit "parent" link in the avl_node_t.
65 * - The left/right children pointers of a node are in an array.
66 * In the code, variables (instead of constants) are used to represent
67 * left and right indices. The implementation is written as if it only
68 * dealt with left handed manipulations. By changing the value assigned
69 * to "left", the code also works for right handed trees. The
70 * following variables/terms are frequently used:
72 * int left; // 0 when dealing with left children,
73 * // 1 for dealing with right children
75 * int left_heavy; // -1 when left subtree is taller at some node,
76 * // +1 when right subtree is taller
78 * int right; // will be the opposite of left (0 or 1)
79 * int right_heavy;// will be the opposite of left_heavy (-1 or 1)
81 * int direction; // 0 for "<" (ie. left child); 1 for ">" (right)
83 * Though it is a little more confusing to read the code, the approach
84 * allows using half as much code (and hence cache footprint) for tree
85 * manipulations and eliminates many conditional branches.
87 * - The avl_index_t is an opaque "cookie" used to find nodes at or
88 * adjacent to where a new value would be inserted in the tree. The value
89 * is a modified "avl_node_t *". The bottom bit (normally 0 for a
90 * pointer) is set to indicate if that the new node has a value greater
91 * than the value of the indicated "avl_node_t *".
93 * Note - in addition to userland (e.g. libavl and libutil) and the kernel
94 * (e.g. genunix), avl.c is compiled into ld.so and kmdb's genunix module,
95 * which each have their own compilation environments and subsequent
96 * requirements. Each of these environments must be considered when adding
97 * dependencies from avl.c.
100 #include <sys/types.h>
101 #include <sys/param.h>
102 #include <sys/debug.h>
104 #include <sys/cmn_err.h>
107 * Small arrays to translate between balance (or diff) values and child indices.
109 * Code that deals with binary tree data structures will randomly use
110 * left and right children when examining a tree. C "if()" statements
111 * which evaluate randomly suffer from very poor hardware branch prediction.
112 * In this code we avoid some of the branch mispredictions by using the
113 * following translation arrays. They replace random branches with an
114 * additional memory reference. Since the translation arrays are both very
115 * small the data should remain efficiently in cache.
117 static const int avl_child2balance
[2] = {-1, 1};
118 static const int avl_balance2child
[] = {0, 0, 1};
122 * Walk from one node to the previous valued node (ie. an infix walk
123 * towards the left). At any given node we do one of 2 things:
125 * - If there is a left child, go to it, then to it's rightmost descendant.
127 * - otherwise we return through parent nodes until we've come from a right
131 * NULL - if at the end of the nodes
132 * otherwise next node
135 avl_walk(avl_tree_t
*tree
, void *oldnode
, int left
)
137 size_t off
= tree
->avl_offset
;
138 avl_node_t
*node
= AVL_DATA2NODE(oldnode
, off
);
139 int right
= 1 - left
;
144 * nowhere to walk to if tree is empty
150 * Visit the previous valued node. There are two possibilities:
152 * If this node has a left child, go down one left, then all
155 if (node
->avl_child
[left
] != NULL
) {
156 for (node
= node
->avl_child
[left
];
157 node
->avl_child
[right
] != NULL
;
158 node
= node
->avl_child
[right
])
161 * Otherwise, return thru left children as far as we can.
165 was_child
= AVL_XCHILD(node
);
166 node
= AVL_XPARENT(node
);
169 if (was_child
== right
)
174 return (AVL_NODE2DATA(node
, off
));
178 * Return the lowest valued node in a tree or NULL.
179 * (leftmost child from root of tree)
182 avl_first(avl_tree_t
*tree
)
185 avl_node_t
*prev
= NULL
;
186 size_t off
= tree
->avl_offset
;
188 for (node
= tree
->avl_root
; node
!= NULL
; node
= node
->avl_child
[0])
192 return (AVL_NODE2DATA(prev
, off
));
197 * Return the highest valued node in a tree or NULL.
198 * (rightmost child from root of tree)
201 avl_last(avl_tree_t
*tree
)
204 avl_node_t
*prev
= NULL
;
205 size_t off
= tree
->avl_offset
;
207 for (node
= tree
->avl_root
; node
!= NULL
; node
= node
->avl_child
[1])
211 return (AVL_NODE2DATA(prev
, off
));
216 * Access the node immediately before or after an insertion point.
218 * "avl_index_t" is a (avl_node_t *) with the bottom bit indicating a child
221 * NULL: no node in the given direction
222 * "void *" of the found tree node
225 avl_nearest(avl_tree_t
*tree
, avl_index_t where
, int direction
)
227 int child
= AVL_INDEX2CHILD(where
);
228 avl_node_t
*node
= AVL_INDEX2NODE(where
);
230 size_t off
= tree
->avl_offset
;
233 ASSERT(tree
->avl_root
== NULL
);
236 data
= AVL_NODE2DATA(node
, off
);
237 if (child
!= direction
)
240 return (avl_walk(tree
, data
, direction
));
245 * Search for the node which contains "value". The algorithm is a
246 * simple binary tree search.
249 * NULL: the value is not in the AVL tree
250 * *where (if not NULL) is set to indicate the insertion point
251 * "void *" of the found tree node
254 avl_find(avl_tree_t
*tree
, const void *value
, avl_index_t
*where
)
257 avl_node_t
*prev
= NULL
;
260 size_t off
= tree
->avl_offset
;
262 for (node
= tree
->avl_root
; node
!= NULL
;
263 node
= node
->avl_child
[child
]) {
267 diff
= tree
->avl_compar(value
, AVL_NODE2DATA(node
, off
));
268 ASSERT(-1 <= diff
&& diff
<= 1);
274 return (AVL_NODE2DATA(node
, off
));
276 child
= avl_balance2child
[1 + diff
];
281 *where
= AVL_MKINDEX(prev
, child
);
288 * Perform a rotation to restore balance at the subtree given by depth.
290 * This routine is used by both insertion and deletion. The return value
292 * 0 : subtree did not change height
293 * !0 : subtree was reduced in height
295 * The code is written as if handling left rotations, right rotations are
296 * symmetric and handled by swapping values of variables right/left[_heavy]
298 * On input balance is the "new" balance at "node". This value is either
302 avl_rotation(avl_tree_t
*tree
, avl_node_t
*node
, int balance
)
304 int left
= !(balance
< 0); /* when balance = -2, left will be 0 */
305 int right
= 1 - left
;
306 int left_heavy
= balance
>> 1;
307 int right_heavy
= -left_heavy
;
308 avl_node_t
*parent
= AVL_XPARENT(node
);
309 avl_node_t
*child
= node
->avl_child
[left
];
314 int which_child
= AVL_XCHILD(node
);
315 int child_bal
= AVL_XBALANCE(child
);
319 * case 1 : node is overly left heavy, the left child is balanced or
320 * also left heavy. This requires the following rotation.
325 * (child bal:0 or -1)
340 * we detect this situation by noting that child's balance is not
344 if (child_bal
!= right_heavy
) {
347 * compute new balance of nodes
349 * If child used to be left heavy (now balanced) we reduced
350 * the height of this sub-tree -- used in "return...;" below
352 child_bal
+= right_heavy
; /* adjust towards right */
355 * move "cright" to be node's left child
357 cright
= child
->avl_child
[right
];
358 node
->avl_child
[left
] = cright
;
359 if (cright
!= NULL
) {
360 AVL_SETPARENT(cright
, node
);
361 AVL_SETCHILD(cright
, left
);
365 * move node to be child's right child
367 child
->avl_child
[right
] = node
;
368 AVL_SETBALANCE(node
, -child_bal
);
369 AVL_SETCHILD(node
, right
);
370 AVL_SETPARENT(node
, child
);
373 * update the pointer into this subtree
375 AVL_SETBALANCE(child
, child_bal
);
376 AVL_SETCHILD(child
, which_child
);
377 AVL_SETPARENT(child
, parent
);
379 parent
->avl_child
[which_child
] = child
;
381 tree
->avl_root
= child
;
383 return (child_bal
== 0);
388 * case 2 : When node is left heavy, but child is right heavy we use
389 * a different rotation.
409 * (child b:?) (node b:?)
414 * computing the new balances is more complicated. As an example:
415 * if gchild was right_heavy, then child is now left heavy
416 * else it is balanced
419 gchild
= child
->avl_child
[right
];
420 gleft
= gchild
->avl_child
[left
];
421 gright
= gchild
->avl_child
[right
];
424 * move gright to left child of node and
426 * move gleft to right child of node
428 node
->avl_child
[left
] = gright
;
429 if (gright
!= NULL
) {
430 AVL_SETPARENT(gright
, node
);
431 AVL_SETCHILD(gright
, left
);
434 child
->avl_child
[right
] = gleft
;
436 AVL_SETPARENT(gleft
, child
);
437 AVL_SETCHILD(gleft
, right
);
441 * move child to left child of gchild and
443 * move node to right child of gchild and
445 * fixup parent of all this to point to gchild
447 balance
= AVL_XBALANCE(gchild
);
448 gchild
->avl_child
[left
] = child
;
449 AVL_SETBALANCE(child
, (balance
== right_heavy
? left_heavy
: 0));
450 AVL_SETPARENT(child
, gchild
);
451 AVL_SETCHILD(child
, left
);
453 gchild
->avl_child
[right
] = node
;
454 AVL_SETBALANCE(node
, (balance
== left_heavy
? right_heavy
: 0));
455 AVL_SETPARENT(node
, gchild
);
456 AVL_SETCHILD(node
, right
);
458 AVL_SETBALANCE(gchild
, 0);
459 AVL_SETPARENT(gchild
, parent
);
460 AVL_SETCHILD(gchild
, which_child
);
462 parent
->avl_child
[which_child
] = gchild
;
464 tree
->avl_root
= gchild
;
466 return (1); /* the new tree is always shorter */
471 * Insert a new node into an AVL tree at the specified (from avl_find()) place.
473 * Newly inserted nodes are always leaf nodes in the tree, since avl_find()
474 * searches out to the leaf positions. The avl_index_t indicates the node
475 * which will be the parent of the new node.
477 * After the node is inserted, a single rotation further up the tree may
478 * be necessary to maintain an acceptable AVL balance.
481 avl_insert(avl_tree_t
*tree
, void *new_data
, avl_index_t where
)
484 avl_node_t
*parent
= AVL_INDEX2NODE(where
);
487 int which_child
= AVL_INDEX2CHILD(where
);
488 size_t off
= tree
->avl_offset
;
492 ASSERT(((uintptr_t)new_data
& 0x7) == 0);
495 node
= AVL_DATA2NODE(new_data
, off
);
498 * First, add the node to the tree at the indicated position.
500 ++tree
->avl_numnodes
;
502 node
->avl_child
[0] = NULL
;
503 node
->avl_child
[1] = NULL
;
505 AVL_SETCHILD(node
, which_child
);
506 AVL_SETBALANCE(node
, 0);
507 AVL_SETPARENT(node
, parent
);
508 if (parent
!= NULL
) {
509 ASSERT(parent
->avl_child
[which_child
] == NULL
);
510 parent
->avl_child
[which_child
] = node
;
512 ASSERT(tree
->avl_root
== NULL
);
513 tree
->avl_root
= node
;
516 * Now, back up the tree modifying the balance of all nodes above the
517 * insertion point. If we get to a highly unbalanced ancestor, we
518 * need to do a rotation. If we back out of the tree we are done.
519 * If we brought any subtree into perfect balance (0), we are also done.
527 * Compute the new balance
529 old_balance
= AVL_XBALANCE(node
);
530 new_balance
= old_balance
+ avl_child2balance
[which_child
];
533 * If we introduced equal balance, then we are done immediately
535 if (new_balance
== 0) {
536 AVL_SETBALANCE(node
, 0);
541 * If both old and new are not zero we went
542 * from -1 to -2 balance, do a rotation.
544 if (old_balance
!= 0)
547 AVL_SETBALANCE(node
, new_balance
);
548 parent
= AVL_XPARENT(node
);
549 which_child
= AVL_XCHILD(node
);
553 * perform a rotation to fix the tree and return
555 (void) avl_rotation(tree
, node
, new_balance
);
559 * Insert "new_data" in "tree" in the given "direction" either after or
560 * before (AVL_AFTER, AVL_BEFORE) the data "here".
562 * Insertions can only be done at empty leaf points in the tree, therefore
563 * if the given child of the node is already present we move to either
564 * the AVL_PREV or AVL_NEXT and reverse the insertion direction. Since
565 * every other node in the tree is a leaf, this always works.
567 * To help developers using this interface, we assert that the new node
568 * is correctly ordered at every step of the way in DEBUG kernels.
578 int child
= direction
; /* rely on AVL_BEFORE == 0, AVL_AFTER == 1 */
583 ASSERT(tree
!= NULL
);
584 ASSERT(new_data
!= NULL
);
585 ASSERT(here
!= NULL
);
586 ASSERT(direction
== AVL_BEFORE
|| direction
== AVL_AFTER
);
589 * If corresponding child of node is not NULL, go to the neighboring
590 * node and reverse the insertion direction.
592 node
= AVL_DATA2NODE(here
, tree
->avl_offset
);
595 diff
= tree
->avl_compar(new_data
, here
);
596 ASSERT(-1 <= diff
&& diff
<= 1);
598 ASSERT(diff
> 0 ? child
== 1 : child
== 0);
601 if (node
->avl_child
[child
] != NULL
) {
602 node
= node
->avl_child
[child
];
604 while (node
->avl_child
[child
] != NULL
) {
606 diff
= tree
->avl_compar(new_data
,
607 AVL_NODE2DATA(node
, tree
->avl_offset
));
608 ASSERT(-1 <= diff
&& diff
<= 1);
610 ASSERT(diff
> 0 ? child
== 1 : child
== 0);
612 node
= node
->avl_child
[child
];
615 diff
= tree
->avl_compar(new_data
,
616 AVL_NODE2DATA(node
, tree
->avl_offset
));
617 ASSERT(-1 <= diff
&& diff
<= 1);
619 ASSERT(diff
> 0 ? child
== 1 : child
== 0);
622 ASSERT(node
->avl_child
[child
] == NULL
);
624 avl_insert(tree
, new_data
, AVL_MKINDEX(node
, child
));
628 * Add a new node to an AVL tree.
631 avl_add(avl_tree_t
*tree
, void *new_node
)
633 avl_index_t where
= 0;
636 * This is unfortunate. We want to call panic() here, even for
637 * non-DEBUG kernels. In userland, however, we can't depend on anything
638 * in libc or else the rtld build process gets confused. So, all we can
639 * do in userland is resort to a normal ASSERT().
641 if (avl_find(tree
, new_node
, &where
) != NULL
)
643 panic("avl_find() succeeded inside avl_add()");
647 avl_insert(tree
, new_node
, where
);
651 * Delete a node from the AVL tree. Deletion is similar to insertion, but
652 * with 2 complications.
654 * First, we may be deleting an interior node. Consider the following subtree:
662 * When we are deleting node (d), we find and bring up an adjacent valued leaf
663 * node, say (c), to take the interior node's place. In the code this is
664 * handled by temporarily swapping (d) and (c) in the tree and then using
665 * common code to delete (d) from the leaf position.
667 * Secondly, an interior deletion from a deep tree may require more than one
668 * rotation to fix the balance. This is handled by moving up the tree through
669 * parents and applying rotations as needed. The return value from
670 * avl_rotation() is used to detect when a subtree did not change overall
671 * height due to a rotation.
674 avl_remove(avl_tree_t
*tree
, void *data
)
685 size_t off
= tree
->avl_offset
;
689 delete = AVL_DATA2NODE(data
, off
);
692 * Deletion is easiest with a node that has at most 1 child.
693 * We swap a node with 2 children with a sequentially valued
694 * neighbor node. That node will have at most 1 child. Note this
695 * has no effect on the ordering of the remaining nodes.
697 * As an optimization, we choose the greater neighbor if the tree
698 * is right heavy, otherwise the left neighbor. This reduces the
699 * number of rotations needed.
701 if (delete->avl_child
[0] != NULL
&& delete->avl_child
[1] != NULL
) {
704 * choose node to swap from whichever side is taller
706 old_balance
= AVL_XBALANCE(delete);
707 left
= avl_balance2child
[old_balance
+ 1];
711 * get to the previous value'd node
712 * (down 1 left, as far as possible right)
714 for (node
= delete->avl_child
[left
];
715 node
->avl_child
[right
] != NULL
;
716 node
= node
->avl_child
[right
])
720 * create a temp placeholder for 'node'
721 * move 'node' to delete's spot in the tree
726 if (node
->avl_child
[left
] == node
)
727 node
->avl_child
[left
] = &tmp
;
729 parent
= AVL_XPARENT(node
);
731 parent
->avl_child
[AVL_XCHILD(node
)] = node
;
733 tree
->avl_root
= node
;
734 AVL_SETPARENT(node
->avl_child
[left
], node
);
735 AVL_SETPARENT(node
->avl_child
[right
], node
);
738 * Put tmp where node used to be (just temporary).
739 * It always has a parent and at most 1 child.
742 parent
= AVL_XPARENT(delete);
743 parent
->avl_child
[AVL_XCHILD(delete)] = delete;
744 which_child
= (delete->avl_child
[1] != 0);
745 if (delete->avl_child
[which_child
] != NULL
)
746 AVL_SETPARENT(delete->avl_child
[which_child
], delete);
751 * Here we know "delete" is at least partially a leaf node. It can
752 * be easily removed from the tree.
754 ASSERT(tree
->avl_numnodes
> 0);
755 --tree
->avl_numnodes
;
756 parent
= AVL_XPARENT(delete);
757 which_child
= AVL_XCHILD(delete);
758 if (delete->avl_child
[0] != NULL
)
759 node
= delete->avl_child
[0];
761 node
= delete->avl_child
[1];
764 * Connect parent directly to node (leaving out delete).
767 AVL_SETPARENT(node
, parent
);
768 AVL_SETCHILD(node
, which_child
);
770 if (parent
== NULL
) {
771 tree
->avl_root
= node
;
774 parent
->avl_child
[which_child
] = node
;
778 * Since the subtree is now shorter, begin adjusting parent balances
779 * and performing any needed rotations.
784 * Move up the tree and adjust the balance
786 * Capture the parent and which_child values for the next
787 * iteration before any rotations occur.
790 old_balance
= AVL_XBALANCE(node
);
791 new_balance
= old_balance
- avl_child2balance
[which_child
];
792 parent
= AVL_XPARENT(node
);
793 which_child
= AVL_XCHILD(node
);
796 * If a node was in perfect balance but isn't anymore then
797 * we can stop, since the height didn't change above this point
800 if (old_balance
== 0) {
801 AVL_SETBALANCE(node
, new_balance
);
806 * If the new balance is zero, we don't need to rotate
808 * need a rotation to fix the balance.
809 * If the rotation doesn't change the height
810 * of the sub-tree we have finished adjusting.
812 if (new_balance
== 0)
813 AVL_SETBALANCE(node
, new_balance
);
814 else if (!avl_rotation(tree
, node
, new_balance
))
816 } while (parent
!= NULL
);
819 #define AVL_REINSERT(tree, obj) \
820 avl_remove((tree), (obj)); \
821 avl_add((tree), (obj))
824 avl_update_lt(avl_tree_t
*t
, void *obj
)
828 ASSERT(((neighbor
= AVL_NEXT(t
, obj
)) == NULL
) ||
829 (t
->avl_compar(obj
, neighbor
) <= 0));
831 neighbor
= AVL_PREV(t
, obj
);
832 if ((neighbor
!= NULL
) && (t
->avl_compar(obj
, neighbor
) < 0)) {
833 AVL_REINSERT(t
, obj
);
841 avl_update_gt(avl_tree_t
*t
, void *obj
)
845 ASSERT(((neighbor
= AVL_PREV(t
, obj
)) == NULL
) ||
846 (t
->avl_compar(obj
, neighbor
) >= 0));
848 neighbor
= AVL_NEXT(t
, obj
);
849 if ((neighbor
!= NULL
) && (t
->avl_compar(obj
, neighbor
) > 0)) {
850 AVL_REINSERT(t
, obj
);
858 avl_update(avl_tree_t
*t
, void *obj
)
862 neighbor
= AVL_PREV(t
, obj
);
863 if ((neighbor
!= NULL
) && (t
->avl_compar(obj
, neighbor
) < 0)) {
864 AVL_REINSERT(t
, obj
);
868 neighbor
= AVL_NEXT(t
, obj
);
869 if ((neighbor
!= NULL
) && (t
->avl_compar(obj
, neighbor
) > 0)) {
870 AVL_REINSERT(t
, obj
);
878 avl_swap(avl_tree_t
*tree1
, avl_tree_t
*tree2
)
880 avl_node_t
*temp_node
;
881 ulong_t temp_numnodes
;
883 ASSERT3P(tree1
->avl_compar
, ==, tree2
->avl_compar
);
884 ASSERT3U(tree1
->avl_offset
, ==, tree2
->avl_offset
);
885 ASSERT3U(tree1
->avl_size
, ==, tree2
->avl_size
);
887 temp_node
= tree1
->avl_root
;
888 temp_numnodes
= tree1
->avl_numnodes
;
889 tree1
->avl_root
= tree2
->avl_root
;
890 tree1
->avl_numnodes
= tree2
->avl_numnodes
;
891 tree2
->avl_root
= temp_node
;
892 tree2
->avl_numnodes
= temp_numnodes
;
896 * initialize a new AVL tree
899 avl_create(avl_tree_t
*tree
, int (*compar
) (const void *, const void *),
900 size_t size
, size_t offset
)
905 ASSERT(size
>= offset
+ sizeof (avl_node_t
));
907 ASSERT((offset
& 0x7) == 0);
910 tree
->avl_compar
= compar
;
911 tree
->avl_root
= NULL
;
912 tree
->avl_numnodes
= 0;
913 tree
->avl_size
= size
;
914 tree
->avl_offset
= offset
;
922 avl_destroy(avl_tree_t
*tree
)
925 ASSERT(tree
->avl_numnodes
== 0);
926 ASSERT(tree
->avl_root
== NULL
);
931 * Return the number of nodes in an AVL tree.
934 avl_numnodes(avl_tree_t
*tree
)
937 return (tree
->avl_numnodes
);
941 avl_is_empty(avl_tree_t
*tree
)
944 return (tree
->avl_numnodes
== 0);
947 #define CHILDBIT (1L)
950 * Post-order tree walk used to visit all tree nodes and destroy the tree
951 * in post order. This is used for destroying a tree without paying any cost
952 * for rebalancing it.
956 * void *cookie = NULL;
959 * while ((node = avl_destroy_nodes(tree, &cookie)) != NULL)
963 * The cookie is really an avl_node_t to the current node's parent and
964 * an indication of which child you looked at last.
966 * On input, a cookie value of CHILDBIT indicates the tree is done.
969 avl_destroy_nodes(avl_tree_t
*tree
, void **cookie
)
975 size_t off
= tree
->avl_offset
;
978 * Initial calls go to the first node or it's right descendant.
980 if (*cookie
== NULL
) {
981 first
= avl_first(tree
);
984 * deal with an empty tree
987 *cookie
= (void *)CHILDBIT
;
991 node
= AVL_DATA2NODE(first
, off
);
992 parent
= AVL_XPARENT(node
);
993 goto check_right_side
;
997 * If there is no parent to return to we are done.
999 parent
= (avl_node_t
*)((uintptr_t)(*cookie
) & ~CHILDBIT
);
1000 if (parent
== NULL
) {
1001 if (tree
->avl_root
!= NULL
) {
1002 ASSERT(tree
->avl_numnodes
== 1);
1003 tree
->avl_root
= NULL
;
1004 tree
->avl_numnodes
= 0;
1010 * Remove the child pointer we just visited from the parent and tree.
1012 child
= (uintptr_t)(*cookie
) & CHILDBIT
;
1013 parent
->avl_child
[child
] = NULL
;
1014 ASSERT(tree
->avl_numnodes
> 1);
1015 --tree
->avl_numnodes
;
1018 * If we just did a right child or there isn't one, go up to parent.
1020 if (child
== 1 || parent
->avl_child
[1] == NULL
) {
1022 parent
= AVL_XPARENT(parent
);
1027 * Do parent's right child, then leftmost descendent.
1029 node
= parent
->avl_child
[1];
1030 while (node
->avl_child
[0] != NULL
) {
1032 node
= node
->avl_child
[0];
1036 * If here, we moved to a left child. It may have one
1037 * child on the right (when balance == +1).
1040 if (node
->avl_child
[1] != NULL
) {
1041 ASSERT(AVL_XBALANCE(node
) == 1);
1043 node
= node
->avl_child
[1];
1044 ASSERT(node
->avl_child
[0] == NULL
&&
1045 node
->avl_child
[1] == NULL
);
1047 ASSERT(AVL_XBALANCE(node
) <= 0);
1051 if (parent
== NULL
) {
1052 *cookie
= (void *)CHILDBIT
;
1053 ASSERT(node
== tree
->avl_root
);
1055 *cookie
= (void *)((uintptr_t)parent
| AVL_XCHILD(node
));
1058 return (AVL_NODE2DATA(node
, off
));
1061 #if defined(_KERNEL) && defined(HAVE_SPL)
1073 module_init(avl_init
);
1074 module_exit(avl_fini
);
1076 MODULE_DESCRIPTION("Generic AVL tree implementation");
1077 MODULE_AUTHOR(ZFS_META_AUTHOR
);
1078 MODULE_LICENSE(ZFS_META_LICENSE
);
1079 MODULE_VERSION(ZFS_META_VERSION
"-" ZFS_META_RELEASE
);
1081 EXPORT_SYMBOL(avl_create
);
1082 EXPORT_SYMBOL(avl_find
);
1083 EXPORT_SYMBOL(avl_insert
);
1084 EXPORT_SYMBOL(avl_insert_here
);
1085 EXPORT_SYMBOL(avl_walk
);
1086 EXPORT_SYMBOL(avl_first
);
1087 EXPORT_SYMBOL(avl_last
);
1088 EXPORT_SYMBOL(avl_nearest
);
1089 EXPORT_SYMBOL(avl_add
);
1090 EXPORT_SYMBOL(avl_swap
);
1091 EXPORT_SYMBOL(avl_is_empty
);
1092 EXPORT_SYMBOL(avl_remove
);
1093 EXPORT_SYMBOL(avl_numnodes
);
1094 EXPORT_SYMBOL(avl_destroy_nodes
);
1095 EXPORT_SYMBOL(avl_destroy
);