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_DESERIALIZER_ALLOCATOR_H_ 6 #define V8_SNAPSHOT_DEFAULT_DESERIALIZER_ALLOCATOR_H_ 7 8 #include "src/globals.h" 9 #include "src/heap/heap.h" 10 #include "src/snapshot/serializer-common.h" 11 12 namespace v8 { 13 namespace internal { 14 15 template <class AllocatorT> 16 class Deserializer; 17 18 class BuiltinDeserializer; 19 class StartupDeserializer; 20 21 class DefaultDeserializerAllocator final { 22 public: 23 DefaultDeserializerAllocator( 24 Deserializer<DefaultDeserializerAllocator>* deserializer); 25 26 // ------- Allocation Methods ------- 27 // Methods related to memory allocation during deserialization. 28 29 Address Allocate(AllocationSpace space, int size); 30 31 void MoveToNextChunk(AllocationSpace space); SetAlignment(AllocationAlignment alignment)32 void SetAlignment(AllocationAlignment alignment) { 33 DCHECK_EQ(kWordAligned, next_alignment_); 34 DCHECK_LE(kWordAligned, alignment); 35 DCHECK_LE(alignment, kDoubleUnaligned); 36 next_alignment_ = static_cast<AllocationAlignment>(alignment); 37 } 38 set_next_reference_is_weak(bool next_reference_is_weak)39 void set_next_reference_is_weak(bool next_reference_is_weak) { 40 next_reference_is_weak_ = next_reference_is_weak; 41 } 42 GetAndClearNextReferenceIsWeak()43 bool GetAndClearNextReferenceIsWeak() { 44 bool saved = next_reference_is_weak_; 45 next_reference_is_weak_ = false; 46 return saved; 47 } 48 49 #ifdef DEBUG next_reference_is_weak()50 bool next_reference_is_weak() const { return next_reference_is_weak_; } 51 #endif 52 53 HeapObject* GetMap(uint32_t index); 54 HeapObject* GetLargeObject(uint32_t index); 55 HeapObject* GetObject(AllocationSpace space, uint32_t chunk_index, 56 uint32_t chunk_offset); 57 58 // ------- Reservation Methods ------- 59 // Methods related to memory reservations (prior to deserialization). 60 61 void DecodeReservation(std::vector<SerializedData::Reservation> res); 62 bool ReserveSpace(); 63 64 // Atomically reserves space for the two given deserializers. Guarantees 65 // reservation for both without garbage collection in-between. 66 static bool ReserveSpace(StartupDeserializer* startup_deserializer, 67 BuiltinDeserializer* builtin_deserializer); 68 69 bool ReservationsAreFullyUsed() const; 70 71 // ------- Misc Utility Methods ------- 72 73 void RegisterDeserializedObjectsForBlackAllocation(); 74 75 private: 76 Isolate* isolate() const; 77 78 // Raw allocation without considering alignment. 79 Address AllocateRaw(AllocationSpace space, int size); 80 81 private: 82 static constexpr int kNumberOfPreallocatedSpaces = 83 SerializerDeserializer::kNumberOfPreallocatedSpaces; 84 static constexpr int kNumberOfSpaces = 85 SerializerDeserializer::kNumberOfSpaces; 86 87 // The address of the next object that will be allocated in each space. 88 // Each space has a number of chunks reserved by the GC, with each chunk 89 // fitting into a page. Deserialized objects are allocated into the 90 // current chunk of the target space by bumping up high water mark. 91 Heap::Reservation reservations_[kNumberOfSpaces]; 92 uint32_t current_chunk_[kNumberOfPreallocatedSpaces]; 93 Address high_water_[kNumberOfPreallocatedSpaces]; 94 95 // The alignment of the next allocation. 96 AllocationAlignment next_alignment_ = kWordAligned; 97 bool next_reference_is_weak_ = false; 98 99 // All required maps are pre-allocated during reservation. {next_map_index_} 100 // stores the index of the next map to return from allocation. 101 uint32_t next_map_index_ = 0; 102 std::vector<Address> allocated_maps_; 103 104 // Allocated large objects are kept in this map and may be fetched later as 105 // back-references. 106 std::vector<HeapObject*> deserialized_large_objects_; 107 108 // The current deserializer. 109 Deserializer<DefaultDeserializerAllocator>* const deserializer_; 110 111 DISALLOW_COPY_AND_ASSIGN(DefaultDeserializerAllocator) 112 }; 113 114 } // namespace internal 115 } // namespace v8 116 117 #endif // V8_SNAPSHOT_DEFAULT_DESERIALIZER_ALLOCATOR_H_ 118