1 // Copyright 2017 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_DEFAULT_SERIALIZER_ALLOCATOR_H_ 6 #define V8_SNAPSHOT_DEFAULT_SERIALIZER_ALLOCATOR_H_ 7 8 #include "src/snapshot/serializer-common.h" 9 10 namespace v8 { 11 namespace internal { 12 13 template <class AllocatorT> 14 class Serializer; 15 16 class DefaultSerializerAllocator final { 17 public: 18 DefaultSerializerAllocator( 19 Serializer<DefaultSerializerAllocator>* serializer); 20 21 SerializerReference Allocate(AllocationSpace space, uint32_t size); 22 SerializerReference AllocateMap(); 23 SerializerReference AllocateLargeObject(uint32_t size); 24 SerializerReference AllocateOffHeapBackingStore(); 25 26 void UseCustomChunkSize(uint32_t chunk_size); 27 28 #ifdef DEBUG 29 bool BackReferenceIsAlreadyAllocated( 30 SerializerReference back_reference) const; 31 #endif 32 33 std::vector<SerializedData::Reservation> EncodeReservations() const; 34 35 void OutputStatistics(); 36 37 private: 38 // We try to not exceed this size for every chunk. We will not succeed for 39 // larger objects though. 40 uint32_t TargetChunkSize(int space); 41 42 static constexpr int kNumberOfPreallocatedSpaces = 43 SerializerDeserializer::kNumberOfPreallocatedSpaces; 44 static constexpr int kNumberOfSpaces = 45 SerializerDeserializer::kNumberOfSpaces; 46 47 // Objects from the same space are put into chunks for bulk-allocation 48 // when deserializing. We have to make sure that each chunk fits into a 49 // page. So we track the chunk size in pending_chunk_ of a space, but 50 // when it exceeds a page, we complete the current chunk and start a new one. 51 uint32_t pending_chunk_[kNumberOfPreallocatedSpaces]; 52 std::vector<uint32_t> completed_chunks_[kNumberOfPreallocatedSpaces]; 53 54 // Number of maps that we need to allocate. 55 uint32_t num_maps_ = 0; 56 57 // We map serialized large objects to indexes for back-referencing. 58 uint32_t large_objects_total_size_ = 0; 59 uint32_t seen_large_objects_index_ = 0; 60 61 // Used to keep track of the off-heap backing stores used by TypedArrays/ 62 // ArrayBuffers. Note that the index begins at 1 and not 0, because when a 63 // TypedArray has an on-heap backing store, the backing_store pointer in the 64 // corresponding ArrayBuffer will be null, which makes it indistinguishable 65 // from index 0. 66 uint32_t seen_backing_stores_index_ = 1; 67 68 uint32_t custom_chunk_size_ = 0; 69 70 // The current serializer. 71 Serializer<DefaultSerializerAllocator>* const serializer_; 72 73 DISALLOW_COPY_AND_ASSIGN(DefaultSerializerAllocator) 74 }; 75 76 } // namespace internal 77 } // namespace v8 78 79 #endif // V8_SNAPSHOT_DEFAULT_SERIALIZER_ALLOCATOR_H_ 80