]> git.proxmox.com Git - mirror_zfs-debian.git/blame - lib/libspl/include/assert.h
New upstream version 0.7.11
[mirror_zfs-debian.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
a08ee875 30#define _LIBSPL_ASSERT_H
a26baf28
BB
31
32#include <stdio.h>
33#include <stdlib.h>
cae5b340 34#include <stdarg.h>
a26baf28 35
cae5b340
AX
36static inline int
37libspl_assert(const char *buf, const char *file, const char *func, int line)
a26baf28 38{
cae5b340
AX
39 fprintf(stderr, "%s\n", buf);
40 fprintf(stderr, "ASSERT at %s:%d:%s()", file, line, func);
a26baf28
BB
41 abort();
42}
a26baf28 43
cae5b340
AX
44/* printf version of libspl_assert */
45static inline void
46libspl_assertf(const char *file, const char *func, int line, char *format, ...)
47{
48 va_list args;
a26baf28 49
cae5b340
AX
50 va_start(args, format);
51 vfprintf(stderr, format, args);
52 fprintf(stderr, "\n");
53 fprintf(stderr, "ASSERT at %s:%d:%s()", file, line, func);
54 va_end(args);
55 abort();
56}
a26baf28 57
cae5b340
AX
58#ifdef verify
59#undef verify
60#endif
a26baf28 61
cae5b340
AX
62#define VERIFY(cond) \
63 (void) ((!(cond)) && \
64 libspl_assert(#cond, __FILE__, __FUNCTION__, __LINE__))
65#define verify(cond) \
66 (void) ((!(cond)) && \
67 libspl_assert(#cond, __FILE__, __FUNCTION__, __LINE__))
e10b0808 68
cae5b340
AX
69#define VERIFY3_IMPL(LEFT, OP, RIGHT, TYPE) \
70do { \
71 const TYPE __left = (TYPE)(LEFT); \
72 const TYPE __right = (TYPE)(RIGHT); \
73 if (!(__left OP __right)) \
74 libspl_assertf(__FILE__, __FUNCTION__, __LINE__, \
75 "%s %s %s (0x%llx %s 0x%llx)", #LEFT, #OP, #RIGHT, \
76 (u_longlong_t)__left, #OP, (u_longlong_t)__right); \
a26baf28 77} while (0)
a26baf28
BB
78
79#define VERIFY3S(x, y, z) VERIFY3_IMPL(x, y, z, int64_t)
80#define VERIFY3U(x, y, z) VERIFY3_IMPL(x, y, z, uint64_t)
81#define VERIFY3P(x, y, z) VERIFY3_IMPL(x, y, z, uintptr_t)
c06d4368 82#define VERIFY0(x) VERIFY3_IMPL(x, ==, 0, uint64_t)
a26baf28 83
cae5b340
AX
84#ifdef assert
85#undef assert
86#endif
87
88/* Compile time assert */
89#define CTASSERT_GLOBAL(x) _CTASSERT(x, __LINE__)
90#define CTASSERT(x) { _CTASSERT(x, __LINE__); }
91#define _CTASSERT(x, y) __CTASSERT(x, y)
92#define __CTASSERT(x, y) \
93 typedef char __attribute__((unused)) \
94 __compile_time_assertion__ ## y[(x) ? 1 : -1]
95
a26baf28
BB
96#ifdef NDEBUG
97#define ASSERT3S(x, y, z) ((void)0)
98#define ASSERT3U(x, y, z) ((void)0)
99#define ASSERT3P(x, y, z) ((void)0)
c06d4368 100#define ASSERT0(x) ((void)0)
cae5b340
AX
101#define ASSERT(x) ((void)0)
102#define assert(x) ((void)0)
a26baf28 103#define ASSERTV(x)
e10b0808
AX
104#define IMPLY(A, B) ((void)0)
105#define EQUIV(A, B) ((void)0)
a26baf28
BB
106#else
107#define ASSERT3S(x, y, z) VERIFY3S(x, y, z)
108#define ASSERT3U(x, y, z) VERIFY3U(x, y, z)
109#define ASSERT3P(x, y, z) VERIFY3P(x, y, z)
c06d4368 110#define ASSERT0(x) VERIFY0(x)
cae5b340
AX
111#define ASSERT(x) VERIFY(x)
112#define assert(x) VERIFY(x)
a26baf28 113#define ASSERTV(x) x
e10b0808
AX
114#define IMPLY(A, B) \
115 ((void)(((!(A)) || (B)) || \
cae5b340
AX
116 libspl_assert("(" #A ") implies (" #B ")", \
117 __FILE__, __FUNCTION__, __LINE__)))
e10b0808
AX
118#define EQUIV(A, B) \
119 ((void)((!!(A) == !!(B)) || \
cae5b340
AX
120 libspl_assert("(" #A ") is equivalent to (" #B ")", \
121 __FILE__, __FUNCTION__, __LINE__)))
e10b0808 122
a26baf28
BB
123#endif /* NDEBUG */
124
125#endif /* _LIBSPL_ASSERT_H */