]> git.proxmox.com Git - mirror_zfs.git/blame - include/libuutil.h
etc/init.d: decide which variant to use at build time.
[mirror_zfs.git] / include / libuutil.h
CommitLineData
34dc7c2f
BB
1/*
2 * CDDL HEADER START
3 *
4 * The contents of this file are subject to the terms of the
b128c09f
BB
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
34dc7c2f
BB
7 *
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
1d3ba0bf 9 * or https://opensource.org/licenses/CDDL-1.0.
34dc7c2f
BB
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/*
572e2857 22 * Copyright (c) 2004, 2010, Oracle and/or its affiliates. All rights reserved.
34dc7c2f
BB
23 */
24
25#ifndef _LIBUUTIL_H
26#define _LIBUUTIL_H
27
34dc7c2f
BB
28#include <sys/types.h>
29#include <stdarg.h>
572e2857 30#include <stdio.h>
34dc7c2f
BB
31
32#ifdef __cplusplus
33extern "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
34dc7c2f
BB
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 */
68uint32_t uu_error(void);
69const char *uu_strerror(uint32_t);
70
34dc7c2f
BB
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
77int uu_check_name(const char *, uint_t);
78
34dc7c2f
BB
79/*
80 * Convenience functions.
81 */
572e2857
BB
82#define UU_NELEM(a) (sizeof (a) / sizeof ((a)[0]))
83
5dbf6c5a
AZ
84extern char *uu_msprintf(const char *format, ...)
85 __attribute__((format(printf, 1, 2)));
34dc7c2f 86extern void *uu_zalloc(size_t);
b128c09f 87extern char *uu_strdup(const char *);
34dc7c2f
BB
88extern void uu_free(void *);
89
572e2857
BB
90extern boolean_t uu_strcaseeq(const char *a, const char *b);
91extern boolean_t uu_streq(const char *a, const char *b);
92extern char *uu_strndup(const char *s, size_t n);
93extern boolean_t uu_strbw(const char *a, const char *b);
94extern void *uu_memdup(const void *buf, size_t sz);
572e2857 95
34dc7c2f
BB
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 */
101typedef 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 */
125typedef int uu_walk_fn_t(void *_elem, void *_private);
126
127/*
128 * lists: opaque structures
129 */
130typedef struct uu_list_pool uu_list_pool_t;
131typedef struct uu_list uu_list_t;
132
133typedef struct uu_list_node {
134 uintptr_t uln_opaque[2];
135} uu_list_node_t;
136
137typedef struct uu_list_walk uu_list_walk_t;
138
139typedef 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 */
171uu_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
175void uu_list_pool_destroy(uu_list_pool_t *);
176
177/*
178 * usage:
179 *
180 * foo_t *a;
b5256303 181 * a = malloc(sizeof (*a));
34dc7c2f
BB
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 */
187void uu_list_node_init(void *, uu_list_node_t *, uu_list_pool_t *);
188void uu_list_node_fini(void *, uu_list_node_t *, uu_list_pool_t *);
189
190uu_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
194void uu_list_destroy(uu_list_t *); /* list must be empty */
195
196size_t uu_list_numnodes(uu_list_t *);
197
198void *uu_list_first(uu_list_t *);
199void *uu_list_last(uu_list_t *);
200
201void *uu_list_next(uu_list_t *, void *);
202void *uu_list_prev(uu_list_t *, void *);
203
204int uu_list_walk(uu_list_t *, uu_walk_fn_t *, void *, uint32_t);
205
206uu_list_walk_t *uu_list_walk_start(uu_list_t *, uint32_t);
207void *uu_list_walk_next(uu_list_walk_t *);
208void uu_list_walk_end(uu_list_walk_t *);
209
210void *uu_list_find(uu_list_t *, void *, void *, uu_list_index_t *);
211void uu_list_insert(uu_list_t *, void *, uu_list_index_t);
212
213void *uu_list_nearest_next(uu_list_t *, uu_list_index_t);
214void *uu_list_nearest_prev(uu_list_t *, uu_list_index_t);
215
216void *uu_list_teardown(uu_list_t *, void **);
217
218void uu_list_remove(uu_list_t *, void *);
219
220/*
221 * lists: interfaces for non-sorted lists only
222 */
223int uu_list_insert_before(uu_list_t *, void *_target, void *_elem);
224int uu_list_insert_after(uu_list_t *, void *_target, void *_elem);
225
226/*
227 * avl trees: opaque structures
228 */
229typedef struct uu_avl_pool uu_avl_pool_t;
230typedef struct uu_avl uu_avl_t;
231
232typedef 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
240typedef struct uu_avl_walk uu_avl_walk_t;
241
242typedef 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 */
274uu_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
278void uu_avl_pool_destroy(uu_avl_pool_t *);
279
280/*
281 * usage:
282 *
283 * foo_t *a;
b5256303 284 * a = malloc(sizeof (*a));
34dc7c2f
BB
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 */
290void uu_avl_node_init(void *, uu_avl_node_t *, uu_avl_pool_t *);
291void uu_avl_node_fini(void *, uu_avl_node_t *, uu_avl_pool_t *);
292
293uu_avl_t *uu_avl_create(uu_avl_pool_t *, void *_parent, uint32_t);
294#define UU_AVL_DEBUG 0x00000001
295
296void uu_avl_destroy(uu_avl_t *); /* list must be empty */
297
298size_t uu_avl_numnodes(uu_avl_t *);
299
300void *uu_avl_first(uu_avl_t *);
301void *uu_avl_last(uu_avl_t *);
302
303void *uu_avl_next(uu_avl_t *, void *);
304void *uu_avl_prev(uu_avl_t *, void *);
305
306int uu_avl_walk(uu_avl_t *, uu_walk_fn_t *, void *, uint32_t);
307
308uu_avl_walk_t *uu_avl_walk_start(uu_avl_t *, uint32_t);
309void *uu_avl_walk_next(uu_avl_walk_t *);
310void uu_avl_walk_end(uu_avl_walk_t *);
311
312void *uu_avl_find(uu_avl_t *, void *, void *, uu_avl_index_t *);
313void uu_avl_insert(uu_avl_t *, void *, uu_avl_index_t);
314
315void *uu_avl_nearest_next(uu_avl_t *, uu_avl_index_t);
316void *uu_avl_nearest_prev(uu_avl_t *, uu_avl_index_t);
317
318void *uu_avl_teardown(uu_avl_t *, void **);
319
320void uu_avl_remove(uu_avl_t *, void *);
321
322#ifdef __cplusplus
323}
324#endif
325
326#endif /* _LIBUUTIL_H */