Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions src/jni/File.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@
#include "File.h"
#include <sstream>
#include <fstream>
#include <sys/mman.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <assert.h>

using namespace std;

Expand Down Expand Up @@ -98,6 +103,41 @@ namespace tns
return Buffer;
}

MemoryMappedFile MemoryMappedFile::Open(const char* filePath)
{
void* memory = nullptr;
int length = 0;
if (FILE* file = fopen(filePath, "r+"))
{
if (fseek(file, 0, SEEK_END) == 0)
{
length = ftell(file);
if (length >= 0)
{
memory = mmap(NULL, length, PROT_READ, MAP_SHARED, fileno(file), 0);
if (memory == MAP_FAILED)
{
memory = nullptr;
}
}
}
fclose(file);
}
return MemoryMappedFile(memory, length);
}

MemoryMappedFile::MemoryMappedFile(void* memory, size_t size)
:
memory(memory), size(size)
{
}

MemoryMappedFile::~MemoryMappedFile()
{
int result = munmap(this->memory, this->size);
assert(result == 0);
}

char* File::Buffer = new char[BUFFER_SIZE];

const char* File::WRITE_BINARY = "wb";
Expand Down
10 changes: 10 additions & 0 deletions src/jni/File.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,16 @@

namespace tns
{
struct MemoryMappedFile final
{
static MemoryMappedFile Open(const char* filePath);
MemoryMappedFile(void* memory, size_t size);
~MemoryMappedFile();

void* memory = nullptr;
size_t size = 0;
};

class File
{
public:
Expand Down
16 changes: 6 additions & 10 deletions src/jni/Runtime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
#include "JsDebugger.h"
#include "SimpleProfiler.h"
#include "SimpleAllocator.h"
#include "File.h"
#include "JType.h"
#include "Module.h"
#include "NativeScriptException.h"
Expand Down Expand Up @@ -58,7 +57,6 @@ Runtime::Runtime(JNIEnv *env, jobject runtime, int id)
{
m_runtime = m_env.NewGlobalRef(runtime);
m_objectManager = new ObjectManager(m_runtime);
m_startupData = nullptr;
s_id2RuntimeCache.insert(make_pair(id, this));
}

Expand Down Expand Up @@ -339,10 +337,8 @@ void Runtime::PassUncaughtExceptionToJsNative(JNIEnv *env, jobject obj, jthrowab
}

void Runtime::ClearStartupData(JNIEnv *env, jobject obj) {
if (m_startupData) {
delete m_startupData->data;
delete m_startupData;
}
delete m_heapSnapshotBlob;
delete m_startupData;
}

Isolate* Runtime::PrepareV8Runtime(const string& filesPath, jstring packageName, jobject jsDebugger, jstring profilerOutputDir)
Expand Down Expand Up @@ -377,11 +373,11 @@ Isolate* Runtime::PrepareV8Runtime(const string& filesPath, jstring packageName,

if (File::Exists(snapshotPath))
{
int length;
m_startupData->data = reinterpret_cast<char*>(File::ReadBinary(snapshotPath, length));
m_startupData->raw_size = length;
m_heapSnapshotBlob = new MemoryMappedFile(MemoryMappedFile::Open(snapshotPath.c_str()));
m_startupData->data = static_cast<const char*>(m_heapSnapshotBlob->memory);
m_startupData->raw_size = m_heapSnapshotBlob->size;

DEBUG_WRITE_FORCE("Snapshot read %s (%dB).", snapshotPath.c_str(), length);
DEBUG_WRITE_FORCE("Snapshot read %s (%dB).", snapshotPath.c_str(), m_heapSnapshotBlob->size);
}
else if(saveSnapshot)
{
Expand Down
4 changes: 3 additions & 1 deletion src/jni/Runtime.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "WeakRef.h"
#include "ArrayBufferHelper.h"
#include "Profiler.h"
#include "File.h"

jobject ConvertJsValueToJavaObject(tns::JEnv& env, const v8::Local<v8::Value>& value, int classReturnType);

Expand Down Expand Up @@ -58,7 +59,8 @@ namespace tns

Profiler m_profiler;

v8::StartupData *m_startupData;
v8::StartupData *m_startupData = nullptr;
MemoryMappedFile *m_heapSnapshotBlob = nullptr;

static void PrepareExtendFunction(v8::Isolate *isolate, jstring filesPath);
v8::Isolate* PrepareV8Runtime(const std::string& filesPath, jstring packageName, jobject jsDebugger, jstring profilerOutputDir);
Expand Down