| 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/. */ |
| 6 |
|
| 7 |
#ifndef gc_FinalizationObservers_h |
| 8 |
#define gc_FinalizationObservers_h |
| 9 |
|
| 10 |
#include "gc/Barrier.h" |
| 11 |
#include "gc/WeakMap.h" |
| 12 |
#include "gc/ZoneAllocator.h" |
| 13 |
#include "js/friend/CycleCollector.h" |
| 14 |
#include "js/GCHashTable.h" |
| 15 |
#include "js/GCVector.h" |
| 16 |
|
| 17 |
namespace js { |
| 18 |
|
| 19 |
class FinalizationRegistryObject; |
| 20 |
class FinalizationRecordObject; |
| 21 |
class WeakRefObject; |
| 22 |
|
| 23 |
namespace gc { |
| 24 |
|
| 25 |
// Per-zone data structures to support FinalizationRegistry and WeakRef. |
| 26 |
class FinalizationObservers { |
| 27 |
Zone* const zone; |
| 28 |
|
| 29 |
// The set of all finalization registries in the associated zone. These are |
| 30 |
// direct pointers and are not wrapped. |
| 31 |
using RegistrySet = |
| 32 |
GCHashSet<HeapPtr<JSObject*>, StableCellHasher<HeapPtr<JSObject*>>, |
| 33 |
ZoneAllocPolicy>; |
| 34 |
RegistrySet registries; |
| 35 |
|
| 36 |
// A vector of FinalizationRecord objects, or CCWs to them. |
| 37 |
using RecordVector = GCVector<HeapPtr<JSObject*>, 1, ZoneAllocPolicy>; |
| 38 |
|
| 39 |
// A map from finalization registry targets to a vector of finalization |
| 40 |
// records representing registries that the target is registered with and |
| 41 |
// their associated held values. The records may be in other zones and are |
| 42 |
// wrapped appropriately. |
| 43 |
using RecordMap = |
| 44 |
GCHashMap<HeapPtr<JSObject*>, RecordVector, |
| 45 |
StableCellHasher<HeapPtr<JSObject*>>, ZoneAllocPolicy>; |
| 46 |
RecordMap recordMap; |
| 47 |
|
| 48 |
// A weak map used as a set of cross-zone wrappers. This is used for both |
| 49 |
// finalization registries and weak refs. For the former it has wrappers to |
| 50 |
// finalization record objects and for the latter wrappers to weak refs. |
| 51 |
// |
| 52 |
// The weak map marking rules keep the wrappers alive while their targets are |
| 53 |
// alive and ensure that they are both swept in the same sweep group. |
| 54 |
using WrapperWeakSet = ObjectValueWeakMap; |
| 55 |
WrapperWeakSet crossZoneRecords; |
| 56 |
|
| 57 |
// A map of weak ref targets to a vector of weak refs that are observing the |
| 58 |
// target. The weak refs may be in other zones and are wrapped appropriately. |
| 59 |
using WeakRefHeapPtrVector = |
| 60 |
GCVector<js::HeapPtr<JSObject*>, 1, js::ZoneAllocPolicy>; |
| 61 |
using WeakRefMap = |
| 62 |
GCHashMap<HeapPtr<JSObject*>, WeakRefHeapPtrVector, |
| 63 |
StableCellHasher<HeapPtr<JSObject*>>, ZoneAllocPolicy>; |
| 64 |
WeakRefMap weakRefMap; |
| 65 |
|
| 66 |
// A weak map used as a set of cross-zone weak refs wrappers. |
| 67 |
WrapperWeakSet crossZoneWeakRefs; |
| 68 |
|
| 69 |
public: |
| 70 |
explicit FinalizationObservers(Zone* zone); |
| 71 |
~FinalizationObservers(); |
| 72 |
|
| 73 |
// FinalizationRegistry support: |
| 74 |
bool addRegistry(Handle<FinalizationRegistryObject*> registry); |
| 75 |
bool addRecord(HandleObject target, HandleObject record); |
| 76 |
void clearRecords(); |
| 77 |
|
| 78 |
void updateForRemovedRecord(JSObject* wrapper, |
| 79 |
FinalizationRecordObject* record); |
| 80 |
|
| 81 |
// WeakRef support: |
| 82 |
bool addWeakRefTarget(Handle<JSObject*> target, Handle<JSObject*> weakRef); |
| 83 |
void removeWeakRefTarget(Handle<JSObject*> target, |
| 84 |
Handle<WeakRefObject*> weakRef); |
| 85 |
void maybeClearWeakRefTargets(JS::ShouldClearWeakRefTargetCallback callback, |
| 86 |
void* data); |
| 87 |
|
| 88 |
void unregisterWeakRefWrapper(JSObject* wrapper, WeakRefObject* weakRef); |
| 89 |
|
| 90 |
void traceRoots(JSTracer* trc); |
| 91 |
void traceWeakEdges(JSTracer* trc); |
| 92 |
|
| 93 |
#ifdef DEBUG |
| 94 |
void checkTables() const; |
| 95 |
#endif |
| 96 |
|
| 97 |
private: |
| 98 |
bool addCrossZoneWrapper(WrapperWeakSet& weakSet, JSObject* wrapper); |
| 99 |
void removeCrossZoneWrapper(WrapperWeakSet& weakSet, JSObject* wrapper); |
| 100 |
|
| 101 |
void updateForRemovedWeakRef(JSObject* wrapper, WeakRefObject* weakRef); |
| 102 |
|
| 103 |
void traceWeakFinalizationRegistryEdges(JSTracer* trc); |
| 104 |
void traceWeakWeakRefEdges(JSTracer* trc); |
| 105 |
void traceWeakWeakRefVector(JSTracer* trc, WeakRefHeapPtrVector& weakRefs, |
| 106 |
JSObject* target); |
| 107 |
|
| 108 |
static bool shouldRemoveRecord(FinalizationRecordObject* record); |
| 109 |
}; |
| 110 |
|
| 111 |
// Per-global data structures to support FinalizationRegistry. |
| 112 |
class FinalizationRegistryGlobalData { |
| 113 |
// Set of finalization records for finalization registries in this |
| 114 |
// realm. These are traced as part of the realm's global. |
| 115 |
using RecordSet = |
| 116 |
GCHashSet<HeapPtr<JSObject*>, StableCellHasher<HeapPtr<JSObject*>>, |
| 117 |
ZoneAllocPolicy>; |
| 118 |
RecordSet recordSet; |
| 119 |
|
| 120 |
public: |
| 121 |
explicit FinalizationRegistryGlobalData(Zone* zone); |
| 122 |
|
| 123 |
bool addRecord(FinalizationRecordObject* record); |
| 124 |
void removeRecord(FinalizationRecordObject* record); |
| 125 |
|
| 126 |
void trace(JSTracer* trc); |
| 127 |
}; |
| 128 |
|
| 129 |
} // namespace gc |
| 130 |
} // namespace js |
| 131 |
|
| 132 |
#endif // gc_FinalizationObservers_h |