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