Path: blob/main/sys/contrib/openzfs/module/avl/avl.c
48383 views
// SPDX-License-Identifier: CDDL-1.01/*2* CDDL HEADER START3*4* The contents of this file are subject to the terms of the5* Common Development and Distribution License (the "License").6* You may not use this file except in compliance with the License.7*8* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE9* or https://opensource.org/licenses/CDDL-1.0.10* See the License for the specific language governing permissions11* and limitations under the License.12*13* When distributing Covered Code, include this CDDL HEADER in each14* file and include the License file at usr/src/OPENSOLARIS.LICENSE.15* If applicable, add the following below this CDDL HEADER, with the16* fields enclosed by brackets "[]" replaced with your own identifying17* information: Portions Copyright [yyyy] [name of copyright owner]18*19* CDDL HEADER END20*/21/*22* Copyright 2009 Sun Microsystems, Inc. All rights reserved.23* Use is subject to license terms.24*/2526/*27* Copyright 2015 Nexenta Systems, Inc. All rights reserved.28* Copyright (c) 2015 by Delphix. All rights reserved.29*/3031/*32* AVL - generic AVL tree implementation for kernel use33*34* A complete description of AVL trees can be found in many CS textbooks.35*36* Here is a very brief overview. An AVL tree is a binary search tree that is37* almost perfectly balanced. By "almost" perfectly balanced, we mean that at38* any given node, the left and right subtrees are allowed to differ in height39* by at most 1 level.40*41* This relaxation from a perfectly balanced binary tree allows doing42* insertion and deletion relatively efficiently. Searching the tree is43* still a fast operation, roughly O(log(N)).44*45* The key to insertion and deletion is a set of tree manipulations called46* rotations, which bring unbalanced subtrees back into the semi-balanced state.47*48* This implementation of AVL trees has the following peculiarities:49*50* - The AVL specific data structures are physically embedded as fields51* in the "using" data structures. To maintain generality the code52* must constantly translate between "avl_node_t *" and containing53* data structure "void *"s by adding/subtracting the avl_offset.54*55* - Since the AVL data is always embedded in other structures, there is56* no locking or memory allocation in the AVL routines. This must be57* provided for by the enclosing data structure's semantics. Typically,58* avl_insert()/_add()/_remove()/avl_insert_here() require some kind of59* exclusive write lock. Other operations require a read lock.60*61* - The implementation uses iteration instead of explicit recursion,62* since it is intended to run on limited size kernel stacks. Since63* there is no recursion stack present to move "up" in the tree,64* there is an explicit "parent" link in the avl_node_t.65*66* - The left/right children pointers of a node are in an array.67* In the code, variables (instead of constants) are used to represent68* left and right indices. The implementation is written as if it only69* dealt with left handed manipulations. By changing the value assigned70* to "left", the code also works for right handed trees. The71* following variables/terms are frequently used:72*73* int left; // 0 when dealing with left children,74* // 1 for dealing with right children75*76* int left_heavy; // -1 when left subtree is taller at some node,77* // +1 when right subtree is taller78*79* int right; // will be the opposite of left (0 or 1)80* int right_heavy;// will be the opposite of left_heavy (-1 or 1)81*82* int direction; // 0 for "<" (ie. left child); 1 for ">" (right)83*84* Though it is a little more confusing to read the code, the approach85* allows using half as much code (and hence cache footprint) for tree86* manipulations and eliminates many conditional branches.87*88* - The avl_index_t is an opaque "cookie" used to find nodes at or89* adjacent to where a new value would be inserted in the tree. The value90* is a modified "avl_node_t *". The bottom bit (normally 0 for a91* pointer) is set to indicate if that the new node has a value greater92* than the value of the indicated "avl_node_t *".93*94* Note - in addition to userland (e.g. libavl and libutil) and the kernel95* (e.g. genunix), avl.c is compiled into ld.so and kmdb's genunix module,96* which each have their own compilation environments and subsequent97* requirements. Each of these environments must be considered when adding98* dependencies from avl.c.99*100* Link to Illumos.org for more information on avl function:101* [1] https://illumos.org/man/9f/avl102*/103104#include <sys/types.h>105#include <sys/param.h>106#include <sys/debug.h>107#include <sys/avl.h>108#include <sys/cmn_err.h>109#include <sys/mod.h>110111#ifndef _KERNEL112#include <string.h>113#endif114115/*116* Walk from one node to the previous valued node (ie. an infix walk117* towards the left). At any given node we do one of 2 things:118*119* - If there is a left child, go to it, then to it's rightmost descendant.120*121* - otherwise we return through parent nodes until we've come from a right122* child.123*124* Return Value:125* NULL - if at the end of the nodes126* otherwise next node127*/128void *129avl_walk(avl_tree_t *tree, void *oldnode, int left)130{131size_t off = tree->avl_offset;132avl_node_t *node = AVL_DATA2NODE(oldnode, off);133int right = 1 - left;134int was_child;135136137/*138* nowhere to walk to if tree is empty139*/140if (node == NULL)141return (NULL);142143/*144* Visit the previous valued node. There are two possibilities:145*146* If this node has a left child, go down one left, then all147* the way right.148*/149if (node->avl_child[left] != NULL) {150for (node = node->avl_child[left];151node->avl_child[right] != NULL;152node = node->avl_child[right])153;154/*155* Otherwise, return through left children as far as we can.156*/157} else {158for (;;) {159was_child = AVL_XCHILD(node);160node = AVL_XPARENT(node);161if (node == NULL)162return (NULL);163if (was_child == right)164break;165}166}167168return (AVL_NODE2DATA(node, off));169}170171/*172* Return the lowest valued node in a tree or NULL.173* (leftmost child from root of tree)174*/175void *176avl_first(avl_tree_t *tree)177{178avl_node_t *node;179avl_node_t *prev = NULL;180size_t off = tree->avl_offset;181182for (node = tree->avl_root; node != NULL; node = node->avl_child[0])183prev = node;184185if (prev != NULL)186return (AVL_NODE2DATA(prev, off));187return (NULL);188}189190/*191* Return the highest valued node in a tree or NULL.192* (rightmost child from root of tree)193*/194void *195avl_last(avl_tree_t *tree)196{197avl_node_t *node;198avl_node_t *prev = NULL;199size_t off = tree->avl_offset;200201for (node = tree->avl_root; node != NULL; node = node->avl_child[1])202prev = node;203204if (prev != NULL)205return (AVL_NODE2DATA(prev, off));206return (NULL);207}208209/*210* Access the node immediately before or after an insertion point.211*212* "avl_index_t" is a (avl_node_t *) with the bottom bit indicating a child213*214* Return value:215* NULL: no node in the given direction216* "void *" of the found tree node217*/218void *219avl_nearest(avl_tree_t *tree, avl_index_t where, int direction)220{221int child = AVL_INDEX2CHILD(where);222avl_node_t *node = AVL_INDEX2NODE(where);223void *data;224size_t off = tree->avl_offset;225226if (node == NULL) {227ASSERT0P(tree->avl_root);228return (NULL);229}230data = AVL_NODE2DATA(node, off);231if (child != direction)232return (data);233234return (avl_walk(tree, data, direction));235}236237238/*239* Search for the node which contains "value". The algorithm is a240* simple binary tree search.241*242* return value:243* NULL: the value is not in the AVL tree244* *where (if not NULL) is set to indicate the insertion point245* "void *" of the found tree node246*/247void *248avl_find(avl_tree_t *tree, const void *value, avl_index_t *where)249{250avl_node_t *node;251avl_node_t *prev = NULL;252int child = 0;253int diff;254size_t off = tree->avl_offset;255256for (node = tree->avl_root; node != NULL;257node = node->avl_child[child]) {258259prev = node;260261diff = tree->avl_compar(value, AVL_NODE2DATA(node, off));262ASSERT(-1 <= diff && diff <= 1);263if (diff == 0) {264#ifdef ZFS_DEBUG265if (where != NULL)266*where = 0;267#endif268return (AVL_NODE2DATA(node, off));269}270child = (diff > 0);271}272273if (where != NULL)274*where = AVL_MKINDEX(prev, child);275276return (NULL);277}278279280/*281* Perform a rotation to restore balance at the subtree given by depth.282*283* This routine is used by both insertion and deletion. The return value284* indicates:285* 0 : subtree did not change height286* !0 : subtree was reduced in height287*288* The code is written as if handling left rotations, right rotations are289* symmetric and handled by swapping values of variables right/left[_heavy]290*291* On input balance is the "new" balance at "node". This value is either292* -2 or +2.293*/294static int295avl_rotation(avl_tree_t *tree, avl_node_t *node, int balance)296{297int left = !(balance < 0); /* when balance = -2, left will be 0 */298int right = 1 - left;299int left_heavy = balance >> 1;300int right_heavy = -left_heavy;301avl_node_t *parent = AVL_XPARENT(node);302avl_node_t *child = node->avl_child[left];303avl_node_t *cright;304avl_node_t *gchild;305avl_node_t *gright;306avl_node_t *gleft;307int which_child = AVL_XCHILD(node);308int child_bal = AVL_XBALANCE(child);309310/*311* case 1 : node is overly left heavy, the left child is balanced or312* also left heavy. This requires the following rotation.313*314* (node bal:-2)315* / \316* / \317* (child bal:0 or -1)318* / \319* / \320* cright321*322* becomes:323*324* (child bal:1 or 0)325* / \326* / \327* (node bal:-1 or 0)328* / \329* / \330* cright331*332* we detect this situation by noting that child's balance is not333* right_heavy.334*/335if (child_bal != right_heavy) {336337/*338* compute new balance of nodes339*340* If child used to be left heavy (now balanced) we reduced341* the height of this sub-tree -- used in "return...;" below342*/343child_bal += right_heavy; /* adjust towards right */344345/*346* move "cright" to be node's left child347*/348cright = child->avl_child[right];349node->avl_child[left] = cright;350if (cright != NULL) {351AVL_SETPARENT(cright, node);352AVL_SETCHILD(cright, left);353}354355/*356* move node to be child's right child357*/358child->avl_child[right] = node;359AVL_SETBALANCE(node, -child_bal);360AVL_SETCHILD(node, right);361AVL_SETPARENT(node, child);362363/*364* update the pointer into this subtree365*/366AVL_SETBALANCE(child, child_bal);367AVL_SETCHILD(child, which_child);368AVL_SETPARENT(child, parent);369if (parent != NULL)370parent->avl_child[which_child] = child;371else372tree->avl_root = child;373374return (child_bal == 0);375}376377/*378* case 2 : When node is left heavy, but child is right heavy we use379* a different rotation.380*381* (node b:-2)382* / \383* / \384* / \385* (child b:+1)386* / \387* / \388* (gchild b: != 0)389* / \390* / \391* gleft gright392*393* becomes:394*395* (gchild b:0)396* / \397* / \398* / \399* (child b:?) (node b:?)400* / \ / \401* / \ / \402* gleft gright403*404* computing the new balances is more complicated. As an example:405* if gchild was right_heavy, then child is now left heavy406* else it is balanced407*/408gchild = child->avl_child[right];409gleft = gchild->avl_child[left];410gright = gchild->avl_child[right];411412/*413* move gright to left child of node and414*415* move gleft to right child of node416*/417node->avl_child[left] = gright;418if (gright != NULL) {419AVL_SETPARENT(gright, node);420AVL_SETCHILD(gright, left);421}422423child->avl_child[right] = gleft;424if (gleft != NULL) {425AVL_SETPARENT(gleft, child);426AVL_SETCHILD(gleft, right);427}428429/*430* move child to left child of gchild and431*432* move node to right child of gchild and433*434* fixup parent of all this to point to gchild435*/436balance = AVL_XBALANCE(gchild);437gchild->avl_child[left] = child;438AVL_SETBALANCE(child, (balance == right_heavy ? left_heavy : 0));439AVL_SETPARENT(child, gchild);440AVL_SETCHILD(child, left);441442gchild->avl_child[right] = node;443AVL_SETBALANCE(node, (balance == left_heavy ? right_heavy : 0));444AVL_SETPARENT(node, gchild);445AVL_SETCHILD(node, right);446447AVL_SETBALANCE(gchild, 0);448AVL_SETPARENT(gchild, parent);449AVL_SETCHILD(gchild, which_child);450if (parent != NULL)451parent->avl_child[which_child] = gchild;452else453tree->avl_root = gchild;454455return (1); /* the new tree is always shorter */456}457458459/*460* Insert a new node into an AVL tree at the specified (from avl_find()) place.461*462* Newly inserted nodes are always leaf nodes in the tree, since avl_find()463* searches out to the leaf positions. The avl_index_t indicates the node464* which will be the parent of the new node.465*466* After the node is inserted, a single rotation further up the tree may467* be necessary to maintain an acceptable AVL balance.468*/469void470avl_insert(avl_tree_t *tree, void *new_data, avl_index_t where)471{472avl_node_t *node;473avl_node_t *parent = AVL_INDEX2NODE(where);474int old_balance;475int new_balance;476int which_child = AVL_INDEX2CHILD(where);477size_t off = tree->avl_offset;478479#ifdef _LP64480ASSERT0(((uintptr_t)new_data & 0x7));481#endif482483node = AVL_DATA2NODE(new_data, off);484485/*486* First, add the node to the tree at the indicated position.487*/488++tree->avl_numnodes;489490node->avl_child[0] = NULL;491node->avl_child[1] = NULL;492493AVL_SETCHILD(node, which_child);494AVL_SETBALANCE(node, 0);495AVL_SETPARENT(node, parent);496if (parent != NULL) {497ASSERT0P(parent->avl_child[which_child]);498parent->avl_child[which_child] = node;499} else {500ASSERT0P(tree->avl_root);501tree->avl_root = node;502}503/*504* Now, back up the tree modifying the balance of all nodes above the505* insertion point. If we get to a highly unbalanced ancestor, we506* need to do a rotation. If we back out of the tree we are done.507* If we brought any subtree into perfect balance (0), we are also done.508*/509for (;;) {510node = parent;511if (node == NULL)512return;513514/*515* Compute the new balance516*/517old_balance = AVL_XBALANCE(node);518new_balance = old_balance + (which_child ? 1 : -1);519520/*521* If we introduced equal balance, then we are done immediately522*/523if (new_balance == 0) {524AVL_SETBALANCE(node, 0);525return;526}527528/*529* If both old and new are not zero we went530* from -1 to -2 balance, do a rotation.531*/532if (old_balance != 0)533break;534535AVL_SETBALANCE(node, new_balance);536parent = AVL_XPARENT(node);537which_child = AVL_XCHILD(node);538}539540/*541* perform a rotation to fix the tree and return542*/543(void) avl_rotation(tree, node, new_balance);544}545546/*547* Insert "new_data" in "tree" in the given "direction" either after or548* before (AVL_AFTER, AVL_BEFORE) the data "here".549*550* Insertions can only be done at empty leaf points in the tree, therefore551* if the given child of the node is already present we move to either552* the AVL_PREV or AVL_NEXT and reverse the insertion direction. Since553* every other node in the tree is a leaf, this always works.554*555* To help developers using this interface, we assert that the new node556* is correctly ordered at every step of the way in DEBUG kernels.557*/558void559avl_insert_here(560avl_tree_t *tree,561void *new_data,562void *here,563int direction)564{565avl_node_t *node;566int child = direction; /* rely on AVL_BEFORE == 0, AVL_AFTER == 1 */567#ifdef ZFS_DEBUG568int diff;569#endif570571ASSERT(tree != NULL);572ASSERT(new_data != NULL);573ASSERT(here != NULL);574ASSERT(direction == AVL_BEFORE || direction == AVL_AFTER);575576/*577* If corresponding child of node is not NULL, go to the neighboring578* node and reverse the insertion direction.579*/580node = AVL_DATA2NODE(here, tree->avl_offset);581582#ifdef ZFS_DEBUG583diff = tree->avl_compar(new_data, here);584ASSERT(-1 <= diff && diff <= 1);585ASSERT(diff != 0);586ASSERT(diff > 0 ? child == 1 : child == 0);587#endif588589if (node->avl_child[child] != NULL) {590node = node->avl_child[child];591child = 1 - child;592while (node->avl_child[child] != NULL) {593#ifdef ZFS_DEBUG594diff = tree->avl_compar(new_data,595AVL_NODE2DATA(node, tree->avl_offset));596ASSERT(-1 <= diff && diff <= 1);597ASSERT(diff != 0);598ASSERT(diff > 0 ? child == 1 : child == 0);599#endif600node = node->avl_child[child];601}602#ifdef ZFS_DEBUG603diff = tree->avl_compar(new_data,604AVL_NODE2DATA(node, tree->avl_offset));605ASSERT(-1 <= diff && diff <= 1);606ASSERT(diff != 0);607ASSERT(diff > 0 ? child == 1 : child == 0);608#endif609}610ASSERT0P(node->avl_child[child]);611612avl_insert(tree, new_data, AVL_MKINDEX(node, child));613}614615/*616* Add a new node to an AVL tree. Strictly enforce that no duplicates can617* be added to the tree with a VERIFY which is enabled for non-DEBUG builds.618*/619void620avl_add(avl_tree_t *tree, void *new_node)621{622avl_index_t where = 0;623624VERIFY(avl_find(tree, new_node, &where) == NULL);625626avl_insert(tree, new_node, where);627}628629/*630* Delete a node from the AVL tree. Deletion is similar to insertion, but631* with 2 complications.632*633* First, we may be deleting an interior node. Consider the following subtree:634*635* d c c636* / \ / \ / \637* b e b e b e638* / \ / \ /639* a c a a640*641* When we are deleting node (d), we find and bring up an adjacent valued leaf642* node, say (c), to take the interior node's place. In the code this is643* handled by temporarily swapping (d) and (c) in the tree and then using644* common code to delete (d) from the leaf position.645*646* Secondly, an interior deletion from a deep tree may require more than one647* rotation to fix the balance. This is handled by moving up the tree through648* parents and applying rotations as needed. The return value from649* avl_rotation() is used to detect when a subtree did not change overall650* height due to a rotation.651*/652void653avl_remove(avl_tree_t *tree, void *data)654{655avl_node_t *delete;656avl_node_t *parent;657avl_node_t *node;658avl_node_t tmp;659int old_balance;660int new_balance;661int left;662int right;663int which_child;664size_t off = tree->avl_offset;665666delete = AVL_DATA2NODE(data, off);667668/*669* Deletion is easiest with a node that has at most 1 child.670* We swap a node with 2 children with a sequentially valued671* neighbor node. That node will have at most 1 child. Note this672* has no effect on the ordering of the remaining nodes.673*674* As an optimization, we choose the greater neighbor if the tree675* is right heavy, otherwise the left neighbor. This reduces the676* number of rotations needed.677*/678if (delete->avl_child[0] != NULL && delete->avl_child[1] != NULL) {679680/*681* choose node to swap from whichever side is taller682*/683old_balance = AVL_XBALANCE(delete);684left = (old_balance > 0);685right = 1 - left;686687/*688* get to the previous value'd node689* (down 1 left, as far as possible right)690*/691for (node = delete->avl_child[left];692node->avl_child[right] != NULL;693node = node->avl_child[right])694;695696/*697* create a temp placeholder for 'node'698* move 'node' to delete's spot in the tree699*/700tmp = *node;701702memcpy(node, delete, sizeof (*node));703if (node->avl_child[left] == node)704node->avl_child[left] = &tmp;705706parent = AVL_XPARENT(node);707if (parent != NULL)708parent->avl_child[AVL_XCHILD(node)] = node;709else710tree->avl_root = node;711AVL_SETPARENT(node->avl_child[left], node);712AVL_SETPARENT(node->avl_child[right], node);713714/*715* Put tmp where node used to be (just temporary).716* It always has a parent and at most 1 child.717*/718delete = &tmp;719parent = AVL_XPARENT(delete);720parent->avl_child[AVL_XCHILD(delete)] = delete;721which_child = (delete->avl_child[1] != 0);722if (delete->avl_child[which_child] != NULL)723AVL_SETPARENT(delete->avl_child[which_child], delete);724}725726727/*728* Here we know "delete" is at least partially a leaf node. It can729* be easily removed from the tree.730*/731ASSERT(tree->avl_numnodes > 0);732--tree->avl_numnodes;733parent = AVL_XPARENT(delete);734which_child = AVL_XCHILD(delete);735if (delete->avl_child[0] != NULL)736node = delete->avl_child[0];737else738node = delete->avl_child[1];739740/*741* Connect parent directly to node (leaving out delete).742*/743if (node != NULL) {744AVL_SETPARENT(node, parent);745AVL_SETCHILD(node, which_child);746}747if (parent == NULL) {748tree->avl_root = node;749return;750}751parent->avl_child[which_child] = node;752753754/*755* Since the subtree is now shorter, begin adjusting parent balances756* and performing any needed rotations.757*/758do {759760/*761* Move up the tree and adjust the balance762*763* Capture the parent and which_child values for the next764* iteration before any rotations occur.765*/766node = parent;767old_balance = AVL_XBALANCE(node);768new_balance = old_balance - (which_child ? 1 : -1);769parent = AVL_XPARENT(node);770which_child = AVL_XCHILD(node);771772/*773* If a node was in perfect balance but isn't anymore then774* we can stop, since the height didn't change above this point775* due to a deletion.776*/777if (old_balance == 0) {778AVL_SETBALANCE(node, new_balance);779break;780}781782/*783* If the new balance is zero, we don't need to rotate784* else785* need a rotation to fix the balance.786* If the rotation doesn't change the height787* of the sub-tree we have finished adjusting.788*/789if (new_balance == 0)790AVL_SETBALANCE(node, new_balance);791else if (!avl_rotation(tree, node, new_balance))792break;793} while (parent != NULL);794}795796#define AVL_REINSERT(tree, obj) \797avl_remove((tree), (obj)); \798avl_add((tree), (obj))799800boolean_t801avl_update_lt(avl_tree_t *t, void *obj)802{803void *neighbor;804805ASSERT(((neighbor = AVL_NEXT(t, obj)) == NULL) ||806(t->avl_compar(obj, neighbor) <= 0));807808neighbor = AVL_PREV(t, obj);809if ((neighbor != NULL) && (t->avl_compar(obj, neighbor) < 0)) {810AVL_REINSERT(t, obj);811return (B_TRUE);812}813814return (B_FALSE);815}816817boolean_t818avl_update_gt(avl_tree_t *t, void *obj)819{820void *neighbor;821822ASSERT(((neighbor = AVL_PREV(t, obj)) == NULL) ||823(t->avl_compar(obj, neighbor) >= 0));824825neighbor = AVL_NEXT(t, obj);826if ((neighbor != NULL) && (t->avl_compar(obj, neighbor) > 0)) {827AVL_REINSERT(t, obj);828return (B_TRUE);829}830831return (B_FALSE);832}833834boolean_t835avl_update(avl_tree_t *t, void *obj)836{837void *neighbor;838839neighbor = AVL_PREV(t, obj);840if ((neighbor != NULL) && (t->avl_compar(obj, neighbor) < 0)) {841AVL_REINSERT(t, obj);842return (B_TRUE);843}844845neighbor = AVL_NEXT(t, obj);846if ((neighbor != NULL) && (t->avl_compar(obj, neighbor) > 0)) {847AVL_REINSERT(t, obj);848return (B_TRUE);849}850851return (B_FALSE);852}853854void855avl_swap(avl_tree_t *tree1, avl_tree_t *tree2)856{857avl_node_t *temp_node;858ulong_t temp_numnodes;859860ASSERT3P(tree1->avl_compar, ==, tree2->avl_compar);861ASSERT3U(tree1->avl_offset, ==, tree2->avl_offset);862863temp_node = tree1->avl_root;864temp_numnodes = tree1->avl_numnodes;865tree1->avl_root = tree2->avl_root;866tree1->avl_numnodes = tree2->avl_numnodes;867tree2->avl_root = temp_node;868tree2->avl_numnodes = temp_numnodes;869}870871/*872* initialize a new AVL tree873*/874void875avl_create(avl_tree_t *tree, int (*compar) (const void *, const void *),876size_t size, size_t offset)877{878ASSERT(tree);879ASSERT(compar);880ASSERT(size > 0);881ASSERT(size >= offset + sizeof (avl_node_t));882#ifdef _LP64883ASSERT0((offset & 0x7));884#endif885886tree->avl_compar = compar;887tree->avl_root = NULL;888tree->avl_numnodes = 0;889tree->avl_offset = offset;890}891892/*893* Delete a tree.894*/895void896avl_destroy(avl_tree_t *tree)897{898ASSERT(tree);899ASSERT0(tree->avl_numnodes);900ASSERT0P(tree->avl_root);901}902903904/*905* Return the number of nodes in an AVL tree.906*/907ulong_t908avl_numnodes(avl_tree_t *tree)909{910ASSERT(tree);911return (tree->avl_numnodes);912}913914boolean_t915avl_is_empty(avl_tree_t *tree)916{917ASSERT(tree);918return (tree->avl_numnodes == 0);919}920921#define CHILDBIT (1L)922923/*924* Post-order tree walk used to visit all tree nodes and destroy the tree925* in post order. This is used for removing all the nodes from a tree without926* paying any cost for rebalancing it.927*928* example:929*930* void *cookie = NULL;931* my_data_t *node;932*933* while ((node = avl_destroy_nodes(tree, &cookie)) != NULL)934* free(node);935* avl_destroy(tree);936*937* The cookie is really an avl_node_t to the current node's parent and938* an indication of which child you looked at last.939*940* On input, a cookie value of CHILDBIT indicates the tree is done.941*/942void *943avl_destroy_nodes(avl_tree_t *tree, void **cookie)944{945avl_node_t *node;946avl_node_t *parent;947int child;948void *first;949size_t off = tree->avl_offset;950951/*952* Initial calls go to the first node or it's right descendant.953*/954if (*cookie == NULL) {955first = avl_first(tree);956957/*958* deal with an empty tree959*/960if (first == NULL) {961*cookie = (void *)CHILDBIT;962return (NULL);963}964965node = AVL_DATA2NODE(first, off);966parent = AVL_XPARENT(node);967goto check_right_side;968}969970/*971* If there is no parent to return to we are done.972*/973parent = (avl_node_t *)((uintptr_t)(*cookie) & ~CHILDBIT);974if (parent == NULL) {975if (tree->avl_root != NULL) {976ASSERT(tree->avl_numnodes == 1);977tree->avl_root = NULL;978tree->avl_numnodes = 0;979}980return (NULL);981}982983/*984* Remove the child pointer we just visited from the parent and tree.985*/986child = (uintptr_t)(*cookie) & CHILDBIT;987parent->avl_child[child] = NULL;988ASSERT(tree->avl_numnodes > 1);989--tree->avl_numnodes;990991/*992* If we just removed a right child or there isn't one, go up to parent.993*/994if (child == 1 || parent->avl_child[1] == NULL) {995node = parent;996parent = AVL_XPARENT(parent);997goto done;998}9991000/*1001* Do parent's right child, then leftmost descendent.1002*/1003node = parent->avl_child[1];1004while (node->avl_child[0] != NULL) {1005parent = node;1006node = node->avl_child[0];1007}10081009/*1010* If here, we moved to a left child. It may have one1011* child on the right (when balance == +1).1012*/1013check_right_side:1014if (node->avl_child[1] != NULL) {1015ASSERT(AVL_XBALANCE(node) == 1);1016parent = node;1017node = node->avl_child[1];1018ASSERT(node->avl_child[0] == NULL &&1019node->avl_child[1] == NULL);1020} else {1021ASSERT(AVL_XBALANCE(node) <= 0);1022}10231024done:1025if (parent == NULL) {1026*cookie = (void *)CHILDBIT;1027ASSERT(node == tree->avl_root);1028} else {1029*cookie = (void *)((uintptr_t)parent | AVL_XCHILD(node));1030}10311032return (AVL_NODE2DATA(node, off));1033}10341035EXPORT_SYMBOL(avl_create);1036EXPORT_SYMBOL(avl_find);1037EXPORT_SYMBOL(avl_insert);1038EXPORT_SYMBOL(avl_insert_here);1039EXPORT_SYMBOL(avl_walk);1040EXPORT_SYMBOL(avl_first);1041EXPORT_SYMBOL(avl_last);1042EXPORT_SYMBOL(avl_nearest);1043EXPORT_SYMBOL(avl_add);1044EXPORT_SYMBOL(avl_swap);1045EXPORT_SYMBOL(avl_is_empty);1046EXPORT_SYMBOL(avl_remove);1047EXPORT_SYMBOL(avl_numnodes);1048EXPORT_SYMBOL(avl_destroy_nodes);1049EXPORT_SYMBOL(avl_destroy);1050EXPORT_SYMBOL(avl_update_lt);1051EXPORT_SYMBOL(avl_update_gt);1052EXPORT_SYMBOL(avl_update);105310541055