Commit 16006814 authored by jochen@chromium.org's avatar jochen@chromium.org

Remove dependency from platform files on v8.h

Pass flag values in through a central initialize call.

We still depend on utils.h for RoundUp and IsAligned.

BUG=none
R=bmeurer@chromium.org
LOG=n

Review URL: https://codereview.chromium.org/353113003

git-svn-id: https://v8.googlecode.com/svn/branches/bleeding_edge@22048 ce2b1a6d-e550-0410-aec6-3dcde31c8c00
parent 092389bb
......@@ -6,6 +6,7 @@
#define V8_BASE_MACROS_H_
#include "include/v8stdint.h"
#include "src/base/build_config.h"
// The expression OFFSET_OF(type, field) computes the byte-offset
......@@ -112,6 +113,52 @@ inline void USE(T) { }
#define IS_POWER_OF_TWO(x) ((x) != 0 && (((x) & ((x) - 1)) == 0))
// Define our own macros for writing 64-bit constants. This is less fragile
// than defining __STDC_CONSTANT_MACROS before including <stdint.h>, and it
// works on compilers that don't have it (like MSVC).
#if V8_CC_MSVC
# define V8_UINT64_C(x) (x ## UI64)
# define V8_INT64_C(x) (x ## I64)
# if V8_HOST_ARCH_64_BIT
# define V8_INTPTR_C(x) (x ## I64)
# define V8_PTR_PREFIX "ll"
# else
# define V8_INTPTR_C(x) (x)
# define V8_PTR_PREFIX ""
# endif // V8_HOST_ARCH_64_BIT
#elif V8_CC_MINGW64
# define V8_UINT64_C(x) (x ## ULL)
# define V8_INT64_C(x) (x ## LL)
# define V8_INTPTR_C(x) (x ## LL)
# define V8_PTR_PREFIX "I64"
#elif V8_HOST_ARCH_64_BIT
# if V8_OS_MACOSX
# define V8_UINT64_C(x) (x ## ULL)
# define V8_INT64_C(x) (x ## LL)
# else
# define V8_UINT64_C(x) (x ## UL)
# define V8_INT64_C(x) (x ## L)
# endif
# define V8_INTPTR_C(x) (x ## L)
# define V8_PTR_PREFIX "l"
#else
# define V8_UINT64_C(x) (x ## ULL)
# define V8_INT64_C(x) (x ## LL)
# define V8_INTPTR_C(x) (x)
# define V8_PTR_PREFIX ""
#endif
#define V8PRIxPTR V8_PTR_PREFIX "x"
#define V8PRIdPTR V8_PTR_PREFIX "d"
#define V8PRIuPTR V8_PTR_PREFIX "u"
// Fix for Mac OS X defining uintptr_t as "unsigned long":
#if V8_OS_MACOSX
#undef V8PRIxPTR
#define V8PRIxPTR "lx"
#endif
// The following macro works on both 32 and 64-bit platforms.
// Usage: instead of writing 0x1234567890123456
// write V8_2PART_UINT64_C(0x12345678,90123456);
......
......@@ -66,51 +66,6 @@ typedef unsigned int __my_bool__;
typedef uint8_t byte;
typedef byte* Address;
// Define our own macros for writing 64-bit constants. This is less fragile
// than defining __STDC_CONSTANT_MACROS before including <stdint.h>, and it
// works on compilers that don't have it (like MSVC).
#if V8_CC_MSVC
# define V8_UINT64_C(x) (x ## UI64)
# define V8_INT64_C(x) (x ## I64)
# if V8_HOST_ARCH_64_BIT
# define V8_INTPTR_C(x) (x ## I64)
# define V8_PTR_PREFIX "ll"
# else
# define V8_INTPTR_C(x) (x)
# define V8_PTR_PREFIX ""
# endif // V8_HOST_ARCH_64_BIT
#elif V8_CC_MINGW64
# define V8_UINT64_C(x) (x ## ULL)
# define V8_INT64_C(x) (x ## LL)
# define V8_INTPTR_C(x) (x ## LL)
# define V8_PTR_PREFIX "I64"
#elif V8_HOST_ARCH_64_BIT
# if V8_OS_MACOSX
# define V8_UINT64_C(x) (x ## ULL)
# define V8_INT64_C(x) (x ## LL)
# else
# define V8_UINT64_C(x) (x ## UL)
# define V8_INT64_C(x) (x ## L)
# endif
# define V8_INTPTR_C(x) (x ## L)
# define V8_PTR_PREFIX "l"
#else
# define V8_UINT64_C(x) (x ## ULL)
# define V8_INT64_C(x) (x ## LL)
# define V8_INTPTR_C(x) (x)
# define V8_PTR_PREFIX ""
#endif
#define V8PRIxPTR V8_PTR_PREFIX "x"
#define V8PRIdPTR V8_PTR_PREFIX "d"
#define V8PRIuPTR V8_PTR_PREFIX "u"
// Fix for Mac OS X defining uintptr_t as "unsigned long":
#if V8_OS_MACOSX
#undef V8PRIxPTR
#define V8PRIxPTR "lx"
#endif
// -----------------------------------------------------------------------------
// Constants
......
......@@ -14,12 +14,13 @@
#include <sys/time.h>
#include <unistd.h> // sysconf
#undef MAP_TYPE
#include <cmath>
#include "src/v8.h"
#undef MAP_TYPE
#include "src/base/win32-headers.h"
#include "src/platform.h"
#include "src/utils.h"
namespace v8 {
namespace internal {
......@@ -237,7 +238,7 @@ VirtualMemory::VirtualMemory(size_t size, size_t alignment)
static_cast<intptr_t>(OS::AllocateAlignment()));
void* address = ReserveRegion(request_size);
if (address == NULL) return;
Address base = RoundUp(static_cast<Address>(address), alignment);
uint8_t* base = RoundUp(static_cast<uint8_t*>(address), alignment);
// Try reducing the size by freeing and then reallocating a specific area.
bool result = ReleaseRegion(address, request_size);
USE(result);
......@@ -245,7 +246,7 @@ VirtualMemory::VirtualMemory(size_t size, size_t alignment)
address = VirtualAlloc(base, size, MEM_RESERVE, PAGE_NOACCESS);
if (address != NULL) {
request_size = size;
ASSERT(base == static_cast<Address>(address));
ASSERT(base == static_cast<uint8_t*>(address));
} else {
// Resizing failed, just go with a bigger area.
address = ReserveRegion(request_size);
......
......@@ -25,11 +25,12 @@
#include <stdarg.h>
#include <strings.h> // index
#undef MAP_TYPE
#include <cmath>
#include "src/v8.h"
#undef MAP_TYPE
#include "src/platform.h"
#include "src/utils.h"
namespace v8 {
......@@ -192,8 +193,8 @@ VirtualMemory::VirtualMemory(size_t size, size_t alignment)
kMmapFdOffset);
if (reservation == MAP_FAILED) return;
Address base = static_cast<Address>(reservation);
Address aligned_base = RoundUp(base, alignment);
uint8_t* base = static_cast<uint8_t*>(reservation);
uint8_t* aligned_base = RoundUp(base, alignment);
ASSERT_LE(base, aligned_base);
// Unmap extra memory reserved before and after the desired block.
......
......@@ -39,11 +39,12 @@
#include <sanitizer/lsan_interface.h>
#endif
#undef MAP_TYPE
#include <cmath>
#include "src/v8.h"
#undef MAP_TYPE
#include "src/platform.h"
#include "src/utils.h"
namespace v8 {
......@@ -254,9 +255,9 @@ void OS::SignalCodeMovingGC() {
// by the kernel and allows us to synchronize V8 code log and the
// kernel log.
int size = sysconf(_SC_PAGESIZE);
FILE* f = fopen(FLAG_gc_fake_mmap, "w+");
FILE* f = fopen(OS::GetGCFakeMMapFile(), "w+");
if (f == NULL) {
OS::PrintError("Failed to open %s\n", FLAG_gc_fake_mmap);
OS::PrintError("Failed to open %s\n", OS::GetGCFakeMMapFile());
OS::Abort();
}
void* addr = mmap(OS::GetRandomMmapAddr(),
......@@ -302,8 +303,8 @@ VirtualMemory::VirtualMemory(size_t size, size_t alignment)
kMmapFdOffset);
if (reservation == MAP_FAILED) return;
Address base = static_cast<Address>(reservation);
Address aligned_base = RoundUp(base, alignment);
uint8_t* base = static_cast<uint8_t*>(reservation);
uint8_t* aligned_base = RoundUp(base, alignment);
ASSERT_LE(base, aligned_base);
// Unmap extra memory reserved before and after the desired block.
......
......@@ -31,11 +31,12 @@
#include <sys/time.h>
#include <sys/types.h>
#undef MAP_TYPE
#include <cmath>
#include "src/v8.h"
#undef MAP_TYPE
#include "src/platform.h"
#include "src/utils.h"
namespace v8 {
......@@ -194,8 +195,8 @@ VirtualMemory::VirtualMemory(size_t size, size_t alignment)
kMmapFdOffset);
if (reservation == MAP_FAILED) return;
Address base = static_cast<Address>(reservation);
Address aligned_base = RoundUp(base, alignment);
uint8_t* base = static_cast<uint8_t*>(reservation);
uint8_t* aligned_base = RoundUp(base, alignment);
ASSERT_LE(base, aligned_base);
// Unmap extra memory reserved before and after the desired block.
......
......@@ -23,11 +23,12 @@
#include <sys/types.h> // mmap & munmap
#include <unistd.h> // sysconf
#undef MAP_TYPE
#include <cmath>
#include "src/v8.h"
#undef MAP_TYPE
#include "src/platform.h"
#include "src/utils.h"
namespace v8 {
......@@ -184,9 +185,9 @@ void OS::SignalCodeMovingGC() {
// by the kernel and allows us to synchronize V8 code log and the
// kernel log.
int size = sysconf(_SC_PAGESIZE);
FILE* f = fopen(FLAG_gc_fake_mmap, "w+");
FILE* f = fopen(OS::GetGCFakeMMapFile(), "w+");
if (f == NULL) {
OS::PrintError("Failed to open %s\n", FLAG_gc_fake_mmap);
OS::PrintError("Failed to open %s\n", OS::GetGCFakeMMapFile());
OS::Abort();
}
void* addr = mmap(NULL, size, PROT_READ | PROT_EXEC, MAP_PRIVATE,
......@@ -223,8 +224,8 @@ VirtualMemory::VirtualMemory(size_t size, size_t alignment)
kMmapFdOffset);
if (reservation == MAP_FAILED) return;
Address base = static_cast<Address>(reservation);
Address aligned_base = RoundUp(base, alignment);
uint8_t* base = static_cast<uint8_t*>(reservation);
uint8_t* aligned_base = RoundUp(base, alignment);
ASSERT_LE(base, aligned_base);
// Unmap extra memory reserved before and after the desired block.
......
......@@ -8,6 +8,7 @@
#include <dlfcn.h>
#include <errno.h>
#include <limits.h>
#include <pthread.h>
#if defined(__DragonFly__) || defined(__FreeBSD__) || defined(__OpenBSD__)
#include <pthread_np.h> // for pthread_set_name_np
......@@ -41,10 +42,13 @@
#include <android/log.h> // NOLINT
#endif
#include "src/v8.h"
#include <cmath>
#include <cstdlib>
#include "src/base/lazy-instance.h"
#include "src/base/macros.h"
#include "src/platform.h"
#include "src/platform/time.h"
#include "src/utils/random-number-generator.h"
#ifdef V8_FAST_TLS_SUPPORTED
......@@ -54,8 +58,16 @@
namespace v8 {
namespace internal {
namespace {
// 0 is never a valid thread id.
static const pthread_t kNoThread = (pthread_t) 0;
const pthread_t kNoThread = (pthread_t) 0;
bool g_hard_abort = false;
const char* g_gc_fake_mmap = NULL;
} // namespace
int OS::NumberOfProcessorsOnline() {
......@@ -191,8 +203,18 @@ static base::LazyInstance<RandomNumberGenerator>::type
platform_random_number_generator = LAZY_INSTANCE_INITIALIZER;
void OS::SetRandomSeed(int64_t seed) {
platform_random_number_generator.Pointer()->SetSeed(seed);
void OS::Initialize(int64_t random_seed, bool hard_abort,
const char* const gc_fake_mmap) {
if (random_seed) {
platform_random_number_generator.Pointer()->SetSeed(random_seed);
}
g_hard_abort = hard_abort;
g_gc_fake_mmap = gc_fake_mmap;
}
const char* OS::GetGCFakeMMapFile() {
return g_gc_fake_mmap;
}
......@@ -253,7 +275,7 @@ void OS::Sleep(int milliseconds) {
void OS::Abort() {
if (FLAG_hard_abort) {
if (g_hard_abort) {
V8_IMMEDIATE_CRASH();
}
// Redirect to std abort to signal abnormal program termination.
......
......@@ -27,11 +27,12 @@
#include <sys/types.h> // mmap & munmap
#include <unistd.h> // sysconf
#undef MAP_TYPE
#include <cmath>
#include "src/v8.h"
#undef MAP_TYPE
#include "src/platform.h"
#include "src/utils.h"
namespace v8 {
......@@ -259,8 +260,8 @@ VirtualMemory::VirtualMemory(size_t size, size_t alignment)
kMmapFdOffset);
if (reservation == MAP_FAILED) return;
Address base = static_cast<Address>(reservation);
Address aligned_base = RoundUp(base, alignment);
uint8_t* base = static_cast<uint8_t*>(reservation);
uint8_t* aligned_base = RoundUp(base, alignment);
ASSERT_LE(base, aligned_base);
// Unmap extra memory reserved before and after the desired block.
......
......@@ -23,12 +23,12 @@
#include <ucontext.h> // walkstack(), getcontext()
#include <unistd.h> // getpagesize(), usleep()
#include <cmath>
#undef MAP_TYPE
#include "src/v8.h"
#include "src/platform.h"
#include "src/utils.h"
// It seems there is a bug in some Solaris distributions (experienced in
......@@ -165,8 +165,8 @@ VirtualMemory::VirtualMemory(size_t size, size_t alignment)
kMmapFdOffset);
if (reservation == MAP_FAILED) return;
Address base = static_cast<Address>(reservation);
Address aligned_base = RoundUp(base, alignment);
uint8_t* base = static_cast<uint8_t*>(reservation);
uint8_t* aligned_base = RoundUp(base, alignment);
ASSERT_LE(base, aligned_base);
// Unmap extra memory reserved before and after the desired block.
......
......@@ -17,10 +17,10 @@
#include "src/base/win32-headers.h"
#include "src/v8.h"
#include "src/base/lazy-instance.h"
#include "src/platform.h"
#include "src/platform/time.h"
#include "src/utils.h"
#include "src/utils/random-number-generator.h"
#ifdef _MSC_VER
......@@ -103,6 +103,12 @@ int strncpy_s(char* dest, size_t dest_size, const char* source, size_t count) {
namespace v8 {
namespace internal {
namespace {
bool g_hard_abort = false;
} // namespace
intptr_t OS::MaxVirtualMemory() {
return 0;
}
......@@ -713,8 +719,12 @@ static base::LazyInstance<RandomNumberGenerator>::type
platform_random_number_generator = LAZY_INSTANCE_INITIALIZER;
void OS::SetRandomSeed(int64_t seed) {
platform_random_number_generator.Pointer()->SetSeed(seed);
void OS::Initialize(int64_t random_seed, bool hard_abort,
const char* const gc_fake_mmap) {
if (random_seed) {
platform_random_number_generator.Pointer()->SetSeed(random_seed);
}
g_hard_abort = hard_abort;
}
......@@ -808,7 +818,7 @@ void OS::Sleep(int milliseconds) {
void OS::Abort() {
if (FLAG_hard_abort) {
if (g_hard_abort) {
V8_IMMEDIATE_CRASH();
}
// Make the MSVCRT do a silent abort.
......@@ -1217,7 +1227,7 @@ VirtualMemory::VirtualMemory(size_t size, size_t alignment)
static_cast<intptr_t>(OS::AllocateAlignment()));
void* address = ReserveRegion(request_size);
if (address == NULL) return;
Address base = RoundUp(static_cast<Address>(address), alignment);
uint8_t* base = RoundUp(static_cast<uint8_t*>(address), alignment);
// Try reducing the size by freeing and then reallocating a specific area.
bool result = ReleaseRegion(address, request_size);
USE(result);
......@@ -1225,7 +1235,7 @@ VirtualMemory::VirtualMemory(size_t size, size_t alignment)
address = VirtualAlloc(base, size, MEM_RESERVE, PAGE_NOACCESS);
if (address != NULL) {
request_size = size;
ASSERT(base == static_cast<Address>(address));
ASSERT(base == static_cast<uint8_t*>(address));
} else {
// Resizing failed, just go with a bigger area.
address = ReserveRegion(request_size);
......
......@@ -141,6 +141,14 @@ class TimezoneCache;
class OS {
public:
// Initialize the OS class.
// - random_seed: Used for the GetRandomMmapAddress() if non-zero.
// - hard_abort: If true, OS::Abort() will crash instead of aborting.
// - gc_fake_mmap: Name of the file for fake gc mmap used in ll_prof.
static void Initialize(int64_t random_seed,
bool hard_abort,
const char* const gc_fake_mmap);
// Returns the accumulated user time for thread. This routine
// can be used for profiling. The implementation should
// strive for high-precision timer resolution, preferable
......@@ -212,10 +220,6 @@ class OS {
// Assign memory as a guard page so that access will cause an exception.
static void Guard(void* address, const size_t size);
// Set a fixed random seed for the random number generator used for
// GetRandomMmapAddr.
static void SetRandomSeed(int64_t seed);
// Generate a random address to be used for hinting mmap().
static void* GetRandomMmapAddr();
......@@ -306,6 +310,10 @@ class OS {
private:
static const int msPerSecond = 1000;
#if V8_OS_POSIX
static const char* GetGCFakeMMapFile();
#endif
DISALLOW_IMPLICIT_CONSTRUCTORS(OS);
};
......
......@@ -90,7 +90,7 @@ void V8::InitializeOncePerProcessImpl() {
FLAG_max_semi_space_size = 1;
}
if (FLAG_random_seed != 0) OS::SetRandomSeed(FLAG_random_seed);
OS::Initialize(FLAG_random_seed, FLAG_hard_abort, FLAG_gc_fake_mmap);
#ifdef V8_USE_DEFAULT_PLATFORM
platform_ = new DefaultPlatform;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment