blob: a70ee98fb6d9fffd6651f64e048887305eae8b07 [file] [log] [blame]
// Copyright 2014 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef V8_BASE_MACROS_H_
#define V8_BASE_MACROS_H_
#include <limits>
#include <type_traits>
#include "src/base/compiler-specific.h"
#include "src/base/logging.h"
#include "src/base/platform/wrappers.h"
// No-op macro which is used to work around MSVC's funky VA_ARGS support.
#define EXPAND(x) x
// This macro does nothing. That's all.
#define NOTHING(...)
// TODO(all) Replace all uses of this macro with C++'s offsetof. To do that, we
// have to make sure that only standard-layout types and simple field
// designators are used.
#define OFFSET_OF(type, field) \
(reinterpret_cast<intptr_t>(&(reinterpret_cast<type*>(16)->field)) - 16)
// The arraysize(arr) macro returns the # of elements in an array arr.
// The expression is a compile-time constant, and therefore can be
// used in defining new arrays, for example. If you use arraysize on
// a pointer by mistake, you will get a compile-time error.
#define arraysize(array) (sizeof(ArraySizeHelper(array)))
// This template function declaration is used in defining arraysize.
// Note that the function doesn't need an implementation, as we only
// use its type.
template <typename T, size_t N>
char (&ArraySizeHelper(T (&array)[N]))[N];
#if !V8_CC_MSVC
// That gcc wants both of these prototypes seems mysterious. VC, for
// its part, can't decide which to use (another mystery). Matching of
// template overloads: the final frontier.
template <typename T, size_t N>
char (&ArraySizeHelper(const T (&array)[N]))[N];
// bit_cast<Dest,Source> is a template function that implements the
// equivalent of "*reinterpret_cast<Dest*>(&source)". We need this in
// very low-level functions like the protobuf library and fast math
// support.
// float f = 3.14159265358979;
// int i = bit_cast<int32>(f);
// // i = 0x40490fdb
// The classical address-casting method is:
// // WRONG
// float f = 3.14159265358979; // WRONG
// int i = * reinterpret_cast<int*>(&f); // WRONG
// The address-casting method actually produces undefined behavior
// according to ISO C++ specification section 3.10 -15 -. Roughly, this
// section says: if an object in memory has one type, and a program
// accesses it with a different type, then the result is undefined
// behavior for most values of "different type".
// This is true for any cast syntax, either *(int*)&f or
// *reinterpret_cast<int*>(&f). And it is particularly true for
// conversions between integral lvalues and floating-point lvalues.
// The purpose of 3.10 -15- is to allow optimizing compilers to assume
// that expressions with different types refer to different memory. gcc
// 4.0.1 has an optimizer that takes advantage of this. So a
// non-conforming program quietly produces wildly incorrect output.
// The problem is not the use of reinterpret_cast. The problem is type
// punning: holding an object in memory of one type and reading its bits
// back using a different type.
// The C++ standard is more subtle and complex than this, but that
// is the basic idea.
// Anyways ...
// bit_cast<> calls memcpy() which is blessed by the standard,
// especially by the example in section 3.9 . Also, of course,
// bit_cast<> wraps up the nasty logic in one place.
// Fortunately memcpy() is very fast. In optimized mode, with a
// constant size, gcc 2.95.3, gcc 4.0.1, and msvc 7.1 produce inline
// code with the minimal amount of data movement. On a 32-bit system,
// memcpy(d,s,4) compiles to one load and one store, and memcpy(d,s,8)
// compiles to two loads and two stores.
// I tested this code with gcc 2.95.3, gcc 4.0.1, icc 8.1, and msvc 7.1.
// WARNING: if Dest or Source is a non-POD type, the result of the memcpy
// is likely to surprise you.
template <class Dest, class Source>
V8_INLINE Dest bit_cast(Source const& source) {
static_assert(sizeof(Dest) == sizeof(Source),
"source and dest must be same size");
Dest dest;
v8::base::Memcpy(&dest, &source, sizeof(dest));
return dest;
// Explicitly declare the assignment operator as deleted.
// Note: This macro is deprecated and will be removed soon. Please explicitly
// delete the assignment operator instead.
#define DISALLOW_ASSIGN(TypeName) TypeName& operator=(const TypeName&) = delete
// Explicitly declare the copy constructor and assignment operator as deleted.
// This also deletes the implicit move constructor and implicit move assignment
// operator, but still allows to manually define them.
// Note: This macro is deprecated and will be removed soon. Please explicitly
// delete the copy constructor and assignment operator instead.
TypeName(const TypeName&) = delete; \
// Explicitly declare all implicit constructors as deleted, namely the
// default constructor, copy constructor and operator= functions.
// This is especially useful for classes containing only static methods.
TypeName() = delete; \
// Disallow copying a type, but provide default construction, move construction
// and move assignment. Especially useful for move-only structs.
TypeName() = default; \
// Disallow copying a type, and only provide move construction and move
// assignment. Especially useful for move-only structs.
TypeName(TypeName&&) V8_NOEXCEPT = default; \
TypeName& operator=(TypeName&&) V8_NOEXCEPT = default; \
// A macro to disallow the dynamic allocation.
// This should be used in the private: declarations for a class
// Declaring operator new and delete as deleted is not spec compliant.
// Extract from 3.2.2 of C++11 spec:
// [...] A non-placement deallocation function for a class is
// odr-used by the definition of the destructor of that class, [...]
void* operator new(size_t) { v8::base::OS::Abort(); } \
void* operator new[](size_t) { v8::base::OS::Abort(); } \
void operator delete(void*, size_t) { v8::base::OS::Abort(); } \
void operator delete[](void*, size_t) { v8::base::OS::Abort(); }
#if defined(__has_feature)
#if __has_feature(address_sanitizer)
// Define DISABLE_ASAN macro.
#define DISABLE_ASAN __attribute__((no_sanitize_address))
// Define V8_USE_MEMORY_SANITIZER macro.
#if defined(__has_feature)
#if __has_feature(memory_sanitizer)
// DISABLE_CFI_PERF -- Disable Control Flow Integrity checks for Perf reasons.
// DISABLE_CFI_ICALL -- Disable Control Flow Integrity indirect call checks,
// useful because calls into JITed code can not be CFI verified.
#ifdef V8_OS_WIN
// On Windows, also needs __declspec(guard(nocf)) for CFG.
V8_CLANG_NO_SANITIZE("cfi-icall") \
#if V8_CC_GNU
#define V8_IMMEDIATE_CRASH() __builtin_trap()
#define V8_IMMEDIATE_CRASH() ((void(*)())0)()
// A convenience wrapper around static_assert without a string message argument.
// Once C++17 becomes the default, this macro can be removed in favor of the
// new static_assert(condition) overload.
#define STATIC_ASSERT(test) static_assert(test, #test)
namespace v8 {
namespace base {
// Note that some implementations of std::is_trivially_copyable mandate that at
// least one of the copy constructor, move constructor, copy assignment or move
// assignment is non-deleted, while others do not. Be aware that also
// base::is_trivially_copyable will differ for these cases.
template <typename T>
struct is_trivially_copyable {
#if V8_CC_MSVC
// Unfortunately, MSVC 2015 is broken in that std::is_trivially_copyable can
// be false even though it should be true according to the standard.
// (status at 2018-02-26, observed on the msvc waterfall bot).
// Interestingly, the lower-level primitives used below are working as
// intended, so we reimplement this according to the standard.
// See also
// 170883/msvc-type-traits-stdis-trivial-is-bugged.html.
static constexpr bool value =
// Copy constructor is trivial or deleted.
(std::is_trivially_copy_constructible<T>::value ||
!std::is_copy_constructible<T>::value) &&
// Copy assignment operator is trivial or deleted.
(std::is_trivially_copy_assignable<T>::value ||
!std::is_copy_assignable<T>::value) &&
// Move constructor is trivial or deleted.
(std::is_trivially_move_constructible<T>::value ||
!std::is_move_constructible<T>::value) &&
// Move assignment operator is trivial or deleted.
(std::is_trivially_move_assignable<T>::value ||
!std::is_move_assignable<T>::value) &&
// (Some implementations mandate that one of the above is non-deleted, but
// the standard does not, so let's skip this check.)
// Trivial non-deleted destructor.
static constexpr bool value = std::is_trivially_copyable<T>::value;
static_assert(::v8::base::is_trivially_copyable<T>::value, \
#T " should be trivially copyable")
static_assert(!::v8::base::is_trivially_copyable<T>::value, \
#T " should not be trivially copyable")
// The USE(x, ...) template is used to silence C++ compiler warnings
// issued for (yet) unused variables (typically parameters).
// The arguments are guaranteed to be evaluated from left to right.
struct Use {
template <typename T>
Use(T&&) {} // NOLINT(runtime/explicit)
#define USE(...) \
do { \
::v8::base::Use unused_tmp_array_for_use_macro[]{__VA_ARGS__}; \
(void)unused_tmp_array_for_use_macro; \
} while (false)
// Evaluate the instantiations of an expression with parameter packs.
// Since USE has left-to-right evaluation order of it's arguments,
// the parameter pack is iterated from left to right and side effects
// have defined behavior.
#define ITERATE_PACK(...) USE(0, ((__VA_ARGS__), 0)...)
} // namespace base
} // namespace v8
// implicit_cast<A>(x) triggers an implicit cast from {x} to type {A}. This is
// useful in situations where static_cast<A>(x) would do too much.
// Only use this for cheap-to-copy types, or use move semantics explicitly.
template <class A>
V8_INLINE A implicit_cast(A x) {
return x;
// Define our own macros for writing 64-bit constants. This is less fragile
// than defining __STDC_CONSTANT_MACROS before including <stdint.h>, and it
// works on compilers that don't have it (like MSVC).
#if V8_CC_MSVC
# if V8_HOST_ARCH_64_BIT
# define V8_PTR_PREFIX "ll"
# else
# define V8_PTR_PREFIX ""
# endif // V8_HOST_ARCH_64_BIT
#elif V8_CC_MINGW64
# define V8_PTR_PREFIX "I64"
#elif V8_HOST_ARCH_64_BIT
# define V8_PTR_PREFIX "l"
#if V8_OS_AIX
#define V8_PTR_PREFIX "l"
# define V8_PTR_PREFIX ""
#define V8PRIxPTR V8_PTR_PREFIX "x"
#define V8PRIdPTR V8_PTR_PREFIX "d"
#define V8PRIuPTR V8_PTR_PREFIX "u"
#define V8_PTR_HEX_DIGITS 12
#define V8PRIxPTR_FMT "0x%012" V8PRIxPTR
#define V8_PTR_HEX_DIGITS 8
#define V8PRIxPTR_FMT "0x%08" V8PRIxPTR
// ptrdiff_t is 't' according to the standard, but MSVC uses 'I'.
#if V8_CC_MSVC
#define V8PRIxPTRDIFF "Ix"
#define V8PRIdPTRDIFF "Id"
#define V8PRIuPTRDIFF "Iu"
#define V8PRIxPTRDIFF "tx"
#define V8PRIdPTRDIFF "td"
#define V8PRIuPTRDIFF "tu"
// Fix for Mac OS X defining uintptr_t as "unsigned long":
#undef V8PRIxPTR
#define V8PRIxPTR "lx"
#undef V8PRIdPTR
#define V8PRIdPTR "ld"
#undef V8PRIuPTR
#define V8PRIuPTR "lxu"
// Make a uint64 from two uint32_t halves.
inline uint64_t make_uint64(uint32_t high, uint32_t low) {
return (uint64_t{high} << 32) + low;
// Return the largest multiple of m which is <= x.
template <typename T>
inline T RoundDown(T x, intptr_t m) {
// m must be a power of two.
DCHECK(m != 0 && ((m & (m - 1)) == 0));
return x & static_cast<T>(-m);
template <intptr_t m, typename T>
constexpr inline T RoundDown(T x) {
// m must be a power of two.
STATIC_ASSERT(m != 0 && ((m & (m - 1)) == 0));
return x & static_cast<T>(-m);
// Return the smallest multiple of m which is >= x.
template <typename T>
inline T RoundUp(T x, intptr_t m) {
return RoundDown<T>(static_cast<T>(x + m - 1), m);
template <intptr_t m, typename T>
constexpr inline T RoundUp(T x) {
return RoundDown<m, T>(static_cast<T>(x + (m - 1)));
template <typename T, typename U>
constexpr inline bool IsAligned(T value, U alignment) {
return (value & (alignment - 1)) == 0;
inline void* AlignedAddress(void* address, size_t alignment) {
// The alignment must be a power of two.
DCHECK_EQ(alignment & (alignment - 1), 0u);
return reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(address) &
~static_cast<uintptr_t>(alignment - 1));
// Bounds checks for float to integer conversions, which does truncation. Hence,
// the range of legal values is (min - 1, max + 1).
template <typename int_t, typename float_t, typename biggest_int_t = int64_t>
bool is_inbounds(float_t v) {
static_assert(sizeof(int_t) < sizeof(biggest_int_t),
"int_t can't be bounds checked by the compiler");
constexpr float_t kLowerBound =
static_cast<float_t>(std::numeric_limits<int_t>::min()) - 1;
constexpr float_t kUpperBound =
static_cast<float_t>(std::numeric_limits<int_t>::max()) + 1;
constexpr bool kLowerBoundIsMin =
static_cast<biggest_int_t>(kLowerBound) ==
constexpr bool kUpperBoundIsMax =
static_cast<biggest_int_t>(kUpperBound) ==
// Using USE(var) is only a workaround for a GCC 8.1 bug.
return (kLowerBoundIsMin ? (kLowerBound <= v) : (kLowerBound < v)) &&
(kUpperBoundIsMax ? (v <= kUpperBound) : (v < kUpperBound));
#ifdef V8_OS_WIN
// Setup for Windows shared library export.
#define V8_EXPORT_PRIVATE __declspec(dllexport)
#define V8_EXPORT_PRIVATE __declspec(dllimport)
#else // V8_OS_WIN
// Setup for Linux shared library export.
#define V8_EXPORT_PRIVATE __attribute__((visibility("default")))
#endif // V8_OS_WIN
#endif // V8_BASE_MACROS_H_