1*22ce4affSfengbojiang /*
2*22ce4affSfengbojiang  * CDDL HEADER START
3*22ce4affSfengbojiang  *
4*22ce4affSfengbojiang  * The contents of this file are subject to the terms of the
5*22ce4affSfengbojiang  * Common Development and Distribution License, Version 1.0 only
6*22ce4affSfengbojiang  * (the "License").  You may not use this file except in compliance
7*22ce4affSfengbojiang  * with the License.
8*22ce4affSfengbojiang  *
9*22ce4affSfengbojiang  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*22ce4affSfengbojiang  * or http://www.opensolaris.org/os/licensing.
11*22ce4affSfengbojiang  * See the License for the specific language governing permissions
12*22ce4affSfengbojiang  * and limitations under the License.
13*22ce4affSfengbojiang  *
14*22ce4affSfengbojiang  * When distributing Covered Code, include this CDDL HEADER in each
15*22ce4affSfengbojiang  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*22ce4affSfengbojiang  * If applicable, add the following below this CDDL HEADER, with the
17*22ce4affSfengbojiang  * fields enclosed by brackets "[]" replaced with your own identifying
18*22ce4affSfengbojiang  * information: Portions Copyright [yyyy] [name of copyright owner]
19*22ce4affSfengbojiang  *
20*22ce4affSfengbojiang  * CDDL HEADER END
21*22ce4affSfengbojiang  */
22*22ce4affSfengbojiang /*
23*22ce4affSfengbojiang  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
24*22ce4affSfengbojiang  * Use is subject to license terms.
25*22ce4affSfengbojiang  */
26*22ce4affSfengbojiang 
27*22ce4affSfengbojiang #include_next <assert.h>
28*22ce4affSfengbojiang 
29*22ce4affSfengbojiang #ifndef _LIBSPL_ASSERT_H
30*22ce4affSfengbojiang #define	_LIBSPL_ASSERT_H
31*22ce4affSfengbojiang 
32*22ce4affSfengbojiang #include <stdio.h>
33*22ce4affSfengbojiang #include <stdlib.h>
34*22ce4affSfengbojiang #include <stdarg.h>
35*22ce4affSfengbojiang 
36*22ce4affSfengbojiang /* Set to non-zero to avoid abort()ing on an assertion failure */
37*22ce4affSfengbojiang extern int aok;
38*22ce4affSfengbojiang 
39*22ce4affSfengbojiang /* printf version of libspl_assert */
40*22ce4affSfengbojiang extern void libspl_assertf(const char *file, const char *func, int line,
41*22ce4affSfengbojiang     const char *format, ...);
42*22ce4affSfengbojiang 
43*22ce4affSfengbojiang static inline int
libspl_assert(const char * buf,const char * file,const char * func,int line)44*22ce4affSfengbojiang libspl_assert(const char *buf, const char *file, const char *func, int line)
45*22ce4affSfengbojiang {
46*22ce4affSfengbojiang 	libspl_assertf(file, func, line, "%s", buf);
47*22ce4affSfengbojiang 	return (0);
48*22ce4affSfengbojiang }
49*22ce4affSfengbojiang 
50*22ce4affSfengbojiang #ifdef verify
51*22ce4affSfengbojiang #undef verify
52*22ce4affSfengbojiang #endif
53*22ce4affSfengbojiang 
54*22ce4affSfengbojiang #define	VERIFY(cond)							\
55*22ce4affSfengbojiang 	(void) ((!(cond)) &&						\
56*22ce4affSfengbojiang 	    libspl_assert(#cond, __FILE__, __FUNCTION__, __LINE__))
57*22ce4affSfengbojiang #define	verify(cond)							\
58*22ce4affSfengbojiang 	(void) ((!(cond)) &&						\
59*22ce4affSfengbojiang 	    libspl_assert(#cond, __FILE__, __FUNCTION__, __LINE__))
60*22ce4affSfengbojiang 
61*22ce4affSfengbojiang #define	VERIFY3B(LEFT, OP, RIGHT)					\
62*22ce4affSfengbojiang do {									\
63*22ce4affSfengbojiang 	const boolean_t __left = (boolean_t)(LEFT);			\
64*22ce4affSfengbojiang 	const boolean_t __right = (boolean_t)(RIGHT);			\
65*22ce4affSfengbojiang 	if (!(__left OP __right))					\
66*22ce4affSfengbojiang 		libspl_assertf(__FILE__, __FUNCTION__, __LINE__,	\
67*22ce4affSfengbojiang 		    "%s %s %s (0x%llx %s 0x%llx)", #LEFT, #OP, #RIGHT,	\
68*22ce4affSfengbojiang 		    (u_longlong_t)__left, #OP, (u_longlong_t)__right);	\
69*22ce4affSfengbojiang } while (0)
70*22ce4affSfengbojiang 
71*22ce4affSfengbojiang #define	VERIFY3S(LEFT, OP, RIGHT)					\
72*22ce4affSfengbojiang do {									\
73*22ce4affSfengbojiang 	const int64_t __left = (int64_t)(LEFT);				\
74*22ce4affSfengbojiang 	const int64_t __right = (int64_t)(RIGHT);			\
75*22ce4affSfengbojiang 	if (!(__left OP __right))					\
76*22ce4affSfengbojiang 		libspl_assertf(__FILE__, __FUNCTION__, __LINE__,	\
77*22ce4affSfengbojiang 		    "%s %s %s (0x%llx %s 0x%llx)", #LEFT, #OP, #RIGHT,	\
78*22ce4affSfengbojiang 		    (u_longlong_t)__left, #OP, (u_longlong_t)__right);	\
79*22ce4affSfengbojiang } while (0)
80*22ce4affSfengbojiang 
81*22ce4affSfengbojiang #define	VERIFY3U(LEFT, OP, RIGHT)					\
82*22ce4affSfengbojiang do {									\
83*22ce4affSfengbojiang 	const uint64_t __left = (uint64_t)(LEFT);			\
84*22ce4affSfengbojiang 	const uint64_t __right = (uint64_t)(RIGHT);			\
85*22ce4affSfengbojiang 	if (!(__left OP __right))					\
86*22ce4affSfengbojiang 		libspl_assertf(__FILE__, __FUNCTION__, __LINE__,	\
87*22ce4affSfengbojiang 		    "%s %s %s (0x%llx %s 0x%llx)", #LEFT, #OP, #RIGHT,	\
88*22ce4affSfengbojiang 		    (u_longlong_t)__left, #OP, (u_longlong_t)__right);	\
89*22ce4affSfengbojiang } while (0)
90*22ce4affSfengbojiang 
91*22ce4affSfengbojiang #define	VERIFY3P(LEFT, OP, RIGHT)					\
92*22ce4affSfengbojiang do {									\
93*22ce4affSfengbojiang 	const uintptr_t __left = (uintptr_t)(LEFT);			\
94*22ce4affSfengbojiang 	const uintptr_t __right = (uintptr_t)(RIGHT);			\
95*22ce4affSfengbojiang 	if (!(__left OP __right))					\
96*22ce4affSfengbojiang 		libspl_assertf(__FILE__, __FUNCTION__, __LINE__,	\
97*22ce4affSfengbojiang 		    "%s %s %s (0x%llx %s 0x%llx)", #LEFT, #OP, #RIGHT,	\
98*22ce4affSfengbojiang 		    (u_longlong_t)__left, #OP, (u_longlong_t)__right);	\
99*22ce4affSfengbojiang } while (0)
100*22ce4affSfengbojiang 
101*22ce4affSfengbojiang #define	VERIFY0(LEFT)							\
102*22ce4affSfengbojiang do {									\
103*22ce4affSfengbojiang 	const uint64_t __left = (uint64_t)(LEFT);			\
104*22ce4affSfengbojiang 	if (!(__left == 0))						\
105*22ce4affSfengbojiang 		libspl_assertf(__FILE__, __FUNCTION__, __LINE__,	\
106*22ce4affSfengbojiang 		    "%s == 0 (0x%llx == 0)", #LEFT,			\
107*22ce4affSfengbojiang 		    (u_longlong_t)__left);				\
108*22ce4affSfengbojiang } while (0)
109*22ce4affSfengbojiang 
110*22ce4affSfengbojiang #ifdef assert
111*22ce4affSfengbojiang #undef assert
112*22ce4affSfengbojiang #endif
113*22ce4affSfengbojiang 
114*22ce4affSfengbojiang /* Compile time assert */
115*22ce4affSfengbojiang #define	CTASSERT_GLOBAL(x)		_CTASSERT(x, __LINE__)
116*22ce4affSfengbojiang #define	CTASSERT(x)			{ _CTASSERT(x, __LINE__); }
117*22ce4affSfengbojiang #define	_CTASSERT(x, y)			__CTASSERT(x, y)
118*22ce4affSfengbojiang #define	__CTASSERT(x, y)						\
119*22ce4affSfengbojiang 	typedef char __attribute__((unused))				\
120*22ce4affSfengbojiang 	__compile_time_assertion__ ## y[(x) ? 1 : -1]
121*22ce4affSfengbojiang 
122*22ce4affSfengbojiang #ifdef NDEBUG
123*22ce4affSfengbojiang #define	ASSERT3B(x, y, z)	((void)0)
124*22ce4affSfengbojiang #define	ASSERT3S(x, y, z)	((void)0)
125*22ce4affSfengbojiang #define	ASSERT3U(x, y, z)	((void)0)
126*22ce4affSfengbojiang #define	ASSERT3P(x, y, z)	((void)0)
127*22ce4affSfengbojiang #define	ASSERT0(x)		((void)0)
128*22ce4affSfengbojiang #define	ASSERT(x)		((void)0)
129*22ce4affSfengbojiang #define	assert(x)		((void)0)
130*22ce4affSfengbojiang #define	IMPLY(A, B)		((void)0)
131*22ce4affSfengbojiang #define	EQUIV(A, B)		((void)0)
132*22ce4affSfengbojiang #else
133*22ce4affSfengbojiang #define	ASSERT3B	VERIFY3B
134*22ce4affSfengbojiang #define	ASSERT3S	VERIFY3S
135*22ce4affSfengbojiang #define	ASSERT3U	VERIFY3U
136*22ce4affSfengbojiang #define	ASSERT3P	VERIFY3P
137*22ce4affSfengbojiang #define	ASSERT0		VERIFY0
138*22ce4affSfengbojiang #define	ASSERT		VERIFY
139*22ce4affSfengbojiang #define	assert		VERIFY
140*22ce4affSfengbojiang #define	IMPLY(A, B) \
141*22ce4affSfengbojiang 	((void)(((!(A)) || (B)) || \
142*22ce4affSfengbojiang 	    libspl_assert("(" #A ") implies (" #B ")", \
143*22ce4affSfengbojiang 	    __FILE__, __FUNCTION__, __LINE__)))
144*22ce4affSfengbojiang #define	EQUIV(A, B) \
145*22ce4affSfengbojiang 	((void)((!!(A) == !!(B)) || \
146*22ce4affSfengbojiang 	    libspl_assert("(" #A ") is equivalent to (" #B ")", \
147*22ce4affSfengbojiang 	    __FILE__, __FUNCTION__, __LINE__)))
148*22ce4affSfengbojiang 
149*22ce4affSfengbojiang #endif  /* NDEBUG */
150*22ce4affSfengbojiang 
151*22ce4affSfengbojiang #endif  /* _LIBSPL_ASSERT_H */
152