persistent-node.cc 4.98 KB
Newer Older
1 2 3 4 5 6 7 8 9
// 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.

#include "include/cppgc/internal/persistent-node.h"

#include <algorithm>
#include <numeric>

10
#include "include/cppgc/cross-thread-persistent.h"
11
#include "include/cppgc/persistent.h"
12
#include "src/base/platform/platform.h"
13
#include "src/heap/cppgc/platform.h"
14
#include "src/heap/cppgc/process-heap.h"
15

16 17 18
namespace cppgc {
namespace internal {

19 20 21 22
PersistentRegionBase::PersistentRegionBase(
    const FatalOutOfMemoryHandler& oom_handler)
    : oom_handler_(oom_handler) {}

23
PersistentRegionBase::~PersistentRegionBase() { ClearAllUsedNodes(); }
24

25
template <typename PersistentBaseClass>
26
void PersistentRegionBase::ClearAllUsedNodes() {
27 28
  for (auto& slots : nodes_) {
    for (auto& node : *slots) {
29 30 31 32 33 34 35 36 37 38
      if (!node.IsUsed()) continue;

      static_cast<PersistentBaseClass*>(node.owner())->ClearFromGC();

      // Add nodes back to the free list to allow reusing for subsequent
      // creation calls.
      node.InitializeAsFreeNode(free_list_head_);
      free_list_head_ = &node;
      CPPGC_DCHECK(nodes_in_use_ > 0);
      nodes_in_use_--;
39 40
    }
  }
41
  CPPGC_DCHECK(0u == nodes_in_use_);
42 43
}

44 45 46
template void
PersistentRegionBase::ClearAllUsedNodes<CrossThreadPersistentBase>();
template void PersistentRegionBase::ClearAllUsedNodes<PersistentBase>();
47

48
void PersistentRegionBase::ClearAllUsedNodes() {
49 50 51
  ClearAllUsedNodes<PersistentBase>();
}

52
size_t PersistentRegionBase::NodesInUse() const {
53 54
#ifdef DEBUG
  const size_t accumulated_nodes_in_use_ = std::accumulate(
55 56 57 58 59 60
      nodes_.cbegin(), nodes_.cend(), 0u, [](size_t acc, const auto& slots) {
        return acc + std::count_if(slots->cbegin(), slots->cend(),
                                   [](const PersistentNode& node) {
                                     return node.IsUsed();
                                   });
      });
61 62 63
  DCHECK_EQ(accumulated_nodes_in_use_, nodes_in_use_);
#endif  // DEBUG
  return nodes_in_use_;
64 65
}

66
void PersistentRegionBase::RefillFreeList() {
67 68
  auto node_slots = std::make_unique<PersistentNodeSlots>();
  if (!node_slots.get()) {
69
    oom_handler_("Oilpan: PersistentRegionBase::RefillFreeList()");
70 71
  }
  nodes_.push_back(std::move(node_slots));
72 73 74 75 76 77
  for (auto& node : *nodes_.back()) {
    node.InitializeAsFreeNode(free_list_head_);
    free_list_head_ = &node;
  }
}

78 79 80 81 82 83 84 85
PersistentNode* PersistentRegionBase::RefillFreeListAndAllocateNode(
    void* owner, TraceCallback trace) {
  RefillFreeList();
  auto* node = TryAllocateNodeFromFreeList(owner, trace);
  CPPGC_DCHECK(node);
  return node;
}

86
void PersistentRegionBase::Trace(Visitor* visitor) {
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
  free_list_head_ = nullptr;
  for (auto& slots : nodes_) {
    bool is_empty = true;
    for (auto& node : *slots) {
      if (node.IsUsed()) {
        node.Trace(visitor);
        is_empty = false;
      } else {
        node.InitializeAsFreeNode(free_list_head_);
        free_list_head_ = &node;
      }
    }
    if (is_empty) {
      PersistentNode* first_next = (*slots)[0].FreeListNext();
      // First next was processed first in the loop above, guaranteeing that it
      // either points to null or into a different node block.
      CPPGC_DCHECK(!first_next || first_next < &slots->front() ||
                   first_next > &slots->back());
      free_list_head_ = first_next;
      slots.reset();
    }
  }
  nodes_.erase(std::remove_if(nodes_.begin(), nodes_.end(),
                              [](const auto& ptr) { return !ptr; }),
               nodes_.end());
}

114 115
PersistentRegion::PersistentRegion(const FatalOutOfMemoryHandler& oom_handler)
    : PersistentRegionBase(oom_handler),
116
      creation_thread_id_(v8::base::OS::GetCurrentThreadId()) {
117 118 119
  USE(creation_thread_id_);
}

120
bool PersistentRegion::IsCreationThread() {
121
  return creation_thread_id_ == v8::base::OS::GetCurrentThreadId();
122 123
}

124 125 126 127 128 129 130 131
PersistentRegionLock::PersistentRegionLock() {
  g_process_mutex.Pointer()->Lock();
}

PersistentRegionLock::~PersistentRegionLock() {
  g_process_mutex.Pointer()->Unlock();
}

132 133 134 135 136
// static
void PersistentRegionLock::AssertLocked() {
  return g_process_mutex.Pointer()->AssertHeld();
}

137 138 139 140
CrossThreadPersistentRegion::CrossThreadPersistentRegion(
    const FatalOutOfMemoryHandler& oom_handler)
    : PersistentRegionBase(oom_handler) {}

141 142
CrossThreadPersistentRegion::~CrossThreadPersistentRegion() {
  PersistentRegionLock guard;
143
  PersistentRegionBase::ClearAllUsedNodes<CrossThreadPersistentBase>();
144
  nodes_.clear();
145
  // PersistentRegionBase destructor will be a noop.
146 147 148 149
}

void CrossThreadPersistentRegion::Trace(Visitor* visitor) {
  PersistentRegionLock::AssertLocked();
150
  PersistentRegionBase::Trace(visitor);
151 152 153 154
}

size_t CrossThreadPersistentRegion::NodesInUse() const {
  // This method does not require a lock.
155
  return PersistentRegionBase::NodesInUse();
156 157 158 159
}

void CrossThreadPersistentRegion::ClearAllUsedNodes() {
  PersistentRegionLock::AssertLocked();
160
  PersistentRegionBase::ClearAllUsedNodes<CrossThreadPersistentBase>();
161 162
}

163 164
}  // namespace internal
}  // namespace cppgc