Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1130)

Side by Side Diff: runtime/platform/globals.h

Issue 9209001: Move utils.h and utils.cc from runtime/vm to runtime/platform (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Addresed new comments from ager@ Created 8 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « runtime/platform/assert.cc ('k') | runtime/platform/platform_headers.gypi » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a 2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file. 3 // BSD-style license that can be found in the LICENSE file.
4 4
5 #ifndef PLATFORM_GLOBALS_H_ 5 #ifndef PLATFORM_GLOBALS_H_
6 #define PLATFORM_GLOBALS_H_ 6 #define PLATFORM_GLOBALS_H_
7 7
8 // __STDC_FORMAT_MACROS has to be defined to enable platform independent printf. 8 // __STDC_FORMAT_MACROS has to be defined to enable platform independent printf.
9 #ifndef __STDC_FORMAT_MACROS 9 #ifndef __STDC_FORMAT_MACROS
10 #define __STDC_FORMAT_MACROS 10 #define __STDC_FORMAT_MACROS
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
54 #if defined(__linux__) || defined(__FreeBSD__) 54 #if defined(__linux__) || defined(__FreeBSD__)
55 #define TARGET_OS_LINUX 1 55 #define TARGET_OS_LINUX 1
56 #elif defined(__APPLE__) 56 #elif defined(__APPLE__)
57 #define TARGET_OS_MACOS 1 57 #define TARGET_OS_MACOS 1
58 #elif defined(_WIN32) 58 #elif defined(_WIN32)
59 #define TARGET_OS_WINDOWS 1 59 #define TARGET_OS_WINDOWS 1
60 #else 60 #else
61 #error Automatic target os detection failed. 61 #error Automatic target os detection failed.
62 #endif 62 #endif
63 63
64 // Processor architecture detection. For more info on what's defined, see:
65 // http://msdn.microsoft.com/en-us/library/b0084kay.aspx
66 // http://www.agner.org/optimize/calling_conventions.pdf
67 // or with gcc, run: "echo | gcc -E -dM -"
68 #if defined(_M_X64) || defined(__x86_64__)
69 #define HOST_ARCH_X64 1
70 #define ARCH_IS_64_BIT 1
71 #elif defined(_M_IX86) || defined(__i386__)
72 #define HOST_ARCH_IA32 1
73 #define ARCH_IS_32_BIT 1
74 #elif defined(__ARMEL__)
75 #define HOST_ARCH_ARM 1
76 #define ARCH_IS_32_BIT 1
77 #else
78 #error Architecture was not detected as supported by Dart.
79 #endif
80
81 #if !defined(TARGET_ARCH_ARM)
82 #if !defined(TARGET_ARCH_X64)
83 #if !defined(TARGET_ARCH_IA32)
84 // No target architecture specified pick the one matching the host architecture.
85 #if defined(HOST_ARCH_ARM)
86 #define TARGET_ARCH_ARM 1
87 #elif defined(HOST_ARCH_X64)
88 #define TARGET_ARCH_X64 1
89 #elif defined(HOST_ARCH_IA32)
90 #define TARGET_ARCH_IA32 1
91 #else
92 #error Automatic target architecture detection failed.
93 #endif
94 #endif
95 #endif
96 #endif
97
98 // Verify that host and target architectures match, we cannot
99 // have a 64 bit Dart VM generating 32 bit code or vice-versa.
100 #if defined(TARGET_ARCH_X64)
101 #if !defined(ARCH_IS_64_BIT)
102 #error Mismatched Host/Target architectures.
103 #endif
104 #elif defined(TARGET_ARCH_IA32) || defined(TARGET_ARCH_ARM)
105 #if !defined(ARCH_IS_32_BIT)
106 #error Mismatched Host/Target architectures.
107 #endif
108 #endif
109
110
111 // Printf format for intptr_t on Windows.
112 #if !defined(PRIxPTR) && defined(TARGET_OS_WINDOWS)
113 #if defined(ARCH_IS_32_BIT)
114 #define PRIxPTR "x"
115 #else
116 #define PRIxPTR "llx"
117 #endif // defined(ARCH_IS_32_BIT)
118 #endif // !defined(PRIxPTR) && defined(TARGET_OS_WINDOWS)
119
120
121 // Suffixes for 64-bit integer literals.
122 #ifdef _MSC_VER
123 #define DART_INT64_C(x) x##I64
124 #define DART_UINT64_C(x) x##UI64
125 #else
126 #define DART_INT64_C(x) x##LL
127 #define DART_UINT64_C(x) x##ULL
128 #endif
129
130
131 // The following macro works on both 32 and 64-bit platforms.
132 // Usage: instead of writing 0x1234567890123456
133 // write DART_2PART_UINT64_C(0x12345678,90123456);
134 #define DART_2PART_UINT64_C(a, b) \
135 (((static_cast<uint64_t>(a) << 32) + 0x##b##u))
136
137
138 // Types for native machine words. Guaranteed to be able to hold pointers and
139 // integers.
140 typedef intptr_t word;
141 typedef uintptr_t uword;
142
143 // Byte sizes.
144 const int kWordSize = sizeof(word);
145 #ifdef ARCH_IS_32_BIT
146 const int kWordSizeLog2 = 2;
147 #else
148 const int kWordSizeLog2 = 3;
149 #endif
150
151 // Bit sizes.
152 const int kBitsPerByte = 8;
153 const int kBitsPerByteLog2 = 3;
154 const int kBitsPerWord = kWordSize * kBitsPerByte;
155
156 // System-wide named constants.
157 const int KB = 1024;
158 const int MB = KB * KB;
159 const int GB = KB * KB * KB;
160 const intptr_t kIntptrOne = 1;
161 const intptr_t kIntptrMin = (kIntptrOne << (kBitsPerWord - 1));
162 const intptr_t kIntptrMax = ~kIntptrMin;
163
164 // Time constants.
165 const int kMillisecondsPerSecond = 1000;
166 const int kMicrosecondsPerMillisecond = 1000;
167 const int kMicrosecondsPerSecond = (kMicrosecondsPerMillisecond *
168 kMillisecondsPerSecond);
169 const int kNanosecondsPerMicrosecond = 1000;
170 const int kNanosecondsPerMillisecond = (kNanosecondsPerMicrosecond *
171 kMicrosecondsPerMillisecond);
172 const int kNanosecondsPerSecond = (kNanosecondsPerMicrosecond *
173 kMicrosecondsPerSecond);
64 174
65 // A macro to disallow the copy constructor and operator= functions. 175 // A macro to disallow the copy constructor and operator= functions.
66 // This should be used in the private: declarations for a class. 176 // This should be used in the private: declarations for a class.
67 #define DISALLOW_COPY_AND_ASSIGN(TypeName) \ 177 #define DISALLOW_COPY_AND_ASSIGN(TypeName) \
68 private: \ 178 private: \
69 TypeName(const TypeName&); \ 179 TypeName(const TypeName&); \
70 void operator=(const TypeName&) 180 void operator=(const TypeName&)
71 181
72 182
73 // A macro to disallow all the implicit constructors, namely the default 183 // A macro to disallow all the implicit constructors, namely the default
(...skipping 15 matching lines...) Expand all
89 private: \ 199 private: \
90 void* operator new(size_t size); 200 void* operator new(size_t size);
91 201
92 202
93 // The USE(x) template is used to silence C++ compiler warnings issued 203 // The USE(x) template is used to silence C++ compiler warnings issued
94 // for unused variables. 204 // for unused variables.
95 template <typename T> 205 template <typename T>
96 static inline void USE(T) { } 206 static inline void USE(T) { }
97 207
98 208
209 // Use implicit_cast as a safe version of static_cast or const_cast
210 // for upcasting in the type hierarchy (i.e. casting a pointer to Foo
211 // to a pointer to SuperclassOfFoo or casting a pointer to Foo to
212 // a const pointer to Foo).
213 // When you use implicit_cast, the compiler checks that the cast is safe.
214 // Such explicit implicit_casts are necessary in surprisingly many
215 // situations where C++ demands an exact type match instead of an
216 // argument type convertable to a target type.
217 //
218 // The From type can be inferred, so the preferred syntax for using
219 // implicit_cast is the same as for static_cast etc.:
220 //
221 // implicit_cast<ToType>(expr)
222 //
223 // implicit_cast would have been part of the C++ standard library,
224 // but the proposal was submitted too late. It will probably make
225 // its way into the language in the future.
226 template<typename To, typename From>
227 inline To implicit_cast(From const &f) {
228 return f;
229 }
230
231
232 // Use like this: down_cast<T*>(foo);
233 template<typename To, typename From> // use like this: down_cast<T*>(foo);
234 inline To down_cast(From* f) { // so we only accept pointers
235 // Ensures that To is a sub-type of From *. This test is here only
236 // for compile-time type checking, and has no overhead in an
237 // optimized build at run-time, as it will be optimized away completely.
238 if (false) {
239 implicit_cast<From, To>(0);
240 }
241 return static_cast<To>(f);
242 }
243
244
245 // The type-based aliasing rule allows the compiler to assume that
246 // pointers of different types (for some definition of different)
247 // never alias each other. Thus the following code does not work:
248 //
249 // float f = foo();
250 // int fbits = *(int*)(&f);
251 //
252 // The compiler 'knows' that the int pointer can't refer to f since
253 // the types don't match, so the compiler may cache f in a register,
254 // leaving random data in fbits. Using C++ style casts makes no
255 // difference, however a pointer to char data is assumed to alias any
256 // other pointer. This is the 'memcpy exception'.
257 //
258 // The bit_cast function uses the memcpy exception to move the bits
259 // from a variable of one type to a variable of another type. Of
260 // course the end result is likely to be implementation dependent.
261 // Most compilers (gcc-4.2 and MSVC 2005) will completely optimize
262 // bit_cast away.
263 //
264 // There is an additional use for bit_cast. Recent gccs will warn when
265 // they see casts that may result in breakage due to the type-based
266 // aliasing rule. If you have checked that there is no breakage you
267 // can use bit_cast to cast one pointer type to another. This confuses
268 // gcc enough that it can no longer see that you have cast one pointer
269 // type to another thus avoiding the warning.
270 template <class D, class S>
271 inline D bit_cast(const S& source) {
272 // Compile time assertion: sizeof(D) == sizeof(S). A compile error
273 // here means your D and S have different sizes.
274 typedef char VerifySizesAreEqual[sizeof(D) == sizeof(S) ? 1 : -1];
275
276 D destination;
277 // This use of memcpy is safe: source and destination cannot overlap.
278 memcpy(&destination, &source, sizeof(destination));
279 return destination;
280 }
281
282
283 // Similar to bit_cast, but allows copying from types of unrelated
284 // sizes. This method was introduced to enable the strict aliasing
285 // optimizations of GCC 4.4. Basically, GCC mindlessly relies on
286 // obscure details in the C++ standard that make reinterpret_cast
287 // virtually useless.
288 template<class D, class S>
289 inline D bit_copy(const S& source) {
290 D destination;
291 // This use of memcpy is safe: source and destination cannot overlap.
292 memcpy(&destination,
293 reinterpret_cast<const void*>(&source),
294 sizeof(destination));
295 return destination;
296 }
297
298
299 // A macro to ensure that memcpy cannot be called. memcpy does not handle
300 // overlapping memory regions. Even though this is well documented it seems
301 // to be used in error quite often. To avoid problems we disallow the direct
302 // use of memcpy here.
303 //
304 // On Windows the basic libraries use memcpy and therefore compilation will
305 // fail if memcpy is overwritten even if user code does not use memcpy.
306 #if defined(memcpy)
307 #undef memcpy
308 #endif
309 #if !defined(TARGET_OS_WINDOWS)
310 #define memcpy "Please use memmove instead of memcpy."
311 #endif
312
313
99 // On Windows the reentrent version of strtok is called 314 // On Windows the reentrent version of strtok is called
100 // strtok_s. Unify on the posix name strtok_r. 315 // strtok_s. Unify on the posix name strtok_r.
101 #if defined(TARGET_OS_WINDOWS) 316 #if defined(TARGET_OS_WINDOWS)
102 #define snprintf _snprintf 317 #define snprintf _snprintf
103 #define strtok_r strtok_s 318 #define strtok_r strtok_s
104 #endif 319 #endif
105 320
106 #if !defined(TARGET_OS_WINDOWS) && !defined(TEMP_FAILURE_RETRY) 321 #if !defined(TARGET_OS_WINDOWS) && !defined(TEMP_FAILURE_RETRY)
107 // TEMP_FAILURE_RETRY is defined in unistd.h on some platforms. The 322 // TEMP_FAILURE_RETRY is defined in unistd.h on some platforms. The
108 // definition below is copied from Linux and adapted to avoid lint 323 // definition below is copied from Linux and adapted to avoid lint
109 // errors (type long int changed to int64_t and do/while split on 324 // errors (type long int changed to int64_t and do/while split on
110 // separate lines with body in {}s). 325 // separate lines with body in {}s).
111 # define TEMP_FAILURE_RETRY(expression) \ 326 # define TEMP_FAILURE_RETRY(expression) \
112 ({ int64_t __result; \ 327 ({ int64_t __result; \
113 do { \ 328 do { \
114 __result = (int64_t) (expression); \ 329 __result = (int64_t) (expression); \
115 } while (__result == -1L && errno == EINTR); \ 330 } while (__result == -1L && errno == EINTR); \
116 __result; }) 331 __result; })
117 #endif 332 #endif
118 333
119 #endif // PLATFORM_GLOBALS_H_ 334 #endif // PLATFORM_GLOBALS_H_
OLDNEW
« no previous file with comments | « runtime/platform/assert.cc ('k') | runtime/platform/platform_headers.gypi » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698