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