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