Home | History | Annotate | Download | only in base
      1 // Copyright 2014 the V8 project authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 #ifndef V8_BASE_MACROS_H_
      6 #define V8_BASE_MACROS_H_
      7 
      8 #include "include/v8stdint.h"
      9 
     10 
     11 // The expression OFFSET_OF(type, field) computes the byte-offset
     12 // of the specified field relative to the containing type. This
     13 // corresponds to 'offsetof' (in stddef.h), except that it doesn't
     14 // use 0 or NULL, which causes a problem with the compiler warnings
     15 // we have enabled (which is also why 'offsetof' doesn't seem to work).
     16 // Here we simply use the non-zero value 4, which seems to work.
     17 #define OFFSET_OF(type, field)                                          \
     18   (reinterpret_cast<intptr_t>(&(reinterpret_cast<type*>(4)->field)) - 4)
     19 
     20 
     21 // The expression ARRAY_SIZE(a) is a compile-time constant of type
     22 // size_t which represents the number of elements of the given
     23 // array. You should only use ARRAY_SIZE on statically allocated
     24 // arrays.
     25 #define ARRAY_SIZE(a)                                   \
     26   ((sizeof(a) / sizeof(*(a))) /                         \
     27   static_cast<size_t>(!(sizeof(a) % sizeof(*(a)))))
     28 
     29 
     30 // A macro to disallow the evil copy constructor and operator= functions
     31 // This should be used in the private: declarations for a class
     32 #define DISALLOW_COPY_AND_ASSIGN(TypeName)  \
     33   TypeName(const TypeName&) V8_DELETE;      \
     34   void operator=(const TypeName&) V8_DELETE
     35 
     36 
     37 // A macro to disallow all the implicit constructors, namely the
     38 // default constructor, copy constructor and operator= functions.
     39 //
     40 // This should be used in the private: declarations for a class
     41 // that wants to prevent anyone from instantiating it. This is
     42 // especially useful for classes containing only static methods.
     43 #define DISALLOW_IMPLICIT_CONSTRUCTORS(TypeName)  \
     44   TypeName() V8_DELETE;                           \
     45   DISALLOW_COPY_AND_ASSIGN(TypeName)
     46 
     47 
     48 // Newly written code should use V8_INLINE and V8_NOINLINE directly.
     49 #define INLINE(declarator)    V8_INLINE declarator
     50 #define NO_INLINE(declarator) V8_NOINLINE declarator
     51 
     52 
     53 // Newly written code should use V8_WARN_UNUSED_RESULT.
     54 #define MUST_USE_RESULT V8_WARN_UNUSED_RESULT
     55 
     56 
     57 // Define V8_USE_ADDRESS_SANITIZER macros.
     58 #if defined(__has_feature)
     59 #if __has_feature(address_sanitizer)
     60 #define V8_USE_ADDRESS_SANITIZER 1
     61 #endif
     62 #endif
     63 
     64 // Define DISABLE_ASAN macros.
     65 #ifdef V8_USE_ADDRESS_SANITIZER
     66 #define DISABLE_ASAN __attribute__((no_sanitize_address))
     67 #else
     68 #define DISABLE_ASAN
     69 #endif
     70 
     71 
     72 #if V8_CC_GNU
     73 #define V8_IMMEDIATE_CRASH() __builtin_trap()
     74 #else
     75 #define V8_IMMEDIATE_CRASH() ((void(*)())0)()
     76 #endif
     77 
     78 
     79 // Use C++11 static_assert if possible, which gives error
     80 // messages that are easier to understand on first sight.
     81 #if V8_HAS_CXX11_STATIC_ASSERT
     82 #define STATIC_ASSERT(test) static_assert(test, #test)
     83 #else
     84 // This is inspired by the static assertion facility in boost.  This
     85 // is pretty magical.  If it causes you trouble on a platform you may
     86 // find a fix in the boost code.
     87 template <bool> class StaticAssertion;
     88 template <> class StaticAssertion<true> { };
     89 // This macro joins two tokens.  If one of the tokens is a macro the
     90 // helper call causes it to be resolved before joining.
     91 #define SEMI_STATIC_JOIN(a, b) SEMI_STATIC_JOIN_HELPER(a, b)
     92 #define SEMI_STATIC_JOIN_HELPER(a, b) a##b
     93 // Causes an error during compilation of the condition is not
     94 // statically known to be true.  It is formulated as a typedef so that
     95 // it can be used wherever a typedef can be used.  Beware that this
     96 // actually causes each use to introduce a new defined type with a
     97 // name depending on the source line.
     98 template <int> class StaticAssertionHelper { };
     99 #define STATIC_ASSERT(test)                                                    \
    100   typedef                                                                     \
    101     StaticAssertionHelper<sizeof(StaticAssertion<static_cast<bool>((test))>)> \
    102     SEMI_STATIC_JOIN(__StaticAssertTypedef__, __LINE__) V8_UNUSED
    103 
    104 #endif
    105 
    106 
    107 // The USE(x) template is used to silence C++ compiler warnings
    108 // issued for (yet) unused variables (typically parameters).
    109 template <typename T>
    110 inline void USE(T) { }
    111 
    112 
    113 #define IS_POWER_OF_TWO(x) ((x) != 0 && (((x) & ((x) - 1)) == 0))
    114 
    115 // The following macro works on both 32 and 64-bit platforms.
    116 // Usage: instead of writing 0x1234567890123456
    117 //      write V8_2PART_UINT64_C(0x12345678,90123456);
    118 #define V8_2PART_UINT64_C(a, b) (((static_cast<uint64_t>(a) << 32) + 0x##b##u))
    119 
    120 #endif   // V8_BASE_MACROS_H_
    121