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 37namespace v8 { 38namespace internal { 39 40 41static 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 property_cell_size; 50#ifdef _MSC_VER 51 // Avoid warning about unsafe fscanf from MSVC. 52 // Please note that this is only fine if %c and %s are not being used. 53#define fscanf fscanf_s 54#endif 55 CHECK_EQ(1, fscanf(fp, "new %d\n", &new_size)); 56 CHECK_EQ(1, fscanf(fp, "pointer %d\n", &pointer_size)); 57 CHECK_EQ(1, fscanf(fp, "data %d\n", &data_size)); 58 CHECK_EQ(1, fscanf(fp, "code %d\n", &code_size)); 59 CHECK_EQ(1, fscanf(fp, "map %d\n", &map_size)); 60 CHECK_EQ(1, fscanf(fp, "cell %d\n", &cell_size)); 61 CHECK_EQ(1, fscanf(fp, "property cell %d\n", &property_cell_size)); 62#ifdef _MSC_VER 63#undef fscanf 64#endif 65 fclose(fp); 66 deserializer->set_reservation(NEW_SPACE, new_size); 67 deserializer->set_reservation(OLD_POINTER_SPACE, pointer_size); 68 deserializer->set_reservation(OLD_DATA_SPACE, data_size); 69 deserializer->set_reservation(CODE_SPACE, code_size); 70 deserializer->set_reservation(MAP_SPACE, map_size); 71 deserializer->set_reservation(CELL_SPACE, cell_size); 72 deserializer->set_reservation(PROPERTY_CELL_SPACE, 73 property_cell_size); 74 name.Dispose(); 75} 76 77 78void Snapshot::ReserveSpaceForLinkedInSnapshot(Deserializer* deserializer) { 79 deserializer->set_reservation(NEW_SPACE, new_space_used_); 80 deserializer->set_reservation(OLD_POINTER_SPACE, pointer_space_used_); 81 deserializer->set_reservation(OLD_DATA_SPACE, data_space_used_); 82 deserializer->set_reservation(CODE_SPACE, code_space_used_); 83 deserializer->set_reservation(MAP_SPACE, map_space_used_); 84 deserializer->set_reservation(CELL_SPACE, cell_space_used_); 85 deserializer->set_reservation(PROPERTY_CELL_SPACE, 86 property_cell_space_used_); 87} 88 89 90bool Snapshot::Initialize(const char* snapshot_file) { 91 if (snapshot_file) { 92 int len; 93 byte* str = ReadBytes(snapshot_file, &len); 94 if (!str) return false; 95 bool success; 96 { 97 SnapshotByteSource source(str, len); 98 Deserializer deserializer(&source); 99 ReserveSpaceForSnapshot(&deserializer, snapshot_file); 100 success = V8::Initialize(&deserializer); 101 } 102 DeleteArray(str); 103 return success; 104 } else if (size_ > 0) { 105 SnapshotByteSource source(raw_data_, raw_size_); 106 Deserializer deserializer(&source); 107 ReserveSpaceForLinkedInSnapshot(&deserializer); 108 return V8::Initialize(&deserializer); 109 } 110 return false; 111} 112 113 114bool Snapshot::HaveASnapshotToStartFrom() { 115 return size_ != 0; 116} 117 118 119Handle<Context> Snapshot::NewContextFromSnapshot() { 120 if (context_size_ == 0) { 121 return Handle<Context>(); 122 } 123 SnapshotByteSource source(context_raw_data_, 124 context_raw_size_); 125 Deserializer deserializer(&source); 126 Object* root; 127 deserializer.set_reservation(NEW_SPACE, context_new_space_used_); 128 deserializer.set_reservation(OLD_POINTER_SPACE, context_pointer_space_used_); 129 deserializer.set_reservation(OLD_DATA_SPACE, context_data_space_used_); 130 deserializer.set_reservation(CODE_SPACE, context_code_space_used_); 131 deserializer.set_reservation(MAP_SPACE, context_map_space_used_); 132 deserializer.set_reservation(CELL_SPACE, context_cell_space_used_); 133 deserializer.set_reservation(PROPERTY_CELL_SPACE, 134 context_property_cell_space_used_); 135 deserializer.DeserializePartial(&root); 136 CHECK(root->IsContext()); 137 return Handle<Context>(Context::cast(root)); 138} 139 140} } // namespace v8::internal 141