| 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 |
#include "gc/WeakMap-inl.h" |
| 8 |
|
| 9 |
#include <string.h> |
| 10 |
|
| 11 |
#include "gc/PublicIterators.h" |
| 12 |
#include "vm/JSObject.h" |
| 13 |
|
| 14 |
#include "gc/Marking-inl.h" |
| 15 |
|
| 16 |
using namespace js; |
| 17 |
using namespace js::gc; |
| 18 |
|
| 19 |
WeakMapBase::WeakMapBase(JSObject* memOf, Zone* zone) |
| 20 |
: memberOf(memOf), zone_(zone) { |
| 21 |
MOZ_ASSERT_IF(memberOf, memberOf->compartment()->zone() == zone); |
| 22 |
MOZ_ASSERT(!IsMarked(mapColor())); |
| 23 |
} |
| 24 |
|
| 25 |
void WeakMapBase::unmarkZone(JS::Zone* zone) { |
| 26 |
zone->gcEphemeronEdges().clearAndCompact(); |
| 27 |
MOZ_ASSERT(zone->gcNurseryEphemeronEdges().count() == 0); |
| 28 |
|
| 29 |
for (WeakMapBase* m : zone->gcWeakMapList()) { |
| 30 |
m->setMapColor(CellColor::White); |
| 31 |
} |
| 32 |
} |
| 33 |
|
| 34 |
void Zone::traceWeakMaps(JSTracer* trc) { |
| 35 |
MOZ_ASSERT(trc->weakMapAction() != JS::WeakMapTraceAction::Skip); |
| 36 |
for (WeakMapBase* m : gcWeakMapList()) { |
| 37 |
m->trace(trc); |
| 38 |
TraceNullableEdge(trc, &m->memberOf, "memberOf"); |
| 39 |
} |
| 40 |
} |
| 41 |
|
| 42 |
bool WeakMapBase::markMap(MarkColor markColor) { |
| 43 |
// We may be marking in parallel here so use a compare exchange loop to handle |
| 44 |
// concurrent updates to the map color. |
| 45 |
// |
| 46 |
// The color increases monotonically; we don't downgrade from black to gray. |
| 47 |
// |
| 48 |
// We can attempt to mark gray after marking black when a barrier pushes the |
| 49 |
// map object onto the black mark stack when it's already present on the |
| 50 |
// gray mark stack, since this is marked later. |
| 51 |
|
| 52 |
uint32_t targetColor = uint32_t(markColor); |
| 53 |
|
| 54 |
for (;;) { |
| 55 |
uint32_t currentColor = mapColor_; |
| 56 |
|
| 57 |
if (currentColor >= targetColor) { |
| 58 |
return false; |
| 59 |
} |
| 60 |
|
| 61 |
if (mapColor_.compareExchange(currentColor, targetColor)) { |
| 62 |
return true; |
| 63 |
} |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
bool WeakMapBase::addEphemeronEdgesForEntry(MarkColor mapColor, Cell* key, |
| 68 |
Cell* delegate, |
| 69 |
TenuredCell* value) { |
| 70 |
if (delegate && !addEphemeronEdge(mapColor, delegate, key)) { |
| 71 |
return false; |
| 72 |
} |
| 73 |
|
| 74 |
if (value && !addEphemeronEdge(mapColor, key, value)) { |
| 75 |
return false; |
| 76 |
} |
| 77 |
|
| 78 |
return true; |
| 79 |
} |
| 80 |
|
| 81 |
bool WeakMapBase::addEphemeronEdge(MarkColor color, gc::Cell* src, |
| 82 |
gc::Cell* dst) { |
| 83 |
// Add an implicit edge from |src| to |dst|. |
| 84 |
|
| 85 |
auto& edgeTable = src->zone()->gcEphemeronEdges(src); |
| 86 |
auto p = edgeTable.lookupForAdd(src); |
| 87 |
if (!p) { |
| 88 |
if (!edgeTable.add(p, src, EphemeronEdgeVector())) { |
| 89 |
return false; |
| 90 |
} |
| 91 |
} |
| 92 |
return p->value().emplaceBack(color, dst); |
| 93 |
} |
| 94 |
|
| 95 |
#if defined(JS_GC_ZEAL) || defined(DEBUG) |
| 96 |
bool WeakMapBase::checkMarkingForZone(JS::Zone* zone) { |
| 97 |
// This is called at the end of marking. |
| 98 |
MOZ_ASSERT(zone->isGCMarking()); |
| 99 |
|
| 100 |
bool ok = true; |
| 101 |
for (WeakMapBase* m : zone->gcWeakMapList()) { |
| 102 |
if (IsMarked(m->mapColor()) && !m->checkMarking()) { |
| 103 |
ok = false; |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
return ok; |
| 108 |
} |
| 109 |
#endif |
| 110 |
|
| 111 |
#ifdef JSGC_HASH_TABLE_CHECKS |
| 112 |
/* static */ |
| 113 |
void WeakMapBase::checkWeakMapsAfterMovingGC(JS::Zone* zone) { |
| 114 |
for (WeakMapBase* map : zone->gcWeakMapList()) { |
| 115 |
map->checkAfterMovingGC(); |
| 116 |
} |
| 117 |
} |
| 118 |
#endif |
| 119 |
|
| 120 |
bool WeakMapBase::markZoneIteratively(JS::Zone* zone, GCMarker* marker) { |
| 121 |
bool markedAny = false; |
| 122 |
for (WeakMapBase* m : zone->gcWeakMapList()) { |
| 123 |
if (IsMarked(m->mapColor()) && m->markEntries(marker)) { |
| 124 |
markedAny = true; |
| 125 |
} |
| 126 |
} |
| 127 |
return markedAny; |
| 128 |
} |
| 129 |
|
| 130 |
bool WeakMapBase::findSweepGroupEdgesForZone(JS::Zone* atomsZone, |
| 131 |
JS::Zone* mapZone) { |
| 132 |
for (WeakMapBase* m : mapZone->gcWeakMapList()) { |
| 133 |
if (!m->findSweepGroupEdges(atomsZone)) { |
| 134 |
return false; |
| 135 |
} |
| 136 |
} |
| 137 |
return true; |
| 138 |
} |
| 139 |
|
| 140 |
void Zone::sweepWeakMaps(JSTracer* trc) { |
| 141 |
for (WeakMapBase* m = gcWeakMapList().getFirst(); m;) { |
| 142 |
WeakMapBase* next = m->getNext(); |
| 143 |
if (IsMarked(m->mapColor())) { |
| 144 |
m->traceWeakEdges(trc); |
| 145 |
} else { |
| 146 |
m->clearAndCompact(); |
| 147 |
m->removeFrom(gcWeakMapList()); |
| 148 |
} |
| 149 |
m = next; |
| 150 |
} |
| 151 |
|
| 152 |
#ifdef DEBUG |
| 153 |
for (WeakMapBase* m : gcWeakMapList()) { |
| 154 |
MOZ_ASSERT(m->isInList() && IsMarked(m->mapColor())); |
| 155 |
} |
| 156 |
#endif |
| 157 |
} |
| 158 |
|
| 159 |
void WeakMapBase::traceAllMappings(WeakMapTracer* tracer) { |
| 160 |
JSRuntime* rt = tracer->runtime; |
| 161 |
for (ZonesIter zone(rt, SkipAtoms); !zone.done(); zone.next()) { |
| 162 |
for (WeakMapBase* m : zone->gcWeakMapList()) { |
| 163 |
// The WeakMapTracer callback is not allowed to GC. |
| 164 |
JS::AutoSuppressGCAnalysis nogc; |
| 165 |
m->traceMappings(tracer); |
| 166 |
} |
| 167 |
} |
| 168 |
} |
| 169 |
|
| 170 |
bool WeakMapBase::saveZoneMarkedWeakMaps(JS::Zone* zone, |
| 171 |
WeakMapColors& markedWeakMaps) { |
| 172 |
for (WeakMapBase* m : zone->gcWeakMapList()) { |
| 173 |
if (IsMarked(m->mapColor()) && !markedWeakMaps.put(m, m->mapColor())) { |
| 174 |
return false; |
| 175 |
} |
| 176 |
} |
| 177 |
return true; |
| 178 |
} |
| 179 |
|
| 180 |
void WeakMapBase::restoreMarkedWeakMaps(WeakMapColors& markedWeakMaps) { |
| 181 |
for (WeakMapColors::Range r = markedWeakMaps.all(); !r.empty(); |
| 182 |
r.popFront()) { |
| 183 |
WeakMapBase* map = r.front().key(); |
| 184 |
MOZ_ASSERT(map->zone()->isGCMarking()); |
| 185 |
MOZ_ASSERT(!IsMarked(map->mapColor())); |
| 186 |
map->setMapColor(r.front().value()); |
| 187 |
} |
| 188 |
} |
| 189 |
|
| 190 |
namespace js { |
| 191 |
template class WeakMap<JSObject*, JSObject*>; |
| 192 |
} // namespace js |