]> git.ozlabs.org Git - ccan/blob - ccan/build_assert/build_assert.h
tdb2: test: fix run-57-die-during-transaction.c to be more efficient.
[ccan] / ccan / build_assert / build_assert.h
1 #ifndef CCAN_BUILD_ASSERT_H
2 #define CCAN_BUILD_ASSERT_H
3
4 /**
5  * BUILD_ASSERT - assert a build-time dependency.
6  * @cond: the compile-time condition which must be true.
7  *
8  * Your compile will fail if the condition isn't true, or can't be evaluated
9  * by the compiler.  This can only be used within a function.
10  *
11  * Example:
12  *      #include <stddef.h>
13  *      ...
14  *      static char *foo_to_char(struct foo *foo)
15  *      {
16  *              // This code needs string to be at start of foo.
17  *              BUILD_ASSERT(offsetof(struct foo, string) == 0);
18  *              return (char *)foo;
19  *      }
20  */
21 #define BUILD_ASSERT(cond) \
22         do { (void) sizeof(char [1 - 2*!(cond)]); } while(0)
23
24 /**
25  * BUILD_ASSERT_OR_ZERO - assert a build-time dependency, as an expression.
26  * @cond: the compile-time condition which must be true.
27  *
28  * Your compile will fail if the condition isn't true, or can't be evaluated
29  * by the compiler.  This can be used in an expression: its value is "0".
30  *
31  * Example:
32  *      #define foo_to_char(foo)                                        \
33  *               ((char *)(foo)                                         \
34  *                + BUILD_ASSERT_OR_ZERO(offsetof(struct foo, string) == 0))
35  */
36 #define BUILD_ASSERT_OR_ZERO(cond) \
37         (sizeof(char [1 - 2*!(cond)]) - 1)
38
39 #endif /* CCAN_BUILD_ASSERT_H */