Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-02-22 10:42:26

0001 // Copyright 2020 the V8 project authors. All rights reserved.
0002 // Use of this source code is governed by a BSD-style license that can be
0003 // found in the LICENSE file.
0004 
0005 #ifndef INCLUDE_CPPGC_HEAP_H_
0006 #define INCLUDE_CPPGC_HEAP_H_
0007 
0008 #include <cstddef>
0009 #include <cstdint>
0010 #include <memory>
0011 #include <vector>
0012 
0013 #include "cppgc/common.h"
0014 #include "cppgc/custom-space.h"
0015 #include "cppgc/platform.h"
0016 #include "v8config.h"  // NOLINT(build/include_directory)
0017 
0018 /**
0019  * cppgc - A C++ garbage collection library.
0020  */
0021 namespace cppgc {
0022 
0023 class AllocationHandle;
0024 class HeapHandle;
0025 
0026 /**
0027  * Implementation details of cppgc. Those details are considered internal and
0028  * may change at any point in time without notice. Users should never rely on
0029  * the contents of this namespace.
0030  */
0031 namespace internal {
0032 class Heap;
0033 }  // namespace internal
0034 
0035 class V8_EXPORT Heap {
0036  public:
0037   /**
0038    * Specifies the stack state the embedder is in.
0039    */
0040   using StackState = EmbedderStackState;
0041 
0042   /**
0043    * Specifies whether conservative stack scanning is supported.
0044    */
0045   enum class StackSupport : uint8_t {
0046     /**
0047      * Conservative stack scan is supported.
0048      */
0049     kSupportsConservativeStackScan,
0050     /**
0051      * Conservative stack scan is not supported. Embedders may use this option
0052      * when using custom infrastructure that is unsupported by the library.
0053      */
0054     kNoConservativeStackScan,
0055   };
0056 
0057   /**
0058    * Specifies supported marking types.
0059    */
0060   enum class MarkingType : uint8_t {
0061     /**
0062      * Atomic stop-the-world marking. This option does not require any write
0063      * barriers but is the most intrusive in terms of jank.
0064      */
0065     kAtomic,
0066     /**
0067      * Incremental marking interleaves marking with the rest of the application
0068      * workload on the same thread.
0069      */
0070     kIncremental,
0071     /**
0072      * Incremental and concurrent marking.
0073      */
0074     kIncrementalAndConcurrent
0075   };
0076 
0077   /**
0078    * Specifies supported sweeping types.
0079    */
0080   enum class SweepingType : uint8_t {
0081     /**
0082      * Atomic stop-the-world sweeping. All of sweeping is performed at once.
0083      */
0084     kAtomic,
0085     /**
0086      * Incremental sweeping interleaves sweeping with the rest of the
0087      * application workload on the same thread.
0088      */
0089     kIncremental,
0090     /**
0091      * Incremental and concurrent sweeping. Sweeping is split and interleaved
0092      * with the rest of the application.
0093      */
0094     kIncrementalAndConcurrent
0095   };
0096 
0097   /**
0098    * Constraints for a Heap setup.
0099    */
0100   struct ResourceConstraints {
0101     /**
0102      * Allows the heap to grow to some initial size in bytes before triggering
0103      * garbage collections. This is useful when it is known that applications
0104      * need a certain minimum heap to run to avoid repeatedly invoking the
0105      * garbage collector when growing the heap.
0106      */
0107     size_t initial_heap_size_bytes = 0;
0108   };
0109 
0110   /**
0111    * Options specifying Heap properties (e.g. custom spaces) when initializing a
0112    * heap through `Heap::Create()`.
0113    */
0114   struct HeapOptions {
0115     /**
0116      * Creates reasonable defaults for instantiating a Heap.
0117      *
0118      * \returns the HeapOptions that can be passed to `Heap::Create()`.
0119      */
0120     static HeapOptions Default() { return {}; }
0121 
0122     /**
0123      * Custom spaces added to heap are required to have indices forming a
0124      * numbered sequence starting at 0, i.e., their `kSpaceIndex` must
0125      * correspond to the index they reside in the vector.
0126      */
0127     std::vector<std::unique_ptr<CustomSpaceBase>> custom_spaces;
0128 
0129     /**
0130      * Specifies whether conservative stack scan is supported. When conservative
0131      * stack scan is not supported, the collector may try to invoke
0132      * garbage collections using non-nestable task, which are guaranteed to have
0133      * no interesting stack, through the provided Platform. If such tasks are
0134      * not supported by the Platform, the embedder must take care of invoking
0135      * the GC through `ForceGarbageCollectionSlow()`.
0136      */
0137     StackSupport stack_support = StackSupport::kSupportsConservativeStackScan;
0138 
0139     /**
0140      * Specifies which types of marking are supported by the heap.
0141      */
0142     MarkingType marking_support = MarkingType::kIncrementalAndConcurrent;
0143 
0144     /**
0145      * Specifies which types of sweeping are supported by the heap.
0146      */
0147     SweepingType sweeping_support = SweepingType::kIncrementalAndConcurrent;
0148 
0149     /**
0150      * Resource constraints specifying various properties that the internal
0151      * GC scheduler follows.
0152      */
0153     ResourceConstraints resource_constraints;
0154   };
0155 
0156   /**
0157    * Creates a new heap that can be used for object allocation.
0158    *
0159    * \param platform implemented and provided by the embedder.
0160    * \param options HeapOptions specifying various properties for the Heap.
0161    * \returns a new Heap instance.
0162    */
0163   static std::unique_ptr<Heap> Create(
0164       std::shared_ptr<Platform> platform,
0165       HeapOptions options = HeapOptions::Default());
0166 
0167   virtual ~Heap() = default;
0168 
0169   /**
0170    * Forces garbage collection.
0171    *
0172    * \param source String specifying the source (or caller) triggering a
0173    *   forced garbage collection.
0174    * \param reason String specifying the reason for the forced garbage
0175    *   collection.
0176    * \param stack_state The embedder stack state, see StackState.
0177    */
0178   void ForceGarbageCollectionSlow(
0179       const char* source, const char* reason,
0180       StackState stack_state = StackState::kMayContainHeapPointers);
0181 
0182   /**
0183    * \returns the opaque handle for allocating objects using
0184    * `MakeGarbageCollected()`.
0185    */
0186   AllocationHandle& GetAllocationHandle();
0187 
0188   /**
0189    * \returns the opaque heap handle which may be used to refer to this heap in
0190    *   other APIs. Valid as long as the underlying `Heap` is alive.
0191    */
0192   HeapHandle& GetHeapHandle();
0193 
0194  private:
0195   Heap() = default;
0196 
0197   friend class internal::Heap;
0198 };
0199 
0200 }  // namespace cppgc
0201 
0202 #endif  // INCLUDE_CPPGC_HEAP_H_