Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 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 | #ifndef V8_SNAPSHOT_SNAPSHOT_H_ |
| 6 | #define V8_SNAPSHOT_SNAPSHOT_H_ |
| 7 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 8 | #include "src/snapshot/partial-serializer.h" |
| 9 | #include "src/snapshot/startup-serializer.h" |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 10 | |
| 11 | namespace v8 { |
| 12 | namespace internal { |
| 13 | |
| 14 | // Forward declarations. |
| 15 | class Isolate; |
| 16 | class PartialSerializer; |
| 17 | class StartupSerializer; |
| 18 | |
| 19 | class Snapshot : public AllStatic { |
| 20 | public: |
| 21 | class Metadata { |
| 22 | public: |
| 23 | explicit Metadata(uint32_t data = 0) : data_(data) {} |
| 24 | bool embeds_script() { return EmbedsScriptBits::decode(data_); } |
| 25 | void set_embeds_script(bool v) { |
| 26 | data_ = EmbedsScriptBits::update(data_, v); |
| 27 | } |
| 28 | |
| 29 | uint32_t& RawValue() { return data_; } |
| 30 | |
| 31 | private: |
| 32 | class EmbedsScriptBits : public BitField<bool, 0, 1> {}; |
| 33 | uint32_t data_; |
| 34 | }; |
| 35 | |
| 36 | // Initialize the Isolate from the internal snapshot. Returns false if no |
| 37 | // snapshot could be found. |
| 38 | static bool Initialize(Isolate* isolate); |
| 39 | // Create a new context using the internal partial snapshot. |
| 40 | static MaybeHandle<Context> NewContextFromSnapshot( |
| 41 | Isolate* isolate, Handle<JSGlobalProxy> global_proxy); |
| 42 | |
| 43 | static bool HaveASnapshotToStartFrom(Isolate* isolate); |
| 44 | |
| 45 | static bool EmbedsScript(Isolate* isolate); |
| 46 | |
| 47 | static uint32_t SizeOfFirstPage(Isolate* isolate, AllocationSpace space); |
| 48 | |
| 49 | |
| 50 | // To be implemented by the snapshot source. |
| 51 | static const v8::StartupData* DefaultSnapshotBlob(); |
| 52 | |
| 53 | static v8::StartupData CreateSnapshotBlob( |
| 54 | const StartupSerializer& startup_ser, |
| 55 | const PartialSerializer& context_ser, Snapshot::Metadata metadata); |
| 56 | |
| 57 | #ifdef DEBUG |
| 58 | static bool SnapshotIsValid(v8::StartupData* snapshot_blob); |
| 59 | #endif // DEBUG |
| 60 | |
| 61 | private: |
| 62 | static Vector<const byte> ExtractStartupData(const v8::StartupData* data); |
| 63 | static Vector<const byte> ExtractContextData(const v8::StartupData* data); |
| 64 | static Metadata ExtractMetadata(const v8::StartupData* data); |
| 65 | |
| 66 | // Snapshot blob layout: |
| 67 | // [0] metadata |
| 68 | // [1 - 6] pre-calculated first page sizes for paged spaces |
| 69 | // [7] serialized start up data length |
| 70 | // ... serialized start up data |
| 71 | // ... serialized context data |
| 72 | |
| 73 | static const int kNumPagedSpaces = LAST_PAGED_SPACE - FIRST_PAGED_SPACE + 1; |
| 74 | |
| 75 | static const int kMetadataOffset = 0; |
| 76 | static const int kFirstPageSizesOffset = kMetadataOffset + kInt32Size; |
| 77 | static const int kStartupLengthOffset = |
| 78 | kFirstPageSizesOffset + kNumPagedSpaces * kInt32Size; |
| 79 | static const int kStartupDataOffset = kStartupLengthOffset + kInt32Size; |
| 80 | |
| 81 | static int ContextOffset(int startup_length) { |
| 82 | return kStartupDataOffset + startup_length; |
| 83 | } |
| 84 | |
| 85 | DISALLOW_IMPLICIT_CONSTRUCTORS(Snapshot); |
| 86 | }; |
| 87 | |
| 88 | #ifdef V8_USE_EXTERNAL_STARTUP_DATA |
| 89 | void SetSnapshotFromFile(StartupData* snapshot_blob); |
| 90 | #endif |
| 91 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 92 | // Wrapper around reservation sizes and the serialization payload. |
| 93 | class SnapshotData : public SerializedData { |
| 94 | public: |
| 95 | // Used when producing. |
| 96 | explicit SnapshotData(const Serializer& ser); |
| 97 | |
| 98 | // Used when consuming. |
| 99 | explicit SnapshotData(const Vector<const byte> snapshot) |
| 100 | : SerializedData(const_cast<byte*>(snapshot.begin()), snapshot.length()) { |
| 101 | CHECK(IsSane()); |
| 102 | } |
| 103 | |
| 104 | Vector<const Reservation> Reservations() const; |
| 105 | Vector<const byte> Payload() const; |
| 106 | |
| 107 | Vector<const byte> RawData() const { |
| 108 | return Vector<const byte>(data_, size_); |
| 109 | } |
| 110 | |
| 111 | private: |
| 112 | bool IsSane(); |
| 113 | |
| 114 | // The data header consists of uint32_t-sized entries: |
| 115 | // [0] magic number and external reference count |
| 116 | // [1] version hash |
| 117 | // [2] number of reservation size entries |
| 118 | // [3] payload length |
| 119 | // ... reservations |
| 120 | // ... serialized payload |
| 121 | static const int kCheckSumOffset = kMagicNumberOffset + kInt32Size; |
| 122 | static const int kNumReservationsOffset = kCheckSumOffset + kInt32Size; |
| 123 | static const int kPayloadLengthOffset = kNumReservationsOffset + kInt32Size; |
| 124 | static const int kHeaderSize = kPayloadLengthOffset + kInt32Size; |
| 125 | }; |
| 126 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 127 | } // namespace internal |
| 128 | } // namespace v8 |
| 129 | |
| 130 | #endif // V8_SNAPSHOT_SNAPSHOT_H_ |