// Copyright 2011 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 "src/heap/objects-visiting.h" #include "src/heap/heap-inl.h" #include "src/heap/mark-compact-inl.h" #include "src/heap/objects-visiting-inl.h" namespace v8 { namespace internal { // We don't record weak slots during marking or scavenges. Instead we do it // once when we complete mark-compact cycle. Note that write barrier has no // effect if we are already in the middle of compacting mark-sweep cycle and we // have to record slots manually. static bool MustRecordSlots(Heap* heap) { return heap->gc_state() == Heap::MARK_COMPACT && heap->mark_compact_collector()->is_compacting(); } template struct WeakListVisitor; template Tagged VisitWeakList(Heap* heap, Tagged list, WeakObjectRetainer* retainer) { Tagged undefined = ReadOnlyRoots(heap).undefined_value(); Tagged head = undefined; T tail; bool record_slots = MustRecordSlots(heap); while (list != undefined) { // Check whether to keep the candidate in the list. T candidate = T::cast(list); Tagged retained = retainer->RetainAs(list); // Move to the next element before the WeakNext is cleared. list = WeakListVisitor::WeakNext(candidate); if (retained != Object()) { if (head == undefined) { // First element in the list. head = retained; } else { // Subsequent elements in the list. DCHECK(!tail.is_null()); WeakListVisitor::SetWeakNext(tail, HeapObject::cast(retained)); if (record_slots) { Tagged slot_holder = WeakListVisitor::WeakNextHolder(tail); int slot_offset = WeakListVisitor::WeakNextOffset(); ObjectSlot slot = slot_holder->RawField(slot_offset); MarkCompactCollector::RecordSlot(slot_holder, slot, HeapObject::cast(retained)); } } // Retained object is new tail. DCHECK(!IsUndefined(retained, heap->isolate())); candidate = T::cast(retained); tail = candidate; // tail is a live object, visit it. WeakListVisitor::VisitLiveObject(heap, tail, retainer); } else { WeakListVisitor::VisitPhantomObject(heap, candidate); } } // Terminate the list if there is one or more elements. if (!tail.is_null()) WeakListVisitor::SetWeakNext(tail, undefined); return head; } template static void ClearWeakList(Heap* heap, Tagged list) { Tagged undefined = ReadOnlyRoots(heap).undefined_value(); while (list != undefined) { T candidate = T::cast(list); list = WeakListVisitor::WeakNext(candidate); WeakListVisitor::SetWeakNext(candidate, undefined); } } template <> struct WeakListVisitor { static void SetWeakNext(Tagged context, Tagged next) { context->set(Context::NEXT_CONTEXT_LINK, next, UPDATE_WRITE_BARRIER); } static Tagged WeakNext(Tagged context) { return context->next_context_link(); } static Tagged WeakNextHolder(Tagged context) { return context; } static int WeakNextOffset() { return FixedArray::SizeFor(Context::NEXT_CONTEXT_LINK); } static void VisitLiveObject(Heap* heap, Tagged context, WeakObjectRetainer* retainer) { if (heap->gc_state() == Heap::MARK_COMPACT) { // Record the slots of the weak entries in the native context. for (int idx = Context::FIRST_WEAK_SLOT; idx < Context::NATIVE_CONTEXT_SLOTS; ++idx) { ObjectSlot slot = context->RawField(Context::OffsetOfElementAt(idx)); MarkCompactCollector::RecordSlot(context, slot, HeapObject::cast(*slot)); } } } template static void DoWeakList(Heap* heap, Tagged context, WeakObjectRetainer* retainer, int index) { // Visit the weak list, removing dead intermediate elements. Tagged list_head = VisitWeakList(heap, context->get(index), retainer); // Update the list head. context->set(index, list_head, UPDATE_WRITE_BARRIER); if (MustRecordSlots(heap)) { // Record the updated slot if necessary. ObjectSlot head_slot = context->RawField(FixedArray::SizeFor(index)); heap->mark_compact_collector()->RecordSlot(context, head_slot, HeapObject::cast(list_head)); } } static void VisitPhantomObject(Heap* heap, Tagged context) {} }; template <> struct WeakListVisitor { static void SetWeakNext(Tagged obj, Tagged next) { obj->set_weak_next(next, UPDATE_WRITE_BARRIER); } static Tagged WeakNext(Tagged obj) { return obj->weak_next(); } static Tagged WeakNextHolder(Tagged obj) { return obj; } static int WeakNextOffset() { return AllocationSite::kWeakNextOffset; } static void VisitLiveObject(Heap*, Tagged, WeakObjectRetainer*) {} static void VisitPhantomObject(Heap*, Tagged) {} }; template <> struct WeakListVisitor { static void SetWeakNext(Tagged obj, Tagged next) { obj->set_next_dirty(next, UPDATE_WRITE_BARRIER); } static Tagged WeakNext(Tagged obj) { return obj->next_dirty(); } static Tagged WeakNextHolder(Tagged obj) { return obj; } static int WeakNextOffset() { return JSFinalizationRegistry::kNextDirtyOffset; } static void VisitLiveObject(Heap* heap, Tagged obj, WeakObjectRetainer*) { heap->set_dirty_js_finalization_registries_list_tail(obj); } static void VisitPhantomObject(Heap*, Tagged) {} }; template Tagged VisitWeakList(Heap* heap, Tagged list, WeakObjectRetainer* retainer); template Tagged VisitWeakList( Heap* heap, Tagged list, WeakObjectRetainer* retainer); template Tagged VisitWeakList( Heap* heap, Tagged list, WeakObjectRetainer* retainer); } // namespace internal } // namespace v8