|
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 mozilla_CycleCollectedJSRuntime_h__ |
|
8 #define mozilla_CycleCollectedJSRuntime_h__ |
|
9 |
|
10 #include "mozilla/MemoryReporting.h" |
|
11 #include "jsapi.h" |
|
12 |
|
13 #include "nsCycleCollector.h" |
|
14 #include "nsCycleCollectionParticipant.h" |
|
15 #include "nsDataHashtable.h" |
|
16 #include "nsHashKeys.h" |
|
17 #include "nsTArray.h" |
|
18 |
|
19 class nsCycleCollectionNoteRootCallback; |
|
20 class nsIException; |
|
21 |
|
22 namespace js { |
|
23 class Class; |
|
24 } |
|
25 |
|
26 namespace mozilla { |
|
27 |
|
28 class JSGCThingParticipant: public nsCycleCollectionParticipant |
|
29 { |
|
30 public: |
|
31 NS_IMETHOD_(void) Root(void *n) |
|
32 { |
|
33 } |
|
34 |
|
35 NS_IMETHOD_(void) Unlink(void *n) |
|
36 { |
|
37 } |
|
38 |
|
39 NS_IMETHOD_(void) Unroot(void *n) |
|
40 { |
|
41 } |
|
42 |
|
43 NS_IMETHOD_(void) DeleteCycleCollectable(void *n) |
|
44 { |
|
45 } |
|
46 |
|
47 NS_IMETHOD Traverse(void *n, nsCycleCollectionTraversalCallback &cb); |
|
48 }; |
|
49 |
|
50 class JSZoneParticipant : public nsCycleCollectionParticipant |
|
51 { |
|
52 public: |
|
53 MOZ_CONSTEXPR JSZoneParticipant(): nsCycleCollectionParticipant() {} |
|
54 |
|
55 NS_IMETHOD_(void) Root(void *p) |
|
56 { |
|
57 } |
|
58 |
|
59 NS_IMETHOD_(void) Unlink(void *p) |
|
60 { |
|
61 } |
|
62 |
|
63 NS_IMETHOD_(void) Unroot(void *p) |
|
64 { |
|
65 } |
|
66 |
|
67 NS_IMETHOD_(void) DeleteCycleCollectable(void *n) |
|
68 { |
|
69 } |
|
70 |
|
71 NS_IMETHOD Traverse(void *p, nsCycleCollectionTraversalCallback &cb); |
|
72 }; |
|
73 |
|
74 class IncrementalFinalizeRunnable; |
|
75 |
|
76 // Contains various stats about the cycle collection. |
|
77 struct CycleCollectorResults |
|
78 { |
|
79 void Init() |
|
80 { |
|
81 mForcedGC = false; |
|
82 mMergedZones = false; |
|
83 mVisitedRefCounted = 0; |
|
84 mVisitedGCed = 0; |
|
85 mFreedRefCounted = 0; |
|
86 mFreedGCed = 0; |
|
87 } |
|
88 |
|
89 bool mForcedGC; |
|
90 bool mMergedZones; |
|
91 uint32_t mVisitedRefCounted; |
|
92 uint32_t mVisitedGCed; |
|
93 uint32_t mFreedRefCounted; |
|
94 uint32_t mFreedGCed; |
|
95 }; |
|
96 |
|
97 class CycleCollectedJSRuntime |
|
98 { |
|
99 friend class JSGCThingParticipant; |
|
100 friend class JSZoneParticipant; |
|
101 friend class IncrementalFinalizeRunnable; |
|
102 protected: |
|
103 CycleCollectedJSRuntime(JSRuntime* aParentRuntime, |
|
104 uint32_t aMaxbytes, |
|
105 JSUseHelperThreads aUseHelperThreads); |
|
106 virtual ~CycleCollectedJSRuntime(); |
|
107 |
|
108 size_t SizeOfExcludingThis(mozilla::MallocSizeOf aMallocSizeOf) const; |
|
109 void UnmarkSkippableJSHolders(); |
|
110 |
|
111 virtual void TraverseAdditionalNativeRoots(nsCycleCollectionNoteRootCallback& aCb) {} |
|
112 virtual void TraceAdditionalNativeGrayRoots(JSTracer* aTracer) {} |
|
113 |
|
114 virtual void CustomGCCallback(JSGCStatus aStatus) {} |
|
115 virtual bool CustomContextCallback(JSContext* aCx, unsigned aOperation) |
|
116 { |
|
117 return true; // Don't block context creation. |
|
118 } |
|
119 |
|
120 private: |
|
121 |
|
122 void |
|
123 DescribeGCThing(bool aIsMarked, void* aThing, JSGCTraceKind aTraceKind, |
|
124 nsCycleCollectionTraversalCallback& aCb) const; |
|
125 |
|
126 virtual bool |
|
127 DescribeCustomObjects(JSObject* aObject, const js::Class* aClasp, |
|
128 char (&aName)[72]) const |
|
129 { |
|
130 return false; // We did nothing. |
|
131 } |
|
132 |
|
133 void |
|
134 NoteGCThingJSChildren(void* aThing, JSGCTraceKind aTraceKind, |
|
135 nsCycleCollectionTraversalCallback& aCb) const; |
|
136 |
|
137 void |
|
138 NoteGCThingXPCOMChildren(const js::Class* aClasp, JSObject* aObj, |
|
139 nsCycleCollectionTraversalCallback& aCb) const; |
|
140 |
|
141 virtual bool |
|
142 NoteCustomGCThingXPCOMChildren(const js::Class* aClasp, JSObject* aObj, |
|
143 nsCycleCollectionTraversalCallback& aCb) const |
|
144 { |
|
145 return false; // We did nothing. |
|
146 } |
|
147 |
|
148 enum TraverseSelect { |
|
149 TRAVERSE_CPP, |
|
150 TRAVERSE_FULL |
|
151 }; |
|
152 |
|
153 void |
|
154 TraverseGCThing(TraverseSelect aTs, void* aThing, |
|
155 JSGCTraceKind aTraceKind, |
|
156 nsCycleCollectionTraversalCallback& aCb); |
|
157 |
|
158 void |
|
159 TraverseZone(JS::Zone* aZone, nsCycleCollectionTraversalCallback& aCb); |
|
160 |
|
161 static void |
|
162 TraverseObjectShim(void* aData, void* aThing); |
|
163 |
|
164 void TraverseNativeRoots(nsCycleCollectionNoteRootCallback& aCb); |
|
165 |
|
166 static void TraceBlackJS(JSTracer* aTracer, void* aData); |
|
167 static void TraceGrayJS(JSTracer* aTracer, void* aData); |
|
168 static void GCCallback(JSRuntime* aRuntime, JSGCStatus aStatus, void* aData); |
|
169 static bool ContextCallback(JSContext* aCx, unsigned aOperation, |
|
170 void* aData); |
|
171 |
|
172 virtual void TraceNativeBlackRoots(JSTracer* aTracer) { }; |
|
173 void TraceNativeGrayRoots(JSTracer* aTracer); |
|
174 |
|
175 enum DeferredFinalizeType { |
|
176 FinalizeIncrementally, |
|
177 FinalizeNow, |
|
178 }; |
|
179 |
|
180 void FinalizeDeferredThings(DeferredFinalizeType aType); |
|
181 |
|
182 void OnGC(JSGCStatus aStatus); |
|
183 |
|
184 public: |
|
185 void AddJSHolder(void* aHolder, nsScriptObjectTracer* aTracer); |
|
186 void RemoveJSHolder(void* aHolder); |
|
187 #ifdef DEBUG |
|
188 bool IsJSHolder(void* aHolder); |
|
189 void AssertNoObjectsToTrace(void* aPossibleJSHolder); |
|
190 #endif |
|
191 |
|
192 already_AddRefed<nsIException> GetPendingException() const; |
|
193 void SetPendingException(nsIException* aException); |
|
194 |
|
195 nsCycleCollectionParticipant* GCThingParticipant(); |
|
196 nsCycleCollectionParticipant* ZoneParticipant(); |
|
197 |
|
198 nsresult TraverseRoots(nsCycleCollectionNoteRootCallback &aCb); |
|
199 bool UsefulToMergeZones() const; |
|
200 void FixWeakMappingGrayBits() const; |
|
201 bool NeedCollect() const; |
|
202 void Collect(uint32_t reason) const; |
|
203 |
|
204 void DeferredFinalize(DeferredFinalizeAppendFunction aAppendFunc, |
|
205 DeferredFinalizeFunction aFunc, |
|
206 void* aThing); |
|
207 void DeferredFinalize(nsISupports* aSupports); |
|
208 |
|
209 void DumpJSHeap(FILE* aFile); |
|
210 |
|
211 virtual void PrepareForForgetSkippable() = 0; |
|
212 virtual void BeginCycleCollectionCallback() = 0; |
|
213 virtual void EndCycleCollectionCallback(CycleCollectorResults &aResults) = 0; |
|
214 virtual void DispatchDeferredDeletion(bool aContinuation) = 0; |
|
215 |
|
216 JSRuntime* Runtime() const |
|
217 { |
|
218 MOZ_ASSERT(mJSRuntime); |
|
219 return mJSRuntime; |
|
220 } |
|
221 |
|
222 // Get the current thread's CycleCollectedJSRuntime. Returns null if there |
|
223 // isn't one. |
|
224 static CycleCollectedJSRuntime* Get(); |
|
225 |
|
226 private: |
|
227 JSGCThingParticipant mGCThingCycleCollectorGlobal; |
|
228 |
|
229 JSZoneParticipant mJSZoneCycleCollectorGlobal; |
|
230 |
|
231 JSRuntime* mJSRuntime; |
|
232 |
|
233 nsDataHashtable<nsPtrHashKey<void>, nsScriptObjectTracer*> mJSHolders; |
|
234 |
|
235 nsTArray<nsISupports*> mDeferredSupports; |
|
236 typedef nsDataHashtable<nsFuncPtrHashKey<DeferredFinalizeFunction>, void*> |
|
237 DeferredFinalizerTable; |
|
238 DeferredFinalizerTable mDeferredFinalizerTable; |
|
239 |
|
240 nsRefPtr<IncrementalFinalizeRunnable> mFinalizeRunnable; |
|
241 |
|
242 nsCOMPtr<nsIException> mPendingException; |
|
243 }; |
|
244 |
|
245 } // namespace mozilla |
|
246 |
|
247 #endif // mozilla_CycleCollectedJSRuntime_h__ |