| 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(HOST_OS_MACOS) | 6 #if defined(HOST_OS_MACOS) |
| 7 | 7 |
| 8 #include <errno.h> // NOLINT | 8 #include <errno.h> // NOLINT |
| 9 #include <mach/clock.h> // NOLINT | 9 #include <mach/clock.h> // NOLINT |
| 10 #include <mach/mach.h> // NOLINT | 10 #include <mach/mach.h> // NOLINT |
| (...skipping 14 matching lines...) Expand all Loading... |
| 25 | 25 |
| 26 OSError::OSError() : sub_system_(kSystem), code_(0), message_(NULL) { | 26 OSError::OSError() : sub_system_(kSystem), code_(0), message_(NULL) { |
| 27 set_sub_system(kSystem); | 27 set_sub_system(kSystem); |
| 28 set_code(errno); | 28 set_code(errno); |
| 29 const int kBufferSize = 1024; | 29 const int kBufferSize = 1024; |
| 30 char error_message[kBufferSize]; | 30 char error_message[kBufferSize]; |
| 31 Utils::StrError(errno, error_message, kBufferSize); | 31 Utils::StrError(errno, error_message, kBufferSize); |
| 32 SetMessage(error_message); | 32 SetMessage(error_message); |
| 33 } | 33 } |
| 34 | 34 |
| 35 | |
| 36 void OSError::SetCodeAndMessage(SubSystem sub_system, int code) { | 35 void OSError::SetCodeAndMessage(SubSystem sub_system, int code) { |
| 37 set_sub_system(sub_system); | 36 set_sub_system(sub_system); |
| 38 set_code(code); | 37 set_code(code); |
| 39 if (sub_system == kSystem) { | 38 if (sub_system == kSystem) { |
| 40 const int kBufferSize = 1024; | 39 const int kBufferSize = 1024; |
| 41 char error_message[kBufferSize]; | 40 char error_message[kBufferSize]; |
| 42 Utils::StrError(code, error_message, kBufferSize); | 41 Utils::StrError(code, error_message, kBufferSize); |
| 43 SetMessage(error_message); | 42 SetMessage(error_message); |
| 44 } else if (sub_system == kGetAddressInfo) { | 43 } else if (sub_system == kGetAddressInfo) { |
| 45 SetMessage(gai_strerror(code)); | 44 SetMessage(gai_strerror(code)); |
| 46 } else { | 45 } else { |
| 47 UNREACHABLE(); | 46 UNREACHABLE(); |
| 48 } | 47 } |
| 49 } | 48 } |
| 50 | 49 |
| 51 | |
| 52 const char* StringUtils::ConsoleStringToUtf8(const char* str, | 50 const char* StringUtils::ConsoleStringToUtf8(const char* str, |
| 53 intptr_t len, | 51 intptr_t len, |
| 54 intptr_t* result_len) { | 52 intptr_t* result_len) { |
| 55 UNIMPLEMENTED(); | 53 UNIMPLEMENTED(); |
| 56 return NULL; | 54 return NULL; |
| 57 } | 55 } |
| 58 | 56 |
| 59 | |
| 60 const char* StringUtils::Utf8ToConsoleString(const char* utf8, | 57 const char* StringUtils::Utf8ToConsoleString(const char* utf8, |
| 61 intptr_t len, | 58 intptr_t len, |
| 62 intptr_t* result_len) { | 59 intptr_t* result_len) { |
| 63 UNIMPLEMENTED(); | 60 UNIMPLEMENTED(); |
| 64 return NULL; | 61 return NULL; |
| 65 } | 62 } |
| 66 | 63 |
| 67 | |
| 68 char* StringUtils::ConsoleStringToUtf8(char* str, | 64 char* StringUtils::ConsoleStringToUtf8(char* str, |
| 69 intptr_t len, | 65 intptr_t len, |
| 70 intptr_t* result_len) { | 66 intptr_t* result_len) { |
| 71 UNIMPLEMENTED(); | 67 UNIMPLEMENTED(); |
| 72 return NULL; | 68 return NULL; |
| 73 } | 69 } |
| 74 | 70 |
| 75 | |
| 76 char* StringUtils::Utf8ToConsoleString(char* utf8, | 71 char* StringUtils::Utf8ToConsoleString(char* utf8, |
| 77 intptr_t len, | 72 intptr_t len, |
| 78 intptr_t* result_len) { | 73 intptr_t* result_len) { |
| 79 UNIMPLEMENTED(); | 74 UNIMPLEMENTED(); |
| 80 return NULL; | 75 return NULL; |
| 81 } | 76 } |
| 82 | 77 |
| 83 | |
| 84 char* StringUtils::StrNDup(const char* s, intptr_t n) { | 78 char* StringUtils::StrNDup(const char* s, intptr_t n) { |
| 85 // strndup has only been added to Mac OS X in 10.7. We are supplying | 79 // strndup has only been added to Mac OS X in 10.7. We are supplying |
| 86 // our own copy here if needed. | 80 // our own copy here if needed. |
| 87 #if !defined(__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__) || \ | 81 #if !defined(__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__) || \ |
| 88 __ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ <= 1060 | 82 __ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ <= 1060 |
| 89 intptr_t len = strlen(s); | 83 intptr_t len = strlen(s); |
| 90 if ((n < 0) || (len < 0)) { | 84 if ((n < 0) || (len < 0)) { |
| 91 return NULL; | 85 return NULL; |
| 92 } | 86 } |
| 93 if (n < len) { | 87 if (n < len) { |
| 94 len = n; | 88 len = n; |
| 95 } | 89 } |
| 96 char* result = reinterpret_cast<char*>(malloc(len + 1)); | 90 char* result = reinterpret_cast<char*>(malloc(len + 1)); |
| 97 if (result == NULL) { | 91 if (result == NULL) { |
| 98 return NULL; | 92 return NULL; |
| 99 } | 93 } |
| 100 result[len] = '\0'; | 94 result[len] = '\0'; |
| 101 return reinterpret_cast<char*>(memmove(result, s, len)); | 95 return reinterpret_cast<char*>(memmove(result, s, len)); |
| 102 #else // !defined(__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__) || ... | 96 #else // !defined(__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__) || ... |
| 103 return strndup(s, n); | 97 return strndup(s, n); |
| 104 #endif // !defined(__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__) || ... | 98 #endif // !defined(__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__) || ... |
| 105 } | 99 } |
| 106 | 100 |
| 107 | |
| 108 bool ShellUtils::GetUtf8Argv(int argc, char** argv) { | 101 bool ShellUtils::GetUtf8Argv(int argc, char** argv) { |
| 109 return false; | 102 return false; |
| 110 } | 103 } |
| 111 | 104 |
| 112 | |
| 113 static mach_timebase_info_data_t timebase_info; | 105 static mach_timebase_info_data_t timebase_info; |
| 114 | 106 |
| 115 | |
| 116 void TimerUtils::InitOnce() { | 107 void TimerUtils::InitOnce() { |
| 117 kern_return_t kr = mach_timebase_info(&timebase_info); | 108 kern_return_t kr = mach_timebase_info(&timebase_info); |
| 118 ASSERT(KERN_SUCCESS == kr); | 109 ASSERT(KERN_SUCCESS == kr); |
| 119 } | 110 } |
| 120 | 111 |
| 121 | |
| 122 int64_t TimerUtils::GetCurrentMonotonicMillis() { | 112 int64_t TimerUtils::GetCurrentMonotonicMillis() { |
| 123 return GetCurrentMonotonicMicros() / 1000; | 113 return GetCurrentMonotonicMicros() / 1000; |
| 124 } | 114 } |
| 125 | 115 |
| 126 | |
| 127 #if HOST_OS_IOS | 116 #if HOST_OS_IOS |
| 128 static int64_t GetCurrentTimeMicros() { | 117 static int64_t GetCurrentTimeMicros() { |
| 129 // gettimeofday has microsecond resolution. | 118 // gettimeofday has microsecond resolution. |
| 130 struct timeval tv; | 119 struct timeval tv; |
| 131 if (gettimeofday(&tv, NULL) < 0) { | 120 if (gettimeofday(&tv, NULL) < 0) { |
| 132 UNREACHABLE(); | 121 UNREACHABLE(); |
| 133 return 0; | 122 return 0; |
| 134 } | 123 } |
| 135 return (static_cast<int64_t>(tv.tv_sec) * 1000000) + tv.tv_usec; | 124 return (static_cast<int64_t>(tv.tv_sec) * 1000000) + tv.tv_usec; |
| 136 } | 125 } |
| 137 #endif // HOST_OS_IOS | 126 #endif // HOST_OS_IOS |
| 138 | 127 |
| 139 | |
| 140 int64_t TimerUtils::GetCurrentMonotonicMicros() { | 128 int64_t TimerUtils::GetCurrentMonotonicMicros() { |
| 141 #if HOST_OS_IOS | 129 #if HOST_OS_IOS |
| 142 // On iOS mach_absolute_time stops while the device is sleeping. Instead use | 130 // On iOS mach_absolute_time stops while the device is sleeping. Instead use |
| 143 // now - KERN_BOOTTIME to get a time difference that is not impacted by clock | 131 // now - KERN_BOOTTIME to get a time difference that is not impacted by clock |
| 144 // changes. KERN_BOOTTIME will be updated by the system whenever the system | 132 // changes. KERN_BOOTTIME will be updated by the system whenever the system |
| 145 // clock change. | 133 // clock change. |
| 146 struct timeval boottime; | 134 struct timeval boottime; |
| 147 int mib[2] = {CTL_KERN, KERN_BOOTTIME}; | 135 int mib[2] = {CTL_KERN, KERN_BOOTTIME}; |
| 148 size_t size = sizeof(boottime); | 136 size_t size = sizeof(boottime); |
| 149 int kr = sysctl(mib, sizeof(mib) / sizeof(mib[0]), &boottime, &size, NULL, 0); | 137 int kr = sysctl(mib, sizeof(mib) / sizeof(mib[0]), &boottime, &size, NULL, 0); |
| 150 ASSERT(KERN_SUCCESS == kr); | 138 ASSERT(KERN_SUCCESS == kr); |
| 151 int64_t now = GetCurrentTimeMicros(); | 139 int64_t now = GetCurrentTimeMicros(); |
| 152 int64_t origin = boottime.tv_sec * kMicrosecondsPerSecond; | 140 int64_t origin = boottime.tv_sec * kMicrosecondsPerSecond; |
| 153 origin += boottime.tv_usec; | 141 origin += boottime.tv_usec; |
| 154 return now - origin; | 142 return now - origin; |
| 155 #else | 143 #else |
| 156 ASSERT(timebase_info.denom != 0); | 144 ASSERT(timebase_info.denom != 0); |
| 157 // timebase_info converts absolute time tick units into nanoseconds. Convert | 145 // timebase_info converts absolute time tick units into nanoseconds. Convert |
| 158 // to microseconds. | 146 // to microseconds. |
| 159 int64_t result = mach_absolute_time() / kNanosecondsPerMicrosecond; | 147 int64_t result = mach_absolute_time() / kNanosecondsPerMicrosecond; |
| 160 result *= timebase_info.numer; | 148 result *= timebase_info.numer; |
| 161 result /= timebase_info.denom; | 149 result /= timebase_info.denom; |
| 162 return result; | 150 return result; |
| 163 #endif // HOST_OS_IOS | 151 #endif // HOST_OS_IOS |
| 164 } | 152 } |
| 165 | 153 |
| 166 | |
| 167 void TimerUtils::Sleep(int64_t millis) { | 154 void TimerUtils::Sleep(int64_t millis) { |
| 168 struct timespec req; // requested. | 155 struct timespec req; // requested. |
| 169 struct timespec rem; // remainder. | 156 struct timespec rem; // remainder. |
| 170 int64_t micros = millis * kMicrosecondsPerMillisecond; | 157 int64_t micros = millis * kMicrosecondsPerMillisecond; |
| 171 int64_t seconds = micros / kMicrosecondsPerSecond; | 158 int64_t seconds = micros / kMicrosecondsPerSecond; |
| 172 micros = micros - seconds * kMicrosecondsPerSecond; | 159 micros = micros - seconds * kMicrosecondsPerSecond; |
| 173 int64_t nanos = micros * kNanosecondsPerMicrosecond; | 160 int64_t nanos = micros * kNanosecondsPerMicrosecond; |
| 174 req.tv_sec = seconds; | 161 req.tv_sec = seconds; |
| 175 req.tv_nsec = nanos; | 162 req.tv_nsec = nanos; |
| 176 while (true) { | 163 while (true) { |
| 177 int r = nanosleep(&req, &rem); | 164 int r = nanosleep(&req, &rem); |
| 178 if (r == 0) { | 165 if (r == 0) { |
| 179 break; | 166 break; |
| 180 } | 167 } |
| 181 // We should only ever see an interrupt error. | 168 // We should only ever see an interrupt error. |
| 182 ASSERT(errno == EINTR); | 169 ASSERT(errno == EINTR); |
| 183 // Copy remainder into requested and repeat. | 170 // Copy remainder into requested and repeat. |
| 184 req = rem; | 171 req = rem; |
| 185 } | 172 } |
| 186 } | 173 } |
| 187 | 174 |
| 188 } // namespace bin | 175 } // namespace bin |
| 189 } // namespace dart | 176 } // namespace dart |
| 190 | 177 |
| 191 #endif // defined(HOST_OS_MACOS) | 178 #endif // defined(HOST_OS_MACOS) |
| OLD | NEW |