| 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 "vm/globals.h" | 5 #include "vm/globals.h" |
| 6 #if defined(HOST_OS_MACOS) | 6 #if defined(HOST_OS_MACOS) |
| 7 | 7 |
| 8 #include "vm/os.h" | 8 #include "vm/os.h" |
| 9 | 9 |
| 10 #include <errno.h> // NOLINT | 10 #include <errno.h> // NOLINT |
| 11 #include <limits.h> // NOLINT | 11 #include <limits.h> // NOLINT |
| 12 #include <mach/clock.h> // NOLINT |
| 12 #include <mach/mach.h> // NOLINT | 13 #include <mach/mach.h> // NOLINT |
| 13 #include <mach/clock.h> // NOLINT | |
| 14 #include <mach/mach_time.h> // NOLINT | 14 #include <mach/mach_time.h> // NOLINT |
| 15 #include <sys/resource.h> // NOLINT |
| 15 #include <sys/time.h> // NOLINT | 16 #include <sys/time.h> // NOLINT |
| 16 #include <sys/resource.h> // NOLINT | |
| 17 #include <unistd.h> // NOLINT | 17 #include <unistd.h> // NOLINT |
| 18 #if HOST_OS_IOS | 18 #if HOST_OS_IOS |
| 19 #include <sys/sysctl.h> // NOLINT | 19 #include <sys/sysctl.h> // NOLINT |
| 20 #include <syslog.h> // NOLINT | 20 #include <syslog.h> // NOLINT |
| 21 #endif | 21 #endif |
| 22 | 22 |
| 23 #include "platform/utils.h" | 23 #include "platform/utils.h" |
| 24 #include "vm/isolate.h" | 24 #include "vm/isolate.h" |
| 25 #include "vm/zone.h" | 25 #include "vm/zone.h" |
| 26 | 26 |
| 27 namespace dart { | 27 namespace dart { |
| 28 | 28 |
| 29 const char* OS::Name() { | 29 const char* OS::Name() { |
| 30 #if HOST_OS_IOS | 30 #if HOST_OS_IOS |
| 31 return "ios"; | 31 return "ios"; |
| 32 #else | 32 #else |
| 33 return "macos"; | 33 return "macos"; |
| 34 #endif | 34 #endif |
| 35 } | 35 } |
| 36 | 36 |
| 37 | |
| 38 intptr_t OS::ProcessId() { | 37 intptr_t OS::ProcessId() { |
| 39 return static_cast<intptr_t>(getpid()); | 38 return static_cast<intptr_t>(getpid()); |
| 40 } | 39 } |
| 41 | 40 |
| 42 | |
| 43 static bool LocalTime(int64_t seconds_since_epoch, tm* tm_result) { | 41 static bool LocalTime(int64_t seconds_since_epoch, tm* tm_result) { |
| 44 time_t seconds = static_cast<time_t>(seconds_since_epoch); | 42 time_t seconds = static_cast<time_t>(seconds_since_epoch); |
| 45 if (seconds != seconds_since_epoch) return false; | 43 if (seconds != seconds_since_epoch) return false; |
| 46 struct tm* error_code = localtime_r(&seconds, tm_result); | 44 struct tm* error_code = localtime_r(&seconds, tm_result); |
| 47 return error_code != NULL; | 45 return error_code != NULL; |
| 48 } | 46 } |
| 49 | 47 |
| 50 | |
| 51 const char* OS::GetTimeZoneName(int64_t seconds_since_epoch) { | 48 const char* OS::GetTimeZoneName(int64_t seconds_since_epoch) { |
| 52 tm decomposed; | 49 tm decomposed; |
| 53 bool succeeded = LocalTime(seconds_since_epoch, &decomposed); | 50 bool succeeded = LocalTime(seconds_since_epoch, &decomposed); |
| 54 // If unsuccessful, return an empty string like V8 does. | 51 // If unsuccessful, return an empty string like V8 does. |
| 55 return (succeeded && (decomposed.tm_zone != NULL)) ? decomposed.tm_zone : ""; | 52 return (succeeded && (decomposed.tm_zone != NULL)) ? decomposed.tm_zone : ""; |
| 56 } | 53 } |
| 57 | 54 |
| 58 | |
| 59 int OS::GetTimeZoneOffsetInSeconds(int64_t seconds_since_epoch) { | 55 int OS::GetTimeZoneOffsetInSeconds(int64_t seconds_since_epoch) { |
| 60 tm decomposed; | 56 tm decomposed; |
| 61 bool succeeded = LocalTime(seconds_since_epoch, &decomposed); | 57 bool succeeded = LocalTime(seconds_since_epoch, &decomposed); |
| 62 // Even if the offset was 24 hours it would still easily fit into 32 bits. | 58 // Even if the offset was 24 hours it would still easily fit into 32 bits. |
| 63 // If unsuccessful, return zero like V8 does. | 59 // If unsuccessful, return zero like V8 does. |
| 64 return succeeded ? static_cast<int>(decomposed.tm_gmtoff) : 0; | 60 return succeeded ? static_cast<int>(decomposed.tm_gmtoff) : 0; |
| 65 } | 61 } |
| 66 | 62 |
| 67 | |
| 68 int OS::GetLocalTimeZoneAdjustmentInSeconds() { | 63 int OS::GetLocalTimeZoneAdjustmentInSeconds() { |
| 69 // TODO(floitsch): avoid excessive calls to tzset? | 64 // TODO(floitsch): avoid excessive calls to tzset? |
| 70 tzset(); | 65 tzset(); |
| 71 // Even if the offset was 24 hours it would still easily fit into 32 bits. | 66 // Even if the offset was 24 hours it would still easily fit into 32 bits. |
| 72 // Note that Unix and Dart disagree on the sign. | 67 // Note that Unix and Dart disagree on the sign. |
| 73 return static_cast<int>(-timezone); | 68 return static_cast<int>(-timezone); |
| 74 } | 69 } |
| 75 | 70 |
| 76 | |
| 77 int64_t OS::GetCurrentTimeMillis() { | 71 int64_t OS::GetCurrentTimeMillis() { |
| 78 return GetCurrentTimeMicros() / 1000; | 72 return GetCurrentTimeMicros() / 1000; |
| 79 } | 73 } |
| 80 | 74 |
| 81 | |
| 82 int64_t OS::GetCurrentTimeMicros() { | 75 int64_t OS::GetCurrentTimeMicros() { |
| 83 // gettimeofday has microsecond resolution. | 76 // gettimeofday has microsecond resolution. |
| 84 struct timeval tv; | 77 struct timeval tv; |
| 85 if (gettimeofday(&tv, NULL) < 0) { | 78 if (gettimeofday(&tv, NULL) < 0) { |
| 86 UNREACHABLE(); | 79 UNREACHABLE(); |
| 87 return 0; | 80 return 0; |
| 88 } | 81 } |
| 89 return (static_cast<int64_t>(tv.tv_sec) * 1000000) + tv.tv_usec; | 82 return (static_cast<int64_t>(tv.tv_sec) * 1000000) + tv.tv_usec; |
| 90 } | 83 } |
| 91 | 84 |
| 92 | |
| 93 #if !HOST_OS_IOS | 85 #if !HOST_OS_IOS |
| 94 static mach_timebase_info_data_t timebase_info; | 86 static mach_timebase_info_data_t timebase_info; |
| 95 #endif | 87 #endif |
| 96 | 88 |
| 97 | |
| 98 int64_t OS::GetCurrentMonotonicTicks() { | 89 int64_t OS::GetCurrentMonotonicTicks() { |
| 99 #if HOST_OS_IOS | 90 #if HOST_OS_IOS |
| 100 // On iOS mach_absolute_time stops while the device is sleeping. Instead use | 91 // On iOS mach_absolute_time stops while the device is sleeping. Instead use |
| 101 // now - KERN_BOOTTIME to get a time difference that is not impacted by clock | 92 // now - KERN_BOOTTIME to get a time difference that is not impacted by clock |
| 102 // changes. KERN_BOOTTIME will be updated by the system whenever the system | 93 // changes. KERN_BOOTTIME will be updated by the system whenever the system |
| 103 // clock change. | 94 // clock change. |
| 104 struct timeval boottime; | 95 struct timeval boottime; |
| 105 int mib[2] = {CTL_KERN, KERN_BOOTTIME}; | 96 int mib[2] = {CTL_KERN, KERN_BOOTTIME}; |
| 106 size_t size = sizeof(boottime); | 97 size_t size = sizeof(boottime); |
| 107 int kr = sysctl(mib, sizeof(mib) / sizeof(mib[0]), &boottime, &size, NULL, 0); | 98 int kr = sysctl(mib, sizeof(mib) / sizeof(mib[0]), &boottime, &size, NULL, 0); |
| 108 ASSERT(KERN_SUCCESS == kr); | 99 ASSERT(KERN_SUCCESS == kr); |
| 109 int64_t now = GetCurrentTimeMicros(); | 100 int64_t now = GetCurrentTimeMicros(); |
| 110 int64_t origin = boottime.tv_sec * kMicrosecondsPerSecond; | 101 int64_t origin = boottime.tv_sec * kMicrosecondsPerSecond; |
| 111 origin += boottime.tv_usec; | 102 origin += boottime.tv_usec; |
| 112 return now - origin; | 103 return now - origin; |
| 113 #else | 104 #else |
| 114 if (timebase_info.denom == 0) { | 105 if (timebase_info.denom == 0) { |
| 115 kern_return_t kr = mach_timebase_info(&timebase_info); | 106 kern_return_t kr = mach_timebase_info(&timebase_info); |
| 116 ASSERT(KERN_SUCCESS == kr); | 107 ASSERT(KERN_SUCCESS == kr); |
| 117 } | 108 } |
| 118 ASSERT(timebase_info.denom != 0); | 109 ASSERT(timebase_info.denom != 0); |
| 119 // timebase_info converts absolute time tick units into nanoseconds. | 110 // timebase_info converts absolute time tick units into nanoseconds. |
| 120 int64_t result = mach_absolute_time(); | 111 int64_t result = mach_absolute_time(); |
| 121 result *= timebase_info.numer; | 112 result *= timebase_info.numer; |
| 122 result /= timebase_info.denom; | 113 result /= timebase_info.denom; |
| 123 return result; | 114 return result; |
| 124 #endif // HOST_OS_IOS | 115 #endif // HOST_OS_IOS |
| 125 } | 116 } |
| 126 | 117 |
| 127 | |
| 128 int64_t OS::GetCurrentMonotonicFrequency() { | 118 int64_t OS::GetCurrentMonotonicFrequency() { |
| 129 #if HOST_OS_IOS | 119 #if HOST_OS_IOS |
| 130 return kMicrosecondsPerSecond; | 120 return kMicrosecondsPerSecond; |
| 131 #else | 121 #else |
| 132 return kNanosecondsPerSecond; | 122 return kNanosecondsPerSecond; |
| 133 #endif // HOST_OS_IOS | 123 #endif // HOST_OS_IOS |
| 134 } | 124 } |
| 135 | 125 |
| 136 | |
| 137 int64_t OS::GetCurrentMonotonicMicros() { | 126 int64_t OS::GetCurrentMonotonicMicros() { |
| 138 #if HOST_OS_IOS | 127 #if HOST_OS_IOS |
| 139 ASSERT(GetCurrentMonotonicFrequency() == kMicrosecondsPerSecond); | 128 ASSERT(GetCurrentMonotonicFrequency() == kMicrosecondsPerSecond); |
| 140 return GetCurrentMonotonicTicks(); | 129 return GetCurrentMonotonicTicks(); |
| 141 #else | 130 #else |
| 142 ASSERT(GetCurrentMonotonicFrequency() == kNanosecondsPerSecond); | 131 ASSERT(GetCurrentMonotonicFrequency() == kNanosecondsPerSecond); |
| 143 return GetCurrentMonotonicTicks() / kNanosecondsPerMicrosecond; | 132 return GetCurrentMonotonicTicks() / kNanosecondsPerMicrosecond; |
| 144 #endif // HOST_OS_IOS | 133 #endif // HOST_OS_IOS |
| 145 } | 134 } |
| 146 | 135 |
| 147 | |
| 148 int64_t OS::GetCurrentThreadCPUMicros() { | 136 int64_t OS::GetCurrentThreadCPUMicros() { |
| 149 mach_msg_type_number_t count = THREAD_BASIC_INFO_COUNT; | 137 mach_msg_type_number_t count = THREAD_BASIC_INFO_COUNT; |
| 150 thread_basic_info_data_t info_data; | 138 thread_basic_info_data_t info_data; |
| 151 thread_basic_info_t info = &info_data; | 139 thread_basic_info_t info = &info_data; |
| 152 mach_port_t thread_port = mach_thread_self(); | 140 mach_port_t thread_port = mach_thread_self(); |
| 153 if (thread_port == MACH_PORT_NULL) { | 141 if (thread_port == MACH_PORT_NULL) { |
| 154 return -1; | 142 return -1; |
| 155 } | 143 } |
| 156 kern_return_t r = | 144 kern_return_t r = |
| 157 thread_info(thread_port, THREAD_BASIC_INFO, (thread_info_t)info, &count); | 145 thread_info(thread_port, THREAD_BASIC_INFO, (thread_info_t)info, &count); |
| 158 mach_port_deallocate(mach_task_self(), thread_port); | 146 mach_port_deallocate(mach_task_self(), thread_port); |
| 159 ASSERT(r == KERN_SUCCESS); | 147 ASSERT(r == KERN_SUCCESS); |
| 160 int64_t thread_cpu_micros = | 148 int64_t thread_cpu_micros = |
| 161 (info->system_time.seconds + info->user_time.seconds); | 149 (info->system_time.seconds + info->user_time.seconds); |
| 162 thread_cpu_micros *= kMicrosecondsPerSecond; | 150 thread_cpu_micros *= kMicrosecondsPerSecond; |
| 163 thread_cpu_micros += info->user_time.microseconds; | 151 thread_cpu_micros += info->user_time.microseconds; |
| 164 thread_cpu_micros += info->system_time.microseconds; | 152 thread_cpu_micros += info->system_time.microseconds; |
| 165 return thread_cpu_micros; | 153 return thread_cpu_micros; |
| 166 } | 154 } |
| 167 | 155 |
| 168 | |
| 169 intptr_t OS::ActivationFrameAlignment() { | 156 intptr_t OS::ActivationFrameAlignment() { |
| 170 #if HOST_OS_IOS | 157 #if HOST_OS_IOS |
| 171 #if TARGET_ARCH_ARM | 158 #if TARGET_ARCH_ARM |
| 172 // Even if we generate code that maintains a stronger alignment, we cannot | 159 // Even if we generate code that maintains a stronger alignment, we cannot |
| 173 // assert the stronger stack alignment because C++ code will not maintain it. | 160 // assert the stronger stack alignment because C++ code will not maintain it. |
| 174 return 8; | 161 return 8; |
| 175 #elif TARGET_ARCH_ARM64 | 162 #elif TARGET_ARCH_ARM64 |
| 176 return 16; | 163 return 16; |
| 177 #elif TARGET_ARCH_IA32 | 164 #elif TARGET_ARCH_IA32 |
| 178 return 16; // iOS simulator | 165 return 16; // iOS simulator |
| 179 #elif TARGET_ARCH_X64 | 166 #elif TARGET_ARCH_X64 |
| 180 return 16; // iOS simulator | 167 return 16; // iOS simulator |
| 181 #elif TARGET_ARCH_DBC | 168 #elif TARGET_ARCH_DBC |
| 182 return 16; | 169 return 16; |
| 183 #else | 170 #else |
| 184 #error Unimplemented | 171 #error Unimplemented |
| 185 #endif | 172 #endif |
| 186 #else // HOST_OS_IOS | 173 #else // HOST_OS_IOS |
| 187 // OS X activation frames must be 16 byte-aligned; see "Mac OS X ABI | 174 // OS X activation frames must be 16 byte-aligned; see "Mac OS X ABI |
| 188 // Function Call Guide". | 175 // Function Call Guide". |
| 189 return 16; | 176 return 16; |
| 190 #endif // HOST_OS_IOS | 177 #endif // HOST_OS_IOS |
| 191 } | 178 } |
| 192 | 179 |
| 193 | |
| 194 intptr_t OS::PreferredCodeAlignment() { | 180 intptr_t OS::PreferredCodeAlignment() { |
| 195 #if defined(TARGET_ARCH_IA32) || defined(TARGET_ARCH_X64) || \ | 181 #if defined(TARGET_ARCH_IA32) || defined(TARGET_ARCH_X64) || \ |
| 196 defined(TARGET_ARCH_ARM64) || defined(TARGET_ARCH_DBC) | 182 defined(TARGET_ARCH_ARM64) || defined(TARGET_ARCH_DBC) |
| 197 const int kMinimumAlignment = 32; | 183 const int kMinimumAlignment = 32; |
| 198 #elif defined(TARGET_ARCH_ARM) | 184 #elif defined(TARGET_ARCH_ARM) |
| 199 const int kMinimumAlignment = 16; | 185 const int kMinimumAlignment = 16; |
| 200 #else | 186 #else |
| 201 #error Unsupported architecture. | 187 #error Unsupported architecture. |
| 202 #endif | 188 #endif |
| 203 intptr_t alignment = kMinimumAlignment; | 189 intptr_t alignment = kMinimumAlignment; |
| 204 // TODO(5411554): Allow overriding default code alignment for | 190 // TODO(5411554): Allow overriding default code alignment for |
| 205 // testing purposes. | 191 // testing purposes. |
| 206 // Flags::DebugIsInt("codealign", &alignment); | 192 // Flags::DebugIsInt("codealign", &alignment); |
| 207 ASSERT(Utils::IsPowerOfTwo(alignment)); | 193 ASSERT(Utils::IsPowerOfTwo(alignment)); |
| 208 ASSERT(alignment >= kMinimumAlignment); | 194 ASSERT(alignment >= kMinimumAlignment); |
| 209 ASSERT(alignment <= OS::kMaxPreferredCodeAlignment); | 195 ASSERT(alignment <= OS::kMaxPreferredCodeAlignment); |
| 210 return alignment; | 196 return alignment; |
| 211 } | 197 } |
| 212 | 198 |
| 213 | |
| 214 int OS::NumberOfAvailableProcessors() { | 199 int OS::NumberOfAvailableProcessors() { |
| 215 return sysconf(_SC_NPROCESSORS_ONLN); | 200 return sysconf(_SC_NPROCESSORS_ONLN); |
| 216 } | 201 } |
| 217 | 202 |
| 218 | |
| 219 uintptr_t OS::MaxRSS() { | 203 uintptr_t OS::MaxRSS() { |
| 220 struct rusage usage; | 204 struct rusage usage; |
| 221 usage.ru_maxrss = 0; | 205 usage.ru_maxrss = 0; |
| 222 int r = getrusage(RUSAGE_SELF, &usage); | 206 int r = getrusage(RUSAGE_SELF, &usage); |
| 223 ASSERT(r == 0); | 207 ASSERT(r == 0); |
| 224 return usage.ru_maxrss; | 208 return usage.ru_maxrss; |
| 225 } | 209 } |
| 226 | 210 |
| 227 | |
| 228 void OS::Sleep(int64_t millis) { | 211 void OS::Sleep(int64_t millis) { |
| 229 int64_t micros = millis * kMicrosecondsPerMillisecond; | 212 int64_t micros = millis * kMicrosecondsPerMillisecond; |
| 230 SleepMicros(micros); | 213 SleepMicros(micros); |
| 231 } | 214 } |
| 232 | 215 |
| 233 | |
| 234 void OS::SleepMicros(int64_t micros) { | 216 void OS::SleepMicros(int64_t micros) { |
| 235 struct timespec req; // requested. | 217 struct timespec req; // requested. |
| 236 struct timespec rem; // remainder. | 218 struct timespec rem; // remainder. |
| 237 int64_t seconds = micros / kMicrosecondsPerSecond; | 219 int64_t seconds = micros / kMicrosecondsPerSecond; |
| 238 if (seconds > kMaxInt32) { | 220 if (seconds > kMaxInt32) { |
| 239 // Avoid truncation of overly large sleep values. | 221 // Avoid truncation of overly large sleep values. |
| 240 seconds = kMaxInt32; | 222 seconds = kMaxInt32; |
| 241 } | 223 } |
| 242 micros = micros - seconds * kMicrosecondsPerSecond; | 224 micros = micros - seconds * kMicrosecondsPerSecond; |
| 243 int64_t nanos = micros * kNanosecondsPerMicrosecond; | 225 int64_t nanos = micros * kNanosecondsPerMicrosecond; |
| 244 req.tv_sec = static_cast<int32_t>(seconds); | 226 req.tv_sec = static_cast<int32_t>(seconds); |
| 245 req.tv_nsec = static_cast<long>(nanos); // NOLINT (long used in timespec). | 227 req.tv_nsec = static_cast<long>(nanos); // NOLINT (long used in timespec). |
| 246 while (true) { | 228 while (true) { |
| 247 int r = nanosleep(&req, &rem); | 229 int r = nanosleep(&req, &rem); |
| 248 if (r == 0) { | 230 if (r == 0) { |
| 249 break; | 231 break; |
| 250 } | 232 } |
| 251 // We should only ever see an interrupt error. | 233 // We should only ever see an interrupt error. |
| 252 ASSERT(errno == EINTR); | 234 ASSERT(errno == EINTR); |
| 253 // Copy remainder into requested and repeat. | 235 // Copy remainder into requested and repeat. |
| 254 req = rem; | 236 req = rem; |
| 255 } | 237 } |
| 256 } | 238 } |
| 257 | 239 |
| 258 | |
| 259 void OS::DebugBreak() { | 240 void OS::DebugBreak() { |
| 260 __builtin_trap(); | 241 __builtin_trap(); |
| 261 } | 242 } |
| 262 | 243 |
| 263 | |
| 264 uintptr_t DART_NOINLINE OS::GetProgramCounter() { | 244 uintptr_t DART_NOINLINE OS::GetProgramCounter() { |
| 265 return reinterpret_cast<uintptr_t>( | 245 return reinterpret_cast<uintptr_t>( |
| 266 __builtin_extract_return_addr(__builtin_return_address(0))); | 246 __builtin_extract_return_addr(__builtin_return_address(0))); |
| 267 } | 247 } |
| 268 | 248 |
| 269 | |
| 270 char* OS::StrNDup(const char* s, intptr_t n) { | 249 char* OS::StrNDup(const char* s, intptr_t n) { |
| 271 // strndup has only been added to Mac OS X in 10.7. We are supplying | 250 // strndup has only been added to Mac OS X in 10.7. We are supplying |
| 272 // our own copy here if needed. | 251 // our own copy here if needed. |
| 273 #if !defined(__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__) || \ | 252 #if !defined(__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__) || \ |
| 274 __ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ <= 1060 | 253 __ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ <= 1060 |
| 275 intptr_t len = strlen(s); | 254 intptr_t len = strlen(s); |
| 276 if ((n < 0) || (len < 0)) { | 255 if ((n < 0) || (len < 0)) { |
| 277 return NULL; | 256 return NULL; |
| 278 } | 257 } |
| 279 if (n < len) { | 258 if (n < len) { |
| 280 len = n; | 259 len = n; |
| 281 } | 260 } |
| 282 char* result = reinterpret_cast<char*>(malloc(len + 1)); | 261 char* result = reinterpret_cast<char*>(malloc(len + 1)); |
| 283 if (result == NULL) { | 262 if (result == NULL) { |
| 284 return NULL; | 263 return NULL; |
| 285 } | 264 } |
| 286 result[len] = '\0'; | 265 result[len] = '\0'; |
| 287 return reinterpret_cast<char*>(memmove(result, s, len)); | 266 return reinterpret_cast<char*>(memmove(result, s, len)); |
| 288 #else // !defined(__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__) || ... | 267 #else // !defined(__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__) || ... |
| 289 return strndup(s, n); | 268 return strndup(s, n); |
| 290 #endif // !defined(__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__) || ... | 269 #endif // !defined(__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__) || ... |
| 291 } | 270 } |
| 292 | 271 |
| 293 | |
| 294 intptr_t OS::StrNLen(const char* s, intptr_t n) { | 272 intptr_t OS::StrNLen(const char* s, intptr_t n) { |
| 295 // strnlen has only been added to Mac OS X in 10.7. We are supplying | 273 // strnlen has only been added to Mac OS X in 10.7. We are supplying |
| 296 // our own copy here if needed. | 274 // our own copy here if needed. |
| 297 #if !defined(__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__) || \ | 275 #if !defined(__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__) || \ |
| 298 __ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ <= 1060 | 276 __ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ <= 1060 |
| 299 intptr_t len = 0; | 277 intptr_t len = 0; |
| 300 while ((len <= n) && (*s != '\0')) { | 278 while ((len <= n) && (*s != '\0')) { |
| 301 s++; | 279 s++; |
| 302 len++; | 280 len++; |
| 303 } | 281 } |
| 304 return len; | 282 return len; |
| 305 #else // !defined(__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__) || ... | 283 #else // !defined(__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__) || ... |
| 306 return strnlen(s, n); | 284 return strnlen(s, n); |
| 307 #endif // !defined(__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__) || ... | 285 #endif // !defined(__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__) || ... |
| 308 } | 286 } |
| 309 | 287 |
| 310 | |
| 311 void OS::Print(const char* format, ...) { | 288 void OS::Print(const char* format, ...) { |
| 312 #if HOST_OS_IOS | 289 #if HOST_OS_IOS |
| 313 va_list args; | 290 va_list args; |
| 314 va_start(args, format); | 291 va_start(args, format); |
| 315 vsyslog(LOG_INFO, format, args); | 292 vsyslog(LOG_INFO, format, args); |
| 316 va_end(args); | 293 va_end(args); |
| 317 #else | 294 #else |
| 318 va_list args; | 295 va_list args; |
| 319 va_start(args, format); | 296 va_start(args, format); |
| 320 VFPrint(stdout, format, args); | 297 VFPrint(stdout, format, args); |
| 321 va_end(args); | 298 va_end(args); |
| 322 #endif | 299 #endif |
| 323 } | 300 } |
| 324 | 301 |
| 325 | |
| 326 void OS::VFPrint(FILE* stream, const char* format, va_list args) { | 302 void OS::VFPrint(FILE* stream, const char* format, va_list args) { |
| 327 vfprintf(stream, format, args); | 303 vfprintf(stream, format, args); |
| 328 fflush(stream); | 304 fflush(stream); |
| 329 } | 305 } |
| 330 | 306 |
| 331 | |
| 332 int OS::SNPrint(char* str, size_t size, const char* format, ...) { | 307 int OS::SNPrint(char* str, size_t size, const char* format, ...) { |
| 333 va_list args; | 308 va_list args; |
| 334 va_start(args, format); | 309 va_start(args, format); |
| 335 int retval = VSNPrint(str, size, format, args); | 310 int retval = VSNPrint(str, size, format, args); |
| 336 va_end(args); | 311 va_end(args); |
| 337 return retval; | 312 return retval; |
| 338 } | 313 } |
| 339 | 314 |
| 340 | |
| 341 int OS::VSNPrint(char* str, size_t size, const char* format, va_list args) { | 315 int OS::VSNPrint(char* str, size_t size, const char* format, va_list args) { |
| 342 int retval = vsnprintf(str, size, format, args); | 316 int retval = vsnprintf(str, size, format, args); |
| 343 if (retval < 0) { | 317 if (retval < 0) { |
| 344 FATAL1("Fatal error in OS::VSNPrint with format '%s'", format); | 318 FATAL1("Fatal error in OS::VSNPrint with format '%s'", format); |
| 345 } | 319 } |
| 346 return retval; | 320 return retval; |
| 347 } | 321 } |
| 348 | 322 |
| 349 | |
| 350 char* OS::SCreate(Zone* zone, const char* format, ...) { | 323 char* OS::SCreate(Zone* zone, const char* format, ...) { |
| 351 va_list args; | 324 va_list args; |
| 352 va_start(args, format); | 325 va_start(args, format); |
| 353 char* buffer = VSCreate(zone, format, args); | 326 char* buffer = VSCreate(zone, format, args); |
| 354 va_end(args); | 327 va_end(args); |
| 355 return buffer; | 328 return buffer; |
| 356 } | 329 } |
| 357 | 330 |
| 358 | |
| 359 char* OS::VSCreate(Zone* zone, const char* format, va_list args) { | 331 char* OS::VSCreate(Zone* zone, const char* format, va_list args) { |
| 360 // Measure. | 332 // Measure. |
| 361 va_list measure_args; | 333 va_list measure_args; |
| 362 va_copy(measure_args, args); | 334 va_copy(measure_args, args); |
| 363 intptr_t len = VSNPrint(NULL, 0, format, measure_args); | 335 intptr_t len = VSNPrint(NULL, 0, format, measure_args); |
| 364 va_end(measure_args); | 336 va_end(measure_args); |
| 365 | 337 |
| 366 char* buffer; | 338 char* buffer; |
| 367 if (zone) { | 339 if (zone) { |
| 368 buffer = zone->Alloc<char>(len + 1); | 340 buffer = zone->Alloc<char>(len + 1); |
| 369 } else { | 341 } else { |
| 370 buffer = reinterpret_cast<char*>(malloc(len + 1)); | 342 buffer = reinterpret_cast<char*>(malloc(len + 1)); |
| 371 } | 343 } |
| 372 ASSERT(buffer != NULL); | 344 ASSERT(buffer != NULL); |
| 373 | 345 |
| 374 // Print. | 346 // Print. |
| 375 va_list print_args; | 347 va_list print_args; |
| 376 va_copy(print_args, args); | 348 va_copy(print_args, args); |
| 377 VSNPrint(buffer, len + 1, format, print_args); | 349 VSNPrint(buffer, len + 1, format, print_args); |
| 378 va_end(print_args); | 350 va_end(print_args); |
| 379 return buffer; | 351 return buffer; |
| 380 } | 352 } |
| 381 | 353 |
| 382 | |
| 383 bool OS::StringToInt64(const char* str, int64_t* value) { | 354 bool OS::StringToInt64(const char* str, int64_t* value) { |
| 384 ASSERT(str != NULL && strlen(str) > 0 && value != NULL); | 355 ASSERT(str != NULL && strlen(str) > 0 && value != NULL); |
| 385 int32_t base = 10; | 356 int32_t base = 10; |
| 386 char* endptr; | 357 char* endptr; |
| 387 int i = 0; | 358 int i = 0; |
| 388 if (str[0] == '-') { | 359 if (str[0] == '-') { |
| 389 i = 1; | 360 i = 1; |
| 390 } | 361 } |
| 391 if ((str[i] == '0') && (str[i + 1] == 'x' || str[i + 1] == 'X') && | 362 if ((str[i] == '0') && (str[i + 1] == 'x' || str[i + 1] == 'X') && |
| 392 (str[i + 2] != '\0')) { | 363 (str[i + 2] != '\0')) { |
| 393 base = 16; | 364 base = 16; |
| 394 } | 365 } |
| 395 errno = 0; | 366 errno = 0; |
| 396 *value = strtoll(str, &endptr, base); | 367 *value = strtoll(str, &endptr, base); |
| 397 return ((errno == 0) && (endptr != str) && (*endptr == 0)); | 368 return ((errno == 0) && (endptr != str) && (*endptr == 0)); |
| 398 } | 369 } |
| 399 | 370 |
| 400 | |
| 401 void OS::RegisterCodeObservers() {} | 371 void OS::RegisterCodeObservers() {} |
| 402 | 372 |
| 403 | |
| 404 void OS::PrintErr(const char* format, ...) { | 373 void OS::PrintErr(const char* format, ...) { |
| 405 #if HOST_OS_IOS | 374 #if HOST_OS_IOS |
| 406 va_list args; | 375 va_list args; |
| 407 va_start(args, format); | 376 va_start(args, format); |
| 408 vsyslog(LOG_ERR, format, args); | 377 vsyslog(LOG_ERR, format, args); |
| 409 va_end(args); | 378 va_end(args); |
| 410 #else | 379 #else |
| 411 va_list args; | 380 va_list args; |
| 412 va_start(args, format); | 381 va_start(args, format); |
| 413 VFPrint(stderr, format, args); | 382 VFPrint(stderr, format, args); |
| 414 va_end(args); | 383 va_end(args); |
| 415 #endif | 384 #endif |
| 416 } | 385 } |
| 417 | 386 |
| 418 | |
| 419 void OS::InitOnce() { | 387 void OS::InitOnce() { |
| 420 // TODO(5411554): For now we check that initonce is called only once, | 388 // TODO(5411554): For now we check that initonce is called only once, |
| 421 // Once there is more formal mechanism to call InitOnce we can move | 389 // Once there is more formal mechanism to call InitOnce we can move |
| 422 // this check there. | 390 // this check there. |
| 423 static bool init_once_called = false; | 391 static bool init_once_called = false; |
| 424 ASSERT(init_once_called == false); | 392 ASSERT(init_once_called == false); |
| 425 init_once_called = true; | 393 init_once_called = true; |
| 426 | 394 |
| 427 // See https://github.com/dart-lang/sdk/issues/29539 | 395 // See https://github.com/dart-lang/sdk/issues/29539 |
| 428 // This is a workaround for a macos bug, we eagerly call localtime_r so that | 396 // This is a workaround for a macos bug, we eagerly call localtime_r so that |
| 429 // libnotify is initialized early before any fork happens. | 397 // libnotify is initialized early before any fork happens. |
| 430 struct timeval tv; | 398 struct timeval tv; |
| 431 if (gettimeofday(&tv, NULL) < 0) { | 399 if (gettimeofday(&tv, NULL) < 0) { |
| 432 FATAL1("gettimeofday returned an error (%s)\n", strerror(errno)); | 400 FATAL1("gettimeofday returned an error (%s)\n", strerror(errno)); |
| 433 return; | 401 return; |
| 434 } | 402 } |
| 435 tm decomposed; | 403 tm decomposed; |
| 436 struct tm* error_code = localtime_r(&(tv.tv_sec), &decomposed); | 404 struct tm* error_code = localtime_r(&(tv.tv_sec), &decomposed); |
| 437 if (error_code == NULL) { | 405 if (error_code == NULL) { |
| 438 FATAL1("localtime_r returned an error (%s)\n", strerror(errno)); | 406 FATAL1("localtime_r returned an error (%s)\n", strerror(errno)); |
| 439 return; | 407 return; |
| 440 } | 408 } |
| 441 } | 409 } |
| 442 | 410 |
| 443 | |
| 444 void OS::Shutdown() {} | 411 void OS::Shutdown() {} |
| 445 | 412 |
| 446 | |
| 447 void OS::Abort() { | 413 void OS::Abort() { |
| 448 abort(); | 414 abort(); |
| 449 } | 415 } |
| 450 | 416 |
| 451 | |
| 452 void OS::Exit(int code) { | 417 void OS::Exit(int code) { |
| 453 exit(code); | 418 exit(code); |
| 454 } | 419 } |
| 455 | 420 |
| 456 } // namespace dart | 421 } // namespace dart |
| 457 | 422 |
| 458 #endif // defined(HOST_OS_MACOS) | 423 #endif // defined(HOST_OS_MACOS) |
| OLD | NEW |