| OLD | NEW |
| 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 #include "platform/globals.h" | 5 #include "platform/globals.h" |
| 6 #if defined(TARGET_OS_WINDOWS) | 6 #if defined(TARGET_OS_WINDOWS) |
| 7 | 7 |
| 8 #include <errno.h> // NOLINT | 8 #include <errno.h> // NOLINT |
| 9 #include <time.h> // NOLINT | 9 #include <time.h> // NOLINT |
| 10 | 10 |
| (...skipping 29 matching lines...) Expand all Loading... |
| 40 | 40 |
| 41 | 41 |
| 42 OSError::OSError() : sub_system_(kSystem), code_(0), message_(NULL) { | 42 OSError::OSError() : sub_system_(kSystem), code_(0), message_(NULL) { |
| 43 set_code(GetLastError()); | 43 set_code(GetLastError()); |
| 44 | 44 |
| 45 static const int kMaxMessageLength = 256; | 45 static const int kMaxMessageLength = 256; |
| 46 wchar_t message[kMaxMessageLength]; | 46 wchar_t message[kMaxMessageLength]; |
| 47 FormatMessageIntoBuffer(code_, message, kMaxMessageLength); | 47 FormatMessageIntoBuffer(code_, message, kMaxMessageLength); |
| 48 char* utf8 = StringUtilsWin::WideToUtf8(message); | 48 char* utf8 = StringUtilsWin::WideToUtf8(message); |
| 49 SetMessage(utf8); | 49 SetMessage(utf8); |
| 50 free(utf8); | |
| 51 } | 50 } |
| 52 | 51 |
| 52 |
| 53 void OSError::SetCodeAndMessage(SubSystem sub_system, int code) { | 53 void OSError::SetCodeAndMessage(SubSystem sub_system, int code) { |
| 54 set_sub_system(sub_system); | 54 set_sub_system(sub_system); |
| 55 set_code(code); | 55 set_code(code); |
| 56 | 56 |
| 57 static const int kMaxMessageLength = 256; | 57 static const int kMaxMessageLength = 256; |
| 58 wchar_t message[kMaxMessageLength]; | 58 wchar_t message[kMaxMessageLength]; |
| 59 FormatMessageIntoBuffer(code_, message, kMaxMessageLength); | 59 FormatMessageIntoBuffer(code_, message, kMaxMessageLength); |
| 60 char* utf8 = StringUtilsWin::WideToUtf8(message); | 60 char* utf8 = StringUtilsWin::WideToUtf8(message); |
| 61 SetMessage(utf8); | 61 SetMessage(utf8); |
| 62 free(utf8); | |
| 63 } | 62 } |
| 64 | 63 |
| 64 |
| 65 char* StringUtils::ConsoleStringToUtf8(char* str, | 65 char* StringUtils::ConsoleStringToUtf8(char* str, |
| 66 intptr_t len, | 66 intptr_t len, |
| 67 intptr_t* result_len) { | 67 intptr_t* result_len) { |
| 68 int wide_len = MultiByteToWideChar(CP_ACP, 0, str, len, NULL, 0); | 68 int wide_len = MultiByteToWideChar(CP_ACP, 0, str, len, NULL, 0); |
| 69 wchar_t* wide = new wchar_t[wide_len]; | 69 wchar_t* wide; |
| 70 wide = |
| 71 reinterpret_cast<wchar_t*>(Dart_ScopeAllocate(wide_len * sizeof(*wide))); |
| 70 MultiByteToWideChar(CP_ACP, 0, str, len, wide, wide_len); | 72 MultiByteToWideChar(CP_ACP, 0, str, len, wide, wide_len); |
| 71 char* utf8 = StringUtilsWin::WideToUtf8(wide, wide_len, result_len); | 73 char* utf8 = StringUtilsWin::WideToUtf8(wide, wide_len, result_len); |
| 72 delete[] wide; | |
| 73 return utf8; | 74 return utf8; |
| 74 } | 75 } |
| 75 | 76 |
| 77 |
| 76 char* StringUtils::Utf8ToConsoleString(char* utf8, | 78 char* StringUtils::Utf8ToConsoleString(char* utf8, |
| 77 intptr_t len, | 79 intptr_t len, |
| 78 intptr_t* result_len) { | 80 intptr_t* result_len) { |
| 79 intptr_t wide_len; | 81 intptr_t wide_len; |
| 80 wchar_t* wide = StringUtilsWin::Utf8ToWide(utf8, len, &wide_len); | 82 wchar_t* wide = StringUtilsWin::Utf8ToWide(utf8, len, &wide_len); |
| 81 int system_len = WideCharToMultiByte( | 83 int system_len = WideCharToMultiByte( |
| 82 CP_ACP, 0, wide, wide_len, NULL, 0, NULL, NULL); | 84 CP_ACP, 0, wide, wide_len, NULL, 0, NULL, NULL); |
| 83 char* ansi = reinterpret_cast<char*>(malloc(system_len)); | 85 char* ansi; |
| 86 ansi = |
| 87 reinterpret_cast<char*>(Dart_ScopeAllocate(system_len * sizeof(*ansi))); |
| 84 if (ansi == NULL) { | 88 if (ansi == NULL) { |
| 85 free(wide); | |
| 86 return NULL; | 89 return NULL; |
| 87 } | 90 } |
| 88 WideCharToMultiByte(CP_ACP, 0, wide, wide_len, ansi, system_len, NULL, NULL); | 91 WideCharToMultiByte(CP_ACP, 0, wide, wide_len, ansi, system_len, NULL, NULL); |
| 89 free(wide); | |
| 90 if (result_len != NULL) { | 92 if (result_len != NULL) { |
| 91 *result_len = system_len; | 93 *result_len = system_len; |
| 92 } | 94 } |
| 93 return ansi; | 95 return ansi; |
| 94 } | 96 } |
| 95 | 97 |
| 98 |
| 96 char* StringUtilsWin::WideToUtf8(wchar_t* wide, | 99 char* StringUtilsWin::WideToUtf8(wchar_t* wide, |
| 97 intptr_t len, | 100 intptr_t len, |
| 98 intptr_t* result_len) { | 101 intptr_t* result_len) { |
| 99 // If len is -1 then WideCharToMultiByte will include the terminating | 102 // If len is -1 then WideCharToMultiByte will include the terminating |
| 100 // NUL byte in the length. | 103 // NUL byte in the length. |
| 101 int utf8_len = WideCharToMultiByte( | 104 int utf8_len = WideCharToMultiByte( |
| 102 CP_UTF8, 0, wide, len, NULL, 0, NULL, NULL); | 105 CP_UTF8, 0, wide, len, NULL, 0, NULL, NULL); |
| 103 char* utf8 = reinterpret_cast<char*>(malloc(utf8_len)); | 106 char* utf8; |
| 107 utf8 = reinterpret_cast<char*>(Dart_ScopeAllocate(utf8_len * sizeof(*utf8))); |
| 104 WideCharToMultiByte(CP_UTF8, 0, wide, len, utf8, utf8_len, NULL, NULL); | 108 WideCharToMultiByte(CP_UTF8, 0, wide, len, utf8, utf8_len, NULL, NULL); |
| 105 if (result_len != NULL) { | 109 if (result_len != NULL) { |
| 106 *result_len = utf8_len; | 110 *result_len = utf8_len; |
| 107 } | 111 } |
| 108 return utf8; | 112 return utf8; |
| 109 } | 113 } |
| 110 | 114 |
| 111 | 115 |
| 112 wchar_t* StringUtilsWin::Utf8ToWide(char* utf8, | 116 wchar_t* StringUtilsWin::Utf8ToWide(char* utf8, |
| 113 intptr_t len, | 117 intptr_t len, |
| 114 intptr_t* result_len) { | 118 intptr_t* result_len) { |
| 115 // If len is -1 then MultiByteToWideChar will include the terminating | 119 // If len is -1 then MultiByteToWideChar will include the terminating |
| 116 // NUL byte in the length. | 120 // NUL byte in the length. |
| 117 int wide_len = MultiByteToWideChar(CP_UTF8, 0, utf8, len, NULL, 0); | 121 int wide_len = MultiByteToWideChar(CP_UTF8, 0, utf8, len, NULL, 0); |
| 118 wchar_t* wide = | 122 wchar_t* wide; |
| 119 reinterpret_cast<wchar_t*>(malloc((wide_len) * sizeof(wchar_t))); | 123 wide = |
| 124 reinterpret_cast<wchar_t*>(Dart_ScopeAllocate(wide_len * sizeof(*wide))); |
| 120 MultiByteToWideChar(CP_UTF8, 0, utf8, len, wide, wide_len); | 125 MultiByteToWideChar(CP_UTF8, 0, utf8, len, wide, wide_len); |
| 121 if (result_len != NULL) { | 126 if (result_len != NULL) { |
| 122 *result_len = wide_len; | 127 *result_len = wide_len; |
| 123 } | 128 } |
| 124 return wide; | 129 return wide; |
| 125 } | 130 } |
| 126 | 131 |
| 132 |
| 127 const char* StringUtils::Utf8ToConsoleString( | 133 const char* StringUtils::Utf8ToConsoleString( |
| 128 const char* utf8, intptr_t len, intptr_t* result_len) { | 134 const char* utf8, intptr_t len, intptr_t* result_len) { |
| 129 return const_cast<const char*>( | 135 return const_cast<const char*>( |
| 130 StringUtils::Utf8ToConsoleString( | 136 StringUtils::Utf8ToConsoleString( |
| 131 const_cast<char*>(utf8), len, result_len)); | 137 const_cast<char*>(utf8), len, result_len)); |
| 132 } | 138 } |
| 133 | 139 |
| 140 |
| 134 const char* StringUtils::ConsoleStringToUtf8( | 141 const char* StringUtils::ConsoleStringToUtf8( |
| 135 const char* str, intptr_t len, intptr_t* result_len) { | 142 const char* str, intptr_t len, intptr_t* result_len) { |
| 136 return const_cast<const char*>( | 143 return const_cast<const char*>( |
| 137 StringUtils::ConsoleStringToUtf8( | 144 StringUtils::ConsoleStringToUtf8( |
| 138 const_cast<char*>(str), len, result_len)); | 145 const_cast<char*>(str), len, result_len)); |
| 139 } | 146 } |
| 140 | 147 |
| 148 |
| 141 const char* StringUtilsWin::WideToUtf8( | 149 const char* StringUtilsWin::WideToUtf8( |
| 142 const wchar_t* wide, intptr_t len, intptr_t* result_len) { | 150 const wchar_t* wide, intptr_t len, intptr_t* result_len) { |
| 143 return const_cast<const char*>( | 151 return const_cast<const char*>( |
| 144 StringUtilsWin::WideToUtf8(const_cast<wchar_t*>(wide), len, result_len)); | 152 StringUtilsWin::WideToUtf8(const_cast<wchar_t*>(wide), len, result_len)); |
| 145 } | 153 } |
| 146 | 154 |
| 155 |
| 147 const wchar_t* StringUtilsWin::Utf8ToWide( | 156 const wchar_t* StringUtilsWin::Utf8ToWide( |
| 148 const char* utf8, intptr_t len, intptr_t* result_len) { | 157 const char* utf8, intptr_t len, intptr_t* result_len) { |
| 149 return const_cast<const wchar_t*>( | 158 return const_cast<const wchar_t*>( |
| 150 StringUtilsWin::Utf8ToWide(const_cast<char*>(utf8), len, result_len)); | 159 StringUtilsWin::Utf8ToWide(const_cast<char*>(utf8), len, result_len)); |
| 151 } | 160 } |
| 152 | 161 |
| 162 |
| 153 bool ShellUtils::GetUtf8Argv(int argc, char** argv) { | 163 bool ShellUtils::GetUtf8Argv(int argc, char** argv) { |
| 154 wchar_t* command_line = GetCommandLineW(); | 164 wchar_t* command_line = GetCommandLineW(); |
| 155 int unicode_argc; | 165 int unicode_argc; |
| 156 wchar_t** unicode_argv = CommandLineToArgvW(command_line, &unicode_argc); | 166 wchar_t** unicode_argv = CommandLineToArgvW(command_line, &unicode_argc); |
| 157 if (unicode_argv == NULL) return false; | 167 if (unicode_argv == NULL) { |
| 168 return false; |
| 169 } |
| 158 // The argc passed to main should have the same argc as we get here. | 170 // The argc passed to main should have the same argc as we get here. |
| 159 ASSERT(argc == unicode_argc); | 171 ASSERT(argc == unicode_argc); |
| 160 if (argc < unicode_argc) { | 172 if (argc < unicode_argc) { |
| 161 unicode_argc = argc; | 173 unicode_argc = argc; |
| 162 } | 174 } |
| 163 for (int i = 0; i < unicode_argc; i++) { | 175 for (int i = 0; i < unicode_argc; i++) { |
| 164 wchar_t* arg = unicode_argv[i]; | 176 wchar_t* arg = unicode_argv[i]; |
| 165 argv[i] = StringUtilsWin::WideToUtf8(arg); | 177 int arg_len = |
| 178 WideCharToMultiByte(CP_UTF8, 0, arg, -1, NULL, 0, NULL, NULL); |
| 179 char* utf8_arg = reinterpret_cast<char*>(malloc(arg_len)); |
| 180 WideCharToMultiByte(CP_UTF8, 0, arg, -1, utf8_arg, arg_len, NULL, NULL); |
| 181 argv[i] = utf8_arg; |
| 166 } | 182 } |
| 167 LocalFree(unicode_argv); | 183 LocalFree(unicode_argv); |
| 168 return true; | 184 return true; |
| 169 } | 185 } |
| 170 | 186 |
| 187 |
| 171 static int64_t GetCurrentTimeMicros() { | 188 static int64_t GetCurrentTimeMicros() { |
| 172 static const int64_t kTimeEpoc = 116444736000000000LL; | 189 static const int64_t kTimeEpoc = 116444736000000000LL; |
| 173 static const int64_t kTimeScaler = 10; // 100 ns to us. | 190 static const int64_t kTimeScaler = 10; // 100 ns to us. |
| 174 | 191 |
| 175 // Although win32 uses 64-bit integers for representing timestamps, | 192 // Although win32 uses 64-bit integers for representing timestamps, |
| 176 // these are packed into a FILETIME structure. The FILETIME | 193 // these are packed into a FILETIME structure. The FILETIME |
| 177 // structure is just a struct representing a 64-bit integer. The | 194 // structure is just a struct representing a 64-bit integer. The |
| 178 // TimeStamp union allows access to both a FILETIME and an integer | 195 // TimeStamp union allows access to both a FILETIME and an integer |
| 179 // representation of the timestamp. The Windows timestamp is in | 196 // representation of the timestamp. The Windows timestamp is in |
| 180 // 100-nanosecond intervals since January 1, 1601. | 197 // 100-nanosecond intervals since January 1, 1601. |
| 181 union TimeStamp { | 198 union TimeStamp { |
| 182 FILETIME ft_; | 199 FILETIME ft_; |
| 183 int64_t t_; | 200 int64_t t_; |
| 184 }; | 201 }; |
| 185 TimeStamp time; | 202 TimeStamp time; |
| 186 GetSystemTimeAsFileTime(&time.ft_); | 203 GetSystemTimeAsFileTime(&time.ft_); |
| 187 return (time.t_ - kTimeEpoc) / kTimeScaler; | 204 return (time.t_ - kTimeEpoc) / kTimeScaler; |
| 188 } | 205 } |
| 189 | 206 |
| 207 |
| 190 static int64_t qpc_ticks_per_second = 0; | 208 static int64_t qpc_ticks_per_second = 0; |
| 191 | 209 |
| 192 void TimerUtils::InitOnce() { | 210 void TimerUtils::InitOnce() { |
| 193 LARGE_INTEGER ticks_per_sec; | 211 LARGE_INTEGER ticks_per_sec; |
| 194 if (!QueryPerformanceFrequency(&ticks_per_sec)) { | 212 if (!QueryPerformanceFrequency(&ticks_per_sec)) { |
| 195 qpc_ticks_per_second = 0; | 213 qpc_ticks_per_second = 0; |
| 196 } else { | 214 } else { |
| 197 qpc_ticks_per_second = static_cast<int64_t>(ticks_per_sec.QuadPart); | 215 qpc_ticks_per_second = static_cast<int64_t>(ticks_per_sec.QuadPart); |
| 198 } | 216 } |
| 199 } | 217 } |
| 200 | 218 |
| 219 |
| 201 int64_t TimerUtils::GetCurrentMonotonicMillis() { | 220 int64_t TimerUtils::GetCurrentMonotonicMillis() { |
| 202 return GetCurrentMonotonicMicros() / 1000; | 221 return GetCurrentMonotonicMicros() / 1000; |
| 203 } | 222 } |
| 204 | 223 |
| 224 |
| 205 int64_t TimerUtils::GetCurrentMonotonicMicros() { | 225 int64_t TimerUtils::GetCurrentMonotonicMicros() { |
| 206 if (qpc_ticks_per_second == 0) { | 226 if (qpc_ticks_per_second == 0) { |
| 207 // QueryPerformanceCounter not supported, fallback. | 227 // QueryPerformanceCounter not supported, fallback. |
| 208 return GetCurrentTimeMicros(); | 228 return GetCurrentTimeMicros(); |
| 209 } | 229 } |
| 210 // Grab performance counter value. | 230 // Grab performance counter value. |
| 211 LARGE_INTEGER now; | 231 LARGE_INTEGER now; |
| 212 QueryPerformanceCounter(&now); | 232 QueryPerformanceCounter(&now); |
| 213 int64_t qpc_value = static_cast<int64_t>(now.QuadPart); | 233 int64_t qpc_value = static_cast<int64_t>(now.QuadPart); |
| 214 // Convert to microseconds. | 234 // Convert to microseconds. |
| 215 int64_t seconds = qpc_value / qpc_ticks_per_second; | 235 int64_t seconds = qpc_value / qpc_ticks_per_second; |
| 216 int64_t leftover_ticks = qpc_value - (seconds * qpc_ticks_per_second); | 236 int64_t leftover_ticks = qpc_value - (seconds * qpc_ticks_per_second); |
| 217 int64_t result = seconds * kMicrosecondsPerSecond; | 237 int64_t result = seconds * kMicrosecondsPerSecond; |
| 218 result += ((leftover_ticks * kMicrosecondsPerSecond) / qpc_ticks_per_second); | 238 result += ((leftover_ticks * kMicrosecondsPerSecond) / qpc_ticks_per_second); |
| 219 return result; | 239 return result; |
| 220 } | 240 } |
| 221 | 241 |
| 242 |
| 222 void TimerUtils::Sleep(int64_t millis) { | 243 void TimerUtils::Sleep(int64_t millis) { |
| 223 ::Sleep(millis); | 244 ::Sleep(millis); |
| 224 } | 245 } |
| 225 | 246 |
| 226 } // namespace bin | 247 } // namespace bin |
| 227 } // namespace dart | 248 } // namespace dart |
| 228 | 249 |
| 229 #endif // defined(TARGET_OS_WINDOWS) | 250 #endif // defined(TARGET_OS_WINDOWS) |
| OLD | NEW |