v8  3.14.5(node0.10.28)
V8 is Google's open source JavaScript engine
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
snapshot-common.cc
Go to the documentation of this file.
1 // Copyright 2006-2008 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are
4 // met:
5 //
6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above
9 // copyright notice, this list of conditions and the following
10 // disclaimer in the documentation and/or other materials provided
11 // with the distribution.
12 // * Neither the name of Google Inc. nor the names of its
13 // contributors may be used to endorse or promote products derived
14 // from this software without specific prior written permission.
15 //
16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 
28 // The common functionality when building with or without snapshots.
29 
30 #include "v8.h"
31 
32 #include "api.h"
33 #include "serialize.h"
34 #include "snapshot.h"
35 #include "platform.h"
36 
37 namespace v8 {
38 namespace internal {
39 
40 
41 static void ReserveSpaceForSnapshot(Deserializer* deserializer,
42  const char* file_name) {
43  int file_name_length = StrLength(file_name) + 10;
44  Vector<char> name = Vector<char>::New(file_name_length + 1);
45  OS::SNPrintF(name, "%s.size", file_name);
46  FILE* fp = OS::FOpen(name.start(), "r");
47  CHECK_NE(NULL, fp);
48  int new_size, pointer_size, data_size, code_size, map_size, cell_size;
49 #ifdef _MSC_VER
50  // Avoid warning about unsafe fscanf from MSVC.
51  // Please note that this is only fine if %c and %s are not being used.
52 #define fscanf fscanf_s
53 #endif
54  CHECK_EQ(1, fscanf(fp, "new %d\n", &new_size));
55  CHECK_EQ(1, fscanf(fp, "pointer %d\n", &pointer_size));
56  CHECK_EQ(1, fscanf(fp, "data %d\n", &data_size));
57  CHECK_EQ(1, fscanf(fp, "code %d\n", &code_size));
58  CHECK_EQ(1, fscanf(fp, "map %d\n", &map_size));
59  CHECK_EQ(1, fscanf(fp, "cell %d\n", &cell_size));
60 #ifdef _MSC_VER
61 #undef fscanf
62 #endif
63  fclose(fp);
64  deserializer->set_reservation(NEW_SPACE, new_size);
65  deserializer->set_reservation(OLD_POINTER_SPACE, pointer_size);
66  deserializer->set_reservation(OLD_DATA_SPACE, data_size);
67  deserializer->set_reservation(CODE_SPACE, code_size);
68  deserializer->set_reservation(MAP_SPACE, map_size);
69  deserializer->set_reservation(CELL_SPACE, cell_size);
70  name.Dispose();
71 }
72 
73 
74 void Snapshot::ReserveSpaceForLinkedInSnapshot(Deserializer* deserializer) {
75  deserializer->set_reservation(NEW_SPACE, new_space_used_);
76  deserializer->set_reservation(OLD_POINTER_SPACE, pointer_space_used_);
77  deserializer->set_reservation(OLD_DATA_SPACE, data_space_used_);
78  deserializer->set_reservation(CODE_SPACE, code_space_used_);
79  deserializer->set_reservation(MAP_SPACE, map_space_used_);
80  deserializer->set_reservation(CELL_SPACE, cell_space_used_);
81 }
82 
83 
84 bool Snapshot::Initialize(const char* snapshot_file) {
85  if (snapshot_file) {
86  int len;
87  byte* str = ReadBytes(snapshot_file, &len);
88  if (!str) return false;
89  bool success;
90  {
91  SnapshotByteSource source(str, len);
92  Deserializer deserializer(&source);
93  ReserveSpaceForSnapshot(&deserializer, snapshot_file);
94  success = V8::Initialize(&deserializer);
95  }
96  DeleteArray(str);
97  return success;
98  } else if (size_ > 0) {
99  SnapshotByteSource source(raw_data_, raw_size_);
100  Deserializer deserializer(&source);
101  ReserveSpaceForLinkedInSnapshot(&deserializer);
102  return V8::Initialize(&deserializer);
103  }
104  return false;
105 }
106 
107 
109  return size_ != 0;
110 }
111 
112 
114  if (context_size_ == 0) {
115  return Handle<Context>();
116  }
117  SnapshotByteSource source(context_raw_data_,
118  context_raw_size_);
119  Deserializer deserializer(&source);
120  Object* root;
121  deserializer.set_reservation(NEW_SPACE, context_new_space_used_);
122  deserializer.set_reservation(OLD_POINTER_SPACE, context_pointer_space_used_);
123  deserializer.set_reservation(OLD_DATA_SPACE, context_data_space_used_);
124  deserializer.set_reservation(CODE_SPACE, context_code_space_used_);
125  deserializer.set_reservation(MAP_SPACE, context_map_space_used_);
126  deserializer.set_reservation(CELL_SPACE, context_cell_space_used_);
127  deserializer.DeserializePartial(&root);
128  CHECK(root->IsContext());
129  return Handle<Context>(Context::cast(root));
130 }
131 
132 } } // namespace v8::internal
static bool Initialize(Deserializer *des)
Definition: v8.cc:64
#define CHECK_EQ(expected, value)
Definition: checks.h:219
static Handle< Context > NewContextFromSnapshot()
static Context * cast(Object *context)
Definition: contexts.h:212
void DeserializePartial(Object **root)
Definition: serialize.cc:639
#define CHECK(condition)
Definition: checks.h:56
byte * ReadBytes(const char *filename, int *size, bool verbose)
Definition: v8utils.cc:163
void set_reservation(int space_number, int reservation)
Definition: serialize.h:331
uint8_t byte
Definition: globals.h:156
static FILE * FOpen(const char *path, const char *mode)
static Vector< T > New(int length)
Definition: utils.h:370
#define CHECK_NE(unexpected, value)
Definition: checks.h:223
int StrLength(const char *string)
Definition: utils.h:234
static int SNPrintF(Vector< char > str, const char *format,...)
static bool HaveASnapshotToStartFrom()
activate correct semantics for inheriting readonliness enable harmony semantics for typeof enable harmony enable harmony proxies enable all harmony harmony_scoping harmony_proxies harmony_scoping tracks arrays with only smi values automatically unbox arrays of doubles use crankshaft use hydrogen range analysis use hydrogen global value numbering use function inlining maximum number of AST nodes considered for a single inlining loop invariant code motion print statistics for hydrogen trace generated IR for specified phases trace register allocator trace range analysis trace representation types environment for every instruction put a break point before deoptimizing polymorphic inlining perform array bounds checks elimination use dead code elimination trace on stack replacement optimize closures cache optimized code for closures functions with arguments object loop weight for representation inference allow uint32 values on optimize frames if they are used only in safe operations track parallel recompilation enable all profiler experiments number of stack frames inspected by the profiler call recompile stub directly when self optimizing trigger profiler ticks based on counting instead of timing weight back edges by jump distance for interrupt triggering percentage of ICs that must have type info to allow optimization watch_ic_patching retry_self_opt interrupt_at_exit extra verbose compilation tracing generate extra emit comments in code disassembly enable use of SSE3 instructions if available enable use of CMOV instruction if available enable use of SAHF instruction if enable use of VFP3 instructions if available this implies enabling ARMv7 and VFP2 enable use of VFP2 instructions if available enable use of SDIV and UDIV instructions if enable loading bit constant by means of movw movt instruction enable unaligned accesses for enable use of MIPS FPU instructions if NULL
Definition: flags.cc:301
const Register fp
void DeleteArray(T *array)
Definition: allocation.h:91
static bool Initialize(const char *snapshot_file=NULL)