OLD | NEW |
1 // Copyright 2014 the V8 project authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef V8_BASE_MACROS_H_ | 5 #ifndef V8_BASE_MACROS_H_ |
6 #define V8_BASE_MACROS_H_ | 6 #define V8_BASE_MACROS_H_ |
7 | 7 |
8 #include <stddef.h> | 8 #include <stddef.h> |
9 #include <stdint.h> | 9 #include <stdint.h> |
10 | 10 |
11 #include <cstring> | 11 #include <cstring> |
12 | 12 |
13 #include "src/base/build_config.h" | 13 #include "src/base/build_config.h" |
14 #include "src/base/compiler-specific.h" | 14 #include "src/base/compiler-specific.h" |
15 #include "src/base/logging.h" | 15 #include "src/base/logging.h" |
16 | 16 |
17 | 17 |
18 // The expression OFFSET_OF(type, field) computes the byte-offset | 18 // The expression OFFSET_OF(type, field) computes the byte-offset |
19 // of the specified field relative to the containing type. This | 19 // of the specified field relative to the containing type. This |
20 // corresponds to 'offsetof' (in stddef.h), except that it doesn't | 20 // corresponds to 'offsetof' (in stddef.h), except that it doesn't |
21 // use 0 or NULL, which causes a problem with the compiler warnings | 21 // use 0 or NULL, which causes a problem with the compiler warnings |
22 // we have enabled (which is also why 'offsetof' doesn't seem to work). | 22 // we have enabled (which is also why 'offsetof' doesn't seem to work). |
23 // Here we simply use the non-zero value 4, which seems to work. | 23 // Here we simply use the non-zero value 4, which seems to work. |
24 #define OFFSET_OF(type, field) \ | 24 #define OFFSET_OF(type, field) \ |
25 (reinterpret_cast<intptr_t>(&(reinterpret_cast<type*>(4)->field)) - 4) | 25 (reinterpret_cast<intptr_t>(&(reinterpret_cast<type*>(4)->field)) - 4) |
26 | 26 |
27 | 27 |
| 28 #if V8_OS_NACL |
| 29 |
28 // ARRAYSIZE_UNSAFE performs essentially the same calculation as arraysize, | 30 // ARRAYSIZE_UNSAFE performs essentially the same calculation as arraysize, |
29 // but can be used on anonymous types or types defined inside | 31 // but can be used on anonymous types or types defined inside |
30 // functions. It's less safe than arraysize as it accepts some | 32 // functions. It's less safe than arraysize as it accepts some |
31 // (although not all) pointers. Therefore, you should use arraysize | 33 // (although not all) pointers. Therefore, you should use arraysize |
32 // whenever possible. | 34 // whenever possible. |
33 // | 35 // |
34 // The expression ARRAYSIZE_UNSAFE(a) is a compile-time constant of type | 36 // The expression ARRAYSIZE_UNSAFE(a) is a compile-time constant of type |
35 // size_t. | 37 // size_t. |
36 // | 38 // |
37 // ARRAYSIZE_UNSAFE catches a few type errors. If you see a compiler error | 39 // ARRAYSIZE_UNSAFE catches a few type errors. If you see a compiler error |
(...skipping 20 matching lines...) Expand all Loading... |
58 // | 60 // |
59 // This macro is not perfect as it wrongfully accepts certain | 61 // This macro is not perfect as it wrongfully accepts certain |
60 // pointers, namely where the pointer size is divisible by the pointee | 62 // pointers, namely where the pointer size is divisible by the pointee |
61 // size. Since all our code has to go through a 32-bit compiler, | 63 // size. Since all our code has to go through a 32-bit compiler, |
62 // where a pointer is 4 bytes, this means all pointers to a type whose | 64 // where a pointer is 4 bytes, this means all pointers to a type whose |
63 // size is 3 or greater than 4 will be (righteously) rejected. | 65 // size is 3 or greater than 4 will be (righteously) rejected. |
64 #define ARRAYSIZE_UNSAFE(a) \ | 66 #define ARRAYSIZE_UNSAFE(a) \ |
65 ((sizeof(a) / sizeof(*(a))) / \ | 67 ((sizeof(a) / sizeof(*(a))) / \ |
66 static_cast<size_t>(!(sizeof(a) % sizeof(*(a))))) // NOLINT | 68 static_cast<size_t>(!(sizeof(a) % sizeof(*(a))))) // NOLINT |
67 | 69 |
68 | |
69 #if V8_OS_NACL | |
70 | |
71 // TODO(bmeurer): For some reason, the NaCl toolchain cannot handle the correct | 70 // TODO(bmeurer): For some reason, the NaCl toolchain cannot handle the correct |
72 // definition of arraysize() below, so we have to use the unsafe version for | 71 // definition of arraysize() below, so we have to use the unsafe version for |
73 // now. | 72 // now. |
74 #define arraysize ARRAYSIZE_UNSAFE | 73 #define arraysize ARRAYSIZE_UNSAFE |
75 | 74 |
76 #else // V8_OS_NACL | 75 #else // V8_OS_NACL |
77 | 76 |
78 // The arraysize(arr) macro returns the # of elements in an array arr. | 77 // The arraysize(arr) macro returns the # of elements in an array arr. |
79 // The expression is a compile-time constant, and therefore can be | 78 // The expression is a compile-time constant, and therefore can be |
80 // used in defining new arrays, for example. If you use arraysize on | 79 // used in defining new arrays, for example. If you use arraysize on |
(...skipping 311 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
392 } | 391 } |
393 | 392 |
394 | 393 |
395 // Return the smallest multiple of m which is >= x. | 394 // Return the smallest multiple of m which is >= x. |
396 template <typename T> | 395 template <typename T> |
397 inline T RoundUp(T x, intptr_t m) { | 396 inline T RoundUp(T x, intptr_t m) { |
398 return RoundDown<T>(static_cast<T>(x + m - 1), m); | 397 return RoundDown<T>(static_cast<T>(x + m - 1), m); |
399 } | 398 } |
400 | 399 |
401 #endif // V8_BASE_MACROS_H_ | 400 #endif // V8_BASE_MACROS_H_ |
OLD | NEW |