mirror of
https://github.com/dart-lang/sdk
synced 2024-10-06 00:20:56 +00:00
28c8553cb6
when compiling for ia32 on Mac. R=vegorov@google.com Review URL: https://codereview.chromium.org//415513002 git-svn-id: https://dart.googlecode.com/svn/branches/bleeding_edge/dart@38499 260f80e4-7a28-3924-810f-c04153c831b5
278 lines
6.6 KiB
C++
278 lines
6.6 KiB
C++
// Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
|
|
// for details. All rights reserved. Use of this source code is governed by a
|
|
// BSD-style license that can be found in the LICENSE file.
|
|
|
|
#include "vm/globals.h"
|
|
#if defined(TARGET_OS_MACOS)
|
|
|
|
#include "vm/os.h"
|
|
|
|
#include <errno.h> // NOLINT
|
|
#include <limits.h> // NOLINT
|
|
#include <mach/mach.h> // NOLINT
|
|
#include <mach/clock.h> // NOLINT
|
|
#include <mach/mach_time.h> // NOLINT
|
|
#include <sys/time.h> // NOLINT
|
|
#include <sys/resource.h> // NOLINT
|
|
#include <unistd.h> // NOLINT
|
|
|
|
#include "platform/utils.h"
|
|
#include "vm/isolate.h"
|
|
|
|
namespace dart {
|
|
|
|
const char* OS::Name() {
|
|
return "macos";
|
|
}
|
|
|
|
|
|
intptr_t OS::ProcessId() {
|
|
return static_cast<intptr_t>(getpid());
|
|
}
|
|
|
|
|
|
static bool LocalTime(int64_t seconds_since_epoch, tm* tm_result) {
|
|
time_t seconds = static_cast<time_t>(seconds_since_epoch);
|
|
if (seconds != seconds_since_epoch) return false;
|
|
struct tm* error_code = localtime_r(&seconds, tm_result);
|
|
return error_code != NULL;
|
|
}
|
|
|
|
|
|
const char* OS::GetTimeZoneName(int64_t seconds_since_epoch) {
|
|
tm decomposed;
|
|
bool succeeded = LocalTime(seconds_since_epoch, &decomposed);
|
|
// If unsuccessful, return an empty string like V8 does.
|
|
return (succeeded && (decomposed.tm_zone != NULL)) ? decomposed.tm_zone : "";
|
|
}
|
|
|
|
|
|
int OS::GetTimeZoneOffsetInSeconds(int64_t seconds_since_epoch) {
|
|
tm decomposed;
|
|
bool succeeded = LocalTime(seconds_since_epoch, &decomposed);
|
|
// Even if the offset was 24 hours it would still easily fit into 32 bits.
|
|
// If unsuccessful, return zero like V8 does.
|
|
return succeeded ? static_cast<int>(decomposed.tm_gmtoff) : 0;
|
|
}
|
|
|
|
|
|
int OS::GetLocalTimeZoneAdjustmentInSeconds() {
|
|
// TODO(floitsch): avoid excessive calls to tzset?
|
|
tzset();
|
|
// Even if the offset was 24 hours it would still easily fit into 32 bits.
|
|
// Note that Unix and Dart disagree on the sign.
|
|
return static_cast<int>(-timezone);
|
|
}
|
|
|
|
|
|
int64_t OS::GetCurrentTimeMillis() {
|
|
return GetCurrentTimeMicros() / 1000;
|
|
}
|
|
|
|
|
|
int64_t OS::GetCurrentTimeMicros() {
|
|
// gettimeofday has microsecond resolution.
|
|
struct timeval tv;
|
|
if (gettimeofday(&tv, NULL) < 0) {
|
|
UNREACHABLE();
|
|
return 0;
|
|
}
|
|
return (static_cast<int64_t>(tv.tv_sec) * 1000000) + tv.tv_usec;
|
|
}
|
|
|
|
|
|
void* OS::AlignedAllocate(intptr_t size, intptr_t alignment) {
|
|
const int kMinimumAlignment = 16;
|
|
ASSERT(Utils::IsPowerOfTwo(alignment));
|
|
ASSERT(alignment >= kMinimumAlignment);
|
|
// Temporary workaround until xcode is upgraded.
|
|
// Mac guarantees malloc returns a 16 byte aligned memory chunk.
|
|
// Currently we only allocate with 16-bye alignment.
|
|
ASSERT(alignment == 16);
|
|
// TODO(johnmccutchan): Remove hack and switch to posix_memalign.
|
|
return malloc(size);
|
|
}
|
|
|
|
|
|
void OS::AlignedFree(void* ptr) {
|
|
free(ptr);
|
|
}
|
|
|
|
|
|
intptr_t OS::ActivationFrameAlignment() {
|
|
// OS X activation frames must be 16 byte-aligned; see "Mac OS X ABI
|
|
// Function Call Guide".
|
|
return 16;
|
|
}
|
|
|
|
|
|
intptr_t OS::PreferredCodeAlignment() {
|
|
ASSERT(32 <= OS::kMaxPreferredCodeAlignment);
|
|
return 32;
|
|
}
|
|
|
|
|
|
bool OS::AllowStackFrameIteratorFromAnotherThread() {
|
|
return false;
|
|
}
|
|
|
|
|
|
int OS::NumberOfAvailableProcessors() {
|
|
return sysconf(_SC_NPROCESSORS_ONLN);
|
|
}
|
|
|
|
|
|
void OS::Sleep(int64_t millis) {
|
|
int64_t micros = millis * kMicrosecondsPerMillisecond;
|
|
SleepMicros(micros);
|
|
}
|
|
|
|
|
|
void OS::SleepMicros(int64_t micros) {
|
|
struct timespec req; // requested.
|
|
struct timespec rem; // remainder.
|
|
int64_t seconds = micros / kMicrosecondsPerSecond;
|
|
if (seconds > kMaxInt32) {
|
|
// Avoid truncation of overly large sleep values.
|
|
seconds = kMaxInt32;
|
|
}
|
|
micros = micros - seconds * kMicrosecondsPerSecond;
|
|
int64_t nanos = micros * kNanosecondsPerMicrosecond;
|
|
req.tv_sec = static_cast<int32_t>(seconds);
|
|
req.tv_nsec = static_cast<long>(nanos); // NOLINT (long used in timespec).
|
|
while (true) {
|
|
int r = nanosleep(&req, &rem);
|
|
if (r == 0) {
|
|
break;
|
|
}
|
|
// We should only ever see an interrupt error.
|
|
ASSERT(errno == EINTR);
|
|
// Copy remainder into requested and repeat.
|
|
req = rem;
|
|
}
|
|
}
|
|
|
|
|
|
void OS::DebugBreak() {
|
|
#if defined(HOST_ARCH_X64) || defined(HOST_ARCH_IA32)
|
|
asm("int $3");
|
|
#else
|
|
#error Unsupported architecture.
|
|
#endif
|
|
}
|
|
|
|
|
|
char* OS::StrNDup(const char* s, intptr_t n) {
|
|
// strndup has only been added to Mac OS X in 10.7. We are supplying
|
|
// our own copy here if needed.
|
|
#if !defined(__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__) || \
|
|
__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ <= 1060
|
|
intptr_t len = strlen(s);
|
|
if ((n < 0) || (len < 0)) {
|
|
return NULL;
|
|
}
|
|
if (n < len) {
|
|
len = n;
|
|
}
|
|
char* result = reinterpret_cast<char*>(malloc(len + 1));
|
|
if (result == NULL) {
|
|
return NULL;
|
|
}
|
|
result[len] = '\0';
|
|
return reinterpret_cast<char*>(memmove(result, s, len));
|
|
#else // !defined(__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__) || ...
|
|
return strndup(s, n);
|
|
#endif // !defined(__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__) || ...
|
|
}
|
|
|
|
|
|
void OS::Print(const char* format, ...) {
|
|
va_list args;
|
|
va_start(args, format);
|
|
VFPrint(stdout, format, args);
|
|
va_end(args);
|
|
}
|
|
|
|
|
|
void OS::VFPrint(FILE* stream, const char* format, va_list args) {
|
|
vfprintf(stream, format, args);
|
|
fflush(stream);
|
|
}
|
|
|
|
|
|
int OS::SNPrint(char* str, size_t size, const char* format, ...) {
|
|
va_list args;
|
|
va_start(args, format);
|
|
int retval = VSNPrint(str, size, format, args);
|
|
va_end(args);
|
|
return retval;
|
|
}
|
|
|
|
|
|
int OS::VSNPrint(char* str, size_t size, const char* format, va_list args) {
|
|
int retval = vsnprintf(str, size, format, args);
|
|
if (retval < 0) {
|
|
FATAL1("Fatal error in OS::VSNPrint with format '%s'", format);
|
|
}
|
|
return retval;
|
|
}
|
|
|
|
|
|
bool OS::StringToInt64(const char* str, int64_t* value) {
|
|
ASSERT(str != NULL && strlen(str) > 0 && value != NULL);
|
|
int32_t base = 10;
|
|
char* endptr;
|
|
int i = 0;
|
|
if (str[0] == '-') {
|
|
i = 1;
|
|
}
|
|
if ((str[i] == '0') &&
|
|
(str[i + 1] == 'x' || str[i + 1] == 'X') &&
|
|
(str[i + 2] != '\0')) {
|
|
base = 16;
|
|
}
|
|
errno = 0;
|
|
*value = strtoll(str, &endptr, base);
|
|
return ((errno == 0) && (endptr != str) && (*endptr == 0));
|
|
}
|
|
|
|
|
|
void OS::RegisterCodeObservers() {
|
|
}
|
|
|
|
|
|
void OS::PrintErr(const char* format, ...) {
|
|
va_list args;
|
|
va_start(args, format);
|
|
VFPrint(stderr, format, args);
|
|
va_end(args);
|
|
}
|
|
|
|
|
|
void OS::InitOnce() {
|
|
// TODO(5411554): For now we check that initonce is called only once,
|
|
// Once there is more formal mechanism to call InitOnce we can move
|
|
// this check there.
|
|
static bool init_once_called = false;
|
|
ASSERT(init_once_called == false);
|
|
init_once_called = true;
|
|
}
|
|
|
|
|
|
void OS::Shutdown() {
|
|
}
|
|
|
|
|
|
void OS::Abort() {
|
|
abort();
|
|
}
|
|
|
|
|
|
void OS::Exit(int code) {
|
|
exit(code);
|
|
}
|
|
|
|
} // namespace dart
|
|
|
|
#endif // defined(TARGET_OS_MACOS)
|