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 // 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 
     59 template <bool>
     60 struct CompileAssert {
     61 };
     62 
     63 #define COMPILE_ASSERT(expr, msg) \
     64   typedef CompileAssert<(bool(expr))> msg[bool(expr) ? 1 : -1] // NOLINT
     65 
     66 // DISALLOW_COPY_AND_ASSIGN disallows the copy and operator= functions.
     67 // It goes in the private: declarations in a class.
     68 #define DISALLOW_COPY_AND_ASSIGN(TypeName) \
     69   TypeName(const TypeName&);               \
     70   void operator=(const TypeName&)
     71 
     72 // A macro to disallow all the implicit constructors, namely the
     73 // default constructor, copy constructor and operator= functions.
     74 //
     75 // This should be used in the private: declarations for a class
     76 // that wants to prevent anyone from instantiating it. This is
     77 // especially useful for classes containing only static methods.
     78 #define DISALLOW_IMPLICIT_CONSTRUCTORS(TypeName) \
     79   TypeName();                                    \
     80   DISALLOW_COPY_AND_ASSIGN(TypeName)
     81 
     82 // The arraysize(arr) macro returns the # of elements in an array arr.
     83 // The expression is a compile-time constant, and therefore can be
     84 // used in defining new arrays, for example.  If you use arraysize on
     85 // a pointer by mistake, you will get a compile-time error.
     86 //
     87 // One caveat is that arraysize() doesn't accept any array of an
     88 // anonymous type or a type defined inside a function.  In these rare
     89 // cases, you have to use the unsafe ARRAYSIZE_UNSAFE() macro below.  This is
     90 // due to a limitation in C++'s template system.  The limitation might
     91 // eventually be removed, but it hasn't happened yet.
     92 
     93 // This template function declaration is used in defining arraysize.
     94 // Note that the function doesn't need an implementation, as we only
     95 // use its type.
     96 template <typename T, size_t N>
     97 char (&ArraySizeHelper(T (&array)[N]))[N];
     98 
     99 #define arraysize(array) (sizeof(ArraySizeHelper(array)))
    100 
    101 // ARRAYSIZE_UNSAFE performs essentially the same calculation as arraysize,
    102 // but can be used on anonymous types or types defined inside
    103 // functions.  It's less safe than arraysize as it accepts some
    104 // (although not all) pointers.  Therefore, you should use arraysize
    105 // whenever possible.
    106 //
    107 // The expression ARRAYSIZE_UNSAFE(a) is a compile-time constant of type
    108 // size_t.
    109 //
    110 // ARRAYSIZE_UNSAFE catches a few type errors.  If you see a compiler error
    111 //
    112 //   "warning: division by zero in ..."
    113 //
    114 // when using ARRAYSIZE_UNSAFE, you are (wrongfully) giving it a pointer.
    115 // You should only use ARRAYSIZE_UNSAFE on statically allocated arrays.
    116 //
    117 // The following comments are on the implementation details, and can
    118 // be ignored by the users.
    119 //
    120 // ARRAYSIZE_UNSAFE(arr) works by inspecting sizeof(arr) (the # of bytes in
    121 // the array) and sizeof(*(arr)) (the # of bytes in one array
    122 // element).  If the former is divisible by the latter, perhaps arr is
    123 // indeed an array, in which case the division result is the # of
    124 // elements in the array.  Otherwise, arr cannot possibly be an array,
    125 // and we generate a compiler error to prevent the code from
    126 // compiling.
    127 //
    128 // Since the size of bool is implementation-defined, we need to cast
    129 // !(sizeof(a) & sizeof(*(a))) to size_t in order to ensure the final
    130 // result has type size_t.
    131 //
    132 // This macro is not perfect as it wrongfully accepts certain
    133 // pointers, namely where the pointer size is divisible by the pointee
    134 // size.  Since all our code has to go through a 32-bit compiler,
    135 // where a pointer is 4 bytes, this means all pointers to a type whose
    136 // size is 3 or greater than 4 will be (righteously) rejected.
    137 #define ARRAYSIZE_UNSAFE(a) \
    138   ((sizeof(a) / sizeof(*(a))) / static_cast<size_t>(!(sizeof(a) % sizeof(*(a)))))
    139 
    140 #define SIZEOF_MEMBER(t, f) sizeof((reinterpret_cast<t*>(4096))->f)
    141 
    142 #define OFFSETOF_MEMBER(t, f) \
    143   (reinterpret_cast<const char*>(&reinterpret_cast<t*>(16)->f) - reinterpret_cast<const char*>(16)) // NOLINT
    144 
    145 #define OFFSETOF_VOLATILE_MEMBER(t, f) \
    146   (reinterpret_cast<volatile char*>(&reinterpret_cast<t*>(16)->f) - reinterpret_cast<volatile char*>(16)) // NOLINT
    147 
    148 #define PACKED(x) __attribute__ ((__aligned__(x), __packed__))
    149 
    150 #define LIKELY(x)       __builtin_expect((x), true)
    151 #define UNLIKELY(x)     __builtin_expect((x), false)
    152 
    153 // Stringify the argument.
    154 #define QUOTE(x) #x
    155 #define STRINGIFY(x) QUOTE(x)
    156 
    157 #ifndef NDEBUG
    158 #define ALWAYS_INLINE
    159 #else
    160 #define ALWAYS_INLINE  __attribute__ ((always_inline))
    161 #endif
    162 
    163 #ifdef __clang__
    164 /* clang doesn't like attributes on lambda functions */
    165 #define ALWAYS_INLINE_LAMBDA
    166 #else
    167 #define ALWAYS_INLINE_LAMBDA ALWAYS_INLINE
    168 #endif
    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 template<typename T> void UNUSED(const T&) {}
    182 
    183 // Annotalysis thread-safety analysis support.
    184 #if defined(__SUPPORT_TS_ANNOTATION__) || defined(__clang__)
    185 #define THREAD_ANNOTATION_ATTRIBUTE__(x)   __attribute__((x))
    186 #else
    187 #define THREAD_ANNOTATION_ATTRIBUTE__(x)   // no-op
    188 #endif
    189 
    190 #define ACQUIRED_AFTER(...) THREAD_ANNOTATION_ATTRIBUTE__(acquired_after(__VA_ARGS__))
    191 #define ACQUIRED_BEFORE(...) THREAD_ANNOTATION_ATTRIBUTE__(acquired_before(__VA_ARGS__))
    192 #define EXCLUSIVE_LOCKS_REQUIRED(...) THREAD_ANNOTATION_ATTRIBUTE__(exclusive_locks_required(__VA_ARGS__))
    193 #define GUARDED_BY(x) THREAD_ANNOTATION_ATTRIBUTE__(guarded_by(x))
    194 #define GUARDED_VAR THREAD_ANNOTATION_ATTRIBUTE__(guarded)
    195 #define LOCKABLE THREAD_ANNOTATION_ATTRIBUTE__(lockable)
    196 #define LOCK_RETURNED(x) THREAD_ANNOTATION_ATTRIBUTE__(lock_returned(x))
    197 #define LOCKS_EXCLUDED(...) THREAD_ANNOTATION_ATTRIBUTE__(locks_excluded(__VA_ARGS__))
    198 #define NO_THREAD_SAFETY_ANALYSIS THREAD_ANNOTATION_ATTRIBUTE__(no_thread_safety_analysis)
    199 #define PT_GUARDED_BY(x)
    200 // THREAD_ANNOTATION_ATTRIBUTE__(point_to_guarded_by(x))
    201 #define PT_GUARDED_VAR THREAD_ANNOTATION_ATTRIBUTE__(point_to_guarded)
    202 #define SCOPED_LOCKABLE THREAD_ANNOTATION_ATTRIBUTE__(scoped_lockable)
    203 #define SHARED_LOCKS_REQUIRED(...) THREAD_ANNOTATION_ATTRIBUTE__(shared_locks_required(__VA_ARGS__))
    204 
    205 #if defined(__clang__)
    206 #define EXCLUSIVE_LOCK_FUNCTION(...) THREAD_ANNOTATION_ATTRIBUTE__(exclusive_lock_function(__VA_ARGS__))
    207 #define EXCLUSIVE_TRYLOCK_FUNCTION(...) THREAD_ANNOTATION_ATTRIBUTE__(exclusive_trylock_function(__VA_ARGS__))
    208 #define SHARED_LOCK_FUNCTION(...) THREAD_ANNOTATION_ATTRIBUTE__(shared_lock_function(__VA_ARGS__))
    209 #define SHARED_TRYLOCK_FUNCTION(...) THREAD_ANNOTATION_ATTRIBUTE__(shared_trylock_function(__VA_ARGS__))
    210 #define UNLOCK_FUNCTION(...) THREAD_ANNOTATION_ATTRIBUTE__(unlock_function(__VA_ARGS__))
    211 #else
    212 #define EXCLUSIVE_LOCK_FUNCTION(...) THREAD_ANNOTATION_ATTRIBUTE__(exclusive_lock(__VA_ARGS__))
    213 #define EXCLUSIVE_TRYLOCK_FUNCTION(...) THREAD_ANNOTATION_ATTRIBUTE__(exclusive_trylock(__VA_ARGS__))
    214 #define SHARED_LOCK_FUNCTION(...) THREAD_ANNOTATION_ATTRIBUTE__(shared_lock(__VA_ARGS__))
    215 #define SHARED_TRYLOCK_FUNCTION(...) THREAD_ANNOTATION_ATTRIBUTE__(shared_trylock(__VA_ARGS__))
    216 #define UNLOCK_FUNCTION(...) THREAD_ANNOTATION_ATTRIBUTE__(unlock(__VA_ARGS__))
    217 #endif
    218 
    219 #endif  // ART_RUNTIME_BASE_MACROS_H_
    220