2012-01-16 12:28:10 +00:00
|
|
|
// Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
|
2011-10-05 05:20:07 +00:00
|
|
|
// 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.
|
|
|
|
|
2012-01-16 12:28:10 +00:00
|
|
|
#include "vm/os.h"
|
|
|
|
|
2011-10-05 05:20:07 +00:00
|
|
|
#include <errno.h>
|
|
|
|
#include <limits.h>
|
|
|
|
#include <mach/mach.h>
|
|
|
|
#include <mach/clock.h>
|
|
|
|
#include <mach/mach_time.h>
|
|
|
|
#include <sys/time.h>
|
|
|
|
#include <sys/resource.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
2012-01-16 13:23:40 +00:00
|
|
|
#include "platform/utils.h"
|
2011-10-05 05:20:07 +00:00
|
|
|
#include "vm/isolate.h"
|
|
|
|
|
|
|
|
namespace dart {
|
|
|
|
|
2012-06-14 13:05:22 +00:00
|
|
|
static bool LocalTime(int64_t seconds_since_epoch, tm* tm_result) {
|
2012-05-03 15:40:40 +00:00
|
|
|
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);
|
2011-10-05 05:20:07 +00:00
|
|
|
return error_code != NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-06-14 13:05:22 +00:00
|
|
|
const char* OS::GetTimeZoneName(int64_t seconds_since_epoch) {
|
|
|
|
tm decomposed;
|
|
|
|
bool succeeded = LocalTime(seconds_since_epoch, &decomposed);
|
|
|
|
ASSERT(succeeded);
|
|
|
|
return decomposed.tm_zone;
|
2011-10-05 05:20:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-06-14 13:05:22 +00:00
|
|
|
int OS::GetTimeZoneOffsetInSeconds(int64_t seconds_since_epoch) {
|
2012-05-21 16:11:20 +00:00
|
|
|
tm decomposed;
|
|
|
|
bool succeeded = LocalTime(seconds_since_epoch, &decomposed);
|
2012-06-14 13:05:22 +00:00
|
|
|
ASSERT(succeeded);
|
|
|
|
// Even if the offset was 24 hours it would still easily fit into 32 bits.
|
|
|
|
return static_cast<int>(decomposed.tm_gmtoff);
|
2012-05-21 16:11:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-06-14 13:05:22 +00:00
|
|
|
int OS::GetLocalTimeZoneAdjustmentInSeconds() {
|
|
|
|
// TODO(floitsch): avoid excessive calls to tzset?
|
|
|
|
tzset();
|
2012-05-21 16:11:20 +00:00
|
|
|
// Even if the offset was 24 hours it would still easily fit into 32 bits.
|
2012-06-14 13:05:22 +00:00
|
|
|
// Note that Unix and Dart disagree on the sign.
|
|
|
|
return static_cast<int>(-timezone);
|
2012-05-21 16:11:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-10-05 05:20:07 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
word OS::ActivationFrameAlignment() {
|
|
|
|
// OS X activation frames must be 16 byte-aligned; see "Mac OS X ABI
|
|
|
|
// Function Call Guide".
|
|
|
|
return 16;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
word OS::PreferredCodeAlignment() {
|
|
|
|
return 32;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
uword OS::GetStackSizeLimit() {
|
|
|
|
struct rlimit stack_limit;
|
|
|
|
int retval = getrlimit(RLIMIT_STACK, &stack_limit);
|
|
|
|
ASSERT(retval == 0);
|
|
|
|
if (stack_limit.rlim_cur > INT_MAX) {
|
|
|
|
retval = INT_MAX;
|
|
|
|
} else {
|
|
|
|
retval = stack_limit.rlim_cur;
|
|
|
|
}
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int OS::NumberOfAvailableProcessors() {
|
|
|
|
return sysconf(_SC_NPROCESSORS_ONLN);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void OS::Sleep(int64_t millis) {
|
|
|
|
// TODO(5411554): For now just use usleep we may have to revisit this.
|
|
|
|
usleep(millis * 1000);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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) {
|
2012-07-11 17:54:24 +00:00
|
|
|
int retval = vsnprintf(str, size, format, args);
|
|
|
|
if (retval < 0) {
|
|
|
|
FATAL1("Fatal error in OS::VSNPrint with format '%s'", format);
|
|
|
|
}
|
|
|
|
return retval;
|
2011-10-05 05:20:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-07-12 19:22:33 +00:00
|
|
|
bool OS::StringToInteger(const char* str, int64_t* value) {
|
|
|
|
ASSERT(str != NULL && strlen(str) > 0 && value != NULL);
|
|
|
|
bool negative_value = false;
|
|
|
|
int32_t base = 10;
|
|
|
|
if (str[0] == '-') {
|
|
|
|
negative_value = true;
|
|
|
|
str += 1;
|
|
|
|
}
|
|
|
|
if ((str[0] == '0') && (str[1] == 'x' || str[1] == 'X') && (str[2] != '\0')) {
|
|
|
|
base = 16;
|
|
|
|
}
|
|
|
|
errno = 0;
|
|
|
|
*value = strtoll(str, NULL, base);
|
|
|
|
if (errno == 0) {
|
|
|
|
if (negative_value) {
|
|
|
|
*value = -(*value);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-10-05 05:20:07 +00:00
|
|
|
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
|