Update Red Hat Copyright Notices
[nbdkit.git] / common / include / compiler-macros.h
blob36c435ee4cb0cf893895146b3f5ea016f6070200
1 /* nbdkit
2 * Copyright Red Hat
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met:
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
11 * * Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * * Neither the name of Red Hat nor the names of its contributors may be
16 * used to endorse or promote products derived from this software without
17 * specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY RED HAT AND CONTRIBUTORS ''AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
22 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL RED HAT OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
26 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
27 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
29 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
33 #ifndef NBDKIT_COMPILER_MACROS_H
34 #define NBDKIT_COMPILER_MACROS_H
36 #ifndef __cplusplus
38 /* BUILD_BUG_UNLESS_TRUE(1) => 0
39 * BUILD_BUG_UNLESS_TRUE(0) => compile-time failure
41 * It works by constructing a struct which has an impossible
42 * (negative-sized) bitfield in the false case. Anonymous bitfields
43 * are permitted in C99 and above.
45 * The Linux kernel calls this BUILD_BUG_ON_ZERO(!cond) which is a
46 * confusing name. It has the same semantics except cond is negated.
48 #define BUILD_BUG_STRUCT_SIZE(cond) \
49 (sizeof (struct { int: (cond) ? 1 : -1; }))
50 #define BUILD_BUG_UNLESS_TRUE(cond) \
51 (BUILD_BUG_STRUCT_SIZE (cond) - BUILD_BUG_STRUCT_SIZE (cond))
53 /* Each of TYPE_IS_POINTER() and TYPE_IS_ARRAY() produces a build failure if it
54 * is invoked with an object that has neither pointer-to-object type nor array
55 * type.
57 * C99 6.5.2.1 constrains one of the operands of the subscript operator to have
58 * pointer-to-object type, and the other operand to have integer type. In the
59 * replacement text of TYPE_IS_POINTER(), we use [0] as subscript (providing the
60 * integer operand), therefore the macro argument (p) is constrained to have
61 * pointer-to-object type.
63 * If TYPE_IS_POINTER() is invoked with a pointer that has pointer-to-object
64 * type, the constraint is directly satisfied, and TYPE_IS_POINTER() evaluates,
65 * at compile time, to 1.
67 * If TYPE_IS_POINTER() is invoked with an array, the constraint of the
68 * subscript operator is satisfied again -- because the array argument "decays"
69 * to a pointer to the array's initial element (C99 6.3.2p3) --, and
70 * TYPE_IS_POINTER() evaluates, at compile time, to 0.
72 * If TYPE_IS_POINTER() is invoked with an argument having any other type, then
73 * the subscript operator constraint is not satisfied, and C99 5.1.1.3p1
74 * requires the emission of a diagnostic message -- the build breaks. Therefore,
75 * TYPE_IS_ARRAY() can be defined simply as the logical negation of
76 * TYPE_IS_POINTER().
78 #define TYPE_IS_POINTER(p) \
79 (__builtin_types_compatible_p (typeof (p), typeof (&(p)[0])))
80 #define TYPE_IS_ARRAY(a) (!TYPE_IS_POINTER (a))
82 #else /* __cplusplus */
84 #define BUILD_BUG_UNLESS_TRUE(cond) 0
85 #define TYPE_IS_POINTER(p) 1
86 #define TYPE_IS_ARRAY(a) 1
88 #endif /* __cplusplus */
90 #endif /* NBDKIT_COMPILER_MACROS_H */