xref: /titanic_52/usr/src/lib/libuutil/common/libuutil.h (revision cb174861876aea6950a7ab4ce944aff84b1914cd)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
51f6eb021SLiane Praza  * Common Development and Distribution License (the "License").
61f6eb021SLiane Praza  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22*cb174861Sjoyce mcintosh  * Copyright (c) 2004, 2010, Oracle and/or its affiliates. All rights reserved.
237c478bd9Sstevel@tonic-gate  */
247c478bd9Sstevel@tonic-gate 
257c478bd9Sstevel@tonic-gate #ifndef	_LIBUUTIL_H
267c478bd9Sstevel@tonic-gate #define	_LIBUUTIL_H
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate #include <sys/types.h>
297c478bd9Sstevel@tonic-gate #include <stdarg.h>
30*cb174861Sjoyce mcintosh #include <stdio.h>
317c478bd9Sstevel@tonic-gate 
327c478bd9Sstevel@tonic-gate #ifdef	__cplusplus
337c478bd9Sstevel@tonic-gate extern "C" {
347c478bd9Sstevel@tonic-gate #endif
357c478bd9Sstevel@tonic-gate 
367c478bd9Sstevel@tonic-gate /*
377c478bd9Sstevel@tonic-gate  * Standard flags codes.
387c478bd9Sstevel@tonic-gate  */
397c478bd9Sstevel@tonic-gate #define	UU_DEFAULT		0
407c478bd9Sstevel@tonic-gate 
417c478bd9Sstevel@tonic-gate /*
427c478bd9Sstevel@tonic-gate  * Standard error codes.
437c478bd9Sstevel@tonic-gate  */
447c478bd9Sstevel@tonic-gate #define	UU_ERROR_NONE		0	/* no error */
457c478bd9Sstevel@tonic-gate #define	UU_ERROR_INVALID_ARGUMENT 1	/* invalid argument */
467c478bd9Sstevel@tonic-gate #define	UU_ERROR_UNKNOWN_FLAG	2	/* passed flag invalid */
477c478bd9Sstevel@tonic-gate #define	UU_ERROR_NO_MEMORY	3	/* out of memory */
487c478bd9Sstevel@tonic-gate #define	UU_ERROR_CALLBACK_FAILED 4	/* callback-initiated error */
497c478bd9Sstevel@tonic-gate #define	UU_ERROR_NOT_SUPPORTED	5	/* operation not supported */
507c478bd9Sstevel@tonic-gate #define	UU_ERROR_EMPTY		6	/* no value provided */
517c478bd9Sstevel@tonic-gate #define	UU_ERROR_UNDERFLOW	7	/* value is too small */
527c478bd9Sstevel@tonic-gate #define	UU_ERROR_OVERFLOW	8	/* value is too value */
537c478bd9Sstevel@tonic-gate #define	UU_ERROR_INVALID_CHAR	9	/* value contains unexpected char */
547c478bd9Sstevel@tonic-gate #define	UU_ERROR_INVALID_DIGIT	10	/* value contains digit not in base */
557c478bd9Sstevel@tonic-gate 
567c478bd9Sstevel@tonic-gate #define	UU_ERROR_SYSTEM		99	/* underlying system error */
577c478bd9Sstevel@tonic-gate #define	UU_ERROR_UNKNOWN	100	/* error status not known */
587c478bd9Sstevel@tonic-gate 
597c478bd9Sstevel@tonic-gate /*
607c478bd9Sstevel@tonic-gate  * Standard program exit codes.
617c478bd9Sstevel@tonic-gate  */
627c478bd9Sstevel@tonic-gate #define	UU_EXIT_OK	(*(uu_exit_ok()))
637c478bd9Sstevel@tonic-gate #define	UU_EXIT_FATAL	(*(uu_exit_fatal()))
647c478bd9Sstevel@tonic-gate #define	UU_EXIT_USAGE	(*(uu_exit_usage()))
657c478bd9Sstevel@tonic-gate 
667c478bd9Sstevel@tonic-gate /*
677c478bd9Sstevel@tonic-gate  * Exit status profiles.
687c478bd9Sstevel@tonic-gate  */
697c478bd9Sstevel@tonic-gate #define	UU_PROFILE_DEFAULT	0
707c478bd9Sstevel@tonic-gate #define	UU_PROFILE_LAUNCHER	1
717c478bd9Sstevel@tonic-gate 
727c478bd9Sstevel@tonic-gate /*
737c478bd9Sstevel@tonic-gate  * Error reporting functions.
747c478bd9Sstevel@tonic-gate  */
757c478bd9Sstevel@tonic-gate uint32_t uu_error(void);
767c478bd9Sstevel@tonic-gate const char *uu_strerror(uint32_t);
777c478bd9Sstevel@tonic-gate 
787c478bd9Sstevel@tonic-gate /*
797c478bd9Sstevel@tonic-gate  * Program notification functions.
807c478bd9Sstevel@tonic-gate  */
817c478bd9Sstevel@tonic-gate extern void uu_alt_exit(int);
827c478bd9Sstevel@tonic-gate extern const char *uu_setpname(char *);
837c478bd9Sstevel@tonic-gate extern const char *uu_getpname(void);
847c478bd9Sstevel@tonic-gate /*PRINTFLIKE1*/
857c478bd9Sstevel@tonic-gate extern void uu_warn(const char *, ...);
867c478bd9Sstevel@tonic-gate extern void uu_vwarn(const char *, va_list);
877c478bd9Sstevel@tonic-gate /*PRINTFLIKE1*/
880b5c9250Shg115875 extern void uu_die(const char *, ...) __NORETURN;
890b5c9250Shg115875 extern void uu_vdie(const char *, va_list) __NORETURN;
907c478bd9Sstevel@tonic-gate /*PRINTFLIKE2*/
910b5c9250Shg115875 extern void uu_xdie(int, const char *, ...) __NORETURN;
920b5c9250Shg115875 extern void uu_vxdie(int, const char *, va_list) __NORETURN;
937c478bd9Sstevel@tonic-gate 
947c478bd9Sstevel@tonic-gate /*
957c478bd9Sstevel@tonic-gate  * Exit status functions (not to be used directly)
967c478bd9Sstevel@tonic-gate  */
977c478bd9Sstevel@tonic-gate extern int *uu_exit_ok(void);
987c478bd9Sstevel@tonic-gate extern int *uu_exit_fatal(void);
997c478bd9Sstevel@tonic-gate extern int *uu_exit_usage(void);
1007c478bd9Sstevel@tonic-gate 
1017c478bd9Sstevel@tonic-gate /*
1027c478bd9Sstevel@tonic-gate  * string->number conversions
1037c478bd9Sstevel@tonic-gate  */
1047c478bd9Sstevel@tonic-gate extern int uu_strtoint(const char *, void *, size_t, int, int64_t, int64_t);
1057c478bd9Sstevel@tonic-gate extern int uu_strtouint(const char *, void *, size_t, int, uint64_t, uint64_t);
1067c478bd9Sstevel@tonic-gate 
1077c478bd9Sstevel@tonic-gate /*
1087c478bd9Sstevel@tonic-gate  * Debug print facility functions.
1097c478bd9Sstevel@tonic-gate  */
1107c478bd9Sstevel@tonic-gate typedef struct uu_dprintf uu_dprintf_t;
1117c478bd9Sstevel@tonic-gate 
1127c478bd9Sstevel@tonic-gate typedef enum {
1137c478bd9Sstevel@tonic-gate 	UU_DPRINTF_SILENT,
1147c478bd9Sstevel@tonic-gate 	UU_DPRINTF_FATAL,
1157c478bd9Sstevel@tonic-gate 	UU_DPRINTF_WARNING,
1167c478bd9Sstevel@tonic-gate 	UU_DPRINTF_NOTICE,
1177c478bd9Sstevel@tonic-gate 	UU_DPRINTF_INFO,
1187c478bd9Sstevel@tonic-gate 	UU_DPRINTF_DEBUG
1197c478bd9Sstevel@tonic-gate } uu_dprintf_severity_t;
1207c478bd9Sstevel@tonic-gate 
1217c478bd9Sstevel@tonic-gate extern uu_dprintf_t *uu_dprintf_create(const char *, uu_dprintf_severity_t,
1227c478bd9Sstevel@tonic-gate     uint_t);
1237c478bd9Sstevel@tonic-gate /*PRINTFLIKE3*/
1247c478bd9Sstevel@tonic-gate extern void uu_dprintf(uu_dprintf_t *, uu_dprintf_severity_t,
1257c478bd9Sstevel@tonic-gate     const char *, ...);
1267c478bd9Sstevel@tonic-gate extern void uu_dprintf_destroy(uu_dprintf_t *);
1277c478bd9Sstevel@tonic-gate extern const char *uu_dprintf_getname(uu_dprintf_t *);
1287c478bd9Sstevel@tonic-gate 
1297c478bd9Sstevel@tonic-gate /*
1307c478bd9Sstevel@tonic-gate  * Identifier test flags and function.
1317c478bd9Sstevel@tonic-gate  */
1327c478bd9Sstevel@tonic-gate #define	UU_NAME_DOMAIN		0x1	/* allow SUNW, or com.sun, prefix */
1337c478bd9Sstevel@tonic-gate #define	UU_NAME_PATH		0x2	/* allow '/'-delimited paths */
1347c478bd9Sstevel@tonic-gate 
1357c478bd9Sstevel@tonic-gate int uu_check_name(const char *, uint_t);
1367c478bd9Sstevel@tonic-gate 
1377c478bd9Sstevel@tonic-gate /*
1387c478bd9Sstevel@tonic-gate  * File creation functions.
1397c478bd9Sstevel@tonic-gate  */
1407c478bd9Sstevel@tonic-gate extern int uu_open_tmp(const char *dir, uint_t uflags);
1417c478bd9Sstevel@tonic-gate 
1427c478bd9Sstevel@tonic-gate /*
1437c478bd9Sstevel@tonic-gate  * Convenience functions.
1447c478bd9Sstevel@tonic-gate  */
145*cb174861Sjoyce mcintosh #define	UU_NELEM(a)	(sizeof (a) / sizeof ((a)[0]))
146*cb174861Sjoyce mcintosh 
1477c478bd9Sstevel@tonic-gate /*PRINTFLIKE1*/
1487c478bd9Sstevel@tonic-gate extern char *uu_msprintf(const char *format, ...);
1497c478bd9Sstevel@tonic-gate extern void *uu_zalloc(size_t);
1501f6eb021SLiane Praza extern char *uu_strdup(const char *);
1517c478bd9Sstevel@tonic-gate extern void uu_free(void *);
1527c478bd9Sstevel@tonic-gate 
153*cb174861Sjoyce mcintosh extern boolean_t uu_strcaseeq(const char *a, const char *b);
154*cb174861Sjoyce mcintosh extern boolean_t uu_streq(const char *a, const char *b);
155*cb174861Sjoyce mcintosh extern char *uu_strndup(const char *s, size_t n);
156*cb174861Sjoyce mcintosh extern boolean_t uu_strbw(const char *a, const char *b);
157*cb174861Sjoyce mcintosh extern void *uu_memdup(const void *buf, size_t sz);
158*cb174861Sjoyce mcintosh extern void uu_dump(FILE *out, const char *prefix, const void *buf, size_t len);
159*cb174861Sjoyce mcintosh 
1607c478bd9Sstevel@tonic-gate /*
1617c478bd9Sstevel@tonic-gate  * Comparison function type definition.
1627c478bd9Sstevel@tonic-gate  *   Developers should be careful in their use of the _private argument. If you
1637c478bd9Sstevel@tonic-gate  *   break interface guarantees, you get undefined behavior.
1647c478bd9Sstevel@tonic-gate  */
1657c478bd9Sstevel@tonic-gate typedef int uu_compare_fn_t(const void *__left, const void *__right,
1667c478bd9Sstevel@tonic-gate     void *__private);
1677c478bd9Sstevel@tonic-gate 
1687c478bd9Sstevel@tonic-gate /*
1697c478bd9Sstevel@tonic-gate  * Walk variant flags.
1707c478bd9Sstevel@tonic-gate  *   A data structure need not provide support for all variants and
1717c478bd9Sstevel@tonic-gate  *   combinations.  Refer to the appropriate documentation.
1727c478bd9Sstevel@tonic-gate  */
1737c478bd9Sstevel@tonic-gate #define	UU_WALK_ROBUST		0x00000001	/* walk can survive removes */
1747c478bd9Sstevel@tonic-gate #define	UU_WALK_REVERSE		0x00000002	/* reverse walk order */
1757c478bd9Sstevel@tonic-gate 
1767c478bd9Sstevel@tonic-gate #define	UU_WALK_PREORDER	0x00000010	/* walk tree in pre-order */
1777c478bd9Sstevel@tonic-gate #define	UU_WALK_POSTORDER	0x00000020	/* walk tree in post-order */
1787c478bd9Sstevel@tonic-gate 
1797c478bd9Sstevel@tonic-gate /*
1807c478bd9Sstevel@tonic-gate  * Walk callback function return codes.
1817c478bd9Sstevel@tonic-gate  */
1827c478bd9Sstevel@tonic-gate #define	UU_WALK_ERROR		-1
1837c478bd9Sstevel@tonic-gate #define	UU_WALK_NEXT		0
1847c478bd9Sstevel@tonic-gate #define	UU_WALK_DONE		1
1857c478bd9Sstevel@tonic-gate 
1867c478bd9Sstevel@tonic-gate /*
1877c478bd9Sstevel@tonic-gate  * Walk callback function type definition.
1887c478bd9Sstevel@tonic-gate  */
1897c478bd9Sstevel@tonic-gate typedef int uu_walk_fn_t(void *_elem, void *_private);
1907c478bd9Sstevel@tonic-gate 
1917c478bd9Sstevel@tonic-gate /*
1927c478bd9Sstevel@tonic-gate  * lists: opaque structures
1937c478bd9Sstevel@tonic-gate  */
1947c478bd9Sstevel@tonic-gate typedef struct uu_list_pool uu_list_pool_t;
1957c478bd9Sstevel@tonic-gate typedef struct uu_list uu_list_t;
1967c478bd9Sstevel@tonic-gate 
1977c478bd9Sstevel@tonic-gate typedef struct uu_list_node {
1987c478bd9Sstevel@tonic-gate 	uintptr_t uln_opaque[2];
1997c478bd9Sstevel@tonic-gate } uu_list_node_t;
2007c478bd9Sstevel@tonic-gate 
2017c478bd9Sstevel@tonic-gate typedef struct uu_list_walk uu_list_walk_t;
2027c478bd9Sstevel@tonic-gate 
2037c478bd9Sstevel@tonic-gate typedef uintptr_t uu_list_index_t;
2047c478bd9Sstevel@tonic-gate 
2057c478bd9Sstevel@tonic-gate /*
2067c478bd9Sstevel@tonic-gate  * lists: interface
2077c478bd9Sstevel@tonic-gate  *
2087c478bd9Sstevel@tonic-gate  * basic usage:
2097c478bd9Sstevel@tonic-gate  *	typedef struct foo {
2107c478bd9Sstevel@tonic-gate  *		...
2117c478bd9Sstevel@tonic-gate  *		uu_list_node_t foo_node;
2127c478bd9Sstevel@tonic-gate  *		...
2137c478bd9Sstevel@tonic-gate  *	} foo_t;
2147c478bd9Sstevel@tonic-gate  *
2157c478bd9Sstevel@tonic-gate  *	static int
2167c478bd9Sstevel@tonic-gate  *	foo_compare(void *l_arg, void *r_arg, void *private)
2177c478bd9Sstevel@tonic-gate  *	{
2187c478bd9Sstevel@tonic-gate  *		foo_t *l = l_arg;
2197c478bd9Sstevel@tonic-gate  *		foo_t *r = r_arg;
2207c478bd9Sstevel@tonic-gate  *
2217c478bd9Sstevel@tonic-gate  *		if (... l greater than r ...)
2227c478bd9Sstevel@tonic-gate  *			return (1);
2237c478bd9Sstevel@tonic-gate  *		if (... l less than r ...)
2247c478bd9Sstevel@tonic-gate  *			return (-1);
2257c478bd9Sstevel@tonic-gate  *		return (0);
2267c478bd9Sstevel@tonic-gate  *	}
2277c478bd9Sstevel@tonic-gate  *
2287c478bd9Sstevel@tonic-gate  *	...
2297c478bd9Sstevel@tonic-gate  *		// at initialization time
2307c478bd9Sstevel@tonic-gate  *		foo_pool = uu_list_pool_create("foo_pool",
2317c478bd9Sstevel@tonic-gate  *		    sizeof (foo_t), offsetof(foo_t, foo_node), foo_compare,
2327c478bd9Sstevel@tonic-gate  *		    debugging? 0 : UU_AVL_POOL_DEBUG);
2337c478bd9Sstevel@tonic-gate  *	...
2347c478bd9Sstevel@tonic-gate  */
2357c478bd9Sstevel@tonic-gate uu_list_pool_t *uu_list_pool_create(const char *, size_t, size_t,
2367c478bd9Sstevel@tonic-gate     uu_compare_fn_t *, uint32_t);
2377c478bd9Sstevel@tonic-gate #define	UU_LIST_POOL_DEBUG	0x00000001
2387c478bd9Sstevel@tonic-gate 
2397c478bd9Sstevel@tonic-gate void uu_list_pool_destroy(uu_list_pool_t *);
2407c478bd9Sstevel@tonic-gate 
2417c478bd9Sstevel@tonic-gate /*
2427c478bd9Sstevel@tonic-gate  * usage:
2437c478bd9Sstevel@tonic-gate  *
2447c478bd9Sstevel@tonic-gate  *	foo_t *a;
2457c478bd9Sstevel@tonic-gate  *	a = malloc(sizeof(*a));
2467c478bd9Sstevel@tonic-gate  *	uu_list_node_init(a, &a->foo_list, pool);
2477c478bd9Sstevel@tonic-gate  *	...
2487c478bd9Sstevel@tonic-gate  *	uu_list_node_fini(a, &a->foo_list, pool);
2497c478bd9Sstevel@tonic-gate  *	free(a);
2507c478bd9Sstevel@tonic-gate  */
2517c478bd9Sstevel@tonic-gate void uu_list_node_init(void *, uu_list_node_t *, uu_list_pool_t *);
2527c478bd9Sstevel@tonic-gate void uu_list_node_fini(void *, uu_list_node_t *, uu_list_pool_t *);
2537c478bd9Sstevel@tonic-gate 
2547c478bd9Sstevel@tonic-gate uu_list_t *uu_list_create(uu_list_pool_t *, void *_parent, uint32_t);
2557c478bd9Sstevel@tonic-gate #define	UU_LIST_DEBUG	0x00000001
2567c478bd9Sstevel@tonic-gate #define	UU_LIST_SORTED	0x00000002	/* list is sorted */
2577c478bd9Sstevel@tonic-gate 
2587c478bd9Sstevel@tonic-gate void uu_list_destroy(uu_list_t *);	/* list must be empty */
2597c478bd9Sstevel@tonic-gate 
2607c478bd9Sstevel@tonic-gate size_t uu_list_numnodes(uu_list_t *);
2617c478bd9Sstevel@tonic-gate 
2627c478bd9Sstevel@tonic-gate void *uu_list_first(uu_list_t *);
2637c478bd9Sstevel@tonic-gate void *uu_list_last(uu_list_t *);
2647c478bd9Sstevel@tonic-gate 
2657c478bd9Sstevel@tonic-gate void *uu_list_next(uu_list_t *, void *);
2667c478bd9Sstevel@tonic-gate void *uu_list_prev(uu_list_t *, void *);
2677c478bd9Sstevel@tonic-gate 
2687c478bd9Sstevel@tonic-gate int uu_list_walk(uu_list_t *, uu_walk_fn_t *, void *, uint32_t);
2697c478bd9Sstevel@tonic-gate 
2707c478bd9Sstevel@tonic-gate uu_list_walk_t *uu_list_walk_start(uu_list_t *, uint32_t);
2717c478bd9Sstevel@tonic-gate void *uu_list_walk_next(uu_list_walk_t *);
2727c478bd9Sstevel@tonic-gate void uu_list_walk_end(uu_list_walk_t *);
2737c478bd9Sstevel@tonic-gate 
2747c478bd9Sstevel@tonic-gate void *uu_list_find(uu_list_t *, void *, void *, uu_list_index_t *);
2757c478bd9Sstevel@tonic-gate void uu_list_insert(uu_list_t *, void *, uu_list_index_t);
2767c478bd9Sstevel@tonic-gate 
2777c478bd9Sstevel@tonic-gate void *uu_list_nearest_next(uu_list_t *, uu_list_index_t);
2787c478bd9Sstevel@tonic-gate void *uu_list_nearest_prev(uu_list_t *, uu_list_index_t);
2797c478bd9Sstevel@tonic-gate 
2807c478bd9Sstevel@tonic-gate void *uu_list_teardown(uu_list_t *, void **);
2817c478bd9Sstevel@tonic-gate 
2827c478bd9Sstevel@tonic-gate void uu_list_remove(uu_list_t *, void *);
2837c478bd9Sstevel@tonic-gate 
2847c478bd9Sstevel@tonic-gate /*
2857c478bd9Sstevel@tonic-gate  * lists: interfaces for non-sorted lists only
2867c478bd9Sstevel@tonic-gate  */
2877c478bd9Sstevel@tonic-gate int uu_list_insert_before(uu_list_t *, void *_target, void *_elem);
2887c478bd9Sstevel@tonic-gate int uu_list_insert_after(uu_list_t *, void *_target, void *_elem);
2897c478bd9Sstevel@tonic-gate 
2907c478bd9Sstevel@tonic-gate /*
2917c478bd9Sstevel@tonic-gate  * avl trees: opaque structures
2927c478bd9Sstevel@tonic-gate  */
2937c478bd9Sstevel@tonic-gate typedef struct uu_avl_pool uu_avl_pool_t;
2947c478bd9Sstevel@tonic-gate typedef struct uu_avl uu_avl_t;
2957c478bd9Sstevel@tonic-gate 
2967c478bd9Sstevel@tonic-gate typedef struct uu_avl_node {
2977c478bd9Sstevel@tonic-gate #ifdef _LP64
2987c478bd9Sstevel@tonic-gate 	uintptr_t uan_opaque[3];
2997c478bd9Sstevel@tonic-gate #else
3007c478bd9Sstevel@tonic-gate 	uintptr_t uan_opaque[4];
3017c478bd9Sstevel@tonic-gate #endif
3027c478bd9Sstevel@tonic-gate } uu_avl_node_t;
3037c478bd9Sstevel@tonic-gate 
3047c478bd9Sstevel@tonic-gate typedef struct uu_avl_walk uu_avl_walk_t;
3057c478bd9Sstevel@tonic-gate 
3067c478bd9Sstevel@tonic-gate typedef uintptr_t uu_avl_index_t;
3077c478bd9Sstevel@tonic-gate 
3087c478bd9Sstevel@tonic-gate /*
3097c478bd9Sstevel@tonic-gate  * avl trees: interface
3107c478bd9Sstevel@tonic-gate  *
3117c478bd9Sstevel@tonic-gate  * basic usage:
3127c478bd9Sstevel@tonic-gate  *	typedef struct foo {
3137c478bd9Sstevel@tonic-gate  *		...
3147c478bd9Sstevel@tonic-gate  *		uu_avl_node_t foo_node;
3157c478bd9Sstevel@tonic-gate  *		...
3167c478bd9Sstevel@tonic-gate  *	} foo_t;
3177c478bd9Sstevel@tonic-gate  *
3187c478bd9Sstevel@tonic-gate  *	static int
3197c478bd9Sstevel@tonic-gate  *	foo_compare(void *l_arg, void *r_arg, void *private)
3207c478bd9Sstevel@tonic-gate  *	{
3217c478bd9Sstevel@tonic-gate  *		foo_t *l = l_arg;
3227c478bd9Sstevel@tonic-gate  *		foo_t *r = r_arg;
3237c478bd9Sstevel@tonic-gate  *
3247c478bd9Sstevel@tonic-gate  *		if (... l greater than r ...)
3257c478bd9Sstevel@tonic-gate  *			return (1);
3267c478bd9Sstevel@tonic-gate  *		if (... l less than r ...)
3277c478bd9Sstevel@tonic-gate  *			return (-1);
3287c478bd9Sstevel@tonic-gate  *		return (0);
3297c478bd9Sstevel@tonic-gate  *	}
3307c478bd9Sstevel@tonic-gate  *
3317c478bd9Sstevel@tonic-gate  *	...
3327c478bd9Sstevel@tonic-gate  *		// at initialization time
3337c478bd9Sstevel@tonic-gate  *		foo_pool = uu_avl_pool_create("foo_pool",
3347c478bd9Sstevel@tonic-gate  *		    sizeof (foo_t), offsetof(foo_t, foo_node), foo_compare,
3357c478bd9Sstevel@tonic-gate  *		    debugging? 0 : UU_AVL_POOL_DEBUG);
3367c478bd9Sstevel@tonic-gate  *	...
3377c478bd9Sstevel@tonic-gate  */
3387c478bd9Sstevel@tonic-gate uu_avl_pool_t *uu_avl_pool_create(const char *, size_t, size_t,
3397c478bd9Sstevel@tonic-gate     uu_compare_fn_t *, uint32_t);
3407c478bd9Sstevel@tonic-gate #define	UU_AVL_POOL_DEBUG	0x00000001
3417c478bd9Sstevel@tonic-gate 
3427c478bd9Sstevel@tonic-gate void uu_avl_pool_destroy(uu_avl_pool_t *);
3437c478bd9Sstevel@tonic-gate 
3447c478bd9Sstevel@tonic-gate /*
3457c478bd9Sstevel@tonic-gate  * usage:
3467c478bd9Sstevel@tonic-gate  *
3477c478bd9Sstevel@tonic-gate  *	foo_t *a;
3487c478bd9Sstevel@tonic-gate  *	a = malloc(sizeof(*a));
3497c478bd9Sstevel@tonic-gate  *	uu_avl_node_init(a, &a->foo_avl, pool);
3507c478bd9Sstevel@tonic-gate  *	...
3517c478bd9Sstevel@tonic-gate  *	uu_avl_node_fini(a, &a->foo_avl, pool);
3527c478bd9Sstevel@tonic-gate  *	free(a);
3537c478bd9Sstevel@tonic-gate  */
3547c478bd9Sstevel@tonic-gate void uu_avl_node_init(void *, uu_avl_node_t *, uu_avl_pool_t *);
3557c478bd9Sstevel@tonic-gate void uu_avl_node_fini(void *, uu_avl_node_t *, uu_avl_pool_t *);
3567c478bd9Sstevel@tonic-gate 
3577c478bd9Sstevel@tonic-gate uu_avl_t *uu_avl_create(uu_avl_pool_t *, void *_parent, uint32_t);
3587c478bd9Sstevel@tonic-gate #define	UU_AVL_DEBUG	0x00000001
3597c478bd9Sstevel@tonic-gate 
3607c478bd9Sstevel@tonic-gate void uu_avl_destroy(uu_avl_t *);	/* list must be empty */
3617c478bd9Sstevel@tonic-gate 
3627c478bd9Sstevel@tonic-gate size_t uu_avl_numnodes(uu_avl_t *);
3637c478bd9Sstevel@tonic-gate 
3647c478bd9Sstevel@tonic-gate void *uu_avl_first(uu_avl_t *);
3657c478bd9Sstevel@tonic-gate void *uu_avl_last(uu_avl_t *);
3667c478bd9Sstevel@tonic-gate 
3677c478bd9Sstevel@tonic-gate void *uu_avl_next(uu_avl_t *, void *);
3687c478bd9Sstevel@tonic-gate void *uu_avl_prev(uu_avl_t *, void *);
3697c478bd9Sstevel@tonic-gate 
3707c478bd9Sstevel@tonic-gate int uu_avl_walk(uu_avl_t *, uu_walk_fn_t *, void *, uint32_t);
3717c478bd9Sstevel@tonic-gate 
3727c478bd9Sstevel@tonic-gate uu_avl_walk_t *uu_avl_walk_start(uu_avl_t *, uint32_t);
3737c478bd9Sstevel@tonic-gate void *uu_avl_walk_next(uu_avl_walk_t *);
3747c478bd9Sstevel@tonic-gate void uu_avl_walk_end(uu_avl_walk_t *);
3757c478bd9Sstevel@tonic-gate 
3767c478bd9Sstevel@tonic-gate void *uu_avl_find(uu_avl_t *, void *, void *, uu_avl_index_t *);
3777c478bd9Sstevel@tonic-gate void uu_avl_insert(uu_avl_t *, void *, uu_avl_index_t);
3787c478bd9Sstevel@tonic-gate 
3797c478bd9Sstevel@tonic-gate void *uu_avl_nearest_next(uu_avl_t *, uu_avl_index_t);
3807c478bd9Sstevel@tonic-gate void *uu_avl_nearest_prev(uu_avl_t *, uu_avl_index_t);
3817c478bd9Sstevel@tonic-gate 
3827c478bd9Sstevel@tonic-gate void *uu_avl_teardown(uu_avl_t *, void **);
3837c478bd9Sstevel@tonic-gate 
3847c478bd9Sstevel@tonic-gate void uu_avl_remove(uu_avl_t *, void *);
3857c478bd9Sstevel@tonic-gate 
3867c478bd9Sstevel@tonic-gate #ifdef	__cplusplus
3877c478bd9Sstevel@tonic-gate }
3887c478bd9Sstevel@tonic-gate #endif
3897c478bd9Sstevel@tonic-gate 
3907c478bd9Sstevel@tonic-gate #endif	/* _LIBUUTIL_H */
391