| Rubin Xu | 7bc1b61 | 2021-02-16 09:38:50 +0000 | [diff] [blame^] | 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 "include/v8.h" |
| 8 | #include "src/heap/cppgc-js/unified-heap-marking-state.h" |
| 9 | #include "src/heap/cppgc/heap.h" |
| 10 | #include "src/heap/cppgc/marking-state.h" |
| 11 | #include "src/heap/cppgc/visitor.h" |
| 12 | |
| 13 | namespace v8 { |
| 14 | namespace internal { |
| 15 | |
| 16 | UnifiedHeapMarkingVisitorBase::UnifiedHeapMarkingVisitorBase( |
| 17 | HeapBase& heap, MarkingStateBase& marking_state, |
| 18 | UnifiedHeapMarkingState& unified_heap_marking_state) |
| 19 | : JSVisitor(cppgc::internal::VisitorFactory::CreateKey()), |
| 20 | marking_state_(marking_state), |
| 21 | unified_heap_marking_state_(unified_heap_marking_state) {} |
| 22 | |
| 23 | void UnifiedHeapMarkingVisitorBase::Visit(const void* object, |
| 24 | TraceDescriptor desc) { |
| 25 | marking_state_.MarkAndPush(object, desc); |
| 26 | } |
| 27 | |
| 28 | void UnifiedHeapMarkingVisitorBase::VisitWeak(const void* object, |
| 29 | TraceDescriptor desc, |
| 30 | WeakCallback weak_callback, |
| 31 | const void* weak_member) { |
| 32 | marking_state_.RegisterWeakReferenceIfNeeded(object, desc, weak_callback, |
| 33 | weak_member); |
| 34 | } |
| 35 | |
| 36 | void UnifiedHeapMarkingVisitorBase::VisitEphemeron(const void* key, |
| 37 | TraceDescriptor value_desc) { |
| 38 | marking_state_.ProcessEphemeron(key, value_desc); |
| 39 | } |
| 40 | |
| 41 | void UnifiedHeapMarkingVisitorBase::VisitWeakContainer( |
| 42 | const void* self, TraceDescriptor strong_desc, TraceDescriptor weak_desc, |
| 43 | WeakCallback callback, const void* data) { |
| 44 | marking_state_.ProcessWeakContainer(self, weak_desc, callback, data); |
| 45 | } |
| 46 | |
| 47 | void UnifiedHeapMarkingVisitorBase::RegisterWeakCallback(WeakCallback callback, |
| 48 | const void* object) { |
| 49 | marking_state_.RegisterWeakCallback(callback, object); |
| 50 | } |
| 51 | |
| 52 | void UnifiedHeapMarkingVisitorBase::HandleMovableReference(const void** slot) { |
| 53 | marking_state_.RegisterMovableReference(slot); |
| 54 | } |
| 55 | |
| 56 | namespace { |
| 57 | void DeferredTraceTracedReference(cppgc::Visitor* visitor, const void* ref) { |
| 58 | static_cast<JSVisitor*>(visitor)->Trace( |
| 59 | *static_cast<const TracedReferenceBase*>(ref)); |
| 60 | } |
| 61 | } // namespace |
| 62 | |
| 63 | void UnifiedHeapMarkingVisitorBase::Visit(const TracedReferenceBase& ref) { |
| 64 | bool should_defer_tracing = DeferTraceToMutatorThreadIfConcurrent( |
| 65 | &ref, DeferredTraceTracedReference, 0); |
| 66 | |
| 67 | if (!should_defer_tracing) unified_heap_marking_state_.MarkAndPush(ref); |
| 68 | } |
| 69 | |
| 70 | MutatorUnifiedHeapMarkingVisitor::MutatorUnifiedHeapMarkingVisitor( |
| 71 | HeapBase& heap, MutatorMarkingState& marking_state, |
| 72 | UnifiedHeapMarkingState& unified_heap_marking_state) |
| 73 | : UnifiedHeapMarkingVisitorBase(heap, marking_state, |
| 74 | unified_heap_marking_state) {} |
| 75 | |
| 76 | void MutatorUnifiedHeapMarkingVisitor::VisitRoot(const void* object, |
| 77 | TraceDescriptor desc, |
| 78 | const SourceLocation&) { |
| 79 | this->Visit(object, desc); |
| 80 | } |
| 81 | |
| 82 | void MutatorUnifiedHeapMarkingVisitor::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 | |
| 91 | ConcurrentUnifiedHeapMarkingVisitor::ConcurrentUnifiedHeapMarkingVisitor( |
| 92 | HeapBase& heap, ConcurrentMarkingState& marking_state, |
| 93 | UnifiedHeapMarkingState& unified_heap_marking_state) |
| 94 | : UnifiedHeapMarkingVisitorBase(heap, marking_state, |
| 95 | unified_heap_marking_state) {} |
| 96 | |
| 97 | bool ConcurrentUnifiedHeapMarkingVisitor::DeferTraceToMutatorThreadIfConcurrent( |
| 98 | const void* parameter, cppgc::TraceCallback callback, |
| 99 | size_t deferred_size) { |
| 100 | marking_state_.concurrent_marking_bailout_worklist().Push( |
| 101 | {parameter, callback, deferred_size}); |
| 102 | static_cast<ConcurrentMarkingState&>(marking_state_) |
| 103 | .AccountDeferredMarkedBytes(deferred_size); |
| 104 | return true; |
| 105 | } |
| 106 | |
| 107 | } // namespace internal |
| 108 | } // namespace v8 |