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