File indexing completed on 2026-09-11 09:15:53
0001
0002
0003
0004
0005 #ifndef INCLUDE_V8_CPPGC_H_
0006 #define INCLUDE_V8_CPPGC_H_
0007
0008 #include <cstdint>
0009 #include <memory>
0010 #include <vector>
0011
0012 #include "cppgc/common.h"
0013 #include "cppgc/custom-space.h"
0014 #include "cppgc/heap-statistics.h"
0015 #include "cppgc/visitor.h"
0016 #include "v8-internal.h" // NOLINT(build/include_directory)
0017 #include "v8-platform.h" // NOLINT(build/include_directory)
0018 #include "v8-traced-handle.h" // NOLINT(build/include_directory)
0019
0020 namespace cppgc {
0021 class AllocationHandle;
0022 class HeapHandle;
0023 }
0024
0025 namespace v8 {
0026
0027 class Object;
0028
0029 namespace internal {
0030 class CppHeap;
0031 }
0032
0033 class CustomSpaceStatisticsReceiver;
0034
0035 struct V8_EXPORT CppHeapCreateParams {
0036 explicit CppHeapCreateParams(
0037 std::vector<std::unique_ptr<cppgc::CustomSpaceBase>> custom_spaces)
0038 : custom_spaces(std::move(custom_spaces)) {}
0039
0040 CppHeapCreateParams(const CppHeapCreateParams&) = delete;
0041 CppHeapCreateParams& operator=(const CppHeapCreateParams&) = delete;
0042
0043 std::vector<std::unique_ptr<cppgc::CustomSpaceBase>> custom_spaces;
0044
0045
0046
0047
0048 cppgc::Heap::MarkingType marking_support =
0049 cppgc::Heap::MarkingType::kIncrementalAndConcurrent;
0050
0051
0052
0053
0054 cppgc::Heap::SweepingType sweeping_support =
0055 cppgc::Heap::SweepingType::kIncrementalAndConcurrent;
0056 };
0057
0058
0059
0060
0061
0062
0063
0064
0065 class V8_EXPORT CppHeap {
0066 public:
0067 static std::unique_ptr<CppHeap> Create(v8::Platform* platform,
0068 const CppHeapCreateParams& params);
0069
0070 virtual ~CppHeap() = default;
0071
0072
0073
0074
0075
0076 cppgc::AllocationHandle& GetAllocationHandle();
0077
0078
0079
0080
0081
0082 cppgc::HeapHandle& GetHeapHandle();
0083
0084
0085
0086
0087
0088
0089
0090 V8_DEPRECATED("Terminate gets automatically called in the CppHeap destructor")
0091 void Terminate();
0092
0093
0094
0095
0096
0097
0098
0099 cppgc::HeapStatistics CollectStatistics(
0100 cppgc::HeapStatistics::DetailLevel detail_level);
0101
0102
0103
0104
0105
0106
0107
0108 void CollectCustomSpaceStatisticsAtLastGC(
0109 std::vector<cppgc::CustomSpaceIndex> custom_spaces,
0110 std::unique_ptr<CustomSpaceStatisticsReceiver> receiver);
0111
0112
0113
0114
0115
0116
0117 void EnableDetachedGarbageCollectionsForTesting();
0118
0119
0120
0121
0122
0123
0124 void CollectGarbageForTesting(cppgc::EmbedderStackState stack_state);
0125
0126
0127
0128
0129
0130
0131 void CollectGarbageInYoungGenerationForTesting(
0132 cppgc::EmbedderStackState stack_state);
0133
0134 private:
0135 CppHeap() = default;
0136
0137 friend class internal::CppHeap;
0138 };
0139
0140 class JSVisitor : public cppgc::Visitor {
0141 public:
0142 explicit JSVisitor(cppgc::Visitor::Key key) : cppgc::Visitor(key) {}
0143 ~JSVisitor() override = default;
0144
0145 void Trace(const TracedReferenceBase& ref) {
0146 if (ref.IsEmptyThreadSafe()) return;
0147 Visit(ref);
0148 }
0149
0150 protected:
0151 using cppgc::Visitor::Visit;
0152
0153 virtual void Visit(const TracedReferenceBase& ref) {}
0154 };
0155
0156
0157
0158
0159
0160
0161 class CustomSpaceStatisticsReceiver {
0162 public:
0163 virtual ~CustomSpaceStatisticsReceiver() = default;
0164
0165
0166
0167
0168
0169
0170
0171
0172 virtual void AllocatedBytes(cppgc::CustomSpaceIndex space_index,
0173 size_t bytes) = 0;
0174 };
0175
0176 }
0177
0178 namespace cppgc {
0179
0180 template <typename T>
0181 struct TraceTrait<v8::TracedReference<T>> {
0182 static cppgc::TraceDescriptor GetTraceDescriptor(const void* self) {
0183 return {nullptr, Trace};
0184 }
0185
0186 static void Trace(Visitor* visitor, const void* self) {
0187 static_cast<v8::JSVisitor*>(visitor)->Trace(
0188 *static_cast<const v8::TracedReference<T>*>(self));
0189 }
0190 };
0191
0192 }
0193
0194 #endif