xref: /onnv-gate/usr/src/lib/libuutil/common/uu_avl.c (revision 2856:6f4d5ee1906a)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*2856Snd150628  * Common Development and Distribution License (the "License").
6*2856Snd150628  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
22*2856Snd150628  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
270Sstevel@tonic-gate 
280Sstevel@tonic-gate #include "libuutil_common.h"
290Sstevel@tonic-gate 
300Sstevel@tonic-gate #include <stdlib.h>
310Sstevel@tonic-gate #include <string.h>
320Sstevel@tonic-gate #include <unistd.h>
330Sstevel@tonic-gate #include <sys/avl.h>
340Sstevel@tonic-gate 
350Sstevel@tonic-gate static uu_avl_pool_t	uu_null_apool = { &uu_null_apool, &uu_null_apool };
360Sstevel@tonic-gate static pthread_mutex_t	uu_apool_list_lock = PTHREAD_MUTEX_INITIALIZER;
370Sstevel@tonic-gate 
380Sstevel@tonic-gate /*
390Sstevel@tonic-gate  * The index mark change on every insert and delete, to catch stale
400Sstevel@tonic-gate  * references.
410Sstevel@tonic-gate  *
420Sstevel@tonic-gate  * We leave the low bit alone, since the avl code uses it.
430Sstevel@tonic-gate  */
440Sstevel@tonic-gate #define	INDEX_MAX		(sizeof (uintptr_t) - 2)
450Sstevel@tonic-gate #define	INDEX_NEXT(m)		(((m) == INDEX_MAX)? 2 : ((m) + 2) & INDEX_MAX)
460Sstevel@tonic-gate 
470Sstevel@tonic-gate #define	INDEX_DECODE(i)		((i) & ~INDEX_MAX)
480Sstevel@tonic-gate #define	INDEX_ENCODE(p, n)	(((n) & ~INDEX_MAX) | (p)->ua_index)
490Sstevel@tonic-gate #define	INDEX_VALID(p, i)	(((i) & INDEX_MAX) == (p)->ua_index)
500Sstevel@tonic-gate #define	INDEX_CHECK(i)		(((i) & INDEX_MAX) != 0)
510Sstevel@tonic-gate 
520Sstevel@tonic-gate /*
530Sstevel@tonic-gate  * When an element is inactive (not in a tree), we keep a marked pointer to
540Sstevel@tonic-gate  * its containing pool in its first word, and a NULL pointer in its second.
550Sstevel@tonic-gate  *
560Sstevel@tonic-gate  * On insert, we use these to verify that it comes from the correct pool.
570Sstevel@tonic-gate  */
580Sstevel@tonic-gate #define	NODE_ARRAY(p, n)	((uintptr_t *)((uintptr_t)(n) + \
590Sstevel@tonic-gate 				    (pp)->uap_nodeoffset))
600Sstevel@tonic-gate 
610Sstevel@tonic-gate #define	POOL_TO_MARKER(pp) (((uintptr_t)(pp) | 1))
620Sstevel@tonic-gate 
630Sstevel@tonic-gate #define	DEAD_MARKER		0xc4
640Sstevel@tonic-gate 
650Sstevel@tonic-gate uu_avl_pool_t *
660Sstevel@tonic-gate uu_avl_pool_create(const char *name, size_t objsize, size_t nodeoffset,
670Sstevel@tonic-gate     uu_compare_fn_t *compare_func, uint32_t flags)
680Sstevel@tonic-gate {
690Sstevel@tonic-gate 	uu_avl_pool_t *pp, *next, *prev;
700Sstevel@tonic-gate 
710Sstevel@tonic-gate 	if (name == NULL ||
720Sstevel@tonic-gate 	    uu_check_name(name, UU_NAME_DOMAIN) == -1 ||
730Sstevel@tonic-gate 	    nodeoffset + sizeof (uu_avl_node_t) > objsize ||
740Sstevel@tonic-gate 	    compare_func == NULL) {
750Sstevel@tonic-gate 		uu_set_error(UU_ERROR_INVALID_ARGUMENT);
760Sstevel@tonic-gate 		return (NULL);
770Sstevel@tonic-gate 	}
780Sstevel@tonic-gate 
790Sstevel@tonic-gate 	if (flags & ~UU_AVL_POOL_DEBUG) {
800Sstevel@tonic-gate 		uu_set_error(UU_ERROR_UNKNOWN_FLAG);
810Sstevel@tonic-gate 		return (NULL);
820Sstevel@tonic-gate 	}
830Sstevel@tonic-gate 
840Sstevel@tonic-gate 	pp = uu_zalloc(sizeof (uu_avl_pool_t));
850Sstevel@tonic-gate 	if (pp == NULL) {
860Sstevel@tonic-gate 		uu_set_error(UU_ERROR_NO_MEMORY);
870Sstevel@tonic-gate 		return (NULL);
880Sstevel@tonic-gate 	}
890Sstevel@tonic-gate 
900Sstevel@tonic-gate 	(void) strlcpy(pp->uap_name, name, sizeof (pp->uap_name));
910Sstevel@tonic-gate 	pp->uap_nodeoffset = nodeoffset;
920Sstevel@tonic-gate 	pp->uap_objsize = objsize;
930Sstevel@tonic-gate 	pp->uap_cmp = compare_func;
940Sstevel@tonic-gate 	if (flags & UU_AVL_POOL_DEBUG)
950Sstevel@tonic-gate 		pp->uap_debug = 1;
960Sstevel@tonic-gate 	pp->uap_last_index = 0;
970Sstevel@tonic-gate 
980Sstevel@tonic-gate 	(void) pthread_mutex_init(&pp->uap_lock, NULL);
990Sstevel@tonic-gate 
100407Sjwadams 	pp->uap_null_avl.ua_next_enc = UU_PTR_ENCODE(&pp->uap_null_avl);
101407Sjwadams 	pp->uap_null_avl.ua_prev_enc = UU_PTR_ENCODE(&pp->uap_null_avl);
1020Sstevel@tonic-gate 
1030Sstevel@tonic-gate 	(void) pthread_mutex_lock(&uu_apool_list_lock);
1040Sstevel@tonic-gate 	pp->uap_next = next = &uu_null_apool;
1050Sstevel@tonic-gate 	pp->uap_prev = prev = next->uap_prev;
1060Sstevel@tonic-gate 	next->uap_prev = pp;
1070Sstevel@tonic-gate 	prev->uap_next = pp;
1080Sstevel@tonic-gate 	(void) pthread_mutex_unlock(&uu_apool_list_lock);
1090Sstevel@tonic-gate 
1100Sstevel@tonic-gate 	return (pp);
1110Sstevel@tonic-gate }
1120Sstevel@tonic-gate 
1130Sstevel@tonic-gate void
1140Sstevel@tonic-gate uu_avl_pool_destroy(uu_avl_pool_t *pp)
1150Sstevel@tonic-gate {
1160Sstevel@tonic-gate 	if (pp->uap_debug) {
117407Sjwadams 		if (pp->uap_null_avl.ua_next_enc !=
118407Sjwadams 		    UU_PTR_ENCODE(&pp->uap_null_avl) ||
119407Sjwadams 		    pp->uap_null_avl.ua_prev_enc !=
120407Sjwadams 		    UU_PTR_ENCODE(&pp->uap_null_avl)) {
1210Sstevel@tonic-gate 			uu_panic("uu_avl_pool_destroy: Pool \"%.*s\" (%p) has "
1220Sstevel@tonic-gate 			    "outstanding avls, or is corrupt.\n",
1230Sstevel@tonic-gate 			    sizeof (pp->uap_name), pp->uap_name, pp);
1240Sstevel@tonic-gate 		}
1250Sstevel@tonic-gate 	}
1260Sstevel@tonic-gate 	(void) pthread_mutex_lock(&uu_apool_list_lock);
1270Sstevel@tonic-gate 	pp->uap_next->uap_prev = pp->uap_prev;
1280Sstevel@tonic-gate 	pp->uap_prev->uap_next = pp->uap_next;
1290Sstevel@tonic-gate 	(void) pthread_mutex_unlock(&uu_apool_list_lock);
1300Sstevel@tonic-gate 	pp->uap_prev = NULL;
1310Sstevel@tonic-gate 	pp->uap_next = NULL;
1320Sstevel@tonic-gate 	uu_free(pp);
1330Sstevel@tonic-gate }
1340Sstevel@tonic-gate 
1350Sstevel@tonic-gate void
1360Sstevel@tonic-gate uu_avl_node_init(void *base, uu_avl_node_t *np, uu_avl_pool_t *pp)
1370Sstevel@tonic-gate {
1380Sstevel@tonic-gate 	uintptr_t *na = (uintptr_t *)np;
1390Sstevel@tonic-gate 
1400Sstevel@tonic-gate 	if (pp->uap_debug) {
1410Sstevel@tonic-gate 		uintptr_t offset = (uintptr_t)np - (uintptr_t)base;
1420Sstevel@tonic-gate 		if (offset + sizeof (*np) > pp->uap_objsize) {
1430Sstevel@tonic-gate 			uu_panic("uu_avl_node_init(%p, %p, %p (\"%s\")): "
1440Sstevel@tonic-gate 			    "offset %ld doesn't fit in object (size %ld)\n",
1450Sstevel@tonic-gate 			    base, np, pp, pp->uap_name, offset,
1460Sstevel@tonic-gate 			    pp->uap_objsize);
1470Sstevel@tonic-gate 		}
1480Sstevel@tonic-gate 		if (offset != pp->uap_nodeoffset) {
1490Sstevel@tonic-gate 			uu_panic("uu_avl_node_init(%p, %p, %p (\"%s\")): "
1500Sstevel@tonic-gate 			    "offset %ld doesn't match pool's offset (%ld)\n",
1510Sstevel@tonic-gate 			    base, np, pp, pp->uap_name, offset,
1520Sstevel@tonic-gate 			    pp->uap_objsize);
1530Sstevel@tonic-gate 		}
1540Sstevel@tonic-gate 	}
1550Sstevel@tonic-gate 
1560Sstevel@tonic-gate 	na[0] = POOL_TO_MARKER(pp);
157*2856Snd150628 	na[1] = 0;
1580Sstevel@tonic-gate }
1590Sstevel@tonic-gate 
1600Sstevel@tonic-gate void
1610Sstevel@tonic-gate uu_avl_node_fini(void *base, uu_avl_node_t *np, uu_avl_pool_t *pp)
1620Sstevel@tonic-gate {
1630Sstevel@tonic-gate 	uintptr_t *na = (uintptr_t *)np;
1640Sstevel@tonic-gate 
1650Sstevel@tonic-gate 	if (pp->uap_debug) {
1660Sstevel@tonic-gate 		if (na[0] == DEAD_MARKER && na[1] == DEAD_MARKER) {
1670Sstevel@tonic-gate 			uu_panic("uu_avl_node_fini(%p, %p, %p (\"%s\")): "
1680Sstevel@tonic-gate 			    "node already finied\n",
1690Sstevel@tonic-gate 			    base, np, pp, pp->uap_name);
1700Sstevel@tonic-gate 		}
171*2856Snd150628 		if (na[0] != POOL_TO_MARKER(pp) || na[1] != 0) {
1720Sstevel@tonic-gate 			uu_panic("uu_avl_node_fini(%p, %p, %p (\"%s\")): "
1730Sstevel@tonic-gate 			    "node corrupt, in tree, or in different pool\n",
1740Sstevel@tonic-gate 			    base, np, pp, pp->uap_name);
1750Sstevel@tonic-gate 		}
1760Sstevel@tonic-gate 	}
1770Sstevel@tonic-gate 
1780Sstevel@tonic-gate 	na[0] = DEAD_MARKER;
1790Sstevel@tonic-gate 	na[1] = DEAD_MARKER;
1800Sstevel@tonic-gate 	na[2] = DEAD_MARKER;
1810Sstevel@tonic-gate }
1820Sstevel@tonic-gate 
1830Sstevel@tonic-gate struct uu_avl_node_compare_info {
1840Sstevel@tonic-gate 	uu_compare_fn_t	*ac_compare;
1850Sstevel@tonic-gate 	void		*ac_private;
1860Sstevel@tonic-gate 	void		*ac_right;
1870Sstevel@tonic-gate 	void		*ac_found;
1880Sstevel@tonic-gate };
1890Sstevel@tonic-gate 
1900Sstevel@tonic-gate static int
1910Sstevel@tonic-gate uu_avl_node_compare(const void *l, const void *r)
1920Sstevel@tonic-gate {
1930Sstevel@tonic-gate 	struct uu_avl_node_compare_info *info =
1940Sstevel@tonic-gate 	    (struct uu_avl_node_compare_info *)l;
1950Sstevel@tonic-gate 
1960Sstevel@tonic-gate 	int res = info->ac_compare(r, info->ac_right, info->ac_private);
1970Sstevel@tonic-gate 
1980Sstevel@tonic-gate 	if (res == 0) {
1990Sstevel@tonic-gate 		if (info->ac_found == NULL)
2000Sstevel@tonic-gate 			info->ac_found = (void *)r;
2010Sstevel@tonic-gate 		return (-1);
2020Sstevel@tonic-gate 	}
2030Sstevel@tonic-gate 	if (res < 0)
2040Sstevel@tonic-gate 		return (1);
2050Sstevel@tonic-gate 	return (-1);
2060Sstevel@tonic-gate }
2070Sstevel@tonic-gate 
2080Sstevel@tonic-gate uu_avl_t *
2090Sstevel@tonic-gate uu_avl_create(uu_avl_pool_t *pp, void *parent, uint32_t flags)
2100Sstevel@tonic-gate {
2110Sstevel@tonic-gate 	uu_avl_t *ap, *next, *prev;
2120Sstevel@tonic-gate 
213407Sjwadams 	if (flags & ~UU_AVL_DEBUG) {
2140Sstevel@tonic-gate 		uu_set_error(UU_ERROR_UNKNOWN_FLAG);
2150Sstevel@tonic-gate 		return (NULL);
2160Sstevel@tonic-gate 	}
2170Sstevel@tonic-gate 
2180Sstevel@tonic-gate 	ap = uu_zalloc(sizeof (*ap));
2190Sstevel@tonic-gate 	if (ap == NULL) {
2200Sstevel@tonic-gate 		uu_set_error(UU_ERROR_NO_MEMORY);
2210Sstevel@tonic-gate 		return (NULL);
2220Sstevel@tonic-gate 	}
2230Sstevel@tonic-gate 
2240Sstevel@tonic-gate 	ap->ua_pool = pp;
225407Sjwadams 	ap->ua_parent_enc = UU_PTR_ENCODE(parent);
2260Sstevel@tonic-gate 	ap->ua_debug = pp->uap_debug || (flags & UU_AVL_DEBUG);
2270Sstevel@tonic-gate 	ap->ua_index = (pp->uap_last_index = INDEX_NEXT(pp->uap_last_index));
2280Sstevel@tonic-gate 
2290Sstevel@tonic-gate 	avl_create(&ap->ua_tree, &uu_avl_node_compare, pp->uap_objsize,
2300Sstevel@tonic-gate 	    pp->uap_nodeoffset);
2310Sstevel@tonic-gate 
2320Sstevel@tonic-gate 	ap->ua_null_walk.uaw_next = &ap->ua_null_walk;
2330Sstevel@tonic-gate 	ap->ua_null_walk.uaw_prev = &ap->ua_null_walk;
2340Sstevel@tonic-gate 
2350Sstevel@tonic-gate 	(void) pthread_mutex_lock(&pp->uap_lock);
236407Sjwadams 	next = &pp->uap_null_avl;
237407Sjwadams 	prev = UU_PTR_DECODE(next->ua_prev_enc);
238407Sjwadams 	ap->ua_next_enc = UU_PTR_ENCODE(next);
239407Sjwadams 	ap->ua_prev_enc = UU_PTR_ENCODE(prev);
240407Sjwadams 	next->ua_prev_enc = UU_PTR_ENCODE(ap);
241407Sjwadams 	prev->ua_next_enc = UU_PTR_ENCODE(ap);
2420Sstevel@tonic-gate 	(void) pthread_mutex_unlock(&pp->uap_lock);
2430Sstevel@tonic-gate 
2440Sstevel@tonic-gate 	return (ap);
2450Sstevel@tonic-gate }
2460Sstevel@tonic-gate 
2470Sstevel@tonic-gate void
2480Sstevel@tonic-gate uu_avl_destroy(uu_avl_t *ap)
2490Sstevel@tonic-gate {
2500Sstevel@tonic-gate 	uu_avl_pool_t *pp = ap->ua_pool;
2510Sstevel@tonic-gate 
2520Sstevel@tonic-gate 	if (ap->ua_debug) {
2530Sstevel@tonic-gate 		if (avl_numnodes(&ap->ua_tree) != 0) {
2540Sstevel@tonic-gate 			uu_panic("uu_avl_destroy(%p): tree not empty\n", ap);
2550Sstevel@tonic-gate 		}
2560Sstevel@tonic-gate 		if (ap->ua_null_walk.uaw_next != &ap->ua_null_walk ||
2570Sstevel@tonic-gate 		    ap->ua_null_walk.uaw_prev != &ap->ua_null_walk) {
2580Sstevel@tonic-gate 			uu_panic("uu_avl_destroy(%p):  outstanding walkers\n",
2590Sstevel@tonic-gate 			    ap);
2600Sstevel@tonic-gate 		}
2610Sstevel@tonic-gate 	}
2620Sstevel@tonic-gate 	(void) pthread_mutex_lock(&pp->uap_lock);
263407Sjwadams 	UU_AVL_PTR(ap->ua_next_enc)->ua_prev_enc = ap->ua_prev_enc;
264407Sjwadams 	UU_AVL_PTR(ap->ua_prev_enc)->ua_next_enc = ap->ua_next_enc;
2650Sstevel@tonic-gate 	(void) pthread_mutex_unlock(&pp->uap_lock);
266407Sjwadams 	ap->ua_prev_enc = UU_PTR_ENCODE(NULL);
267407Sjwadams 	ap->ua_next_enc = UU_PTR_ENCODE(NULL);
2680Sstevel@tonic-gate 
2690Sstevel@tonic-gate 	ap->ua_pool = NULL;
2700Sstevel@tonic-gate 	avl_destroy(&ap->ua_tree);
2710Sstevel@tonic-gate 
2720Sstevel@tonic-gate 	uu_free(ap);
2730Sstevel@tonic-gate }
2740Sstevel@tonic-gate 
2750Sstevel@tonic-gate size_t
2760Sstevel@tonic-gate uu_avl_numnodes(uu_avl_t *ap)
2770Sstevel@tonic-gate {
2780Sstevel@tonic-gate 	return (avl_numnodes(&ap->ua_tree));
2790Sstevel@tonic-gate }
2800Sstevel@tonic-gate 
2810Sstevel@tonic-gate void *
2820Sstevel@tonic-gate uu_avl_first(uu_avl_t *ap)
2830Sstevel@tonic-gate {
2840Sstevel@tonic-gate 	return (avl_first(&ap->ua_tree));
2850Sstevel@tonic-gate }
2860Sstevel@tonic-gate 
2870Sstevel@tonic-gate void *
2880Sstevel@tonic-gate uu_avl_last(uu_avl_t *ap)
2890Sstevel@tonic-gate {
2900Sstevel@tonic-gate 	return (avl_last(&ap->ua_tree));
2910Sstevel@tonic-gate }
2920Sstevel@tonic-gate 
2930Sstevel@tonic-gate void *
2940Sstevel@tonic-gate uu_avl_next(uu_avl_t *ap, void *node)
2950Sstevel@tonic-gate {
2960Sstevel@tonic-gate 	return (AVL_NEXT(&ap->ua_tree, node));
2970Sstevel@tonic-gate }
2980Sstevel@tonic-gate 
2990Sstevel@tonic-gate void *
3000Sstevel@tonic-gate uu_avl_prev(uu_avl_t *ap, void *node)
3010Sstevel@tonic-gate {
3020Sstevel@tonic-gate 	return (AVL_PREV(&ap->ua_tree, node));
3030Sstevel@tonic-gate }
3040Sstevel@tonic-gate 
3050Sstevel@tonic-gate static void
3060Sstevel@tonic-gate _avl_walk_init(uu_avl_walk_t *wp, uu_avl_t *ap, uint32_t flags)
3070Sstevel@tonic-gate {
3080Sstevel@tonic-gate 	uu_avl_walk_t *next, *prev;
3090Sstevel@tonic-gate 
3100Sstevel@tonic-gate 	int robust = (flags & UU_WALK_ROBUST);
3110Sstevel@tonic-gate 	int direction = (flags & UU_WALK_REVERSE)? -1 : 1;
3120Sstevel@tonic-gate 
3130Sstevel@tonic-gate 	(void) memset(wp, 0, sizeof (*wp));
3140Sstevel@tonic-gate 	wp->uaw_avl = ap;
3150Sstevel@tonic-gate 	wp->uaw_robust = robust;
3160Sstevel@tonic-gate 	wp->uaw_dir = direction;
3170Sstevel@tonic-gate 
3180Sstevel@tonic-gate 	if (direction > 0)
3190Sstevel@tonic-gate 		wp->uaw_next_result = avl_first(&ap->ua_tree);
3200Sstevel@tonic-gate 	else
3210Sstevel@tonic-gate 		wp->uaw_next_result = avl_last(&ap->ua_tree);
3220Sstevel@tonic-gate 
3230Sstevel@tonic-gate 	if (ap->ua_debug || robust) {
3240Sstevel@tonic-gate 		wp->uaw_next = next = &ap->ua_null_walk;
3250Sstevel@tonic-gate 		wp->uaw_prev = prev = next->uaw_prev;
3260Sstevel@tonic-gate 		next->uaw_prev = wp;
3270Sstevel@tonic-gate 		prev->uaw_next = wp;
3280Sstevel@tonic-gate 	}
3290Sstevel@tonic-gate }
3300Sstevel@tonic-gate 
3310Sstevel@tonic-gate static void *
3320Sstevel@tonic-gate _avl_walk_advance(uu_avl_walk_t *wp, uu_avl_t *ap)
3330Sstevel@tonic-gate {
3340Sstevel@tonic-gate 	void *np = wp->uaw_next_result;
3350Sstevel@tonic-gate 
3360Sstevel@tonic-gate 	avl_tree_t *t = &ap->ua_tree;
3370Sstevel@tonic-gate 
3380Sstevel@tonic-gate 	if (np == NULL)
3390Sstevel@tonic-gate 		return (NULL);
3400Sstevel@tonic-gate 
3410Sstevel@tonic-gate 	wp->uaw_next_result = (wp->uaw_dir > 0)? AVL_NEXT(t, np) :
3420Sstevel@tonic-gate 	    AVL_PREV(t, np);
3430Sstevel@tonic-gate 
3440Sstevel@tonic-gate 	return (np);
3450Sstevel@tonic-gate }
3460Sstevel@tonic-gate 
3470Sstevel@tonic-gate static void
3480Sstevel@tonic-gate _avl_walk_fini(uu_avl_walk_t *wp)
3490Sstevel@tonic-gate {
3500Sstevel@tonic-gate 	if (wp->uaw_next != NULL) {
3510Sstevel@tonic-gate 		wp->uaw_next->uaw_prev = wp->uaw_prev;
3520Sstevel@tonic-gate 		wp->uaw_prev->uaw_next = wp->uaw_next;
3530Sstevel@tonic-gate 		wp->uaw_next = NULL;
3540Sstevel@tonic-gate 		wp->uaw_prev = NULL;
3550Sstevel@tonic-gate 	}
3560Sstevel@tonic-gate 	wp->uaw_avl = NULL;
3570Sstevel@tonic-gate 	wp->uaw_next_result = NULL;
3580Sstevel@tonic-gate }
3590Sstevel@tonic-gate 
3600Sstevel@tonic-gate uu_avl_walk_t *
3610Sstevel@tonic-gate uu_avl_walk_start(uu_avl_t *ap, uint32_t flags)
3620Sstevel@tonic-gate {
3630Sstevel@tonic-gate 	uu_avl_walk_t *wp;
3640Sstevel@tonic-gate 
3650Sstevel@tonic-gate 	if (flags & ~(UU_WALK_ROBUST | UU_WALK_REVERSE)) {
3660Sstevel@tonic-gate 		uu_set_error(UU_ERROR_UNKNOWN_FLAG);
3670Sstevel@tonic-gate 		return (NULL);
3680Sstevel@tonic-gate 	}
3690Sstevel@tonic-gate 
3700Sstevel@tonic-gate 	wp = uu_zalloc(sizeof (*wp));
3710Sstevel@tonic-gate 	if (wp == NULL) {
3720Sstevel@tonic-gate 		uu_set_error(UU_ERROR_NO_MEMORY);
3730Sstevel@tonic-gate 		return (NULL);
3740Sstevel@tonic-gate 	}
3750Sstevel@tonic-gate 
3760Sstevel@tonic-gate 	_avl_walk_init(wp, ap, flags);
3770Sstevel@tonic-gate 	return (wp);
3780Sstevel@tonic-gate }
3790Sstevel@tonic-gate 
3800Sstevel@tonic-gate void *
3810Sstevel@tonic-gate uu_avl_walk_next(uu_avl_walk_t *wp)
3820Sstevel@tonic-gate {
3830Sstevel@tonic-gate 	return (_avl_walk_advance(wp, wp->uaw_avl));
3840Sstevel@tonic-gate }
3850Sstevel@tonic-gate 
3860Sstevel@tonic-gate void
3870Sstevel@tonic-gate uu_avl_walk_end(uu_avl_walk_t *wp)
3880Sstevel@tonic-gate {
3890Sstevel@tonic-gate 	_avl_walk_fini(wp);
3900Sstevel@tonic-gate 	uu_free(wp);
3910Sstevel@tonic-gate }
3920Sstevel@tonic-gate 
3930Sstevel@tonic-gate int
3940Sstevel@tonic-gate uu_avl_walk(uu_avl_t *ap, uu_walk_fn_t *func, void *private, uint32_t flags)
3950Sstevel@tonic-gate {
3960Sstevel@tonic-gate 	void *e;
3970Sstevel@tonic-gate 	uu_avl_walk_t my_walk;
3980Sstevel@tonic-gate 
3990Sstevel@tonic-gate 	int status = UU_WALK_NEXT;
4000Sstevel@tonic-gate 
4010Sstevel@tonic-gate 	if (flags & ~(UU_WALK_ROBUST | UU_WALK_REVERSE)) {
4020Sstevel@tonic-gate 		uu_set_error(UU_ERROR_UNKNOWN_FLAG);
4030Sstevel@tonic-gate 		return (-1);
4040Sstevel@tonic-gate 	}
4050Sstevel@tonic-gate 
4060Sstevel@tonic-gate 	_avl_walk_init(&my_walk, ap, flags);
4070Sstevel@tonic-gate 	while (status == UU_WALK_NEXT &&
4080Sstevel@tonic-gate 	    (e = _avl_walk_advance(&my_walk, ap)) != NULL)
4090Sstevel@tonic-gate 		status = (*func)(e, private);
4100Sstevel@tonic-gate 	_avl_walk_fini(&my_walk);
4110Sstevel@tonic-gate 
4120Sstevel@tonic-gate 	if (status >= 0)
4130Sstevel@tonic-gate 		return (0);
4140Sstevel@tonic-gate 	uu_set_error(UU_ERROR_CALLBACK_FAILED);
4150Sstevel@tonic-gate 	return (-1);
4160Sstevel@tonic-gate }
4170Sstevel@tonic-gate 
4180Sstevel@tonic-gate void
4190Sstevel@tonic-gate uu_avl_remove(uu_avl_t *ap, void *elem)
4200Sstevel@tonic-gate {
4210Sstevel@tonic-gate 	uu_avl_walk_t *wp;
4220Sstevel@tonic-gate 	uu_avl_pool_t *pp = ap->ua_pool;
4230Sstevel@tonic-gate 	uintptr_t *na = NODE_ARRAY(pp, elem);
4240Sstevel@tonic-gate 
4250Sstevel@tonic-gate 	if (ap->ua_debug) {
4260Sstevel@tonic-gate 		/*
4270Sstevel@tonic-gate 		 * invalidate outstanding uu_avl_index_ts.
4280Sstevel@tonic-gate 		 */
4290Sstevel@tonic-gate 		ap->ua_index = INDEX_NEXT(ap->ua_index);
4300Sstevel@tonic-gate 	}
4310Sstevel@tonic-gate 
4320Sstevel@tonic-gate 	/*
4330Sstevel@tonic-gate 	 * Robust walkers most be advanced, if we are removing the node
4340Sstevel@tonic-gate 	 * they are currently using.  In debug mode, non-robust walkers
4350Sstevel@tonic-gate 	 * are also on the walker list.
4360Sstevel@tonic-gate 	 */
4370Sstevel@tonic-gate 	for (wp = ap->ua_null_walk.uaw_next; wp != &ap->ua_null_walk;
4380Sstevel@tonic-gate 	    wp = wp->uaw_next) {
4390Sstevel@tonic-gate 		if (wp->uaw_robust) {
4400Sstevel@tonic-gate 			if (elem == wp->uaw_next_result)
4410Sstevel@tonic-gate 				(void) _avl_walk_advance(wp, ap);
4420Sstevel@tonic-gate 		} else if (wp->uaw_next_result != NULL) {
4430Sstevel@tonic-gate 			uu_panic("uu_avl_remove(%p, %p): active non-robust "
4440Sstevel@tonic-gate 			    "walker\n", ap, elem);
4450Sstevel@tonic-gate 		}
4460Sstevel@tonic-gate 	}
4470Sstevel@tonic-gate 
4480Sstevel@tonic-gate 	avl_remove(&ap->ua_tree, elem);
4490Sstevel@tonic-gate 
4500Sstevel@tonic-gate 	na[0] = POOL_TO_MARKER(pp);
451*2856Snd150628 	na[1] = 0;
4520Sstevel@tonic-gate }
4530Sstevel@tonic-gate 
4540Sstevel@tonic-gate void *
4550Sstevel@tonic-gate uu_avl_teardown(uu_avl_t *ap, void **cookie)
4560Sstevel@tonic-gate {
457407Sjwadams 	void *elem = avl_destroy_nodes(&ap->ua_tree, cookie);
458407Sjwadams 
459407Sjwadams 	if (elem != NULL) {
460407Sjwadams 		uu_avl_pool_t *pp = ap->ua_pool;
461407Sjwadams 		uintptr_t *na = NODE_ARRAY(pp, elem);
462407Sjwadams 
463407Sjwadams 		na[0] = POOL_TO_MARKER(pp);
464*2856Snd150628 		na[1] = 0;
465407Sjwadams 	}
466407Sjwadams 	return (elem);
4670Sstevel@tonic-gate }
4680Sstevel@tonic-gate 
4690Sstevel@tonic-gate void *
4700Sstevel@tonic-gate uu_avl_find(uu_avl_t *ap, void *elem, void *private, uu_avl_index_t *out)
4710Sstevel@tonic-gate {
4720Sstevel@tonic-gate 	struct uu_avl_node_compare_info info;
4730Sstevel@tonic-gate 	void *result;
4740Sstevel@tonic-gate 
4750Sstevel@tonic-gate 	info.ac_compare = ap->ua_pool->uap_cmp;
4760Sstevel@tonic-gate 	info.ac_private = private;
4770Sstevel@tonic-gate 	info.ac_right = elem;
4780Sstevel@tonic-gate 	info.ac_found = NULL;
4790Sstevel@tonic-gate 
4800Sstevel@tonic-gate 	result = avl_find(&ap->ua_tree, &info, out);
4810Sstevel@tonic-gate 	if (out != NULL)
4820Sstevel@tonic-gate 		*out = INDEX_ENCODE(ap, *out);
4830Sstevel@tonic-gate 
4840Sstevel@tonic-gate 	if (ap->ua_debug && result != NULL)
4850Sstevel@tonic-gate 		uu_panic("uu_avl_find: internal error: avl_find succeeded\n");
4860Sstevel@tonic-gate 
4870Sstevel@tonic-gate 	return (info.ac_found);
4880Sstevel@tonic-gate }
4890Sstevel@tonic-gate 
4900Sstevel@tonic-gate void
4910Sstevel@tonic-gate uu_avl_insert(uu_avl_t *ap, void *elem, uu_avl_index_t idx)
4920Sstevel@tonic-gate {
4930Sstevel@tonic-gate 	if (ap->ua_debug) {
4940Sstevel@tonic-gate 		uu_avl_pool_t *pp = ap->ua_pool;
4950Sstevel@tonic-gate 		uintptr_t *na = NODE_ARRAY(pp, elem);
4960Sstevel@tonic-gate 
497*2856Snd150628 		if (na[1] != 0)
4980Sstevel@tonic-gate 			uu_panic("uu_avl_insert(%p, %p, %p): node already "
4990Sstevel@tonic-gate 			    "in tree, or corrupt\n",
5000Sstevel@tonic-gate 			    ap, elem, idx);
501*2856Snd150628 		if (na[0] == 0)
5020Sstevel@tonic-gate 			uu_panic("uu_avl_insert(%p, %p, %p): node not "
5030Sstevel@tonic-gate 			    "initialized\n",
5040Sstevel@tonic-gate 			    ap, elem, idx);
5050Sstevel@tonic-gate 		if (na[0] != POOL_TO_MARKER(pp))
5060Sstevel@tonic-gate 			uu_panic("uu_avl_insert(%p, %p, %p): node from "
5070Sstevel@tonic-gate 			    "other pool, or corrupt\n",
5080Sstevel@tonic-gate 			    ap, elem, idx);
5090Sstevel@tonic-gate 
5100Sstevel@tonic-gate 		if (!INDEX_VALID(ap, idx))
5110Sstevel@tonic-gate 			uu_panic("uu_avl_insert(%p, %p, %p): %s\n",
5120Sstevel@tonic-gate 			    ap, elem, idx,
5130Sstevel@tonic-gate 			    INDEX_CHECK(idx)? "outdated index" :
5140Sstevel@tonic-gate 			    "invalid index");
5150Sstevel@tonic-gate 
5160Sstevel@tonic-gate 		/*
5170Sstevel@tonic-gate 		 * invalidate outstanding uu_avl_index_ts.
5180Sstevel@tonic-gate 		 */
5190Sstevel@tonic-gate 		ap->ua_index = INDEX_NEXT(ap->ua_index);
5200Sstevel@tonic-gate 	}
5210Sstevel@tonic-gate 	avl_insert(&ap->ua_tree, elem, INDEX_DECODE(idx));
5220Sstevel@tonic-gate }
5230Sstevel@tonic-gate 
5240Sstevel@tonic-gate void *
5250Sstevel@tonic-gate uu_avl_nearest_next(uu_avl_t *ap, uu_avl_index_t idx)
5260Sstevel@tonic-gate {
5270Sstevel@tonic-gate 	if (ap->ua_debug && !INDEX_VALID(ap, idx))
5280Sstevel@tonic-gate 		uu_panic("uu_avl_nearest_next(%p, %p): %s\n",
5290Sstevel@tonic-gate 		    ap, idx, INDEX_CHECK(idx)? "outdated index" :
5300Sstevel@tonic-gate 		    "invalid index");
5310Sstevel@tonic-gate 	return (avl_nearest(&ap->ua_tree, INDEX_DECODE(idx), AVL_AFTER));
5320Sstevel@tonic-gate }
5330Sstevel@tonic-gate 
5340Sstevel@tonic-gate void *
5350Sstevel@tonic-gate uu_avl_nearest_prev(uu_avl_t *ap, uu_avl_index_t idx)
5360Sstevel@tonic-gate {
5370Sstevel@tonic-gate 	if (ap->ua_debug && !INDEX_VALID(ap, idx))
5380Sstevel@tonic-gate 		uu_panic("uu_avl_nearest_prev(%p, %p): %s\n",
5390Sstevel@tonic-gate 		    ap, idx, INDEX_CHECK(idx)? "outdated index" :
5400Sstevel@tonic-gate 		    "invalid index");
5410Sstevel@tonic-gate 	return (avl_nearest(&ap->ua_tree, INDEX_DECODE(idx), AVL_BEFORE));
5420Sstevel@tonic-gate }
5430Sstevel@tonic-gate 
5440Sstevel@tonic-gate /*
5450Sstevel@tonic-gate  * called from uu_lockup() and uu_release(), as part of our fork1()-safety.
5460Sstevel@tonic-gate  */
5470Sstevel@tonic-gate void
5480Sstevel@tonic-gate uu_avl_lockup(void)
5490Sstevel@tonic-gate {
5500Sstevel@tonic-gate 	uu_avl_pool_t *pp;
5510Sstevel@tonic-gate 
5520Sstevel@tonic-gate 	(void) pthread_mutex_lock(&uu_apool_list_lock);
5530Sstevel@tonic-gate 	for (pp = uu_null_apool.uap_next; pp != &uu_null_apool;
5540Sstevel@tonic-gate 	    pp = pp->uap_next)
5550Sstevel@tonic-gate 		(void) pthread_mutex_lock(&pp->uap_lock);
5560Sstevel@tonic-gate }
5570Sstevel@tonic-gate 
5580Sstevel@tonic-gate void
5590Sstevel@tonic-gate uu_avl_release(void)
5600Sstevel@tonic-gate {
5610Sstevel@tonic-gate 	uu_avl_pool_t *pp;
5620Sstevel@tonic-gate 
5630Sstevel@tonic-gate 	for (pp = uu_null_apool.uap_next; pp != &uu_null_apool;
5640Sstevel@tonic-gate 	    pp = pp->uap_next)
5650Sstevel@tonic-gate 		(void) pthread_mutex_unlock(&pp->uap_lock);
5660Sstevel@tonic-gate 	(void) pthread_mutex_unlock(&uu_apool_list_lock);
5670Sstevel@tonic-gate }
568