]> CyberLeo.Net >> Repos - FreeBSD/releng/8.1.git/blob - cddl/contrib/opensolaris/lib/libuutil/common/libuutil.h
Copy stable/8 to releng/8.1 in preparation for 8.1-RC1.
[FreeBSD/releng/8.1.git] / cddl / contrib / opensolaris / lib / libuutil / common / libuutil.h
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 http://www.opensolaris.org/os/licensing.
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 2008 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25
26 #ifndef _LIBUUTIL_H
27 #define _LIBUUTIL_H
28
29 #include <solaris.h>
30 #include <sys/types.h>
31 #include <stdarg.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  * Standard program exit codes.
62  */
63 #define UU_EXIT_OK      (*(uu_exit_ok()))
64 #define UU_EXIT_FATAL   (*(uu_exit_fatal()))
65 #define UU_EXIT_USAGE   (*(uu_exit_usage()))
66
67 /*
68  * Exit status profiles.
69  */
70 #define UU_PROFILE_DEFAULT      0
71 #define UU_PROFILE_LAUNCHER     1
72
73 /*
74  * Error reporting functions.
75  */
76 uint32_t uu_error(void);
77 const char *uu_strerror(uint32_t);
78
79 /*
80  * Program notification functions.
81  */
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
95 /*
96  * Exit status functions (not to be used directly)
97  */
98 extern int *uu_exit_ok(void);
99 extern int *uu_exit_fatal(void);
100 extern int *uu_exit_usage(void);
101
102 /*
103  * string->number conversions
104  */
105 extern int uu_strtoint(const char *, void *, size_t, int, int64_t, int64_t);
106 extern int uu_strtouint(const char *, void *, size_t, int, uint64_t, uint64_t);
107
108 /*
109  * Debug print facility functions.
110  */
111 typedef struct uu_dprintf uu_dprintf_t;
112
113 typedef enum {
114         UU_DPRINTF_SILENT,
115         UU_DPRINTF_FATAL,
116         UU_DPRINTF_WARNING,
117         UU_DPRINTF_NOTICE,
118         UU_DPRINTF_INFO,
119         UU_DPRINTF_DEBUG
120 } uu_dprintf_severity_t;
121
122 extern uu_dprintf_t *uu_dprintf_create(const char *, uu_dprintf_severity_t,
123     uint_t);
124 /*PRINTFLIKE3*/
125 extern void uu_dprintf(uu_dprintf_t *, uu_dprintf_severity_t,
126     const char *, ...);
127 extern void uu_dprintf_destroy(uu_dprintf_t *);
128 extern const char *uu_dprintf_getname(uu_dprintf_t *);
129
130 /*
131  * Identifier test flags and function.
132  */
133 #define UU_NAME_DOMAIN          0x1     /* allow SUNW, or com.sun, prefix */
134 #define UU_NAME_PATH            0x2     /* allow '/'-delimited paths */
135
136 int uu_check_name(const char *, uint_t);
137
138 /*
139  * File creation functions.
140  */
141 extern int uu_open_tmp(const char *dir, uint_t uflags);
142
143 /*
144  * Convenience functions.
145  */
146 /*PRINTFLIKE1*/
147 extern char *uu_msprintf(const char *format, ...);
148 extern void *uu_zalloc(size_t);
149 extern char *uu_strdup(const char *);
150 extern void uu_free(void *);
151
152 /*
153  * Comparison function type definition.
154  *   Developers should be careful in their use of the _private argument. If you
155  *   break interface guarantees, you get undefined behavior.
156  */
157 typedef int uu_compare_fn_t(const void *__left, const void *__right,
158     void *__private);
159
160 /*
161  * Walk variant flags.
162  *   A data structure need not provide support for all variants and
163  *   combinations.  Refer to the appropriate documentation.
164  */
165 #define UU_WALK_ROBUST          0x00000001      /* walk can survive removes */
166 #define UU_WALK_REVERSE         0x00000002      /* reverse walk order */
167
168 #define UU_WALK_PREORDER        0x00000010      /* walk tree in pre-order */
169 #define UU_WALK_POSTORDER       0x00000020      /* walk tree in post-order */
170
171 /*
172  * Walk callback function return codes.
173  */
174 #define UU_WALK_ERROR           -1
175 #define UU_WALK_NEXT            0
176 #define UU_WALK_DONE            1
177
178 /*
179  * Walk callback function type definition.
180  */
181 typedef int uu_walk_fn_t(void *_elem, void *_private);
182
183 /*
184  * lists: opaque structures
185  */
186 typedef struct uu_list_pool uu_list_pool_t;
187 typedef struct uu_list uu_list_t;
188
189 typedef struct uu_list_node {
190         uintptr_t uln_opaque[2];
191 } uu_list_node_t;
192
193 typedef struct uu_list_walk uu_list_walk_t;
194
195 typedef uintptr_t uu_list_index_t;
196
197 /*
198  * lists: interface
199  *
200  * basic usage:
201  *      typedef struct foo {
202  *              ...
203  *              uu_list_node_t foo_node;
204  *              ...
205  *      } foo_t;
206  *
207  *      static int
208  *      foo_compare(void *l_arg, void *r_arg, void *private)
209  *      {
210  *              foo_t *l = l_arg;
211  *              foo_t *r = r_arg;
212  *
213  *              if (... l greater than r ...)
214  *                      return (1);
215  *              if (... l less than r ...)
216  *                      return (-1);
217  *              return (0);
218  *      }
219  *
220  *      ...
221  *              // at initialization time
222  *              foo_pool = uu_list_pool_create("foo_pool",
223  *                  sizeof (foo_t), offsetof(foo_t, foo_node), foo_compare,
224  *                  debugging? 0 : UU_AVL_POOL_DEBUG);
225  *      ...
226  */
227 uu_list_pool_t *uu_list_pool_create(const char *, size_t, size_t,
228     uu_compare_fn_t *, uint32_t);
229 #define UU_LIST_POOL_DEBUG      0x00000001
230
231 void uu_list_pool_destroy(uu_list_pool_t *);
232
233 /*
234  * usage:
235  *
236  *      foo_t *a;
237  *      a = malloc(sizeof(*a));
238  *      uu_list_node_init(a, &a->foo_list, pool);
239  *      ...
240  *      uu_list_node_fini(a, &a->foo_list, pool);
241  *      free(a);
242  */
243 void uu_list_node_init(void *, uu_list_node_t *, uu_list_pool_t *);
244 void uu_list_node_fini(void *, uu_list_node_t *, uu_list_pool_t *);
245
246 uu_list_t *uu_list_create(uu_list_pool_t *, void *_parent, uint32_t);
247 #define UU_LIST_DEBUG   0x00000001
248 #define UU_LIST_SORTED  0x00000002      /* list is sorted */
249
250 void uu_list_destroy(uu_list_t *);      /* list must be empty */
251
252 size_t uu_list_numnodes(uu_list_t *);
253
254 void *uu_list_first(uu_list_t *);
255 void *uu_list_last(uu_list_t *);
256
257 void *uu_list_next(uu_list_t *, void *);
258 void *uu_list_prev(uu_list_t *, void *);
259
260 int uu_list_walk(uu_list_t *, uu_walk_fn_t *, void *, uint32_t);
261
262 uu_list_walk_t *uu_list_walk_start(uu_list_t *, uint32_t);
263 void *uu_list_walk_next(uu_list_walk_t *);
264 void uu_list_walk_end(uu_list_walk_t *);
265
266 void *uu_list_find(uu_list_t *, void *, void *, uu_list_index_t *);
267 void uu_list_insert(uu_list_t *, void *, uu_list_index_t);
268
269 void *uu_list_nearest_next(uu_list_t *, uu_list_index_t);
270 void *uu_list_nearest_prev(uu_list_t *, uu_list_index_t);
271
272 void *uu_list_teardown(uu_list_t *, void **);
273
274 void uu_list_remove(uu_list_t *, void *);
275
276 /*
277  * lists: interfaces for non-sorted lists only
278  */
279 int uu_list_insert_before(uu_list_t *, void *_target, void *_elem);
280 int uu_list_insert_after(uu_list_t *, void *_target, void *_elem);
281
282 /*
283  * avl trees: opaque structures
284  */
285 typedef struct uu_avl_pool uu_avl_pool_t;
286 typedef struct uu_avl uu_avl_t;
287
288 typedef struct uu_avl_node {
289 #ifdef _LP64
290         uintptr_t uan_opaque[3];
291 #else
292         uintptr_t uan_opaque[4];
293 #endif
294 } uu_avl_node_t;
295
296 typedef struct uu_avl_walk uu_avl_walk_t;
297
298 typedef uintptr_t uu_avl_index_t;
299
300 /*
301  * avl trees: interface
302  *
303  * basic usage:
304  *      typedef struct foo {
305  *              ...
306  *              uu_avl_node_t foo_node;
307  *              ...
308  *      } foo_t;
309  *
310  *      static int
311  *      foo_compare(void *l_arg, void *r_arg, void *private)
312  *      {
313  *              foo_t *l = l_arg;
314  *              foo_t *r = r_arg;
315  *
316  *              if (... l greater than r ...)
317  *                      return (1);
318  *              if (... l less than r ...)
319  *                      return (-1);
320  *              return (0);
321  *      }
322  *
323  *      ...
324  *              // at initialization time
325  *              foo_pool = uu_avl_pool_create("foo_pool",
326  *                  sizeof (foo_t), offsetof(foo_t, foo_node), foo_compare,
327  *                  debugging? 0 : UU_AVL_POOL_DEBUG);
328  *      ...
329  */
330 uu_avl_pool_t *uu_avl_pool_create(const char *, size_t, size_t,
331     uu_compare_fn_t *, uint32_t);
332 #define UU_AVL_POOL_DEBUG       0x00000001
333
334 void uu_avl_pool_destroy(uu_avl_pool_t *);
335
336 /*
337  * usage:
338  *
339  *      foo_t *a;
340  *      a = malloc(sizeof(*a));
341  *      uu_avl_node_init(a, &a->foo_avl, pool);
342  *      ...
343  *      uu_avl_node_fini(a, &a->foo_avl, pool);
344  *      free(a);
345  */
346 void uu_avl_node_init(void *, uu_avl_node_t *, uu_avl_pool_t *);
347 void uu_avl_node_fini(void *, uu_avl_node_t *, uu_avl_pool_t *);
348
349 uu_avl_t *uu_avl_create(uu_avl_pool_t *, void *_parent, uint32_t);
350 #define UU_AVL_DEBUG    0x00000001
351
352 void uu_avl_destroy(uu_avl_t *);        /* list must be empty */
353
354 size_t uu_avl_numnodes(uu_avl_t *);
355
356 void *uu_avl_first(uu_avl_t *);
357 void *uu_avl_last(uu_avl_t *);
358
359 void *uu_avl_next(uu_avl_t *, void *);
360 void *uu_avl_prev(uu_avl_t *, void *);
361
362 int uu_avl_walk(uu_avl_t *, uu_walk_fn_t *, void *, uint32_t);
363
364 uu_avl_walk_t *uu_avl_walk_start(uu_avl_t *, uint32_t);
365 void *uu_avl_walk_next(uu_avl_walk_t *);
366 void uu_avl_walk_end(uu_avl_walk_t *);
367
368 void *uu_avl_find(uu_avl_t *, void *, void *, uu_avl_index_t *);
369 void uu_avl_insert(uu_avl_t *, void *, uu_avl_index_t);
370
371 void *uu_avl_nearest_next(uu_avl_t *, uu_avl_index_t);
372 void *uu_avl_nearest_prev(uu_avl_t *, uu_avl_index_t);
373
374 void *uu_avl_teardown(uu_avl_t *, void **);
375
376 void uu_avl_remove(uu_avl_t *, void *);
377
378 #ifdef  __cplusplus
379 }
380 #endif
381
382 #endif  /* _LIBUUTIL_H */