summaryrefslogtreecommitdiffstats
path: root/lib/libuutil/include/libuutil.h
blob: ccd46b97748e7930bcdfd7c123d3b7d6dbb14533 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

#ifndef	_LIBUUTIL_H
#define	_LIBUUTIL_H

#include <sys/types.h>
#include <stdarg.h>

#ifdef	__cplusplus
extern "C" {
#endif

/*
 * Standard flags codes.
 */
#define	UU_DEFAULT		0

/*
 * Standard error codes.
 */
#define	UU_ERROR_NONE		0	/* no error */
#define	UU_ERROR_INVALID_ARGUMENT 1	/* invalid argument */
#define	UU_ERROR_UNKNOWN_FLAG	2	/* passed flag invalid */
#define	UU_ERROR_NO_MEMORY	3	/* out of memory */
#define	UU_ERROR_CALLBACK_FAILED 4	/* callback-initiated error */
#define	UU_ERROR_NOT_SUPPORTED	5	/* operation not supported */
#define	UU_ERROR_EMPTY		6	/* no value provided */
#define	UU_ERROR_UNDERFLOW	7	/* value is too small */
#define	UU_ERROR_OVERFLOW	8	/* value is too value */
#define	UU_ERROR_INVALID_CHAR	9	/* value contains unexpected char */
#define	UU_ERROR_INVALID_DIGIT	10	/* value contains digit not in base */

#define	UU_ERROR_SYSTEM		99	/* underlying system error */
#define	UU_ERROR_UNKNOWN	100	/* error status not known */

/*
 * Standard program exit codes.
 */
#define	UU_EXIT_OK	(*(uu_exit_ok()))
#define	UU_EXIT_FATAL	(*(uu_exit_fatal()))
#define	UU_EXIT_USAGE	(*(uu_exit_usage()))

/*
 * Exit status profiles.
 */
#define	UU_PROFILE_DEFAULT	0
#define	UU_PROFILE_LAUNCHER	1

/*
 * Error reporting functions.
 */
uint32_t uu_error(void);
const char *uu_strerror(uint32_t);

/*
 * Program notification functions.
 */
extern void uu_alt_exit(int);
extern const char *uu_setpname(char *);
extern const char *uu_getpname(void);
/*PRINTFLIKE1*/
extern void uu_warn(const char *, ...);
extern void uu_vwarn(const char *, va_list);
/*PRINTFLIKE1*/
extern void uu_die(const char *, ...) __NORETURN;
extern void uu_vdie(const char *, va_list) __NORETURN;
/*PRINTFLIKE2*/
extern void uu_xdie(int, const char *, ...) __NORETURN;
extern void uu_vxdie(int, const char *, va_list) __NORETURN;

/*
 * Exit status functions (not to be used directly)
 */
extern int *uu_exit_ok(void);
extern int *uu_exit_fatal(void);
extern int *uu_exit_usage(void);

/*
 * string->number conversions
 */
extern int uu_strtoint(const char *, void *, size_t, int, int64_t, int64_t);
extern int uu_strtouint(const char *, void *, size_t, int, uint64_t, uint64_t);

/*
 * Debug print facility functions.
 */
typedef struct uu_dprintf uu_dprintf_t;

typedef enum {
	UU_DPRINTF_SILENT,
	UU_DPRINTF_FATAL,
	UU_DPRINTF_WARNING,
	UU_DPRINTF_NOTICE,
	UU_DPRINTF_INFO,
	UU_DPRINTF_DEBUG
} uu_dprintf_severity_t;

extern uu_dprintf_t *uu_dprintf_create(const char *, uu_dprintf_severity_t,
    uint_t);
/*PRINTFLIKE3*/
extern void uu_dprintf(uu_dprintf_t *, uu_dprintf_severity_t,
    const char *, ...);
extern void uu_dprintf_destroy(uu_dprintf_t *);
extern const char *uu_dprintf_getname(uu_dprintf_t *);

/*
 * Identifier test flags and function.
 */
#define	UU_NAME_DOMAIN		0x1	/* allow SUNW, or com.sun, prefix */
#define	UU_NAME_PATH		0x2	/* allow '/'-delimited paths */

int uu_check_name(const char *, uint_t);

/*
 * File creation functions.
 */
extern int uu_open_tmp(const char *dir, uint_t uflags);

/*
 * Convenience functions.
 */
/*PRINTFLIKE1*/
extern char *uu_msprintf(const char *format, ...);
extern void *uu_zalloc(size_t);
extern char *uu_strdup(const char *);
extern void uu_free(void *);

/*
 * Comparison function type definition.
 *   Developers should be careful in their use of the _private argument. If you
 *   break interface guarantees, you get undefined behavior.
 */
typedef int uu_compare_fn_t(const void *__left, const void *__right,
    void *__private);

/*
 * Walk variant flags.
 *   A data structure need not provide support for all variants and
 *   combinations.  Refer to the appropriate documentation.
 */
#define	UU_WALK_ROBUST		0x00000001	/* walk can survive removes */
#define	UU_WALK_REVERSE		0x00000002	/* reverse walk order */

#define	UU_WALK_PREORDER	0x00000010	/* walk tree in pre-order */
#define	UU_WALK_POSTORDER	0x00000020	/* walk tree in post-order */

/*
 * Walk callback function return codes.
 */
#define	UU_WALK_ERROR		-1
#define	UU_WALK_NEXT		0
#define	UU_WALK_DONE		1

/*
 * Walk callback function type definition.
 */
typedef int uu_walk_fn_t(void *_elem, void *_private);

/*
 * lists: opaque structures
 */
typedef struct uu_list_pool uu_list_pool_t;
typedef struct uu_list uu_list_t;

typedef struct uu_list_node {
	uintptr_t uln_opaque[2];
} uu_list_node_t;

typedef struct uu_list_walk uu_list_walk_t;

typedef uintptr_t uu_list_index_t;

/*
 * lists: interface
 *
 * basic usage:
 *	typedef struct foo {
 *		...
 *		uu_list_node_t foo_node;
 *		...
 *	} foo_t;
 *
 *	static int
 *	foo_compare(void *l_arg, void *r_arg, void *private)
 *	{
 *		foo_t *l = l_arg;
 *		foo_t *r = r_arg;
 *
 *		if (... l greater than r ...)
 *			return (1);
 *		if (... l less than r ...)
 *			return (-1);
 *		return (0);
 *	}
 *
 *	...
 *		// at initialization time
 *		foo_pool = uu_list_pool_create("foo_pool",
 *		    sizeof (foo_t), offsetof(foo_t, foo_node), foo_compare,
 *		    debugging? 0 : UU_AVL_POOL_DEBUG);
 *	...
 */
uu_list_pool_t *uu_list_pool_create(const char *, size_t, size_t,
    uu_compare_fn_t *, uint32_t);
#define	UU_LIST_POOL_DEBUG	0x00000001

void uu_list_pool_destroy(uu_list_pool_t *);

/*
 * usage:
 *
 *	foo_t *a;
 *	a = malloc(sizeof(*a));
 *	uu_list_node_init(a, &a->foo_list, pool);
 *	...
 *	uu_list_node_fini(a, &a->foo_list, pool);
 *	free(a);
 */
void uu_list_node_init(void *, uu_list_node_t *, uu_list_pool_t *);
void uu_list_node_fini(void *, uu_list_node_t *, uu_list_pool_t *);

uu_list_t *uu_list_create(uu_list_pool_t *, void *_parent, uint32_t);
#define	UU_LIST_DEBUG	0x00000001
#define	UU_LIST_SORTED	0x00000002	/* list is sorted */

void uu_list_destroy(uu_list_t *);	/* list must be empty */

size_t uu_list_numnodes(uu_list_t *);

void *uu_list_first(uu_list_t *);
void *uu_list_last(uu_list_t *);

void *uu_list_next(uu_list_t *, void *);
void *uu_list_prev(uu_list_t *, void *);

int uu_list_walk(uu_list_t *, uu_walk_fn_t *, void *, uint32_t);

uu_list_walk_t *uu_list_walk_start(uu_list_t *, uint32_t);
void *uu_list_walk_next(uu_list_walk_t *);
void uu_list_walk_end(uu_list_walk_t *);

void *uu_list_find(uu_list_t *, void *, void *, uu_list_index_t *);
void uu_list_insert(uu_list_t *, void *, uu_list_index_t);

void *uu_list_nearest_next(uu_list_t *, uu_list_index_t);
void *uu_list_nearest_prev(uu_list_t *, uu_list_index_t);

void *uu_list_teardown(uu_list_t *, void **);

void uu_list_remove(uu_list_t *, void *);

/*
 * lists: interfaces for non-sorted lists only
 */
int uu_list_insert_before(uu_list_t *, void *_target, void *_elem);
int uu_list_insert_after(uu_list_t *, void *_target, void *_elem);

/*
 * avl trees: opaque structures
 */
typedef struct uu_avl_pool uu_avl_pool_t;
typedef struct uu_avl uu_avl_t;

typedef struct uu_avl_node {
#ifdef _LP64
	uintptr_t uan_opaque[3];
#else
	uintptr_t uan_opaque[4];
#endif
} uu_avl_node_t;

typedef struct uu_avl_walk uu_avl_walk_t;

typedef uintptr_t uu_avl_index_t;

/*
 * avl trees: interface
 *
 * basic usage:
 *	typedef struct foo {
 *		...
 *		uu_avl_node_t foo_node;
 *		...
 *	} foo_t;
 *
 *	static int
 *	foo_compare(void *l_arg, void *r_arg, void *private)
 *	{
 *		foo_t *l = l_arg;
 *		foo_t *r = r_arg;
 *
 *		if (... l greater than r ...)
 *			return (1);
 *		if (... l less than r ...)
 *			return (-1);
 *		return (0);
 *	}
 *
 *	...
 *		// at initialization time
 *		foo_pool = uu_avl_pool_create("foo_pool",
 *		    sizeof (foo_t), offsetof(foo_t, foo_node), foo_compare,
 *		    debugging? 0 : UU_AVL_POOL_DEBUG);
 *	...
 */
uu_avl_pool_t *uu_avl_pool_create(const char *, size_t, size_t,
    uu_compare_fn_t *, uint32_t);
#define	UU_AVL_POOL_DEBUG	0x00000001

void uu_avl_pool_destroy(uu_avl_pool_t *);

/*
 * usage:
 *
 *	foo_t *a;
 *	a = malloc(sizeof(*a));
 *	uu_avl_node_init(a, &a->foo_avl, pool);
 *	...
 *	uu_avl_node_fini(a, &a->foo_avl, pool);
 *	free(a);
 */
void uu_avl_node_init(void *, uu_avl_node_t *, uu_avl_pool_t *);
void uu_avl_node_fini(void *, uu_avl_node_t *, uu_avl_pool_t *);

uu_avl_t *uu_avl_create(uu_avl_pool_t *, void *_parent, uint32_t);
#define	UU_AVL_DEBUG	0x00000001

void uu_avl_destroy(uu_avl_t *);	/* list must be empty */

size_t uu_avl_numnodes(uu_avl_t *);

void *uu_avl_first(uu_avl_t *);
void *uu_avl_last(uu_avl_t *);

void *uu_avl_next(uu_avl_t *, void *);
void *uu_avl_prev(uu_avl_t *, void *);

int uu_avl_walk(uu_avl_t *, uu_walk_fn_t *, void *, uint32_t);

uu_avl_walk_t *uu_avl_walk_start(uu_avl_t *, uint32_t);
void *uu_avl_walk_next(uu_avl_walk_t *);
void uu_avl_walk_end(uu_avl_walk_t *);

void *uu_avl_find(uu_avl_t *, void *, void *, uu_avl_index_t *);
void uu_avl_insert(uu_avl_t *, void *, uu_avl_index_t);

void *uu_avl_nearest_next(uu_avl_t *, uu_avl_index_t);
void *uu_avl_nearest_prev(uu_avl_t *, uu_avl_index_t);

void *uu_avl_teardown(uu_avl_t *, void **);

void uu_avl_remove(uu_avl_t *, void *);

#ifdef	__cplusplus
}
#endif

#endif	/* _LIBUUTIL_H */