Backed out changeset 1e582a0e5593 (bug 1852921) for causing build bustages
[gecko.git] / js / src / gc / FinalizationObservers.h
blob14ce7620e3b99991c9058a8e506d4111b14e0e90
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(HandleObject target, HandleObject weakRef);
83 void unregisterWeakRefWrapper(JSObject* wrapper, WeakRefObject* weakRef);
85 void traceRoots(JSTracer* trc);
86 void traceWeakEdges(JSTracer* trc);
88 #ifdef DEBUG
89 void checkTables() const;
90 #endif
92 private:
93 bool addCrossZoneWrapper(WrapperWeakSet& weakSet, JSObject* wrapper);
94 void removeCrossZoneWrapper(WrapperWeakSet& weakSet, JSObject* wrapper);
96 void updateForRemovedWeakRef(JSObject* wrapper, WeakRefObject* weakRef);
98 void traceWeakFinalizationRegistryEdges(JSTracer* trc);
99 void traceWeakWeakRefEdges(JSTracer* trc);
100 void traceWeakWeakRefVector(JSTracer* trc, WeakRefHeapPtrVector& weakRefs,
101 JSObject* target);
103 static bool shouldRemoveRecord(FinalizationRecordObject* record);
106 // Per-global data structures to support FinalizationRegistry.
107 class FinalizationRegistryGlobalData {
108 // Set of finalization records for finalization registries in this
109 // realm. These are traced as part of the realm's global.
110 using RecordSet =
111 GCHashSet<HeapPtr<JSObject*>, StableCellHasher<HeapPtr<JSObject*>>,
112 ZoneAllocPolicy>;
113 RecordSet recordSet;
115 public:
116 explicit FinalizationRegistryGlobalData(Zone* zone);
118 bool addRecord(FinalizationRecordObject* record);
119 void removeRecord(FinalizationRecordObject* record);
121 void trace(JSTracer* trc);
124 } // namespace gc
125 } // namespace js
127 #endif // gc_FinalizationObservers_h