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
5 #include "src/heap/cppgc-js/unified-heap-marking-visitor.h"
6
7 #include "src/heap/cppgc-js/unified-heap-marking-state-inl.h"
8 #include "src/heap/cppgc/heap.h"
9 #include "src/heap/cppgc/marking-state.h"
10 #include "src/heap/cppgc/visitor.h"
11 #include "src/heap/heap.h"
12 #include "src/heap/mark-compact.h"
13
14 namespace v8 {
15 namespace internal {
16
UnifiedHeapMarkingVisitorBase(HeapBase & heap,cppgc::internal::BasicMarkingState & marking_state,UnifiedHeapMarkingState & unified_heap_marking_state)17 UnifiedHeapMarkingVisitorBase::UnifiedHeapMarkingVisitorBase(
18 HeapBase& heap, cppgc::internal::BasicMarkingState& marking_state,
19 UnifiedHeapMarkingState& unified_heap_marking_state)
20 : JSVisitor(cppgc::internal::VisitorFactory::CreateKey()),
21 marking_state_(marking_state),
22 unified_heap_marking_state_(unified_heap_marking_state) {}
23
Visit(const void * object,TraceDescriptor desc)24 void UnifiedHeapMarkingVisitorBase::Visit(const void* object,
25 TraceDescriptor desc) {
26 marking_state_.MarkAndPush(object, desc);
27 }
28
VisitWeak(const void * object,TraceDescriptor desc,WeakCallback weak_callback,const void * weak_member)29 void UnifiedHeapMarkingVisitorBase::VisitWeak(const void* object,
30 TraceDescriptor desc,
31 WeakCallback weak_callback,
32 const void* weak_member) {
33 marking_state_.RegisterWeakReferenceIfNeeded(object, desc, weak_callback,
34 weak_member);
35 }
36
VisitEphemeron(const void * key,const void * value,TraceDescriptor value_desc)37 void UnifiedHeapMarkingVisitorBase::VisitEphemeron(const void* key,
38 const void* value,
39 TraceDescriptor value_desc) {
40 marking_state_.ProcessEphemeron(key, value, value_desc, *this);
41 }
42
VisitWeakContainer(const void * self,TraceDescriptor strong_desc,TraceDescriptor weak_desc,WeakCallback callback,const void * data)43 void UnifiedHeapMarkingVisitorBase::VisitWeakContainer(
44 const void* self, TraceDescriptor strong_desc, TraceDescriptor weak_desc,
45 WeakCallback callback, const void* data) {
46 marking_state_.ProcessWeakContainer(self, weak_desc, callback, data);
47 }
48
RegisterWeakCallback(WeakCallback callback,const void * object)49 void UnifiedHeapMarkingVisitorBase::RegisterWeakCallback(WeakCallback callback,
50 const void* object) {
51 marking_state_.RegisterWeakCallback(callback, object);
52 }
53
HandleMovableReference(const void ** slot)54 void UnifiedHeapMarkingVisitorBase::HandleMovableReference(const void** slot) {
55 marking_state_.RegisterMovableReference(slot);
56 }
57
Visit(const TracedReferenceBase & ref)58 void UnifiedHeapMarkingVisitorBase::Visit(const TracedReferenceBase& ref) {
59 unified_heap_marking_state_.MarkAndPush(ref);
60 }
61
MutatorUnifiedHeapMarkingVisitor(HeapBase & heap,MutatorMarkingState & marking_state,UnifiedHeapMarkingState & unified_heap_marking_state)62 MutatorUnifiedHeapMarkingVisitor::MutatorUnifiedHeapMarkingVisitor(
63 HeapBase& heap, MutatorMarkingState& marking_state,
64 UnifiedHeapMarkingState& unified_heap_marking_state)
65 : UnifiedHeapMarkingVisitorBase(heap, marking_state,
66 unified_heap_marking_state) {}
67
VisitRoot(const void * object,TraceDescriptor desc,const SourceLocation &)68 void MutatorUnifiedHeapMarkingVisitor::VisitRoot(const void* object,
69 TraceDescriptor desc,
70 const SourceLocation&) {
71 this->Visit(object, desc);
72 }
73
VisitWeakRoot(const void * object,TraceDescriptor desc,WeakCallback weak_callback,const void * weak_root,const SourceLocation &)74 void MutatorUnifiedHeapMarkingVisitor::VisitWeakRoot(const void* object,
75 TraceDescriptor desc,
76 WeakCallback weak_callback,
77 const void* weak_root,
78 const SourceLocation&) {
79 static_cast<MutatorMarkingState&>(marking_state_)
80 .InvokeWeakRootsCallbackIfNeeded(object, desc, weak_callback, weak_root);
81 }
82
ConcurrentUnifiedHeapMarkingVisitor(HeapBase & heap,Heap * v8_heap,cppgc::internal::ConcurrentMarkingState & marking_state)83 ConcurrentUnifiedHeapMarkingVisitor::ConcurrentUnifiedHeapMarkingVisitor(
84 HeapBase& heap, Heap* v8_heap,
85 cppgc::internal::ConcurrentMarkingState& marking_state)
86 : UnifiedHeapMarkingVisitorBase(heap, marking_state,
87 concurrent_unified_heap_marking_state_),
88 local_marking_worklist_(
89 v8_heap ? std::make_unique<MarkingWorklists::Local>(
90 v8_heap->mark_compact_collector()->marking_worklists())
91 : nullptr),
92 concurrent_unified_heap_marking_state_(v8_heap,
93 local_marking_worklist_.get()) {}
94
~ConcurrentUnifiedHeapMarkingVisitor()95 ConcurrentUnifiedHeapMarkingVisitor::~ConcurrentUnifiedHeapMarkingVisitor() {
96 if (local_marking_worklist_) {
97 local_marking_worklist_->Publish();
98 }
99 }
100
DeferTraceToMutatorThreadIfConcurrent(const void * parameter,cppgc::TraceCallback callback,size_t deferred_size)101 bool ConcurrentUnifiedHeapMarkingVisitor::DeferTraceToMutatorThreadIfConcurrent(
102 const void* parameter, cppgc::TraceCallback callback,
103 size_t deferred_size) {
104 marking_state_.concurrent_marking_bailout_worklist().Push(
105 {parameter, callback, deferred_size});
106 static_cast<cppgc::internal::ConcurrentMarkingState&>(marking_state_)
107 .AccountDeferredMarkedBytes(deferred_size);
108 return true;
109 }
110
111 } // namespace internal
112 } // namespace v8
113