Backed out changeset 1e582a0e5593 (bug 1852921) for causing build bustages
[gecko.git] / js / src / gc / PrivateIterators-inl.h
blobf63d1c3c8de3cb842007d364b06f387a4d5c3d7e
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 /*
8 * GC-internal iterators for various data structures.
9 */
11 #ifndef gc_PrivateIterators_inl_h
12 #define gc_PrivateIterators_inl_h
14 #include "gc/PublicIterators.h"
16 #include "gc/GC-inl.h"
18 namespace js::gc {
20 class ArenaCellIterUnderGC : public ArenaCellIter {
21 public:
22 explicit ArenaCellIterUnderGC(Arena* arena) : ArenaCellIter(arena) {
23 MOZ_ASSERT(CurrentThreadIsPerformingGC());
27 class ArenaCellIterUnderFinalize : public ArenaCellIter {
28 public:
29 explicit ArenaCellIterUnderFinalize(Arena* arena) : ArenaCellIter(arena) {
30 MOZ_ASSERT(CurrentThreadIsGCFinalizing());
34 class GrayObjectIter : public ZoneAllCellIter<js::gc::TenuredCell> {
35 public:
36 explicit GrayObjectIter(JS::Zone* zone, AllocKind kind)
37 : ZoneAllCellIter<js::gc::TenuredCell>() {
38 initForTenuredIteration(zone, kind);
41 JSObject* get() const {
42 return ZoneAllCellIter<js::gc::TenuredCell>::get<JSObject>();
44 operator JSObject*() const { return get(); }
45 JSObject* operator->() const { return get(); }
48 class GCZonesIter {
49 AllZonesIter zone;
51 public:
52 explicit GCZonesIter(GCRuntime* gc) : zone(gc) {
53 MOZ_ASSERT(gc->heapState() != JS::HeapState::Idle);
54 if (!done() && !zone->wasGCStarted()) {
55 next();
58 explicit GCZonesIter(JSRuntime* rt) : GCZonesIter(&rt->gc) {}
60 bool done() const { return zone.done(); }
62 void next() {
63 MOZ_ASSERT(!done());
64 do {
65 zone.next();
66 } while (!zone.done() && !zone->wasGCStarted());
69 JS::Zone* get() const {
70 MOZ_ASSERT(!done());
71 return zone;
74 operator JS::Zone*() const { return get(); }
75 JS::Zone* operator->() const { return get(); }
78 using GCCompartmentsIter =
79 CompartmentsOrRealmsIterT<GCZonesIter, CompartmentsInZoneIter>;
80 using GCRealmsIter = CompartmentsOrRealmsIterT<GCZonesIter, RealmsInZoneIter>;
82 /* Iterates over all zones in the current sweep group. */
83 class SweepGroupZonesIter {
84 JS::Zone* current;
86 public:
87 explicit SweepGroupZonesIter(GCRuntime* gc)
88 : current(gc->getCurrentSweepGroup()) {
89 MOZ_ASSERT(CurrentThreadIsPerformingGC());
91 explicit SweepGroupZonesIter(JSRuntime* rt) : SweepGroupZonesIter(&rt->gc) {}
93 bool done() const { return !current; }
95 void next() {
96 MOZ_ASSERT(!done());
97 current = current->nextNodeInGroup();
100 JS::Zone* get() const {
101 MOZ_ASSERT(!done());
102 return current;
105 operator JS::Zone*() const { return get(); }
106 JS::Zone* operator->() const { return get(); }
109 using SweepGroupCompartmentsIter =
110 CompartmentsOrRealmsIterT<SweepGroupZonesIter, CompartmentsInZoneIter>;
111 using SweepGroupRealmsIter =
112 CompartmentsOrRealmsIterT<SweepGroupZonesIter, RealmsInZoneIter>;
114 // Iterate the free cells in an arena. See also ArenaCellIter which iterates
115 // the allocated cells.
116 class ArenaFreeCellIter {
117 Arena* arena;
118 size_t thingSize;
119 FreeSpan span;
120 uint_fast16_t thing;
122 public:
123 explicit ArenaFreeCellIter(Arena* arena)
124 : arena(arena),
125 thingSize(arena->getThingSize()),
126 span(*arena->getFirstFreeSpan()),
127 thing(span.first) {
128 MOZ_ASSERT(arena);
129 MOZ_ASSERT(thing < ArenaSize);
132 bool done() const {
133 MOZ_ASSERT(thing < ArenaSize);
134 return !thing;
137 TenuredCell* get() const {
138 MOZ_ASSERT(!done());
139 return reinterpret_cast<TenuredCell*>(uintptr_t(arena) + thing);
142 void next() {
143 MOZ_ASSERT(!done());
144 MOZ_ASSERT(thing >= span.first && thing <= span.last);
146 if (thing == span.last) {
147 span = *span.nextSpan(arena);
148 thing = span.first;
149 } else {
150 thing += thingSize;
153 MOZ_ASSERT(thing < ArenaSize);
156 operator TenuredCell*() const { return get(); }
157 TenuredCell* operator->() const { return get(); }
160 } // namespace js::gc
162 #endif // gc_PrivateIterators_inl_h