text: // Copyright 2020 the V8 project authors. All rights reserved.
  // Use of this source code is governed by a BSD-style license that can be
  // found in the LICENSE file.
  
  #include "src/snapshot/snapshot-data.h"
  
  #include "src/common/assert-scope.h"
  #include "src/snapshot/serializer.h"
  
  #ifdef V8_SNAPSHOT_COMPRESSION
  #include "src/snapshot/snapshot-compression.h"
  #endif
  
  namespace v8 {
  namespace internal {
  
  void SerializedData::AllocateData(uint32_t size) {
    DCHECK(!owns_data_);
    data_ = NewArray<byte>(size);
    size_ = size;
    owns_data_ = true;
  }
  
  // static
  constexpr uint32_t SerializedData::kMagicNumber;
  
  SnapshotData::SnapshotData(const Serializer* serializer) {
    DisallowGarbageCollection no_gc;
    std::vector<Reservation> reservations = serializer->EncodeReservations();
    const std::vector<byte>* payload = serializer->Payload();
  
    // Calculate sizes.
    uint32_t reservation_size =
        static_cast<uint32_t>(reservations.size()) * kUInt32Size;
    uint32_t payload_offset = kHeaderSize + reservation_size;
    uint32_t padded_payload_offset = POINTER_SIZE_ALIGN(payload_offset);
    uint32_t size =
        padded_payload_offset + static_cast<uint32_t>(payload->size());
  
    // Allocate backing store and create result data.
    AllocateData(size);
  
    // Zero out pre-payload data. Part of that is only used for padding.
    memset(data_, 0, padded_payload_offset);
  
    // Set header values.
    SetMagicNumber();
    SetHeaderValue(kNumReservationsOffset, static_cast<int>(reservations.size()));
    SetHeaderValue(kPayloadLengthOffset, static_cast<int>(payload->size()));
  
    // Copy reservation chunk sizes.
    CopyBytes(data_ + kHeaderSize, reinterpret_cast<byte*>(reservations.data()),
              reservation_size);
  
    // Copy serialized data.
    CopyBytes(data_ + padded_payload_offset, payload->data(),
              static_cast<size_t>(payload->size()));
  }
  
  std::vector<SerializedData::Reservation> SnapshotData::Reservations() const {
    uint32_t size = GetHeaderValue(kNumReservationsOffset);
    std::vector<SerializedData::Reservation> reservations(size);
    memcpy(reservations.data(), data_ + kHeaderSize,
           size * sizeof(SerializedData::Reservation));
    return reservations;
  }
  
  Vector<const byte> SnapshotData::Payload() const {
    uint32_t reservations_size =
        GetHeaderValue(kNumReservationsOffset) * kUInt32Size;
    uint32_t padded_payload_offset =
        POINTER_SIZE_ALIGN(kHeaderSize + reservations_size);
    const byte* payload = data_ + padded_payload_offset;
    uint32_t length = GetHeaderValue(kPayloadLengthOffset);
    DCHECK_EQ(data_ + size_, payload + length);
    return Vector<const byte>(payload, length);
  }
  
  }  // namespace internal
  }  // namespace v8
  