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_COMPACTOR_H_ 6 #define V8_HEAP_CPPGC_COMPACTOR_H_ 7 8 #include "src/heap/cppgc/compaction-worklists.h" 9 #include "src/heap/cppgc/garbage-collector.h" 10 #include "src/heap/cppgc/raw-heap.h" 11 12 namespace cppgc { 13 namespace internal { 14 15 class V8_EXPORT_PRIVATE Compactor final { 16 using CompactableSpaceHandling = 17 Sweeper::SweepingConfig::CompactableSpaceHandling; 18 19 public: 20 explicit Compactor(RawHeap&); ~Compactor()21 ~Compactor() { DCHECK(!is_enabled_); } 22 23 void InitializeIfShouldCompact(GarbageCollector::Config::MarkingType, 24 GarbageCollector::Config::StackState); 25 // Returns true is compaction was cancelled. 26 bool CancelIfShouldNotCompact(GarbageCollector::Config::MarkingType, 27 GarbageCollector::Config::StackState); 28 CompactableSpaceHandling CompactSpacesIfEnabled(); 29 compaction_worklists()30 CompactionWorklists* compaction_worklists() { 31 return compaction_worklists_.get(); 32 } 33 EnableForNextGCForTesting()34 void EnableForNextGCForTesting() { enable_for_next_gc_for_testing_ = true; } 35 IsEnabledForTesting()36 bool IsEnabledForTesting() const { return is_enabled_; } 37 38 private: 39 bool ShouldCompact(GarbageCollector::Config::MarkingType, 40 GarbageCollector::Config::StackState); 41 42 RawHeap& heap_; 43 // Compactor does not own the compactable spaces. The heap owns all spaces. 44 std::vector<NormalPageSpace*> compactable_spaces_; 45 46 std::unique_ptr<CompactionWorklists> compaction_worklists_; 47 48 bool is_enabled_ = false; 49 50 bool enable_for_next_gc_for_testing_ = false; 51 }; 52 53 } // namespace internal 54 } // namespace cppgc 55 56 #endif // V8_HEAP_CPPGC_COMPACTOR_H_ 57