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