Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(210)

Unified Diff: src/startup-data-util.cc

Issue 913703002: Fix cctest + unittest to work with an external snapshot. (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: Addressed comments. Created 5 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « src/startup-data-util.h ('k') | test/cctest/cctest.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: src/startup-data-util.cc
diff --git a/src/startup-data-util.cc b/src/startup-data-util.cc
new file mode 100644
index 0000000000000000000000000000000000000000..2000e3ccca5dda8a38f4d391c437dbcc0de5b3f7
--- /dev/null
+++ b/src/startup-data-util.cc
@@ -0,0 +1,91 @@
+// Copyright 2015 the V8 project authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "src/startup-data-util.h"
+
+#include <stdlib.h>
+#include <string.h>
+
+#include "src/base/logging.h"
+
+
+namespace v8 {
+
+#ifdef V8_USE_EXTERNAL_STARTUP_DATA
+
+StartupDataHandler::StartupDataHandler(const char* exec_path,
+ const char* natives_blob,
+ const char* snapshot_blob) {
+ // If we have (at least one) explicitly given blob, use those.
+ // If not, use the default blob locations next to the d8 binary.
+ if (natives_blob || snapshot_blob) {
+ LoadFromFiles(natives_blob, snapshot_blob);
+ } else {
+ char* natives;
+ char* snapshot;
+ LoadFromFiles(RelativePath(&natives, exec_path, "natives_blob.bin"),
+ RelativePath(&snapshot, exec_path, "snapshot_blob.bin"));
+
+ free(natives);
+ free(snapshot);
+ }
+}
+
+
+StartupDataHandler::~StartupDataHandler() {
+ delete[] natives_.data;
+ delete[] snapshot_.data;
+}
+
+
+char* StartupDataHandler::RelativePath(char** buffer, const char* exec_path,
+ const char* name) {
+ DCHECK(exec_path);
+ const char* last_slash = strrchr(exec_path, '/');
+ if (last_slash) {
+ int after_slash = last_slash - exec_path + 1;
+ int name_length = static_cast<int>(strlen(name));
+ *buffer = reinterpret_cast<char*>(calloc(after_slash + name_length + 1, 1));
+ strncpy(*buffer, exec_path, after_slash);
+ strncat(*buffer, name, name_length);
+ } else {
+ *buffer = strdup(name);
+ }
+ return *buffer;
+}
+
+
+void StartupDataHandler::LoadFromFiles(const char* natives_blob,
+ const char* snapshot_blob) {
+ Load(natives_blob, &natives_, v8::V8::SetNativesDataBlob);
+ Load(snapshot_blob, &snapshot_, v8::V8::SetSnapshotDataBlob);
+}
+
+
+void StartupDataHandler::Load(const char* blob_file,
+ v8::StartupData* startup_data,
+ void (*setter_fn)(v8::StartupData*)) {
+ startup_data->data = NULL;
+ startup_data->raw_size = 0;
+
+ if (!blob_file) return;
+
+ FILE* file = fopen(blob_file, "rb");
+ if (!file) return;
+
+ fseek(file, 0, SEEK_END);
+ startup_data->raw_size = ftell(file);
+ rewind(file);
+
+ startup_data->data = new char[startup_data->raw_size];
+ int read_size = static_cast<int>(fread(const_cast<char*>(startup_data->data),
+ 1, startup_data->raw_size, file));
+ fclose(file);
+
+ if (startup_data->raw_size == read_size) (*setter_fn)(startup_data);
+}
+
+#endif // V8_USE_EXTERNAL_STARTUP_DATA
+
+} // namespace v8
« no previous file with comments | « src/startup-data-util.h ('k') | test/cctest/cctest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698