xref: /freebsd/sys/contrib/openzfs/include/libuutil.h (revision 77013d11e6483b970af25e13c9b892075742f7e5)
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 (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright (c) 2004, 2010, Oracle and/or its affiliates. All rights reserved.
23  */
24 
25 #ifndef	_LIBUUTIL_H
26 #define	_LIBUUTIL_H
27 
28 #include <sys/types.h>
29 #include <stdarg.h>
30 #include <stdio.h>
31 
32 #ifdef	__cplusplus
33 extern "C" {
34 #endif
35 
36 /*
37  * Standard flags codes.
38  */
39 #define	UU_DEFAULT		0
40 
41 /*
42  * Standard error codes.
43  */
44 #define	UU_ERROR_NONE		0	/* no error */
45 #define	UU_ERROR_INVALID_ARGUMENT 1	/* invalid argument */
46 #define	UU_ERROR_UNKNOWN_FLAG	2	/* passed flag invalid */
47 #define	UU_ERROR_NO_MEMORY	3	/* out of memory */
48 #define	UU_ERROR_CALLBACK_FAILED 4	/* callback-initiated error */
49 #define	UU_ERROR_NOT_SUPPORTED	5	/* operation not supported */
50 #define	UU_ERROR_EMPTY		6	/* no value provided */
51 #define	UU_ERROR_UNDERFLOW	7	/* value is too small */
52 #define	UU_ERROR_OVERFLOW	8	/* value is too value */
53 #define	UU_ERROR_INVALID_CHAR	9	/* value contains unexpected char */
54 #define	UU_ERROR_INVALID_DIGIT	10	/* value contains digit not in base */
55 
56 #define	UU_ERROR_SYSTEM		99	/* underlying system error */
57 #define	UU_ERROR_UNKNOWN	100	/* error status not known */
58 
59 /*
60  * Standard program exit codes.
61  */
62 #define	UU_EXIT_OK	(*(uu_exit_ok()))
63 #define	UU_EXIT_FATAL	(*(uu_exit_fatal()))
64 #define	UU_EXIT_USAGE	(*(uu_exit_usage()))
65 
66 /*
67  * Exit status profiles.
68  */
69 #define	UU_PROFILE_DEFAULT	0
70 #define	UU_PROFILE_LAUNCHER	1
71 
72 /*
73  * Error reporting functions.
74  */
75 uint32_t uu_error(void);
76 const char *uu_strerror(uint32_t);
77 
78 /*
79  * Program notification functions.
80  */
81 extern void uu_alt_exit(int);
82 extern const char *uu_setpname(char *);
83 extern const char *uu_getpname(void);
84 /*PRINTFLIKE1*/
85 extern void uu_warn(const char *, ...);
86 extern void uu_vwarn(const char *, va_list);
87 /*PRINTFLIKE1*/
88 extern void uu_die(const char *, ...) __NORETURN;
89 extern void uu_vdie(const char *, va_list) __NORETURN;
90 /*PRINTFLIKE2*/
91 extern void uu_xdie(int, const char *, ...) __NORETURN;
92 extern void uu_vxdie(int, const char *, va_list) __NORETURN;
93 
94 /*
95  * Exit status functions (not to be used directly)
96  */
97 extern int *uu_exit_ok(void);
98 extern int *uu_exit_fatal(void);
99 extern int *uu_exit_usage(void);
100 
101 /*
102  * Identifier test flags and function.
103  */
104 #define	UU_NAME_DOMAIN		0x1	/* allow SUNW, or com.sun, prefix */
105 #define	UU_NAME_PATH		0x2	/* allow '/'-delimited paths */
106 
107 int uu_check_name(const char *, uint_t);
108 
109 /*
110  * Convenience functions.
111  */
112 #define	UU_NELEM(a)	(sizeof (a) / sizeof ((a)[0]))
113 
114 /*PRINTFLIKE1*/
115 extern char *uu_msprintf(const char *format, ...);
116 extern void *uu_zalloc(size_t);
117 extern char *uu_strdup(const char *);
118 extern void uu_free(void *);
119 
120 extern boolean_t uu_strcaseeq(const char *a, const char *b);
121 extern boolean_t uu_streq(const char *a, const char *b);
122 extern char *uu_strndup(const char *s, size_t n);
123 extern boolean_t uu_strbw(const char *a, const char *b);
124 extern void *uu_memdup(const void *buf, size_t sz);
125 
126 /*
127  * Comparison function type definition.
128  *   Developers should be careful in their use of the _private argument. If you
129  *   break interface guarantees, you get undefined behavior.
130  */
131 typedef int uu_compare_fn_t(const void *__left, const void *__right,
132     void *__private);
133 
134 /*
135  * Walk variant flags.
136  *   A data structure need not provide support for all variants and
137  *   combinations.  Refer to the appropriate documentation.
138  */
139 #define	UU_WALK_ROBUST		0x00000001	/* walk can survive removes */
140 #define	UU_WALK_REVERSE		0x00000002	/* reverse walk order */
141 
142 #define	UU_WALK_PREORDER	0x00000010	/* walk tree in pre-order */
143 #define	UU_WALK_POSTORDER	0x00000020	/* walk tree in post-order */
144 
145 /*
146  * Walk callback function return codes.
147  */
148 #define	UU_WALK_ERROR		-1
149 #define	UU_WALK_NEXT		0
150 #define	UU_WALK_DONE		1
151 
152 /*
153  * Walk callback function type definition.
154  */
155 typedef int uu_walk_fn_t(void *_elem, void *_private);
156 
157 /*
158  * lists: opaque structures
159  */
160 typedef struct uu_list_pool uu_list_pool_t;
161 typedef struct uu_list uu_list_t;
162 
163 typedef struct uu_list_node {
164 	uintptr_t uln_opaque[2];
165 } uu_list_node_t;
166 
167 typedef struct uu_list_walk uu_list_walk_t;
168 
169 typedef uintptr_t uu_list_index_t;
170 
171 /*
172  * lists: interface
173  *
174  * basic usage:
175  *	typedef struct foo {
176  *		...
177  *		uu_list_node_t foo_node;
178  *		...
179  *	} foo_t;
180  *
181  *	static int
182  *	foo_compare(void *l_arg, void *r_arg, void *private)
183  *	{
184  *		foo_t *l = l_arg;
185  *		foo_t *r = r_arg;
186  *
187  *		if (... l greater than r ...)
188  *			return (1);
189  *		if (... l less than r ...)
190  *			return (-1);
191  *		return (0);
192  *	}
193  *
194  *	...
195  *		// at initialization time
196  *		foo_pool = uu_list_pool_create("foo_pool",
197  *		    sizeof (foo_t), offsetof(foo_t, foo_node), foo_compare,
198  *		    debugging? 0 : UU_AVL_POOL_DEBUG);
199  *	...
200  */
201 uu_list_pool_t *uu_list_pool_create(const char *, size_t, size_t,
202     uu_compare_fn_t *, uint32_t);
203 #define	UU_LIST_POOL_DEBUG	0x00000001
204 
205 void uu_list_pool_destroy(uu_list_pool_t *);
206 
207 /*
208  * usage:
209  *
210  *	foo_t *a;
211  *	a = malloc(sizeof (*a));
212  *	uu_list_node_init(a, &a->foo_list, pool);
213  *	...
214  *	uu_list_node_fini(a, &a->foo_list, pool);
215  *	free(a);
216  */
217 void uu_list_node_init(void *, uu_list_node_t *, uu_list_pool_t *);
218 void uu_list_node_fini(void *, uu_list_node_t *, uu_list_pool_t *);
219 
220 uu_list_t *uu_list_create(uu_list_pool_t *, void *_parent, uint32_t);
221 #define	UU_LIST_DEBUG	0x00000001
222 #define	UU_LIST_SORTED	0x00000002	/* list is sorted */
223 
224 void uu_list_destroy(uu_list_t *);	/* list must be empty */
225 
226 size_t uu_list_numnodes(uu_list_t *);
227 
228 void *uu_list_first(uu_list_t *);
229 void *uu_list_last(uu_list_t *);
230 
231 void *uu_list_next(uu_list_t *, void *);
232 void *uu_list_prev(uu_list_t *, void *);
233 
234 int uu_list_walk(uu_list_t *, uu_walk_fn_t *, void *, uint32_t);
235 
236 uu_list_walk_t *uu_list_walk_start(uu_list_t *, uint32_t);
237 void *uu_list_walk_next(uu_list_walk_t *);
238 void uu_list_walk_end(uu_list_walk_t *);
239 
240 void *uu_list_find(uu_list_t *, void *, void *, uu_list_index_t *);
241 void uu_list_insert(uu_list_t *, void *, uu_list_index_t);
242 
243 void *uu_list_nearest_next(uu_list_t *, uu_list_index_t);
244 void *uu_list_nearest_prev(uu_list_t *, uu_list_index_t);
245 
246 void *uu_list_teardown(uu_list_t *, void **);
247 
248 void uu_list_remove(uu_list_t *, void *);
249 
250 /*
251  * lists: interfaces for non-sorted lists only
252  */
253 int uu_list_insert_before(uu_list_t *, void *_target, void *_elem);
254 int uu_list_insert_after(uu_list_t *, void *_target, void *_elem);
255 
256 /*
257  * avl trees: opaque structures
258  */
259 typedef struct uu_avl_pool uu_avl_pool_t;
260 typedef struct uu_avl uu_avl_t;
261 
262 typedef struct uu_avl_node {
263 #ifdef _LP64
264 	uintptr_t uan_opaque[3];
265 #else
266 	uintptr_t uan_opaque[4];
267 #endif
268 } uu_avl_node_t;
269 
270 typedef struct uu_avl_walk uu_avl_walk_t;
271 
272 typedef uintptr_t uu_avl_index_t;
273 
274 /*
275  * avl trees: interface
276  *
277  * basic usage:
278  *	typedef struct foo {
279  *		...
280  *		uu_avl_node_t foo_node;
281  *		...
282  *	} foo_t;
283  *
284  *	static int
285  *	foo_compare(void *l_arg, void *r_arg, void *private)
286  *	{
287  *		foo_t *l = l_arg;
288  *		foo_t *r = r_arg;
289  *
290  *		if (... l greater than r ...)
291  *			return (1);
292  *		if (... l less than r ...)
293  *			return (-1);
294  *		return (0);
295  *	}
296  *
297  *	...
298  *		// at initialization time
299  *		foo_pool = uu_avl_pool_create("foo_pool",
300  *		    sizeof (foo_t), offsetof(foo_t, foo_node), foo_compare,
301  *		    debugging? 0 : UU_AVL_POOL_DEBUG);
302  *	...
303  */
304 uu_avl_pool_t *uu_avl_pool_create(const char *, size_t, size_t,
305     uu_compare_fn_t *, uint32_t);
306 #define	UU_AVL_POOL_DEBUG	0x00000001
307 
308 void uu_avl_pool_destroy(uu_avl_pool_t *);
309 
310 /*
311  * usage:
312  *
313  *	foo_t *a;
314  *	a = malloc(sizeof (*a));
315  *	uu_avl_node_init(a, &a->foo_avl, pool);
316  *	...
317  *	uu_avl_node_fini(a, &a->foo_avl, pool);
318  *	free(a);
319  */
320 void uu_avl_node_init(void *, uu_avl_node_t *, uu_avl_pool_t *);
321 void uu_avl_node_fini(void *, uu_avl_node_t *, uu_avl_pool_t *);
322 
323 uu_avl_t *uu_avl_create(uu_avl_pool_t *, void *_parent, uint32_t);
324 #define	UU_AVL_DEBUG	0x00000001
325 
326 void uu_avl_destroy(uu_avl_t *);	/* list must be empty */
327 
328 size_t uu_avl_numnodes(uu_avl_t *);
329 
330 void *uu_avl_first(uu_avl_t *);
331 void *uu_avl_last(uu_avl_t *);
332 
333 void *uu_avl_next(uu_avl_t *, void *);
334 void *uu_avl_prev(uu_avl_t *, void *);
335 
336 int uu_avl_walk(uu_avl_t *, uu_walk_fn_t *, void *, uint32_t);
337 
338 uu_avl_walk_t *uu_avl_walk_start(uu_avl_t *, uint32_t);
339 void *uu_avl_walk_next(uu_avl_walk_t *);
340 void uu_avl_walk_end(uu_avl_walk_t *);
341 
342 void *uu_avl_find(uu_avl_t *, void *, void *, uu_avl_index_t *);
343 void uu_avl_insert(uu_avl_t *, void *, uu_avl_index_t);
344 
345 void *uu_avl_nearest_next(uu_avl_t *, uu_avl_index_t);
346 void *uu_avl_nearest_prev(uu_avl_t *, uu_avl_index_t);
347 
348 void *uu_avl_teardown(uu_avl_t *, void **);
349 
350 void uu_avl_remove(uu_avl_t *, void *);
351 
352 #ifdef	__cplusplus
353 }
354 #endif
355 
356 #endif	/* _LIBUUTIL_H */
357