persistent-node.h 5.89 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11
// Copyright 2020 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef INCLUDE_CPPGC_INTERNAL_PERSISTENT_NODE_H_
#define INCLUDE_CPPGC_INTERNAL_PERSISTENT_NODE_H_

#include <array>
#include <memory>
#include <vector>

12 13
#include "cppgc/internal/logging.h"
#include "cppgc/trace-trait.h"
14
#include "v8config.h"  // NOLINT(build/include_directory)
15 16 17 18

namespace cppgc {
namespace internal {

19
class CrossThreadPersistentRegion;
20
class FatalOutOfMemoryHandler;
21
class RootVisitor;
22

Michael Lippautz's avatar
Michael Lippautz committed
23
// PersistentNode represents a variant of two states:
24 25 26 27 28 29 30 31 32
// 1) traceable node with a back pointer to the Persistent object;
// 2) freelist entry.
class PersistentNode final {
 public:
  PersistentNode() = default;

  PersistentNode(const PersistentNode&) = delete;
  PersistentNode& operator=(const PersistentNode&) = delete;

33
  void InitializeAsUsedNode(void* owner, TraceRootCallback trace) {
34
    CPPGC_DCHECK(trace);
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
    owner_ = owner;
    trace_ = trace;
  }

  void InitializeAsFreeNode(PersistentNode* next) {
    next_ = next;
    trace_ = nullptr;
  }

  void UpdateOwner(void* owner) {
    CPPGC_DCHECK(IsUsed());
    owner_ = owner;
  }

  PersistentNode* FreeListNext() const {
    CPPGC_DCHECK(!IsUsed());
    return next_;
  }

54
  void Trace(RootVisitor& root_visitor) const {
55
    CPPGC_DCHECK(IsUsed());
56
    trace_(root_visitor, owner_);
57 58 59 60
  }

  bool IsUsed() const { return trace_; }

61 62 63 64 65
  void* owner() const {
    CPPGC_DCHECK(IsUsed());
    return owner_;
  }

66 67 68 69 70 71 72 73
 private:
  // PersistentNode acts as a designated union:
  // If trace_ != nullptr, owner_ points to the corresponding Persistent handle.
  // Otherwise, next_ points to the next freed PersistentNode.
  union {
    void* owner_ = nullptr;
    PersistentNode* next_;
  };
74
  TraceRootCallback trace_ = nullptr;
75 76
};

77
class V8_EXPORT PersistentRegionBase {
78 79 80
  using PersistentNodeSlots = std::array<PersistentNode, 256u>;

 public:
81
  // Clears Persistent fields to avoid stale pointers after heap teardown.
82
  ~PersistentRegionBase();
83

84 85
  PersistentRegionBase(const PersistentRegionBase&) = delete;
  PersistentRegionBase& operator=(const PersistentRegionBase&) = delete;
86

87
  void Iterate(RootVisitor&);
88 89 90 91 92 93 94 95 96

  size_t NodesInUse() const;

  void ClearAllUsedNodes();

 protected:
  explicit PersistentRegionBase(const FatalOutOfMemoryHandler& oom_handler);

  PersistentNode* TryAllocateNodeFromFreeList(void* owner,
97
                                              TraceRootCallback trace) {
98 99 100 101 102 103 104
    PersistentNode* node = nullptr;
    if (V8_LIKELY(free_list_head_)) {
      node = free_list_head_;
      free_list_head_ = free_list_head_->FreeListNext();
      CPPGC_DCHECK(!node->IsUsed());
      node->InitializeAsUsedNode(owner, trace);
      nodes_in_use_++;
105 106 107 108 109
    }
    return node;
  }

