blob: 0ec6e6de970d8c8cd26cb9673290b6baf35c1557 [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
Ian Rogers6fac4472014-02-25 17:01:10 -080033#define OVERRIDE override
34#define FINAL final
Ian Rogers6fac4472014-02-25 17:01:10 -080035
Ian Rogers6f3dbba2014-10-14 17:41:57 -070036// Declare a friend relationship in a class with a test. Used rather that FRIEND_TEST to avoid
37// globally importing gtest/gtest.h into the main ART header files.
38#define ART_FRIEND_TEST(test_set_name, individual_test)\
39friend class test_set_name##_##individual_test##_Test
40
Zheng Xuad4450e2015-04-17 18:48:56 +080041// Declare a friend relationship in a class with a typed test.
42#define ART_FRIEND_TYPED_TEST(test_set_name, individual_test)\
43template<typename T> ART_FRIEND_TEST(test_set_name, individual_test)
44
Ian Rogerscf7f1912014-10-22 22:06:39 -070045// DISALLOW_COPY_AND_ASSIGN disallows the copy and operator= functions. It goes in the private:
46// declarations in a class.
Ian Rogers3eaa8522014-11-04 13:20:30 -080047#if !defined(DISALLOW_COPY_AND_ASSIGN)
Carl Shapiro6c21dc12011-06-20 15:20:52 -070048#define DISALLOW_COPY_AND_ASSIGN(TypeName) \
Ian Rogerscf7f1912014-10-22 22:06:39 -070049 TypeName(const TypeName&) = delete; \
50 void operator=(const TypeName&) = delete
Ian Rogers3eaa8522014-11-04 13:20:30 -080051#endif
Carl Shapiro6c21dc12011-06-20 15:20:52 -070052
Ian Rogerscf7f1912014-10-22 22:06:39 -070053// A macro to disallow all the implicit constructors, namely the default constructor, copy
54// constructor and operator= functions.
Carl Shapiro6c21dc12011-06-20 15:20:52 -070055//
Ian Rogerscf7f1912014-10-22 22:06:39 -070056// This should be used in the private: declarations for a class that wants to prevent anyone from
57// instantiating it. This is especially useful for classes containing only static methods.
Carl Shapiro6c21dc12011-06-20 15:20:52 -070058#define DISALLOW_IMPLICIT_CONSTRUCTORS(TypeName) \
Ian Rogerscf7f1912014-10-22 22:06:39 -070059 TypeName() = delete; \
Carl Shapiro6c21dc12011-06-20 15:20:52 -070060 DISALLOW_COPY_AND_ASSIGN(TypeName)
61
Ian Rogerscf7f1912014-10-22 22:06:39 -070062// A macro to disallow new and delete operators for a class. It goes in the private: declarations.
Vladimir Marko76c92ac2015-09-17 15:39:16 +010063// NOTE: Providing placement new (and matching delete) for constructing container elements.
Ian Rogerscf7f1912014-10-22 22:06:39 -070064#define DISALLOW_ALLOCATION() \
65 public: \
Andreas Gampe65b798e2015-04-06 09:35:22 -070066 NO_RETURN ALWAYS_INLINE void operator delete(void*, size_t) { UNREACHABLE(); } \
Vladimir Marko76c92ac2015-09-17 15:39:16 +010067 ALWAYS_INLINE void* operator new(size_t, void* ptr) noexcept { return ptr; } \
68 ALWAYS_INLINE void operator delete(void*, void*) noexcept { } \
Ian Rogerscf7f1912014-10-22 22:06:39 -070069 private: \
Roland Levillain7cbd27f2016-08-11 23:53:33 +010070 void* operator new(size_t) = delete // NOLINT
Ian Rogerscf7f1912014-10-22 22:06:39 -070071
Carl Shapiroa2e18e12011-06-21 18:57:55 -070072// The arraysize(arr) macro returns the # of elements in an array arr.
73// The expression is a compile-time constant, and therefore can be
74// used in defining new arrays, for example. If you use arraysize on
75// a pointer by mistake, you will get a compile-time error.
76//
77// One caveat is that arraysize() doesn't accept any array of an
78// anonymous type or a type defined inside a function. In these rare
Carl Shapirod2bdb572011-06-22 11:45:37 -070079// cases, you have to use the unsafe ARRAYSIZE_UNSAFE() macro below. This is
Carl Shapiroa2e18e12011-06-21 18:57:55 -070080// due to a limitation in C++'s template system. The limitation might
81// eventually be removed, but it hasn't happened yet.
82
83// This template function declaration is used in defining arraysize.
84// Note that the function doesn't need an implementation, as we only
85// use its type.
86template <typename T, size_t N>
87char (&ArraySizeHelper(T (&array)[N]))[N];
88
89#define arraysize(array) (sizeof(ArraySizeHelper(array)))
90
Carl Shapirod2bdb572011-06-22 11:45:37 -070091// ARRAYSIZE_UNSAFE performs essentially the same calculation as arraysize,
92// but can be used on anonymous types or types defined inside
93// functions. It's less safe than arraysize as it accepts some
94// (although not all) pointers. Therefore, you should use arraysize
95// whenever possible.
96//
97// The expression ARRAYSIZE_UNSAFE(a) is a compile-time constant of type
98// size_t.
99//
100// ARRAYSIZE_UNSAFE catches a few type errors. If you see a compiler error
101//
102// "warning: division by zero in ..."
103//
104// when using ARRAYSIZE_UNSAFE, you are (wrongfully) giving it a pointer.
105// You should only use ARRAYSIZE_UNSAFE on statically allocated arrays.
106//
107// The following comments are on the implementation details, and can
108// be ignored by the users.
109//
110// ARRAYSIZE_UNSAFE(arr) works by inspecting sizeof(arr) (the # of bytes in
111// the array) and sizeof(*(arr)) (the # of bytes in one array
112// element). If the former is divisible by the latter, perhaps arr is
113// indeed an array, in which case the division result is the # of
114// elements in the array. Otherwise, arr cannot possibly be an array,
115// and we generate a compiler error to prevent the code from
116// compiling.
117//
118// Since the size of bool is implementation-defined, we need to cast
119// !(sizeof(a) & sizeof(*(a))) to size_t in order to ensure the final
120// result has type size_t.
121//
122// This macro is not perfect as it wrongfully accepts certain
123// pointers, namely where the pointer size is divisible by the pointee
124// size. Since all our code has to go through a 32-bit compiler,
125// where a pointer is 4 bytes, this means all pointers to a type whose
126// size is 3 or greater than 4 will be (righteously) rejected.
127#define ARRAYSIZE_UNSAFE(a) \
Elliott Hughes362f9bc2011-10-17 18:56:41 -0700128 ((sizeof(a) / sizeof(*(a))) / static_cast<size_t>(!(sizeof(a) % sizeof(*(a)))))
Carl Shapirod2bdb572011-06-22 11:45:37 -0700129
Roland Levillain7cbd27f2016-08-11 23:53:33 +0100130#define SIZEOF_MEMBER(t, f) sizeof((reinterpret_cast<t*>(4096))->f) // NOLINT
Carl Shapiro59e85cd2011-06-21 10:16:23 -0700131
Elliott Hughes362f9bc2011-10-17 18:56:41 -0700132#define OFFSETOF_MEMBER(t, f) \
Roland Levillain7cbd27f2016-08-11 23:53:33 +0100133 (reinterpret_cast<uintptr_t>(&reinterpret_cast<t*>(16)->f) - static_cast<uintptr_t>(16u)) // NOLINT
Carl Shapiro59e85cd2011-06-21 10:16:23 -0700134
Vladimir Marko46817b82016-03-29 12:21:58 +0100135#define OFFSETOF_MEMBERPTR(t, f) \
Roland Levillain7cbd27f2016-08-11 23:53:33 +0100136 (reinterpret_cast<uintptr_t>(&(reinterpret_cast<t*>(16)->*f)) - static_cast<uintptr_t>(16)) // NOLINT
Elliott Hughes93e74e82011-09-13 11:07:03 -0700137
Brian Carlstromb1eba212013-07-17 18:07:19 -0700138#define PACKED(x) __attribute__ ((__aligned__(x), __packed__))
Elliott Hughes85d15452011-09-16 17:33:01 -0700139
Elliott Hughesff17f1f2012-01-24 18:12:29 -0800140#define LIKELY(x) __builtin_expect((x), true)
141#define UNLIKELY(x) __builtin_expect((x), false)
Ian Rogerscaab8c42011-10-12 12:11:18 -0700142
Dave Allison70202782013-10-22 17:52:19 -0700143// Stringify the argument.
144#define QUOTE(x) #x
145#define STRINGIFY(x) QUOTE(x)
146
Ian Rogerse8ae0dc2013-02-07 10:20:45 -0800147#ifndef NDEBUG
Ian Rogers1ffa32f2013-02-05 18:29:08 -0800148#define ALWAYS_INLINE
149#else
Ian Rogerse8ae0dc2013-02-07 10:20:45 -0800150#define ALWAYS_INLINE __attribute__ ((always_inline))
Ian Rogers1ffa32f2013-02-05 18:29:08 -0800151#endif
152
Andreas Gampe9231f4e2016-08-23 17:35:19 -0700153// clang doesn't like attributes on lambda functions. It would be nice to say:
154// #define ALWAYS_INLINE_LAMBDA ALWAYS_INLINE
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100155#define ALWAYS_INLINE_LAMBDA
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100156
Andreas Gampe86830382014-12-12 21:41:29 -0800157#define NO_INLINE __attribute__ ((noinline))
158
Anwar Ghuloum63937db2013-05-24 09:08:32 -0700159#if defined (__APPLE__)
Anwar Ghuloum1d9314c2013-05-24 10:44:48 -0700160#define HOT_ATTR
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700161#define COLD_ATTR
Anwar Ghuloum63937db2013-05-24 09:08:32 -0700162#else
Anwar Ghuloum1d9314c2013-05-24 10:44:48 -0700163#define HOT_ATTR __attribute__ ((hot))
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700164#define COLD_ATTR __attribute__ ((cold))
Anwar Ghuloum63937db2013-05-24 09:08:32 -0700165#endif
166
Ian Rogers96faf5b2013-08-09 22:05:32 -0700167#define PURE __attribute__ ((__pure__))
Mathieu Chartier4c13a3f2014-07-14 14:57:16 -0700168#define WARN_UNUSED __attribute__((warn_unused_result))
Ian Rogers96faf5b2013-08-09 22:05:32 -0700169
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700170// A deprecated function to call to create a false use of the parameter, for example:
171// int foo(int x) { UNUSED(x); return 10; }
172// to avoid compiler warnings. Going forward we prefer ATTRIBUTE_UNUSED.
173template<typename... T> void UNUSED(const T&...) {}
174
175// An attribute to place on a parameter to a function, for example:
176// int foo(int x ATTRIBUTE_UNUSED) { return 10; }
177// to avoid compiler warnings.
178#define ATTRIBUTE_UNUSED __attribute__((__unused__))
179
180// Define that a position within code is unreachable, for example:
181// int foo () { LOG(FATAL) << "Don't call me"; UNREACHABLE(); }
182// without the UNREACHABLE a return statement would be necessary.
Ian Rogers07140832014-09-30 15:43:59 -0700183#define UNREACHABLE __builtin_unreachable
Elliott Hughesc151f902012-06-21 20:33:21 -0700184
Andreas Gampe794ad762015-02-23 08:12:24 -0800185// Add the C++11 noreturn attribute.
186#define NO_RETURN [[ noreturn ]] // NOLINT[whitespace/braces] [5]
187
Ian Rogersfc787ec2014-10-09 21:56:44 -0700188// The FALLTHROUGH_INTENDED macro can be used to annotate implicit fall-through
189// between switch labels:
190// switch (x) {
191// case 40:
192// case 41:
193// if (truth_is_out_there) {
194// ++x;
195// FALLTHROUGH_INTENDED; // Use instead of/along with annotations in
196// // comments.
197// } else {
198// return x;
199// }
200// case 42:
201// ...
202//
203// As shown in the example above, the FALLTHROUGH_INTENDED macro should be
204// followed by a semicolon. It is designed to mimic control-flow statements
205// like 'break;', so it can be placed in most places where 'break;' can, but
206// only if there are no statements on the execution path between it and the
207// next switch label.
208//
209// When compiled with clang in C++11 mode, the FALLTHROUGH_INTENDED macro is
210// expanded to [[clang::fallthrough]] attribute, which is analysed when
211// performing switch labels fall-through diagnostic ('-Wimplicit-fallthrough').
212// See clang documentation on language extensions for details:
213// http://clang.llvm.org/docs/LanguageExtensions.html#clang__fallthrough
214//
215// When used with unsupported compilers, the FALLTHROUGH_INTENDED macro has no
216// effect on diagnostics.
217//
218// In either case this macro has no effect on runtime behavior and performance
219// of code.
Ian Rogersfc787ec2014-10-09 21:56:44 -0700220#if __has_feature(cxx_attributes) && __has_warning("-Wimplicit-fallthrough")
221#define FALLTHROUGH_INTENDED [[clang::fallthrough]] // NOLINT
222#endif
Ian Rogersfc787ec2014-10-09 21:56:44 -0700223
224#ifndef FALLTHROUGH_INTENDED
225#define FALLTHROUGH_INTENDED do { } while (0)
226#endif
227
Ian Rogers719d1a32014-03-06 12:13:39 -0800228// Annotalysis thread-safety analysis support.
Elliott Hughesf8349362012-06-18 15:00:06 -0700229
Andreas Gampe9231f4e2016-08-23 17:35:19 -0700230#define ACQUIRED_AFTER(...) __attribute__((acquired_after(__VA_ARGS__)))
231#define ACQUIRED_BEFORE(...) __attribute__((acquired_before(__VA_ARGS__)))
232#define GUARDED_BY(x) __attribute__((guarded_by(x)))
233#define GUARDED_VAR __attribute__((guarded))
234#define LOCK_RETURNED(x) __attribute__((lock_returned(x)))
235#define NO_THREAD_SAFETY_ANALYSIS __attribute__((no_thread_safety_analysis))
Elliott Hughesf8349362012-06-18 15:00:06 -0700236#define PT_GUARDED_BY(x)
Ian Rogers719d1a32014-03-06 12:13:39 -0800237// THREAD_ANNOTATION_ATTRIBUTE__(point_to_guarded_by(x))
Andreas Gampe9231f4e2016-08-23 17:35:19 -0700238#define PT_GUARDED_VAR __attribute__((point_to_guarded))
239#define SCOPED_LOCKABLE __attribute__((scoped_lockable))
Elliott Hughesf8349362012-06-18 15:00:06 -0700240
Andreas Gampe9231f4e2016-08-23 17:35:19 -0700241#define EXCLUSIVE_LOCK_FUNCTION(...) __attribute__((exclusive_lock_function(__VA_ARGS__)))
242#define EXCLUSIVE_TRYLOCK_FUNCTION(...) __attribute__((exclusive_trylock_function(__VA_ARGS__)))
243#define SHARED_LOCK_FUNCTION(...) __attribute__((shared_lock_function(__VA_ARGS__)))
244#define SHARED_TRYLOCK_FUNCTION(...) __attribute__((shared_trylock_function(__VA_ARGS__)))
245#define UNLOCK_FUNCTION(...) __attribute__((unlock_function(__VA_ARGS__)))
246#define REQUIRES(...) __attribute__((requires_capability(__VA_ARGS__)))
247#define SHARED_REQUIRES(...) __attribute__((requires_shared_capability(__VA_ARGS__)))
248#define CAPABILITY(...) __attribute__((capability(__VA_ARGS__)))
249#define SHARED_CAPABILITY(...) __attribute__((shared_capability(__VA_ARGS__)))
250#define ASSERT_CAPABILITY(...) __attribute__((assert_capability(__VA_ARGS__)))
251#define ASSERT_SHARED_CAPABILITY(...) __attribute__((assert_shared_capability(__VA_ARGS__)))
252#define RETURN_CAPABILITY(...) __attribute__((lock_returned(__VA_ARGS__)))
253#define TRY_ACQUIRE(...) __attribute__((try_acquire_capability(__VA_ARGS__)))
254#define TRY_ACQUIRE_SHARED(...) __attribute__((try_acquire_shared_capability(__VA_ARGS__)))
255#define ACQUIRE(...) __attribute__((acquire_capability(__VA_ARGS__)))
256#define ACQUIRE_SHARED(...) __attribute__((acquire_shared_capability(__VA_ARGS__)))
257#define RELEASE(...) __attribute__((release_capability(__VA_ARGS__)))
258#define RELEASE_SHARED(...) __attribute__((release_shared_capability(__VA_ARGS__)))
259#define SCOPED_CAPABILITY __attribute__((scoped_lockable))
Elliott Hughesf8349362012-06-18 15:00:06 -0700260
Mathieu Chartier90443472015-07-16 20:32:27 -0700261#define LOCKABLE CAPABILITY("mutex")
262#define SHARED_LOCKABLE SHARED_CAPABILITY("mutex")
263
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700264#endif // ART_RUNTIME_BASE_MACROS_H_