startup-data-util.cc 3.12 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
// 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"
vogelheim's avatar
vogelheim committed
11
#include "src/base/platform/platform.h"
12
#include "src/utils.h"
13 14 15


namespace v8 {
vogelheim's avatar
vogelheim committed
16
namespace internal {
17 18 19

#ifdef V8_USE_EXTERNAL_STARTUP_DATA

vogelheim's avatar
vogelheim committed
20
namespace {
21

vogelheim's avatar
vogelheim committed
22 23
v8::StartupData g_natives;
v8::StartupData g_snapshot;
24 25


vogelheim's avatar
vogelheim committed
26 27 28
void ClearStartupData(v8::StartupData* data) {
  data->data = nullptr;
  data->raw_size = 0;
29 30 31
}


vogelheim's avatar
vogelheim committed
32 33 34
void DeleteStartupData(v8::StartupData* data) {
  delete[] data->data;
  ClearStartupData(data);
35 36 37
}


vogelheim's avatar
vogelheim committed
38 39 40
void FreeStartupData() {
  DeleteStartupData(&g_natives);
  DeleteStartupData(&g_snapshot);
41 42 43
}


vogelheim's avatar
vogelheim committed
44 45 46
void Load(const char* blob_file, v8::StartupData* startup_data,
          void (*setter_fn)(v8::StartupData*)) {
  ClearStartupData(startup_data);
47

48
  CHECK(blob_file);
49 50

  FILE* file = fopen(blob_file, "rb");
51 52 53 54
  if (!file) {
    PrintF(stderr, "Failed to open startup resource '%s'.\n", blob_file);
    return;
  }
55 56

  fseek(file, 0, SEEK_END);
57
  startup_data->raw_size = static_cast<int>(ftell(file));
58 59 60 61 62 63 64
  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);

65 66 67 68 69
  if (startup_data->raw_size == read_size) {
    (*setter_fn)(startup_data);
  } else {
    PrintF(stderr, "Corrupted startup resource '%s'.\n", blob_file);
  }
70 71
}

vogelheim's avatar
vogelheim committed
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101

void LoadFromFiles(const char* natives_blob, const char* snapshot_blob) {
  Load(natives_blob, &g_natives, v8::V8::SetNativesDataBlob);
  Load(snapshot_blob, &g_snapshot, v8::V8::SetSnapshotDataBlob);

  atexit(&FreeStartupData);
}


char* RelativePath(char** buffer, const char* exec_path, const char* name) {
  DCHECK(exec_path);
  int path_separator = static_cast<int>(strlen(exec_path)) - 1;
  while (path_separator >= 0 &&
         !base::OS::isDirectorySeparator(exec_path[path_separator])) {
    path_separator--;
  }
  if (path_separator >= 0) {
    int name_length = static_cast<int>(strlen(name));
    *buffer =
        reinterpret_cast<char*>(calloc(path_separator + name_length + 2, 1));
    *buffer[0] = '\0';
    strncat(*buffer, exec_path, path_separator + 1);
    strncat(*buffer, name, name_length);
  } else {
    *buffer = strdup(name);
  }
  return *buffer;
}

}  // namespace
102 103
#endif  // V8_USE_EXTERNAL_STARTUP_DATA

vogelheim's avatar
vogelheim committed
104 105 106 107 108 109

void InitializeExternalStartupData(const char* directory_path) {
#ifdef V8_USE_EXTERNAL_STARTUP_DATA
  char* natives;
  char* snapshot;
  LoadFromFiles(RelativePath(&natives, directory_path, "natives_blob.bin"),
110
                RelativePath(&snapshot, directory_path, "snapshot_blob.bin"));
vogelheim's avatar
vogelheim committed
111 112 113 114 115 116 117 118 119 120 121 122 123 124
  free(natives);
  free(snapshot);
#endif  // V8_USE_EXTERNAL_STARTUP_DATA
}


void InitializeExternalStartupData(const char* natives_blob,
                                   const char* snapshot_blob) {
#ifdef V8_USE_EXTERNAL_STARTUP_DATA
  LoadFromFiles(natives_blob, snapshot_blob);
#endif  // V8_USE_EXTERNAL_STARTUP_DATA
}

}  // namespace internal
125
}  // namespace v8