Source code

Revision control

Other Tools

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
/*
8
* JS engine garbage collector API.
9
*/
10
11
#ifndef gc_GC_h
12
#define gc_GC_h
13
14
#include "jsapi.h"
15
16
#include "gc/AllocKind.h"
17
#include "gc/GCEnum.h"
18
#include "js/TraceKind.h"
19
20
class JSExternalString;
21
class JSFatInlineString;
22
class JSTracer;
23
24
namespace js {
25
26
class AccessorShape;
27
class FatInlineAtom;
28
class NormalAtom;
29
30
class Nursery;
31
32
namespace gc {
33
34
class Arena;
35
struct Chunk;
36
struct Cell;
37
38
/*
39
* Map from C++ type to alloc kind for non-object types. JSObject does not have
40
* a 1:1 mapping, so must use Arena::thingSize.
41
*
42
* The AllocKind is available as MapTypeToFinalizeKind<SomeType>::kind.
43
*/
44
template <typename T>
45
struct MapTypeToFinalizeKind {};
46
#define EXPAND_MAPTYPETOFINALIZEKIND(allocKind, traceKind, type, sizedType, \
47
bgFinal, nursery, compact) \
48
template <> \
49
struct MapTypeToFinalizeKind<type> { \
50
static const AllocKind kind = AllocKind::allocKind; \
51
};
52
FOR_EACH_NONOBJECT_ALLOCKIND(EXPAND_MAPTYPETOFINALIZEKIND)
53
#undef EXPAND_MAPTYPETOFINALIZEKIND
54
55
} /* namespace gc */
56
57
extern void TraceRuntime(JSTracer* trc);
58
59
// Trace roots but don't evict the nursery first; used from DumpHeap.
60
extern void TraceRuntimeWithoutEviction(JSTracer* trc);
61
62
extern void ReleaseAllJITCode(JSFreeOp* op);
63
64
extern void PrepareForDebugGC(JSRuntime* rt);
65
66
/* Functions for managing cross compartment gray pointers. */
67
68
extern void NotifyGCNukeWrapper(JSObject* o);
69
70
extern unsigned NotifyGCPreSwap(JSObject* a, JSObject* b);
71
72
extern void NotifyGCPostSwap(JSObject* a, JSObject* b, unsigned preResult);
73
74
using IterateChunkCallback = void (*)(JSRuntime*, void*, gc::Chunk*);
75
using IterateZoneCallback = void (*)(JSRuntime*, void*, JS::Zone*);
76
using IterateArenaCallback = void (*)(JSRuntime*, void*, gc::Arena*,
77
JS::TraceKind, size_t);
78
using IterateCellCallback = void (*)(JSRuntime*, void*, JS::GCCellPtr, size_t);
79
80
/*
81
* This function calls |zoneCallback| on every zone, |realmCallback| on
82
* every realm, |arenaCallback| on every in-use arena, and |cellCallback|
83
* on every in-use cell in the GC heap.
84
*
85
* Note that no read barrier is triggered on the cells passed to cellCallback,
86
* so no these pointers must not escape the callback.
87
*/
88
extern void IterateHeapUnbarriered(JSContext* cx, void* data,
89
IterateZoneCallback zoneCallback,
90
JS::IterateRealmCallback realmCallback,
91
IterateArenaCallback arenaCallback,
92
IterateCellCallback cellCallback);
93
94
/*
95
* This function is like IterateHeapUnbarriered, but does it for a single zone.
96
*/
97
extern void IterateHeapUnbarrieredForZone(
98
JSContext* cx, JS::Zone* zone, void* data, IterateZoneCallback zoneCallback,
99
JS::IterateRealmCallback realmCallback, IterateArenaCallback arenaCallback,
100
IterateCellCallback cellCallback);
101
102
/*
103
* Invoke chunkCallback on every in-use chunk.
104
*/
105
extern void IterateChunks(JSContext* cx, void* data,
106
IterateChunkCallback chunkCallback);
107
108
using IterateScriptCallback = void (*)(JSRuntime*, void*, BaseScript*,
109
const JS::AutoRequireNoGC&);
110
111
/*
112
* Invoke scriptCallback on every in-use script for the given realm or for all
113
* realms if it is null.
114
*/
115
extern void IterateScripts(JSContext* cx, JS::Realm* realm, void* data,
116
IterateScriptCallback scriptCallback);
117
extern void IterateLazyScripts(JSContext* cx, JS::Realm* realm, void* data,
118
IterateScriptCallback lazyScriptCallback);
119
120
JS::Realm* NewRealm(JSContext* cx, JSPrincipals* principals,
121
const JS::RealmOptions& options);
122
123
namespace gc {
124
125
void FinishGC(JSContext* cx, JS::GCReason = JS::GCReason::FINISH_GC);
126
127
/*
128
* Merge all contents of source into target. This can only be used if source is
129
* the only realm in its zone.
130
*/
131
void MergeRealms(JS::Realm* source, JS::Realm* target);
132
133
void CollectSelfHostingZone(JSContext* cx);
134
135
enum VerifierType { PreBarrierVerifier };
136
137
#ifdef JS_GC_ZEAL
138
139
extern const char ZealModeHelpText[];
140
141
/* Check that write barriers have been used correctly. See gc/Verifier.cpp. */
142
void VerifyBarriers(JSRuntime* rt, VerifierType type);
143
144
void MaybeVerifyBarriers(JSContext* cx, bool always = false);
145
146
void DumpArenaInfo();
147
148
#else
149
150
static inline void VerifyBarriers(JSRuntime* rt, VerifierType type) {}
151
152
static inline void MaybeVerifyBarriers(JSContext* cx, bool always = false) {}
153
154
#endif
155
156
/*
157
* Instances of this class prevent GC while they are live by updating the
158
* |JSContext::suppressGC| counter. Use of this class is highly
159
* discouraged. Please carefully read the comment in vm/JSContext.h above
160
* |suppressGC| and take all appropriate precautions before instantiating this
161
* class.
162
*/
163
class MOZ_RAII JS_HAZ_GC_SUPPRESSED AutoSuppressGC {
164
int32_t& suppressGC_;
165
166
public:
167
explicit AutoSuppressGC(JSContext* cx);
168
169
~AutoSuppressGC() { suppressGC_--; }
170
};
171
172
const char* StateName(State state);
173
174
} /* namespace gc */
175
176
/* Use this to avoid assertions when manipulating the wrapper map. */
177
class MOZ_RAII AutoDisableProxyCheck {
178
public:
179
#ifdef DEBUG
180
AutoDisableProxyCheck();
181
~AutoDisableProxyCheck();
182
#else
183
AutoDisableProxyCheck() {}
184
#endif
185
};
186
187
struct MOZ_RAII AutoDisableCompactingGC {
188
explicit AutoDisableCompactingGC(JSContext* cx);
189
~AutoDisableCompactingGC();
190
191
private:
192
JSContext* cx;
193
};
194
195
} /* namespace js */
196
197
#endif /* gc_GC_h */