1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/js/src/gc/Iteration.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,137 @@ 1.4 +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- 1.5 + * vim: set ts=8 sts=4 et sw=4 tw=99: 1.6 + * This Source Code Form is subject to the terms of the Mozilla Public 1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.8 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.9 + 1.10 +#include "jscompartment.h" 1.11 +#include "jsgc.h" 1.12 + 1.13 +#include "gc/GCInternals.h" 1.14 +#include "js/HashTable.h" 1.15 +#include "vm/Runtime.h" 1.16 + 1.17 +#include "jscntxtinlines.h" 1.18 +#include "jsgcinlines.h" 1.19 + 1.20 +using namespace js; 1.21 +using namespace js::gc; 1.22 + 1.23 +void 1.24 +js::TraceRuntime(JSTracer *trc) 1.25 +{ 1.26 + JS_ASSERT(!IS_GC_MARKING_TRACER(trc)); 1.27 + 1.28 + MinorGC(trc->runtime(), JS::gcreason::EVICT_NURSERY); 1.29 + AutoPrepareForTracing prep(trc->runtime(), WithAtoms); 1.30 + MarkRuntime(trc); 1.31 +} 1.32 + 1.33 +static void 1.34 +IterateCompartmentsArenasCells(JSRuntime *rt, Zone *zone, void *data, 1.35 + JSIterateCompartmentCallback compartmentCallback, 1.36 + IterateArenaCallback arenaCallback, 1.37 + IterateCellCallback cellCallback) 1.38 +{ 1.39 + for (CompartmentsInZoneIter comp(zone); !comp.done(); comp.next()) 1.40 + (*compartmentCallback)(rt, data, comp); 1.41 + 1.42 + for (size_t thingKind = 0; thingKind != FINALIZE_LIMIT; thingKind++) { 1.43 + JSGCTraceKind traceKind = MapAllocToTraceKind(AllocKind(thingKind)); 1.44 + size_t thingSize = Arena::thingSize(AllocKind(thingKind)); 1.45 + 1.46 + for (ArenaIter aiter(zone, AllocKind(thingKind)); !aiter.done(); aiter.next()) { 1.47 + ArenaHeader *aheader = aiter.get(); 1.48 + (*arenaCallback)(rt, data, aheader->getArena(), traceKind, thingSize); 1.49 + for (CellIterUnderGC iter(aheader); !iter.done(); iter.next()) 1.50 + (*cellCallback)(rt, data, iter.getCell(), traceKind, thingSize); 1.51 + } 1.52 + } 1.53 +} 1.54 + 1.55 +void 1.56 +js::IterateZonesCompartmentsArenasCells(JSRuntime *rt, void *data, 1.57 + IterateZoneCallback zoneCallback, 1.58 + JSIterateCompartmentCallback compartmentCallback, 1.59 + IterateArenaCallback arenaCallback, 1.60 + IterateCellCallback cellCallback) 1.61 +{ 1.62 + AutoPrepareForTracing prop(rt, WithAtoms); 1.63 + 1.64 + for (ZonesIter zone(rt, WithAtoms); !zone.done(); zone.next()) { 1.65 + (*zoneCallback)(rt, data, zone); 1.66 + IterateCompartmentsArenasCells(rt, zone, data, 1.67 + compartmentCallback, arenaCallback, cellCallback); 1.68 + } 1.69 +} 1.70 + 1.71 +void 1.72 +js::IterateZoneCompartmentsArenasCells(JSRuntime *rt, Zone *zone, void *data, 1.73 + IterateZoneCallback zoneCallback, 1.74 + JSIterateCompartmentCallback compartmentCallback, 1.75 + IterateArenaCallback arenaCallback, 1.76 + IterateCellCallback cellCallback) 1.77 +{ 1.78 + AutoPrepareForTracing prop(rt, WithAtoms); 1.79 + 1.80 + (*zoneCallback)(rt, data, zone); 1.81 + IterateCompartmentsArenasCells(rt, zone, data, 1.82 + compartmentCallback, arenaCallback, cellCallback); 1.83 +} 1.84 + 1.85 +void 1.86 +js::IterateChunks(JSRuntime *rt, void *data, IterateChunkCallback chunkCallback) 1.87 +{ 1.88 + AutoPrepareForTracing prep(rt, SkipAtoms); 1.89 + 1.90 + for (js::GCChunkSet::Range r = rt->gcChunkSet.all(); !r.empty(); r.popFront()) 1.91 + chunkCallback(rt, data, r.front()); 1.92 +} 1.93 + 1.94 +void 1.95 +js::IterateScripts(JSRuntime *rt, JSCompartment *compartment, 1.96 + void *data, IterateScriptCallback scriptCallback) 1.97 +{ 1.98 + MinorGC(rt, JS::gcreason::EVICT_NURSERY); 1.99 + AutoPrepareForTracing prep(rt, SkipAtoms); 1.100 + 1.101 + if (compartment) { 1.102 + for (CellIterUnderGC i(compartment->zone(), gc::FINALIZE_SCRIPT); !i.done(); i.next()) { 1.103 + JSScript *script = i.get<JSScript>(); 1.104 + if (script->compartment() == compartment) 1.105 + scriptCallback(rt, data, script); 1.106 + } 1.107 + } else { 1.108 + for (ZonesIter zone(rt, SkipAtoms); !zone.done(); zone.next()) { 1.109 + for (CellIterUnderGC i(zone, gc::FINALIZE_SCRIPT); !i.done(); i.next()) 1.110 + scriptCallback(rt, data, i.get<JSScript>()); 1.111 + } 1.112 + } 1.113 +} 1.114 + 1.115 +void 1.116 +js::IterateGrayObjects(Zone *zone, GCThingCallback cellCallback, void *data) 1.117 +{ 1.118 + MinorGC(zone->runtimeFromMainThread(), JS::gcreason::EVICT_NURSERY); 1.119 + AutoPrepareForTracing prep(zone->runtimeFromMainThread(), SkipAtoms); 1.120 + 1.121 + for (size_t finalizeKind = 0; finalizeKind <= FINALIZE_OBJECT_LAST; finalizeKind++) { 1.122 + for (CellIterUnderGC i(zone, AllocKind(finalizeKind)); !i.done(); i.next()) { 1.123 + JSObject *obj = i.get<JSObject>(); 1.124 + if (obj->isMarked(GRAY)) 1.125 + cellCallback(data, obj); 1.126 + } 1.127 + } 1.128 +} 1.129 + 1.130 +JS_PUBLIC_API(void) 1.131 +JS_IterateCompartments(JSRuntime *rt, void *data, 1.132 + JSIterateCompartmentCallback compartmentCallback) 1.133 +{ 1.134 + JS_ASSERT(!rt->isHeapBusy()); 1.135 + 1.136 + AutoTraceSession session(rt); 1.137 + 1.138 + for (CompartmentsIter c(rt, WithAtoms); !c.done(); c.next()) 1.139 + (*compartmentCallback)(rt, data, c); 1.140 +}