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