/src/node/deps/v8/include/v8-cppgc.h
Line  | Count  | Source  | 
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  |  |  | 
20  |  | namespace cppgc { | 
21  |  | class AllocationHandle;  | 
22  |  | class HeapHandle;  | 
23  |  | }  // namespace cppgc  | 
24  |  |  | 
25  |  | namespace v8 { | 
26  |  |  | 
27  |  | class Object;  | 
28  |  |  | 
29  |  | namespace internal { | 
30  |  | class CppHeap;  | 
31  |  | }  // namespace internal  | 
32  |  |  | 
33  |  | class CustomSpaceStatisticsReceiver;  | 
34  |  |  | 
35  |  | struct V8_EXPORT CppHeapCreateParams { | 
36  |  |   explicit CppHeapCreateParams(  | 
37  |  |       std::vector<std::unique_ptr<cppgc::CustomSpaceBase>> custom_spaces)  | 
38  | 35  |       : custom_spaces(std::move(custom_spaces)) {} | 
39  |  |  | 
40  |  |   CppHeapCreateParams(const CppHeapCreateParams&) = delete;  | 
41  |  |   CppHeapCreateParams& operator=(const CppHeapCreateParams&) = delete;  | 
42  |  |  | 
43  |  |   std::vector<std::unique_ptr<cppgc::CustomSpaceBase>> custom_spaces;  | 
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  |  |    */  | 
48  |  |   cppgc::Heap::MarkingType marking_support =  | 
49  |  |       cppgc::Heap::MarkingType::kIncrementalAndConcurrent;  | 
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  |  |    */  | 
54  |  |   cppgc::Heap::SweepingType sweeping_support =  | 
55  |  |       cppgc::Heap::SweepingType::kIncrementalAndConcurrent;  | 
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  |  |  */  | 
65  |  | class V8_EXPORT CppHeap { | 
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  |  |    */  | 
82  |  |   cppgc::HeapHandle& GetHeapHandle();  | 
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  |  |    */  | 
108  |  |   void CollectCustomSpaceStatisticsAtLastGC(  | 
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  |  |    */  | 
117  |  |   void EnableDetachedGarbageCollectionsForTesting();  | 
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  |  |    */  | 
124  |  |   void CollectGarbageForTesting(cppgc::EmbedderStackState stack_state);  | 
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  |  |    */  | 
131  |  |   void CollectGarbageInYoungGenerationForTesting(  | 
132  |  |       cppgc::EmbedderStackState stack_state);  | 
133  |  |  | 
134  |  |  private:  | 
135  |  |   CppHeap() = default;  | 
136  |  |  | 
137  |  |   friend class internal::CppHeap;  | 
138  |  | };  | 
139  |  |  | 
140  |  | class JSVisitor : public cppgc::Visitor { | 
141  |  |  public:  | 
142  | 0  |   explicit JSVisitor(cppgc::Visitor::Key key) : cppgc::Visitor(key) {} | 
143  |  |   ~JSVisitor() override = default;  | 
144  |  |  | 
145  | 0  |   void Trace(const TracedReferenceBase& ref) { | 
146  | 0  |     if (ref.IsEmptyThreadSafe()) return;  | 
147  | 0  |     Visit(ref);  | 
148  | 0  |   }  | 
149  |  |  | 
150  |  |  protected:  | 
151  |  |   using cppgc::Visitor::Visit;  | 
152  |  |  | 
153  | 0  |   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  |  |  */  | 
161  |  | class CustomSpaceStatisticsReceiver { | 
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  |  |  | 
178  |  | namespace cppgc { | 
179  |  |  | 
180  |  | template <typename T>  | 
181  |  | struct TraceTrait<v8::TracedReference<T>> { | 
182  |  |   static cppgc::TraceDescriptor GetTraceDescriptor(const void* self) { | 
183  |  |     return {nullptr, Trace}; | 
184  |  |   }  | 
185  |  |  | 
186  | 0  |   static void Trace(Visitor* visitor, const void* self) { | 
187  | 0  |     static_cast<v8::JSVisitor*>(visitor)->Trace(  | 
188  | 0  |         *static_cast<const v8::TracedReference<T>*>(self));  | 
189  | 0  |   } Unexecuted instantiation: cppgc::TraceTrait<v8::TracedReference<v8::Object> >::Trace(cppgc::Visitor*, void const*) Unexecuted instantiation: cppgc::TraceTrait<v8::TracedReference<v8::Context> >::Trace(cppgc::Visitor*, void const*) Unexecuted instantiation: cppgc::TraceTrait<v8::TracedReference<v8::UnboundScript> >::Trace(cppgc::Visitor*, void const*)  | 
190  |  | };  | 
191  |  |  | 
192  |  | }  // namespace cppgc  | 
193  |  |  | 
194  |  | #endif  // INCLUDE_V8_CPPGC_H_  |