Home | History | Annotate | Download | only in base
      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 // NOTE: Providing placement new (and matching delete) for constructing container elements.
     72 #define DISALLOW_ALLOCATION() \
     73   public: \
     74     NO_RETURN ALWAYS_INLINE void operator delete(void*, size_t) { UNREACHABLE(); } \
     75     ALWAYS_INLINE void* operator new(size_t, void* ptr) noexcept { return ptr; } \
     76     ALWAYS_INLINE void operator delete(void*, void*) noexcept { } \
     77   private: \
     78     void* operator new(size_t) = delete
     79 
     80 // The arraysize(arr) macro returns the # of elements in an array arr.
     81 // The expression is a compile-time constant, and therefore can be
     82 // used in defining new arrays, for example.  If you use arraysize on
     83 // a pointer by mistake, you will get a compile-time error.
     84 //
     85 // One caveat is that arraysize() doesn't accept any array of an
     86 // anonymous type or a type defined inside a function.  In these rare
     87 // cases, you have to use the unsafe ARRAYSIZE_UNSAFE() macro below.  This is
     88 // due to a limitation in C++'s template system.  The limitation might
     89 // eventually be removed, but it hasn't happened yet.
     90 
     91 // This template function declaration is used in defining arraysize.
     92 // Note that the function doesn't need an implementation, as we only
     93 // use its type.
     94 template <typename T, size_t N>
     95 char (&ArraySizeHelper(T (&array)[N]))[N];
     96 
     97 #define arraysize(array) (sizeof(ArraySizeHelper(array)))
     98 
     99 // ARRAYSIZE_UNSAFE performs essentially the same calculation as arraysize,
    100 // but can be used on anonymous types or types defined inside
    101 // functions.  It's less safe than arraysize as it accepts some
    102 // (although not all) pointers.  Therefore, you should use arraysize
    103 // whenever possible.
    104 //
    105 // The expression ARRAYSIZE_UNSAFE(a) is a compile-time constant of type
    106 // size_t.
    107 //
    108 // ARRAYSIZE_UNSAFE catches a few type errors.  If you see a compiler error
    109 //
    110 //   "warning: division by zero in ..."
    111 //
    112 // when using ARRAYSIZE_UNSAFE, you are (wrongfully) giving it a pointer.
    113 // You should only use ARRAYSIZE_UNSAFE on statically allocated arrays.
    114 //
    115 // The following comments are on the implementation details, and can
    116 // be ignored by the users.
    117 //
    118 // ARRAYSIZE_UNSAFE(arr) works by inspecting sizeof(arr) (the # of bytes in
    119 // the array) and sizeof(*(arr)) (the # of bytes in one array
    120 // element).  If the former is divisible by the latter, perhaps arr is
    121 // indeed an array, in which case the division result is the # of
    122 // elements in the array.  Otherwise, arr cannot possibly be an array,
    123 // and we generate a compiler error to prevent the code from
    124 // compiling.
    125 //
    126 // Since the size of bool is implementation-defined, we need to cast
    127 // !(sizeof(a) & sizeof(*(a))) to size_t in order to ensure the final
    128 // result has type size_t.
    129 //
    130 // This macro is not perfect as it wrongfully accepts certain
    131 // pointers, namely where the pointer size is divisible by the pointee
    132 // size.  Since all our code has to go through a 32-bit compiler,
    133 // where a pointer is 4 bytes, this means all pointers to a type whose
    134 // size is 3 or greater than 4 will be (righteously) rejected.
    135 #define ARRAYSIZE_UNSAFE(a) \
    136   ((sizeof(a) / sizeof(*(a))) / static_cast<size_t>(!(sizeof(a) % sizeof(*(a)))))
    137 
    138 #define SIZEOF_MEMBER(t, f) sizeof((reinterpret_cast<t*>(4096))->f)
    139 
    140 #define OFFSETOF_MEMBER(t, f) \
    141   (reinterpret_cast<uintptr_t>(&reinterpret_cast<t*>(16)->f) - static_cast<uintptr_t>(16u)) // NOLINT
    142 
    143 #define OFFSETOF_MEMBERPTR(t, f) \
    144   (reinterpret_cast<uintptr_t>(&(reinterpret_cast<t*>(16)->*f)) - static_cast<uintptr_t>(16)) // NOLINT
    145 
    146 #define PACKED(x) __attribute__ ((__aligned__(x), __packed__))
    147 
    148 #define LIKELY(x)       __builtin_expect((x), true)
    149 #define UNLIKELY(x)     __builtin_expect((x), false)
    150 
    151 // Stringify the argument.
    152 #define QUOTE(x) #x
    153 #define STRINGIFY(x) QUOTE(x)
    154 
    155 #ifndef NDEBUG
    156 #define ALWAYS_INLINE
    157 #else
    158 #define ALWAYS_INLINE  __attribute__ ((always_inline))
    159 #endif
    160 
    161 #ifdef __clang__
    162 /* clang doesn't like attributes on lambda functions */
    163 #define ALWAYS_INLINE_LAMBDA
    164 #else
    165 #define ALWAYS_INLINE_LAMBDA ALWAYS_INLINE
    166 #endif
    167 
    168 #define NO_INLINE __attribute__ ((noinline))
    169 
    170 #if defined (__APPLE__)
    171 #define HOT_ATTR
    172 #define COLD_ATTR
    173 #else
    174 #define HOT_ATTR __attribute__ ((hot))
    175 #define COLD_ATTR __attribute__ ((cold))
    176 #endif
    177 
    178 #define PURE __attribute__ ((__pure__))
    179 #define WARN_UNUSED __attribute__((warn_unused_result))
    180 
    181 // A deprecated function to call to create a false use of the parameter, for example:
    182 //   int foo(int x) { UNUSED(x); return 10; }
    183 // to avoid compiler warnings. Going forward we prefer ATTRIBUTE_UNUSED.
    184 template<typename... T> void UNUSED(const T&...) {}
    185 
    186 // An attribute to place on a parameter to a function, for example:
    187 //   int foo(int x ATTRIBUTE_UNUSED) { return 10; }
    188 // to avoid compiler warnings.
    189 #define ATTRIBUTE_UNUSED __attribute__((__unused__))
    190 
    191 // Define that a position within code is unreachable, for example:
    192 //   int foo () { LOG(FATAL) << "Don't call me"; UNREACHABLE(); }
    193 // without the UNREACHABLE a return statement would be necessary.
    194 #define UNREACHABLE  __builtin_unreachable
    195 
    196 // Add the C++11 noreturn attribute.
    197 #define NO_RETURN [[ noreturn ]]  // NOLINT[whitespace/braces] [5]
    198 
    199 // The FALLTHROUGH_INTENDED macro can be used to annotate implicit fall-through
    200 // between switch labels:
    201 //  switch (x) {
    202 //    case 40:
    203 //    case 41:
    204 //      if (truth_is_out_there) {
    205 //        ++x;
    206 //        FALLTHROUGH_INTENDED;  // Use instead of/along with annotations in
    207 //                               // comments.
    208 //      } else {
    209 //        return x;
    210 //      }
    211 //    case 42:
    212 //      ...
    213 //
    214 //  As shown in the example above, the FALLTHROUGH_INTENDED macro should be
    215 //  followed by a semicolon. It is designed to mimic control-flow statements
    216 //  like 'break;', so it can be placed in most places where 'break;' can, but
    217 //  only if there are no statements on the execution path between it and the
    218 //  next switch label.
    219 //
    220 //  When compiled with clang in C++11 mode, the FALLTHROUGH_INTENDED macro is
    221 //  expanded to [[clang::fallthrough]] attribute, which is analysed when
    222 //  performing switch labels fall-through diagnostic ('-Wimplicit-fallthrough').
    223 //  See clang documentation on language extensions for details:
    224 //  http://clang.llvm.org/docs/LanguageExtensions.html#clang__fallthrough
    225 //
    226 //  When used with unsupported compilers, the FALLTHROUGH_INTENDED macro has no
    227 //  effect on diagnostics.
    228 //
    229 //  In either case this macro has no effect on runtime behavior and performance
    230 //  of code.
    231 #if defined(__clang__) && __cplusplus >= 201103L && defined(__has_warning)
    232 #if __has_feature(cxx_attributes) && __has_warning("-Wimplicit-fallthrough")
    233 #define FALLTHROUGH_INTENDED [[clang::fallthrough]]  // NOLINT
    234 #endif
    235 #endif
    236 
    237 #ifndef FALLTHROUGH_INTENDED
    238 #define FALLTHROUGH_INTENDED do { } while (0)
    239 #endif
    240 
    241 // Annotalysis thread-safety analysis support.
    242 #if defined(__SUPPORT_TS_ANNOTATION__) || defined(__clang__)
    243 #define THREAD_ANNOTATION_ATTRIBUTE__(x)   __attribute__((x))
    244 #else
    245 #define THREAD_ANNOTATION_ATTRIBUTE__(x)   // no-op
    246 #endif
    247 
    248 #define ACQUIRED_AFTER(...) THREAD_ANNOTATION_ATTRIBUTE__(acquired_after(__VA_ARGS__))
    249 #define ACQUIRED_BEFORE(...) THREAD_ANNOTATION_ATTRIBUTE__(acquired_before(__VA_ARGS__))
    250 #define GUARDED_BY(x) THREAD_ANNOTATION_ATTRIBUTE__(guarded_by(x))
    251 #define GUARDED_VAR THREAD_ANNOTATION_ATTRIBUTE__(guarded)
    252 #define LOCK_RETURNED(x) THREAD_ANNOTATION_ATTRIBUTE__(lock_returned(x))
    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 
    259 #if defined(__clang__)
    260 #define EXCLUSIVE_LOCK_FUNCTION(...) THREAD_ANNOTATION_ATTRIBUTE__(exclusive_lock_function(__VA_ARGS__))
    261 #define EXCLUSIVE_TRYLOCK_FUNCTION(...) THREAD_ANNOTATION_ATTRIBUTE__(exclusive_trylock_function(__VA_ARGS__))
    262 #define SHARED_LOCK_FUNCTION(...) THREAD_ANNOTATION_ATTRIBUTE__(shared_lock_function(__VA_ARGS__))
    263 #define SHARED_TRYLOCK_FUNCTION(...) THREAD_ANNOTATION_ATTRIBUTE__(shared_trylock_function(__VA_ARGS__))
    264 #define UNLOCK_FUNCTION(...) THREAD_ANNOTATION_ATTRIBUTE__(unlock_function(__VA_ARGS__))
    265 #define REQUIRES(...) THREAD_ANNOTATION_ATTRIBUTE__(requires_capability(__VA_ARGS__))
    266 #define SHARED_REQUIRES(...) THREAD_ANNOTATION_ATTRIBUTE__(requires_shared_capability(__VA_ARGS__))
    267 #define CAPABILITY(...) THREAD_ANNOTATION_ATTRIBUTE__(capability(__VA_ARGS__))
    268 #define SHARED_CAPABILITY(...) THREAD_ANNOTATION_ATTRIBUTE__(shared_capability(__VA_ARGS__))
    269 #define ASSERT_CAPABILITY(...) THREAD_ANNOTATION_ATTRIBUTE__(assert_capability(__VA_ARGS__))
    270 #define ASSERT_SHARED_CAPABILITY(...) THREAD_ANNOTATION_ATTRIBUTE__(assert_shared_capability(__VA_ARGS__))
    271 #define RETURN_CAPABILITY(...) THREAD_ANNOTATION_ATTRIBUTE__(lock_returned(__VA_ARGS__))
    272 #define TRY_ACQUIRE(...) THREAD_ANNOTATION_ATTRIBUTE__(try_acquire_capability(__VA_ARGS__))
    273 #define TRY_ACQUIRE_SHARED(...) THREAD_ANNOTATION_ATTRIBUTE__(try_acquire_shared_capability(__VA_ARGS__))
    274 #define ACQUIRE(...) THREAD_ANNOTATION_ATTRIBUTE__(acquire_capability(__VA_ARGS__))
    275 #define ACQUIRE_SHARED(...) THREAD_ANNOTATION_ATTRIBUTE__(acquire_shared_capability(__VA_ARGS__))
    276 #define RELEASE(...) THREAD_ANNOTATION_ATTRIBUTE__(release_capability(__VA_ARGS__))
    277 #define RELEASE_SHARED(...) THREAD_ANNOTATION_ATTRIBUTE__(release_shared_capability(__VA_ARGS__))
    278 #define SCOPED_CAPABILITY THREAD_ANNOTATION_ATTRIBUTE__(scoped_lockable)
    279 #else
    280 #define EXCLUSIVE_LOCK_FUNCTION(...) THREAD_ANNOTATION_ATTRIBUTE__(exclusive_lock(__VA_ARGS__))
    281 #define EXCLUSIVE_TRYLOCK_FUNCTION(...) THREAD_ANNOTATION_ATTRIBUTE__(exclusive_trylock(__VA_ARGS__))
    282 #define SHARED_LOCK_FUNCTION(...) THREAD_ANNOTATION_ATTRIBUTE__(shared_lock(__VA_ARGS__))
    283 #define SHARED_TRYLOCK_FUNCTION(...) THREAD_ANNOTATION_ATTRIBUTE__(shared_trylock(__VA_ARGS__))
    284 #define UNLOCK_FUNCTION(...) THREAD_ANNOTATION_ATTRIBUTE__(unlock(__VA_ARGS__))
    285 #define REQUIRES(...)
    286 #define SHARED_REQUIRES(...)
    287 #define CAPABILITY(...)
    288 #define SHARED_CAPABILITY(...)
    289 #define ASSERT_CAPABILITY(...)
    290 #define ASSERT_SHARED_CAPABILITY(...)
    291 #define RETURN_CAPABILITY(...)
    292 #define TRY_ACQUIRE(...)
    293 #define TRY_ACQUIRE_SHARED(...)
    294 #define ACQUIRE(...)
    295 #define ACQUIRE_SHARED(...)
    296 #define RELEASE(...)
    297 #define RELEASE_SHARED(...)
    298 #define SCOPED_CAPABILITY
    299 #endif
    300 
    301 #define LOCKABLE CAPABILITY("mutex")
    302 #define SHARED_LOCKABLE SHARED_CAPABILITY("mutex")
    303 
    304 #endif  // ART_RUNTIME_BASE_MACROS_H_
    305