v8 14.1.146 (node 25.0.0)
V8 is Google's open source JavaScript engine
Loading...
Searching...
No Matches
v8-cppgc.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 INCLUDE_V8_CPPGC_H_
6#define INCLUDE_V8_CPPGC_H_
7
8#include <cstdint>
9#include <memory>
10#include <vector>
11
12#include "cppgc/common.h"
13#include "cppgc/custom-space.h"
14#include "cppgc/heap-statistics.h"
15#include "cppgc/visitor.h"
16#include "v8-internal.h" // NOLINT(build/include_directory)
17#include "v8-platform.h" // NOLINT(build/include_directory)
18#include "v8-traced-handle.h" // NOLINT(build/include_directory)
19
20namespace cppgc {
21class AllocationHandle;
22class HeapHandle;
23} // namespace cppgc
24
25namespace v8 {
26
27class Object;
28
29namespace internal {
30class CppHeap;
31} // namespace internal
32
34
37 std::vector<std::unique_ptr<cppgc::CustomSpaceBase>> custom_spaces)
39
42
44 /**
45 * Specifies which kind of marking are supported by the heap. The type may be
46 * further reduced via runtime flags when attaching the heap to an Isolate.
47 */
50 /**
51 * Specifies which kind of sweeping is supported by the heap. The type may be
52 * further reduced via runtime flags when attaching the heap to an Isolate.
53 */
56};
57
58/**
59 * A heap for allocating managed C++ objects.
60 *
61 * Similar to v8::Isolate, the heap may only be accessed from one thread at a
62 * time. The heap may be used from different threads using the
63 * v8::Locker/v8::Unlocker APIs which is different from generic Oilpan.
64 */
66 public:
67 static std::unique_ptr<CppHeap> Create(v8::Platform* platform,
68 const CppHeapCreateParams& params);
69
70 virtual ~CppHeap() = default;
71
72 /**
73 * \returns the opaque handle for allocating objects using
74 * `MakeGarbageCollected()`.
75 */
76 cppgc::AllocationHandle& GetAllocationHandle();
77
78 /**
79 * \returns the opaque heap handle which may be used to refer to this heap in
80 * other APIs. Valid as long as the underlying `CppHeap` is alive.
81 */
83
84 /**
85 * Terminate clears all roots and performs multiple garbage collections to
86 * reclaim potentially newly created objects in destructors.
87 *
88 * After this call, object allocation is prohibited.
89 */
90 V8_DEPRECATED("Terminate gets automatically called in the CppHeap destructor")
91 void Terminate();
92
93 /**
94 * \param detail_level specifies whether should return detailed
95 * statistics or only brief summary statistics.
96 * \returns current CppHeap statistics regarding memory consumption
97 * and utilization.
98 */
99 cppgc::HeapStatistics CollectStatistics(
100 cppgc::HeapStatistics::DetailLevel detail_level);
101
102 /**
103 * Collects statistics for the given spaces and reports them to the receiver.
104 *
105 * \param custom_spaces a collection of custom space indices.
106 * \param receiver an object that gets the results.
107 */
109 std::vector<cppgc::CustomSpaceIndex> custom_spaces,
110 std::unique_ptr<CustomSpaceStatisticsReceiver> receiver);
111
112 /**
113 * Enables a detached mode that allows testing garbage collection using
114 * `cppgc::testing` APIs. Once used, the heap cannot be attached to an
115 * `Isolate` anymore.
116 */
118
119 /**
120 * Performs a stop-the-world garbage collection for testing purposes.
121 *
122 * \param stack_state The stack state to assume for the garbage collection.
123 */
125
126 /**
127 * Performs a stop-the-world minor garbage collection for testing purposes.
128 *
129 * \param stack_state The stack state to assume for the garbage collection.
130 */
132 cppgc::EmbedderStackState stack_state);
133
134 private:
135 CppHeap() = default;
136
137 friend class internal::CppHeap;
138};
139
140class JSVisitor : public cppgc::Visitor {
141 public:
142 explicit JSVisitor(cppgc::Visitor::Key key) : cppgc::Visitor(key) {}
143 ~JSVisitor() override = default;
144
145 void Trace(const TracedReferenceBase& ref) {
146 if (ref.IsEmptyThreadSafe()) return;
147 Visit(ref);
148 }
149
150 protected:
151 using cppgc::Visitor::Visit;
152
153 virtual void Visit(const TracedReferenceBase& ref) {}
154};
155
156/**
157 * Provided as input to `CppHeap::CollectCustomSpaceStatisticsAtLastGC()`.
158 *
159 * Its method is invoked with the results of the statistic collection.
160 */
162 public:
163 virtual ~CustomSpaceStatisticsReceiver() = default;
164 /**
165 * Reports the size of a space at the last GC. It is called for each space
166 * that was requested in `CollectCustomSpaceStatisticsAtLastGC()`.
167 *
168 * \param space_index The index of the space.
169 * \param bytes The total size of live objects in the space at the last GC.
170 * It is zero if there was no GC yet.
171 */
172 virtual void AllocatedBytes(cppgc::CustomSpaceIndex space_index,
173 size_t bytes) = 0;
174};
175
176} // namespace v8
177
178namespace cppgc {
179
180template <typename T>
182 static cppgc::TraceDescriptor GetTraceDescriptor(const void* self) {
183 return {nullptr, Trace};
184 }
185
186 static void Trace(Visitor* visitor, const void* self) {
187 static_cast<v8::JSVisitor*>(visitor)->Trace(
188 *static_cast<const v8::TracedReference<T>*>(self));
189 }
190};
191
192} // namespace cppgc
193
194#endif // INCLUDE_V8_CPPGC_H_
MarkingType
Definition heap.h:60
SweepingType
Definition heap.h:80
virtual ~CppHeap()=default
cppgc::HeapHandle & GetHeapHandle()
void EnableDetachedGarbageCollectionsForTesting()
cppgc::HeapStatistics CollectStatistics(cppgc::HeapStatistics::DetailLevel detail_level)
void CollectGarbageInYoungGenerationForTesting(cppgc::EmbedderStackState stack_state)
void CollectGarbageForTesting(cppgc::EmbedderStackState stack_state)
void Terminate()
void CollectCustomSpaceStatisticsAtLastGC(std::vector< cppgc::CustomSpaceIndex > custom_spaces, std::unique_ptr< CustomSpaceStatisticsReceiver > receiver)
cppgc::AllocationHandle & GetAllocationHandle()
static std::unique_ptr< CppHeap > Create(v8::Platform *platform, const CppHeapCreateParams &params)
virtual void AllocatedBytes(cppgc::CustomSpaceIndex space_index, size_t bytes)=0
virtual ~CustomSpaceStatisticsReceiver()=default
void Trace(const TracedReferenceBase &ref)
Definition v8-cppgc.h:145
~JSVisitor() override=default
virtual void Visit(const TracedReferenceBase &ref)
Definition v8-cppgc.h:153
JSVisitor(cppgc::Visitor::Key key)
Definition v8-cppgc.h:142
EmbedderStackState
Definition common.h:15
static void Trace(Visitor *visitor, const void *self)
Definition v8-cppgc.h:186
static cppgc::TraceDescriptor GetTraceDescriptor(const void *self)
Definition v8-cppgc.h:182
CppHeapCreateParams(const CppHeapCreateParams &)=delete
cppgc::Heap::SweepingType sweeping_support
Definition v8-cppgc.h:54
CppHeapCreateParams & operator=(const CppHeapCreateParams &)=delete
cppgc::Heap::MarkingType marking_support
Definition v8-cppgc.h:48
CppHeapCreateParams(std::vector< std::unique_ptr< cppgc::CustomSpaceBase > > custom_spaces)
Definition v8-cppgc.h:36
std::vector< std::unique_ptr< cppgc::CustomSpaceBase > > custom_spaces
Definition v8-cppgc.h:43
#define V8_EXPORT
Definition v8config.h:860
#define V8_DEPRECATED(message)
Definition v8config.h:619