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