v8  9.4.146 (node 16.13.0)
V8 is Google's open source JavaScript engine
heap.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_CPPGC_HEAP_H_
6 #define INCLUDE_CPPGC_HEAP_H_
7 
8 #include <cstddef>
9 #include <cstdint>
10 #include <memory>
11 #include <vector>
12 
13 #include "cppgc/common.h"
14 #include "cppgc/custom-space.h"
15 #include "cppgc/platform.h"
16 #include "v8config.h" // NOLINT(build/include_directory)
17 
18 /**
19  * cppgc - A C++ garbage collection library.
20  */
21 namespace cppgc {
22 
23 class AllocationHandle;
24 
25 /**
26  * Implementation details of cppgc. Those details are considered internal and
27  * may change at any point in time without notice. Users should never rely on
28  * the contents of this namespace.
29  */
30 namespace internal {
31 class Heap;
32 } // namespace internal
33 
34 /**
35  * Used for additional heap APIs.
36  */
37 class HeapHandle;
38 
39 class V8_EXPORT Heap {
40  public:
41  /**
42  * Specifies the stack state the embedder is in.
43  */
44  using StackState = EmbedderStackState;
45 
46  /**
47  * Specifies whether conservative stack scanning is supported.
48  */
49  enum class StackSupport : uint8_t {
50  /**
51  * Conservative stack scan is supported.
52  */
54  /**
55  * Conservative stack scan is not supported. Embedders may use this option
56  * when using custom infrastructure that is unsupported by the library.
57  */
59  };
60 
61  /**
62  * Specifies supported marking types
63  */
64  enum class MarkingType : uint8_t {
65  /**
66  * Atomic stop-the-world marking. This option does not require any write
67  * barriers but is the most intrusive in terms of jank.
68  */
69  kAtomic,
70  /**
71  * Incremental marking, i.e. interleave marking is the rest of the
72  * application on the same thread.
73  */
75  /**
76  * Incremental and concurrent marking.
77  */
79  };
80 
81  /**
82  * Specifies supported sweeping types
83  */
84  enum class SweepingType : uint8_t {
85  /**
86  * Atomic stop-the-world sweeping. All of sweeping is performed at once.
87  */
88  kAtomic,
89  /**
90  * Incremental and concurrent sweeping. Sweeping is split and interleaved
91  * with the rest of the application.
92  */
94  };
95 
96  /**
97  * Constraints for a Heap setup.
98  */
100  /**
101  * Allows the heap to grow to some initial size in bytes before triggering
102  * garbage collections. This is useful when it is known that applications
103  * need a certain minimum heap to run to avoid repeatedly invoking the
104  * garbage collector when growing the heap.
105  */
107  };
108 
109  /**
110  * Options specifying Heap properties (e.g. custom spaces) when initializing a
111  * heap through `Heap::Create()`.
112  */
113  struct HeapOptions {
114  /**
115  * Creates reasonable defaults for instantiating a Heap.
116  *
117  * \returns the HeapOptions that can be passed to `Heap::Create()`.
118  */
119  static HeapOptions Default() { return {}; }
120 
121  /**
122  * Custom spaces added to heap are required to have indices forming a
123  * numbered sequence starting at 0, i.e., their `kSpaceIndex` must
124  * correspond to the index they reside in the vector.
125  */
126  std::vector<std::unique_ptr<CustomSpaceBase>> custom_spaces;
127 
128  /**
129  * Specifies whether conservative stack scan is supported. When conservative
130  * stack scan is not supported, the collector may try to invoke
131  * garbage collections using non-nestable task, which are guaranteed to have
132  * no interesting stack, through the provided Platform. If such tasks are
133  * not supported by the Platform, the embedder must take care of invoking
134  * the GC through `ForceGarbageCollectionSlow()`.
135  */
137 
138  /**
139  * Specifies which types of marking are supported by the heap.
140  */
142 
143  /**
144  * Specifies which types of sweeping are supported by the heap.
145  */
147 
148  /**
149  * Resource constraints specifying various properties that the internal
150  * GC scheduler follows.
151  */
153  };
154 
155  /**
156  * Creates a new heap that can be used for object allocation.
157  *
158  * \param platform implemented and provided by the embedder.
159  * \param options HeapOptions specifying various properties for the Heap.
160  * \returns a new Heap instance.
161  */
162  static std::unique_ptr<Heap> Create(
163  std::shared_ptr<Platform> platform,
165 
166  virtual ~Heap() = default;
167 
168  /**
169  * Forces garbage collection.
170  *
171  * \param source String specifying the source (or caller) triggering a
172  * forced garbage collection.
173  * \param reason String specifying the reason for the forced garbage
174  * collection.
175  * \param stack_state The embedder stack state, see StackState.
176  */
178  const char* source, const char* reason,
179  StackState stack_state = StackState::kMayContainHeapPointers);
180 
181  /**
182  * \returns the opaque handle for allocating objects using
183  * `MakeGarbageCollected()`.
184  */
185  AllocationHandle& GetAllocationHandle();
186 
187  /**
188  * \returns the opaque heap handle which may be used to refer to this heap in
189  * other APIs. Valid as long as the underlying `Heap` is alive.
190  */
191  HeapHandle& GetHeapHandle();
192 
193  private:
194  Heap() = default;
195 
196  friend class internal::Heap;
197 };
198 
199 } // namespace cppgc
200 
201 #endif // INCLUDE_CPPGC_HEAP_H_