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"
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)
38 : custom_spaces(std::move(custom_spaces)) {}
39
42
43 std::vector<std::unique_ptr<cppgc::CustomSpaceBase>> custom_spaces;
48 cppgc::Heap::MarkingType marking_support =
54 cppgc::Heap::SweepingType sweeping_support =
60 std::optional<cppgc::StackStartMarker> stack_start_marker = std::nullopt;
61};
62
71 public:
72 static std::unique_ptr<CppHeap> Create(v8::Platform* platform,
73 const CppHeapCreateParams& params);
74
75 virtual ~CppHeap() = default;
76
81 cppgc::AllocationHandle& GetAllocationHandle();
82
88
95 V8_DEPRECATED("Terminate gets automatically called in the CppHeap destructor")
96 void Terminate();
97
104 cppgc::HeapStatistics CollectStatistics(
105 cppgc::HeapStatistics::DetailLevel detail_level);
106
113 void CollectCustomSpaceStatisticsAtLastGC(
114 std::vector<cppgc::CustomSpaceIndex> custom_spaces,
115 std::unique_ptr<CustomSpaceStatisticsReceiver> receiver);
116
122 void EnableDetachedGarbageCollectionsForTesting();
123
129 void CollectGarbageForTesting(cppgc::EmbedderStackState stack_state);
130
136 void CollectGarbageInYoungGenerationForTesting(
137 cppgc::EmbedderStackState stack_state);
138
139 private:
140 CppHeap() = default;
141
142 friend class internal::CppHeap;
143};
144
145class JSVisitor : public cppgc::Visitor {
146 public:
147 explicit JSVisitor(cppgc::Visitor::Key key) : cppgc::Visitor(key) {}
148 ~JSVisitor() override = default;
149
150 void Trace(const TracedReferenceBase& ref) {
151 if (ref.IsEmptyThreadSafe()) return;
152 Visit(ref);
153 }
154
155 protected:
157
158 virtual void Visit(const TracedReferenceBase& ref) {}
159};
160
167 public:
177 virtual void AllocatedBytes(cppgc::CustomSpaceIndex space_index,
178 size_t bytes) = 0;
179};
180
181} // namespace v8
182
183namespace cppgc {
184
185template <typename T>
186struct TraceTrait<v8::TracedReference<T>> {
188 return {nullptr, Trace};
189 }
190
191 static void Trace(Visitor* visitor, const void* self) {
192 static_cast<v8::JSVisitor*>(visitor)->Trace(
193 *static_cast<const v8::TracedReference<T>*>(self));
194 }
195};
196
197} // namespace cppgc
198
199#endif // INCLUDE_V8_CPPGC_H_
Definition: heap-handle.h:21
MarkingType
Definition: heap.h:72
SweepingType
Definition: heap.h:92
Definition: visitor.h:77
Definition: visitor.h:75
virtual void Visit(const void *self, TraceDescriptor)
Definition: visitor.h:379
Definition: v8-cppgc.h:70
virtual ~CppHeap()=default
static std::unique_ptr< CppHeap > Create(v8::Platform *platform, const CppHeapCreateParams &params)
cppgc::HeapHandle & GetHeapHandle()
cppgc::AllocationHandle & GetAllocationHandle()
Definition: v8-cppgc.h:166
virtual void AllocatedBytes(cppgc::CustomSpaceIndex space_index, size_t bytes)=0
virtual ~CustomSpaceStatisticsReceiver()=default
Definition: v8-statistics.h:140
Definition: v8-cppgc.h:145
JSVisitor(cppgc::Visitor::Key key)
Definition: v8-cppgc.h:147
~JSVisitor() override=default
virtual void Visit(const TracedReferenceBase &ref)
Definition: v8-cppgc.h:158
void Trace(const TracedReferenceBase &ref)
Definition: v8-cppgc.h:150
Definition: v8-platform.h:1224
Definition: v8-traced-handle.h:54
bool IsEmptyThreadSafe() const
Definition: v8-traced-handle.h:77
Definition: v8-traced-handle.h:165
Definition: allocation.h:38
Definition: libplatform.h:15
Definition: custom-space.h:15
Definition: trace-trait.h:43
static cppgc::TraceDescriptor GetTraceDescriptor(const void *self)
Definition: v8-cppgc.h:187
static void Trace(Visitor *visitor, const void *self)
Definition: v8-cppgc.h:191
Definition: trace-trait.h:104
Definition: v8-cppgc.h:35
std::vector< std::unique_ptr< cppgc::CustomSpaceBase > > custom_spaces
Definition: v8-cppgc.h:43
CppHeapCreateParams(const CppHeapCreateParams &)=delete
CppHeapCreateParams(std::vector< std::unique_ptr< cppgc::CustomSpaceBase > > custom_spaces)
Definition: v8-cppgc.h:36
CppHeapCreateParams & operator=(const CppHeapCreateParams &)=delete
#define V8_EXPORT
Definition: v8config.h:854
#define V8_DEPRECATED(message)
Definition: v8config.h:613