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   Compactor(const Compactor&) = delete;
24   Compactor& operator=(const Compactor&) = delete;
25 
26   void InitializeIfShouldCompact(GarbageCollector::Config::MarkingType,
27                                  GarbageCollector::Config::StackState);
28   // Returns true is compaction was cancelled.
29   bool CancelIfShouldNotCompact(GarbageCollector::Config::MarkingType,
30                                 GarbageCollector::Config::StackState);
31   CompactableSpaceHandling CompactSpacesIfEnabled();
32 
compaction_worklists()33   CompactionWorklists* compaction_worklists() {
34     return compaction_worklists_.get();
35   }
36 
37   void EnableForNextGCForTesting();
IsEnabledForTesting()38   bool IsEnabledForTesting() const { return is_enabled_; }
39 
40  private:
41   bool ShouldCompact(GarbageCollector::Config::MarkingType,
42                      GarbageCollector::Config::StackState) const;
43 
44   RawHeap& heap_;
45   // Compactor does not own the compactable spaces. The heap owns all spaces.
46   std::vector<NormalPageSpace*> compactable_spaces_;
47 
48   std::unique_ptr<CompactionWorklists> compaction_worklists_;
49 
50   bool is_enabled_ = false;
51   bool enable_for_next_gc_for_testing_ = false;
52 };
53 
54 }  // namespace internal
55 }  // namespace cppgc
56 
57 #endif  // V8_HEAP_CPPGC_COMPACTOR_H_
58