v8
V8 is Google’s open source high-performance JavaScript and WebAssembly engine, written in C++.
Loading...
Searching...
No Matches
prefinalizer-handler.cc
Go to the documentation of this file.
1// Copyright 2020 the V8 project authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
6
7#include <algorithm>
8#include <memory>
9
12#include "src/heap/cppgc/heap.h"
15
16namespace cppgc {
17namespace internal {
18
21 auto* page = BasePage::FromPayload(object);
22 DCHECK(!page->space().is_compactable());
23 page->heap().prefinalizer_handler()->RegisterPrefinalizer({object, callback});
24}
25
26bool PreFinalizer::operator==(const PreFinalizer& other) const {
27 return (object == other.object) && (callback == other.callback);
28}
29
31 : current_ordered_pre_finalizers_(&ordered_pre_finalizers_),
32 heap_(heap)
33{
35}
36
47
50 StatsCollector::kAtomicSweep);
51 StatsCollector::EnabledScope nested_stats_scope(
52 heap_.stats_collector(), StatsCollector::kSweepInvokePreFinalizers);
53
56 is_invoking_ = true;
58 // Reset all LABs to force allocations to the slow path for black allocation.
59 // This also ensures that a CHECK() hits in case prefinalizers allocate in the
60 // configuration that prohibits this.
62 // Prefinalizers can allocate other objects with prefinalizers, which will
63 // modify ordered_pre_finalizers_ and break iterators.
64 std::vector<PreFinalizer> new_ordered_pre_finalizers;
65 current_ordered_pre_finalizers_ = &new_ordered_pre_finalizers;
68 std::remove_if(ordered_pre_finalizers_.rbegin(),
70 [liveness_broker](const PreFinalizer& pf) {
71 return (pf.callback)(liveness_broker, pf.object);
72 })
73 .base());
74#ifndef CPPGC_ALLOW_ALLOCATIONS_IN_PREFINALIZERS
75 CHECK(new_ordered_pre_finalizers.empty());
76#else // CPPGC_ALLOW_ALLOCATIONS_IN_PREFINALIZERS
77 // Newly added objects with prefinalizers will always survive the current GC
78 // cycle, so it's safe to add them after clearing out the older prefinalizers.
80 new_ordered_pre_finalizers.begin(),
81 new_ordered_pre_finalizers.end());
82#endif // CPPGC_ALLOW_ALLOCATIONS_IN_PREFINALIZERS
84 is_invoking_ = false;
85 ordered_pre_finalizers_.shrink_to_fit();
86}
87
89#ifdef DEBUG
91#else
92 return true;
93#endif
94}
95
101
102} // namespace internal
103} // namespace cppgc
static BasePage * FromPayload(void *)
Definition heap-page.h:314
virtual bool CurrentThreadIsHeapThread() const
Definition heap-base.cc:351
StatsCollector * stats_collector()
Definition heap-base.h:118
ObjectAllocator & object_allocator()
Definition heap-base.h:135
void RegisterPrefinalizer(PreFinalizer pre_finalizer)
std::vector< PreFinalizer > * current_ordered_pre_finalizers_
std::vector< PreFinalizer > ordered_pre_finalizers_
bool(*)(const cppgc::LivenessBroker &, void *) Callback
TNode< Object > callback
#define CHECK(condition)
Definition logging.h:124
#define DCHECK(condition)
Definition logging.h:482
#define DCHECK_EQ(v1, v2)
Definition logging.h:485
#define DCHECK_GT(v1, v2)
Definition logging.h:487
bool operator==(const PreFinalizer &other) const
Heap * heap_