1 // Copyright 2006-2008 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 // The common functionality when building with or without snapshots.
6 
7 #include "src/v8.h"
8 
9 #include "src/api.h"
10 #include "src/base/platform/platform.h"
11 #include "src/serialize.h"
12 #include "src/snapshot.h"
13 
14 namespace v8 {
15 namespace internal {
16 
ReserveSpaceForLinkedInSnapshot(Deserializer * deserializer)17 void Snapshot::ReserveSpaceForLinkedInSnapshot(Deserializer* deserializer) {
18   deserializer->set_reservation(NEW_SPACE, new_space_used_);
19   deserializer->set_reservation(OLD_POINTER_SPACE, pointer_space_used_);
20   deserializer->set_reservation(OLD_DATA_SPACE, data_space_used_);
21   deserializer->set_reservation(CODE_SPACE, code_space_used_);
22   deserializer->set_reservation(MAP_SPACE, map_space_used_);
23   deserializer->set_reservation(CELL_SPACE, cell_space_used_);
24   deserializer->set_reservation(PROPERTY_CELL_SPACE,
25                                 property_cell_space_used_);
26 }
27 
28 
Initialize(Isolate * isolate)29 bool Snapshot::Initialize(Isolate* isolate) {
30   if (size_ > 0) {
31     base::ElapsedTimer timer;
32     if (FLAG_profile_deserialization) {
33       timer.Start();
34     }
35     SnapshotByteSource source(raw_data_, raw_size_);
36     Deserializer deserializer(&source);
37     ReserveSpaceForLinkedInSnapshot(&deserializer);
38     bool success = isolate->Init(&deserializer);
39     if (FLAG_profile_deserialization) {
40       double ms = timer.Elapsed().InMillisecondsF();
41       PrintF("[Snapshot loading and deserialization took %0.3f ms]\n", ms);
42     }
43     return success;
44   }
45   return false;
46 }
47 
48 
HaveASnapshotToStartFrom()49 bool Snapshot::HaveASnapshotToStartFrom() {
50   return size_ != 0;
51 }
52 
53 
NewContextFromSnapshot(Isolate * isolate)54 Handle<Context> Snapshot::NewContextFromSnapshot(Isolate* isolate) {
55   if (context_size_ == 0) {
56     return Handle<Context>();
57   }
58   SnapshotByteSource source(context_raw_data_,
59                             context_raw_size_);
60   Deserializer deserializer(&source);
61   Object* root;
62   deserializer.set_reservation(NEW_SPACE, context_new_space_used_);
63   deserializer.set_reservation(OLD_POINTER_SPACE, context_pointer_space_used_);
64   deserializer.set_reservation(OLD_DATA_SPACE, context_data_space_used_);
65   deserializer.set_reservation(CODE_SPACE, context_code_space_used_);
66   deserializer.set_reservation(MAP_SPACE, context_map_space_used_);
67   deserializer.set_reservation(CELL_SPACE, context_cell_space_used_);
68   deserializer.set_reservation(PROPERTY_CELL_SPACE,
69                                context_property_cell_space_used_);
70   deserializer.DeserializePartial(isolate, &root);
71   CHECK(root->IsContext());
72   return Handle<Context>(Context::cast(root));
73 }
74 
75 
76 #ifdef V8_USE_EXTERNAL_STARTUP_DATA
77 // Dummy implementations of Set*FromFile(..) APIs.
78 //
79 // These are meant for use with snapshot-external.cc. Should this file
80 // be compiled with those options we just supply these dummy implementations
81 // below. This happens when compiling the mksnapshot utility.
SetNativesFromFile(StartupData * data)82 void SetNativesFromFile(StartupData* data) { CHECK(false); }
SetSnapshotFromFile(StartupData * data)83 void SetSnapshotFromFile(StartupData* data) { CHECK(false); }
84 #endif  // V8_USE_EXTERNAL_STARTUP_DATA
85 
86 } }  // namespace v8::internal
87