Replace *CTASSERT() with _Static_assert()
[freebsd.git] / lib / libspl / include / assert.h
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 libspl_assert_ok;
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
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 #ifdef NDEBUG
115 #define ASSERT3B(x, y, z)                                               \
116         ((void) sizeof ((uintptr_t)(x)), (void) sizeof ((uintptr_t)(z)))
117 #define ASSERT3S(x, y, z)                                               \
118         ((void) sizeof ((uintptr_t)(x)), (void) sizeof ((uintptr_t)(z)))
119 #define ASSERT3U(x, y, z)                                               \
120         ((void) sizeof ((uintptr_t)(x)), (void) sizeof ((uintptr_t)(z)))
121 #define ASSERT3P(x, y, z)                                               \
122         ((void) sizeof ((uintptr_t)(x)), (void) sizeof ((uintptr_t)(z)))
123 #define ASSERT0(x)              ((void) sizeof ((uintptr_t)(x)))
124 #define ASSERT(x)               ((void) sizeof ((uintptr_t)(x)))
125 #define assert(x)               ((void) sizeof ((uintptr_t)(x)))
126 #define IMPLY(A, B)                                                     \
127         ((void) sizeof ((uintptr_t)(A)), (void) sizeof ((uintptr_t)(B)))
128 #define EQUIV(A, B)                                                     \
129         ((void) sizeof ((uintptr_t)(A)), (void) sizeof ((uintptr_t)(B)))
130 #else
131 #define ASSERT3B        VERIFY3B
132 #define ASSERT3S        VERIFY3S
133 #define ASSERT3U        VERIFY3U
134 #define ASSERT3P        VERIFY3P
135 #define ASSERT0         VERIFY0
136 #define ASSERT          VERIFY
137 #define assert          VERIFY
138 #define IMPLY(A, B) \
139         ((void)(((!(A)) || (B)) || \
140             libspl_assert("(" #A ") implies (" #B ")", \
141             __FILE__, __FUNCTION__, __LINE__)))
142 #define EQUIV(A, B) \
143         ((void)((!!(A) == !!(B)) || \
144             libspl_assert("(" #A ") is equivalent to (" #B ")", \
145             __FILE__, __FUNCTION__, __LINE__)))
146
147 #endif  /* NDEBUG */
148
149 #endif  /* _LIBSPL_ASSERT_H */