OLD | NEW |
---|---|
1 // Copyright 2006-2008 the V8 project authors. All rights reserved. | 1 // Copyright 2006-2008 the V8 project authors. All rights reserved. |
2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
4 // met: | 4 // met: |
5 // | 5 // |
6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
(...skipping 19 matching lines...) Expand all Loading... | |
30 #include "v8.h" | 30 #include "v8.h" |
31 | 31 |
32 #include "api.h" | 32 #include "api.h" |
33 #include "serialize.h" | 33 #include "serialize.h" |
34 #include "snapshot.h" | 34 #include "snapshot.h" |
35 #include "platform.h" | 35 #include "platform.h" |
36 | 36 |
37 namespace v8 { | 37 namespace v8 { |
38 namespace internal { | 38 namespace internal { |
39 | 39 |
40 bool Snapshot::Deserialize(const byte* content, int len) { | 40 |
41 SnapshotByteSource source(content, len); | 41 static void ReserveSpaceForSnapshot(Deserializer* deserializer, |
42 Deserializer deserializer(&source); | 42 const char* file_name) { |
43 return V8::Initialize(&deserializer); | 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_); | |
44 } | 81 } |
45 | 82 |
46 | 83 |
47 bool Snapshot::Initialize(const char* snapshot_file) { | 84 bool Snapshot::Initialize(const char* snapshot_file) { |
48 if (snapshot_file) { | 85 if (snapshot_file) { |
49 int len; | 86 int len; |
50 byte* str = ReadBytes(snapshot_file, &len); | 87 byte* str = ReadBytes(snapshot_file, &len); |
51 if (!str) return false; | 88 if (!str) return false; |
52 Deserialize(str, len); | 89 SnapshotByteSource source(str, len); |
90 Deserializer deserializer(&source); | |
91 ReserveSpaceForSnapshot(&deserializer, snapshot_file); | |
92 bool success = V8::Initialize(&deserializer); | |
Yang
2012/09/13 08:47:59
Is there any plans to include a SnapshotByteSink t
Erik Corry
2012/09/13 12:13:35
This is currently only used for tests.
| |
53 DeleteArray(str); | 93 DeleteArray(str); |
54 return true; | 94 return success; |
55 } else if (size_ > 0) { | 95 } else if (size_ > 0) { |
56 Deserialize(raw_data_, raw_size_); | 96 SnapshotByteSource source(raw_data_, raw_size_); |
57 return true; | 97 Deserializer deserializer(&source); |
98 ReserveSpaceForLinkedInSnapshot(&deserializer); | |
99 return V8::Initialize(&deserializer); | |
58 } | 100 } |
59 return false; | 101 return false; |
60 } | 102 } |
61 | 103 |
62 | 104 |
63 bool Snapshot::HaveASnapshotToStartFrom() { | 105 bool Snapshot::HaveASnapshotToStartFrom() { |
64 return size_ != 0; | 106 return size_ != 0; |
65 } | 107 } |
66 | 108 |
67 | 109 |
68 Handle<Context> Snapshot::NewContextFromSnapshot() { | 110 Handle<Context> Snapshot::NewContextFromSnapshot() { |
69 if (context_size_ == 0) { | 111 if (context_size_ == 0) { |
70 return Handle<Context>(); | 112 return Handle<Context>(); |
71 } | 113 } |
72 HEAP->ReserveSpace(new_space_used_, | |
73 pointer_space_used_, | |
74 data_space_used_, | |
75 code_space_used_, | |
76 map_space_used_, | |
77 cell_space_used_, | |
78 large_space_used_); | |
79 SnapshotByteSource source(context_raw_data_, | 114 SnapshotByteSource source(context_raw_data_, |
80 context_raw_size_); | 115 context_raw_size_); |
81 Deserializer deserializer(&source); | 116 Deserializer deserializer(&source); |
82 Object* root; | 117 Object* root; |
118 deserializer.set_reservation(NEW_SPACE, context_new_space_used_); | |
119 deserializer.set_reservation(OLD_POINTER_SPACE, context_pointer_space_used_); | |
120 deserializer.set_reservation(OLD_DATA_SPACE, context_data_space_used_); | |
121 deserializer.set_reservation(CODE_SPACE, context_code_space_used_); | |
122 deserializer.set_reservation(MAP_SPACE, context_map_space_used_); | |
123 deserializer.set_reservation(CELL_SPACE, context_cell_space_used_); | |
83 deserializer.DeserializePartial(&root); | 124 deserializer.DeserializePartial(&root); |
84 CHECK(root->IsContext()); | 125 CHECK(root->IsContext()); |
85 return Handle<Context>(Context::cast(root)); | 126 return Handle<Context>(Context::cast(root)); |
86 } | 127 } |
87 | 128 |
88 } } // namespace v8::internal | 129 } } // namespace v8::internal |
OLD | NEW |