File indexing completed on 2025-02-21 10:05:23
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
0036
0037
0038
0039 struct WrapperDescriptor final {
0040
0041
0042
0043
0044
0045 using InternalFieldIndex = int;
0046
0047
0048
0049
0050
0051 static constexpr uint16_t kUnknownEmbedderId = UINT16_MAX;
0052
0053 constexpr WrapperDescriptor(InternalFieldIndex wrappable_type_index,
0054 InternalFieldIndex wrappable_instance_index,
0055 uint16_t embedder_id_for_garbage_collected)
0056 : wrappable_type_index(wrappable_type_index),
0057 wrappable_instance_index(wrappable_instance_index),
0058 embedder_id_for_garbage_collected(embedder_id_for_garbage_collected) {}
0059
0060
0061
0062
0063 InternalFieldIndex wrappable_type_index;
0064
0065
0066
0067
0068 InternalFieldIndex wrappable_instance_index;
0069
0070
0071
0072
0073
0074
0075
0076 uint16_t embedder_id_for_garbage_collected;
0077 };
0078
0079 struct V8_EXPORT CppHeapCreateParams {
0080 CppHeapCreateParams(
0081 std::vector<std::unique_ptr<cppgc::CustomSpaceBase>> custom_spaces,
0082 WrapperDescriptor wrapper_descriptor)
0083 : custom_spaces(std::move(custom_spaces)),
0084 wrapper_descriptor(wrapper_descriptor) {}
0085
0086 CppHeapCreateParams(const CppHeapCreateParams&) = delete;
0087 CppHeapCreateParams& operator=(const CppHeapCreateParams&) = delete;
0088
0089 std::vector<std::unique_ptr<cppgc::CustomSpaceBase>> custom_spaces;
0090 WrapperDescriptor wrapper_descriptor;
0091
0092
0093
0094
0095 cppgc::Heap::MarkingType marking_support =
0096 cppgc::Heap::MarkingType::kIncrementalAndConcurrent;
0097
0098
0099
0100
0101 cppgc::Heap::SweepingType sweeping_support =
0102 cppgc::Heap::SweepingType::kIncrementalAndConcurrent;
0103 };
0104
0105
0106
0107
0108
0109
0110
0111
0112 class V8_EXPORT CppHeap {
0113 public:
0114 static std::unique_ptr<CppHeap> Create(v8::Platform* platform,
0115 const CppHeapCreateParams& params);
0116
0117 virtual ~CppHeap() = default;
0118
0119
0120
0121
0122
0123 cppgc::AllocationHandle& GetAllocationHandle();
0124
0125
0126
0127
0128
0129 cppgc::HeapHandle& GetHeapHandle();
0130
0131
0132
0133
0134
0135
0136
0137 void Terminate();
0138
0139
0140
0141
0142
0143
0144
0145 cppgc::HeapStatistics CollectStatistics(
0146 cppgc::HeapStatistics::DetailLevel detail_level);
0147
0148
0149
0150
0151
0152
0153
0154 void CollectCustomSpaceStatisticsAtLastGC(
0155 std::vector<cppgc::CustomSpaceIndex> custom_spaces,
0156 std::unique_ptr<CustomSpaceStatisticsReceiver> receiver);
0157
0158
0159
0160
0161
0162
0163 void EnableDetachedGarbageCollectionsForTesting();
0164
0165
0166
0167
0168
0169
0170 void CollectGarbageForTesting(cppgc::EmbedderStackState stack_state);
0171
0172
0173
0174
0175
0176
0177 void CollectGarbageInYoungGenerationForTesting(
0178 cppgc::EmbedderStackState stack_state);
0179
0180
0181
0182
0183 v8::WrapperDescriptor wrapper_descriptor() const;
0184
0185 private:
0186 CppHeap() = default;
0187
0188 friend class internal::CppHeap;
0189 };
0190
0191 class JSVisitor : public cppgc::Visitor {
0192 public:
0193 explicit JSVisitor(cppgc::Visitor::Key key) : cppgc::Visitor(key) {}
0194 ~JSVisitor() override = default;
0195
0196 void Trace(const TracedReferenceBase& ref) {
0197 if (ref.IsEmptyThreadSafe()) return;
0198 Visit(ref);
0199 }
0200
0201 protected:
0202 using cppgc::Visitor::Visit;
0203
0204 virtual void Visit(const TracedReferenceBase& ref) {}
0205 };
0206
0207
0208
0209
0210
0211
0212 class CustomSpaceStatisticsReceiver {
0213 public:
0214 virtual ~CustomSpaceStatisticsReceiver() = default;
0215
0216
0217
0218
0219
0220
0221
0222
0223 virtual void AllocatedBytes(cppgc::CustomSpaceIndex space_index,
0224 size_t bytes) = 0;
0225 };
0226
0227 }
0228
0229 namespace cppgc {
0230
0231 template <typename T>
0232 struct TraceTrait<v8::TracedReference<T>> {
0233 static cppgc::TraceDescriptor GetTraceDescriptor(const void* self) {
0234 return {nullptr, Trace};
0235 }
0236
0237 static void Trace(Visitor* visitor, const void* self) {
0238 static_cast<v8::JSVisitor*>(visitor)->Trace(
0239 *static_cast<const v8::TracedReference<T>*>(self));
0240 }
0241 };
0242
0243 }
0244
0245 #endif