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_LIBARTBASE_BASE_MACROS_H_
     18 #define ART_LIBARTBASE_BASE_MACROS_H_
     19 
     20 #include <stddef.h>  // for size_t
     21 #include <unistd.h>  // for TEMP_FAILURE_RETRY
     22 
     23 #include "android-base/macros.h"
     24 #include "android-base/thread_annotations.h"
     25 
     26 #define OVERRIDE override
     27 #define FINAL final
     28 
     29 // Declare a friend relationship in a class with a test. Used rather that FRIEND_TEST to avoid
     30 // globally importing gtest/gtest.h into the main ART header files.
     31 #define ART_FRIEND_TEST(test_set_name, individual_test)\
     32 friend class test_set_name##_##individual_test##_Test
     33 
     34 // Declare a friend relationship in a class with a typed test.
     35 #define ART_FRIEND_TYPED_TEST(test_set_name, individual_test)\
     36 template<typename T> ART_FRIEND_TEST(test_set_name, individual_test)
     37 
     38 // A macro to disallow new and delete operators for a class. It goes in the private: declarations.
     39 // NOTE: Providing placement new (and matching delete) for constructing container elements.
     40 #define DISALLOW_ALLOCATION() \
     41   public: \
     42     NO_RETURN ALWAYS_INLINE void operator delete(void*, size_t) { UNREACHABLE(); } \
     43     ALWAYS_INLINE void* operator new(size_t, void* ptr) noexcept { return ptr; } \
     44     ALWAYS_INLINE void operator delete(void*, void*) noexcept { } \
     45   private: \
     46     void* operator new(size_t) = delete  // NOLINT
     47 
     48 #define SIZEOF_MEMBER(t, f) sizeof((reinterpret_cast<t*>(4096))->f)  // NOLINT
     49 
     50 #define OFFSETOF_MEMBER(t, f) \
     51   (reinterpret_cast<uintptr_t>(&reinterpret_cast<t*>(16)->f) - static_cast<uintptr_t>(16u))  // NOLINT
     52 
     53 #define OFFSETOF_MEMBERPTR(t, f) \
     54   (reinterpret_cast<uintptr_t>(&(reinterpret_cast<t*>(16)->*f)) - static_cast<uintptr_t>(16))  // NOLINT
     55 
     56 #define PACKED(x) __attribute__ ((__aligned__(x), __packed__))
     57 
     58 // Stringify the argument.
     59 #define QUOTE(x) #x
     60 #define STRINGIFY(x) QUOTE(x)
     61 
     62 // Append tokens after evaluating.
     63 #define APPEND_TOKENS_AFTER_EVAL_2(a, b) a ## b
     64 #define APPEND_TOKENS_AFTER_EVAL(a, b) APPEND_TOKENS_AFTER_EVAL_2(a, b)
     65 
     66 #ifndef NDEBUG
     67 #define ALWAYS_INLINE
     68 #else
     69 #define ALWAYS_INLINE  __attribute__ ((always_inline))
     70 #endif
     71 
     72 // clang doesn't like attributes on lambda functions. It would be nice to say:
     73 //   #define ALWAYS_INLINE_LAMBDA ALWAYS_INLINE
     74 #define ALWAYS_INLINE_LAMBDA
     75 
     76 #define NO_INLINE __attribute__ ((noinline))
     77 
     78 #if defined (__APPLE__)
     79 #define HOT_ATTR
     80 #define COLD_ATTR
     81 #else
     82 #define HOT_ATTR __attribute__ ((hot))
     83 #define COLD_ATTR __attribute__ ((cold))
     84 #endif
     85 
     86 #define PURE __attribute__ ((__pure__))
     87 
     88 // Define that a position within code is unreachable, for example:
     89 //   int foo () { LOG(FATAL) << "Don't call me"; UNREACHABLE(); }
     90 // without the UNREACHABLE a return statement would be necessary.
     91 #define UNREACHABLE  __builtin_unreachable
     92 
     93 // Add the C++11 noreturn attribute.
     94 #define NO_RETURN [[ noreturn ]]  // NOLINT[whitespace/braces] [5]
     95 
     96 // Annotalysis thread-safety analysis support. Things that are not in base.
     97 
     98 #define LOCKABLE CAPABILITY("mutex")
     99 #define SHARED_LOCKABLE SHARED_CAPABILITY("mutex")
    100 
    101 #endif  // ART_LIBARTBASE_BASE_MACROS_H_
    102