blob: c80d35e42b62d2524ae839f1b8a40b554b8397ec [file] [log] [blame]
Elliott Hughes2faa5f12012-01-30 14:42:07 -08001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Carl Shapiro6c21dc12011-06-20 15:20:52 -070016
Brian Carlstromfc0e3212013-07-17 14:40:12 -070017#ifndef ART_RUNTIME_BASE_MACROS_H_
18#define ART_RUNTIME_BASE_MACROS_H_
Carl Shapiro6c21dc12011-06-20 15:20:52 -070019
Carl Shapiro12eb78e2011-06-24 14:51:06 -070020#include <stddef.h> // for size_t
Mark Salyzyn47a4cc72014-05-22 16:27:06 -070021#include <unistd.h> // for TEMP_FAILURE_RETRY
22
23// bionic and glibc both have TEMP_FAILURE_RETRY, but eg Mac OS' libc doesn't.
24#ifndef TEMP_FAILURE_RETRY
25#define TEMP_FAILURE_RETRY(exp) ({ \
26 decltype(exp) _rc; \
27 do { \
28 _rc = (exp); \
29 } while (_rc == -1 && errno == EINTR); \
30 _rc; })
31#endif
Carl Shapiro12eb78e2011-06-24 14:51:06 -070032
Brian Carlstrom7a00a3c2012-01-25 18:38:03 -080033#define GCC_VERSION (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__)
Shih-wei Liao24782c62012-01-08 12:46:11 -080034
Ian Rogers6fac4472014-02-25 17:01:10 -080035// C++11 final and override keywords that were introduced in GCC version 4.7.
Ian Rogers9758f792014-03-13 09:02:55 -070036#if defined(__clang__) || GCC_VERSION >= 40700
Ian Rogers6fac4472014-02-25 17:01:10 -080037#define OVERRIDE override
38#define FINAL final
39#else
40#define OVERRIDE
41#define FINAL
42#endif
43
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -070044// The COMPILE_ASSERT macro can be used to verify that a compile time
45// expression is true. For example, you could use it to verify the
46// size of a static array:
47//
48// COMPILE_ASSERT(ARRAYSIZE(content_type_names) == CONTENT_NUM_TYPES,
49// content_type_names_incorrect_size);
50//
51// or to make sure a struct is smaller than a certain size:
52//
53// COMPILE_ASSERT(sizeof(foo) < 128, foo_too_large);
54//
55// The second argument to the macro is the name of the variable. If
56// the expression is false, most compilers will issue a warning/error
57// containing the name of the variable.
58
59template <bool>
60struct CompileAssert {
61};
62
63#define COMPILE_ASSERT(expr, msg) \
Elliott Hughes398f64b2012-03-26 18:05:48 -070064 typedef CompileAssert<(bool(expr))> msg[bool(expr) ? 1 : -1] // NOLINT
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -070065
Ian Rogers6f3dbba2014-10-14 17:41:57 -070066// Declare a friend relationship in a class with a test. Used rather that FRIEND_TEST to avoid
67// globally importing gtest/gtest.h into the main ART header files.
68#define ART_FRIEND_TEST(test_set_name, individual_test)\
69friend class test_set_name##_##individual_test##_Test
70
Carl Shapiro6c21dc12011-06-20 15:20:52 -070071// DISALLOW_COPY_AND_ASSIGN disallows the copy and operator= functions.
72// It goes in the private: declarations in a class.
73#define DISALLOW_COPY_AND_ASSIGN(TypeName) \
74 TypeName(const TypeName&); \
75 void operator=(const TypeName&)
76
77// A macro to disallow all the implicit constructors, namely the
78// default constructor, copy constructor and operator= functions.
79//
80// This should be used in the private: declarations for a class
81// that wants to prevent anyone from instantiating it. This is
82// especially useful for classes containing only static methods.
83#define DISALLOW_IMPLICIT_CONSTRUCTORS(TypeName) \
84 TypeName(); \
85 DISALLOW_COPY_AND_ASSIGN(TypeName)
86
Carl Shapiroa2e18e12011-06-21 18:57:55 -070087// The arraysize(arr) macro returns the # of elements in an array arr.
88// The expression is a compile-time constant, and therefore can be
89// used in defining new arrays, for example. If you use arraysize on
90// a pointer by mistake, you will get a compile-time error.
91//
92// One caveat is that arraysize() doesn't accept any array of an
93// anonymous type or a type defined inside a function. In these rare
Carl Shapirod2bdb572011-06-22 11:45:37 -070094// cases, you have to use the unsafe ARRAYSIZE_UNSAFE() macro below. This is
Carl Shapiroa2e18e12011-06-21 18:57:55 -070095// due to a limitation in C++'s template system. The limitation might
96// eventually be removed, but it hasn't happened yet.
97
98// This template function declaration is used in defining arraysize.
99// Note that the function doesn't need an implementation, as we only
100// use its type.
101template <typename T, size_t N>
102char (&ArraySizeHelper(T (&array)[N]))[N];
103
104#define arraysize(array) (sizeof(ArraySizeHelper(array)))
105
Carl Shapirod2bdb572011-06-22 11:45:37 -0700106// ARRAYSIZE_UNSAFE performs essentially the same calculation as arraysize,
107// but can be used on anonymous types or types defined inside
108// functions. It's less safe than arraysize as it accepts some
109// (although not all) pointers. Therefore, you should use arraysize
110// whenever possible.
111//
112// The expression ARRAYSIZE_UNSAFE(a) is a compile-time constant of type
113// size_t.
114//
115// ARRAYSIZE_UNSAFE catches a few type errors. If you see a compiler error
116//
117// "warning: division by zero in ..."
118//
119// when using ARRAYSIZE_UNSAFE, you are (wrongfully) giving it a pointer.
120// You should only use ARRAYSIZE_UNSAFE on statically allocated arrays.
121//
122// The following comments are on the implementation details, and can
123// be ignored by the users.
124//
125// ARRAYSIZE_UNSAFE(arr) works by inspecting sizeof(arr) (the # of bytes in
126// the array) and sizeof(*(arr)) (the # of bytes in one array
127// element). If the former is divisible by the latter, perhaps arr is
128// indeed an array, in which case the division result is the # of
129// elements in the array. Otherwise, arr cannot possibly be an array,
130// and we generate a compiler error to prevent the code from
131// compiling.
132//
133// Since the size of bool is implementation-defined, we need to cast
134// !(sizeof(a) & sizeof(*(a))) to size_t in order to ensure the final
135// result has type size_t.
136//
137// This macro is not perfect as it wrongfully accepts certain
138// pointers, namely where the pointer size is divisible by the pointee
139// size. Since all our code has to go through a 32-bit compiler,
140// where a pointer is 4 bytes, this means all pointers to a type whose
141// size is 3 or greater than 4 will be (righteously) rejected.
142#define ARRAYSIZE_UNSAFE(a) \
Elliott Hughes362f9bc2011-10-17 18:56:41 -0700143 ((sizeof(a) / sizeof(*(a))) / static_cast<size_t>(!(sizeof(a) % sizeof(*(a)))))
Carl Shapirod2bdb572011-06-22 11:45:37 -0700144
Elliott Hughesff17f1f2012-01-24 18:12:29 -0800145#define SIZEOF_MEMBER(t, f) sizeof((reinterpret_cast<t*>(4096))->f)
Carl Shapiro59e85cd2011-06-21 10:16:23 -0700146
Elliott Hughes362f9bc2011-10-17 18:56:41 -0700147#define OFFSETOF_MEMBER(t, f) \
Ian Rogers8a01a3a2013-05-06 13:25:44 -0700148 (reinterpret_cast<const char*>(&reinterpret_cast<t*>(16)->f) - reinterpret_cast<const char*>(16)) // NOLINT
Carl Shapiro59e85cd2011-06-21 10:16:23 -0700149
Elliott Hughes93e74e82011-09-13 11:07:03 -0700150#define OFFSETOF_VOLATILE_MEMBER(t, f) \
Elliott Hughes398f64b2012-03-26 18:05:48 -0700151 (reinterpret_cast<volatile char*>(&reinterpret_cast<t*>(16)->f) - reinterpret_cast<volatile char*>(16)) // NOLINT
Elliott Hughes93e74e82011-09-13 11:07:03 -0700152
Brian Carlstromb1eba212013-07-17 18:07:19 -0700153#define PACKED(x) __attribute__ ((__aligned__(x), __packed__))
Elliott Hughes85d15452011-09-16 17:33:01 -0700154
Elliott Hughesff17f1f2012-01-24 18:12:29 -0800155#define LIKELY(x) __builtin_expect((x), true)
156#define UNLIKELY(x) __builtin_expect((x), false)
Ian Rogerscaab8c42011-10-12 12:11:18 -0700157
Dave Allison70202782013-10-22 17:52:19 -0700158// Stringify the argument.
159#define QUOTE(x) #x
160#define STRINGIFY(x) QUOTE(x)
161
Ian Rogerse8ae0dc2013-02-07 10:20:45 -0800162#ifndef NDEBUG
Ian Rogers1ffa32f2013-02-05 18:29:08 -0800163#define ALWAYS_INLINE
164#else
Ian Rogerse8ae0dc2013-02-07 10:20:45 -0800165#define ALWAYS_INLINE __attribute__ ((always_inline))
Ian Rogers1ffa32f2013-02-05 18:29:08 -0800166#endif
167
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100168#ifdef __clang__
169/* clang doesn't like attributes on lambda functions */
170#define ALWAYS_INLINE_LAMBDA
171#else
172#define ALWAYS_INLINE_LAMBDA ALWAYS_INLINE
173#endif
174
Anwar Ghuloum63937db2013-05-24 09:08:32 -0700175#if defined (__APPLE__)
Anwar Ghuloum1d9314c2013-05-24 10:44:48 -0700176#define HOT_ATTR
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700177#define COLD_ATTR
Anwar Ghuloum63937db2013-05-24 09:08:32 -0700178#else
Anwar Ghuloum1d9314c2013-05-24 10:44:48 -0700179#define HOT_ATTR __attribute__ ((hot))
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700180#define COLD_ATTR __attribute__ ((cold))
Anwar Ghuloum63937db2013-05-24 09:08:32 -0700181#endif
182
Ian Rogers96faf5b2013-08-09 22:05:32 -0700183#define PURE __attribute__ ((__pure__))
Mathieu Chartier4c13a3f2014-07-14 14:57:16 -0700184#define WARN_UNUSED __attribute__((warn_unused_result))
Ian Rogers96faf5b2013-08-09 22:05:32 -0700185
Elliott Hughesc151f902012-06-21 20:33:21 -0700186template<typename T> void UNUSED(const T&) {}
Ian Rogers07140832014-09-30 15:43:59 -0700187#define UNREACHABLE __builtin_unreachable
Elliott Hughesc151f902012-06-21 20:33:21 -0700188
Ian Rogersfc787ec2014-10-09 21:56:44 -0700189// The FALLTHROUGH_INTENDED macro can be used to annotate implicit fall-through
190// between switch labels:
191// switch (x) {
192// case 40:
193// case 41:
194// if (truth_is_out_there) {
195// ++x;
196// FALLTHROUGH_INTENDED; // Use instead of/along with annotations in
197// // comments.
198// } else {
199// return x;
200// }
201// case 42:
202// ...
203//
204// As shown in the example above, the FALLTHROUGH_INTENDED macro should be
205// followed by a semicolon. It is designed to mimic control-flow statements
206// like 'break;', so it can be placed in most places where 'break;' can, but
207// only if there are no statements on the execution path between it and the
208// next switch label.
209//
210// When compiled with clang in C++11 mode, the FALLTHROUGH_INTENDED macro is
211// expanded to [[clang::fallthrough]] attribute, which is analysed when
212// performing switch labels fall-through diagnostic ('-Wimplicit-fallthrough').
213// See clang documentation on language extensions for details:
214// http://clang.llvm.org/docs/LanguageExtensions.html#clang__fallthrough
215//
216// When used with unsupported compilers, the FALLTHROUGH_INTENDED macro has no
217// effect on diagnostics.
218//
219// In either case this macro has no effect on runtime behavior and performance
220// of code.
221#if defined(__clang__) && __cplusplus >= 201103L && defined(__has_warning)
222#if __has_feature(cxx_attributes) && __has_warning("-Wimplicit-fallthrough")
223#define FALLTHROUGH_INTENDED [[clang::fallthrough]] // NOLINT
224#endif
225#endif
226
227#ifndef FALLTHROUGH_INTENDED
228#define FALLTHROUGH_INTENDED do { } while (0)
229#endif
230
Ian Rogers719d1a32014-03-06 12:13:39 -0800231// Annotalysis thread-safety analysis support.
232#if defined(__SUPPORT_TS_ANNOTATION__) || defined(__clang__)
233#define THREAD_ANNOTATION_ATTRIBUTE__(x) __attribute__((x))
Elliott Hughesf8349362012-06-18 15:00:06 -0700234#else
Ian Rogers719d1a32014-03-06 12:13:39 -0800235#define THREAD_ANNOTATION_ATTRIBUTE__(x) // no-op
236#endif
Elliott Hughesf8349362012-06-18 15:00:06 -0700237
Ian Rogers719d1a32014-03-06 12:13:39 -0800238#define ACQUIRED_AFTER(...) THREAD_ANNOTATION_ATTRIBUTE__(acquired_after(__VA_ARGS__))
239#define ACQUIRED_BEFORE(...) THREAD_ANNOTATION_ATTRIBUTE__(acquired_before(__VA_ARGS__))
240#define EXCLUSIVE_LOCKS_REQUIRED(...) THREAD_ANNOTATION_ATTRIBUTE__(exclusive_locks_required(__VA_ARGS__))
241#define GUARDED_BY(x) THREAD_ANNOTATION_ATTRIBUTE__(guarded_by(x))
242#define GUARDED_VAR THREAD_ANNOTATION_ATTRIBUTE__(guarded)
243#define LOCKABLE THREAD_ANNOTATION_ATTRIBUTE__(lockable)
244#define LOCK_RETURNED(x) THREAD_ANNOTATION_ATTRIBUTE__(lock_returned(x))
245#define LOCKS_EXCLUDED(...) THREAD_ANNOTATION_ATTRIBUTE__(locks_excluded(__VA_ARGS__))
246#define NO_THREAD_SAFETY_ANALYSIS THREAD_ANNOTATION_ATTRIBUTE__(no_thread_safety_analysis)
Elliott Hughesf8349362012-06-18 15:00:06 -0700247#define PT_GUARDED_BY(x)
Ian Rogers719d1a32014-03-06 12:13:39 -0800248// THREAD_ANNOTATION_ATTRIBUTE__(point_to_guarded_by(x))
249#define PT_GUARDED_VAR THREAD_ANNOTATION_ATTRIBUTE__(point_to_guarded)
250#define SCOPED_LOCKABLE THREAD_ANNOTATION_ATTRIBUTE__(scoped_lockable)
251#define SHARED_LOCKS_REQUIRED(...) THREAD_ANNOTATION_ATTRIBUTE__(shared_locks_required(__VA_ARGS__))
Elliott Hughesf8349362012-06-18 15:00:06 -0700252
Ian Rogers719d1a32014-03-06 12:13:39 -0800253#if defined(__clang__)
254#define EXCLUSIVE_LOCK_FUNCTION(...) THREAD_ANNOTATION_ATTRIBUTE__(exclusive_lock_function(__VA_ARGS__))
255#define EXCLUSIVE_TRYLOCK_FUNCTION(...) THREAD_ANNOTATION_ATTRIBUTE__(exclusive_trylock_function(__VA_ARGS__))
256#define SHARED_LOCK_FUNCTION(...) THREAD_ANNOTATION_ATTRIBUTE__(shared_lock_function(__VA_ARGS__))
257#define SHARED_TRYLOCK_FUNCTION(...) THREAD_ANNOTATION_ATTRIBUTE__(shared_trylock_function(__VA_ARGS__))
258#define UNLOCK_FUNCTION(...) THREAD_ANNOTATION_ATTRIBUTE__(unlock_function(__VA_ARGS__))
259#else
260#define EXCLUSIVE_LOCK_FUNCTION(...) THREAD_ANNOTATION_ATTRIBUTE__(exclusive_lock(__VA_ARGS__))
261#define EXCLUSIVE_TRYLOCK_FUNCTION(...) THREAD_ANNOTATION_ATTRIBUTE__(exclusive_trylock(__VA_ARGS__))
262#define SHARED_LOCK_FUNCTION(...) THREAD_ANNOTATION_ATTRIBUTE__(shared_lock(__VA_ARGS__))
263#define SHARED_TRYLOCK_FUNCTION(...) THREAD_ANNOTATION_ATTRIBUTE__(shared_trylock(__VA_ARGS__))
264#define UNLOCK_FUNCTION(...) THREAD_ANNOTATION_ATTRIBUTE__(unlock(__VA_ARGS__))
265#endif
Elliott Hughesf8349362012-06-18 15:00:06 -0700266
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700267#endif // ART_RUNTIME_BASE_MACROS_H_