• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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  */
16 
17 #ifndef ART_RUNTIME_BASE_MACROS_H_
18 #define ART_RUNTIME_BASE_MACROS_H_
19 
20 #include <stddef.h>  // for size_t
21 #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
32 
33 #define GCC_VERSION (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__)
34 
35 // C++11 final and override keywords that were introduced in GCC version 4.7.
36 #if defined(__clang__) || GCC_VERSION >= 40700
37 #define OVERRIDE override
38 #define FINAL final
39 #else
40 #define OVERRIDE
41 #define FINAL
42 #endif
43 
44 // Declare a friend relationship in a class with a test. Used rather that FRIEND_TEST to avoid
45 // globally importing gtest/gtest.h into the main ART header files.
46 #define ART_FRIEND_TEST(test_set_name, individual_test)\
47 friend class test_set_name##_##individual_test##_Test
48 
49 // Declare a friend relationship in a class with a typed test.
50 #define ART_FRIEND_TYPED_TEST(test_set_name, individual_test)\
51 template<typename T> ART_FRIEND_TEST(test_set_name, individual_test)
52 
53 // DISALLOW_COPY_AND_ASSIGN disallows the copy and operator= functions. It goes in the private:
54 // declarations in a class.
55 #if !defined(DISALLOW_COPY_AND_ASSIGN)
56 #define DISALLOW_COPY_AND_ASSIGN(TypeName) \
57   TypeName(const TypeName&) = delete;  \
58   void operator=(const TypeName&) = delete
59 #endif
60 
61 // A macro to disallow all the implicit constructors, namely the default constructor, copy
62 // constructor and operator= functions.
63 //
64 // This should be used in the private: declarations for a class that wants to prevent anyone from
65 // instantiating it. This is especially useful for classes containing only static methods.
66 #define DISALLOW_IMPLICIT_CONSTRUCTORS(TypeName) \
67   TypeName() = delete;  \
68   DISALLOW_COPY_AND_ASSIGN(TypeName)
69 
70 // A macro to disallow new and delete operators for a class. It goes in the private: declarations.
71 #define DISALLOW_ALLOCATION() \
72   public: \
73     NO_RETURN ALWAYS_INLINE void operator delete(void*, size_t) { UNREACHABLE(); } \
74   private: \
75     void* operator new(size_t) = delete
76 
77 // The arraysize(arr) macro returns the # of elements in an array arr.
78 // The expression is a compile-time constant, and therefore can be
79 // used in defining new arrays, for example.  If you use arraysize on
80 // a pointer by mistake, you will get a compile-time error.
81 //
82 // One caveat is that arraysize() doesn't accept any array of an
83 // anonymous type or a type defined inside a function.  In these rare
84 // cases, you have to use the unsafe ARRAYSIZE_UNSAFE() macro below.  This is
85 // due to a limitation in C++'s template system.  The limitation might
86 // eventually be removed, but it hasn't happened yet.
87 
88 // This template function declaration is used in defining arraysize.
89 // Note that the function doesn't need an implementation, as we only
90 // use its type.
91 template <typename T, size_t N>
92 char (&ArraySizeHelper(T (&array)[N]))[N];
93 
94 #define arraysize(array) (sizeof(ArraySizeHelper(array)))
95 
96 // ARRAYSIZE_UNSAFE performs essentially the same calculation as arraysize,
97 // but can be used on anonymous types or types defined inside
98 // functions.  It's less safe than arraysize as it accepts some
99 // (although not all) pointers.  Therefore, you should use arraysize
100 // whenever possible.
101 //
102 // The expression ARRAYSIZE_UNSAFE(a) is a compile-time constant of type
103 // size_t.
104 //
105 // ARRAYSIZE_UNSAFE catches a few type errors.  If you see a compiler error
106 //
107 //   "warning: division by zero in ..."
108 //
109 // when using ARRAYSIZE_UNSAFE, you are (wrongfully) giving it a pointer.
110 // You should only use ARRAYSIZE_UNSAFE on statically allocated arrays.
111 //
112 // The following comments are on the implementation details, and can
113 // be ignored by the users.
114 //
115 // ARRAYSIZE_UNSAFE(arr) works by inspecting sizeof(arr) (the # of bytes in
116 // the array) and sizeof(*(arr)) (the # of bytes in one array
117 // element).  If the former is divisible by the latter, perhaps arr is
118 // indeed an array, in which case the division result is the # of
119 // elements in the array.  Otherwise, arr cannot possibly be an array,
120 // and we generate a compiler error to prevent the code from
121 // compiling.
122 //
123 // Since the size of bool is implementation-defined, we need to cast
124 // !(sizeof(a) & sizeof(*(a))) to size_t in order to ensure the final
125 // result has type size_t.
126 //
127 // This macro is not perfect as it wrongfully accepts certain
128 // pointers, namely where the pointer size is divisible by the pointee
129 // size.  Since all our code has to go through a 32-bit compiler,
130 // where a pointer is 4 bytes, this means all pointers to a type whose
131 // size is 3 or greater than 4 will be (righteously) rejected.
132 #define ARRAYSIZE_UNSAFE(a) \
133   ((sizeof(a) / sizeof(*(a))) / static_cast<size_t>(!(sizeof(a) % sizeof(*(a)))))
134 
135 #define SIZEOF_MEMBER(t, f) sizeof((reinterpret_cast<t*>(4096))->f)
136 
137 #define OFFSETOF_MEMBER(t, f) \
138   (reinterpret_cast<const char*>(&reinterpret_cast<t*>(16)->f) - reinterpret_cast<const char*>(16)) // NOLINT
139 
140 #define OFFSETOF_VOLATILE_MEMBER(t, f) \
141   (reinterpret_cast<volatile char*>(&reinterpret_cast<t*>(16)->f) - reinterpret_cast<volatile char*>(16)) // NOLINT
142 
143 #define PACKED(x) __attribute__ ((__aligned__(x), __packed__))
144 
145 #define LIKELY(x)       __builtin_expect((x), true)
146 #define UNLIKELY(x)     __builtin_expect((x), false)
147 
148 // Stringify the argument.
149 #define QUOTE(x) #x
150 #define STRINGIFY(x) QUOTE(x)
151 
152 #ifndef NDEBUG
153 #define ALWAYS_INLINE
154 #else
155 #define ALWAYS_INLINE  __attribute__ ((always_inline))
156 #endif
157 
158 #ifdef __clang__
159 /* clang doesn't like attributes on lambda functions */
160 #define ALWAYS_INLINE_LAMBDA
161 #else
162 #define ALWAYS_INLINE_LAMBDA ALWAYS_INLINE
163 #endif
164 
165 #define NO_INLINE __attribute__ ((noinline))
166 
167 #if defined (__APPLE__)
168 #define HOT_ATTR
169 #define COLD_ATTR
170 #else
171 #define HOT_ATTR __attribute__ ((hot))
172 #define COLD_ATTR __attribute__ ((cold))
173 #endif
174 
175 #define PURE __attribute__ ((__pure__))
176 #define WARN_UNUSED __attribute__((warn_unused_result))
177 
178 // A deprecated function to call to create a false use of the parameter, for example:
179 //   int foo(int x) { UNUSED(x); return 10; }
180 // to avoid compiler warnings. Going forward we prefer ATTRIBUTE_UNUSED.
UNUSED(const T &...)181 template<typename... T> void UNUSED(const T&...) {}
182 
183 // An attribute to place on a parameter to a function, for example:
184 //   int foo(int x ATTRIBUTE_UNUSED) { return 10; }
185 // to avoid compiler warnings.
186 #define ATTRIBUTE_UNUSED __attribute__((__unused__))
187 
188 // Define that a position within code is unreachable, for example:
189 //   int foo () { LOG(FATAL) << "Don't call me"; UNREACHABLE(); }
190 // without the UNREACHABLE a return statement would be necessary.
191 #define UNREACHABLE  __builtin_unreachable
192 
193 // Add the C++11 noreturn attribute.
194 #define NO_RETURN [[ noreturn ]]  // NOLINT[whitespace/braces] [5]
195 
196 // The FALLTHROUGH_INTENDED macro can be used to annotate implicit fall-through
197 // between switch labels:
198 //  switch (x) {
199 //    case 40:
200 //    case 41:
201 //      if (truth_is_out_there) {
202 //        ++x;
203 //        FALLTHROUGH_INTENDED;  // Use instead of/along with annotations in
204 //                               // comments.
205 //      } else {
206 //        return x;
207 //      }
208 //    case 42:
209 //      ...
210 //
211 //  As shown in the example above, the FALLTHROUGH_INTENDED macro should be
212 //  followed by a semicolon. It is designed to mimic control-flow statements
213 //  like 'break;', so it can be placed in most places where 'break;' can, but
214 //  only if there are no statements on the execution path between it and the
215 //  next switch label.
216 //
217 //  When compiled with clang in C++11 mode, the FALLTHROUGH_INTENDED macro is
218 //  expanded to [[clang::fallthrough]] attribute, which is analysed when
219 //  performing switch labels fall-through diagnostic ('-Wimplicit-fallthrough').
220 //  See clang documentation on language extensions for details:
221 //  http://clang.llvm.org/docs/LanguageExtensions.html#clang__fallthrough
222 //
223 //  When used with unsupported compilers, the FALLTHROUGH_INTENDED macro has no
224 //  effect on diagnostics.
225 //
226 //  In either case this macro has no effect on runtime behavior and performance
227 //  of code.
228 #if defined(__clang__) && __cplusplus >= 201103L && defined(__has_warning)
229 #if __has_feature(cxx_attributes) && __has_warning("-Wimplicit-fallthrough")
230 #define FALLTHROUGH_INTENDED [[clang::fallthrough]]  // NOLINT
231 #endif
232 #endif
233 
234 #ifndef FALLTHROUGH_INTENDED
235 #define FALLTHROUGH_INTENDED do { } while (0)
236 #endif
237 
238 // Annotalysis thread-safety analysis support.
239 #if defined(__SUPPORT_TS_ANNOTATION__) || defined(__clang__)
240 #define THREAD_ANNOTATION_ATTRIBUTE__(x)   __attribute__((x))
241 #else
242 #define THREAD_ANNOTATION_ATTRIBUTE__(x)   // no-op
243 #endif
244 
245 #define ACQUIRED_AFTER(...) THREAD_ANNOTATION_ATTRIBUTE__(acquired_after(__VA_ARGS__))
246 #define ACQUIRED_BEFORE(...) THREAD_ANNOTATION_ATTRIBUTE__(acquired_before(__VA_ARGS__))
247 #define EXCLUSIVE_LOCKS_REQUIRED(...) THREAD_ANNOTATION_ATTRIBUTE__(exclusive_locks_required(__VA_ARGS__))
248 #define GUARDED_BY(x) THREAD_ANNOTATION_ATTRIBUTE__(guarded_by(x))
249 #define GUARDED_VAR THREAD_ANNOTATION_ATTRIBUTE__(guarded)
250 #define LOCKABLE THREAD_ANNOTATION_ATTRIBUTE__(lockable)
251 #define LOCK_RETURNED(x) THREAD_ANNOTATION_ATTRIBUTE__(lock_returned(x))
252 #define LOCKS_EXCLUDED(...) THREAD_ANNOTATION_ATTRIBUTE__(locks_excluded(__VA_ARGS__))
253 #define NO_THREAD_SAFETY_ANALYSIS THREAD_ANNOTATION_ATTRIBUTE__(no_thread_safety_analysis)
254 #define PT_GUARDED_BY(x)
255 // THREAD_ANNOTATION_ATTRIBUTE__(point_to_guarded_by(x))
256 #define PT_GUARDED_VAR THREAD_ANNOTATION_ATTRIBUTE__(point_to_guarded)
257 #define SCOPED_LOCKABLE THREAD_ANNOTATION_ATTRIBUTE__(scoped_lockable)
258 #define SHARED_LOCKS_REQUIRED(...) THREAD_ANNOTATION_ATTRIBUTE__(shared_locks_required(__VA_ARGS__))
259 
260 #if defined(__clang__)
261 #define EXCLUSIVE_LOCK_FUNCTION(...) THREAD_ANNOTATION_ATTRIBUTE__(exclusive_lock_function(__VA_ARGS__))
262 #define EXCLUSIVE_TRYLOCK_FUNCTION(...) THREAD_ANNOTATION_ATTRIBUTE__(exclusive_trylock_function(__VA_ARGS__))
263 #define SHARED_LOCK_FUNCTION(...) THREAD_ANNOTATION_ATTRIBUTE__(shared_lock_function(__VA_ARGS__))
264 #define SHARED_TRYLOCK_FUNCTION(...) THREAD_ANNOTATION_ATTRIBUTE__(shared_trylock_function(__VA_ARGS__))
265 #define UNLOCK_FUNCTION(...) THREAD_ANNOTATION_ATTRIBUTE__(unlock_function(__VA_ARGS__))
266 #else
267 #define EXCLUSIVE_LOCK_FUNCTION(...) THREAD_ANNOTATION_ATTRIBUTE__(exclusive_lock(__VA_ARGS__))
268 #define EXCLUSIVE_TRYLOCK_FUNCTION(...) THREAD_ANNOTATION_ATTRIBUTE__(exclusive_trylock(__VA_ARGS__))
269 #define SHARED_LOCK_FUNCTION(...) THREAD_ANNOTATION_ATTRIBUTE__(shared_lock(__VA_ARGS__))
270 #define SHARED_TRYLOCK_FUNCTION(...) THREAD_ANNOTATION_ATTRIBUTE__(shared_trylock(__VA_ARGS__))
271 #define UNLOCK_FUNCTION(...) THREAD_ANNOTATION_ATTRIBUTE__(unlock(__VA_ARGS__))
272 #endif
273 
274 #endif  // ART_RUNTIME_BASE_MACROS_H_
275