xref: /onnv-gate/usr/src/lib/libuutil/common/uu_avl.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24*0Sstevel@tonic-gate  * Use is subject to license terms.
25*0Sstevel@tonic-gate  */
26*0Sstevel@tonic-gate 
27*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*0Sstevel@tonic-gate 
29*0Sstevel@tonic-gate #include "libuutil_common.h"
30*0Sstevel@tonic-gate 
31*0Sstevel@tonic-gate #include <stdlib.h>
32*0Sstevel@tonic-gate #include <string.h>
33*0Sstevel@tonic-gate #include <unistd.h>
34*0Sstevel@tonic-gate #include <sys/avl.h>
35*0Sstevel@tonic-gate 
36*0Sstevel@tonic-gate static uu_avl_pool_t	uu_null_apool = { &uu_null_apool, &uu_null_apool };
37*0Sstevel@tonic-gate static pthread_mutex_t	uu_apool_list_lock = PTHREAD_MUTEX_INITIALIZER;
38*0Sstevel@tonic-gate 
39*0Sstevel@tonic-gate /*
40*0Sstevel@tonic-gate  * The index mark change on every insert and delete, to catch stale
41*0Sstevel@tonic-gate  * references.
42*0Sstevel@tonic-gate  *
43*0Sstevel@tonic-gate  * We leave the low bit alone, since the avl code uses it.
44*0Sstevel@tonic-gate  */
45*0Sstevel@tonic-gate #define	INDEX_MAX		(sizeof (uintptr_t) - 2)
46*0Sstevel@tonic-gate #define	INDEX_NEXT(m)		(((m) == INDEX_MAX)? 2 : ((m) + 2) & INDEX_MAX)
47*0Sstevel@tonic-gate 
48*0Sstevel@tonic-gate #define	INDEX_DECODE(i)		((i) & ~INDEX_MAX)
49*0Sstevel@tonic-gate #define	INDEX_ENCODE(p, n)	(((n) & ~INDEX_MAX) | (p)->ua_index)
50*0Sstevel@tonic-gate #define	INDEX_VALID(p, i)	(((i) & INDEX_MAX) == (p)->ua_index)
51*0Sstevel@tonic-gate #define	INDEX_CHECK(i)		(((i) & INDEX_MAX) != 0)
52*0Sstevel@tonic-gate 
53*0Sstevel@tonic-gate /*
54*0Sstevel@tonic-gate  * When an element is inactive (not in a tree), we keep a marked pointer to
55*0Sstevel@tonic-gate  * its containing pool in its first word, and a NULL pointer in its second.
56*0Sstevel@tonic-gate  *
57*0Sstevel@tonic-gate  * On insert, we use these to verify that it comes from the correct pool.
58*0Sstevel@tonic-gate  */
59*0Sstevel@tonic-gate #define	NODE_ARRAY(p, n)	((uintptr_t *)((uintptr_t)(n) + \
60*0Sstevel@tonic-gate 				    (pp)->uap_nodeoffset))
61*0Sstevel@tonic-gate 
62*0Sstevel@tonic-gate #define	POOL_TO_MARKER(pp) (((uintptr_t)(pp) | 1))
63*0Sstevel@tonic-gate 
64*0Sstevel@tonic-gate #define	DEAD_MARKER		0xc4
65*0Sstevel@tonic-gate 
66*0Sstevel@tonic-gate uu_avl_pool_t *
67*0Sstevel@tonic-gate uu_avl_pool_create(const char *name, size_t objsize, size_t nodeoffset,
68*0Sstevel@tonic-gate     uu_compare_fn_t *compare_func, uint32_t flags)
69*0Sstevel@tonic-gate {
70*0Sstevel@tonic-gate 	uu_avl_pool_t *pp, *next, *prev;
71*0Sstevel@tonic-gate 
72*0Sstevel@tonic-gate 	if (name == NULL ||
73*0Sstevel@tonic-gate 	    uu_check_name(name, UU_NAME_DOMAIN) == -1 ||
74*0Sstevel@tonic-gate 	    nodeoffset + sizeof (uu_avl_node_t) > objsize ||
75*0Sstevel@tonic-gate 	    compare_func == NULL) {
76*0Sstevel@tonic-gate 		uu_set_error(UU_ERROR_INVALID_ARGUMENT);
77*0Sstevel@tonic-gate 		return (NULL);
78*0Sstevel@tonic-gate 	}
79*0Sstevel@tonic-gate 
80*0Sstevel@tonic-gate 	if (flags & ~UU_AVL_POOL_DEBUG) {
81*0Sstevel@tonic-gate 		uu_set_error(UU_ERROR_UNKNOWN_FLAG);
82*0Sstevel@tonic-gate 		return (NULL);
83*0Sstevel@tonic-gate 	}
84*0Sstevel@tonic-gate 
85*0Sstevel@tonic-gate 	pp = uu_zalloc(sizeof (uu_avl_pool_t));
86*0Sstevel@tonic-gate 	if (pp == NULL) {
87*0Sstevel@tonic-gate 		uu_set_error(UU_ERROR_NO_MEMORY);
88*0Sstevel@tonic-gate 		return (NULL);
89*0Sstevel@tonic-gate 	}
90*0Sstevel@tonic-gate 
91*0Sstevel@tonic-gate 	(void) strlcpy(pp->uap_name, name, sizeof (pp->uap_name));
92*0Sstevel@tonic-gate 	pp->uap_nodeoffset = nodeoffset;
93*0Sstevel@tonic-gate 	pp->uap_objsize = objsize;
94*0Sstevel@tonic-gate 	pp->uap_cmp = compare_func;
95*0Sstevel@tonic-gate 	if (flags & UU_AVL_POOL_DEBUG)
96*0Sstevel@tonic-gate 		pp->uap_debug = 1;
97*0Sstevel@tonic-gate 	pp->uap_last_index = 0;
98*0Sstevel@tonic-gate 
99*0Sstevel@tonic-gate 	(void) pthread_mutex_init(&pp->uap_lock, NULL);
100*0Sstevel@tonic-gate 
101*0Sstevel@tonic-gate 	pp->uap_null_avl.ua_next = &pp->uap_null_avl;
102*0Sstevel@tonic-gate 	pp->uap_null_avl.ua_prev = &pp->uap_null_avl;
103*0Sstevel@tonic-gate 
104*0Sstevel@tonic-gate 	(void) pthread_mutex_lock(&uu_apool_list_lock);
105*0Sstevel@tonic-gate 	pp->uap_next = next = &uu_null_apool;
106*0Sstevel@tonic-gate 	pp->uap_prev = prev = next->uap_prev;
107*0Sstevel@tonic-gate 	next->uap_prev = pp;
108*0Sstevel@tonic-gate 	prev->uap_next = pp;
109*0Sstevel@tonic-gate 	(void) pthread_mutex_unlock(&uu_apool_list_lock);
110*0Sstevel@tonic-gate 
111*0Sstevel@tonic-gate 	return (pp);
112*0Sstevel@tonic-gate }
113*0Sstevel@tonic-gate 
114*0Sstevel@tonic-gate void
115*0Sstevel@tonic-gate uu_avl_pool_destroy(uu_avl_pool_t *pp)
116*0Sstevel@tonic-gate {
117*0Sstevel@tonic-gate 	if (pp->uap_debug) {
118*0Sstevel@tonic-gate 		if (pp->uap_null_avl.ua_next != &pp->uap_null_avl ||
119*0Sstevel@tonic-gate 		    pp->uap_null_avl.ua_prev != &pp->uap_null_avl) {
120*0Sstevel@tonic-gate 			uu_panic("uu_avl_pool_destroy: Pool \"%.*s\" (%p) has "
121*0Sstevel@tonic-gate 			    "outstanding avls, or is corrupt.\n",
122*0Sstevel@tonic-gate 			    sizeof (pp->uap_name), pp->uap_name, pp);
123*0Sstevel@tonic-gate 		}
124*0Sstevel@tonic-gate 	}
125*0Sstevel@tonic-gate 	(void) pthread_mutex_lock(&uu_apool_list_lock);
126*0Sstevel@tonic-gate 	pp->uap_next->uap_prev = pp->uap_prev;
127*0Sstevel@tonic-gate 	pp->uap_prev->uap_next = pp->uap_next;
128*0Sstevel@tonic-gate 	(void) pthread_mutex_unlock(&uu_apool_list_lock);
129*0Sstevel@tonic-gate 	pp->uap_prev = NULL;
130*0Sstevel@tonic-gate 	pp->uap_next = NULL;
131*0Sstevel@tonic-gate 	uu_free(pp);
132*0Sstevel@tonic-gate }
133*0Sstevel@tonic-gate 
134*0Sstevel@tonic-gate void
135*0Sstevel@tonic-gate uu_avl_node_init(void *base, uu_avl_node_t *np, uu_avl_pool_t *pp)
136*0Sstevel@tonic-gate {
137*0Sstevel@tonic-gate 	uintptr_t *na = (uintptr_t *)np;
138*0Sstevel@tonic-gate 
139*0Sstevel@tonic-gate 	if (pp->uap_debug) {
140*0Sstevel@tonic-gate 		uintptr_t offset = (uintptr_t)np - (uintptr_t)base;
141*0Sstevel@tonic-gate 		if (offset + sizeof (*np) > pp->uap_objsize) {
142*0Sstevel@tonic-gate 			uu_panic("uu_avl_node_init(%p, %p, %p (\"%s\")): "
143*0Sstevel@tonic-gate 			    "offset %ld doesn't fit in object (size %ld)\n",
144*0Sstevel@tonic-gate 			    base, np, pp, pp->uap_name, offset,
145*0Sstevel@tonic-gate 			    pp->uap_objsize);
146*0Sstevel@tonic-gate 		}
147*0Sstevel@tonic-gate 		if (offset != pp->uap_nodeoffset) {
148*0Sstevel@tonic-gate 			uu_panic("uu_avl_node_init(%p, %p, %p (\"%s\")): "
149*0Sstevel@tonic-gate 			    "offset %ld doesn't match pool's offset (%ld)\n",
150*0Sstevel@tonic-gate 			    base, np, pp, pp->uap_name, offset,
151*0Sstevel@tonic-gate 			    pp->uap_objsize);
152*0Sstevel@tonic-gate 		}
153*0Sstevel@tonic-gate 	}
154*0Sstevel@tonic-gate 
155*0Sstevel@tonic-gate 	na[0] = POOL_TO_MARKER(pp);
156*0Sstevel@tonic-gate 	na[1] = NULL;
157*0Sstevel@tonic-gate }
158*0Sstevel@tonic-gate 
159*0Sstevel@tonic-gate void
160*0Sstevel@tonic-gate uu_avl_node_fini(void *base, uu_avl_node_t *np, uu_avl_pool_t *pp)
161*0Sstevel@tonic-gate {
162*0Sstevel@tonic-gate 	uintptr_t *na = (uintptr_t *)np;
163*0Sstevel@tonic-gate 
164*0Sstevel@tonic-gate 	if (pp->uap_debug) {
165*0Sstevel@tonic-gate 		if (na[0] == DEAD_MARKER && na[1] == DEAD_MARKER) {
166*0Sstevel@tonic-gate 			uu_panic("uu_avl_node_fini(%p, %p, %p (\"%s\")): "
167*0Sstevel@tonic-gate 			    "node already finied\n",
168*0Sstevel@tonic-gate 			    base, np, pp, pp->uap_name);
169*0Sstevel@tonic-gate 		}
170*0Sstevel@tonic-gate 		if (na[0] != POOL_TO_MARKER(pp) || na[1] != NULL) {
171*0Sstevel@tonic-gate 			uu_panic("uu_avl_node_fini(%p, %p, %p (\"%s\")): "
172*0Sstevel@tonic-gate 			    "node corrupt, in tree, or in different pool\n",
173*0Sstevel@tonic-gate 			    base, np, pp, pp->uap_name);
174*0Sstevel@tonic-gate 		}
175*0Sstevel@tonic-gate 	}
176*0Sstevel@tonic-gate 
177*0Sstevel@tonic-gate 	na[0] = DEAD_MARKER;
178*0Sstevel@tonic-gate 	na[1] = DEAD_MARKER;
179*0Sstevel@tonic-gate 	na[2] = DEAD_MARKER;
180*0Sstevel@tonic-gate }
181*0Sstevel@tonic-gate 
182*0Sstevel@tonic-gate struct uu_avl_node_compare_info {
183*0Sstevel@tonic-gate 	uu_compare_fn_t	*ac_compare;
184*0Sstevel@tonic-gate 	void		*ac_private;
185*0Sstevel@tonic-gate 	void		*ac_right;
186*0Sstevel@tonic-gate 	void		*ac_found;
187*0Sstevel@tonic-gate };
188*0Sstevel@tonic-gate 
189*0Sstevel@tonic-gate static int
190*0Sstevel@tonic-gate uu_avl_node_compare(const void *l, const void *r)
191*0Sstevel@tonic-gate {
192*0Sstevel@tonic-gate 	struct uu_avl_node_compare_info *info =
193*0Sstevel@tonic-gate 	    (struct uu_avl_node_compare_info *)l;
194*0Sstevel@tonic-gate 
195*0Sstevel@tonic-gate 	int res = info->ac_compare(r, info->ac_right, info->ac_private);
196*0Sstevel@tonic-gate 
197*0Sstevel@tonic-gate 	if (res == 0) {
198*0Sstevel@tonic-gate 		if (info->ac_found == NULL)
199*0Sstevel@tonic-gate 			info->ac_found = (void *)r;
200*0Sstevel@tonic-gate 		return (-1);
201*0Sstevel@tonic-gate 	}
202*0Sstevel@tonic-gate 	if (res < 0)
203*0Sstevel@tonic-gate 		return (1);
204*0Sstevel@tonic-gate 	return (-1);
205*0Sstevel@tonic-gate }
206*0Sstevel@tonic-gate 
207*0Sstevel@tonic-gate uu_avl_t *
208*0Sstevel@tonic-gate uu_avl_create(uu_avl_pool_t *pp, void *parent, uint32_t flags)
209*0Sstevel@tonic-gate {
210*0Sstevel@tonic-gate 	uu_avl_t *ap, *next, *prev;
211*0Sstevel@tonic-gate 
212*0Sstevel@tonic-gate 	if (flags & UU_AVL_DEBUG) {
213*0Sstevel@tonic-gate 		uu_set_error(UU_ERROR_UNKNOWN_FLAG);
214*0Sstevel@tonic-gate 		return (NULL);
215*0Sstevel@tonic-gate 	}
216*0Sstevel@tonic-gate 
217*0Sstevel@tonic-gate 	ap = uu_zalloc(sizeof (*ap));
218*0Sstevel@tonic-gate 	if (ap == NULL) {
219*0Sstevel@tonic-gate 		uu_set_error(UU_ERROR_NO_MEMORY);
220*0Sstevel@tonic-gate 		return (NULL);
221*0Sstevel@tonic-gate 	}
222*0Sstevel@tonic-gate 
223*0Sstevel@tonic-gate 	ap->ua_pool = pp;
224*0Sstevel@tonic-gate 	ap->ua_parent = parent;
225*0Sstevel@tonic-gate 	ap->ua_debug = pp->uap_debug || (flags & UU_AVL_DEBUG);
226*0Sstevel@tonic-gate 	ap->ua_index = (pp->uap_last_index = INDEX_NEXT(pp->uap_last_index));
227*0Sstevel@tonic-gate 
228*0Sstevel@tonic-gate 	avl_create(&ap->ua_tree, &uu_avl_node_compare, pp->uap_objsize,
229*0Sstevel@tonic-gate 	    pp->uap_nodeoffset);
230*0Sstevel@tonic-gate 
231*0Sstevel@tonic-gate 	ap->ua_null_walk.uaw_next = &ap->ua_null_walk;
232*0Sstevel@tonic-gate 	ap->ua_null_walk.uaw_prev = &ap->ua_null_walk;
233*0Sstevel@tonic-gate 
234*0Sstevel@tonic-gate 	(void) pthread_mutex_lock(&pp->uap_lock);
235*0Sstevel@tonic-gate 	ap->ua_next = next = &pp->uap_null_avl;
236*0Sstevel@tonic-gate 	ap->ua_prev = prev = next->ua_prev;
237*0Sstevel@tonic-gate 	next->ua_prev = ap;
238*0Sstevel@tonic-gate 	prev->ua_next = ap;
239*0Sstevel@tonic-gate 	(void) pthread_mutex_unlock(&pp->uap_lock);
240*0Sstevel@tonic-gate 
241*0Sstevel@tonic-gate 	return (ap);
242*0Sstevel@tonic-gate }
243*0Sstevel@tonic-gate 
244*0Sstevel@tonic-gate void
245*0Sstevel@tonic-gate uu_avl_destroy(uu_avl_t *ap)
246*0Sstevel@tonic-gate {
247*0Sstevel@tonic-gate 	uu_avl_pool_t *pp = ap->ua_pool;
248*0Sstevel@tonic-gate 
249*0Sstevel@tonic-gate 	if (ap->ua_debug) {
250*0Sstevel@tonic-gate 		if (avl_numnodes(&ap->ua_tree) != 0) {
251*0Sstevel@tonic-gate 			uu_panic("uu_avl_destroy(%p): tree not empty\n", ap);
252*0Sstevel@tonic-gate 		}
253*0Sstevel@tonic-gate 		if (ap->ua_null_walk.uaw_next != &ap->ua_null_walk ||
254*0Sstevel@tonic-gate 		    ap->ua_null_walk.uaw_prev != &ap->ua_null_walk) {
255*0Sstevel@tonic-gate 			uu_panic("uu_avl_destroy(%p):  outstanding walkers\n",
256*0Sstevel@tonic-gate 			    ap);
257*0Sstevel@tonic-gate 		}
258*0Sstevel@tonic-gate 	}
259*0Sstevel@tonic-gate 	(void) pthread_mutex_lock(&pp->uap_lock);
260*0Sstevel@tonic-gate 	ap->ua_next->ua_prev = ap->ua_prev;
261*0Sstevel@tonic-gate 	ap->ua_prev->ua_next = ap->ua_next;
262*0Sstevel@tonic-gate 	(void) pthread_mutex_unlock(&pp->uap_lock);
263*0Sstevel@tonic-gate 	ap->ua_prev = NULL;
264*0Sstevel@tonic-gate 	ap->ua_next = NULL;
265*0Sstevel@tonic-gate 
266*0Sstevel@tonic-gate 	ap->ua_pool = NULL;
267*0Sstevel@tonic-gate 	avl_destroy(&ap->ua_tree);
268*0Sstevel@tonic-gate 
269*0Sstevel@tonic-gate 	uu_free(ap);
270*0Sstevel@tonic-gate }
271*0Sstevel@tonic-gate 
272*0Sstevel@tonic-gate size_t
273*0Sstevel@tonic-gate uu_avl_numnodes(uu_avl_t *ap)
274*0Sstevel@tonic-gate {
275*0Sstevel@tonic-gate 	return (avl_numnodes(&ap->ua_tree));
276*0Sstevel@tonic-gate }
277*0Sstevel@tonic-gate 
278*0Sstevel@tonic-gate void *
279*0Sstevel@tonic-gate uu_avl_first(uu_avl_t *ap)
280*0Sstevel@tonic-gate {
281*0Sstevel@tonic-gate 	return (avl_first(&ap->ua_tree));
282*0Sstevel@tonic-gate }
283*0Sstevel@tonic-gate 
284*0Sstevel@tonic-gate void *
285*0Sstevel@tonic-gate uu_avl_last(uu_avl_t *ap)
286*0Sstevel@tonic-gate {
287*0Sstevel@tonic-gate 	return (avl_last(&ap->ua_tree));
288*0Sstevel@tonic-gate }
289*0Sstevel@tonic-gate 
290*0Sstevel@tonic-gate void *
291*0Sstevel@tonic-gate uu_avl_next(uu_avl_t *ap, void *node)
292*0Sstevel@tonic-gate {
293*0Sstevel@tonic-gate 	return (AVL_NEXT(&ap->ua_tree, node));
294*0Sstevel@tonic-gate }
295*0Sstevel@tonic-gate 
296*0Sstevel@tonic-gate void *
297*0Sstevel@tonic-gate uu_avl_prev(uu_avl_t *ap, void *node)
298*0Sstevel@tonic-gate {
299*0Sstevel@tonic-gate 	return (AVL_PREV(&ap->ua_tree, node));
300*0Sstevel@tonic-gate }
301*0Sstevel@tonic-gate 
302*0Sstevel@tonic-gate static void
303*0Sstevel@tonic-gate _avl_walk_init(uu_avl_walk_t *wp, uu_avl_t *ap, uint32_t flags)
304*0Sstevel@tonic-gate {
305*0Sstevel@tonic-gate 	uu_avl_walk_t *next, *prev;
306*0Sstevel@tonic-gate 
307*0Sstevel@tonic-gate 	int robust = (flags & UU_WALK_ROBUST);
308*0Sstevel@tonic-gate 	int direction = (flags & UU_WALK_REVERSE)? -1 : 1;
309*0Sstevel@tonic-gate 
310*0Sstevel@tonic-gate 	(void) memset(wp, 0, sizeof (*wp));
311*0Sstevel@tonic-gate 	wp->uaw_avl = ap;
312*0Sstevel@tonic-gate 	wp->uaw_robust = robust;
313*0Sstevel@tonic-gate 	wp->uaw_dir = direction;
314*0Sstevel@tonic-gate 
315*0Sstevel@tonic-gate 	if (direction > 0)
316*0Sstevel@tonic-gate 		wp->uaw_next_result = avl_first(&ap->ua_tree);
317*0Sstevel@tonic-gate 	else
318*0Sstevel@tonic-gate 		wp->uaw_next_result = avl_last(&ap->ua_tree);
319*0Sstevel@tonic-gate 
320*0Sstevel@tonic-gate 	if (ap->ua_debug || robust) {
321*0Sstevel@tonic-gate 		wp->uaw_next = next = &ap->ua_null_walk;
322*0Sstevel@tonic-gate 		wp->uaw_prev = prev = next->uaw_prev;
323*0Sstevel@tonic-gate 		next->uaw_prev = wp;
324*0Sstevel@tonic-gate 		prev->uaw_next = wp;
325*0Sstevel@tonic-gate 	}
326*0Sstevel@tonic-gate }
327*0Sstevel@tonic-gate 
328*0Sstevel@tonic-gate static void *
329*0Sstevel@tonic-gate _avl_walk_advance(uu_avl_walk_t *wp, uu_avl_t *ap)
330*0Sstevel@tonic-gate {
331*0Sstevel@tonic-gate 	void *np = wp->uaw_next_result;
332*0Sstevel@tonic-gate 
333*0Sstevel@tonic-gate 	avl_tree_t *t = &ap->ua_tree;
334*0Sstevel@tonic-gate 
335*0Sstevel@tonic-gate 	if (np == NULL)
336*0Sstevel@tonic-gate 		return (NULL);
337*0Sstevel@tonic-gate 
338*0Sstevel@tonic-gate 	wp->uaw_next_result = (wp->uaw_dir > 0)? AVL_NEXT(t, np) :
339*0Sstevel@tonic-gate 	    AVL_PREV(t, np);
340*0Sstevel@tonic-gate 
341*0Sstevel@tonic-gate 	return (np);
342*0Sstevel@tonic-gate }
343*0Sstevel@tonic-gate 
344*0Sstevel@tonic-gate static void
345*0Sstevel@tonic-gate _avl_walk_fini(uu_avl_walk_t *wp)
346*0Sstevel@tonic-gate {
347*0Sstevel@tonic-gate 	if (wp->uaw_next != NULL) {
348*0Sstevel@tonic-gate 		wp->uaw_next->uaw_prev = wp->uaw_prev;
349*0Sstevel@tonic-gate 		wp->uaw_prev->uaw_next = wp->uaw_next;
350*0Sstevel@tonic-gate 		wp->uaw_next = NULL;
351*0Sstevel@tonic-gate 		wp->uaw_prev = NULL;
352*0Sstevel@tonic-gate 	}
353*0Sstevel@tonic-gate 	wp->uaw_avl = NULL;
354*0Sstevel@tonic-gate 	wp->uaw_next_result = NULL;
355*0Sstevel@tonic-gate }
356*0Sstevel@tonic-gate 
357*0Sstevel@tonic-gate uu_avl_walk_t *
358*0Sstevel@tonic-gate uu_avl_walk_start(uu_avl_t *ap, uint32_t flags)
359*0Sstevel@tonic-gate {
360*0Sstevel@tonic-gate 	uu_avl_walk_t *wp;
361*0Sstevel@tonic-gate 
362*0Sstevel@tonic-gate 	if (flags & ~(UU_WALK_ROBUST | UU_WALK_REVERSE)) {
363*0Sstevel@tonic-gate 		uu_set_error(UU_ERROR_UNKNOWN_FLAG);
364*0Sstevel@tonic-gate 		return (NULL);
365*0Sstevel@tonic-gate 	}
366*0Sstevel@tonic-gate 
367*0Sstevel@tonic-gate 	wp = uu_zalloc(sizeof (*wp));
368*0Sstevel@tonic-gate 	if (wp == NULL) {
369*0Sstevel@tonic-gate 		uu_set_error(UU_ERROR_NO_MEMORY);
370*0Sstevel@tonic-gate 		return (NULL);
371*0Sstevel@tonic-gate 	}
372*0Sstevel@tonic-gate 
373*0Sstevel@tonic-gate 	_avl_walk_init(wp, ap, flags);
374*0Sstevel@tonic-gate 	return (wp);
375*0Sstevel@tonic-gate }
376*0Sstevel@tonic-gate 
377*0Sstevel@tonic-gate void *
378*0Sstevel@tonic-gate uu_avl_walk_next(uu_avl_walk_t *wp)
379*0Sstevel@tonic-gate {
380*0Sstevel@tonic-gate 	return (_avl_walk_advance(wp, wp->uaw_avl));
381*0Sstevel@tonic-gate }
382*0Sstevel@tonic-gate 
383*0Sstevel@tonic-gate void
384*0Sstevel@tonic-gate uu_avl_walk_end(uu_avl_walk_t *wp)
385*0Sstevel@tonic-gate {
386*0Sstevel@tonic-gate 	_avl_walk_fini(wp);
387*0Sstevel@tonic-gate 	uu_free(wp);
388*0Sstevel@tonic-gate }
389*0Sstevel@tonic-gate 
390*0Sstevel@tonic-gate int
391*0Sstevel@tonic-gate uu_avl_walk(uu_avl_t *ap, uu_walk_fn_t *func, void *private, uint32_t flags)
392*0Sstevel@tonic-gate {
393*0Sstevel@tonic-gate 	void *e;
394*0Sstevel@tonic-gate 	uu_avl_walk_t my_walk;
395*0Sstevel@tonic-gate 
396*0Sstevel@tonic-gate 	int status = UU_WALK_NEXT;
397*0Sstevel@tonic-gate 
398*0Sstevel@tonic-gate 	if (flags & ~(UU_WALK_ROBUST | UU_WALK_REVERSE)) {
399*0Sstevel@tonic-gate 		uu_set_error(UU_ERROR_UNKNOWN_FLAG);
400*0Sstevel@tonic-gate 		return (-1);
401*0Sstevel@tonic-gate 	}
402*0Sstevel@tonic-gate 
403*0Sstevel@tonic-gate 	_avl_walk_init(&my_walk, ap, flags);
404*0Sstevel@tonic-gate 	while (status == UU_WALK_NEXT &&
405*0Sstevel@tonic-gate 	    (e = _avl_walk_advance(&my_walk, ap)) != NULL)
406*0Sstevel@tonic-gate 		status = (*func)(e, private);
407*0Sstevel@tonic-gate 	_avl_walk_fini(&my_walk);
408*0Sstevel@tonic-gate 
409*0Sstevel@tonic-gate 	if (status >= 0)
410*0Sstevel@tonic-gate 		return (0);
411*0Sstevel@tonic-gate 	uu_set_error(UU_ERROR_CALLBACK_FAILED);
412*0Sstevel@tonic-gate 	return (-1);
413*0Sstevel@tonic-gate }
414*0Sstevel@tonic-gate 
415*0Sstevel@tonic-gate void
416*0Sstevel@tonic-gate uu_avl_remove(uu_avl_t *ap, void *elem)
417*0Sstevel@tonic-gate {
418*0Sstevel@tonic-gate 	uu_avl_walk_t *wp;
419*0Sstevel@tonic-gate 	uu_avl_pool_t *pp = ap->ua_pool;
420*0Sstevel@tonic-gate 	uintptr_t *na = NODE_ARRAY(pp, elem);
421*0Sstevel@tonic-gate 
422*0Sstevel@tonic-gate 	if (ap->ua_debug) {
423*0Sstevel@tonic-gate 		/*
424*0Sstevel@tonic-gate 		 * invalidate outstanding uu_avl_index_ts.
425*0Sstevel@tonic-gate 		 */
426*0Sstevel@tonic-gate 		ap->ua_index = INDEX_NEXT(ap->ua_index);
427*0Sstevel@tonic-gate 	}
428*0Sstevel@tonic-gate 
429*0Sstevel@tonic-gate 	/*
430*0Sstevel@tonic-gate 	 * Robust walkers most be advanced, if we are removing the node
431*0Sstevel@tonic-gate 	 * they are currently using.  In debug mode, non-robust walkers
432*0Sstevel@tonic-gate 	 * are also on the walker list.
433*0Sstevel@tonic-gate 	 */
434*0Sstevel@tonic-gate 	for (wp = ap->ua_null_walk.uaw_next; wp != &ap->ua_null_walk;
435*0Sstevel@tonic-gate 	    wp = wp->uaw_next) {
436*0Sstevel@tonic-gate 		if (wp->uaw_robust) {
437*0Sstevel@tonic-gate 			if (elem == wp->uaw_next_result)
438*0Sstevel@tonic-gate 				(void) _avl_walk_advance(wp, ap);
439*0Sstevel@tonic-gate 		} else if (wp->uaw_next_result != NULL) {
440*0Sstevel@tonic-gate 			uu_panic("uu_avl_remove(%p, %p): active non-robust "
441*0Sstevel@tonic-gate 			    "walker\n", ap, elem);
442*0Sstevel@tonic-gate 		}
443*0Sstevel@tonic-gate 	}
444*0Sstevel@tonic-gate 
445*0Sstevel@tonic-gate 	avl_remove(&ap->ua_tree, elem);
446*0Sstevel@tonic-gate 
447*0Sstevel@tonic-gate 	na[0] = POOL_TO_MARKER(pp);
448*0Sstevel@tonic-gate 	na[1] = NULL;
449*0Sstevel@tonic-gate }
450*0Sstevel@tonic-gate 
451*0Sstevel@tonic-gate void *
452*0Sstevel@tonic-gate uu_avl_teardown(uu_avl_t *ap, void **cookie)
453*0Sstevel@tonic-gate {
454*0Sstevel@tonic-gate 	return (avl_destroy_nodes(&ap->ua_tree, cookie));
455*0Sstevel@tonic-gate }
456*0Sstevel@tonic-gate 
457*0Sstevel@tonic-gate void *
458*0Sstevel@tonic-gate uu_avl_find(uu_avl_t *ap, void *elem, void *private, uu_avl_index_t *out)
459*0Sstevel@tonic-gate {
460*0Sstevel@tonic-gate 	struct uu_avl_node_compare_info info;
461*0Sstevel@tonic-gate 	void *result;
462*0Sstevel@tonic-gate 
463*0Sstevel@tonic-gate 	info.ac_compare = ap->ua_pool->uap_cmp;
464*0Sstevel@tonic-gate 	info.ac_private = private;
465*0Sstevel@tonic-gate 	info.ac_right = elem;
466*0Sstevel@tonic-gate 	info.ac_found = NULL;
467*0Sstevel@tonic-gate 
468*0Sstevel@tonic-gate 	result = avl_find(&ap->ua_tree, &info, out);
469*0Sstevel@tonic-gate 	if (out != NULL)
470*0Sstevel@tonic-gate 		*out = INDEX_ENCODE(ap, *out);
471*0Sstevel@tonic-gate 
472*0Sstevel@tonic-gate 	if (ap->ua_debug && result != NULL)
473*0Sstevel@tonic-gate 		uu_panic("uu_avl_find: internal error: avl_find succeeded\n");
474*0Sstevel@tonic-gate 
475*0Sstevel@tonic-gate 	return (info.ac_found);
476*0Sstevel@tonic-gate }
477*0Sstevel@tonic-gate 
478*0Sstevel@tonic-gate void
479*0Sstevel@tonic-gate uu_avl_insert(uu_avl_t *ap, void *elem, uu_avl_index_t idx)
480*0Sstevel@tonic-gate {
481*0Sstevel@tonic-gate 	if (ap->ua_debug) {
482*0Sstevel@tonic-gate 		uu_avl_pool_t *pp = ap->ua_pool;
483*0Sstevel@tonic-gate 		uintptr_t *na = NODE_ARRAY(pp, elem);
484*0Sstevel@tonic-gate 
485*0Sstevel@tonic-gate 		if (na[1] != NULL)
486*0Sstevel@tonic-gate 			uu_panic("uu_avl_insert(%p, %p, %p): node already "
487*0Sstevel@tonic-gate 			    "in tree, or corrupt\n",
488*0Sstevel@tonic-gate 			    ap, elem, idx);
489*0Sstevel@tonic-gate 		if (na[0] == NULL)
490*0Sstevel@tonic-gate 			uu_panic("uu_avl_insert(%p, %p, %p): node not "
491*0Sstevel@tonic-gate 			    "initialized\n",
492*0Sstevel@tonic-gate 			    ap, elem, idx);
493*0Sstevel@tonic-gate 		if (na[0] != POOL_TO_MARKER(pp))
494*0Sstevel@tonic-gate 			uu_panic("uu_avl_insert(%p, %p, %p): node from "
495*0Sstevel@tonic-gate 			    "other pool, or corrupt\n",
496*0Sstevel@tonic-gate 			    ap, elem, idx);
497*0Sstevel@tonic-gate 
498*0Sstevel@tonic-gate 		if (!INDEX_VALID(ap, idx))
499*0Sstevel@tonic-gate 			uu_panic("uu_avl_insert(%p, %p, %p): %s\n",
500*0Sstevel@tonic-gate 			    ap, elem, idx,
501*0Sstevel@tonic-gate 			    INDEX_CHECK(idx)? "outdated index" :
502*0Sstevel@tonic-gate 			    "invalid index");
503*0Sstevel@tonic-gate 
504*0Sstevel@tonic-gate 		/*
505*0Sstevel@tonic-gate 		 * invalidate outstanding uu_avl_index_ts.
506*0Sstevel@tonic-gate 		 */
507*0Sstevel@tonic-gate 		ap->ua_index = INDEX_NEXT(ap->ua_index);
508*0Sstevel@tonic-gate 	}
509*0Sstevel@tonic-gate 	avl_insert(&ap->ua_tree, elem, INDEX_DECODE(idx));
510*0Sstevel@tonic-gate }
511*0Sstevel@tonic-gate 
512*0Sstevel@tonic-gate void *
513*0Sstevel@tonic-gate uu_avl_nearest_next(uu_avl_t *ap, uu_avl_index_t idx)
514*0Sstevel@tonic-gate {
515*0Sstevel@tonic-gate 	if (ap->ua_debug && !INDEX_VALID(ap, idx))
516*0Sstevel@tonic-gate 		uu_panic("uu_avl_nearest_next(%p, %p): %s\n",
517*0Sstevel@tonic-gate 		    ap, idx, INDEX_CHECK(idx)? "outdated index" :
518*0Sstevel@tonic-gate 		    "invalid index");
519*0Sstevel@tonic-gate 	return (avl_nearest(&ap->ua_tree, INDEX_DECODE(idx), AVL_AFTER));
520*0Sstevel@tonic-gate }
521*0Sstevel@tonic-gate 
522*0Sstevel@tonic-gate void *
523*0Sstevel@tonic-gate uu_avl_nearest_prev(uu_avl_t *ap, uu_avl_index_t idx)
524*0Sstevel@tonic-gate {
525*0Sstevel@tonic-gate 	if (ap->ua_debug && !INDEX_VALID(ap, idx))
526*0Sstevel@tonic-gate 		uu_panic("uu_avl_nearest_prev(%p, %p): %s\n",
527*0Sstevel@tonic-gate 		    ap, idx, INDEX_CHECK(idx)? "outdated index" :
528*0Sstevel@tonic-gate 		    "invalid index");
529*0Sstevel@tonic-gate 	return (avl_nearest(&ap->ua_tree, INDEX_DECODE(idx), AVL_BEFORE));
530*0Sstevel@tonic-gate }
531*0Sstevel@tonic-gate 
532*0Sstevel@tonic-gate /*
533*0Sstevel@tonic-gate  * called from uu_lockup() and uu_release(), as part of our fork1()-safety.
534*0Sstevel@tonic-gate  */
535*0Sstevel@tonic-gate void
536*0Sstevel@tonic-gate uu_avl_lockup(void)
537*0Sstevel@tonic-gate {
538*0Sstevel@tonic-gate 	uu_avl_pool_t *pp;
539*0Sstevel@tonic-gate 
540*0Sstevel@tonic-gate 	(void) pthread_mutex_lock(&uu_apool_list_lock);
541*0Sstevel@tonic-gate 	for (pp = uu_null_apool.uap_next; pp != &uu_null_apool;
542*0Sstevel@tonic-gate 	    pp = pp->uap_next)
543*0Sstevel@tonic-gate 		(void) pthread_mutex_lock(&pp->uap_lock);
544*0Sstevel@tonic-gate }
545*0Sstevel@tonic-gate 
546*0Sstevel@tonic-gate void
547*0Sstevel@tonic-gate uu_avl_release(void)
548*0Sstevel@tonic-gate {
549*0Sstevel@tonic-gate 	uu_avl_pool_t *pp;
550*0Sstevel@tonic-gate 
551*0Sstevel@tonic-gate 	for (pp = uu_null_apool.uap_next; pp != &uu_null_apool;
552*0Sstevel@tonic-gate 	    pp = pp->uap_next)
553*0Sstevel@tonic-gate 		(void) pthread_mutex_unlock(&pp->uap_lock);
554*0Sstevel@tonic-gate 	(void) pthread_mutex_unlock(&uu_apool_list_lock);
555*0Sstevel@tonic-gate }
556