Bug 1877642 - Disable browser_fullscreen-tab-close-race.js on apple_silicon !debug...
[gecko.git] / js / src / gc / FinalizationObservers.h
blobeee8ef14e0ab1ba7bb3a175f1282d1901fabcf8e
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 * vim: set ts=8 sts=2 et sw=2 tw=80:
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #ifndef gc_FinalizationObservers_h
8 #define gc_FinalizationObservers_h
10 #include "gc/Barrier.h"
11 #include "gc/WeakMap.h"
12 #include "gc/ZoneAllocator.h"
13 #include "js/GCHashTable.h"
14 #include "js/GCVector.h"
16 namespace js {
18 class FinalizationRegistryObject;
19 class FinalizationRecordObject;
20 class WeakRefObject;
22 namespace gc {
24 // Per-zone data structures to support FinalizationRegistry and WeakRef.
25 class FinalizationObservers {
26 Zone* const zone;
28 // The set of all finalization registries in the associated zone. These are
29 // direct pointers and are not wrapped.
30 using RegistrySet =
31 GCHashSet<HeapPtr<JSObject*>, StableCellHasher<HeapPtr<JSObject*>>,
32 ZoneAllocPolicy>;
33 RegistrySet registries;
35 // A vector of FinalizationRecord objects, or CCWs to them.
36 using RecordVector = GCVector<HeapPtr<JSObject*>, 1, ZoneAllocPolicy>;
38 // A map from finalization registry targets to a vector of finalization
39 // records representing registries that the target is registered with and
40 // their associated held values. The records may be in other zones and are
41 // wrapped appropriately.
42 using RecordMap =
43 GCHashMap<HeapPtr<JSObject*>, RecordVector,
44 StableCellHasher<HeapPtr<JSObject*>>, ZoneAllocPolicy>;
45 RecordMap recordMap;
47 // A weak map used as a set of cross-zone wrappers. This is used for both
48 // finalization registries and weak refs. For the former it has wrappers to
49 // finalization record objects and for the latter wrappers to weak refs.
51 // The weak map marking rules keep the wrappers alive while their targets are
52 // alive and ensure that they are both swept in the same sweep group.
53 using WrapperWeakSet = ObjectValueWeakMap;
54 WrapperWeakSet crossZoneRecords;
56 // A map of weak ref targets to a vector of weak refs that are observing the
57 // target. The weak refs may be in other zones and are wrapped appropriately.
58 using WeakRefHeapPtrVector =
59 GCVector<js::HeapPtr<JSObject*>, 1, js::ZoneAllocPolicy>;
60 using WeakRefMap =
61 GCHashMap<HeapPtr<JSObject*>, WeakRefHeapPtrVector,
62 StableCellHasher<HeapPtr<JSObject*>>, ZoneAllocPolicy>;
63 WeakRefMap weakRefMap;
65 // A weak map used as a set of cross-zone weak refs wrappers.
66 WrapperWeakSet crossZoneWeakRefs;
68 public:
69 explicit FinalizationObservers(Zone* zone);
70 ~FinalizationObservers();
72 // FinalizationRegistry support:
73 bool addRegistry(Handle<FinalizationRegistryObject*> registry);
74 bool addRecord(HandleObject target, HandleObject record);
75 void clearRecords();
77 void updateForRemovedRecord(JSObject* wrapper,
78 FinalizationRecordObject* record);
80 // WeakRef support:
81 bool addWeakRefTarget(Handle<JSObject*> target, Handle<JSObject*> weakRef);
82 void removeWeakRefTarget(Handle<JSObject*> target,
83 Handle<WeakRefObject*> weakRef);
85 void unregisterWeakRefWrapper(JSObject* wrapper, WeakRefObject* weakRef);
87 void traceRoots(JSTracer* trc);
88 void traceWeakEdges(JSTracer* trc);
90 #ifdef DEBUG
91 void checkTables() const;
92 #endif
94 private:
95 bool addCrossZoneWrapper(WrapperWeakSet& weakSet, JSObject* wrapper);
96 void removeCrossZoneWrapper(WrapperWeakSet& weakSet, JSObject* wrapper);
98 void updateForRemovedWeakRef(JSObject* wrapper, WeakRefObject* weakRef);
100 void traceWeakFinalizationRegistryEdges(JSTracer* trc);
101 void traceWeakWeakRefEdges(JSTracer* trc);
102 void traceWeakWeakRefVector(JSTracer* trc, WeakRefHeapPtrVector& weakRefs,
103 JSObject* target);
105 static bool shouldRemoveRecord(FinalizationRecordObject* record);
108 // Per-global data structures to support FinalizationRegistry.
109 class FinalizationRegistryGlobalData {
110 // Set of finalization records for finalization registries in this
111 // realm. These are traced as part of the realm's global.
112 using RecordSet =
113 GCHashSet<HeapPtr<JSObject*>, StableCellHasher<HeapPtr<JSObject*>>,
114 ZoneAllocPolicy>;
115 RecordSet recordSet;
117 public:
118 explicit FinalizationRegistryGlobalData(Zone* zone);
120 bool addRecord(FinalizationRecordObject* record);
121 void removeRecord(FinalizationRecordObject* record);
123 void trace(JSTracer* trc);
126 } // namespace gc
127 } // namespace js
129 #endif // gc_FinalizationObservers_h