xref: /freebsd/sys/contrib/openzfs/include/libuutil.h (revision 8b959dd6a3921c35395bef4a6d7ad2426a3bd88e)
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 extern void uu_warn(const char *, ...)
85     __attribute__((format(printf, 1, 2)));
86 extern void uu_vwarn(const char *, va_list)
87     __attribute__((format(printf, 1, 0)));
88 extern void uu_die(const char *, ...)
89     __attribute__((format(printf, 1, 2))) __NORETURN;
90 extern void uu_vdie(const char *, va_list)
91     __attribute__((format(printf, 1, 0))) __NORETURN;
92 extern void uu_xdie(int, const char *, ...)
93     __attribute__((format(printf, 2, 3))) __NORETURN;
94 extern void uu_vxdie(int, const char *, va_list)
95     __attribute__((format(printf, 2, 0))) __NORETURN;
96 
97 /*
98  * Exit status functions (not to be used directly)
99  */
100 extern int *uu_exit_ok(void);
101 extern int *uu_exit_fatal(void);
102 extern int *uu_exit_usage(void);
103 
104 /*
105  * Identifier test flags and function.
106  */
107 #define	UU_NAME_DOMAIN		0x1	/* allow SUNW, or com.sun, prefix */
108 #define	UU_NAME_PATH		0x2	/* allow '/'-delimited paths */
109 
110 int uu_check_name(const char *, uint_t);
111 
112 /*
113  * Convenience functions.
114  */
115 #define	UU_NELEM(a)	(sizeof (a) / sizeof ((a)[0]))
116 
117 extern char *uu_msprintf(const char *format, ...)
118     __attribute__((format(printf, 1, 2)));
119 extern void *uu_zalloc(size_t);
120 extern char *uu_strdup(const char *);
121 extern void uu_free(void *);
122 
123 extern boolean_t uu_strcaseeq(const char *a, const char *b);
124 extern boolean_t uu_streq(const char *a, const char *b);
125 extern char *uu_strndup(const char *s, size_t n);
126 extern boolean_t uu_strbw(const char *a, const char *b);
127 extern void *uu_memdup(const void *buf, size_t sz);
128 
129 /*
130  * Comparison function type definition.
131  *   Developers should be careful in their use of the _private argument. If you
132  *   break interface guarantees, you get undefined behavior.
133  */
134 typedef int uu_compare_fn_t(const void *__left, const void *__right,
135     void *__private);
136 
137 /*
138  * Walk variant flags.
139  *   A data structure need not provide support for all variants and
140  *   combinations.  Refer to the appropriate documentation.
141  */
142 #define	UU_WALK_ROBUST		0x00000001	/* walk can survive removes */
143 #define	UU_WALK_REVERSE		0x00000002	/* reverse walk order */
144 
145 #define	UU_WALK_PREORDER	0x00000010	/* walk tree in pre-order */
146 #define	UU_WALK_POSTORDER	0x00000020	/* walk tree in post-order */
147 
148 /*
149  * Walk callback function return codes.
150  */
151 #define	UU_WALK_ERROR		-1
152 #define	UU_WALK_NEXT		0
153 #define	UU_WALK_DONE		1
154 
155 /*
156  * Walk callback function type definition.
157  */
158 typedef int uu_walk_fn_t(void *_elem, void *_private);
159 
160 /*
161  * lists: opaque structures
162  */
163 typedef struct uu_list_pool uu_list_pool_t;
164 typedef struct uu_list uu_list_t;
165 
166 typedef struct uu_list_node {
167 	uintptr_t uln_opaque[2];
168 } uu_list_node_t;
169 
170 typedef struct uu_list_walk uu_list_walk_t;
171 
172 typedef uintptr_t uu_list_index_t;
173 
174 /*
175  * lists: interface
176  *
177  * basic usage:
178  *	typedef struct foo {
179  *		...
180  *		uu_list_node_t foo_node;
181  *		...
182  *	} foo_t;
183  *
184  *	static int
185  *	foo_compare(void *l_arg, void *r_arg, void *private)
186  *	{
187  *		foo_t *l = l_arg;
188  *		foo_t *r = r_arg;
189  *
190  *		if (... l greater than r ...)
191  *			return (1);
192  *		if (... l less than r ...)
193  *			return (-1);
194  *		return (0);
195  *	}
196  *
197  *	...
198  *		// at initialization time
199  *		foo_pool = uu_list_pool_create("foo_pool",
200  *		    sizeof (foo_t), offsetof(foo_t, foo_node), foo_compare,
201  *		    debugging? 0 : UU_AVL_POOL_DEBUG);
202  *	...
203  */
204 uu_list_pool_t *uu_list_pool_create(const char *, size_t, size_t,
205     uu_compare_fn_t *, uint32_t);
206 #define	UU_LIST_POOL_DEBUG	0x00000001
207 
208 void uu_list_pool_destroy(uu_list_pool_t *);
209 
210 /*
211  * usage:
212  *
213  *	foo_t *a;
214  *	a = malloc(sizeof (*a));
215  *	uu_list_node_init(a, &a->foo_list, pool);
216  *	...
217  *	uu_list_node_fini(a, &a->foo_list, pool);
218  *	free(a);
219  */
220 void uu_list_node_init(void *, uu_list_node_t *, uu_list_pool_t *);
221 void uu_list_node_fini(void *, uu_list_node_t *, uu_list_pool_t *);
222 
223 uu_list_t *uu_list_create(uu_list_pool_t *, void *_parent, uint32_t);
224 #define	UU_LIST_DEBUG	0x00000001
225 #define	UU_LIST_SORTED	0x00000002	/* list is sorted */
226 
227 void uu_list_destroy(uu_list_t *);	/* list must be empty */
228 
229 size_t uu_list_numnodes(uu_list_t *);
230 
231 void *uu_list_first(uu_list_t *);
232 void *uu_list_last(uu_list_t *);
233 
234 void *uu_list_next(uu_list_t *, void *);
235 void *uu_list_prev(uu_list_t *, void *);
236 
237 int uu_list_walk(uu_list_t *, uu_walk_fn_t *, void *, uint32_t);
238 
239 uu_list_walk_t *uu_list_walk_start(uu_list_t *, uint32_t);
240 void *uu_list_walk_next(uu_list_walk_t *);
241 void uu_list_walk_end(uu_list_walk_t *);
242 
243 void *uu_list_find(uu_list_t *, void *, void *, uu_list_index_t *);
244 void uu_list_insert(uu_list_t *, void *, uu_list_index_t);
245 
246 void *uu_list_nearest_next(uu_list_t *, uu_list_index_t);
247 void *uu_list_nearest_prev(uu_list_t *, uu_list_index_t);
248 
249 void *uu_list_teardown(uu_list_t *, void **);
250 
251 void uu_list_remove(uu_list_t *, void *);
252 
253 /*
254  * lists: interfaces for non-sorted lists only
255  */
256 int uu_list_insert_before(uu_list_t *, void *_target, void *_elem);
257 int uu_list_insert_after(uu_list_t *, void *_target, void *_elem);
258 
259 /*
260  * avl trees: opaque structures
261  */
262 typedef struct uu_avl_pool uu_avl_pool_t;
263 typedef struct uu_avl uu_avl_t;
264 
265 typedef struct uu_avl_node {
266 #ifdef _LP64
267 	uintptr_t uan_opaque[3];
268 #else
269 	uintptr_t uan_opaque[4];
270 #endif
271 } uu_avl_node_t;
272 
273 typedef struct uu_avl_walk uu_avl_walk_t;
274 
275 typedef uintptr_t uu_avl_index_t;
276 
277 /*
278  * avl trees: interface
279  *
280  * basic usage:
281  *	typedef struct foo {
282  *		...
283  *		uu_avl_node_t foo_node;
284  *		...
285  *	} foo_t;
286  *
287  *	static int
288  *	foo_compare(void *l_arg, void *r_arg, void *private)
289  *	{
290  *		foo_t *l = l_arg;
291  *		foo_t *r = r_arg;
292  *
293  *		if (... l greater than r ...)
294  *			return (1);
295  *		if (... l less than r ...)
296  *			return (-1);
297  *		return (0);
298  *	}
299  *
300  *	...
301  *		// at initialization time
302  *		foo_pool = uu_avl_pool_create("foo_pool",
303  *		    sizeof (foo_t), offsetof(foo_t, foo_node), foo_compare,
304  *		    debugging? 0 : UU_AVL_POOL_DEBUG);
305  *	...
306  */
307 uu_avl_pool_t *uu_avl_pool_create(const char *, size_t, size_t,
308     uu_compare_fn_t *, uint32_t);
309 #define	UU_AVL_POOL_DEBUG	0x00000001
310 
311 void uu_avl_pool_destroy(uu_avl_pool_t *);
312 
313 /*
314  * usage:
315  *
316  *	foo_t *a;
317  *	a = malloc(sizeof (*a));
318  *	uu_avl_node_init(a, &a->foo_avl, pool);
319  *	...
320  *	uu_avl_node_fini(a, &a->foo_avl, pool);
321  *	free(a);
322  */
323 void uu_avl_node_init(void *, uu_avl_node_t *, uu_avl_pool_t *);
324 void uu_avl_node_fini(void *, uu_avl_node_t *, uu_avl_pool_t *);
325 
326 uu_avl_t *uu_avl_create(uu_avl_pool_t *, void *_parent, uint32_t);
327 #define	UU_AVL_DEBUG	0x00000001
328 
329 void uu_avl_destroy(uu_avl_t *);	/* list must be empty */
330 
331 size_t uu_avl_numnodes(uu_avl_t *);
332 
333 void *uu_avl_first(uu_avl_t *);
334 void *uu_avl_last(uu_avl_t *);
335 
336 void *uu_avl_next(uu_avl_t *, void *);
337 void *uu_avl_prev(uu_avl_t *, void *);
338 
339 int uu_avl_walk(uu_avl_t *, uu_walk_fn_t *, void *, uint32_t);
340 
341 uu_avl_walk_t *uu_avl_walk_start(uu_avl_t *, uint32_t);
342 void *uu_avl_walk_next(uu_avl_walk_t *);
343 void uu_avl_walk_end(uu_avl_walk_t *);
344 
345 void *uu_avl_find(uu_avl_t *, void *, void *, uu_avl_index_t *);
346 void uu_avl_insert(uu_avl_t *, void *, uu_avl_index_t);
347 
348 void *uu_avl_nearest_next(uu_avl_t *, uu_avl_index_t);
349 void *uu_avl_nearest_prev(uu_avl_t *, uu_avl_index_t);
350 
351 void *uu_avl_teardown(uu_avl_t *, void **);
352 
353 void uu_avl_remove(uu_avl_t *, void *);
354 
355 #ifdef	__cplusplus
356 }
357 #endif
358 
359 #endif	/* _LIBUUTIL_H */
360