7f145b89a37edb1d44004c8a80bb97c473b36004
[zfs.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
35 #ifndef __assert_c99
36 static inline void
37 __assert_c99(const char *expr, const char *file, int line, const char *func)
38 {
39         fprintf(stderr, "%s:%i: %s: Assertion `%s` failed.\n",
40                 file, line, func, expr);
41         abort();
42 }
43 #endif  /* __assert_c99 */
44
45 #ifndef verify
46 #if defined(__STDC__)
47 #if __STDC_VERSION__ - 0 >= 199901L
48 #define verify(EX) (void)((EX) || \
49         (__assert_c99(#EX, __FILE__, __LINE__, __func__), 0))
50 #else
51 #define verify(EX) (void)((EX) || (__assert(#EX, __FILE__, __LINE__), 0))
52 #endif  /* __STDC_VERSION__ - 0 >= 199901L */
53 #else
54 #define verify(EX) (void)((EX) || (_assert("EX", __FILE__, __LINE__), 0))
55 #endif  /* __STDC__ */
56 #endif  /* verify */
57
58 #undef VERIFY
59 #undef ASSERT
60
61 #define VERIFY  verify
62 #define ASSERT  assert
63
64 extern void __assert(const char *, const char *, int);
65
66 /* BEGIN CSTYLED */
67 #define VERIFY3_IMPL(LEFT, OP, RIGHT, TYPE) do { \
68         const TYPE __left = (TYPE)(LEFT); \
69         const TYPE __right = (TYPE)(RIGHT); \
70         if (!(__left OP __right)) { \
71                 char *__buf = alloca(256); \
72                 (void) snprintf(__buf, 256, "%s %s %s (0x%llx %s 0x%llx)", \
73                         #LEFT, #OP, #RIGHT, \
74                         (u_longlong_t)__left, #OP, (u_longlong_t)__right); \
75                 __assert(__buf, __FILE__, __LINE__); \
76         } \
77 } while (0)
78 /* END CSTYLED */
79
80 #define VERIFY3S(x, y, z)       VERIFY3_IMPL(x, y, z, int64_t)
81 #define VERIFY3U(x, y, z)       VERIFY3_IMPL(x, y, z, uint64_t)
82 #define VERIFY3P(x, y, z)       VERIFY3_IMPL(x, y, z, uintptr_t)
83
84 #ifdef NDEBUG
85 #define ASSERT3S(x, y, z)       ((void)0)
86 #define ASSERT3U(x, y, z)       ((void)0)
87 #define ASSERT3P(x, y, z)       ((void)0)
88 #define ASSERTV(x)
89 #else
90 #define ASSERT3S(x, y, z)       VERIFY3S(x, y, z)
91 #define ASSERT3U(x, y, z)       VERIFY3U(x, y, z)
92 #define ASSERT3P(x, y, z)       VERIFY3P(x, y, z)
93 #define ASSERTV(x)              x
94 #endif  /* NDEBUG */
95
96 #endif  /* _LIBSPL_ASSERT_H */