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 /*
23  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #ifndef _UTIL_H
28 #define	_UTIL_H
29 
30 /*
31  * Utility functions and macros
32  */
33 
34 #ifdef	__cplusplus
35 extern "C" {
36 #endif
37 
38 #include <stdio.h>
39 #include <stdarg.h>
40 #include <time.h>
41 
42 extern int _dm_assert(const char *assertion, const char *file, int line,
43     const char *func);
44 
45 #if defined(__STDC__)
46 #if __STDC_VERSION__ - 0 >= 199901L
47 #define	dm_assert(EX) (void)((EX) ? 0 : \
48 	_dm_assert(#EX, __FILE__, __LINE__, __func__))
49 #else
50 #define	dm_assert(EX) (void)((EX) ? 0 : \
51 	_dm_assert(#EX, __FILE__, __LINE__, NULL))
52 #endif /* __STDC_VERSION__ - 0 >= 199901L */
53 #else
54 #define	dm_assert(EX) (void)((EX) ? 0 : \
55 	_dm_assert("EX", __FILE__, __LINE__, NULL))
56 #endif  /* __STDC__ */
57 
58 /*
59  * The following structures comprise the implementation of the
60  * queue structure that's used to construct the list of state
61  * changes.  Removals from the queue are blocking operations that
62  * cause the thread to wait until new entries are added.
63  */
64 struct q_node {
65 	void			*data;
66 	struct q_node		*next;
67 };
68 
69 typedef struct q_head {
70 	/*
71 	 * Block On Empty (when queue is empty, the calling thread will be
72 	 * blocked until something is added)
73 	 */
74 	boolean_t		boe;
75 	pthread_mutex_t		mutex;
76 	pthread_cond_t		cvar;
77 	void			*(*nalloc)(size_t);
78 	void			(*nfree)(void *, size_t);
79 	void			(*data_dealloc)(void *);
80 	struct q_node		*nodep;
81 } qu_t;
82 
83 typedef enum log_class {
84 	MM_CONF		= 0x0001,
85 	MM_HPMGR	= 0x0004,
86 	MM_SCHGMGR	= 0x0008,
87 	MM_MAIN		= 0x0040,
88 	MM_TOPO 	= 0x0100,
89 	MM_ERR		= 0x0200,
90 	MM_WARN		= 0x0400,
91 	MM_NOTE		= 0x0800,
92 	MM_OTHER	= 0x1000
93 } log_class_t;
94 
95 extern void queue_add(qu_t *qp, void *data);
96 extern void *queue_remove(qu_t *qp);
97 extern qu_t *new_queue(boolean_t block_on_empty, void *(*nodealloc)(size_t),
98     void (*nodefree)(void *, size_t), void (*deallocator)(void *));
99 extern void queue_free(qu_t **qp);
100 
101 extern void *dmalloc(size_t sz);
102 extern void *dzmalloc(size_t sz);
103 extern char *dstrdup(const char *s);
104 extern void dfree(void *p, size_t sz);
105 extern void dstrfree(char *s);
106 
107 extern void log_msg(log_class_t cl, const char *fmt, ...);
108 extern void log_err(const char *fmt, ...);
109 extern void log_warn(const char *fmt, ...);
110 extern void log_warn_e(const char *fmt, ...);
111 extern void vcont(log_class_t cl, const char *fmt, va_list val);
112 
113 #ifdef	__cplusplus
114 }
115 #endif
116 
117 #endif /* _UTIL_H */
118