2 * Copyright © 2014 Intel Corporation
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
27 /* Compute the size of an array */
29 # define ARRAY_SIZE(x) (sizeof(x) / sizeof(*(x)))
32 /* For compatibility with Clang's __has_builtin() */
34 # define __has_builtin(x) 0
38 * __builtin_expect macros
40 #if !defined(HAVE___BUILTIN_EXPECT)
41 # define __builtin_expect(x, y) (x)
45 # ifdef HAVE___BUILTIN_EXPECT
46 # define likely(x) __builtin_expect(!!(x), 1)
47 # define unlikely(x) __builtin_expect(!!(x), 0)
49 # define likely(x) (x)
50 # define unlikely(x) (x)
56 * Static (compile-time) assertion.
57 * Basically, use COND to dimension an array. If COND is false/zero the
58 * array size will be -1 and we'll get a compilation error.
60 #define STATIC_ASSERT(COND) \
62 (void) sizeof(char [1 - 2*!(COND)]); \
67 * Unreachable macro. Useful for suppressing "control reaches end of non-void
70 #ifdef HAVE___BUILTIN_UNREACHABLE
71 #define unreachable(str) \
74 __builtin_unreachable(); \
76 #elif _MSC_VER >= 1200
77 #define unreachable(str) \
85 #define unreachable(str)
89 * Assume macro. Useful for expressing our assumptions to the compiler,
90 * typically for purposes of silencing warnings.
92 #if __has_builtin(__builtin_assume)
93 #define assume(expr) \
96 __builtin_assume(expr); \
98 #elif defined HAVE___BUILTIN_UNREACHABLE
99 #define assume(expr) ((expr) ? ((void) 0) \
100 : (assert(!"assumption failed"), \
101 __builtin_unreachable()))
102 #elif _MSC_VER >= 1200
103 #define assume(expr) __assume(expr)
105 #define assume(expr) assert(expr)
108 #ifdef HAVE_FUNC_ATTRIBUTE_FLATTEN
109 #define FLATTEN __attribute__((__flatten__))
114 #ifdef HAVE_FUNC_ATTRIBUTE_FORMAT
115 #define PRINTFLIKE(f, a) __attribute__ ((format(__printf__, f, a)))
117 #define PRINTFLIKE(f, a)
120 #ifdef HAVE_FUNC_ATTRIBUTE_MALLOC
121 #define MALLOCLIKE __attribute__((__malloc__))
126 /* Used to optionally mark structures with misaligned elements or size as
127 * packed, to trade off performance for space.
129 #ifdef HAVE_FUNC_ATTRIBUTE_PACKED
130 #define PACKED __attribute__((__packed__))
137 * Macro function that evaluates to true if T is a trivially
138 * destructible type -- that is, if its (non-virtual) destructor
139 * performs no action and all member variables and base classes are
140 * trivially destructible themselves.
142 # if defined(__GNUC__)
143 # if ((__GNUC__ > 4) || ((__GNUC__ == 4) && (__GNUC_MINOR__ >= 3)))
144 # define HAS_TRIVIAL_DESTRUCTOR(T) __has_trivial_destructor(T)
146 # elif (defined(__clang__) && defined(__has_feature))
147 # if __has_feature(has_trivial_destructor)
148 # define HAS_TRIVIAL_DESTRUCTOR(T) __has_trivial_destructor(T)
151 # ifndef HAS_TRIVIAL_DESTRUCTOR
152 /* It's always safe (if inefficient) to assume that a
153 * destructor is non-trivial.
155 # define HAS_TRIVIAL_DESTRUCTOR(T) (false)
159 #endif /* UTIL_MACROS_H */