OLD | NEW |
1 // Copyright 2015 the V8 project authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "src/startup-data-util.h" | 5 #include "src/startup-data-util.h" |
6 | 6 |
7 #include <stdlib.h> | 7 #include <stdlib.h> |
8 #include <string.h> | 8 #include <string.h> |
9 | 9 |
10 #include "src/base/logging.h" | 10 #include "src/base/logging.h" |
| 11 #include "src/base/platform/platform.h" |
11 | 12 |
12 | 13 |
13 namespace v8 { | 14 namespace v8 { |
| 15 namespace internal { |
14 | 16 |
15 #ifdef V8_USE_EXTERNAL_STARTUP_DATA | 17 #ifdef V8_USE_EXTERNAL_STARTUP_DATA |
16 | 18 |
17 StartupDataHandler::StartupDataHandler(const char* exec_path, | 19 namespace { |
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 | 20 |
30 free(natives); | 21 v8::StartupData g_natives; |
31 free(snapshot); | 22 v8::StartupData g_snapshot; |
32 } | 23 |
| 24 |
| 25 void ClearStartupData(v8::StartupData* data) { |
| 26 data->data = nullptr; |
| 27 data->raw_size = 0; |
33 } | 28 } |
34 | 29 |
35 | 30 |
36 StartupDataHandler::~StartupDataHandler() { | 31 void DeleteStartupData(v8::StartupData* data) { |
37 delete[] natives_.data; | 32 delete[] data->data; |
38 delete[] snapshot_.data; | 33 ClearStartupData(data); |
39 } | 34 } |
40 | 35 |
41 | 36 |
42 char* StartupDataHandler::RelativePath(char** buffer, const char* exec_path, | 37 void FreeStartupData() { |
43 const char* name) { | 38 DeleteStartupData(&g_natives); |
44 DCHECK(exec_path); | 39 DeleteStartupData(&g_snapshot); |
45 const char* last_slash = strrchr(exec_path, '/'); | |
46 if (last_slash) { | |
47 int after_slash = static_cast<int>(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 } | 40 } |
57 | 41 |
58 | 42 |
59 void StartupDataHandler::LoadFromFiles(const char* natives_blob, | 43 void Load(const char* blob_file, v8::StartupData* startup_data, |
60 const char* snapshot_blob) { | 44 void (*setter_fn)(v8::StartupData*)) { |
61 Load(natives_blob, &natives_, v8::V8::SetNativesDataBlob); | 45 ClearStartupData(startup_data); |
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 | 46 |
72 if (!blob_file) return; | 47 if (!blob_file) return; |
73 | 48 |
74 FILE* file = fopen(blob_file, "rb"); | 49 FILE* file = fopen(blob_file, "rb"); |
75 if (!file) return; | 50 if (!file) return; |
76 | 51 |
77 fseek(file, 0, SEEK_END); | 52 fseek(file, 0, SEEK_END); |
78 startup_data->raw_size = static_cast<int>(ftell(file)); | 53 startup_data->raw_size = static_cast<int>(ftell(file)); |
79 rewind(file); | 54 rewind(file); |
80 | 55 |
81 startup_data->data = new char[startup_data->raw_size]; | 56 startup_data->data = new char[startup_data->raw_size]; |
82 int read_size = static_cast<int>(fread(const_cast<char*>(startup_data->data), | 57 int read_size = static_cast<int>(fread(const_cast<char*>(startup_data->data), |
83 1, startup_data->raw_size, file)); | 58 1, startup_data->raw_size, file)); |
84 fclose(file); | 59 fclose(file); |
85 | 60 |
86 if (startup_data->raw_size == read_size) (*setter_fn)(startup_data); | 61 if (startup_data->raw_size == read_size) (*setter_fn)(startup_data); |
87 } | 62 } |
88 | 63 |
| 64 |
| 65 void LoadFromFiles(const char* natives_blob, const char* snapshot_blob) { |
| 66 Load(natives_blob, &g_natives, v8::V8::SetNativesDataBlob); |
| 67 Load(snapshot_blob, &g_snapshot, v8::V8::SetSnapshotDataBlob); |
| 68 |
| 69 atexit(&FreeStartupData); |
| 70 } |
| 71 |
| 72 |
| 73 char* RelativePath(char** buffer, const char* exec_path, const char* name) { |
| 74 DCHECK(exec_path); |
| 75 int path_separator = static_cast<int>(strlen(exec_path)) - 1; |
| 76 while (path_separator >= 0 && |
| 77 !base::OS::isDirectorySeparator(exec_path[path_separator])) { |
| 78 path_separator--; |
| 79 } |
| 80 if (path_separator >= 0) { |
| 81 int name_length = static_cast<int>(strlen(name)); |
| 82 *buffer = |
| 83 reinterpret_cast<char*>(calloc(path_separator + name_length + 2, 1)); |
| 84 *buffer[0] = '\0'; |
| 85 strncat(*buffer, exec_path, path_separator + 1); |
| 86 strncat(*buffer, name, name_length); |
| 87 } else { |
| 88 *buffer = strdup(name); |
| 89 } |
| 90 return *buffer; |
| 91 } |
| 92 |
| 93 } // namespace |
89 #endif // V8_USE_EXTERNAL_STARTUP_DATA | 94 #endif // V8_USE_EXTERNAL_STARTUP_DATA |
90 | 95 |
| 96 |
| 97 void InitializeExternalStartupData(const char* directory_path) { |
| 98 #ifdef V8_USE_EXTERNAL_STARTUP_DATA |
| 99 char* natives; |
| 100 char* snapshot; |
| 101 LoadFromFiles(RelativePath(&natives, directory_path, "natives_blob.bin"), |
| 102 RelativePath(&snapshot, directory_path, "snapshot_blob.bin")); |
| 103 free(natives); |
| 104 free(snapshot); |
| 105 #endif // V8_USE_EXTERNAL_STARTUP_DATA |
| 106 } |
| 107 |
| 108 |
| 109 void InitializeExternalStartupData(const char* natives_blob, |
| 110 const char* snapshot_blob) { |
| 111 #ifdef V8_USE_EXTERNAL_STARTUP_DATA |
| 112 LoadFromFiles(natives_blob, snapshot_blob); |
| 113 #endif // V8_USE_EXTERNAL_STARTUP_DATA |
| 114 } |
| 115 |
| 116 } // namespace internal |
91 } // namespace v8 | 117 } // namespace v8 |
OLD | NEW |