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/marking-visitor.h"
6
7 #include "src/heap/cppgc/heap.h"
8 #include "src/heap/cppgc/marking-state.h"
9
10 namespace cppgc {
11 namespace internal {
12
MarkingVisitorBase(HeapBase & heap,MarkingStateBase & marking_state)13 MarkingVisitorBase::MarkingVisitorBase(HeapBase& heap,
14 MarkingStateBase& marking_state)
15 : marking_state_(marking_state) {}
16
Visit(const void * object,TraceDescriptor desc)17 void MarkingVisitorBase::Visit(const void* object, TraceDescriptor desc) {
18 marking_state_.MarkAndPush(object, desc);
19 }
20
VisitWeak(const void * object,TraceDescriptor desc,WeakCallback weak_callback,const void * weak_member)21 void MarkingVisitorBase::VisitWeak(const void* object, TraceDescriptor desc,
22 WeakCallback weak_callback,
23 const void* weak_member) {
24 marking_state_.RegisterWeakReferenceIfNeeded(object, desc, weak_callback,
25 weak_member);
26 }
27
VisitEphemeron(const void * key,TraceDescriptor value_desc)28 void MarkingVisitorBase::VisitEphemeron(const void* key,
29 TraceDescriptor value_desc) {
30 marking_state_.ProcessEphemeron(key, value_desc);
31 }
32
VisitWeakContainer(const void * object,TraceDescriptor strong_desc,TraceDescriptor weak_desc,WeakCallback callback,const void * data)33 void MarkingVisitorBase::VisitWeakContainer(const void* object,
34 TraceDescriptor strong_desc,
35 TraceDescriptor weak_desc,
36 WeakCallback callback,
37 const void* data) {
38 marking_state_.ProcessWeakContainer(object, weak_desc, callback, data);
39 }
40
RegisterWeakCallback(WeakCallback callback,const void * object)41 void MarkingVisitorBase::RegisterWeakCallback(WeakCallback callback,
42 const void* object) {
43 marking_state_.RegisterWeakCallback(callback, object);
44 }
45
HandleMovableReference(const void ** slot)46 void MarkingVisitorBase::HandleMovableReference(const void** slot) {
47 marking_state_.RegisterMovableReference(slot);
48 }
49
ConservativeMarkingVisitor(HeapBase & heap,MutatorMarkingState & marking_state,cppgc::Visitor & visitor)50 ConservativeMarkingVisitor::ConservativeMarkingVisitor(
51 HeapBase& heap, MutatorMarkingState& marking_state, cppgc::Visitor& visitor)
52 : ConservativeTracingVisitor(heap, *heap.page_backend(), visitor),
53 marking_state_(marking_state) {}
54
VisitFullyConstructedConservatively(HeapObjectHeader & header)55 void ConservativeMarkingVisitor::VisitFullyConstructedConservatively(
56 HeapObjectHeader& header) {
57 if (header.IsMarked()) {
58 if (marking_state_.IsMarkedWeakContainer(header))
59 marking_state_.PushMarkedWeakContainer(header);
60 return;
61 }
62 ConservativeTracingVisitor::VisitFullyConstructedConservatively(header);
63 }
64
VisitInConstructionConservatively(HeapObjectHeader & header,TraceConservativelyCallback callback)65 void ConservativeMarkingVisitor::VisitInConstructionConservatively(
66 HeapObjectHeader& header, TraceConservativelyCallback callback) {
67 DCHECK(!marking_state_.IsMarkedWeakContainer(header));
68 marking_state_.MarkNoPush(header);
69 marking_state_.AccountMarkedBytes(header);
70 callback(this, header);
71 }
72
MutatorMarkingVisitor(HeapBase & heap,MutatorMarkingState & marking_state)73 MutatorMarkingVisitor::MutatorMarkingVisitor(HeapBase& heap,
74 MutatorMarkingState& marking_state)
75 : MarkingVisitorBase(heap, marking_state) {}
76
VisitRoot(const void * object,TraceDescriptor desc,const SourceLocation &)77 void MutatorMarkingVisitor::VisitRoot(const void* object, TraceDescriptor desc,
78 const SourceLocation&) {
79 Visit(object, desc);
80 }
81
VisitWeakRoot(const void * object,TraceDescriptor desc,WeakCallback weak_callback,const void * weak_root,const SourceLocation &)82 void MutatorMarkingVisitor::VisitWeakRoot(const void* object,
83 TraceDescriptor desc,
84 WeakCallback weak_callback,
85 const void* weak_root,
86 const SourceLocation&) {
87 static_cast<MutatorMarkingState&>(marking_state_)
88 .InvokeWeakRootsCallbackIfNeeded(object, desc, weak_callback, weak_root);
89 }
90
ConcurrentMarkingVisitor(HeapBase & heap,ConcurrentMarkingState & marking_state)91 ConcurrentMarkingVisitor::ConcurrentMarkingVisitor(
92 HeapBase& heap, ConcurrentMarkingState& marking_state)
93 : MarkingVisitorBase(heap, marking_state) {}
94
VisitPointer(const void * address)95 void ConservativeMarkingVisitor::VisitPointer(const void* address) {
96 TraceConservativelyIfNeeded(address);
97 }
98
DeferTraceToMutatorThreadIfConcurrent(const void * parameter,TraceCallback callback,size_t deferred_size)99 bool ConcurrentMarkingVisitor::DeferTraceToMutatorThreadIfConcurrent(
100 const void* parameter, TraceCallback callback, size_t deferred_size) {
101 marking_state_.concurrent_marking_bailout_worklist().Push(
102 {parameter, callback, deferred_size});
103 static_cast<ConcurrentMarkingState&>(marking_state_)
104 .AccountDeferredMarkedBytes(deferred_size);
105 return true;
106 }
107
108 } // namespace internal
109 } // namespace cppgc
110