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