stmf: avoid set but not used errors
[unleashed.git] / lib / libuutil / libuutil.h
blobec1bf907c1abd9851710a500e42fe98e4a4fbe65
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 http://www.opensolaris.org/os/licensing.
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.
23 * Copyright (c) 2015, Joyent, Inc. All rights reserved.
26 #ifndef _LIBUUTIL_H
27 #define _LIBUUTIL_H
29 #include <sys/types.h>
30 #include <stdarg.h>
31 #include <stdio.h>
33 #ifdef __cplusplus
34 extern "C" {
35 #endif
38 * Standard flags codes.
40 #define UU_DEFAULT 0
43 * Standard error codes.
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 */
57 #define UU_ERROR_SYSTEM 99 /* underlying system error */
58 #define UU_ERROR_UNKNOWN 100 /* error status not known */
61 * Standard program exit codes.
63 #define UU_EXIT_OK (*(uu_exit_ok()))
64 #define UU_EXIT_FATAL (*(uu_exit_fatal()))
65 #define UU_EXIT_USAGE (*(uu_exit_usage()))
68 * Exit status profiles.
70 #define UU_PROFILE_DEFAULT 0
71 #define UU_PROFILE_LAUNCHER 1
74 * Error reporting functions.
76 uint32_t uu_error(void);
77 const char *uu_strerror(uint32_t);
80 * Program notification functions.
82 extern void uu_alt_exit(int);
83 extern const char *uu_setpname(char *);
84 extern const char *uu_getpname(void);
85 /*PRINTFLIKE1*/
86 extern void uu_warn(const char *, ...);
87 extern void uu_vwarn(const char *, va_list);
88 /*PRINTFLIKE1*/
89 extern void uu_die(const char *, ...) __NORETURN;
90 extern void uu_vdie(const char *, va_list) __NORETURN;
91 /*PRINTFLIKE2*/
92 extern void uu_xdie(int, const char *, ...) __NORETURN;
93 extern void uu_vxdie(int, const char *, va_list) __NORETURN;
94 /*PRINTFLIKE1*/
95 extern void uu_panic(const char *, ...) __NORETURN;
98 * Exit status functions (not to be used directly)
100 extern int *uu_exit_ok(void);
101 extern int *uu_exit_fatal(void);
102 extern int *uu_exit_usage(void);
105 * string->number conversions
107 extern int uu_strtoint(const char *, void *, size_t, int, int64_t, int64_t);
108 extern int uu_strtouint(const char *, void *, size_t, int, uint64_t, uint64_t);
111 * Debug print facility functions.
113 typedef struct uu_dprintf uu_dprintf_t;
115 typedef enum {
116 UU_DPRINTF_SILENT,
117 UU_DPRINTF_FATAL,
118 UU_DPRINTF_WARNING,
119 UU_DPRINTF_NOTICE,
120 UU_DPRINTF_INFO,
121 UU_DPRINTF_DEBUG
122 } uu_dprintf_severity_t;
124 extern uu_dprintf_t *uu_dprintf_create(const char *, uu_dprintf_severity_t,
125 uint_t);
126 /*PRINTFLIKE3*/
127 extern void uu_dprintf(uu_dprintf_t *, uu_dprintf_severity_t,
128 const char *, ...);
129 extern void uu_dprintf_destroy(uu_dprintf_t *);
130 extern const char *uu_dprintf_getname(uu_dprintf_t *);
133 * Identifier test flags and function.
135 #define UU_NAME_DOMAIN 0x1 /* allow SUNW, or com.sun, prefix */
136 #define UU_NAME_PATH 0x2 /* allow '/'-delimited paths */
138 int uu_check_name(const char *, uint_t);
141 * File creation functions.
143 extern int uu_open_tmp(const char *dir, uint_t uflags);
146 * Convenience functions.
148 #define UU_NELEM(a) (sizeof (a) / sizeof ((a)[0]))
150 /*PRINTFLIKE1*/
151 extern char *uu_msprintf(const char *format, ...);
152 extern void *uu_zalloc(size_t);
153 extern char *uu_strdup(const char *);
154 extern void uu_free(void *);
156 extern boolean_t uu_strcaseeq(const char *a, const char *b);
157 extern boolean_t uu_streq(const char *a, const char *b);
158 extern char *uu_strndup(const char *s, size_t n);
159 extern boolean_t uu_strbw(const char *a, const char *b);
160 extern void *uu_memdup(const void *buf, size_t sz);
161 extern void uu_dump(FILE *out, const char *prefix, const void *buf, size_t len);
164 * Comparison function type definition.
165 * Developers should be careful in their use of the _private argument. If you
166 * break interface guarantees, you get undefined behavior.
168 typedef int uu_compare_fn_t(const void *__left, const void *__right,
169 void *__private);
172 * Walk variant flags.
173 * A data structure need not provide support for all variants and
174 * combinations. Refer to the appropriate documentation.
176 #define UU_WALK_ROBUST 0x00000001 /* walk can survive removes */
177 #define UU_WALK_REVERSE 0x00000002 /* reverse walk order */
179 #define UU_WALK_PREORDER 0x00000010 /* walk tree in pre-order */
180 #define UU_WALK_POSTORDER 0x00000020 /* walk tree in post-order */
183 * Walk callback function return codes.
185 #define UU_WALK_ERROR -1
186 #define UU_WALK_NEXT 0
187 #define UU_WALK_DONE 1
190 * Walk callback function type definition.
192 typedef int uu_walk_fn_t(void *_elem, void *_private);
195 * lists: opaque structures
197 typedef struct uu_list_pool uu_list_pool_t;
198 typedef struct uu_list uu_list_t;
200 typedef struct uu_list_node {
201 uintptr_t uln_opaque[2];
202 } uu_list_node_t;
204 typedef struct uu_list_walk uu_list_walk_t;
206 typedef uintptr_t uu_list_index_t;
209 * lists: interface
211 * basic usage:
212 * typedef struct foo {
213 * ...
214 * uu_list_node_t foo_node;
215 * ...
216 * } foo_t;
218 * static int
219 * foo_compare(void *l_arg, void *r_arg, void *private)
221 * foo_t *l = l_arg;
222 * foo_t *r = r_arg;
224 * if (... l greater than r ...)
225 * return (1);
226 * if (... l less than r ...)
227 * return (-1);
228 * return (0);
231 * ...
232 * // at initialization time
233 * foo_pool = uu_list_pool_create("foo_pool",
234 * sizeof (foo_t), offsetof(foo_t, foo_node), foo_compare,
235 * debugging? 0 : UU_AVL_POOL_DEBUG);
236 * ...
238 uu_list_pool_t *uu_list_pool_create(const char *, size_t, size_t,
239 uu_compare_fn_t *, uint32_t);
240 #define UU_LIST_POOL_DEBUG 0x00000001
242 void uu_list_pool_destroy(uu_list_pool_t *);
245 * usage:
247 * foo_t *a;
248 * a = malloc(sizeof(*a));
249 * uu_list_node_init(a, &a->foo_list, pool);
250 * ...
251 * uu_list_node_fini(a, &a->foo_list, pool);
252 * free(a);
254 void uu_list_node_init(void *, uu_list_node_t *, uu_list_pool_t *);
255 void uu_list_node_fini(void *, uu_list_node_t *, uu_list_pool_t *);
257 uu_list_t *uu_list_create(uu_list_pool_t *, void *_parent, uint32_t);
258 #define UU_LIST_DEBUG 0x00000001
259 #define UU_LIST_SORTED 0x00000002 /* list is sorted */
261 void uu_list_destroy(uu_list_t *); /* list must be empty */
263 size_t uu_list_numnodes(uu_list_t *);
265 void *uu_list_first(uu_list_t *);
266 void *uu_list_last(uu_list_t *);
268 void *uu_list_next(uu_list_t *, void *);
269 void *uu_list_prev(uu_list_t *, void *);
271 int uu_list_walk(uu_list_t *, uu_walk_fn_t *, void *, uint32_t);
273 uu_list_walk_t *uu_list_walk_start(uu_list_t *, uint32_t);
274 void *uu_list_walk_next(uu_list_walk_t *);
275 void uu_list_walk_end(uu_list_walk_t *);
277 void *uu_list_find(uu_list_t *, void *, void *, uu_list_index_t *);
278 void uu_list_insert(uu_list_t *, void *, uu_list_index_t);
280 void *uu_list_nearest_next(uu_list_t *, uu_list_index_t);
281 void *uu_list_nearest_prev(uu_list_t *, uu_list_index_t);
283 void *uu_list_teardown(uu_list_t *, void **);
285 void uu_list_remove(uu_list_t *, void *);
288 * lists: interfaces for non-sorted lists only
290 int uu_list_insert_before(uu_list_t *, void *_target, void *_elem);
291 int uu_list_insert_after(uu_list_t *, void *_target, void *_elem);
294 * avl trees: opaque structures
296 typedef struct uu_avl_pool uu_avl_pool_t;
297 typedef struct uu_avl uu_avl_t;
299 typedef struct uu_avl_node {
300 #ifdef _LP64
301 uintptr_t uan_opaque[3];
302 #else
303 uintptr_t uan_opaque[4];
304 #endif
305 } uu_avl_node_t;
307 typedef struct uu_avl_walk uu_avl_walk_t;
309 typedef uintptr_t uu_avl_index_t;
312 * avl trees: interface
314 * basic usage:
315 * typedef struct foo {
316 * ...
317 * uu_avl_node_t foo_node;
318 * ...
319 * } foo_t;
321 * static int
322 * foo_compare(void *l_arg, void *r_arg, void *private)
324 * foo_t *l = l_arg;
325 * foo_t *r = r_arg;
327 * if (... l greater than r ...)
328 * return (1);
329 * if (... l less than r ...)
330 * return (-1);
331 * return (0);
334 * ...
335 * // at initialization time
336 * foo_pool = uu_avl_pool_create("foo_pool",
337 * sizeof (foo_t), offsetof(foo_t, foo_node), foo_compare,
338 * debugging? 0 : UU_AVL_POOL_DEBUG);
339 * ...
341 uu_avl_pool_t *uu_avl_pool_create(const char *, size_t, size_t,
342 uu_compare_fn_t *, uint32_t);
343 #define UU_AVL_POOL_DEBUG 0x00000001
345 void uu_avl_pool_destroy(uu_avl_pool_t *);
348 * usage:
350 * foo_t *a;
351 * a = malloc(sizeof(*a));
352 * uu_avl_node_init(a, &a->foo_avl, pool);
353 * ...
354 * uu_avl_node_fini(a, &a->foo_avl, pool);
355 * free(a);
357 void uu_avl_node_init(void *, uu_avl_node_t *, uu_avl_pool_t *);
358 void uu_avl_node_fini(void *, uu_avl_node_t *, uu_avl_pool_t *);
360 uu_avl_t *uu_avl_create(uu_avl_pool_t *, void *_parent, uint32_t);
361 #define UU_AVL_DEBUG 0x00000001
363 void uu_avl_destroy(uu_avl_t *); /* list must be empty */
365 size_t uu_avl_numnodes(uu_avl_t *);
367 void *uu_avl_first(uu_avl_t *);
368 void *uu_avl_last(uu_avl_t *);
370 void *uu_avl_next(uu_avl_t *, void *);
371 void *uu_avl_prev(uu_avl_t *, void *);
373 int uu_avl_walk(uu_avl_t *, uu_walk_fn_t *, void *, uint32_t);
375 uu_avl_walk_t *uu_avl_walk_start(uu_avl_t *, uint32_t);
376 void *uu_avl_walk_next(uu_avl_walk_t *);
377 void uu_avl_walk_end(uu_avl_walk_t *);
379 void *uu_avl_find(uu_avl_t *, void *, void *, uu_avl_index_t *);
380 void uu_avl_insert(uu_avl_t *, void *, uu_avl_index_t);
382 void *uu_avl_nearest_next(uu_avl_t *, uu_avl_index_t);
383 void *uu_avl_nearest_prev(uu_avl_t *, uu_avl_index_t);
385 void *uu_avl_teardown(uu_avl_t *, void **);
387 void uu_avl_remove(uu_avl_t *, void *);
389 #ifdef __cplusplus
391 #endif
393 #endif /* _LIBUUTIL_H */