1724365f7Ssethg /*
2724365f7Ssethg  * CDDL HEADER START
3724365f7Ssethg  *
4724365f7Ssethg  * The contents of this file are subject to the terms of the
5724365f7Ssethg  * Common Development and Distribution License (the "License").
6724365f7Ssethg  * You may not use this file except in compliance with the License.
7724365f7Ssethg  *
8724365f7Ssethg  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9724365f7Ssethg  * or http://www.opensolaris.org/os/licensing.
10724365f7Ssethg  * See the License for the specific language governing permissions
11724365f7Ssethg  * and limitations under the License.
12724365f7Ssethg  *
13724365f7Ssethg  * When distributing Covered Code, include this CDDL HEADER in each
14724365f7Ssethg  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15724365f7Ssethg  * If applicable, add the following below this CDDL HEADER, with the
16724365f7Ssethg  * fields enclosed by brackets "[]" replaced with your own identifying
17724365f7Ssethg  * information: Portions Copyright [yyyy] [name of copyright owner]
18724365f7Ssethg  *
19724365f7Ssethg  * CDDL HEADER END
20724365f7Ssethg  */
21724365f7Ssethg 
22724365f7Ssethg /*
239113a79cSeschrock  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
24724365f7Ssethg  * Use is subject to license terms.
25724365f7Ssethg  */
26724365f7Ssethg 
27724365f7Ssethg #ifndef _UTIL_H
28724365f7Ssethg #define	_UTIL_H
29724365f7Ssethg 
30724365f7Ssethg /*
317a0b67e3Ssethg  * Utility functions and macros
32724365f7Ssethg  */
33724365f7Ssethg 
34724365f7Ssethg #ifdef	__cplusplus
35724365f7Ssethg extern "C" {
36724365f7Ssethg #endif
37724365f7Ssethg 
38724365f7Ssethg #include <stdio.h>
39724365f7Ssethg #include <stdarg.h>
40724365f7Ssethg #include <time.h>
41724365f7Ssethg 
427a0b67e3Ssethg extern int _dm_assert(const char *assertion, const char *file, int line,
437a0b67e3Ssethg     const char *func);
447a0b67e3Ssethg 
457a0b67e3Ssethg #if defined(__STDC__)
467a0b67e3Ssethg #if __STDC_VERSION__ - 0 >= 199901L
477a0b67e3Ssethg #define	dm_assert(EX) (void)((EX) ? 0 : \
487a0b67e3Ssethg 	_dm_assert(#EX, __FILE__, __LINE__, __func__))
497a0b67e3Ssethg #else
507a0b67e3Ssethg #define	dm_assert(EX) (void)((EX) ? 0 : \
517a0b67e3Ssethg 	_dm_assert(#EX, __FILE__, __LINE__, NULL))
527a0b67e3Ssethg #endif /* __STDC_VERSION__ - 0 >= 199901L */
537a0b67e3Ssethg #else
547a0b67e3Ssethg #define	dm_assert(EX) (void)((EX) ? 0 : \
557a0b67e3Ssethg 	_dm_assert("EX", __FILE__, __LINE__, NULL))
567a0b67e3Ssethg #endif  /* __STDC__ */
577a0b67e3Ssethg 
58724365f7Ssethg /*
59724365f7Ssethg  * The following structures comprise the implementation of the
60724365f7Ssethg  * queue structure that's used to construct the list of state
61724365f7Ssethg  * changes.  Removals from the queue are blocking operations that
62724365f7Ssethg  * cause the thread to wait until new entries are added.
63724365f7Ssethg  */
64724365f7Ssethg struct q_node {
65724365f7Ssethg 	void			*data;
66724365f7Ssethg 	struct q_node		*next;
67724365f7Ssethg };
68724365f7Ssethg 
69724365f7Ssethg typedef struct q_head {
70724365f7Ssethg 	/*
71724365f7Ssethg 	 * Block On Empty (when queue is empty, the calling thread will be
72724365f7Ssethg 	 * blocked until something is added)
73724365f7Ssethg 	 */
74724365f7Ssethg 	boolean_t		boe;
75724365f7Ssethg 	pthread_mutex_t		mutex;
76724365f7Ssethg 	pthread_cond_t		cvar;
77724365f7Ssethg 	void			*(*nalloc)(size_t);
78724365f7Ssethg 	void			(*nfree)(void *, size_t);
79724365f7Ssethg 	void			(*data_dealloc)(void *);
80724365f7Ssethg 	struct q_node		*nodep;
81724365f7Ssethg } qu_t;
82724365f7Ssethg 
83724365f7Ssethg typedef enum log_class {
84724365f7Ssethg 	MM_CONF		= 0x0001,
85724365f7Ssethg 	MM_HPMGR	= 0x0004,
86724365f7Ssethg 	MM_SCHGMGR	= 0x0008,
87724365f7Ssethg 	MM_MAIN		= 0x0040,
88724365f7Ssethg 	MM_TOPO 	= 0x0100,
89724365f7Ssethg 	MM_ERR		= 0x0200,
90724365f7Ssethg 	MM_WARN		= 0x0400,
91724365f7Ssethg 	MM_NOTE		= 0x0800,
92724365f7Ssethg 	MM_OTHER	= 0x1000
93724365f7Ssethg } log_class_t;
94724365f7Ssethg 
95724365f7Ssethg extern void queue_add(qu_t *qp, void *data);
96724365f7Ssethg extern void *queue_remove(qu_t *qp);
97724365f7Ssethg extern qu_t *new_queue(boolean_t block_on_empty, void *(*nodealloc)(size_t),
98724365f7Ssethg     void (*nodefree)(void *, size_t), void (*deallocator)(void *));
99724365f7Ssethg extern void queue_free(qu_t **qp);
100724365f7Ssethg 
101724365f7Ssethg extern void *dmalloc(size_t sz);
102724365f7Ssethg extern void *dzmalloc(size_t sz);
103724365f7Ssethg extern char *dstrdup(const char *s);
104724365f7Ssethg extern void dfree(void *p, size_t sz);
105724365f7Ssethg extern void dstrfree(char *s);
106724365f7Ssethg 
107724365f7Ssethg extern void log_msg(log_class_t cl, const char *fmt, ...);
108724365f7Ssethg extern void log_err(const char *fmt, ...);
109724365f7Ssethg extern void log_warn(const char *fmt, ...);
110724365f7Ssethg extern void log_warn_e(const char *fmt, ...);
111724365f7Ssethg extern void vcont(log_class_t cl, const char *fmt, va_list val);
112724365f7Ssethg 
113724365f7Ssethg #ifdef	__cplusplus
114724365f7Ssethg }
115724365f7Ssethg #endif
116724365f7Ssethg 
117724365f7Ssethg #endif /* _UTIL_H */
118