OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 the V8 project authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "src/startup-data-util.h" |
| 6 |
| 7 #include <stdlib.h> |
| 8 #include <string.h> |
| 9 |
| 10 #include "src/base/logging.h" |
| 11 |
| 12 |
| 13 namespace v8 { |
| 14 |
| 15 #ifdef V8_USE_EXTERNAL_STARTUP_DATA |
| 16 |
| 17 StartupDataHandler::StartupDataHandler(const char* exec_path, |
| 18 const char* natives_blob, |
| 19 const char* snapshot_blob) { |
| 20 // If we have (at least one) explicitly given blob, use those. |
| 21 // If not, use the default blob locations next to the d8 binary. |
| 22 if (natives_blob || snapshot_blob) { |
| 23 LoadFromFiles(natives_blob, snapshot_blob); |
| 24 } else { |
| 25 char* natives; |
| 26 char* snapshot; |
| 27 LoadFromFiles(RelativePath(&natives, exec_path, "natives_blob.bin"), |
| 28 RelativePath(&snapshot, exec_path, "snapshot_blob.bin")); |
| 29 |
| 30 free(natives); |
| 31 free(snapshot); |
| 32 } |
| 33 } |
| 34 |
| 35 |
| 36 StartupDataHandler::~StartupDataHandler() { |
| 37 delete[] natives_.data; |
| 38 delete[] snapshot_.data; |
| 39 } |
| 40 |
| 41 |
| 42 char* StartupDataHandler::RelativePath(char** buffer, const char* exec_path, |
| 43 const char* name) { |
| 44 DCHECK(exec_path); |
| 45 const char* last_slash = strrchr(exec_path, '/'); |
| 46 if (last_slash) { |
| 47 int after_slash = last_slash - exec_path + 1; |
| 48 int name_length = static_cast<int>(strlen(name)); |
| 49 *buffer = reinterpret_cast<char*>(calloc(after_slash + name_length + 1, 1)); |
| 50 strncpy(*buffer, exec_path, after_slash); |
| 51 strncat(*buffer, name, name_length); |
| 52 } else { |
| 53 *buffer = strdup(name); |
| 54 } |
| 55 return *buffer; |
| 56 } |
| 57 |
| 58 |
| 59 void StartupDataHandler::LoadFromFiles(const char* natives_blob, |
| 60 const char* snapshot_blob) { |
| 61 Load(natives_blob, &natives_, v8::V8::SetNativesDataBlob); |
| 62 Load(snapshot_blob, &snapshot_, v8::V8::SetSnapshotDataBlob); |
| 63 } |
| 64 |
| 65 |
| 66 void StartupDataHandler::Load(const char* blob_file, |
| 67 v8::StartupData* startup_data, |
| 68 void (*setter_fn)(v8::StartupData*)) { |
| 69 startup_data->data = NULL; |
| 70 startup_data->raw_size = 0; |
| 71 |
| 72 if (!blob_file) return; |
| 73 |
| 74 FILE* file = fopen(blob_file, "rb"); |
| 75 if (!file) return; |
| 76 |
| 77 fseek(file, 0, SEEK_END); |
| 78 startup_data->raw_size = ftell(file); |
| 79 rewind(file); |
| 80 |
| 81 startup_data->data = new char[startup_data->raw_size]; |
| 82 int read_size = static_cast<int>(fread(const_cast<char*>(startup_data->data), |
| 83 1, startup_data->raw_size, file)); |
| 84 fclose(file); |
| 85 |
| 86 if (startup_data->raw_size == read_size) (*setter_fn)(startup_data); |
| 87 } |
| 88 |
| 89 #endif // V8_USE_EXTERNAL_STARTUP_DATA |
| 90 |
| 91 } // namespace v8 |
OLD | NEW |