Parallel pool import
[zfs.git] / include / libuutil.h
blob906b49ea5ca929d6a0446a6e90ce1f87996a47e3
1 /*
2 * CDDL HEADER START
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.
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.
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]
19 * CDDL HEADER END
22 * Copyright (c) 2004, 2010, Oracle and/or its affiliates. All rights reserved.
25 #ifndef _LIBUUTIL_H
26 #define _LIBUUTIL_H
28 #include <sys/types.h>
29 #include <stdarg.h>
30 #include <stdio.h>
32 #ifdef __cplusplus
33 extern "C" {
34 #endif
37 * Standard flags codes.
39 #define UU_DEFAULT 0
42 * Standard error codes.
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 */
56 #define UU_ERROR_SYSTEM 99 /* underlying system error */
57 #define UU_ERROR_UNKNOWN 100 /* error status not known */
60 * Exit status profiles.
62 #define UU_PROFILE_DEFAULT 0
63 #define UU_PROFILE_LAUNCHER 1
66 * Error reporting functions.
68 uint32_t uu_error(void);
69 const char *uu_strerror(uint32_t);
72 * Identifier test flags and function.
74 #define UU_NAME_DOMAIN 0x1 /* allow SUNW, or com.sun, prefix */
75 #define UU_NAME_PATH 0x2 /* allow '/'-delimited paths */
77 int uu_check_name(const char *, uint_t);
80 * Convenience functions.
82 #define UU_NELEM(a) (sizeof (a) / sizeof ((a)[0]))
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 *);
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);
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.
101 typedef int uu_compare_fn_t(const void *__left, const void *__right,
102 void *__private);
105 * Walk variant flags.
106 * A data structure need not provide support for all variants and
107 * combinations. Refer to the appropriate documentation.
109 #define UU_WALK_ROBUST 0x00000001 /* walk can survive removes */
110 #define UU_WALK_REVERSE 0x00000002 /* reverse walk order */
112 #define UU_WALK_PREORDER 0x00000010 /* walk tree in pre-order */
113 #define UU_WALK_POSTORDER 0x00000020 /* walk tree in post-order */
116 * Walk callback function return codes.
118 #define UU_WALK_ERROR -1
119 #define UU_WALK_NEXT 0
120 #define UU_WALK_DONE 1
123 * Walk callback function type definition.
125 typedef int uu_walk_fn_t(void *_elem, void *_private);
128 * lists: opaque structures
130 typedef struct uu_list_pool uu_list_pool_t;
131 typedef struct uu_list uu_list_t;
133 typedef struct uu_list_node {
134 uintptr_t uln_opaque[2];
135 } uu_list_node_t;
137 typedef struct uu_list_walk uu_list_walk_t;
139 typedef uintptr_t uu_list_index_t;
142 * lists: interface
144 * basic usage:
145 * typedef struct foo {
146 * ...
147 * uu_list_node_t foo_node;
148 * ...
149 * } foo_t;
151 * static int
152 * foo_compare(void *l_arg, void *r_arg, void *private)
154 * foo_t *l = l_arg;
155 * foo_t *r = r_arg;
157 * if (... l greater than r ...)
158 * return (1);
159 * if (... l less than r ...)
160 * return (-1);
161 * return (0);
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 * ...
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
175 void uu_list_pool_destroy(uu_list_pool_t *);
178 * usage:
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);
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 *);
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 */
194 void uu_list_destroy(uu_list_t *); /* list must be empty */
196 size_t uu_list_numnodes(uu_list_t *);
198 void *uu_list_first(uu_list_t *);
199 void *uu_list_last(uu_list_t *);
201 void *uu_list_next(uu_list_t *, void *);
202 void *uu_list_prev(uu_list_t *, void *);
204 int uu_list_walk(uu_list_t *, uu_walk_fn_t *, void *, uint32_t);
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 *);
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);
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);
216 void *uu_list_teardown(uu_list_t *, void **);
218 void uu_list_remove(uu_list_t *, void *);
221 * lists: interfaces for non-sorted lists only
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);
227 * avl trees: opaque structures
229 typedef struct uu_avl_pool uu_avl_pool_t;
230 typedef struct uu_avl uu_avl_t;
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;
240 typedef struct uu_avl_walk uu_avl_walk_t;
242 typedef uintptr_t uu_avl_index_t;
245 * avl trees: interface
247 * basic usage:
248 * typedef struct foo {
249 * ...
250 * uu_avl_node_t foo_node;
251 * ...
252 * } foo_t;
254 * static int
255 * foo_compare(void *l_arg, void *r_arg, void *private)
257 * foo_t *l = l_arg;
258 * foo_t *r = r_arg;
260 * if (... l greater than r ...)
261 * return (1);
262 * if (... l less than r ...)
263 * return (-1);
264 * return (0);
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 * ...
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
278 void uu_avl_pool_destroy(uu_avl_pool_t *);
281 * usage:
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);
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 *);
293 uu_avl_t *uu_avl_create(uu_avl_pool_t *, void *_parent, uint32_t);
294 #define UU_AVL_DEBUG 0x00000001
296 void uu_avl_destroy(uu_avl_t *); /* list must be empty */
298 size_t uu_avl_numnodes(uu_avl_t *);
300 void *uu_avl_first(uu_avl_t *);
301 void *uu_avl_last(uu_avl_t *);
303 void *uu_avl_next(uu_avl_t *, void *);
304 void *uu_avl_prev(uu_avl_t *, void *);
306 int uu_avl_walk(uu_avl_t *, uu_walk_fn_t *, void *, uint32_t);
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 *);
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);
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);
318 void *uu_avl_teardown(uu_avl_t *, void **);
320 void uu_avl_remove(uu_avl_t *, void *);
322 #ifdef __cplusplus
324 #endif
326 #endif /* _LIBUUTIL_H */