  void FreeNode(PersistentNode* node) {
110
    CPPGC_DCHECK(node);
111
    CPPGC_DCHECK(node->IsUsed());
112 113
    node->InitializeAsFreeNode(free_list_head_);
    free_list_head_ = node;
114 115
    CPPGC_DCHECK(nodes_in_use_ > 0);
    nodes_in_use_--;
116 117
  }

118
  PersistentNode* RefillFreeListAndAllocateNode(void* owner,
119
                                                TraceRootCallback trace);
120

121
 private:
122 123 124
  template <typename PersistentBaseClass>
  void ClearAllUsedNodes();

125 126
  void RefillFreeList();

127 128
  std::vector<std::unique_ptr<PersistentNodeSlots>> nodes_;
  PersistentNode* free_list_head_ = nullptr;
129
  size_t nodes_in_use_ = 0;
130
  const FatalOutOfMemoryHandler& oom_handler_;
131 132

  friend class CrossThreadPersistentRegion;
133 134
};

135 136 137 138
// Variant of PersistentRegionBase that checks whether the allocation and
// freeing happens only on the thread that created the region.
class V8_EXPORT PersistentRegion final : public PersistentRegionBase {
 public:
139
  explicit PersistentRegion(const FatalOutOfMemoryHandler&);
140 141 142 143 144 145
  // Clears Persistent fields to avoid stale pointers after heap teardown.
  ~PersistentRegion() = default;

  PersistentRegion(const PersistentRegion&) = delete;
  PersistentRegion& operator=(const PersistentRegion&) = delete;

146
  V8_INLINE PersistentNode* AllocateNode(void* owner, TraceRootCallback trace) {
147
    CPPGC_DCHECK(IsCreationThread());
148 149 150 151 152 153
    auto* node = TryAllocateNodeFromFreeList(owner, trace);
    if (V8_LIKELY(node)) return node;

    // Slow path allocation allows for checking thread correspondence.
    CPPGC_CHECK(IsCreationThread());
    return RefillFreeListAndAllocateNode(owner, trace);
154 155 156
  }

  V8_INLINE void FreeNode(PersistentNode* node) {
157
    CPPGC_DCHECK(IsCreationThread());
158 159 160
    PersistentRegionBase::FreeNode(node);
  }

161
 private:
162
  bool IsCreationThread();
163 164 165 166 167 168

  int creation_thread_id_;
};

// CrossThreadPersistent uses PersistentRegionBase but protects it using this
// lock when needed.
169 170 171 172
class V8_EXPORT PersistentRegionLock final {
 public:
  PersistentRegionLock();
  ~PersistentRegionLock();
173 174

  static void AssertLocked();
175 176
};

177 178 179 180
// Variant of PersistentRegionBase that checks whether the PersistentRegionLock
// is locked.
class V8_EXPORT CrossThreadPersistentRegion final
    : protected PersistentRegionBase {
181
 public:
182
  explicit CrossThreadPersistentRegion(const FatalOutOfMemoryHandler&);
183 184 185 186 187 188 189
  // Clears Persistent fields to avoid stale pointers after heap teardown.
  ~CrossThreadPersistentRegion();

  CrossThreadPersistentRegion(const CrossThreadPersistentRegion&) = delete;
  CrossThreadPersistentRegion& operator=(const CrossThreadPersistentRegion&) =
      delete;

190
  V8_INLINE PersistentNode* AllocateNode(void* owner, TraceRootCallback trace) {
191
    PersistentRegionLock::AssertLocked();
192 193 194 195
    auto* node = TryAllocateNodeFromFreeList(owner, trace);
    if (V8_LIKELY(node)) return node;

    return RefillFreeListAndAllocateNode(owner, trace);
196 197 198 199
  }

  V8_INLINE void FreeNode(PersistentNode* node) {
    PersistentRegionLock::AssertLocked();
200
    PersistentRegionBase::FreeNode(node);
201 202
  }

203
  void Iterate(RootVisitor&);
204 205 206 207 208 209

  size_t NodesInUse() const;

  void ClearAllUsedNodes();
};

210 211 212 213 214
}  // namespace internal

}  // namespace cppgc

#endif  // INCLUDE_CPPGC_INTERNAL_PERSISTENT_NODE_H_