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_SWEEPER_H_ 6 #define V8_HEAP_CPPGC_SWEEPER_H_ 7 8 #include <memory> 9 10 #include "include/cppgc/heap.h" 11 #include "src/base/macros.h" 12 #include "src/base/platform/time.h" 13 #include "src/heap/cppgc/memory.h" 14 15 namespace cppgc { 16 17 class Platform; 18 19 namespace internal { 20 21 class HeapBase; 22 class ConcurrentSweeperTest; 23 class NormalPageSpace; 24 25 class V8_EXPORT_PRIVATE Sweeper final { 26 public: 27 struct SweepingConfig { 28 using SweepingType = cppgc::Heap::SweepingType; 29 enum class CompactableSpaceHandling { kSweep, kIgnore }; 30 enum class FreeMemoryHandling { kDoNotDiscard, kDiscardWherePossible }; 31 32 SweepingType sweeping_type = SweepingType::kIncrementalAndConcurrent; 33 CompactableSpaceHandling compactable_space_handling = 34 CompactableSpaceHandling::kSweep; 35 FreeMemoryHandling free_memory_handling = FreeMemoryHandling::kDoNotDiscard; 36 }; 37 CanDiscardMemory()38 static constexpr bool CanDiscardMemory() { 39 return CheckMemoryIsInaccessibleIsNoop(); 40 } 41 42 explicit Sweeper(HeapBase&); 43 ~Sweeper(); 44 45 Sweeper(const Sweeper&) = delete; 46 Sweeper& operator=(const Sweeper&) = delete; 47 48 // Sweeper::Start assumes the heap holds no linear allocation buffers. 49 void Start(SweepingConfig); 50 void FinishIfRunning(); 51 void FinishIfOutOfWork(); 52 void NotifyDoneIfNeeded(); 53 // SweepForAllocationIfRunning sweeps the given |space| until a slot that can 54 // fit an allocation of size |size| is found. Returns true if a slot was 55 // found. 56 bool SweepForAllocationIfRunning(NormalPageSpace* space, size_t size); 57 58 bool IsSweepingOnMutatorThread() const; 59 bool IsSweepingInProgress() const; 60 61 // Assist with sweeping. Returns true if sweeping is done. 62 bool PerformSweepOnMutatorThread(double deadline_in_seconds); 63 64 private: 65 void WaitForConcurrentSweepingForTesting(); 66 67 class SweeperImpl; 68 69 HeapBase& heap_; 70 std::unique_ptr<SweeperImpl> impl_; 71 72 friend class ConcurrentSweeperTest; 73 }; 74 75 } // namespace internal 76 } // namespace cppgc 77 78 #endif // V8_HEAP_CPPGC_SWEEPER_H_ 79