libuutil.h revision 168404
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#ifndef	_LIBUUTIL_H
28#define	_LIBUUTIL_H
29
30#pragma ident	"%Z%%M%	%I%	%E% SMI"
31
32#include <solaris.h>
33#include <sys/types.h>
34#include <stdarg.h>
35
36#ifdef	__cplusplus
37extern "C" {
38#endif
39
40/*
41 * Standard flags codes.
42 */
43#define	UU_DEFAULT		0
44
45/*
46 * Standard error codes.
47 */
48#define	UU_ERROR_NONE		0	/* no error */
49#define	UU_ERROR_INVALID_ARGUMENT 1	/* invalid argument */
50#define	UU_ERROR_UNKNOWN_FLAG	2	/* passed flag invalid */
51#define	UU_ERROR_NO_MEMORY	3	/* out of memory */
52#define	UU_ERROR_CALLBACK_FAILED 4	/* callback-initiated error */
53#define	UU_ERROR_NOT_SUPPORTED	5	/* operation not supported */
54#define	UU_ERROR_EMPTY		6	/* no value provided */
55#define	UU_ERROR_UNDERFLOW	7	/* value is too small */
56#define	UU_ERROR_OVERFLOW	8	/* value is too value */
57#define	UU_ERROR_INVALID_CHAR	9	/* value contains unexpected char */
58#define	UU_ERROR_INVALID_DIGIT	10	/* value contains digit not in base */
59
60#define	UU_ERROR_SYSTEM		99	/* underlying system error */
61#define	UU_ERROR_UNKNOWN	100	/* error status not known */
62
63/*
64 * Standard program exit codes.
65 */
66#define	UU_EXIT_OK	(*(uu_exit_ok()))
67#define	UU_EXIT_FATAL	(*(uu_exit_fatal()))
68#define	UU_EXIT_USAGE	(*(uu_exit_usage()))
69
70/*
71 * Exit status profiles.
72 */
73#define	UU_PROFILE_DEFAULT	0
74#define	UU_PROFILE_LAUNCHER	1
75
76/*
77 * Error reporting functions.
78 */
79uint32_t uu_error(void);
80const char *uu_strerror(uint32_t);
81
82/*
83 * Program notification functions.
84 */
85extern void uu_alt_exit(int);
86extern const char *uu_setpname(char *);
87extern const char *uu_getpname(void);
88/*PRINTFLIKE1*/
89extern void uu_warn(const char *, ...);
90extern void uu_vwarn(const char *, va_list);
91/*PRINTFLIKE1*/
92extern void uu_die(const char *, ...) __NORETURN;
93extern void uu_vdie(const char *, va_list) __NORETURN;
94/*PRINTFLIKE2*/
95extern void uu_xdie(int, const char *, ...) __NORETURN;
96extern void uu_vxdie(int, const char *, va_list) __NORETURN;
97
98/*
99 * Exit status functions (not to be used directly)
100 */
101extern int *uu_exit_ok(void);
102extern int *uu_exit_fatal(void);
103extern int *uu_exit_usage(void);
104
105/*
106 * string->number conversions
107 */
108extern int uu_strtoint(const char *, void *, size_t, int, int64_t, int64_t);
109extern int uu_strtouint(const char *, void *, size_t, int, uint64_t, uint64_t);
110
111/*
112 * Debug print facility functions.
113 */
114typedef struct uu_dprintf uu_dprintf_t;
115
116typedef enum {
117	UU_DPRINTF_SILENT,
118	UU_DPRINTF_FATAL,
119	UU_DPRINTF_WARNING,
120	UU_DPRINTF_NOTICE,
121	UU_DPRINTF_INFO,
122	UU_DPRINTF_DEBUG
123} uu_dprintf_severity_t;
124
125extern uu_dprintf_t *uu_dprintf_create(const char *, uu_dprintf_severity_t,
126    uint_t);
127/*PRINTFLIKE3*/
128extern void uu_dprintf(uu_dprintf_t *, uu_dprintf_severity_t,
129    const char *, ...);
130extern void uu_dprintf_destroy(uu_dprintf_t *);
131extern const char *uu_dprintf_getname(uu_dprintf_t *);
132
133/*
134 * Identifier test flags and function.
135 */
136#define	UU_NAME_DOMAIN		0x1	/* allow SUNW, or com.sun, prefix */
137#define	UU_NAME_PATH		0x2	/* allow '/'-delimited paths */
138
139int uu_check_name(const char *, uint_t);
140
141/*
142 * File creation functions.
143 */
144extern int uu_open_tmp(const char *dir, uint_t uflags);
145
146/*
147 * Convenience functions.
148 */
149/*PRINTFLIKE1*/
150extern char *uu_msprintf(const char *format, ...);
151extern void *uu_zalloc(size_t);
152extern void uu_free(void *);
153
154/*
155 * Comparison function type definition.
156 *   Developers should be careful in their use of the _private argument. If you
157 *   break interface guarantees, you get undefined behavior.
158 */
159typedef int uu_compare_fn_t(const void *__left, const void *__right,
160    void *__private);
161
162/*
163 * Walk variant flags.
164 *   A data structure need not provide support for all variants and
165 *   combinations.  Refer to the appropriate documentation.
166 */
167#define	UU_WALK_ROBUST		0x00000001	/* walk can survive removes */
168#define	UU_WALK_REVERSE		0x00000002	/* reverse walk order */
169
170#define	UU_WALK_PREORDER	0x00000010	/* walk tree in pre-order */
171#define	UU_WALK_POSTORDER	0x00000020	/* walk tree in post-order */
172
173/*
174 * Walk callback function return codes.
175 */
176#define	UU_WALK_ERROR		-1
177#define	UU_WALK_NEXT		0
178#define	UU_WALK_DONE		1
179
180/*
181 * Walk callback function type definition.
182 */
183typedef int uu_walk_fn_t(void *_elem, void *_private);
184
185/*
186 * lists: opaque structures
187 */
188typedef struct uu_list_pool uu_list_pool_t;
189typedef struct uu_list uu_list_t;
190
191typedef struct uu_list_node {
192	uintptr_t uln_opaque[2];
193} uu_list_node_t;
194
195typedef struct uu_list_walk uu_list_walk_t;
196
197typedef uintptr_t uu_list_index_t;
198
199/*
200 * lists: interface
201 *
202 * basic usage:
203 *	typedef struct foo {
204 *		...
205 *		uu_list_node_t foo_node;
206 *		...
207 *	} foo_t;
208 *
209 *	static int
210 *	foo_compare(void *l_arg, void *r_arg, void *private)
211 *	{
212 *		foo_t *l = l_arg;
213 *		foo_t *r = r_arg;
214 *
215 *		if (... l greater than r ...)
216 *			return (1);
217 *		if (... l less than r ...)
218 *			return (-1);
219 *		return (0);
220 *	}
221 *
222 *	...
223 *		// at initialization time
224 *		foo_pool = uu_list_pool_create("foo_pool",
225 *		    sizeof (foo_t), offsetof(foo_t, foo_node), foo_compare,
226 *		    debugging? 0 : UU_AVL_POOL_DEBUG);
227 *	...
228 */
229uu_list_pool_t *uu_list_pool_create(const char *, size_t, size_t,
230    uu_compare_fn_t *, uint32_t);
231#define	UU_LIST_POOL_DEBUG	0x00000001
232
233void uu_list_pool_destroy(uu_list_pool_t *);
234
235/*
236 * usage:
237 *
238 *	foo_t *a;
239 *	a = malloc(sizeof(*a));
240 *	uu_list_node_init(a, &a->foo_list, pool);
241 *	...
242 *	uu_list_node_fini(a, &a->foo_list, pool);
243 *	free(a);
244 */
245void uu_list_node_init(void *, uu_list_node_t *, uu_list_pool_t *);
246void uu_list_node_fini(void *, uu_list_node_t *, uu_list_pool_t *);
247
248uu_list_t *uu_list_create(uu_list_pool_t *, void *_parent, uint32_t);
249#define	UU_LIST_DEBUG	0x00000001
250#define	UU_LIST_SORTED	0x00000002	/* list is sorted */
251
252void uu_list_destroy(uu_list_t *);	/* list must be empty */
253
254size_t uu_list_numnodes(uu_list_t *);
255
256void *uu_list_first(uu_list_t *);
257void *uu_list_last(uu_list_t *);
258
259void *uu_list_next(uu_list_t *, void *);
260void *uu_list_prev(uu_list_t *, void *);
261
262int uu_list_walk(uu_list_t *, uu_walk_fn_t *, void *, uint32_t);
263
264uu_list_walk_t *uu_list_walk_start(uu_list_t *, uint32_t);
265void *uu_list_walk_next(uu_list_walk_t *);
266void uu_list_walk_end(uu_list_walk_t *);
267
268void *uu_list_find(uu_list_t *, void *, void *, uu_list_index_t *);
269void uu_list_insert(uu_list_t *, void *, uu_list_index_t);
270
271void *uu_list_nearest_next(uu_list_t *, uu_list_index_t);
272void *uu_list_nearest_prev(uu_list_t *, uu_list_index_t);
273
274void *uu_list_teardown(uu_list_t *, void **);
275
276void uu_list_remove(uu_list_t *, void *);
277
278/*
279 * lists: interfaces for non-sorted lists only
280 */
281int uu_list_insert_before(uu_list_t *, void *_target, void *_elem);
282int uu_list_insert_after(uu_list_t *, void *_target, void *_elem);
283
284/*
285 * avl trees: opaque structures
286 */
287typedef struct uu_avl_pool uu_avl_pool_t;
288typedef struct uu_avl uu_avl_t;
289
290typedef struct uu_avl_node {
291#ifdef _LP64
292	uintptr_t uan_opaque[3];
293#else
294	uintptr_t uan_opaque[4];
295#endif
296} uu_avl_node_t;
297
298typedef struct uu_avl_walk uu_avl_walk_t;
299
300typedef uintptr_t uu_avl_index_t;
301
302/*
303 * avl trees: interface
304 *
305 * basic usage:
306 *	typedef struct foo {
307 *		...
308 *		uu_avl_node_t foo_node;
309 *		...
310 *	} foo_t;
311 *
312 *	static int
313 *	foo_compare(void *l_arg, void *r_arg, void *private)
314 *	{
315 *		foo_t *l = l_arg;
316 *		foo_t *r = r_arg;
317 *
318 *		if (... l greater than r ...)
319 *			return (1);
320 *		if (... l less than r ...)
321 *			return (-1);
322 *		return (0);
323 *	}
324 *
325 *	...
326 *		// at initialization time
327 *		foo_pool = uu_avl_pool_create("foo_pool",
328 *		    sizeof (foo_t), offsetof(foo_t, foo_node), foo_compare,
329 *		    debugging? 0 : UU_AVL_POOL_DEBUG);
330 *	...
331 */
332uu_avl_pool_t *uu_avl_pool_create(const char *, size_t, size_t,
333    uu_compare_fn_t *, uint32_t);
334#define	UU_AVL_POOL_DEBUG	0x00000001
335
336void uu_avl_pool_destroy(uu_avl_pool_t *);
337
338/*
339 * usage:
340 *
341 *	foo_t *a;
342 *	a = malloc(sizeof(*a));
343 *	uu_avl_node_init(a, &a->foo_avl, pool);
344 *	...
345 *	uu_avl_node_fini(a, &a->foo_avl, pool);
346 *	free(a);
347 */
348void uu_avl_node_init(void *, uu_avl_node_t *, uu_avl_pool_t *);
349void uu_avl_node_fini(void *, uu_avl_node_t *, uu_avl_pool_t *);
350
351uu_avl_t *uu_avl_create(uu_avl_pool_t *, void *_parent, uint32_t);
352#define	UU_AVL_DEBUG	0x00000001
353
354void uu_avl_destroy(uu_avl_t *);	/* list must be empty */
355
356size_t uu_avl_numnodes(uu_avl_t *);
357
358void *uu_avl_first(uu_avl_t *);
359void *uu_avl_last(uu_avl_t *);
360
361void *uu_avl_next(uu_avl_t *, void *);
362void *uu_avl_prev(uu_avl_t *, void *);
363
364int uu_avl_walk(uu_avl_t *, uu_walk_fn_t *, void *, uint32_t);
365
366uu_avl_walk_t *uu_avl_walk_start(uu_avl_t *, uint32_t);
367void *uu_avl_walk_next(uu_avl_walk_t *);
368void uu_avl_walk_end(uu_avl_walk_t *);
369
370void *uu_avl_find(uu_avl_t *, void *, void *, uu_avl_index_t *);
371void uu_avl_insert(uu_avl_t *, void *, uu_avl_index_t);
372
373void *uu_avl_nearest_next(uu_avl_t *, uu_avl_index_t);
374void *uu_avl_nearest_prev(uu_avl_t *, uu_avl_index_t);
375
376void *uu_avl_teardown(uu_avl_t *, void **);
377
378void uu_avl_remove(uu_avl_t *, void *);
379
380#ifdef	__cplusplus
381}
382#endif
383
384#endif	/* _LIBUUTIL_H */
385