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
29 /* Compute the size of an array */
31 # define ARRAY_SIZE(x) (sizeof(x) / sizeof(*(x)))
34 /* For compatibility with Clang's __has_builtin() */
36 # define __has_builtin(x) 0
40 * __builtin_expect macros
42 #if !defined(HAVE___BUILTIN_EXPECT)
43 # define __builtin_expect(x, y) (x)
47 # ifdef HAVE___BUILTIN_EXPECT
48 # define likely(x) __builtin_expect(!!(x), 1)
49 # define unlikely(x) __builtin_expect(!!(x), 0)
51 # define likely(x) (x)
52 # define unlikely(x) (x)
58 * Static (compile-time) assertion.
59 * Basically, use COND to dimension an array. If COND is false/zero the
60 * array size will be -1 and we'll get a compilation error.
62 #define STATIC_ASSERT(COND) \
64 (void) sizeof(char [1 - 2*!(COND)]); \
69 * Unreachable macro. Useful for suppressing "control reaches end of non-void
72 #ifdef HAVE___BUILTIN_UNREACHABLE
73 #define unreachable(str) \
76 __builtin_unreachable(); \
78 #elif defined (_MSC_VER)
79 #define unreachable(str) \
85 #define unreachable(str) assert(!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 defined (_MSC_VER)
103 #define assume(expr) __assume(expr)
105 #define assume(expr) assert(expr)
108 /* Attribute const is used for functions that have no effects other than their
109 * return value, and only rely on the argument values to compute the return
110 * value. As a result, calls to it can be CSEed. Note that using memory
111 * pointed to by the arguments is not allowed for const functions.
113 #ifdef HAVE_FUNC_ATTRIBUTE_CONST
114 #define ATTRIBUTE_CONST __attribute__((__const__))
116 #define ATTRIBUTE_CONST
119 #ifdef HAVE_FUNC_ATTRIBUTE_FLATTEN
120 #define FLATTEN __attribute__((__flatten__))
125 #ifdef HAVE_FUNC_ATTRIBUTE_FORMAT
126 #define PRINTFLIKE(f, a) __attribute__ ((format(__printf__, f, a)))
128 #define PRINTFLIKE(f, a)
131 #ifdef HAVE_FUNC_ATTRIBUTE_MALLOC
132 #define MALLOCLIKE __attribute__((__malloc__))
137 /* Used to optionally mark structures with misaligned elements or size as
138 * packed, to trade off performance for space.
140 #ifdef HAVE_FUNC_ATTRIBUTE_PACKED
141 #define PACKED __attribute__((__packed__))
146 /* Attribute pure is used for functions that have no effects other than their
147 * return value. As a result, calls to it can be dead code eliminated.
149 #ifdef HAVE_FUNC_ATTRIBUTE_PURE
150 #define ATTRIBUTE_PURE __attribute__((__pure__))
152 #define ATTRIBUTE_PURE
157 * Macro function that evaluates to true if T is a trivially
158 * destructible type -- that is, if its (non-virtual) destructor
159 * performs no action and all member variables and base classes are
160 * trivially destructible themselves.
162 # if defined(__GNUC__)
163 # if ((__GNUC__ > 4) || ((__GNUC__ == 4) && (__GNUC_MINOR__ >= 3)))
164 # define HAS_TRIVIAL_DESTRUCTOR(T) __has_trivial_destructor(T)
166 # elif (defined(__clang__) && defined(__has_feature))
167 # if __has_feature(has_trivial_destructor)
168 # define HAS_TRIVIAL_DESTRUCTOR(T) __has_trivial_destructor(T)
171 # ifndef HAS_TRIVIAL_DESTRUCTOR
172 /* It's always safe (if inefficient) to assume that a
173 * destructor is non-trivial.
175 # define HAS_TRIVIAL_DESTRUCTOR(T) (false)
182 * If we build the library with gcc's -fvisibility=hidden flag, we'll
183 * use the PUBLIC macro to mark functions that are to be exported.
185 * We also need to define a USED attribute, so the optimizer doesn't
186 * inline a static function that we later use in an alias. - ajax
189 # if defined(__GNUC__)
190 # define PUBLIC __attribute__((visibility("default")))
191 # define USED __attribute__((used))
192 # elif defined(_MSC_VER)
193 # define PUBLIC __declspec(dllexport)
201 #ifdef HAVE_FUNC_ATTRIBUTE_UNUSED
202 #define UNUSED __attribute__((unused))
207 #ifdef HAVE_FUNC_ATTRIBUTE_WARN_UNUSED_RESULT
208 #define MUST_CHECK __attribute__((warn_unused_result))
213 /** Compute ceiling of integer quotient of A divided by B. */
214 #define DIV_ROUND_UP( A, B ) ( (A) % (B) == 0 ? (A)/(B) : (A)/(B)+1 )
216 #endif /* UTIL_MACROS_H */