e0ea83ebb1
Summary of changes: - libdtrace changeset: 12902:3bb859a7330c user: Bryan Cantrill <Bryan.Cantrill@Sun.COM> date: Fri Jul 23 17:34:02 2010 -0700 summary: 6679140 asymmetric alloc/dealloc activity can induce dynamic variable drops changeset: 12692:4341b447c069 user: Ali Bahrami <Ali.Bahrami@Oracle.COM> date: Thu Jun 24 18:16:42 2010 -0600 summary: 6916796 OSnet mapfiles should use version 2 link-editor syntax changeset: 12507:501806a754d2 user: Alan Maguire <Alan.Maguire@Sun.COM> date: Thu May 27 17:29:51 2010 -0400 summary: PSARC 2010/106 DTrace TCP and UDP providers changeset: 11798:1e7f1f154004 user: Roger A. Faulkner <Roger.Faulkner@Sun.COM> date: Sun Feb 28 18:42:20 2010 -0800 summary: PSARC 2009/657 delete obsolete system call traps changeset: 11466:d60272412fb0 user: Roger A. Faulkner <Roger.Faulkner@Sun.COM> date: Mon Jan 11 17:42:13 2010 -0800 summary: 6915578 MUTEX_HELD() and RW_LOCK_HELD() macros should be available to Posix threads changeset: 11237:0d23e47ed228 user: Jonathan Haslam <Jonathan.Haslam@Sun.COM> date: Thu Dec 03 13:39:19 2009 +0000 summary: 6795386 macro arguments and globbing in DTrace probe descriptions don't mix changeset: 10791:944abfb5b345 user: Jonathan Haslam <Jonathan.Haslam@Sun.COM> date: Wed Oct 14 11:25:23 2009 +0100 summary: 6886953 large symbols lead to stack exhaustion changeset: 10326:8e3fbeec2d76 user: Siddheshwar Mahesh <Siddheshwar.Mahesh@Sun.COM> date: Mon Aug 17 13:26:49 2009 -0500 summary: 6868411 NFS provider generates error on ci_remote on RDMA operations changeset: 10207:87c40ea3fc4b user: jmcp <James.McPherson@Sun.COM> date: Wed Jul 29 16:56:03 2009 -0700 summary: 6864230 hiho, hiho, it'ch chtime for CH to go changeset: 10044:2643c1cd9e2a user: Priya Krishnan <Priya.Krishnan@Sun.COM> date: Mon Jul 06 21:19:41 2009 -0400 summary: 6855027 iscsit.d breaks dtrace in osol based on snv_117 changeset: 9900:1b86d65a4f9e user: Ali Bahrami <Ali.Bahrami@Sun.COM> date: Thu Jun 18 13:16:39 2009 -0600 summary: 6851224 elf_getshnum() and elf_getshstrndx() incompatible with 2002 ELF gABI agreement changeset: 9885:a3d5e9d9e779 user: Robert Mastors <Robert.Mastors@Sun.COM> date: Tue Jun 16 15:25:25 2009 -0500 summary: 6711844 assert: vp->v_shrlocks == 0L, file: ../../common/fs/vnode.c, line: 2333 changeset: 9881:741c9e4e094c user: Charles Ting <Charles.Ting@Sun.COM> date: Tue Jun 16 14:51:40 2009 -0400 summary: 6849606 SRP DTrace Probe for xfer-done misses completion of READ transfers changeset: 9829:e8059fcaee97 user: Charles Ting <Charles.Ting@Sun.COM> date: Tue Jun 09 10:11:35 2009 -0400 summary: 6804431 Add Dtrace probes to SRPT changeset: 9812:a2990074321f user: Priya Krishnan <Priya.Krishnan@Sun.COM> date: Mon Jun 08 09:49:48 2009 -0400 summary: 6847237 The iscsit.d DTrace translator should include iscsi.d for the definition of iscsiinfo_t changeset: 9721:4f7e194c7c37 user: Priya Krishnan <Priya.Krishnan@Sun.COM> date: Tue May 26 10:40:43 2009 -0400 summary: 6809997 COMSTAR iscsi target DTrace Provider needed changeset: 9625:8aa5731291b4 user: Sam Cramer <Sam.Cramer@Sun.COM> date: Wed May 13 17:10:06 2009 -0700 summary: 6840354 "/usr/lib/dtrace/fc.d", line 59: syntax error near "fct_local_port_t" changeset: 9609:8874cc8d5e3f user: Sam Cramer <Sam.Cramer@Sun.COM> date: Mon May 11 21:02:27 2009 -0700 summary: 6809580 fct DTrace providers needed for qlt changeset: 9578:c4b38ec17f4e user: Sam Cramer <Sam.Cramer@Sun.COM> date: Fri May 08 12:12:40 2009 -0700 summary: 6809580 fct DTrace providers needed for qlt changeset: 9531:dc8924ef7839 user: Rafael Vanoni <rafael.vanoni@sun.com> date: Mon May 04 11:48:15 2009 -0700 summary: 6730130 dtrace missing printf handler for stdev changeset: 9389:750ed3471e90 user: Vamsi Nagineni <Vamsi.Krishna@Sun.COM> date: Fri Apr 17 06:26:47 2009 -0700 summary: 6812050 dtrace should translate curpsinfo->pr_contract changeset: 9085:ff7eb0bace56 user: Ali Bahrami <Ali.Bahrami@Sun.COM> date: Wed Mar 18 13:28:28 2009 -0600 summary: 6813909 generalize eh_frame support to non-amd64 platforms changeset: 8803:8c01b39012c9 user: Jonathan Haslam <Jonathan.Haslam@Sun.COM> date: Fri Feb 13 07:13:13 2009 +0000 summary: PSARC 2008/480 DTrace CPC Provider changeset: 8744:03d5725cda56 user: Ali Bahrami <Ali.Bahrami@Sun.COM> date: Tue Feb 10 09:38:02 2009 -0700 summary: 6798660 Cadmium .NOT file processing problem with CWD relative file paths changeset: 8337:079ecc003ca6 user: Jonathan Haslam <Jonathan.Haslam@Sun.COM> date: Thu Dec 11 11:26:47 2008 +0000 summary: 6750659 drti.o crashes app due to corrupt environment changeset: 7991:d3b751ef3d85 user: Jonathan Haslam <Jonathan.Haslam@Sun.COM> date: Mon Nov 03 10:26:23 2008 +0000 summary: 6738982 Representative thread after DTrace stop() action is incorrect changeset: 7208:568549b138d8 user: vv149972 date: Mon Jul 28 23:14:31 2008 -0700 summary: 6696397 NFS v3 provider reports all UDP clients as 0.0.0.0 changeset: 6878:360e73ea6b0c user: brendan date: Fri Jun 13 19:06:55 2008 -0700 summary: PSARC 2008/302 DTrace IP Provider changeset: 6554:b5817e112852 user: ahl date: Mon May 05 14:38:24 2008 -0700 summary: 6677812 race between dtrace activities in non-local zones ---- - ctf tools: changeset: 12177:800b7f847f1e user: Surya Prakki <Surya.Prakki@Sun.COM> date: Sun Apr 18 23:59:57 2010 -0700 summary: 6941452 ctfconvert fails on VLAs with code generated by SS12u1 changeset: 11432:c1c450bf62f2 user: John Levon <john.levon@sun.com> date: Tue Jan 05 06:57:53 2010 -0800 summary: 6905711 anonymous and empty SOUs crash ctfconvert changeset: 11227:cd2ac59c39f2 user: Ali Bahrami <Ali.Bahrami@Sun.COM> date: Wed Dec 02 15:37:55 2009 -0700 summary: 6900241 ld should track SHT_GROUP sections by symbol name, not section name changeset: 10380:5394a7172e1f user: Ali Bahrami <Ali.Bahrami@Sun.COM> date: Tue Aug 25 13:51:43 2009 -0600 summary: 6866605 SUNWonbld ELF analysis tools need overhaul (fix ctfmerge/libc dependency) changeset: 10207:87c40ea3fc4b user: jmcp <James.McPherson@Sun.COM> date: Wed Jul 29 16:56:03 2009 -0700 summary: 6864230 hiho, hiho, it'ch chtime for CH to go changeset: 10206:51f52702df72 user: John Levon <john.levon@sun.com> date: Wed Jul 29 14:36:30 2009 -0700 summary: 6854065 CTF tools should error out given 1024+-member structures changeset: 7230:429b4f7acf1a user: sn199410 date: Wed Jul 30 16:10:30 2008 -0700 summary: 6575435 ctf tools cannot handle C99 VLAs ("variable length arrays") changeset: 6936:72189fcd99e4 user: sommerfe date: Sun Jun 22 09:13:44 2008 -0700 summary: 6716983 left-for-dead ctfmerge worker threads awake to take out maker ---- - dtrace command utility: changeset: 12507:501806a754d2 user: Alan Maguire <Alan.Maguire@Sun.COM> date: Thu May 27 17:29:51 2010 -0400 summary: PSARC 2010/106 DTrace TCP and UDP providers changeset: 11838:32bb5d254240 user: Liane Praza <Liane.Praza@Sun.COM> date: Tue Mar 02 19:29:26 2010 -0700 summary: PSARC 2010/067 Interim modernization updates changeset: 11270:47a962fe7b45 user: Frank Van Der Linden <Frank.Vanderlinden@Sun.COM> date: Mon Dec 07 13:47:36 2009 -0800 summary: 6907170 fix for 6875656 left out updates to DTrace test suite changeset: 11237:0d23e47ed228 user: Jonathan Haslam <Jonathan.Haslam@Sun.COM> date: Thu Dec 03 13:39:19 2009 +0000 summary: 6795386 macro arguments and globbing in DTrace probe descriptions don't mix changeset: 11153:dec430d20576 user: Frank Van Der Linden <Frank.Vanderlinden@Sun.COM> date: Sun Nov 22 19:22:26 2009 -0800 summary: 6875656 xdt needs to support more XenTrace probes changeset: 11102:b91faef0c984 user: Gavin Maltby <Gavin.Maltby@Sun.COM> date: Thu Nov 19 15:28:11 2009 +1100 summary: PSARC/2009/554 door_xcreate - extended door creation interface for private doors changeset: 11066:cebb50cbe4f9 user: Rafael Vanoni <rafael.vanoni@sun.com> date: Fri Nov 13 01:32:32 2009 -0800 summary: PSARC/2009/396 Tickless Kernel Architecture / lbolt decoupling changeset: 10791:944abfb5b345 user: Jonathan Haslam <Jonathan.Haslam@Sun.COM> date: Wed Oct 14 11:25:23 2009 +0100 summary: 6886953 large symbols lead to stack exhaustion changeset: 10207:87c40ea3fc4b user: jmcp <James.McPherson@Sun.COM> date: Wed Jul 29 16:56:03 2009 -0700 summary: 6864230 hiho, hiho, it'ch chtime for CH to go changeset: 9531:dc8924ef7839 user: Rafael Vanoni <rafael.vanoni@sun.com> date: Mon May 04 11:48:15 2009 -0700 summary: 6730130 dtrace missing printf handler for stdev changeset: 9397:e667d620a75c user: Jonathan Haslam <Jonathan.Haslam@Sun.COM> date: Mon Apr 20 07:58:44 2009 +0100 summary: 6806023 cpc provider event name validation needs to be a bit tighter changeset: 8803:8c01b39012c9 user: Jonathan Haslam <Jonathan.Haslam@Sun.COM> date: Fri Feb 13 07:13:13 2009 +0000 summary: PSARC 2008/480 DTrace CPC Provider changeset: 8605:0189cb9c5358 user: Jonathan Haslam <Jonathan.Haslam@Sun.COM> date: Thu Jan 22 12:09:13 2009 +0000 summary: 6749441 intrstat(1M) shows zeroed values after suspend/resume changeset: 8337:079ecc003ca6 user: Jonathan Haslam <Jonathan.Haslam@Sun.COM> date: Thu Dec 11 11:26:47 2008 +0000 summary: 6750659 drti.o crashes app due to corrupt environment changeset: 8287:771477e4b843 user: John Sonnenschein <John.Sonnenschein@Sun.COM> date: Fri Dec 05 19:08:38 2008 -0800 summary: PSARC 2005/462 Removal of Perl 5.6.1 from Solaris 11 changeset: 7991:d3b751ef3d85 user: Jonathan Haslam <Jonathan.Haslam@Sun.COM> date: Mon Nov 03 10:26:23 2008 +0000 summary: 6738982 Representative thread after DTrace stop() action is incorrect changeset: 7502:da077e5d991e user: Aruna Ramakrishna <aruna@cs.umn.edu> date: Sat Sep 06 05:36:02 2008 -0400 summary: 6706947 tcp_trace should be replaced with dtrace probes. changeset: 7484:a48e950bad22 user: Tom Erickson <tomee@eng.sun.com> date: Wed Sep 03 15:14:25 2008 -0700 summary: 6737926 getAggregate() method fails to specify anonymous aggregation explicitly changeset: 7299:d9a056040774 user: John Beck <John.Beck@Sun.COM> date: Thu Aug 07 12:44:26 2008 -0700 summary: 6734627 protocmp complains about opt/SUNWdtrt/README after TW -> Hg switch changeset: 6998:58787ea78303 user: brendan date: Tue Jul 01 18:28:22 2008 -0700 summary: 6721426 tst.sdtargs.d passes despite dtrace "invalid address" error changeset: 6878:360e73ea6b0c user: brendan date: Fri Jun 13 19:06:55 2008 -0700 summary: PSARC 2008/302 DTrace IP Provider changeset: 6670:1961a43f2335 user: tariq date: Tue May 20 15:08:16 2008 -0700 summary: 6685348 Hypervisor event provider for DTrace changeset: 6554:b5817e112852 user: ahl date: Mon May 05 14:38:24 2008 -0700 summary: 6677812 race between dtrace activities in non-local zones changeset: 6543:465433824d87 user: rie date: Fri May 02 15:01:06 2008 -0700 summary: 6683064 check_rtime could do with some spring cleaning
1031 lines
26 KiB
C
1031 lines
26 KiB
C
/*
|
|
* CDDL HEADER START
|
|
*
|
|
* The contents of this file are subject to the terms of the
|
|
* Common Development and Distribution License (the "License").
|
|
* You may not use this file except in compliance with the License.
|
|
*
|
|
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
|
|
* or http://www.opensolaris.org/os/licensing.
|
|
* See the License for the specific language governing permissions
|
|
* and limitations under the License.
|
|
*
|
|
* When distributing Covered Code, include this CDDL HEADER in each
|
|
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
|
|
* If applicable, add the following below this CDDL HEADER, with the
|
|
* fields enclosed by brackets "[]" replaced with your own identifying
|
|
* information: Portions Copyright [yyyy] [name of copyright owner]
|
|
*
|
|
* CDDL HEADER END
|
|
*/
|
|
/*
|
|
* Copyright 2009 Sun Microsystems, Inc. All rights reserved.
|
|
* Use is subject to license terms.
|
|
*/
|
|
|
|
/*
|
|
* AVL - generic AVL tree implementation for kernel use
|
|
*
|
|
* A complete description of AVL trees can be found in many CS textbooks.
|
|
*
|
|
* Here is a very brief overview. An AVL tree is a binary search tree that is
|
|
* almost perfectly balanced. By "almost" perfectly balanced, we mean that at
|
|
* any given node, the left and right subtrees are allowed to differ in height
|
|
* by at most 1 level.
|
|
*
|
|
* This relaxation from a perfectly balanced binary tree allows doing
|
|
* insertion and deletion relatively efficiently. Searching the tree is
|
|
* still a fast operation, roughly O(log(N)).
|
|
*
|
|
* The key to insertion and deletion is a set of tree maniuplations called
|
|
* rotations, which bring unbalanced subtrees back into the semi-balanced state.
|
|
*
|
|
* This implementation of AVL trees has the following peculiarities:
|
|
*
|
|
* - The AVL specific data structures are physically embedded as fields
|
|
* in the "using" data structures. To maintain generality the code
|
|
* must constantly translate between "avl_node_t *" and containing
|
|
* data structure "void *"s by adding/subracting the avl_offset.
|
|
*
|
|
* - Since the AVL data is always embedded in other structures, there is
|
|
* no locking or memory allocation in the AVL routines. This must be
|
|
* provided for by the enclosing data structure's semantics. Typically,
|
|
* avl_insert()/_add()/_remove()/avl_insert_here() require some kind of
|
|
* exclusive write lock. Other operations require a read lock.
|
|
*
|
|
* - The implementation uses iteration instead of explicit recursion,
|
|
* since it is intended to run on limited size kernel stacks. Since
|
|
* there is no recursion stack present to move "up" in the tree,
|
|
* there is an explicit "parent" link in the avl_node_t.
|
|
*
|
|
* - The left/right children pointers of a node are in an array.
|
|
* In the code, variables (instead of constants) are used to represent
|
|
* left and right indices. The implementation is written as if it only
|
|
* dealt with left handed manipulations. By changing the value assigned
|
|
* to "left", the code also works for right handed trees. The
|
|
* following variables/terms are frequently used:
|
|
*
|
|
* int left; // 0 when dealing with left children,
|
|
* // 1 for dealing with right children
|
|
*
|
|
* int left_heavy; // -1 when left subtree is taller at some node,
|
|
* // +1 when right subtree is taller
|
|
*
|
|
* int right; // will be the opposite of left (0 or 1)
|
|
* int right_heavy;// will be the opposite of left_heavy (-1 or 1)
|
|
*
|
|
* int direction; // 0 for "<" (ie. left child); 1 for ">" (right)
|
|
*
|
|
* Though it is a little more confusing to read the code, the approach
|
|
* allows using half as much code (and hence cache footprint) for tree
|
|
* manipulations and eliminates many conditional branches.
|
|
*
|
|
* - The avl_index_t is an opaque "cookie" used to find nodes at or
|
|
* adjacent to where a new value would be inserted in the tree. The value
|
|
* is a modified "avl_node_t *". The bottom bit (normally 0 for a
|
|
* pointer) is set to indicate if that the new node has a value greater
|
|
* than the value of the indicated "avl_node_t *".
|
|
*/
|
|
|
|
#include <sys/types.h>
|
|
#include <sys/param.h>
|
|
#include <sys/debug.h>
|
|
#include <sys/avl.h>
|
|
#include <sys/cmn_err.h>
|
|
|
|
/*
|
|
* Small arrays to translate between balance (or diff) values and child indeces.
|
|
*
|
|
* Code that deals with binary tree data structures will randomly use
|
|
* left and right children when examining a tree. C "if()" statements
|
|
* which evaluate randomly suffer from very poor hardware branch prediction.
|
|
* In this code we avoid some of the branch mispredictions by using the
|
|
* following translation arrays. They replace random branches with an
|
|
* additional memory reference. Since the translation arrays are both very
|
|
* small the data should remain efficiently in cache.
|
|
*/
|
|
static const int avl_child2balance[2] = {-1, 1};
|
|
static const int avl_balance2child[] = {0, 0, 1};
|
|
|
|
|
|
/*
|
|
* Walk from one node to the previous valued node (ie. an infix walk
|
|
* towards the left). At any given node we do one of 2 things:
|
|
*
|
|
* - If there is a left child, go to it, then to it's rightmost descendant.
|
|
*
|
|
* - otherwise we return thru parent nodes until we've come from a right child.
|
|
*
|
|
* Return Value:
|
|
* NULL - if at the end of the nodes
|
|
* otherwise next node
|
|
*/
|
|
void *
|
|
avl_walk(avl_tree_t *tree, void *oldnode, int left)
|
|
{
|
|
size_t off = tree->avl_offset;
|
|
avl_node_t *node = AVL_DATA2NODE(oldnode, off);
|
|
int right = 1 - left;
|
|
int was_child;
|
|
|
|
|
|
/*
|
|
* nowhere to walk to if tree is empty
|
|
*/
|
|
if (node == NULL)
|
|
return (NULL);
|
|
|
|
/*
|
|
* Visit the previous valued node. There are two possibilities:
|
|
*
|
|
* If this node has a left child, go down one left, then all
|
|
* the way right.
|
|
*/
|
|
if (node->avl_child[left] != NULL) {
|
|
for (node = node->avl_child[left];
|
|
node->avl_child[right] != NULL;
|
|
node = node->avl_child[right])
|
|
;
|
|
/*
|
|
* Otherwise, return thru left children as far as we can.
|
|
*/
|
|
} else {
|
|
for (;;) {
|
|
was_child = AVL_XCHILD(node);
|
|
node = AVL_XPARENT(node);
|
|
if (node == NULL)
|
|
return (NULL);
|
|
if (was_child == right)
|
|
break;
|
|
}
|
|
}
|
|
|
|
return (AVL_NODE2DATA(node, off));
|
|
}
|
|
|
|
/*
|
|
* Return the lowest valued node in a tree or NULL.
|
|
* (leftmost child from root of tree)
|
|
*/
|
|
void *
|
|
avl_first(avl_tree_t *tree)
|
|
{
|
|
avl_node_t *node;
|
|
avl_node_t *prev = NULL;
|
|
size_t off = tree->avl_offset;
|
|
|
|
for (node = tree->avl_root; node != NULL; node = node->avl_child[0])
|
|
prev = node;
|
|
|
|
if (prev != NULL)
|
|
return (AVL_NODE2DATA(prev, off));
|
|
return (NULL);
|
|
}
|
|
|
|
/*
|
|
* Return the highest valued node in a tree or NULL.
|
|
* (rightmost child from root of tree)
|
|
*/
|
|
void *
|
|
avl_last(avl_tree_t *tree)
|
|
{
|
|
avl_node_t *node;
|
|
avl_node_t *prev = NULL;
|
|
size_t off = tree->avl_offset;
|
|
|
|
for (node = tree->avl_root; node != NULL; node = node->avl_child[1])
|
|
prev = node;
|
|
|
|
if (prev != NULL)
|
|
return (AVL_NODE2DATA(prev, off));
|
|
return (NULL);
|
|
}
|
|
|
|
/*
|
|
* Access the node immediately before or after an insertion point.
|
|
*
|
|
* "avl_index_t" is a (avl_node_t *) with the bottom bit indicating a child
|
|
*
|
|
* Return value:
|
|
* NULL: no node in the given direction
|
|
* "void *" of the found tree node
|
|
*/
|
|
void *
|
|
avl_nearest(avl_tree_t *tree, avl_index_t where, int direction)
|
|
{
|
|
int child = AVL_INDEX2CHILD(where);
|
|
avl_node_t *node = AVL_INDEX2NODE(where);
|
|
void *data;
|
|
size_t off = tree->avl_offset;
|
|
|
|
if (node == NULL) {
|
|
ASSERT(tree->avl_root == NULL);
|
|
return (NULL);
|
|
}
|
|
data = AVL_NODE2DATA(node, off);
|
|
if (child != direction)
|
|
return (data);
|
|
|
|
return (avl_walk(tree, data, direction));
|
|
}
|
|
|
|
|
|
/*
|
|
* Search for the node which contains "value". The algorithm is a
|
|
* simple binary tree search.
|
|
*
|
|
* return value:
|
|
* NULL: the value is not in the AVL tree
|
|
* *where (if not NULL) is set to indicate the insertion point
|
|
* "void *" of the found tree node
|
|
*/
|
|
void *
|
|
avl_find(avl_tree_t *tree, const void *value, avl_index_t *where)
|
|
{
|
|
avl_node_t *node;
|
|
avl_node_t *prev = NULL;
|
|
int child = 0;
|
|
int diff;
|
|
size_t off = tree->avl_offset;
|
|
|
|
for (node = tree->avl_root; node != NULL;
|
|
node = node->avl_child[child]) {
|
|
|
|
prev = node;
|
|
|
|
diff = tree->avl_compar(value, AVL_NODE2DATA(node, off));
|
|
ASSERT(-1 <= diff && diff <= 1);
|
|
if (diff == 0) {
|
|
#ifdef DEBUG
|
|
if (where != NULL)
|
|
*where = 0;
|
|
#endif
|
|
return (AVL_NODE2DATA(node, off));
|
|
}
|
|
child = avl_balance2child[1 + diff];
|
|
|
|
}
|
|
|
|
if (where != NULL)
|
|
*where = AVL_MKINDEX(prev, child);
|
|
|
|
return (NULL);
|
|
}
|
|
|
|
|
|
/*
|
|
* Perform a rotation to restore balance at the subtree given by depth.
|
|
*
|
|
* This routine is used by both insertion and deletion. The return value
|
|
* indicates:
|
|
* 0 : subtree did not change height
|
|
* !0 : subtree was reduced in height
|
|
*
|
|
* The code is written as if handling left rotations, right rotations are
|
|
* symmetric and handled by swapping values of variables right/left[_heavy]
|
|
*
|
|
* On input balance is the "new" balance at "node". This value is either
|
|
* -2 or +2.
|
|
*/
|
|
static int
|
|
avl_rotation(avl_tree_t *tree, avl_node_t *node, int balance)
|
|
{
|
|
int left = !(balance < 0); /* when balance = -2, left will be 0 */
|
|
int right = 1 - left;
|
|
int left_heavy = balance >> 1;
|
|
int right_heavy = -left_heavy;
|
|
avl_node_t *parent = AVL_XPARENT(node);
|
|
avl_node_t *child = node->avl_child[left];
|
|
avl_node_t *cright;
|
|
avl_node_t *gchild;
|
|
avl_node_t *gright;
|
|
avl_node_t *gleft;
|
|
int which_child = AVL_XCHILD(node);
|
|
int child_bal = AVL_XBALANCE(child);
|
|
|
|
/* BEGIN CSTYLED */
|
|
/*
|
|
* case 1 : node is overly left heavy, the left child is balanced or
|
|
* also left heavy. This requires the following rotation.
|
|
*
|
|
* (node bal:-2)
|
|
* / \
|
|
* / \
|
|
* (child bal:0 or -1)
|
|
* / \
|
|
* / \
|
|
* cright
|
|
*
|
|
* becomes:
|
|
*
|
|
* (child bal:1 or 0)
|
|
* / \
|
|
* / \
|
|
* (node bal:-1 or 0)
|
|
* / \
|
|
* / \
|
|
* cright
|
|
*
|
|
* we detect this situation by noting that child's balance is not
|
|
* right_heavy.
|
|
*/
|
|
/* END CSTYLED */
|
|
if (child_bal != right_heavy) {
|
|
|
|
/*
|
|
* compute new balance of nodes
|
|
*
|
|
* If child used to be left heavy (now balanced) we reduced
|
|
* the height of this sub-tree -- used in "return...;" below
|
|
*/
|
|
child_bal += right_heavy; /* adjust towards right */
|
|
|
|
/*
|
|
* move "cright" to be node's left child
|
|
*/
|
|
cright = child->avl_child[right];
|
|
node->avl_child[left] = cright;
|
|
if (cright != NULL) {
|
|
AVL_SETPARENT(cright, node);
|
|
AVL_SETCHILD(cright, left);
|
|
}
|
|
|
|
/*
|
|
* move node to be child's right child
|
|
*/
|
|
child->avl_child[right] = node;
|
|
AVL_SETBALANCE(node, -child_bal);
|
|
AVL_SETCHILD(node, right);
|
|
AVL_SETPARENT(node, child);
|
|
|
|
/*
|
|
* update the pointer into this subtree
|
|
*/
|
|
AVL_SETBALANCE(child, child_bal);
|
|
AVL_SETCHILD(child, which_child);
|
|
AVL_SETPARENT(child, parent);
|
|
if (parent != NULL)
|
|
parent->avl_child[which_child] = child;
|
|
else
|
|
tree->avl_root = child;
|
|
|
|
return (child_bal == 0);
|
|
}
|
|
|
|
/* BEGIN CSTYLED */
|
|
/*
|
|
* case 2 : When node is left heavy, but child is right heavy we use
|
|
* a different rotation.
|
|
*
|
|
* (node b:-2)
|
|
* / \
|
|
* / \
|
|
* / \
|
|
* (child b:+1)
|
|
* / \
|
|
* / \
|
|
* (gchild b: != 0)
|
|
* / \
|
|
* / \
|
|
* gleft gright
|
|
*
|
|
* becomes:
|
|
*
|
|
* (gchild b:0)
|
|
* / \
|
|
* / \
|
|
* / \
|
|
* (child b:?) (node b:?)
|
|
* / \ / \
|
|
* / \ / \
|
|
* gleft gright
|
|
*
|
|
* computing the new balances is more complicated. As an example:
|
|
* if gchild was right_heavy, then child is now left heavy
|
|
* else it is balanced
|
|
*/
|
|
/* END CSTYLED */
|
|
gchild = child->avl_child[right];
|
|
gleft = gchild->avl_child[left];
|
|
gright = gchild->avl_child[right];
|
|
|
|
/*
|
|
* move gright to left child of node and
|
|
*
|
|
* move gleft to right child of node
|
|
*/
|
|
node->avl_child[left] = gright;
|
|
if (gright != NULL) {
|
|
AVL_SETPARENT(gright, node);
|
|
AVL_SETCHILD(gright, left);
|
|
}
|
|
|
|
child->avl_child[right] = gleft;
|
|
if (gleft != NULL) {
|
|
AVL_SETPARENT(gleft, child);
|
|
AVL_SETCHILD(gleft, right);
|
|
}
|
|
|
|
/*
|
|
* move child to left child of gchild and
|
|
*
|
|
* move node to right child of gchild and
|
|
*
|
|
* fixup parent of all this to point to gchild
|
|
*/
|
|
balance = AVL_XBALANCE(gchild);
|
|
gchild->avl_child[left] = child;
|
|
AVL_SETBALANCE(child, (balance == right_heavy ? left_heavy : 0));
|
|
AVL_SETPARENT(child, gchild);
|
|
AVL_SETCHILD(child, left);
|
|
|
|
gchild->avl_child[right] = node;
|
|
AVL_SETBALANCE(node, (balance == left_heavy ? right_heavy : 0));
|
|
AVL_SETPARENT(node, gchild);
|
|
AVL_SETCHILD(node, right);
|
|
|
|
AVL_SETBALANCE(gchild, 0);
|
|
AVL_SETPARENT(gchild, parent);
|
|
AVL_SETCHILD(gchild, which_child);
|
|
if (parent != NULL)
|
|
parent->avl_child[which_child] = gchild;
|
|
else
|
|
tree->avl_root = gchild;
|
|
|
|
return (1); /* the new tree is always shorter */
|
|
}
|
|
|
|
|
|
/*
|
|
* Insert a new node into an AVL tree at the specified (from avl_find()) place.
|
|
*
|
|
* Newly inserted nodes are always leaf nodes in the tree, since avl_find()
|
|
* searches out to the leaf positions. The avl_index_t indicates the node
|
|
* which will be the parent of the new node.
|
|
*
|
|
* After the node is inserted, a single rotation further up the tree may
|
|
* be necessary to maintain an acceptable AVL balance.
|
|
*/
|
|
void
|
|
avl_insert(avl_tree_t *tree, void *new_data, avl_index_t where)
|
|
{
|
|
avl_node_t *node;
|
|
avl_node_t *parent = AVL_INDEX2NODE(where);
|
|
int old_balance;
|
|
int new_balance;
|
|
int which_child = AVL_INDEX2CHILD(where);
|
|
size_t off = tree->avl_offset;
|
|
|
|
ASSERT(tree);
|
|
#ifdef _LP64
|
|
ASSERT(((uintptr_t)new_data & 0x7) == 0);
|
|
#endif
|
|
|
|
node = AVL_DATA2NODE(new_data, off);
|
|
|
|
/*
|
|
* First, add the node to the tree at the indicated position.
|
|
*/
|
|
++tree->avl_numnodes;
|
|
|
|
node->avl_child[0] = NULL;
|
|
node->avl_child[1] = NULL;
|
|
|
|
AVL_SETCHILD(node, which_child);
|
|
AVL_SETBALANCE(node, 0);
|
|
AVL_SETPARENT(node, parent);
|
|
if (parent != NULL) {
|
|
ASSERT(parent->avl_child[which_child] == NULL);
|
|
parent->avl_child[which_child] = node;
|
|
} else {
|
|
ASSERT(tree->avl_root == NULL);
|
|
tree->avl_root = node;
|
|
}
|
|
/*
|
|
* Now, back up the tree modifying the balance of all nodes above the
|
|
* insertion point. If we get to a highly unbalanced ancestor, we
|
|
* need to do a rotation. If we back out of the tree we are done.
|
|
* If we brought any subtree into perfect balance (0), we are also done.
|
|
*/
|
|
for (;;) {
|
|
node = parent;
|
|
if (node == NULL)
|
|
return;
|
|
|
|
/*
|
|
* Compute the new balance
|
|
*/
|
|
old_balance = AVL_XBALANCE(node);
|
|
new_balance = old_balance + avl_child2balance[which_child];
|
|
|
|
/*
|
|
* If we introduced equal balance, then we are done immediately
|
|
*/
|
|
if (new_balance == 0) {
|
|
AVL_SETBALANCE(node, 0);
|
|
return;
|
|
}
|
|
|
|
/*
|
|
* If both old and new are not zero we went
|
|
* from -1 to -2 balance, do a rotation.
|
|
*/
|
|
if (old_balance != 0)
|
|
break;
|
|
|
|
AVL_SETBALANCE(node, new_balance);
|
|
parent = AVL_XPARENT(node);
|
|
which_child = AVL_XCHILD(node);
|
|
}
|
|
|
|
/*
|
|
* perform a rotation to fix the tree and return
|
|
*/
|
|
(void) avl_rotation(tree, node, new_balance);
|
|
}
|
|
|
|
/*
|
|
* Insert "new_data" in "tree" in the given "direction" either after or
|
|
* before (AVL_AFTER, AVL_BEFORE) the data "here".
|
|
*
|
|
* Insertions can only be done at empty leaf points in the tree, therefore
|
|
* if the given child of the node is already present we move to either
|
|
* the AVL_PREV or AVL_NEXT and reverse the insertion direction. Since
|
|
* every other node in the tree is a leaf, this always works.
|
|
*
|
|
* To help developers using this interface, we assert that the new node
|
|
* is correctly ordered at every step of the way in DEBUG kernels.
|
|
*/
|
|
void
|
|
avl_insert_here(
|
|
avl_tree_t *tree,
|
|
void *new_data,
|
|
void *here,
|
|
int direction)
|
|
{
|
|
avl_node_t *node;
|
|
int child = direction; /* rely on AVL_BEFORE == 0, AVL_AFTER == 1 */
|
|
#ifdef DEBUG
|
|
int diff;
|
|
#endif
|
|
|
|
ASSERT(tree != NULL);
|
|
ASSERT(new_data != NULL);
|
|
ASSERT(here != NULL);
|
|
ASSERT(direction == AVL_BEFORE || direction == AVL_AFTER);
|
|
|
|
/*
|
|
* If corresponding child of node is not NULL, go to the neighboring
|
|
* node and reverse the insertion direction.
|
|
*/
|
|
node = AVL_DATA2NODE(here, tree->avl_offset);
|
|
|
|
#ifdef DEBUG
|
|
diff = tree->avl_compar(new_data, here);
|
|
ASSERT(-1 <= diff && diff <= 1);
|
|
ASSERT(diff != 0);
|
|
ASSERT(diff > 0 ? child == 1 : child == 0);
|
|
#endif
|
|
|
|
if (node->avl_child[child] != NULL) {
|
|
node = node->avl_child[child];
|
|
child = 1 - child;
|
|
while (node->avl_child[child] != NULL) {
|
|
#ifdef DEBUG
|
|
diff = tree->avl_compar(new_data,
|
|
AVL_NODE2DATA(node, tree->avl_offset));
|
|
ASSERT(-1 <= diff && diff <= 1);
|
|
ASSERT(diff != 0);
|
|
ASSERT(diff > 0 ? child == 1 : child == 0);
|
|
#endif
|
|
node = node->avl_child[child];
|
|
}
|
|
#ifdef DEBUG
|
|
diff = tree->avl_compar(new_data,
|
|
AVL_NODE2DATA(node, tree->avl_offset));
|
|
ASSERT(-1 <= diff && diff <= 1);
|
|
ASSERT(diff != 0);
|
|
ASSERT(diff > 0 ? child == 1 : child == 0);
|
|
#endif
|
|
}
|
|
ASSERT(node->avl_child[child] == NULL);
|
|
|
|
avl_insert(tree, new_data, AVL_MKINDEX(node, child));
|
|
}
|
|
|
|
/*
|
|
* Add a new node to an AVL tree.
|
|
*/
|
|
void
|
|
avl_add(avl_tree_t *tree, void *new_node)
|
|
{
|
|
avl_index_t where;
|
|
|
|
/*
|
|
* This is unfortunate. We want to call panic() here, even for
|
|
* non-DEBUG kernels. In userland, however, we can't depend on anything
|
|
* in libc or else the rtld build process gets confused. So, all we can
|
|
* do in userland is resort to a normal ASSERT().
|
|
*/
|
|
if (avl_find(tree, new_node, &where) != NULL)
|
|
#ifdef _KERNEL
|
|
panic("avl_find() succeeded inside avl_add()");
|
|
#else
|
|
ASSERT(0);
|
|
#endif
|
|
avl_insert(tree, new_node, where);
|
|
}
|
|
|
|
/*
|
|
* Delete a node from the AVL tree. Deletion is similar to insertion, but
|
|
* with 2 complications.
|
|
*
|
|
* First, we may be deleting an interior node. Consider the following subtree:
|
|
*
|
|
* d c c
|
|
* / \ / \ / \
|
|
* b e b e b e
|
|
* / \ / \ /
|
|
* a c a a
|
|
*
|
|
* When we are deleting node (d), we find and bring up an adjacent valued leaf
|
|
* node, say (c), to take the interior node's place. In the code this is
|
|
* handled by temporarily swapping (d) and (c) in the tree and then using
|
|
* common code to delete (d) from the leaf position.
|
|
*
|
|
* Secondly, an interior deletion from a deep tree may require more than one
|
|
* rotation to fix the balance. This is handled by moving up the tree through
|
|
* parents and applying rotations as needed. The return value from
|
|
* avl_rotation() is used to detect when a subtree did not change overall
|
|
* height due to a rotation.
|
|
*/
|
|
void
|
|
avl_remove(avl_tree_t *tree, void *data)
|
|
{
|
|
avl_node_t *delete;
|
|
avl_node_t *parent;
|
|
avl_node_t *node;
|
|
avl_node_t tmp;
|
|
int old_balance;
|
|
int new_balance;
|
|
int left;
|
|
int right;
|
|
int which_child;
|
|
size_t off = tree->avl_offset;
|
|
|
|
ASSERT(tree);
|
|
|
|
delete = AVL_DATA2NODE(data, off);
|
|
|
|
/*
|
|
* Deletion is easiest with a node that has at most 1 child.
|
|
* We swap a node with 2 children with a sequentially valued
|
|
* neighbor node. That node will have at most 1 child. Note this
|
|
* has no effect on the ordering of the remaining nodes.
|
|
*
|
|
* As an optimization, we choose the greater neighbor if the tree
|
|
* is right heavy, otherwise the left neighbor. This reduces the
|
|
* number of rotations needed.
|
|
*/
|
|
if (delete->avl_child[0] != NULL && delete->avl_child[1] != NULL) {
|
|
|
|
/*
|
|
* choose node to swap from whichever side is taller
|
|
*/
|
|
old_balance = AVL_XBALANCE(delete);
|
|
left = avl_balance2child[old_balance + 1];
|
|
right = 1 - left;
|
|
|
|
/*
|
|
* get to the previous value'd node
|
|
* (down 1 left, as far as possible right)
|
|
*/
|
|
for (node = delete->avl_child[left];
|
|
node->avl_child[right] != NULL;
|
|
node = node->avl_child[right])
|
|
;
|
|
|
|
/*
|
|
* create a temp placeholder for 'node'
|
|
* move 'node' to delete's spot in the tree
|
|
*/
|
|
tmp = *node;
|
|
|
|
*node = *delete;
|
|
if (node->avl_child[left] == node)
|
|
node->avl_child[left] = &tmp;
|
|
|
|
parent = AVL_XPARENT(node);
|
|
if (parent != NULL)
|
|
parent->avl_child[AVL_XCHILD(node)] = node;
|
|
else
|
|
tree->avl_root = node;
|
|
AVL_SETPARENT(node->avl_child[left], node);
|
|
AVL_SETPARENT(node->avl_child[right], node);
|
|
|
|
/*
|
|
* Put tmp where node used to be (just temporary).
|
|
* It always has a parent and at most 1 child.
|
|
*/
|
|
delete = &tmp;
|
|
parent = AVL_XPARENT(delete);
|
|
parent->avl_child[AVL_XCHILD(delete)] = delete;
|
|
which_child = (delete->avl_child[1] != 0);
|
|
if (delete->avl_child[which_child] != NULL)
|
|
AVL_SETPARENT(delete->avl_child[which_child], delete);
|
|
}
|
|
|
|
|
|
/*
|
|
* Here we know "delete" is at least partially a leaf node. It can
|
|
* be easily removed from the tree.
|
|
*/
|
|
ASSERT(tree->avl_numnodes > 0);
|
|
--tree->avl_numnodes;
|
|
parent = AVL_XPARENT(delete);
|
|
which_child = AVL_XCHILD(delete);
|
|
if (delete->avl_child[0] != NULL)
|
|
node = delete->avl_child[0];
|
|
else
|
|
node = delete->avl_child[1];
|
|
|
|
/*
|
|
* Connect parent directly to node (leaving out delete).
|
|
*/
|
|
if (node != NULL) {
|
|
AVL_SETPARENT(node, parent);
|
|
AVL_SETCHILD(node, which_child);
|
|
}
|
|
if (parent == NULL) {
|
|
tree->avl_root = node;
|
|
return;
|
|
}
|
|
parent->avl_child[which_child] = node;
|
|
|
|
|
|
/*
|
|
* Since the subtree is now shorter, begin adjusting parent balances
|
|
* and performing any needed rotations.
|
|
*/
|
|
do {
|
|
|
|
/*
|
|
* Move up the tree and adjust the balance
|
|
*
|
|
* Capture the parent and which_child values for the next
|
|
* iteration before any rotations occur.
|
|
*/
|
|
node = parent;
|
|
old_balance = AVL_XBALANCE(node);
|
|
new_balance = old_balance - avl_child2balance[which_child];
|
|
parent = AVL_XPARENT(node);
|
|
which_child = AVL_XCHILD(node);
|
|
|
|
/*
|
|
* If a node was in perfect balance but isn't anymore then
|
|
* we can stop, since the height didn't change above this point
|
|
* due to a deletion.
|
|
*/
|
|
if (old_balance == 0) {
|
|
AVL_SETBALANCE(node, new_balance);
|
|
break;
|
|
}
|
|
|
|
/*
|
|
* If the new balance is zero, we don't need to rotate
|
|
* else
|
|
* need a rotation to fix the balance.
|
|
* If the rotation doesn't change the height
|
|
* of the sub-tree we have finished adjusting.
|
|
*/
|
|
if (new_balance == 0)
|
|
AVL_SETBALANCE(node, new_balance);
|
|
else if (!avl_rotation(tree, node, new_balance))
|
|
break;
|
|
} while (parent != NULL);
|
|
}
|
|
|
|
#define AVL_REINSERT(tree, obj) \
|
|
avl_remove((tree), (obj)); \
|
|
avl_add((tree), (obj))
|
|
|
|
boolean_t
|
|
avl_update_lt(avl_tree_t *t, void *obj)
|
|
{
|
|
void *neighbor;
|
|
|
|
ASSERT(((neighbor = AVL_NEXT(t, obj)) == NULL) ||
|
|
(t->avl_compar(obj, neighbor) <= 0));
|
|
|
|
neighbor = AVL_PREV(t, obj);
|
|
if ((neighbor != NULL) && (t->avl_compar(obj, neighbor) < 0)) {
|
|
AVL_REINSERT(t, obj);
|
|
return (B_TRUE);
|
|
}
|
|
|
|
return (B_FALSE);
|
|
}
|
|
|
|
boolean_t
|
|
avl_update_gt(avl_tree_t *t, void *obj)
|
|
{
|
|
void *neighbor;
|
|
|
|
ASSERT(((neighbor = AVL_PREV(t, obj)) == NULL) ||
|
|
(t->avl_compar(obj, neighbor) >= 0));
|
|
|
|
neighbor = AVL_NEXT(t, obj);
|
|
if ((neighbor != NULL) && (t->avl_compar(obj, neighbor) > 0)) {
|
|
AVL_REINSERT(t, obj);
|
|
return (B_TRUE);
|
|
}
|
|
|
|
return (B_FALSE);
|
|
}
|
|
|
|
boolean_t
|
|
avl_update(avl_tree_t *t, void *obj)
|
|
{
|
|
void *neighbor;
|
|
|
|
neighbor = AVL_PREV(t, obj);
|
|
if ((neighbor != NULL) && (t->avl_compar(obj, neighbor) < 0)) {
|
|
AVL_REINSERT(t, obj);
|
|
return (B_TRUE);
|
|
}
|
|
|
|
neighbor = AVL_NEXT(t, obj);
|
|
if ((neighbor != NULL) && (t->avl_compar(obj, neighbor) > 0)) {
|
|
AVL_REINSERT(t, obj);
|
|
return (B_TRUE);
|
|
}
|
|
|
|
return (B_FALSE);
|
|
}
|
|
|
|
/*
|
|
* initialize a new AVL tree
|
|
*/
|
|
void
|
|
avl_create(avl_tree_t *tree, int (*compar) (const void *, const void *),
|
|
size_t size, size_t offset)
|
|
{
|
|
ASSERT(tree);
|
|
ASSERT(compar);
|
|
ASSERT(size > 0);
|
|
ASSERT(size >= offset + sizeof (avl_node_t));
|
|
#ifdef _LP64
|
|
ASSERT((offset & 0x7) == 0);
|
|
#endif
|
|
|
|
tree->avl_compar = compar;
|
|
tree->avl_root = NULL;
|
|
tree->avl_numnodes = 0;
|
|
tree->avl_size = size;
|
|
tree->avl_offset = offset;
|
|
}
|
|
|
|
/*
|
|
* Delete a tree.
|
|
*/
|
|
/* ARGSUSED */
|
|
void
|
|
avl_destroy(avl_tree_t *tree)
|
|
{
|
|
ASSERT(tree);
|
|
ASSERT(tree->avl_numnodes == 0);
|
|
ASSERT(tree->avl_root == NULL);
|
|
}
|
|
|
|
|
|
/*
|
|
* Return the number of nodes in an AVL tree.
|
|
*/
|
|
ulong_t
|
|
avl_numnodes(avl_tree_t *tree)
|
|
{
|
|
ASSERT(tree);
|
|
return (tree->avl_numnodes);
|
|
}
|
|
|
|
boolean_t
|
|
avl_is_empty(avl_tree_t *tree)
|
|
{
|
|
ASSERT(tree);
|
|
return (tree->avl_numnodes == 0);
|
|
}
|
|
|
|
#define CHILDBIT (1L)
|
|
|
|
/*
|
|
* Post-order tree walk used to visit all tree nodes and destroy the tree
|
|
* in post order. This is used for destroying a tree w/o paying any cost
|
|
* for rebalancing it.
|
|
*
|
|
* example:
|
|
*
|
|
* void *cookie = NULL;
|
|
* my_data_t *node;
|
|
*
|
|
* while ((node = avl_destroy_nodes(tree, &cookie)) != NULL)
|
|
* free(node);
|
|
* avl_destroy(tree);
|
|
*
|
|
* The cookie is really an avl_node_t to the current node's parent and
|
|
* an indication of which child you looked at last.
|
|
*
|
|
* On input, a cookie value of CHILDBIT indicates the tree is done.
|
|
*/
|
|
void *
|
|
avl_destroy_nodes(avl_tree_t *tree, void **cookie)
|
|
{
|
|
avl_node_t *node;
|
|
avl_node_t *parent;
|
|
int child;
|
|
void *first;
|
|
size_t off = tree->avl_offset;
|
|
|
|
/*
|
|
* Initial calls go to the first node or it's right descendant.
|
|
*/
|
|
if (*cookie == NULL) {
|
|
first = avl_first(tree);
|
|
|
|
/*
|
|
* deal with an empty tree
|
|
*/
|
|
if (first == NULL) {
|
|
*cookie = (void *)CHILDBIT;
|
|
return (NULL);
|
|
}
|
|
|
|
node = AVL_DATA2NODE(first, off);
|
|
parent = AVL_XPARENT(node);
|
|
goto check_right_side;
|
|
}
|
|
|
|
/*
|
|
* If there is no parent to return to we are done.
|
|
*/
|
|
parent = (avl_node_t *)((uintptr_t)(*cookie) & ~CHILDBIT);
|
|
if (parent == NULL) {
|
|
if (tree->avl_root != NULL) {
|
|
ASSERT(tree->avl_numnodes == 1);
|
|
tree->avl_root = NULL;
|
|
tree->avl_numnodes = 0;
|
|
}
|
|
return (NULL);
|
|
}
|
|
|
|
/*
|
|
* Remove the child pointer we just visited from the parent and tree.
|
|
*/
|
|
child = (uintptr_t)(*cookie) & CHILDBIT;
|
|
parent->avl_child[child] = NULL;
|
|
ASSERT(tree->avl_numnodes > 1);
|
|
--tree->avl_numnodes;
|
|
|
|
/*
|
|
* If we just did a right child or there isn't one, go up to parent.
|
|
*/
|
|
if (child == 1 || parent->avl_child[1] == NULL) {
|
|
node = parent;
|
|
parent = AVL_XPARENT(parent);
|
|
goto done;
|
|
}
|
|
|
|
/*
|
|
* Do parent's right child, then leftmost descendent.
|
|
*/
|
|
node = parent->avl_child[1];
|
|
while (node->avl_child[0] != NULL) {
|
|
parent = node;
|
|
node = node->avl_child[0];
|
|
}
|
|
|
|
/*
|
|
* If here, we moved to a left child. It may have one
|
|
* child on the right (when balance == +1).
|
|
*/
|
|
check_right_side:
|
|
if (node->avl_child[1] != NULL) {
|
|
ASSERT(AVL_XBALANCE(node) == 1);
|
|
parent = node;
|
|
node = node->avl_child[1];
|
|
ASSERT(node->avl_child[0] == NULL &&
|
|
node->avl_child[1] == NULL);
|
|
} else {
|
|
ASSERT(AVL_XBALANCE(node) <= 0);
|
|
}
|
|
|
|
done:
|
|
if (parent == NULL) {
|
|
*cookie = (void *)CHILDBIT;
|
|
ASSERT(node == tree->avl_root);
|
|
} else {
|
|
*cookie = (void *)((uintptr_t)parent | AVL_XCHILD(node));
|
|
}
|
|
|
|
return (AVL_NODE2DATA(node, off));
|
|
}
|