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