11cb0ef41Sopenharmony_ci// Copyright 2020 the V8 project authors. All rights reserved.
21cb0ef41Sopenharmony_ci// Use of this source code is governed by a BSD-style license that can be
31cb0ef41Sopenharmony_ci// found in the LICENSE file.
41cb0ef41Sopenharmony_ci
51cb0ef41Sopenharmony_ci#ifndef INCLUDE_CPPGC_HEAP_H_
61cb0ef41Sopenharmony_ci#define INCLUDE_CPPGC_HEAP_H_
71cb0ef41Sopenharmony_ci
81cb0ef41Sopenharmony_ci#include <cstddef>
91cb0ef41Sopenharmony_ci#include <cstdint>
101cb0ef41Sopenharmony_ci#include <memory>
111cb0ef41Sopenharmony_ci#include <vector>
121cb0ef41Sopenharmony_ci
131cb0ef41Sopenharmony_ci#include "cppgc/common.h"
141cb0ef41Sopenharmony_ci#include "cppgc/custom-space.h"
151cb0ef41Sopenharmony_ci#include "cppgc/platform.h"
161cb0ef41Sopenharmony_ci#include "v8config.h"  // NOLINT(build/include_directory)
171cb0ef41Sopenharmony_ci
181cb0ef41Sopenharmony_ci/**
191cb0ef41Sopenharmony_ci * cppgc - A C++ garbage collection library.
201cb0ef41Sopenharmony_ci */
211cb0ef41Sopenharmony_cinamespace cppgc {
221cb0ef41Sopenharmony_ci
231cb0ef41Sopenharmony_ciclass AllocationHandle;
241cb0ef41Sopenharmony_ciclass HeapHandle;
251cb0ef41Sopenharmony_ci
261cb0ef41Sopenharmony_ci/**
271cb0ef41Sopenharmony_ci * Implementation details of cppgc. Those details are considered internal and
281cb0ef41Sopenharmony_ci * may change at any point in time without notice. Users should never rely on
291cb0ef41Sopenharmony_ci * the contents of this namespace.
301cb0ef41Sopenharmony_ci */
311cb0ef41Sopenharmony_cinamespace internal {
321cb0ef41Sopenharmony_ciclass Heap;
331cb0ef41Sopenharmony_ci}  // namespace internal
341cb0ef41Sopenharmony_ci
351cb0ef41Sopenharmony_ciclass V8_EXPORT Heap {
361cb0ef41Sopenharmony_ci public:
371cb0ef41Sopenharmony_ci  /**
381cb0ef41Sopenharmony_ci   * Specifies the stack state the embedder is in.
391cb0ef41Sopenharmony_ci   */
401cb0ef41Sopenharmony_ci  using StackState = EmbedderStackState;
411cb0ef41Sopenharmony_ci
421cb0ef41Sopenharmony_ci  /**
431cb0ef41Sopenharmony_ci   * Specifies whether conservative stack scanning is supported.
441cb0ef41Sopenharmony_ci   */
451cb0ef41Sopenharmony_ci  enum class StackSupport : uint8_t {
461cb0ef41Sopenharmony_ci    /**
471cb0ef41Sopenharmony_ci     * Conservative stack scan is supported.
481cb0ef41Sopenharmony_ci     */
491cb0ef41Sopenharmony_ci    kSupportsConservativeStackScan,
501cb0ef41Sopenharmony_ci    /**
511cb0ef41Sopenharmony_ci     * Conservative stack scan is not supported. Embedders may use this option
521cb0ef41Sopenharmony_ci     * when using custom infrastructure that is unsupported by the library.
531cb0ef41Sopenharmony_ci     */
541cb0ef41Sopenharmony_ci    kNoConservativeStackScan,
551cb0ef41Sopenharmony_ci  };
561cb0ef41Sopenharmony_ci
571cb0ef41Sopenharmony_ci  /**
581cb0ef41Sopenharmony_ci   * Specifies supported marking types.
591cb0ef41Sopenharmony_ci   */
601cb0ef41Sopenharmony_ci  enum class MarkingType : uint8_t {
611cb0ef41Sopenharmony_ci    /**
621cb0ef41Sopenharmony_ci     * Atomic stop-the-world marking. This option does not require any write
631cb0ef41Sopenharmony_ci     * barriers but is the most intrusive in terms of jank.
641cb0ef41Sopenharmony_ci     */
651cb0ef41Sopenharmony_ci    kAtomic,
661cb0ef41Sopenharmony_ci    /**
671cb0ef41Sopenharmony_ci     * Incremental marking interleaves marking with the rest of the application
681cb0ef41Sopenharmony_ci     * workload on the same thread.
691cb0ef41Sopenharmony_ci     */
701cb0ef41Sopenharmony_ci    kIncremental,
711cb0ef41Sopenharmony_ci    /**
721cb0ef41Sopenharmony_ci     * Incremental and concurrent marking.
731cb0ef41Sopenharmony_ci     */
741cb0ef41Sopenharmony_ci    kIncrementalAndConcurrent
751cb0ef41Sopenharmony_ci  };
761cb0ef41Sopenharmony_ci
771cb0ef41Sopenharmony_ci  /**
781cb0ef41Sopenharmony_ci   * Specifies supported sweeping types.
791cb0ef41Sopenharmony_ci   */
801cb0ef41Sopenharmony_ci  enum class SweepingType : uint8_t {
811cb0ef41Sopenharmony_ci    /**
821cb0ef41Sopenharmony_ci     * Atomic stop-the-world sweeping. All of sweeping is performed at once.
831cb0ef41Sopenharmony_ci     */
841cb0ef41Sopenharmony_ci    kAtomic,
851cb0ef41Sopenharmony_ci    /**
861cb0ef41Sopenharmony_ci     * Incremental sweeping interleaves sweeping with the rest of the
871cb0ef41Sopenharmony_ci     * application workload on the same thread.
881cb0ef41Sopenharmony_ci     */
891cb0ef41Sopenharmony_ci    kIncremental,
901cb0ef41Sopenharmony_ci    /**
911cb0ef41Sopenharmony_ci     * Incremental and concurrent sweeping. Sweeping is split and interleaved
921cb0ef41Sopenharmony_ci     * with the rest of the application.
931cb0ef41Sopenharmony_ci     */
941cb0ef41Sopenharmony_ci    kIncrementalAndConcurrent
951cb0ef41Sopenharmony_ci  };
961cb0ef41Sopenharmony_ci
971cb0ef41Sopenharmony_ci  /**
981cb0ef41Sopenharmony_ci   * Constraints for a Heap setup.
991cb0ef41Sopenharmony_ci   */
1001cb0ef41Sopenharmony_ci  struct ResourceConstraints {
1011cb0ef41Sopenharmony_ci    /**
1021cb0ef41Sopenharmony_ci     * Allows the heap to grow to some initial size in bytes before triggering
1031cb0ef41Sopenharmony_ci     * garbage collections. This is useful when it is known that applications
1041cb0ef41Sopenharmony_ci     * need a certain minimum heap to run to avoid repeatedly invoking the
1051cb0ef41Sopenharmony_ci     * garbage collector when growing the heap.
1061cb0ef41Sopenharmony_ci     */
1071cb0ef41Sopenharmony_ci    size_t initial_heap_size_bytes = 0;
1081cb0ef41Sopenharmony_ci  };
1091cb0ef41Sopenharmony_ci
1101cb0ef41Sopenharmony_ci  /**
1111cb0ef41Sopenharmony_ci   * Options specifying Heap properties (e.g. custom spaces) when initializing a
1121cb0ef41Sopenharmony_ci   * heap through `Heap::Create()`.
1131cb0ef41Sopenharmony_ci   */
1141cb0ef41Sopenharmony_ci  struct HeapOptions {
1151cb0ef41Sopenharmony_ci    /**
1161cb0ef41Sopenharmony_ci     * Creates reasonable defaults for instantiating a Heap.
1171cb0ef41Sopenharmony_ci     *
1181cb0ef41Sopenharmony_ci     * \returns the HeapOptions that can be passed to `Heap::Create()`.
1191cb0ef41Sopenharmony_ci     */
1201cb0ef41Sopenharmony_ci    static HeapOptions Default() { return {}; }
1211cb0ef41Sopenharmony_ci
1221cb0ef41Sopenharmony_ci    /**
1231cb0ef41Sopenharmony_ci     * Custom spaces added to heap are required to have indices forming a
1241cb0ef41Sopenharmony_ci     * numbered sequence starting at 0, i.e., their `kSpaceIndex` must
1251cb0ef41Sopenharmony_ci     * correspond to the index they reside in the vector.
1261cb0ef41Sopenharmony_ci     */
1271cb0ef41Sopenharmony_ci    std::vector<std::unique_ptr<CustomSpaceBase>> custom_spaces;
1281cb0ef41Sopenharmony_ci
1291cb0ef41Sopenharmony_ci    /**
1301cb0ef41Sopenharmony_ci     * Specifies whether conservative stack scan is supported. When conservative
1311cb0ef41Sopenharmony_ci     * stack scan is not supported, the collector may try to invoke
1321cb0ef41Sopenharmony_ci     * garbage collections using non-nestable task, which are guaranteed to have
1331cb0ef41Sopenharmony_ci     * no interesting stack, through the provided Platform. If such tasks are
1341cb0ef41Sopenharmony_ci     * not supported by the Platform, the embedder must take care of invoking
1351cb0ef41Sopenharmony_ci     * the GC through `ForceGarbageCollectionSlow()`.
1361cb0ef41Sopenharmony_ci     */
1371cb0ef41Sopenharmony_ci    StackSupport stack_support = StackSupport::kSupportsConservativeStackScan;
1381cb0ef41Sopenharmony_ci
1391cb0ef41Sopenharmony_ci    /**
1401cb0ef41Sopenharmony_ci     * Specifies which types of marking are supported by the heap.
1411cb0ef41Sopenharmony_ci     */
1421cb0ef41Sopenharmony_ci    MarkingType marking_support = MarkingType::kIncrementalAndConcurrent;
1431cb0ef41Sopenharmony_ci
1441cb0ef41Sopenharmony_ci    /**
1451cb0ef41Sopenharmony_ci     * Specifies which types of sweeping are supported by the heap.
1461cb0ef41Sopenharmony_ci     */
1471cb0ef41Sopenharmony_ci    SweepingType sweeping_support = SweepingType::kIncrementalAndConcurrent;
1481cb0ef41Sopenharmony_ci
1491cb0ef41Sopenharmony_ci    /**
1501cb0ef41Sopenharmony_ci     * Resource constraints specifying various properties that the internal
1511cb0ef41Sopenharmony_ci     * GC scheduler follows.
1521cb0ef41Sopenharmony_ci     */
1531cb0ef41Sopenharmony_ci    ResourceConstraints resource_constraints;
1541cb0ef41Sopenharmony_ci  };
1551cb0ef41Sopenharmony_ci
1561cb0ef41Sopenharmony_ci  /**
1571cb0ef41Sopenharmony_ci   * Creates a new heap that can be used for object allocation.
1581cb0ef41Sopenharmony_ci   *
1591cb0ef41Sopenharmony_ci   * \param platform implemented and provided by the embedder.
1601cb0ef41Sopenharmony_ci   * \param options HeapOptions specifying various properties for the Heap.
1611cb0ef41Sopenharmony_ci   * \returns a new Heap instance.
1621cb0ef41Sopenharmony_ci   */
1631cb0ef41Sopenharmony_ci  static std::unique_ptr<Heap> Create(
1641cb0ef41Sopenharmony_ci      std::shared_ptr<Platform> platform,
1651cb0ef41Sopenharmony_ci      HeapOptions options = HeapOptions::Default());
1661cb0ef41Sopenharmony_ci
1671cb0ef41Sopenharmony_ci  virtual ~Heap() = default;
1681cb0ef41Sopenharmony_ci
1691cb0ef41Sopenharmony_ci  /**
1701cb0ef41Sopenharmony_ci   * Forces garbage collection.
1711cb0ef41Sopenharmony_ci   *
1721cb0ef41Sopenharmony_ci   * \param source String specifying the source (or caller) triggering a
1731cb0ef41Sopenharmony_ci   *   forced garbage collection.
1741cb0ef41Sopenharmony_ci   * \param reason String specifying the reason for the forced garbage
1751cb0ef41Sopenharmony_ci   *   collection.
1761cb0ef41Sopenharmony_ci   * \param stack_state The embedder stack state, see StackState.
1771cb0ef41Sopenharmony_ci   */
1781cb0ef41Sopenharmony_ci  void ForceGarbageCollectionSlow(
1791cb0ef41Sopenharmony_ci      const char* source, const char* reason,
1801cb0ef41Sopenharmony_ci      StackState stack_state = StackState::kMayContainHeapPointers);
1811cb0ef41Sopenharmony_ci
1821cb0ef41Sopenharmony_ci  /**
1831cb0ef41Sopenharmony_ci   * \returns the opaque handle for allocating objects using
1841cb0ef41Sopenharmony_ci   * `MakeGarbageCollected()`.
1851cb0ef41Sopenharmony_ci   */
1861cb0ef41Sopenharmony_ci  AllocationHandle& GetAllocationHandle();
1871cb0ef41Sopenharmony_ci
1881cb0ef41Sopenharmony_ci  /**
1891cb0ef41Sopenharmony_ci   * \returns the opaque heap handle which may be used to refer to this heap in
1901cb0ef41Sopenharmony_ci   *   other APIs. Valid as long as the underlying `Heap` is alive.
1911cb0ef41Sopenharmony_ci   */
1921cb0ef41Sopenharmony_ci  HeapHandle& GetHeapHandle();
1931cb0ef41Sopenharmony_ci
1941cb0ef41Sopenharmony_ci private:
1951cb0ef41Sopenharmony_ci  Heap() = default;
1961cb0ef41Sopenharmony_ci
1971cb0ef41Sopenharmony_ci  friend class internal::Heap;
1981cb0ef41Sopenharmony_ci};
1991cb0ef41Sopenharmony_ci
2001cb0ef41Sopenharmony_ci}  // namespace cppgc
2011cb0ef41Sopenharmony_ci
2021cb0ef41Sopenharmony_ci#endif  // INCLUDE_CPPGC_HEAP_H_
203