2011-03-30 14:04:26 +00:00
|
|
|
// Copyright 2011 the V8 project authors. All rights reserved.
|
2009-03-24 06:32:00 +00:00
|
|
|
// Redistribution and use in source and binary forms, with or without
|
|
|
|
// modification, are permitted provided that the following conditions are
|
|
|
|
// met:
|
|
|
|
//
|
|
|
|
// * Redistributions of source code must retain the above copyright
|
|
|
|
// notice, this list of conditions and the following disclaimer.
|
|
|
|
// * Redistributions in binary form must reproduce the above
|
|
|
|
// copyright notice, this list of conditions and the following
|
|
|
|
// disclaimer in the documentation and/or other materials provided
|
|
|
|
// with the distribution.
|
|
|
|
// * Neither the name of Google Inc. nor the names of its
|
|
|
|
// contributors may be used to endorse or promote products derived
|
|
|
|
// from this software without specific prior written permission.
|
|
|
|
//
|
|
|
|
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
|
|
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
|
|
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
|
|
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
|
|
|
// Platform specific code for POSIX goes here. This is not a platform on its
|
|
|
|
// own but contains the parts which are the same across POSIX platforms Linux,
|
2009-12-02 13:38:50 +00:00
|
|
|
// Mac OS, FreeBSD and OpenBSD.
|
2009-03-24 06:32:00 +00:00
|
|
|
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <errno.h>
|
2009-03-26 09:28:27 +00:00
|
|
|
#include <time.h>
|
2009-03-24 06:32:00 +00:00
|
|
|
|
2011-07-20 08:06:43 +00:00
|
|
|
#include <sys/mman.h>
|
2009-03-24 06:32:00 +00:00
|
|
|
#include <sys/socket.h>
|
2009-03-26 09:43:14 +00:00
|
|
|
#include <sys/resource.h>
|
|
|
|
#include <sys/time.h>
|
2009-03-24 06:32:00 +00:00
|
|
|
#include <sys/types.h>
|
2011-07-11 15:30:24 +00:00
|
|
|
#include <sys/stat.h>
|
2009-03-24 06:32:00 +00:00
|
|
|
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <netdb.h>
|
|
|
|
|
2011-07-20 08:06:43 +00:00
|
|
|
#undef MAP_TYPE
|
|
|
|
|
2009-07-13 21:22:50 +00:00
|
|
|
#if defined(ANDROID)
|
|
|
|
#define LOG_TAG "v8"
|
|
|
|
#include <utils/Log.h> // LOG_PRI_VA
|
|
|
|
#endif
|
|
|
|
|
2009-03-24 06:32:00 +00:00
|
|
|
#include "v8.h"
|
|
|
|
|
|
|
|
#include "platform.h"
|
|
|
|
|
2009-05-25 10:05:56 +00:00
|
|
|
namespace v8 {
|
|
|
|
namespace internal {
|
2009-03-24 06:32:00 +00:00
|
|
|
|
2011-06-22 10:13:10 +00:00
|
|
|
|
|
|
|
// Maximum size of the virtual memory. 0 means there is no artificial
|
|
|
|
// limit.
|
|
|
|
|
|
|
|
intptr_t OS::MaxVirtualMemory() {
|
|
|
|
struct rlimit limit;
|
|
|
|
int result = getrlimit(RLIMIT_DATA, &limit);
|
|
|
|
if (result != 0) return 0;
|
|
|
|
return limit.rlim_cur;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-08-02 13:34:52 +00:00
|
|
|
// Get rid of writable permission on code allocations.
|
|
|
|
void OS::ProtectCode(void* address, const size_t size) {
|
|
|
|
mprotect(address, size, PROT_READ | PROT_EXEC);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-07-20 08:06:43 +00:00
|
|
|
// Create guard pages.
|
|
|
|
void OS::Guard(void* address, const size_t size) {
|
|
|
|
mprotect(address, size, PROT_NONE);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-10-23 09:18:19 +00:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// Math functions
|
|
|
|
|
|
|
|
double modulo(double x, double y) {
|
|
|
|
return fmod(x, y);
|
|
|
|
}
|
2009-03-24 06:32:00 +00:00
|
|
|
|
2009-12-03 09:29:21 +00:00
|
|
|
|
|
|
|
double OS::nan_value() {
|
|
|
|
// NAN from math.h is defined in C99 and not in POSIX.
|
|
|
|
return NAN;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-03-26 09:28:27 +00:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// POSIX date/time support.
|
|
|
|
//
|
|
|
|
|
2009-03-26 09:43:14 +00:00
|
|
|
int OS::GetUserTime(uint32_t* secs, uint32_t* usecs) {
|
|
|
|
struct rusage usage;
|
|
|
|
|
|
|
|
if (getrusage(RUSAGE_SELF, &usage) < 0) return -1;
|
|
|
|
*secs = usage.ru_utime.tv_sec;
|
|
|
|
*usecs = usage.ru_utime.tv_usec;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
double OS::TimeCurrentMillis() {
|
|
|
|
struct timeval tv;
|
|
|
|
if (gettimeofday(&tv, NULL) < 0) return 0.0;
|
|
|
|
return (static_cast<double>(tv.tv_sec) * 1000) +
|
|
|
|
(static_cast<double>(tv.tv_usec) / 1000);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int64_t OS::Ticks() {
|
|
|
|
// gettimeofday has microsecond resolution.
|
|
|
|
struct timeval tv;
|
|
|
|
if (gettimeofday(&tv, NULL) < 0)
|
|
|
|
return 0;
|
|
|
|
return (static_cast<int64_t>(tv.tv_sec) * 1000000) + tv.tv_usec;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
double OS::DaylightSavingsOffset(double time) {
|
2009-08-04 09:41:18 +00:00
|
|
|
if (isnan(time)) return nan_value();
|
2009-03-26 09:43:14 +00:00
|
|
|
time_t tv = static_cast<time_t>(floor(time/msPerSecond));
|
|
|
|
struct tm* t = localtime(&tv);
|
2009-07-31 13:17:59 +00:00
|
|
|
if (NULL == t) return nan_value();
|
2009-03-26 09:43:14 +00:00
|
|
|
return t->tm_isdst > 0 ? 3600 * msPerSecond : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-08-13 11:11:36 +00:00
|
|
|
int OS::GetLastError() {
|
|
|
|
return errno;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-03-27 08:38:02 +00:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// POSIX stdio support.
|
|
|
|
//
|
|
|
|
|
|
|
|
FILE* OS::FOpen(const char* path, const char* mode) {
|
2011-07-11 15:30:24 +00:00
|
|
|
FILE* file = fopen(path, mode);
|
|
|
|
if (file == NULL) return NULL;
|
|
|
|
struct stat file_stat;
|
|
|
|
if (fstat(fileno(file), &file_stat) != 0) return NULL;
|
|
|
|
bool is_regular_file = ((file_stat.st_mode & S_IFREG) != 0);
|
|
|
|
if (is_regular_file) return file;
|
|
|
|
fclose(file);
|
|
|
|
return NULL;
|
2009-03-27 08:38:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-01-19 14:28:15 +00:00
|
|
|
bool OS::Remove(const char* path) {
|
|
|
|
return (remove(path) == 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-07-13 11:31:22 +00:00
|
|
|
FILE* OS::OpenTemporaryFile() {
|
|
|
|
return tmpfile();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-03-18 20:35:07 +00:00
|
|
|
const char* const OS::LogFileOpenMode = "w";
|
2009-03-31 09:06:37 +00:00
|
|
|
|
|
|
|
|
2009-03-27 08:38:02 +00:00
|
|
|
void OS::Print(const char* format, ...) {
|
|
|
|
va_list args;
|
|
|
|
va_start(args, format);
|
|
|
|
VPrint(format, args);
|
|
|
|
va_end(args);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void OS::VPrint(const char* format, va_list args) {
|
2011-03-17 07:34:37 +00:00
|
|
|
#if defined(ANDROID) && !defined(V8_ANDROID_LOG_STDOUT)
|
2009-07-13 21:22:50 +00:00
|
|
|
LOG_PRI_VA(ANDROID_LOG_INFO, LOG_TAG, format, args);
|
|
|
|
#else
|
2009-03-27 08:38:02 +00:00
|
|
|
vprintf(format, args);
|
2009-07-13 21:22:50 +00:00
|
|
|
#endif
|
2009-03-27 08:38:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-12-20 10:38:19 +00:00
|
|
|
void OS::FPrint(FILE* out, const char* format, ...) {
|
|
|
|
va_list args;
|
|
|
|
va_start(args, format);
|
|
|
|
VFPrint(out, format, args);
|
|
|
|
va_end(args);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void OS::VFPrint(FILE* out, const char* format, va_list args) {
|
2011-03-17 07:34:37 +00:00
|
|
|
#if defined(ANDROID) && !defined(V8_ANDROID_LOG_STDOUT)
|
2010-12-20 10:38:19 +00:00
|
|
|
LOG_PRI_VA(ANDROID_LOG_INFO, LOG_TAG, format, args);
|
|
|
|
#else
|
|
|
|
vfprintf(out, format, args);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-03-27 08:38:02 +00:00
|
|
|
void OS::PrintError(const char* format, ...) {
|
|
|
|
va_list args;
|
|
|
|
va_start(args, format);
|
|
|
|
VPrintError(format, args);
|
|
|
|
va_end(args);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void OS::VPrintError(const char* format, va_list args) {
|
2011-03-17 07:34:37 +00:00
|
|
|
#if defined(ANDROID) && !defined(V8_ANDROID_LOG_STDOUT)
|
2009-07-13 21:22:50 +00:00
|
|
|
LOG_PRI_VA(ANDROID_LOG_ERROR, LOG_TAG, format, args);
|
|
|
|
#else
|
2009-03-27 08:38:02 +00:00
|
|
|
vfprintf(stderr, format, args);
|
2009-07-13 21:22:50 +00:00
|
|
|
#endif
|
2009-03-27 08:38:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int OS::SNPrintF(Vector<char> str, const char* format, ...) {
|
|
|
|
va_list args;
|
|
|
|
va_start(args, format);
|
|
|
|
int result = VSNPrintF(str, format, args);
|
|
|
|
va_end(args);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int OS::VSNPrintF(Vector<char> str,
|
|
|
|
const char* format,
|
|
|
|
va_list args) {
|
|
|
|
int n = vsnprintf(str.start(), str.length(), format, args);
|
|
|
|
if (n < 0 || n >= str.length()) {
|
2010-12-20 10:38:19 +00:00
|
|
|
// If the length is zero, the assignment fails.
|
|
|
|
if (str.length() > 0)
|
|
|
|
str[str.length() - 1] = '\0';
|
2009-03-27 08:38:02 +00:00
|
|
|
return -1;
|
|
|
|
} else {
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-03-30 14:04:26 +00:00
|
|
|
#if defined(V8_TARGET_ARCH_IA32)
|
|
|
|
static OS::MemCopyFunction memcopy_function = NULL;
|
|
|
|
static Mutex* memcopy_function_mutex = OS::CreateMutex();
|
|
|
|
// Defined in codegen-ia32.cc.
|
|
|
|
OS::MemCopyFunction CreateMemCopyFunction();
|
|
|
|
|
|
|
|
// Copy memory area to disjoint memory area.
|
|
|
|
void OS::MemCopy(void* dest, const void* src, size_t size) {
|
|
|
|
if (memcopy_function == NULL) {
|
|
|
|
ScopedLock lock(memcopy_function_mutex);
|
|
|
|
if (memcopy_function == NULL) {
|
2011-03-30 14:31:32 +00:00
|
|
|
OS::MemCopyFunction temp = CreateMemCopyFunction();
|
|
|
|
MemoryBarrier();
|
|
|
|
memcopy_function = temp;
|
2011-03-30 14:04:26 +00:00
|
|
|
}
|
|
|
|
}
|
2011-04-01 14:46:30 +00:00
|
|
|
// Note: here we rely on dependent reads being ordered. This is true
|
|
|
|
// on all architectures we currently support.
|
2011-03-30 14:04:26 +00:00
|
|
|
(*memcopy_function)(dest, src, size);
|
|
|
|
#ifdef DEBUG
|
|
|
|
CHECK_EQ(0, memcmp(dest, src, size));
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
#endif // V8_TARGET_ARCH_IA32
|
|
|
|
|
2009-03-27 08:38:02 +00:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// POSIX string support.
|
|
|
|
//
|
|
|
|
|
|
|
|
char* OS::StrChr(char* str, int c) {
|
|
|
|
return strchr(str, c);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void OS::StrNCpy(Vector<char> dest, const char* src, size_t n) {
|
|
|
|
strncpy(dest.start(), src, n);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-03-24 06:32:00 +00:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// POSIX socket support.
|
|
|
|
//
|
|
|
|
|
|
|
|
class POSIXSocket : public Socket {
|
|
|
|
public:
|
|
|
|
explicit POSIXSocket() {
|
|
|
|
// Create the socket.
|
|
|
|
socket_ = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
|
2010-12-17 12:31:42 +00:00
|
|
|
if (IsValid()) {
|
|
|
|
// Allow rapid reuse.
|
|
|
|
static const int kOn = 1;
|
|
|
|
int ret = setsockopt(socket_, SOL_SOCKET, SO_REUSEADDR,
|
|
|
|
&kOn, sizeof(kOn));
|
|
|
|
ASSERT(ret == 0);
|
|
|
|
USE(ret);
|
|
|
|
}
|
2009-03-24 06:32:00 +00:00
|
|
|
}
|
|
|
|
explicit POSIXSocket(int socket): socket_(socket) { }
|
|
|
|
virtual ~POSIXSocket() { Shutdown(); }
|
|
|
|
|
|
|
|
// Server initialization.
|
|
|
|
bool Bind(const int port);
|
|
|
|
bool Listen(int backlog) const;
|
|
|
|
Socket* Accept() const;
|
|
|
|
|
|
|
|
// Client initialization.
|
|
|
|
bool Connect(const char* host, const char* port);
|
|
|
|
|
|
|
|
// Shutdown socket for both read and write.
|
|
|
|
bool Shutdown();
|
|
|
|
|
|
|
|
// Data Transimission
|
|
|
|
int Send(const char* data, int len) const;
|
|
|
|
int Receive(char* data, int len) const;
|
|
|
|
|
|
|
|
bool SetReuseAddress(bool reuse_address);
|
|
|
|
|
|
|
|
bool IsValid() const { return socket_ != -1; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
int socket_;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
bool POSIXSocket::Bind(const int port) {
|
|
|
|
if (!IsValid()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
sockaddr_in addr;
|
|
|
|
memset(&addr, 0, sizeof(addr));
|
|
|
|
addr.sin_family = AF_INET;
|
|
|
|
addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
|
|
|
|
addr.sin_port = htons(port);
|
|
|
|
int status = bind(socket_,
|
2010-08-12 11:43:10 +00:00
|
|
|
BitCast<struct sockaddr *>(&addr),
|
2009-03-24 06:32:00 +00:00
|
|
|
sizeof(addr));
|
|
|
|
return status == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool POSIXSocket::Listen(int backlog) const {
|
|
|
|
if (!IsValid()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
int status = listen(socket_, backlog);
|
|
|
|
return status == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Socket* POSIXSocket::Accept() const {
|
|
|
|
if (!IsValid()) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
int socket = accept(socket_, NULL, NULL);
|
|
|
|
if (socket == -1) {
|
|
|
|
return NULL;
|
|
|
|
} else {
|
|
|
|
return new POSIXSocket(socket);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool POSIXSocket::Connect(const char* host, const char* port) {
|
|
|
|
if (!IsValid()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Lookup host and port.
|
|
|
|
struct addrinfo *result = NULL;
|
|
|
|
struct addrinfo hints;
|
|
|
|
memset(&hints, 0, sizeof(addrinfo));
|
|
|
|
hints.ai_family = AF_INET;
|
|
|
|
hints.ai_socktype = SOCK_STREAM;
|
|
|
|
hints.ai_protocol = IPPROTO_TCP;
|
|
|
|
int status = getaddrinfo(host, port, &hints, &result);
|
|
|
|
if (status != 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Connect.
|
|
|
|
status = connect(socket_, result->ai_addr, result->ai_addrlen);
|
|
|
|
freeaddrinfo(result);
|
|
|
|
return status == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool POSIXSocket::Shutdown() {
|
|
|
|
if (IsValid()) {
|
|
|
|
// Shutdown socket for both read and write.
|
|
|
|
int status = shutdown(socket_, SHUT_RDWR);
|
|
|
|
close(socket_);
|
|
|
|
socket_ = -1;
|
|
|
|
return status == 0;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int POSIXSocket::Send(const char* data, int len) const {
|
|
|
|
int status = send(socket_, data, len, 0);
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int POSIXSocket::Receive(char* data, int len) const {
|
|
|
|
int status = recv(socket_, data, len, 0);
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool POSIXSocket::SetReuseAddress(bool reuse_address) {
|
|
|
|
int on = reuse_address ? 1 : 0;
|
|
|
|
int status = setsockopt(socket_, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
|
|
|
|
return status == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool Socket::Setup() {
|
|
|
|
// Nothing to do on POSIX.
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int Socket::LastError() {
|
|
|
|
return errno;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
uint16_t Socket::HToN(uint16_t value) {
|
|
|
|
return htons(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
uint16_t Socket::NToH(uint16_t value) {
|
|
|
|
return ntohs(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
uint32_t Socket::HToN(uint32_t value) {
|
|
|
|
return htonl(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
uint32_t Socket::NToH(uint32_t value) {
|
|
|
|
return ntohl(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Socket* OS::CreateSocket() {
|
|
|
|
return new POSIXSocket();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
} } // namespace v8::internal
|