From 36d22d82aa202bb199967e9512281e9a53db42c9 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sun, 7 Apr 2024 21:33:14 +0200 Subject: Adding upstream version 115.7.0esr. Signed-off-by: Daniel Baumann --- js/src/gc/FinalizationObservers.h | 129 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 129 insertions(+) create mode 100644 js/src/gc/FinalizationObservers.h (limited to 'js/src/gc/FinalizationObservers.h') diff --git a/js/src/gc/FinalizationObservers.h b/js/src/gc/FinalizationObservers.h new file mode 100644 index 0000000000..eee8ef14e0 --- /dev/null +++ b/js/src/gc/FinalizationObservers.h @@ -0,0 +1,129 @@ +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- + * vim: set ts=8 sts=2 et sw=2 tw=80: + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +#ifndef gc_FinalizationObservers_h +#define gc_FinalizationObservers_h + +#include "gc/Barrier.h" +#include "gc/WeakMap.h" +#include "gc/ZoneAllocator.h" +#include "js/GCHashTable.h" +#include "js/GCVector.h" + +namespace js { + +class FinalizationRegistryObject; +class FinalizationRecordObject; +class WeakRefObject; + +namespace gc { + +// Per-zone data structures to support FinalizationRegistry and WeakRef. +class FinalizationObservers { + Zone* const zone; + + // The set of all finalization registries in the associated zone. These are + // direct pointers and are not wrapped. + using RegistrySet = + GCHashSet, StableCellHasher>, + ZoneAllocPolicy>; + RegistrySet registries; + + // A vector of FinalizationRecord objects, or CCWs to them. + using RecordVector = GCVector, 1, ZoneAllocPolicy>; + + // A map from finalization registry targets to a vector of finalization + // records representing registries that the target is registered with and + // their associated held values. The records may be in other zones and are + // wrapped appropriately. + using RecordMap = + GCHashMap, RecordVector, + StableCellHasher>, ZoneAllocPolicy>; + RecordMap recordMap; + + // A weak map used as a set of cross-zone wrappers. This is used for both + // finalization registries and weak refs. For the former it has wrappers to + // finalization record objects and for the latter wrappers to weak refs. + // + // The weak map marking rules keep the wrappers alive while their targets are + // alive and ensure that they are both swept in the same sweep group. + using WrapperWeakSet = ObjectValueWeakMap; + WrapperWeakSet crossZoneRecords; + + // A map of weak ref targets to a vector of weak refs that are observing the + // target. The weak refs may be in other zones and are wrapped appropriately. + using WeakRefHeapPtrVector = + GCVector, 1, js::ZoneAllocPolicy>; + using WeakRefMap = + GCHashMap, WeakRefHeapPtrVector, + StableCellHasher>, ZoneAllocPolicy>; + WeakRefMap weakRefMap; + + // A weak map used as a set of cross-zone weak refs wrappers. + WrapperWeakSet crossZoneWeakRefs; + + public: + explicit FinalizationObservers(Zone* zone); + ~FinalizationObservers(); + + // FinalizationRegistry support: + bool addRegistry(Handle registry); + bool addRecord(HandleObject target, HandleObject record); + void clearRecords(); + + void updateForRemovedRecord(JSObject* wrapper, + FinalizationRecordObject* record); + + // WeakRef support: + bool addWeakRefTarget(Handle target, Handle weakRef); + void removeWeakRefTarget(Handle target, + Handle weakRef); + + void unregisterWeakRefWrapper(JSObject* wrapper, WeakRefObject* weakRef); + + void traceRoots(JSTracer* trc); + void traceWeakEdges(JSTracer* trc); + +#ifdef DEBUG + void checkTables() const; +#endif + + private: + bool addCrossZoneWrapper(WrapperWeakSet& weakSet, JSObject* wrapper); + void removeCrossZoneWrapper(WrapperWeakSet& weakSet, JSObject* wrapper); + + void updateForRemovedWeakRef(JSObject* wrapper, WeakRefObject* weakRef); + + void traceWeakFinalizationRegistryEdges(JSTracer* trc); + void traceWeakWeakRefEdges(JSTracer* trc); + void traceWeakWeakRefVector(JSTracer* trc, WeakRefHeapPtrVector& weakRefs, + JSObject* target); + + static bool shouldRemoveRecord(FinalizationRecordObject* record); +}; + +// Per-global data structures to support FinalizationRegistry. +class FinalizationRegistryGlobalData { + // Set of finalization records for finalization registries in this + // realm. These are traced as part of the realm's global. + using RecordSet = + GCHashSet, StableCellHasher>, + ZoneAllocPolicy>; + RecordSet recordSet; + + public: + explicit FinalizationRegistryGlobalData(Zone* zone); + + bool addRecord(FinalizationRecordObject* record); + void removeRecord(FinalizationRecordObject* record); + + void trace(JSTracer* trc); +}; + +} // namespace gc +} // namespace js + +#endif // gc_FinalizationObservers_h -- cgit v1.2.3