]> git.proxmox.com Git - mirror_zfs.git/blame - lib/libspl/include/assert.h
libspl: cast to uintptr_t instead of !!ing
[mirror_zfs.git] / lib / libspl / include / assert.h
CommitLineData
a26baf28
BB
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
d1d7e268 30#define _LIBSPL_ASSERT_H
a26baf28
BB
31
32#include <stdio.h>
33#include <stdlib.h>
478e3ecf 34#include <stdarg.h>
a26baf28 35
b6437ea4 36/* Set to non-zero to avoid abort()ing on an assertion failure */
94f942c6 37extern int libspl_assert_ok;
b6437ea4
AS
38
39/* printf version of libspl_assert */
40extern void libspl_assertf(const char *file, const char *func, int line,
41 const char *format, ...);
7c1bf0cf 42
726c4a25
BB
43static inline int
44libspl_assert(const char *buf, const char *file, const char *func, int line)
a26baf28 45{
b6437ea4
AS
46 libspl_assertf(file, func, line, "%s", buf);
47 return (0);
682ce104
TH
48}
49
726c4a25
BB
50#ifdef verify
51#undef verify
52#endif
a26baf28 53
726c4a25
BB
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__))
a26baf28 60
6e8b2688 61#define VERIFY3B(LEFT, OP, RIGHT) \
726c4a25 62do { \
6e8b2688
PD
63 const boolean_t __left = (boolean_t)(LEFT); \
64 const boolean_t __right = (boolean_t)(RIGHT); \
682ce104
TH
65 if (!(__left OP __right)) \
66 libspl_assertf(__FILE__, __FUNCTION__, __LINE__, \
314be68a
TC
67 "%s %s %s (0x%llx %s 0x%llx)", #LEFT, #OP, #RIGHT, \
68 (u_longlong_t)__left, #OP, (u_longlong_t)__right); \
a26baf28 69} while (0)
a26baf28 70
6e8b2688
PD
71#define VERIFY3S(LEFT, OP, RIGHT) \
72do { \
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) \
82do { \
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) \
92do { \
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) \
102do { \
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)
a26baf28 109
726c4a25
BB
110#ifdef assert
111#undef assert
112#endif
113
3c67d83a
TH
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
a26baf28 122#ifdef NDEBUG
a9e2788f
AZ
123#define ASSERT3B(x, y, z) \
124 ((void) sizeof ((uintptr_t)(x)), (void) sizeof ((uintptr_t)(z)))
125#define ASSERT3S(x, y, z) \
126 ((void) sizeof ((uintptr_t)(x)), (void) sizeof ((uintptr_t)(z)))
127#define ASSERT3U(x, y, z) \
128 ((void) sizeof ((uintptr_t)(x)), (void) sizeof ((uintptr_t)(z)))
129#define ASSERT3P(x, y, z) \
130 ((void) sizeof ((uintptr_t)(x)), (void) sizeof ((uintptr_t)(z)))
131#define ASSERT0(x) ((void) sizeof ((uintptr_t)(x)))
132#define ASSERT(x) ((void) sizeof ((uintptr_t)(x)))
133#define assert(x) ((void) sizeof ((uintptr_t)(x)))
134#define IMPLY(A, B) \
135 ((void) sizeof ((uintptr_t)(A)), (void) sizeof ((uintptr_t)(B)))
136#define EQUIV(A, B) \
137 ((void) sizeof ((uintptr_t)(A)), (void) sizeof ((uintptr_t)(B)))
a26baf28 138#else
6e8b2688
PD
139#define ASSERT3B VERIFY3B
140#define ASSERT3S VERIFY3S
141#define ASSERT3U VERIFY3U
142#define ASSERT3P VERIFY3P
143#define ASSERT0 VERIFY0
144#define ASSERT VERIFY
145#define assert VERIFY
fa720217
BB
146#define IMPLY(A, B) \
147 ((void)(((!(A)) || (B)) || \
726c4a25
BB
148 libspl_assert("(" #A ") implies (" #B ")", \
149 __FILE__, __FUNCTION__, __LINE__)))
fa720217
BB
150#define EQUIV(A, B) \
151 ((void)((!!(A) == !!(B)) || \
726c4a25
BB
152 libspl_assert("(" #A ") is equivalent to (" #B ")", \
153 __FILE__, __FUNCTION__, __LINE__)))
fa720217 154
a26baf28
BB
155#endif /* NDEBUG */
156
157#endif /* _LIBSPL_ASSERT_H */