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_INTERNAL_PERSISTENT_NODE_H_
61cb0ef41Sopenharmony_ci#define INCLUDE_CPPGC_INTERNAL_PERSISTENT_NODE_H_
71cb0ef41Sopenharmony_ci
81cb0ef41Sopenharmony_ci#include <array>
91cb0ef41Sopenharmony_ci#include <memory>
101cb0ef41Sopenharmony_ci#include <vector>
111cb0ef41Sopenharmony_ci
121cb0ef41Sopenharmony_ci#include "cppgc/internal/logging.h"
131cb0ef41Sopenharmony_ci#include "cppgc/trace-trait.h"
141cb0ef41Sopenharmony_ci#include "v8config.h"  // NOLINT(build/include_directory)
151cb0ef41Sopenharmony_ci
161cb0ef41Sopenharmony_cinamespace cppgc {
171cb0ef41Sopenharmony_cinamespace internal {
181cb0ef41Sopenharmony_ci
191cb0ef41Sopenharmony_ciclass CrossThreadPersistentRegion;
201cb0ef41Sopenharmony_ciclass FatalOutOfMemoryHandler;
211cb0ef41Sopenharmony_ciclass RootVisitor;
221cb0ef41Sopenharmony_ci
231cb0ef41Sopenharmony_ci// PersistentNode represents a variant of two states:
241cb0ef41Sopenharmony_ci// 1) traceable node with a back pointer to the Persistent object;
251cb0ef41Sopenharmony_ci// 2) freelist entry.
261cb0ef41Sopenharmony_ciclass PersistentNode final {
271cb0ef41Sopenharmony_ci public:
281cb0ef41Sopenharmony_ci  PersistentNode() = default;
291cb0ef41Sopenharmony_ci
301cb0ef41Sopenharmony_ci  PersistentNode(const PersistentNode&) = delete;
311cb0ef41Sopenharmony_ci  PersistentNode& operator=(const PersistentNode&) = delete;
321cb0ef41Sopenharmony_ci
331cb0ef41Sopenharmony_ci  void InitializeAsUsedNode(void* owner, TraceRootCallback trace) {
341cb0ef41Sopenharmony_ci    CPPGC_DCHECK(trace);
351cb0ef41Sopenharmony_ci    owner_ = owner;
361cb0ef41Sopenharmony_ci    trace_ = trace;
371cb0ef41Sopenharmony_ci  }
381cb0ef41Sopenharmony_ci
391cb0ef41Sopenharmony_ci  void InitializeAsFreeNode(PersistentNode* next) {
401cb0ef41Sopenharmony_ci    next_ = next;
411cb0ef41Sopenharmony_ci    trace_ = nullptr;
421cb0ef41Sopenharmony_ci  }
431cb0ef41Sopenharmony_ci
441cb0ef41Sopenharmony_ci  void UpdateOwner(void* owner) {
451cb0ef41Sopenharmony_ci    CPPGC_DCHECK(IsUsed());
461cb0ef41Sopenharmony_ci    owner_ = owner;
471cb0ef41Sopenharmony_ci  }
481cb0ef41Sopenharmony_ci
491cb0ef41Sopenharmony_ci  PersistentNode* FreeListNext() const {
501cb0ef41Sopenharmony_ci    CPPGC_DCHECK(!IsUsed());
511cb0ef41Sopenharmony_ci    return next_;
521cb0ef41Sopenharmony_ci  }
531cb0ef41Sopenharmony_ci
541cb0ef41Sopenharmony_ci  void Trace(RootVisitor& root_visitor) const {
551cb0ef41Sopenharmony_ci    CPPGC_DCHECK(IsUsed());
561cb0ef41Sopenharmony_ci    trace_(root_visitor, owner_);
571cb0ef41Sopenharmony_ci  }
581cb0ef41Sopenharmony_ci
591cb0ef41Sopenharmony_ci  bool IsUsed() const { return trace_; }
601cb0ef41Sopenharmony_ci
611cb0ef41Sopenharmony_ci  void* owner() const {
621cb0ef41Sopenharmony_ci    CPPGC_DCHECK(IsUsed());
631cb0ef41Sopenharmony_ci    return owner_;
641cb0ef41Sopenharmony_ci  }
651cb0ef41Sopenharmony_ci
661cb0ef41Sopenharmony_ci private:
671cb0ef41Sopenharmony_ci  // PersistentNode acts as a designated union:
681cb0ef41Sopenharmony_ci  // If trace_ != nullptr, owner_ points to the corresponding Persistent handle.
691cb0ef41Sopenharmony_ci  // Otherwise, next_ points to the next freed PersistentNode.
701cb0ef41Sopenharmony_ci  union {
711cb0ef41Sopenharmony_ci    void* owner_ = nullptr;
721cb0ef41Sopenharmony_ci    PersistentNode* next_;
731cb0ef41Sopenharmony_ci  };
741cb0ef41Sopenharmony_ci  TraceRootCallback trace_ = nullptr;
751cb0ef41Sopenharmony_ci};
761cb0ef41Sopenharmony_ci
771cb0ef41Sopenharmony_ciclass V8_EXPORT PersistentRegionBase {
781cb0ef41Sopenharmony_ci  using PersistentNodeSlots = std::array<PersistentNode, 256u>;
791cb0ef41Sopenharmony_ci
801cb0ef41Sopenharmony_ci public:
811cb0ef41Sopenharmony_ci  // Clears Persistent fields to avoid stale pointers after heap teardown.
821cb0ef41Sopenharmony_ci  ~PersistentRegionBase();
831cb0ef41Sopenharmony_ci
841cb0ef41Sopenharmony_ci  PersistentRegionBase(const PersistentRegionBase&) = delete;
851cb0ef41Sopenharmony_ci  PersistentRegionBase& operator=(const PersistentRegionBase&) = delete;
861cb0ef41Sopenharmony_ci
871cb0ef41Sopenharmony_ci  void Iterate(RootVisitor&);
881cb0ef41Sopenharmony_ci
891cb0ef41Sopenharmony_ci  size_t NodesInUse() const;
901cb0ef41Sopenharmony_ci
911cb0ef41Sopenharmony_ci  void ClearAllUsedNodes();
921cb0ef41Sopenharmony_ci
931cb0ef41Sopenharmony_ci protected:
941cb0ef41Sopenharmony_ci  explicit PersistentRegionBase(const FatalOutOfMemoryHandler& oom_handler);
951cb0ef41Sopenharmony_ci
961cb0ef41Sopenharmony_ci  PersistentNode* TryAllocateNodeFromFreeList(void* owner,
971cb0ef41Sopenharmony_ci                                              TraceRootCallback trace) {
981cb0ef41Sopenharmony_ci    PersistentNode* node = nullptr;
991cb0ef41Sopenharmony_ci    if (V8_LIKELY(free_list_head_)) {
1001cb0ef41Sopenharmony_ci      node = free_list_head_;
1011cb0ef41Sopenharmony_ci      free_list_head_ = free_list_head_->FreeListNext();
1021cb0ef41Sopenharmony_ci      CPPGC_DCHECK(!node->IsUsed());
1031cb0ef41Sopenharmony_ci      node->InitializeAsUsedNode(owner, trace);
1041cb0ef41Sopenharmony_ci      nodes_in_use_++;
1051cb0ef41Sopenharmony_ci    }
1061cb0ef41Sopenharmony_ci    return node;
1071cb0ef41Sopenharmony_ci  }
1081cb0ef41Sopenharmony_ci
1091cb0ef41Sopenharmony_ci  void FreeNode(PersistentNode* node) {
1101cb0ef41Sopenharmony_ci    CPPGC_DCHECK(node);
1111cb0ef41Sopenharmony_ci    CPPGC_DCHECK(node->IsUsed());
1121cb0ef41Sopenharmony_ci    node->InitializeAsFreeNode(free_list_head_);
1131cb0ef41Sopenharmony_ci    free_list_head_ = node;
1141cb0ef41Sopenharmony_ci    CPPGC_DCHECK(nodes_in_use_ > 0);
1151cb0ef41Sopenharmony_ci    nodes_in_use_--;
1161cb0ef41Sopenharmony_ci  }
1171cb0ef41Sopenharmony_ci
1181cb0ef41Sopenharmony_ci  PersistentNode* RefillFreeListAndAllocateNode(void* owner,
1191cb0ef41Sopenharmony_ci                                                TraceRootCallback trace);
1201cb0ef41Sopenharmony_ci
1211cb0ef41Sopenharmony_ci private:
1221cb0ef41Sopenharmony_ci  template <typename PersistentBaseClass>
1231cb0ef41Sopenharmony_ci  void ClearAllUsedNodes();
1241cb0ef41Sopenharmony_ci
1251cb0ef41Sopenharmony_ci  void RefillFreeList();
1261cb0ef41Sopenharmony_ci
1271cb0ef41Sopenharmony_ci  std::vector<std::unique_ptr<PersistentNodeSlots>> nodes_;
1281cb0ef41Sopenharmony_ci  PersistentNode* free_list_head_ = nullptr;
1291cb0ef41Sopenharmony_ci  size_t nodes_in_use_ = 0;
1301cb0ef41Sopenharmony_ci  const FatalOutOfMemoryHandler& oom_handler_;
1311cb0ef41Sopenharmony_ci
1321cb0ef41Sopenharmony_ci  friend class CrossThreadPersistentRegion;
1331cb0ef41Sopenharmony_ci};
1341cb0ef41Sopenharmony_ci
1351cb0ef41Sopenharmony_ci// Variant of PersistentRegionBase that checks whether the allocation and
1361cb0ef41Sopenharmony_ci// freeing happens only on the thread that created the region.
1371cb0ef41Sopenharmony_ciclass V8_EXPORT PersistentRegion final : public PersistentRegionBase {
1381cb0ef41Sopenharmony_ci public:
1391cb0ef41Sopenharmony_ci  explicit PersistentRegion(const FatalOutOfMemoryHandler&);
1401cb0ef41Sopenharmony_ci  // Clears Persistent fields to avoid stale pointers after heap teardown.
1411cb0ef41Sopenharmony_ci  ~PersistentRegion() = default;
1421cb0ef41Sopenharmony_ci
1431cb0ef41Sopenharmony_ci  PersistentRegion(const PersistentRegion&) = delete;
1441cb0ef41Sopenharmony_ci  PersistentRegion& operator=(const PersistentRegion&) = delete;
1451cb0ef41Sopenharmony_ci
1461cb0ef41Sopenharmony_ci  V8_INLINE PersistentNode* AllocateNode(void* owner, TraceRootCallback trace) {
1471cb0ef41Sopenharmony_ci    CPPGC_DCHECK(IsCreationThread());
1481cb0ef41Sopenharmony_ci    auto* node = TryAllocateNodeFromFreeList(owner, trace);
1491cb0ef41Sopenharmony_ci    if (V8_LIKELY(node)) return node;
1501cb0ef41Sopenharmony_ci
1511cb0ef41Sopenharmony_ci    // Slow path allocation allows for checking thread correspondence.
1521cb0ef41Sopenharmony_ci    CPPGC_CHECK(IsCreationThread());
1531cb0ef41Sopenharmony_ci    return RefillFreeListAndAllocateNode(owner, trace);
1541cb0ef41Sopenharmony_ci  }
1551cb0ef41Sopenharmony_ci
1561cb0ef41Sopenharmony_ci  V8_INLINE void FreeNode(PersistentNode* node) {
1571cb0ef41Sopenharmony_ci    CPPGC_DCHECK(IsCreationThread());
1581cb0ef41Sopenharmony_ci    PersistentRegionBase::FreeNode(node);
1591cb0ef41Sopenharmony_ci  }
1601cb0ef41Sopenharmony_ci
1611cb0ef41Sopenharmony_ci private:
1621cb0ef41Sopenharmony_ci  bool IsCreationThread();
1631cb0ef41Sopenharmony_ci
1641cb0ef41Sopenharmony_ci  int creation_thread_id_;
1651cb0ef41Sopenharmony_ci};
1661cb0ef41Sopenharmony_ci
1671cb0ef41Sopenharmony_ci// CrossThreadPersistent uses PersistentRegionBase but protects it using this
1681cb0ef41Sopenharmony_ci// lock when needed.
1691cb0ef41Sopenharmony_ciclass V8_EXPORT PersistentRegionLock final {
1701cb0ef41Sopenharmony_ci public:
1711cb0ef41Sopenharmony_ci  PersistentRegionLock();
1721cb0ef41Sopenharmony_ci  ~PersistentRegionLock();
1731cb0ef41Sopenharmony_ci
1741cb0ef41Sopenharmony_ci  static void AssertLocked();
1751cb0ef41Sopenharmony_ci};
1761cb0ef41Sopenharmony_ci
1771cb0ef41Sopenharmony_ci// Variant of PersistentRegionBase that checks whether the PersistentRegionLock
1781cb0ef41Sopenharmony_ci// is locked.
1791cb0ef41Sopenharmony_ciclass V8_EXPORT CrossThreadPersistentRegion final
1801cb0ef41Sopenharmony_ci    : protected PersistentRegionBase {
1811cb0ef41Sopenharmony_ci public:
1821cb0ef41Sopenharmony_ci  explicit CrossThreadPersistentRegion(const FatalOutOfMemoryHandler&);
1831cb0ef41Sopenharmony_ci  // Clears Persistent fields to avoid stale pointers after heap teardown.
1841cb0ef41Sopenharmony_ci  ~CrossThreadPersistentRegion();
1851cb0ef41Sopenharmony_ci
1861cb0ef41Sopenharmony_ci  CrossThreadPersistentRegion(const CrossThreadPersistentRegion&) = delete;
1871cb0ef41Sopenharmony_ci  CrossThreadPersistentRegion& operator=(const CrossThreadPersistentRegion&) =
1881cb0ef41Sopenharmony_ci      delete;
1891cb0ef41Sopenharmony_ci
1901cb0ef41Sopenharmony_ci  V8_INLINE PersistentNode* AllocateNode(void* owner, TraceRootCallback trace) {
1911cb0ef41Sopenharmony_ci    PersistentRegionLock::AssertLocked();
1921cb0ef41Sopenharmony_ci    auto* node = TryAllocateNodeFromFreeList(owner, trace);
1931cb0ef41Sopenharmony_ci    if (V8_LIKELY(node)) return node;
1941cb0ef41Sopenharmony_ci
1951cb0ef41Sopenharmony_ci    return RefillFreeListAndAllocateNode(owner, trace);
1961cb0ef41Sopenharmony_ci  }
1971cb0ef41Sopenharmony_ci
1981cb0ef41Sopenharmony_ci  V8_INLINE void FreeNode(PersistentNode* node) {
1991cb0ef41Sopenharmony_ci    PersistentRegionLock::AssertLocked();
2001cb0ef41Sopenharmony_ci    PersistentRegionBase::FreeNode(node);
2011cb0ef41Sopenharmony_ci  }
2021cb0ef41Sopenharmony_ci
2031cb0ef41Sopenharmony_ci  void Iterate(RootVisitor&);
2041cb0ef41Sopenharmony_ci
2051cb0ef41Sopenharmony_ci  size_t NodesInUse() const;
2061cb0ef41Sopenharmony_ci
2071cb0ef41Sopenharmony_ci  void ClearAllUsedNodes();
2081cb0ef41Sopenharmony_ci};
2091cb0ef41Sopenharmony_ci
2101cb0ef41Sopenharmony_ci}  // namespace internal
2111cb0ef41Sopenharmony_ci
2121cb0ef41Sopenharmony_ci}  // namespace cppgc
2131cb0ef41Sopenharmony_ci
2141cb0ef41Sopenharmony_ci#endif  // INCLUDE_CPPGC_INTERNAL_PERSISTENT_NODE_H_
215