• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2020 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_DATA_H_
6 #define V8_SNAPSHOT_SNAPSHOT_DATA_H_
7 
8 #include "src/base/bit-field.h"
9 #include "src/base/memory.h"
10 #include "src/base/vector.h"
11 #include "src/codegen/external-reference-table.h"
12 #include "src/utils/memcopy.h"
13 
14 namespace v8 {
15 namespace internal {
16 
17 // Forward declarations.
18 class Isolate;
19 class Serializer;
20 
21 class SerializedData {
22  public:
SerializedData(byte * data,int size)23   SerializedData(byte* data, int size)
24       : data_(data), size_(size), owns_data_(false) {}
SerializedData()25   SerializedData() : data_(nullptr), size_(0), owns_data_(false) {}
SerializedData(SerializedData && other)26   SerializedData(SerializedData&& other) V8_NOEXCEPT
27       : data_(other.data_),
28         size_(other.size_),
29         owns_data_(other.owns_data_) {
30     // Ensure |other| will not attempt to destroy our data in destructor.
31     other.owns_data_ = false;
32   }
33   SerializedData(const SerializedData&) = delete;
34   SerializedData& operator=(const SerializedData&) = delete;
35 
~SerializedData()36   virtual ~SerializedData() {
37     if (owns_data_) DeleteArray<byte>(data_);
38   }
39 
GetMagicNumber()40   uint32_t GetMagicNumber() const { return GetHeaderValue(kMagicNumberOffset); }
41 
42   using ChunkSizeBits = base::BitField<uint32_t, 0, 31>;
43   using IsLastChunkBits = base::BitField<bool, 31, 1>;
44 
45   static constexpr uint32_t kMagicNumberOffset = 0;
46   static constexpr uint32_t kMagicNumber =
47       0xC0DE0000 ^ ExternalReferenceTable::kSize;
48 
49  protected:
SetHeaderValue(uint32_t offset,uint32_t value)50   void SetHeaderValue(uint32_t offset, uint32_t value) {
51     base::WriteLittleEndianValue(reinterpret_cast<Address>(data_) + offset,
52                                  value);
53   }
54 
GetHeaderValue(uint32_t offset)55   uint32_t GetHeaderValue(uint32_t offset) const {
56     return base::ReadLittleEndianValue<uint32_t>(
57         reinterpret_cast<Address>(data_) + offset);
58   }
59 
60   void AllocateData(uint32_t size);
61 
SetMagicNumber()62   void SetMagicNumber() { SetHeaderValue(kMagicNumberOffset, kMagicNumber); }
63 
64   byte* data_;
65   uint32_t size_;
66   bool owns_data_;
67 };
68 
69 // Wrapper around reservation sizes and the serialization payload.
70 class V8_EXPORT_PRIVATE SnapshotData : public SerializedData {
71  public:
72   // Used when producing.
73   explicit SnapshotData(const Serializer* serializer);
74 
75   // Used when consuming.
SnapshotData(const base::Vector<const byte> snapshot)76   explicit SnapshotData(const base::Vector<const byte> snapshot)
77       : SerializedData(const_cast<byte*>(snapshot.begin()), snapshot.length()) {
78   }
79 
80   virtual base::Vector<const byte> Payload() const;
81 
RawData()82   base::Vector<const byte> RawData() const {
83     return base::Vector<const byte>(data_, size_);
84   }
85 
86  protected:
87   // Empty constructor used by SnapshotCompression so it can manually allocate
88   // memory.
SnapshotData()89   SnapshotData() : SerializedData() {}
90   friend class SnapshotCompression;
91 
92   // Resize used by SnapshotCompression so it can shrink the compressed
93   // SnapshotData.
Resize(uint32_t size)94   void Resize(uint32_t size) { size_ = size; }
95 
96   // The data header consists of uint32_t-sized entries:
97   // [0] magic number and (internal) external reference count
98   // [1] payload length
99   // ... serialized payload
100   static const uint32_t kPayloadLengthOffset = kMagicNumberOffset + kUInt32Size;
101   static const uint32_t kHeaderSize = kPayloadLengthOffset + kUInt32Size;
102 };
103 
104 }  // namespace internal
105 }  // namespace v8
106 
107 #endif  // V8_SNAPSHOT_SNAPSHOT_DATA_H_
108