5 #ifndef V8_HEAP_SWEEPER_H_ 6 #define V8_HEAP_SWEEPER_H_ 11 #include "src/base/platform/semaphore.h" 12 #include "src/cancelable-task.h" 13 #include "src/globals.h" 18 class MajorNonAtomicMarkingState;
22 enum FreeSpaceTreatmentMode { IGNORE_FREE_SPACE, ZAP_FREE_SPACE };
26 typedef std::vector<Page*> IterabilityList;
27 typedef std::deque<Page*> SweepingList;
28 typedef std::vector<Page*> SweptList;
50 template <
typename Callback>
51 void FilterOldSpaceSweepingPages(Callback callback) {
52 if (!sweeping_in_progress_)
return;
54 SweepingList* sweeper_list =
55 &sweeper_->sweeping_list_[GetSweepSpaceIndex(OLD_SPACE)];
57 for (
auto it = old_space_sweeping_list_.begin();
58 it != old_space_sweeping_list_.end(); it++) {
60 sweeper_list->push_back(*it);
67 SweepingList old_space_sweeping_list_;
69 bool sweeping_in_progress_;
72 enum FreeListRebuildingMode { REBUILD_FREE_LIST, IGNORE_FREE_LIST };
73 enum ClearOldToNewSlotsMode {
78 enum AddPageMode { REGULAR, READD_TEMPORARY_REMOVED_PAGE };
80 Sweeper(Heap* heap, MajorNonAtomicMarkingState* marking_state);
82 bool sweeping_in_progress()
const {
return sweeping_in_progress_; }
84 void AddPage(AllocationSpace space, Page* page, AddPageMode mode);
86 int ParallelSweepSpace(AllocationSpace identity,
int required_freed_bytes,
88 int ParallelSweepPage(Page* page, AllocationSpace identity);
90 void ScheduleIncrementalSweepingTask();
92 int RawSweep(Page* p, FreeListRebuildingMode free_list_mode,
93 FreeSpaceTreatmentMode free_space_mode);
99 void StartSweeperTasks();
100 void EnsureCompleted();
101 bool AreSweeperTasksRunning();
103 Page* GetSweptPageSafe(PagedSpace* space);
105 void EnsurePageIsIterable(Page* page);
107 void AddPageForIterability(Page* page);
108 void StartIterabilityTasks();
109 void EnsureIterabilityCompleted();
112 class IncrementalSweeperTask;
113 class IterabilityTask;
116 static const int kNumberOfSweepingSpaces =
117 LAST_GROWABLE_PAGED_SPACE - FIRST_GROWABLE_PAGED_SPACE + 1;
118 static const int kMaxSweeperTasks = 3;
120 template <
typename Callback>
121 void ForAllSweepingSpaces(Callback callback)
const {
123 callback(CODE_SPACE);
128 bool IsDoneSweeping()
const {
130 ForAllSweepingSpaces([
this, &is_done](AllocationSpace space) {
131 if (!sweeping_list_[GetSweepSpaceIndex(space)].empty()) is_done =
false;
136 void SweepSpaceFromTask(AllocationSpace identity);
140 bool SweepSpaceIncrementallyFromTask(AllocationSpace identity);
142 void AbortAndWaitForTasks();
144 Page* GetSweepingPageSafe(AllocationSpace space);
146 void PrepareToBeSweptPage(AllocationSpace space, Page* page);
148 void SweepOrWaitUntilSweepingCompleted(Page* page);
150 void MakeIterable(Page* page);
152 bool IsValidIterabilitySpace(AllocationSpace space) {
153 return space == NEW_SPACE || space == RO_SPACE;
156 static bool IsValidSweepingSpace(AllocationSpace space) {
157 return space >= FIRST_GROWABLE_PAGED_SPACE &&
158 space <= LAST_GROWABLE_PAGED_SPACE;
161 static int GetSweepSpaceIndex(AllocationSpace space) {
162 DCHECK(IsValidSweepingSpace(space));
163 return space - FIRST_GROWABLE_PAGED_SPACE;
167 MajorNonAtomicMarkingState* marking_state_;
169 CancelableTaskManager::Id task_ids_[kNumberOfSweepingSpaces];
170 base::Semaphore pending_sweeper_tasks_semaphore_;
172 SweptList swept_list_[kNumberOfSweepingSpaces];
173 SweepingList sweeping_list_[kNumberOfSweepingSpaces];
174 bool incremental_sweeper_pending_;
175 bool sweeping_in_progress_;
178 std::atomic<intptr_t> num_sweeping_tasks_;
180 std::atomic<bool> stop_sweeper_tasks_;
183 IterabilityList iterability_list_;
184 CancelableTaskManager::Id iterability_task_id_;
185 base::Semaphore iterability_task_semaphore_;
186 bool iterability_in_progress_;
187 bool iterability_task_started_;
188 bool should_reduce_memory_;
194 #endif // V8_HEAP_SWEEPER_H_