util: Include assert.h in macros.h.
[mesa.git] / src / util / macros.h
1 /*
2 * Copyright © 2014 Intel Corporation
3 *
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:
10 *
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
13 * Software.
14 *
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
21 * IN THE SOFTWARE.
22 */
23
24 #ifndef UTIL_MACROS_H
25 #define UTIL_MACROS_H
26
27 #include <assert.h>
28
29 /* Compute the size of an array */
30 #ifndef ARRAY_SIZE
31 # define ARRAY_SIZE(x) (sizeof(x) / sizeof(*(x)))
32 #endif
33
34 /* For compatibility with Clang's __has_builtin() */
35 #ifndef __has_builtin
36 # define __has_builtin(x) 0
37 #endif
38
39 /**
40 * __builtin_expect macros
41 */
42 #if !defined(HAVE___BUILTIN_EXPECT)
43 # define __builtin_expect(x, y) (x)
44 #endif
45
46 #ifndef likely
47 # ifdef HAVE___BUILTIN_EXPECT
48 # define likely(x) __builtin_expect(!!(x), 1)
49 # define unlikely(x) __builtin_expect(!!(x), 0)
50 # else
51 # define likely(x) (x)
52 # define unlikely(x) (x)
53 # endif
54 #endif
55
56
57 /**
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.
61 */
62 #define STATIC_ASSERT(COND) \
63 do { \
64 (void) sizeof(char [1 - 2*!(COND)]); \
65 } while (0)
66
67
68 /**
69 * Unreachable macro. Useful for suppressing "control reaches end of non-void
70 * function" warnings.
71 */
72 #ifdef HAVE___BUILTIN_UNREACHABLE
73 #define unreachable(str) \
74 do { \
75 assert(!str); \
76 __builtin_unreachable(); \
77 } while (0)
78 #elif defined (_MSC_VER)
79 #define unreachable(str) \
80 do { \
81 assert(!str); \
82 __assume(0); \
83 } while (0)
84 #else
85 #define unreachable(str) assert(!str)
86 #endif
87
88 /**
89 * Assume macro. Useful for expressing our assumptions to the compiler,
90 * typically for purposes of silencing warnings.
91 */
92 #if __has_builtin(__builtin_assume)
93 #define assume(expr) \
94 do { \
95 assert(expr); \
96 __builtin_assume(expr); \
97 } while (0)
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)
104 #else
105 #define assume(expr) assert(expr)
106 #endif
107
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.
112 */
113 #ifdef HAVE_FUNC_ATTRIBUTE_CONST
114 #define ATTRIBUTE_CONST __attribute__((__const__))
115 #else
116 #define ATTRIBUTE_CONST
117 #endif
118
119 #ifdef HAVE_FUNC_ATTRIBUTE_FLATTEN
120 #define FLATTEN __attribute__((__flatten__))
121 #else
122 #define FLATTEN
123 #endif
124
125 #ifdef HAVE_FUNC_ATTRIBUTE_FORMAT
126 #define PRINTFLIKE(f, a) __attribute__ ((format(__printf__, f, a)))
127 #else
128 #define PRINTFLIKE(f, a)
129 #endif
130
131 #ifdef HAVE_FUNC_ATTRIBUTE_MALLOC
132 #define MALLOCLIKE __attribute__((__malloc__))
133 #else
134 #define MALLOCLIKE
135 #endif
136
137 /* Used to optionally mark structures with misaligned elements or size as
138 * packed, to trade off performance for space.
139 */
140 #ifdef HAVE_FUNC_ATTRIBUTE_PACKED
141 #define PACKED __attribute__((__packed__))
142 #else
143 #define PACKED
144 #endif
145
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.
148 */
149 #ifdef HAVE_FUNC_ATTRIBUTE_PURE
150 #define ATTRIBUTE_PURE __attribute__((__pure__))
151 #else
152 #define ATTRIBUTE_PURE
153 #endif
154
155 #ifdef __cplusplus
156 /**
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.
161 */
162 # if defined(__GNUC__)
163 # if ((__GNUC__ > 4) || ((__GNUC__ == 4) && (__GNUC_MINOR__ >= 3)))
164 # define HAS_TRIVIAL_DESTRUCTOR(T) __has_trivial_destructor(T)
165 # endif
166 # elif (defined(__clang__) && defined(__has_feature))
167 # if __has_feature(has_trivial_destructor)
168 # define HAS_TRIVIAL_DESTRUCTOR(T) __has_trivial_destructor(T)
169 # endif
170 # endif
171 # ifndef HAS_TRIVIAL_DESTRUCTOR
172 /* It's always safe (if inefficient) to assume that a
173 * destructor is non-trivial.
174 */
175 # define HAS_TRIVIAL_DESTRUCTOR(T) (false)
176 # endif
177 #endif
178
179 /**
180 * PUBLIC/USED macros
181 *
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.
184 *
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
187 */
188 #ifndef PUBLIC
189 # if defined(__GNUC__) || (defined(__SUNPRO_C) && (__SUNPRO_C >= 0x590))
190 # define PUBLIC __attribute__((visibility("default")))
191 # define USED __attribute__((used))
192 # elif defined(_MSC_VER)
193 # define PUBLIC __declspec(dllexport)
194 # define USED
195 # else
196 # define PUBLIC
197 # define USED
198 # endif
199 #endif
200
201 #ifdef HAVE_FUNC_ATTRIBUTE_UNUSED
202 #define UNUSED __attribute__((unused))
203 #else
204 #define UNUSED
205 #endif
206
207 #ifdef HAVE_FUNC_ATTRIBUTE_WARN_UNUSED_RESULT
208 #define MUST_CHECK __attribute__((warn_unused_result))
209 #else
210 #define MUST_CHECK
211 #endif
212
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 )
215
216 #endif /* UTIL_MACROS_H */