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 #ifndef V8_HEAP_CPPGC_HEAP_BASE_H_ 6 #define V8_HEAP_CPPGC_HEAP_BASE_H_ 7 8 #include <memory> 9 #include <set> 10 11 #include "include/cppgc/heap.h" 12 #include "include/cppgc/internal/persistent-node.h" 13 #include "include/cppgc/macros.h" 14 #include "src/base/macros.h" 15 #include "src/heap/cppgc/compactor.h" 16 #include "src/heap/cppgc/marker.h" 17 #include "src/heap/cppgc/object-allocator.h" 18 #include "src/heap/cppgc/raw-heap.h" 19 #include "src/heap/cppgc/sweeper.h" 20 21 #if defined(CPPGC_CAGED_HEAP) 22 #include "src/heap/cppgc/caged-heap.h" 23 #endif 24 25 namespace heap { 26 namespace base { 27 class Stack; 28 } // namespace base 29 } // namespace heap 30 31 namespace cppgc { 32 33 class Platform; 34 35 namespace internal { 36 37 namespace testing { 38 class TestWithHeap; 39 } // namespace testing 40 41 class PageBackend; 42 class PreFinalizerHandler; 43 class StatsCollector; 44 45 // Base class for heap implementations. 46 class V8_EXPORT_PRIVATE HeapBase { 47 public: 48 using StackSupport = cppgc::Heap::StackSupport; 49 50 // NoGCScope allows going over limits and avoids triggering garbage 51 // collection triggered through allocations or even explicitly. 52 class V8_EXPORT_PRIVATE NoGCScope final { 53 CPPGC_STACK_ALLOCATED(); 54 55 public: 56 explicit NoGCScope(HeapBase& heap); 57 ~NoGCScope(); 58 59 NoGCScope(const NoGCScope&) = delete; 60 NoGCScope& operator=(const NoGCScope&) = delete; 61 62 private: 63 HeapBase& heap_; 64 }; 65 66 HeapBase(std::shared_ptr<cppgc::Platform> platform, 67 const std::vector<std::unique_ptr<CustomSpaceBase>>& custom_spaces, 68 StackSupport stack_support); 69 virtual ~HeapBase(); 70 71 HeapBase(const HeapBase&) = delete; 72 HeapBase& operator=(const HeapBase&) = delete; 73 raw_heap()74 RawHeap& raw_heap() { return raw_heap_; } raw_heap()75 const RawHeap& raw_heap() const { return raw_heap_; } 76 platform()77 cppgc::Platform* platform() { return platform_.get(); } platform()78 const cppgc::Platform* platform() const { return platform_.get(); } 79 page_backend()80 PageBackend* page_backend() { return page_backend_.get(); } page_backend()81 const PageBackend* page_backend() const { return page_backend_.get(); } 82 stats_collector()83 StatsCollector* stats_collector() { return stats_collector_.get(); } stats_collector()84 const StatsCollector* stats_collector() const { 85 return stats_collector_.get(); 86 } 87 88 #if defined(CPPGC_CAGED_HEAP) caged_heap()89 CagedHeap& caged_heap() { return caged_heap_; } caged_heap()90 const CagedHeap& caged_heap() const { return caged_heap_; } 91 #endif 92 stack()93 heap::base::Stack* stack() { return stack_.get(); } 94 prefinalizer_handler()95 PreFinalizerHandler* prefinalizer_handler() { 96 return prefinalizer_handler_.get(); 97 } 98 marker()99 MarkerBase* marker() const { return marker_.get(); } 100 compactor()101 Compactor& compactor() { return compactor_; } 102 object_allocator()103 ObjectAllocator& object_allocator() { return object_allocator_; } 104 sweeper()105 Sweeper& sweeper() { return sweeper_; } 106 GetStrongPersistentRegion()107 PersistentRegion& GetStrongPersistentRegion() { 108 return strong_persistent_region_; 109 } GetStrongPersistentRegion()110 const PersistentRegion& GetStrongPersistentRegion() const { 111 return strong_persistent_region_; 112 } GetWeakPersistentRegion()113 PersistentRegion& GetWeakPersistentRegion() { 114 return weak_persistent_region_; 115 } GetWeakPersistentRegion()116 const PersistentRegion& GetWeakPersistentRegion() const { 117 return weak_persistent_region_; 118 } GetStrongCrossThreadPersistentRegion()119 PersistentRegion& GetStrongCrossThreadPersistentRegion() { 120 return strong_cross_thread_persistent_region_; 121 } GetStrongCrossThreadPersistentRegion()122 const PersistentRegion& GetStrongCrossThreadPersistentRegion() const { 123 return strong_cross_thread_persistent_region_; 124 } GetWeakCrossThreadPersistentRegion()125 PersistentRegion& GetWeakCrossThreadPersistentRegion() { 126 return weak_cross_thread_persistent_region_; 127 } GetWeakCrossThreadPersistentRegion()128 const PersistentRegion& GetWeakCrossThreadPersistentRegion() const { 129 return weak_cross_thread_persistent_region_; 130 } 131 132 #if defined(CPPGC_YOUNG_GENERATION) remembered_slots()133 std::set<void*>& remembered_slots() { return remembered_slots_; } 134 #endif 135 136 size_t ObjectPayloadSize() const; 137 stack_support()138 StackSupport stack_support() const { return stack_support_; } 139 140 void AdvanceIncrementalGarbageCollectionOnAllocationIfNeeded(); 141 142 protected: 143 virtual void FinalizeIncrementalGarbageCollectionIfNeeded( 144 cppgc::Heap::StackState) = 0; 145 in_no_gc_scope()146 bool in_no_gc_scope() const { return no_gc_scope_ > 0; } 147 148 RawHeap raw_heap_; 149 std::shared_ptr<cppgc::Platform> platform_; 150 #if defined(CPPGC_CAGED_HEAP) 151 CagedHeap caged_heap_; 152 #endif 153 std::unique_ptr<PageBackend> page_backend_; 154 155 std::unique_ptr<StatsCollector> stats_collector_; 156 std::unique_ptr<heap::base::Stack> stack_; 157 std::unique_ptr<PreFinalizerHandler> prefinalizer_handler_; 158 std::unique_ptr<MarkerBase> marker_; 159 160 Compactor compactor_; 161 ObjectAllocator object_allocator_; 162 Sweeper sweeper_; 163 164 PersistentRegion strong_persistent_region_; 165 PersistentRegion weak_persistent_region_; 166 PersistentRegion strong_cross_thread_persistent_region_; 167 PersistentRegion weak_cross_thread_persistent_region_; 168 169 #if defined(CPPGC_YOUNG_GENERATION) 170 std::set<void*> remembered_slots_; 171 #endif 172 173 size_t no_gc_scope_ = 0; 174 175 const StackSupport stack_support_; 176 177 friend class MarkerBase::IncrementalMarkingTask; 178 friend class testing::TestWithHeap; 179 }; 180 181 } // namespace internal 182 } // namespace cppgc 183 184 #endif // V8_HEAP_CPPGC_HEAP_BASE_H_ 185