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