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)))
34 * __builtin_expect macros
36 #if !defined(__GNUC__)
37 # define __builtin_expect(x, y) (x)
42 # define likely(x) __builtin_expect(!!(x), 1)
43 # define unlikely(x) __builtin_expect(!!(x), 0)
45 # define likely(x) (x)
46 # define unlikely(x) (x)
52 * Static (compile-time) assertion.
53 * Basically, use COND to dimension an array. If COND is false/zero the
54 * array size will be -1 and we'll get a compilation error.
56 #define STATIC_ASSERT(COND) \
58 (void) sizeof(char [1 - 2*!(COND)]); \
63 * Unreachable macro. Useful for suppressing "control reaches end of non-void
66 #if __GNUC__ >= 4 && __GNUC_MINOR__ >= 5
67 #define unreachable(str) \
70 __builtin_unreachable(); \
72 #elif (defined(__clang__) && defined(__has_builtin))
73 # if __has_builtin(__builtin_unreachable)
74 # define unreachable(str) \
77 __builtin_unreachable(); \
83 #define unreachable(str)
88 #define PRINTFLIKE(f, a) __attribute__ ((format(__printf__, f, a)))
90 #define PRINTFLIKE(f, a)
94 /* Used to optionally mark structures with misaligned elements or size as
95 * packed, to trade off performance for space.
98 #define PACKED __attribute__((__packed__))
106 * Macro function that evaluates to true if T is a trivially
107 * destructible type -- that is, if its (non-virtual) destructor
108 * performs no action and all member variables and base classes are
109 * trivially destructible themselves.
111 # if defined(__GNUC__)
112 # if ((__GNUC__ > 4) || ((__GNUC__ == 4) && (__GNUC_MINOR__ >= 3)))
113 # define HAS_TRIVIAL_DESTRUCTOR(T) __has_trivial_destructor(T)
115 # elif (defined(__clang__) && defined(__has_feature))
116 # if __has_feature(has_trivial_destructor)
117 # define HAS_TRIVIAL_DESTRUCTOR(T) __has_trivial_destructor(T)
120 # ifndef HAS_TRIVIAL_DESTRUCTOR
121 /* It's always safe (if inefficient) to assume that a
122 * destructor is non-trivial.
124 # define HAS_TRIVIAL_DESTRUCTOR(T) (false)
128 #endif /* UTIL_MACROS_H */