v8
V8 is Google’s open source high-performance JavaScript and WebAssembly engine, written in C++.
Loading...
Searching...
No Matches
heap-base.h
Go to the documentation of this file.
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
13#include "include/cppgc/heap.h"
16#include "src/base/macros.h"
28#include "v8config.h" // NOLINT(build/include_directory)
29
30#if defined(CPPGC_YOUNG_GENERATION)
32#endif
33
34namespace v8 {
35namespace base {
36class LsanPageAllocator;
37} // namespace base
38} // namespace v8
39
40namespace heap {
41namespace base {
42class Stack;
43} // namespace base
44} // namespace heap
45
46namespace cppgc {
47namespace subtle {
50} // namespace subtle
51
52namespace testing {
53class Heap;
54} // namespace testing
55
56class Platform;
57
58namespace internal {
59
60class FatalOutOfMemoryHandler;
61class GarbageCollector;
62class PageBackend;
63class PreFinalizerHandler;
64class StatsCollector;
65
66enum class HeapObjectNameForUnnamedObject : uint8_t;
67enum class StickyBits : uint8_t {
70};
71
73 public:
74 // This function may be called simultaneously on multiple threads.
75 // Implementations must not attempt to allocate or do any other actions
76 // which could trigger reentrant GC.
77 virtual void OnMove(Address from, Address to,
78 size_t size_including_header) = 0;
79};
80
81// Base class for heap implementations.
83 public:
87
88 static HeapBase& From(cppgc::HeapHandle& heap_handle) {
89 return static_cast<HeapBase&>(heap_handle);
90 }
91 static const HeapBase& From(const cppgc::HeapHandle& heap_handle) {
92 return static_cast<const HeapBase&>(heap_handle);
93 }
94
95 HeapBase(std::shared_ptr<cppgc::Platform> platform,
96 const std::vector<std::unique_ptr<CustomSpaceBase>>& custom_spaces,
97 StackSupport stack_support, MarkingType marking_support,
98 SweepingType sweeping_support, GarbageCollector& garbage_collector);
99 virtual ~HeapBase();
100
101 HeapBase(const HeapBase&) = delete;
102 HeapBase& operator=(const HeapBase&) = delete;
103
104 RawHeap& raw_heap() { return raw_heap_; }
105 const RawHeap& raw_heap() const { return raw_heap_; }
106
107 cppgc::Platform* platform() { return platform_.get(); }
108 const cppgc::Platform* platform() const { return platform_.get(); }
109
110 FatalOutOfMemoryHandler& oom_handler() { return *oom_handler_.get(); }
112 return *oom_handler_.get();
113 }
114
115 PageBackend* page_backend() { return page_backend_.get(); }
116 const PageBackend* page_backend() const { return page_backend_.get(); }
117
120 return stats_collector_.get();
121 }
122
124 return prefinalizer_handler_.get();
125 }
127 return prefinalizer_handler_.get();
128 }
129
130 MarkerBase* marker() const { return marker_.get(); }
131 std::unique_ptr<MarkerBase>& GetMarkerRefForTesting() { return marker_; }
132
133 Compactor& compactor() { return compactor_; }
134
135 ObjectAllocator& object_allocator() { return object_allocator_; }
136 const ObjectAllocator& object_allocator() const { return object_allocator_; }
137
138 Sweeper& sweeper() { return sweeper_; }
139 const Sweeper& sweeper() const { return sweeper_; }
140
142 return strong_persistent_region_;
143 }
145 return strong_persistent_region_;
146 }
148 return weak_persistent_region_;
149 }
151 return weak_persistent_region_;
152 }
154 return strong_cross_thread_persistent_region_;
155 }
157 const {
158 return strong_cross_thread_persistent_region_;
159 }
161 return weak_cross_thread_persistent_region_;
162 }
164 const {
165 return weak_cross_thread_persistent_region_;
166 }
167
168#if defined(CPPGC_YOUNG_GENERATION)
169 OldToNewRememberedSet& remembered_set() { return remembered_set_; }
170#endif // defined(CPPGC_YOUNG_GENERATION)
171
172 size_t ObjectPayloadSize() const;
173
174 virtual heap::base::Stack* stack() { return stack_.get(); }
175
176 StackSupport stack_support() const { return stack_support_; }
177
178 // These virtual methods are also present in class GarbageCollector.
181
182 // Termination drops all roots (clears them out) and runs garbage collections
183 // in a bounded fixed point loop until no new objects are created in
184 // destructors. Exceeding the loop bound results in a crash.
185 void Terminate();
186
187 virtual bool IsGCForbidden() const;
188 bool in_atomic_pause() const { return in_atomic_pause_; }
189
191
193 return stack_state_of_prev_gc_;
194 }
196 stack_state_of_prev_gc_ = stack_state;
197 }
198
199 void SetInAtomicPauseForTesting(bool value) { in_atomic_pause_ = value; }
200
204
205 void SetMetricRecorder(std::unique_ptr<MetricRecorder> histogram_recorder) {
206 stats_collector_->SetMetricRecorder(std::move(histogram_recorder));
207 }
208
209 virtual bool CurrentThreadIsHeapThread() const;
210
211 MarkingType marking_support() const { return marking_support_; }
212 SweepingType sweeping_support() const { return sweeping_support_; }
213
215 return marking_support_ != MarkingType::kAtomic;
216 }
217
219 const bool supported = is_young_generation_enabled();
220#if defined(CPPGC_YOUNG_GENERATION)
221 DCHECK_IMPLIES(supported, YoungGenerationEnabler::IsEnabled());
222#endif // defined(CPPGC_YOUNG_GENERATION)
223 return supported;
224 }
225
227 return generational_gc_supported() ? StickyBits::kEnabled
228 : StickyBits::kDisabled;
229 }
230
231 // Returns whether objects should derive their name from C++ class names. Also
232 // requires build-time support through `CPPGC_SUPPORTS_OBJECT_NAMES`.
234 return name_for_unnamed_object_;
235 }
237 name_for_unnamed_object_ = value;
238 }
239
240 // Callback support so that other components can listen to when objects are
241 // moved.
242 bool HasMoveListeners() const { return !move_listeners_.empty(); }
243 void CallMoveListeners(Address from, Address to,
244 size_t size_including_header);
245 void RegisterMoveListener(MoveListener* listener);
246 void UnregisterMoveListener(MoveListener* listener);
247
249 is_incremental_marking_in_progress_ = value;
250 }
251
252 void EnterNoGCScope() { ++no_gc_scope_; }
254 DCHECK_GT(no_gc_scope_, 0);
255 --no_gc_scope_;
256 }
257
258 void EnterDisallowGCScope() { ++disallow_gc_scope_; }
260 DCHECK_GT(disallow_gc_scope_, 0);
261 --disallow_gc_scope_;
262 }
263
264 using HeapHandle::is_incremental_marking_in_progress;
265
266 protected:
267 static std::unique_ptr<PageBackend> InitializePageBackend(
268 PageAllocator& allocator);
269
270 // Used by the incremental scheduler to finalize a GC if supported.
273
274 virtual bool IsGCAllowed() const;
275
276 bool in_no_gc_scope() const { return no_gc_scope_ > 0; }
277
278 bool IsMarking() const { return marker_.get(); }
279
280 // Returns amount of bytes allocated while executing prefinalizers.
281 size_t ExecutePreFinalizers();
282
283#if defined(CPPGC_YOUNG_GENERATION)
284 void EnableGenerationalGC();
285 void ResetRememberedSet();
286#endif // defined(CPPGC_YOUNG_GENERATION)
287
288 PageAllocator* page_allocator() const;
289
290 // This field should be first so that it is initialized first at heap creation
291 // and is available upon initialization of other fields.
292 int heap_thread_id_ = v8::base::OS::GetCurrentThreadId();
293
295 std::shared_ptr<cppgc::Platform> platform_;
296 std::unique_ptr<FatalOutOfMemoryHandler> oom_handler_;
297
298#if defined(LEAK_SANITIZER)
299 std::unique_ptr<v8::base::LsanPageAllocator> lsan_page_allocator_;
300#endif // LEAK_SANITIZER
301
302 std::unique_ptr<PageBackend> page_backend_;
303
304 // HeapRegistry requires access to page_backend_.
305 HeapRegistry::Subscription heap_registry_subscription_{*this};
306
307 std::unique_ptr<StatsCollector> stats_collector_;
308 std::unique_ptr<heap::base::Stack> stack_;
309 std::unique_ptr<PreFinalizerHandler> prefinalizer_handler_;
310 std::unique_ptr<MarkerBase> marker_;
311
315
320
323#if defined(CPPGC_YOUNG_GENERATION)
324 OldToNewRememberedSet remembered_set_;
325#endif // defined(CPPGC_YOUNG_GENERATION)
326
327 size_t no_gc_scope_ = 0;
328 size_t disallow_gc_scope_ = 0;
329
331 EmbedderStackState stack_state_of_prev_gc_ =
332 EmbedderStackState::kNoHeapPointers;
333
334 bool in_atomic_pause_ = false;
335
336
339
340 HeapObjectNameForUnnamedObject name_for_unnamed_object_ =
341 HeapObjectNameForUnnamedObject::kUseHiddenName;
342
343 std::vector<MoveListener*> move_listeners_;
344
347 friend class cppgc::testing::Heap;
348};
349
359
360} // namespace internal
361} // namespace cppgc
362
363#endif // V8_HEAP_CPPGC_HEAP_BASE_H_
MarkingType
Definition heap.h:60
StackSupport
Definition heap.h:45
SweepingType
Definition heap.h:80
const HeapObjectNameForUnnamedObject saved_heap_object_name_value_
Definition heap-base.h:357
SweepingType sweeping_support_
Definition heap-base.h:338
const CrossThreadPersistentRegion & GetWeakCrossThreadPersistentRegion() const
Definition heap-base.h:163
virtual void FinalizeIncrementalGarbageCollectionIfNeeded(cppgc::Heap::StackState)=0
PersistentRegion & GetWeakPersistentRegion()
Definition heap-base.h:147
std::vector< MoveListener * > move_listeners_
Definition heap-base.h:343
std::unique_ptr< StatsCollector > stats_collector_
Definition heap-base.h:307
virtual void clear_overridden_stack_state()=0
std::unique_ptr< heap::base::Stack > stack_
Definition heap-base.h:308
HeapObjectNameForUnnamedObject name_of_unnamed_object() const
Definition heap-base.h:233
PersistentRegion weak_persistent_region_
Definition heap-base.h:317
SweepingType sweeping_support() const
Definition heap-base.h:212
EmbedderStackState stack_state_of_prev_gc() const
Definition heap-base.h:192
const PersistentRegion & GetWeakPersistentRegion() const
Definition heap-base.h:150
StackSupport stack_support() const
Definition heap-base.h:176
ObjectAllocator object_allocator_
Definition heap-base.h:313
const PageBackend * page_backend() const
Definition heap-base.h:116
PreFinalizerHandler * prefinalizer_handler()
Definition heap-base.h:123
static const HeapBase & From(const cppgc::HeapHandle &heap_handle)
Definition heap-base.h:91
virtual void set_override_stack_state(EmbedderStackState state)=0
const StackSupport stack_support_
Definition heap-base.h:330
std::shared_ptr< cppgc::Platform > platform_
Definition heap-base.h:295
bool incremental_marking_supported() const
Definition heap-base.h:214
std::unique_ptr< PreFinalizerHandler > prefinalizer_handler_
Definition heap-base.h:309
const CrossThreadPersistentRegion & GetStrongCrossThreadPersistentRegion() const
Definition heap-base.h:156
bool generational_gc_supported() const
Definition heap-base.h:218
std::unique_ptr< FatalOutOfMemoryHandler > oom_handler_
Definition heap-base.h:296
virtual void StartIncrementalGarbageCollectionForTesting()=0
bool in_atomic_pause() const
Definition heap-base.h:188
PersistentRegion & GetStrongPersistentRegion()
Definition heap-base.h:141
const PreFinalizerHandler * prefinalizer_handler() const
Definition heap-base.h:126
HeapBase & operator=(const HeapBase &)=delete
HeapBase(const HeapBase &)=delete
std::unique_ptr< MarkerBase > & GetMarkerRefForTesting()
Definition heap-base.h:131
std::unique_ptr< PageBackend > page_backend_
Definition heap-base.h:302
virtual void FinalizeIncrementalGarbageCollectionForTesting(EmbedderStackState)=0
Compactor & compactor()
Definition heap-base.h:133
void SetInAtomicPauseForTesting(bool value)
Definition heap-base.h:199
StatsCollector * stats_collector()
Definition heap-base.h:118
const Sweeper & sweeper() const
Definition heap-base.h:139
CrossThreadPersistentRegion weak_cross_thread_persistent_region_
Definition heap-base.h:319
MarkingType marking_support() const
Definition heap-base.h:211
CrossThreadPersistentRegion strong_cross_thread_persistent_region_
Definition heap-base.h:318
FatalOutOfMemoryHandler & oom_handler()
Definition heap-base.h:110
CrossThreadPersistentRegion & GetStrongCrossThreadPersistentRegion()
Definition heap-base.h:153
const FatalOutOfMemoryHandler & oom_handler() const
Definition heap-base.h:111
void set_name_of_unnamed_object(HeapObjectNameForUnnamedObject value)
Definition heap-base.h:236
const PersistentRegion & GetStrongPersistentRegion() const
Definition heap-base.h:144
bool HasMoveListeners() const
Definition heap-base.h:242
const StatsCollector * stats_collector() const
Definition heap-base.h:119
StickyBits sticky_bits() const
Definition heap-base.h:226
PersistentRegion strong_persistent_region_
Definition heap-base.h:316
const cppgc::Platform * platform() const
Definition heap-base.h:108
CrossThreadPersistentRegion & GetWeakCrossThreadPersistentRegion()
Definition heap-base.h:160
std::unique_ptr< MarkerBase > marker_
Definition heap-base.h:310
ProcessHeapStatisticsUpdater::AllocationObserverImpl allocation_observer_for_PROCESS_HEAP_STATISTICS_
Definition heap-base.h:322
MarkingType marking_support_
Definition heap-base.h:337
const RawHeap & raw_heap() const
Definition heap-base.h:105
static HeapBase & From(cppgc::HeapHandle &heap_handle)
Definition heap-base.h:88
MarkerBase * marker() const
Definition heap-base.h:130
void SetStackStateOfPrevGC(EmbedderStackState stack_state)
Definition heap-base.h:195
void SetMetricRecorder(std::unique_ptr< MetricRecorder > histogram_recorder)
Definition heap-base.h:205
cppgc::Platform * platform()
Definition heap-base.h:107
virtual heap::base::Stack * stack()
Definition heap-base.h:174
ObjectAllocator & object_allocator()
Definition heap-base.h:135
void set_incremental_marking_in_progress(bool value)
Definition heap-base.h:248
const ObjectAllocator & object_allocator() const
Definition heap-base.h:136
PageBackend * page_backend()
Definition heap-base.h:115
bool in_no_gc_scope() const
Definition heap-base.h:276
virtual void OnMove(Address from, Address to, size_t size_including_header)=0
static int GetCurrentThreadId()
Definition platform.cc:29
v8::Platform * platform_
Definition cpp-heap.cc:193
StatsCollector * stats_collector_
Definition sweeper.cc:595
uint8_t * Address
Definition globals.h:17
EmbedderStackState
Definition common.h:15
#define DCHECK_IMPLIES(v1, v2)
Definition logging.h:493
#define DCHECK_GT(v1, v2)
Definition logging.h:487
#define V8_EXPORT_PRIVATE
Definition macros.h:460
#define V8_NODISCARD
Definition v8config.h:693
std::unique_ptr< ValueMirror > value