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 #include "src/snapshot/snapshot-data.h" 6 7 #include "src/common/assert-scope.h" 8 #include "src/snapshot/serializer.h" 9 10 #ifdef V8_SNAPSHOT_COMPRESSION 11 #include "src/snapshot/snapshot-compression.h" 12 #endif 13 14 namespace v8 { 15 namespace internal { 16 AllocateData(uint32_t size)17void SerializedData::AllocateData(uint32_t size) { 18 DCHECK(!owns_data_); 19 data_ = NewArray<byte>(size); 20 size_ = size; 21 owns_data_ = true; 22 } 23 24 // static 25 constexpr uint32_t SerializedData::kMagicNumber; 26 SnapshotData(const Serializer * serializer)27SnapshotData::SnapshotData(const Serializer* serializer) { 28 DisallowGarbageCollection no_gc; 29 const std::vector<byte>* payload = serializer->Payload(); 30 31 // Calculate sizes. 32 uint32_t size = kHeaderSize + static_cast<uint32_t>(payload->size()); 33 34 // Allocate backing store and create result data. 35 AllocateData(size); 36 37 // Zero out pre-payload data. Part of that is only used for padding. 38 memset(data_, 0, kHeaderSize); 39 40 // Set header values. 41 SetMagicNumber(); 42 SetHeaderValue(kPayloadLengthOffset, static_cast<int>(payload->size())); 43 44 // Copy serialized data. 45 CopyBytes(data_ + kHeaderSize, payload->data(), 46 static_cast<size_t>(payload->size())); 47 } 48 Payload() const49Vector<const byte> SnapshotData::Payload() const { 50 const byte* payload = data_ + kHeaderSize; 51 uint32_t length = GetHeaderValue(kPayloadLengthOffset); 52 DCHECK_EQ(data_ + size_, payload + length); 53 return Vector<const byte>(payload, length); 54 } 55 56 } // namespace internal 57 } // namespace v8 58