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.
5 #ifndef V8_BASE_MACROS_H_
6 #define V8_BASE_MACROS_H_
8 #include "include/v8stdint.h"
9 #include "src/base/build_config.h"
10 #include "src/base/logging.h"
13 // The expression OFFSET_OF(type, field) computes the byte-offset
14 // of the specified field relative to the containing type. This
15 // corresponds to 'offsetof' (in stddef.h), except that it doesn't
16 // use 0 or NULL, which causes a problem with the compiler warnings
17 // we have enabled (which is also why 'offsetof' doesn't seem to work).
18 // Here we simply use the non-zero value 4, which seems to work.
19 #define OFFSET_OF(type, field) \
20 (reinterpret_cast<intptr_t>(&(reinterpret_cast<type*>(4)->field)) - 4)
23 // ARRAYSIZE_UNSAFE performs essentially the same calculation as arraysize,
24 // but can be used on anonymous types or types defined inside
25 // functions. It's less safe than arraysize as it accepts some
26 // (although not all) pointers. Therefore, you should use arraysize
29 // The expression ARRAYSIZE_UNSAFE(a) is a compile-time constant of type
32 // ARRAYSIZE_UNSAFE catches a few type errors. If you see a compiler error
34 // "warning: division by zero in ..."
36 // when using ARRAYSIZE_UNSAFE, you are (wrongfully) giving it a pointer.
37 // You should only use ARRAYSIZE_UNSAFE on statically allocated arrays.
39 // The following comments are on the implementation details, and can
40 // be ignored by the users.
42 // ARRAYSIZE_UNSAFE(arr) works by inspecting sizeof(arr) (the # of bytes in
43 // the array) and sizeof(*(arr)) (the # of bytes in one array
44 // element). If the former is divisible by the latter, perhaps arr is
45 // indeed an array, in which case the division result is the # of
46 // elements in the array. Otherwise, arr cannot possibly be an array,
47 // and we generate a compiler error to prevent the code from
50 // Since the size of bool is implementation-defined, we need to cast
51 // !(sizeof(a) & sizeof(*(a))) to size_t in order to ensure the final
52 // result has type size_t.
54 // This macro is not perfect as it wrongfully accepts certain
55 // pointers, namely where the pointer size is divisible by the pointee
56 // size. Since all our code has to go through a 32-bit compiler,
57 // where a pointer is 4 bytes, this means all pointers to a type whose
58 // size is 3 or greater than 4 will be (righteously) rejected.
59 #define ARRAYSIZE_UNSAFE(a) \
60 ((sizeof(a) / sizeof(*(a))) / \
61 static_cast<size_t>(!(sizeof(a) % sizeof(*(a))))) // NOLINT
66 // TODO(bmeurer): For some reason, the NaCl toolchain cannot handle the correct
67 // definition of arraysize() below, so we have to use the unsafe version for
69 #define arraysize ARRAYSIZE_UNSAFE
73 // The arraysize(arr) macro returns the # of elements in an array arr.
74 // The expression is a compile-time constant, and therefore can be
75 // used in defining new arrays, for example. If you use arraysize on
76 // a pointer by mistake, you will get a compile-time error.
78 // One caveat is that arraysize() doesn't accept any array of an
79 // anonymous type or a type defined inside a function. In these rare
80 // cases, you have to use the unsafe ARRAYSIZE_UNSAFE() macro below. This is
81 // due to a limitation in C++'s template system. The limitation might
82 // eventually be removed, but it hasn't happened yet.
83 #define arraysize(array) (sizeof(ArraySizeHelper(array)))
86 // This template function declaration is used in defining arraysize.
87 // Note that the function doesn't need an implementation, as we only
89 template <typename T, size_t N>
90 char (&ArraySizeHelper(T (&array)[N]))[N];
94 // That gcc wants both of these prototypes seems mysterious. VC, for
95 // its part, can't decide which to use (another mystery). Matching of
96 // template overloads: the final frontier.
97 template <typename T, size_t N>
98 char (&ArraySizeHelper(const T (&array)[N]))[N];
104 // A macro to disallow the evil copy constructor and operator= functions
105 // This should be used in the private: declarations for a class
106 #define DISALLOW_COPY_AND_ASSIGN(TypeName) \
107 TypeName(const TypeName&) V8_DELETE; \
108 void operator=(const TypeName&) V8_DELETE
111 // A macro to disallow all the implicit constructors, namely the
112 // default constructor, copy constructor and operator= functions.
114 // This should be used in the private: declarations for a class
115 // that wants to prevent anyone from instantiating it. This is
116 // especially useful for classes containing only static methods.
117 #define DISALLOW_IMPLICIT_CONSTRUCTORS(TypeName) \
118 TypeName() V8_DELETE; \
119 DISALLOW_COPY_AND_ASSIGN(TypeName)
122 // Newly written code should use V8_INLINE and V8_NOINLINE directly.
123 #define INLINE(declarator) V8_INLINE declarator
124 #define NO_INLINE(declarator) V8_NOINLINE declarator
127 // Newly written code should use V8_WARN_UNUSED_RESULT.
128 #define MUST_USE_RESULT V8_WARN_UNUSED_RESULT
131 // Define V8_USE_ADDRESS_SANITIZER macros.
132 #if defined(__has_feature)
133 #if __has_feature(address_sanitizer)
134 #define V8_USE_ADDRESS_SANITIZER 1
138 // Define DISABLE_ASAN macros.
139 #ifdef V8_USE_ADDRESS_SANITIZER
140 #define DISABLE_ASAN __attribute__((no_sanitize_address))
147 #define V8_IMMEDIATE_CRASH() __builtin_trap()
149 #define V8_IMMEDIATE_CRASH() ((void(*)())0)()
153 // Use C++11 static_assert if possible, which gives error
154 // messages that are easier to understand on first sight.
155 #if V8_HAS_CXX11_STATIC_ASSERT
156 #define STATIC_ASSERT(test) static_assert(test, #test)
158 // This is inspired by the static assertion facility in boost. This
159 // is pretty magical. If it causes you trouble on a platform you may
160 // find a fix in the boost code.
161 template <bool> class StaticAssertion;
162 template <> class StaticAssertion<true> { };
163 // This macro joins two tokens. If one of the tokens is a macro the
164 // helper call causes it to be resolved before joining.
165 #define SEMI_STATIC_JOIN(a, b) SEMI_STATIC_JOIN_HELPER(a, b)
166 #define SEMI_STATIC_JOIN_HELPER(a, b) a##b
167 // Causes an error during compilation of the condition is not
168 // statically known to be true. It is formulated as a typedef so that
169 // it can be used wherever a typedef can be used. Beware that this
170 // actually causes each use to introduce a new defined type with a
171 // name depending on the source line.
172 template <int> class StaticAssertionHelper { };
173 #define STATIC_ASSERT(test) \
175 StaticAssertionHelper<sizeof(StaticAssertion<static_cast<bool>((test))>)> \
176 SEMI_STATIC_JOIN(__StaticAssertTypedef__, __LINE__) V8_UNUSED
181 // The USE(x) template is used to silence C++ compiler warnings
182 // issued for (yet) unused variables (typically parameters).
183 template <typename T>
184 inline void USE(T) { }
187 #define IS_POWER_OF_TWO(x) ((x) != 0 && (((x) & ((x) - 1)) == 0))
190 // Returns true iff x is a power of 2. Cannot be used with the maximally
191 // negative value of the type T (the -1 overflows).
192 template <typename T>
193 inline bool IsPowerOf2(T x) {
194 return IS_POWER_OF_TWO(x);
198 // Define our own macros for writing 64-bit constants. This is less fragile
199 // than defining __STDC_CONSTANT_MACROS before including <stdint.h>, and it
200 // works on compilers that don't have it (like MSVC).
202 # define V8_UINT64_C(x) (x ## UI64)
203 # define V8_INT64_C(x) (x ## I64)
204 # if V8_HOST_ARCH_64_BIT
205 # define V8_INTPTR_C(x) (x ## I64)
206 # define V8_PTR_PREFIX "ll"
208 # define V8_INTPTR_C(x) (x)
209 # define V8_PTR_PREFIX ""
210 # endif // V8_HOST_ARCH_64_BIT
212 # define V8_UINT64_C(x) (x ## ULL)
213 # define V8_INT64_C(x) (x ## LL)
214 # define V8_INTPTR_C(x) (x ## LL)
215 # define V8_PTR_PREFIX "I64"
216 #elif V8_HOST_ARCH_64_BIT
218 # define V8_UINT64_C(x) (x ## ULL)
219 # define V8_INT64_C(x) (x ## LL)
221 # define V8_UINT64_C(x) (x ## UL)
222 # define V8_INT64_C(x) (x ## L)
224 # define V8_INTPTR_C(x) (x ## L)
225 # define V8_PTR_PREFIX "l"
227 # define V8_UINT64_C(x) (x ## ULL)
228 # define V8_INT64_C(x) (x ## LL)
229 # define V8_INTPTR_C(x) (x)
230 # define V8_PTR_PREFIX ""
233 #define V8PRIxPTR V8_PTR_PREFIX "x"
234 #define V8PRIdPTR V8_PTR_PREFIX "d"
235 #define V8PRIuPTR V8_PTR_PREFIX "u"
237 // Fix for Mac OS X defining uintptr_t as "unsigned long":
240 #define V8PRIxPTR "lx"
243 // The following macro works on both 32 and 64-bit platforms.
244 // Usage: instead of writing 0x1234567890123456
245 // write V8_2PART_UINT64_C(0x12345678,90123456);
246 #define V8_2PART_UINT64_C(a, b) (((static_cast<uint64_t>(a) << 32) + 0x##b##u))
249 // Compute the 0-relative offset of some absolute value x of type T.
250 // This allows conversion of Addresses and integral types into
251 // 0-relative int offsets.
252 template <typename T>
253 inline intptr_t OffsetFrom(T x) {
254 return x - static_cast<T>(0);
258 // Compute the absolute value of type T for some 0-relative offset x.
259 // This allows conversion of 0-relative int offsets into Addresses and
261 template <typename T>
262 inline T AddressFrom(intptr_t x) {
263 return static_cast<T>(static_cast<T>(0) + x);
267 // Return the largest multiple of m which is <= x.
268 template <typename T>
269 inline T RoundDown(T x, intptr_t m) {
270 DCHECK(IsPowerOf2(m));
271 return AddressFrom<T>(OffsetFrom(x) & -m);
275 // Return the smallest multiple of m which is >= x.
276 template <typename T>
277 inline T RoundUp(T x, intptr_t m) {
278 return RoundDown<T>(static_cast<T>(x + m - 1), m);
282 // Increment a pointer until it has the specified alignment.
283 // This works like RoundUp, but it works correctly on pointer types where
284 // sizeof(*pointer) might not be 1.
286 T AlignUp(T pointer, size_t alignment) {
287 DCHECK(sizeof(pointer) == sizeof(uintptr_t));
288 uintptr_t pointer_raw = reinterpret_cast<uintptr_t>(pointer);
289 return reinterpret_cast<T>(RoundUp(pointer_raw, alignment));
293 template <typename T, typename U>
294 inline bool IsAligned(T value, U alignment) {
295 return (value & (alignment - 1)) == 0;
299 // Returns the smallest power of two which is >= x. If you pass in a
300 // number that is already a power of two, it is returned as is.
301 // Implementation is from "Hacker's Delight" by Henry S. Warren, Jr.,
302 // figure 3-3, page 48, where the function is called clp2.
303 inline uint32_t RoundUpToPowerOf2(uint32_t x) {
304 DCHECK(x <= 0x80000000u);
315 inline uint32_t RoundDownToPowerOf2(uint32_t x) {
316 uint32_t rounded_up = RoundUpToPowerOf2(x);
317 if (rounded_up > x) return rounded_up >> 1;
322 // Returns current value of top of the stack. Works correctly with ASAN.
324 inline uintptr_t GetCurrentStackPosition() {
325 // Takes the address of the limit variable in order to find out where
326 // the top of stack is right now.
327 uintptr_t limit = reinterpret_cast<uintptr_t>(&limit);
331 #endif // V8_BASE_MACROS_H_