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 defined (_MSC_VER)
77 #define unreachable(str) \
83 #define unreachable(str) assert(!str)
87 * Assume macro. Useful for expressing our assumptions to the compiler,
88 * typically for purposes of silencing warnings.
90 #if __has_builtin(__builtin_assume)
91 #define assume(expr) \
94 __builtin_assume(expr); \
96 #elif defined HAVE___BUILTIN_UNREACHABLE
97 #define assume(expr) ((expr) ? ((void) 0) \
98 : (assert(!"assumption failed"), \
99 __builtin_unreachable()))
100 #elif defined (_MSC_VER)
101 #define assume(expr) __assume(expr)
103 #define assume(expr) assert(expr)
106 /* Attribute const is used for functions that have no effects other than their
107 * return value, and only rely on the argument values to compute the return
108 * value. As a result, calls to it can be CSEed. Note that using memory
109 * pointed to by the arguments is not allowed for const functions.
111 #ifdef HAVE_FUNC_ATTRIBUTE_CONST
112 #define ATTRIBUTE_CONST __attribute__((__const__))
114 #define ATTRIBUTE_CONST
117 #ifdef HAVE_FUNC_ATTRIBUTE_FLATTEN
118 #define FLATTEN __attribute__((__flatten__))
123 #ifdef HAVE_FUNC_ATTRIBUTE_FORMAT
124 #define PRINTFLIKE(f, a) __attribute__ ((format(__printf__, f, a)))
126 #define PRINTFLIKE(f, a)
129 #ifdef HAVE_FUNC_ATTRIBUTE_MALLOC
130 #define MALLOCLIKE __attribute__((__malloc__))
135 /* Used to optionally mark structures with misaligned elements or size as
136 * packed, to trade off performance for space.
138 #ifdef HAVE_FUNC_ATTRIBUTE_PACKED
139 #define PACKED __attribute__((__packed__))
144 /* Attribute pure is used for functions that have no effects other than their
145 * return value. As a result, calls to it can be dead code eliminated.
147 #ifdef HAVE_FUNC_ATTRIBUTE_PURE
148 #define PURE __attribute__((__pure__))
155 * Macro function that evaluates to true if T is a trivially
156 * destructible type -- that is, if its (non-virtual) destructor
157 * performs no action and all member variables and base classes are
158 * trivially destructible themselves.
160 # if defined(__GNUC__)
161 # if ((__GNUC__ > 4) || ((__GNUC__ == 4) && (__GNUC_MINOR__ >= 3)))
162 # define HAS_TRIVIAL_DESTRUCTOR(T) __has_trivial_destructor(T)
164 # elif (defined(__clang__) && defined(__has_feature))
165 # if __has_feature(has_trivial_destructor)
166 # define HAS_TRIVIAL_DESTRUCTOR(T) __has_trivial_destructor(T)
169 # ifndef HAS_TRIVIAL_DESTRUCTOR
170 /* It's always safe (if inefficient) to assume that a
171 * destructor is non-trivial.
173 # define HAS_TRIVIAL_DESTRUCTOR(T) (false)
180 * If we build the library with gcc's -fvisibility=hidden flag, we'll
181 * use the PUBLIC macro to mark functions that are to be exported.
183 * We also need to define a USED attribute, so the optimizer doesn't
184 * inline a static function that we later use in an alias. - ajax
187 # if defined(__GNUC__) || (defined(__SUNPRO_C) && (__SUNPRO_C >= 0x590))
188 # define PUBLIC __attribute__((visibility("default")))
189 # define USED __attribute__((used))
190 # elif defined(_MSC_VER)
191 # define PUBLIC __declspec(dllexport)
199 #ifdef HAVE_FUNC_ATTRIBUTE_UNUSED
200 #define UNUSED __attribute__((unused))
205 #ifdef HAVE_FUNC_ATTRIBUTE_WARN_UNUSED_RESULT
206 #define MUST_CHECK __attribute__((warn_unused_result))
211 /** Compute ceiling of integer quotient of A divided by B. */
212 #define DIV_ROUND_UP( A, B ) ( (A) % (B) == 0 ? (A)/(B) : (A)/(B)+1 )
214 #endif /* UTIL_MACROS_H */