]> git.proxmox.com Git - mirror_zfs.git/blame - lib/libspl/include/assert.h
OpenZFS 4185 - add new cryptographic checksums to ZFS: SHA-512, Skein, Edon-R
[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>
34
726c4a25
BB
35static inline int
36libspl_assert(const char *buf, const char *file, const char *func, int line)
a26baf28 37{
726c4a25
BB
38 fprintf(stderr, "%s\n", buf);
39 fprintf(stderr, "ASSERT at %s:%d:%s()", file, line, func);
a26baf28
BB
40 abort();
41}
a26baf28 42
726c4a25
BB
43#ifdef verify
44#undef verify
45#endif
a26baf28 46
726c4a25
BB
47#define VERIFY(cond) \
48 (void) ((!(cond)) && \
49 libspl_assert(#cond, __FILE__, __FUNCTION__, __LINE__))
50#define verify(cond) \
51 (void) ((!(cond)) && \
52 libspl_assert(#cond, __FILE__, __FUNCTION__, __LINE__))
a26baf28 53
726c4a25
BB
54#define VERIFY3_IMPL(LEFT, OP, RIGHT, TYPE) \
55do { \
56 const TYPE __left = (TYPE)(LEFT); \
57 const TYPE __right = (TYPE)(RIGHT); \
58 if (!(__left OP __right)) { \
59 char *__buf = alloca(256); \
60 (void) snprintf(__buf, 256, \
61 "%s %s %s (0x%llx %s 0x%llx)", #LEFT, #OP, #RIGHT, \
62 (u_longlong_t)__left, #OP, (u_longlong_t)__right); \
63 libspl_assert(__buf, __FILE__, __FUNCTION__, __LINE__); \
64 } \
a26baf28 65} while (0)
a26baf28
BB
66
67#define VERIFY3S(x, y, z) VERIFY3_IMPL(x, y, z, int64_t)
68#define VERIFY3U(x, y, z) VERIFY3_IMPL(x, y, z, uint64_t)
69#define VERIFY3P(x, y, z) VERIFY3_IMPL(x, y, z, uintptr_t)
c99c9001 70#define VERIFY0(x) VERIFY3_IMPL(x, ==, 0, uint64_t)
a26baf28 71
726c4a25
BB
72#ifdef assert
73#undef assert
74#endif
75
3c67d83a
TH
76/* Compile time assert */
77#define CTASSERT_GLOBAL(x) _CTASSERT(x, __LINE__)
78#define CTASSERT(x) { _CTASSERT(x, __LINE__); }
79#define _CTASSERT(x, y) __CTASSERT(x, y)
80#define __CTASSERT(x, y) \
81 typedef char __attribute__((unused)) \
82 __compile_time_assertion__ ## y[(x) ? 1 : -1]
83
a26baf28
BB
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)
c99c9001 88#define ASSERT0(x) ((void)0)
726c4a25
BB
89#define ASSERT(x) ((void)0)
90#define assert(x) ((void)0)
a26baf28 91#define ASSERTV(x)
fa720217
BB
92#define IMPLY(A, B) ((void)0)
93#define EQUIV(A, B) ((void)0)
a26baf28
BB
94#else
95#define ASSERT3S(x, y, z) VERIFY3S(x, y, z)
96#define ASSERT3U(x, y, z) VERIFY3U(x, y, z)
97#define ASSERT3P(x, y, z) VERIFY3P(x, y, z)
c99c9001 98#define ASSERT0(x) VERIFY0(x)
726c4a25
BB
99#define ASSERT(x) VERIFY(x)
100#define assert(x) VERIFY(x)
a26baf28 101#define ASSERTV(x) x
fa720217
BB
102#define IMPLY(A, B) \
103 ((void)(((!(A)) || (B)) || \
726c4a25
BB
104 libspl_assert("(" #A ") implies (" #B ")", \
105 __FILE__, __FUNCTION__, __LINE__)))
fa720217
BB
106#define EQUIV(A, B) \
107 ((void)((!!(A) == !!(B)) || \
726c4a25
BB
108 libspl_assert("(" #A ") is equivalent to (" #B ")", \
109 __FILE__, __FUNCTION__, __LINE__)))
fa720217 110
a26baf28
BB
111#endif /* NDEBUG */
112
113#endif /* _LIBSPL_ASSERT_H */