1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/xpcom/base/CycleCollectedJSRuntime.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,247 @@ 1.4 +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 1.5 +/* vim: set ts=8 sts=2 et sw=2 tw=80: */ 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 +#ifndef mozilla_CycleCollectedJSRuntime_h__ 1.11 +#define mozilla_CycleCollectedJSRuntime_h__ 1.12 + 1.13 +#include "mozilla/MemoryReporting.h" 1.14 +#include "jsapi.h" 1.15 + 1.16 +#include "nsCycleCollector.h" 1.17 +#include "nsCycleCollectionParticipant.h" 1.18 +#include "nsDataHashtable.h" 1.19 +#include "nsHashKeys.h" 1.20 +#include "nsTArray.h" 1.21 + 1.22 +class nsCycleCollectionNoteRootCallback; 1.23 +class nsIException; 1.24 + 1.25 +namespace js { 1.26 +class Class; 1.27 +} 1.28 + 1.29 +namespace mozilla { 1.30 + 1.31 +class JSGCThingParticipant: public nsCycleCollectionParticipant 1.32 +{ 1.33 +public: 1.34 + NS_IMETHOD_(void) Root(void *n) 1.35 + { 1.36 + } 1.37 + 1.38 + NS_IMETHOD_(void) Unlink(void *n) 1.39 + { 1.40 + } 1.41 + 1.42 + NS_IMETHOD_(void) Unroot(void *n) 1.43 + { 1.44 + } 1.45 + 1.46 + NS_IMETHOD_(void) DeleteCycleCollectable(void *n) 1.47 + { 1.48 + } 1.49 + 1.50 + NS_IMETHOD Traverse(void *n, nsCycleCollectionTraversalCallback &cb); 1.51 +}; 1.52 + 1.53 +class JSZoneParticipant : public nsCycleCollectionParticipant 1.54 +{ 1.55 +public: 1.56 + MOZ_CONSTEXPR JSZoneParticipant(): nsCycleCollectionParticipant() {} 1.57 + 1.58 + NS_IMETHOD_(void) Root(void *p) 1.59 + { 1.60 + } 1.61 + 1.62 + NS_IMETHOD_(void) Unlink(void *p) 1.63 + { 1.64 + } 1.65 + 1.66 + NS_IMETHOD_(void) Unroot(void *p) 1.67 + { 1.68 + } 1.69 + 1.70 + NS_IMETHOD_(void) DeleteCycleCollectable(void *n) 1.71 + { 1.72 + } 1.73 + 1.74 + NS_IMETHOD Traverse(void *p, nsCycleCollectionTraversalCallback &cb); 1.75 +}; 1.76 + 1.77 +class IncrementalFinalizeRunnable; 1.78 + 1.79 +// Contains various stats about the cycle collection. 1.80 +struct CycleCollectorResults 1.81 +{ 1.82 + void Init() 1.83 + { 1.84 + mForcedGC = false; 1.85 + mMergedZones = false; 1.86 + mVisitedRefCounted = 0; 1.87 + mVisitedGCed = 0; 1.88 + mFreedRefCounted = 0; 1.89 + mFreedGCed = 0; 1.90 + } 1.91 + 1.92 + bool mForcedGC; 1.93 + bool mMergedZones; 1.94 + uint32_t mVisitedRefCounted; 1.95 + uint32_t mVisitedGCed; 1.96 + uint32_t mFreedRefCounted; 1.97 + uint32_t mFreedGCed; 1.98 +}; 1.99 + 1.100 +class CycleCollectedJSRuntime 1.101 +{ 1.102 + friend class JSGCThingParticipant; 1.103 + friend class JSZoneParticipant; 1.104 + friend class IncrementalFinalizeRunnable; 1.105 +protected: 1.106 + CycleCollectedJSRuntime(JSRuntime* aParentRuntime, 1.107 + uint32_t aMaxbytes, 1.108 + JSUseHelperThreads aUseHelperThreads); 1.109 + virtual ~CycleCollectedJSRuntime(); 1.110 + 1.111 + size_t SizeOfExcludingThis(mozilla::MallocSizeOf aMallocSizeOf) const; 1.112 + void UnmarkSkippableJSHolders(); 1.113 + 1.114 + virtual void TraverseAdditionalNativeRoots(nsCycleCollectionNoteRootCallback& aCb) {} 1.115 + virtual void TraceAdditionalNativeGrayRoots(JSTracer* aTracer) {} 1.116 + 1.117 + virtual void CustomGCCallback(JSGCStatus aStatus) {} 1.118 + virtual bool CustomContextCallback(JSContext* aCx, unsigned aOperation) 1.119 + { 1.120 + return true; // Don't block context creation. 1.121 + } 1.122 + 1.123 +private: 1.124 + 1.125 + void 1.126 + DescribeGCThing(bool aIsMarked, void* aThing, JSGCTraceKind aTraceKind, 1.127 + nsCycleCollectionTraversalCallback& aCb) const; 1.128 + 1.129 + virtual bool 1.130 + DescribeCustomObjects(JSObject* aObject, const js::Class* aClasp, 1.131 + char (&aName)[72]) const 1.132 + { 1.133 + return false; // We did nothing. 1.134 + } 1.135 + 1.136 + void 1.137 + NoteGCThingJSChildren(void* aThing, JSGCTraceKind aTraceKind, 1.138 + nsCycleCollectionTraversalCallback& aCb) const; 1.139 + 1.140 + void 1.141 + NoteGCThingXPCOMChildren(const js::Class* aClasp, JSObject* aObj, 1.142 + nsCycleCollectionTraversalCallback& aCb) const; 1.143 + 1.144 + virtual bool 1.145 + NoteCustomGCThingXPCOMChildren(const js::Class* aClasp, JSObject* aObj, 1.146 + nsCycleCollectionTraversalCallback& aCb) const 1.147 + { 1.148 + return false; // We did nothing. 1.149 + } 1.150 + 1.151 + enum TraverseSelect { 1.152 + TRAVERSE_CPP, 1.153 + TRAVERSE_FULL 1.154 + }; 1.155 + 1.156 + void 1.157 + TraverseGCThing(TraverseSelect aTs, void* aThing, 1.158 + JSGCTraceKind aTraceKind, 1.159 + nsCycleCollectionTraversalCallback& aCb); 1.160 + 1.161 + void 1.162 + TraverseZone(JS::Zone* aZone, nsCycleCollectionTraversalCallback& aCb); 1.163 + 1.164 + static void 1.165 + TraverseObjectShim(void* aData, void* aThing); 1.166 + 1.167 + void TraverseNativeRoots(nsCycleCollectionNoteRootCallback& aCb); 1.168 + 1.169 + static void TraceBlackJS(JSTracer* aTracer, void* aData); 1.170 + static void TraceGrayJS(JSTracer* aTracer, void* aData); 1.171 + static void GCCallback(JSRuntime* aRuntime, JSGCStatus aStatus, void* aData); 1.172 + static bool ContextCallback(JSContext* aCx, unsigned aOperation, 1.173 + void* aData); 1.174 + 1.175 + virtual void TraceNativeBlackRoots(JSTracer* aTracer) { }; 1.176 + void TraceNativeGrayRoots(JSTracer* aTracer); 1.177 + 1.178 + enum DeferredFinalizeType { 1.179 + FinalizeIncrementally, 1.180 + FinalizeNow, 1.181 + }; 1.182 + 1.183 + void FinalizeDeferredThings(DeferredFinalizeType aType); 1.184 + 1.185 + void OnGC(JSGCStatus aStatus); 1.186 + 1.187 +public: 1.188 + void AddJSHolder(void* aHolder, nsScriptObjectTracer* aTracer); 1.189 + void RemoveJSHolder(void* aHolder); 1.190 +#ifdef DEBUG 1.191 + bool IsJSHolder(void* aHolder); 1.192 + void AssertNoObjectsToTrace(void* aPossibleJSHolder); 1.193 +#endif 1.194 + 1.195 + already_AddRefed<nsIException> GetPendingException() const; 1.196 + void SetPendingException(nsIException* aException); 1.197 + 1.198 + nsCycleCollectionParticipant* GCThingParticipant(); 1.199 + nsCycleCollectionParticipant* ZoneParticipant(); 1.200 + 1.201 + nsresult TraverseRoots(nsCycleCollectionNoteRootCallback &aCb); 1.202 + bool UsefulToMergeZones() const; 1.203 + void FixWeakMappingGrayBits() const; 1.204 + bool NeedCollect() const; 1.205 + void Collect(uint32_t reason) const; 1.206 + 1.207 + void DeferredFinalize(DeferredFinalizeAppendFunction aAppendFunc, 1.208 + DeferredFinalizeFunction aFunc, 1.209 + void* aThing); 1.210 + void DeferredFinalize(nsISupports* aSupports); 1.211 + 1.212 + void DumpJSHeap(FILE* aFile); 1.213 + 1.214 + virtual void PrepareForForgetSkippable() = 0; 1.215 + virtual void BeginCycleCollectionCallback() = 0; 1.216 + virtual void EndCycleCollectionCallback(CycleCollectorResults &aResults) = 0; 1.217 + virtual void DispatchDeferredDeletion(bool aContinuation) = 0; 1.218 + 1.219 + JSRuntime* Runtime() const 1.220 + { 1.221 + MOZ_ASSERT(mJSRuntime); 1.222 + return mJSRuntime; 1.223 + } 1.224 + 1.225 + // Get the current thread's CycleCollectedJSRuntime. Returns null if there 1.226 + // isn't one. 1.227 + static CycleCollectedJSRuntime* Get(); 1.228 + 1.229 +private: 1.230 + JSGCThingParticipant mGCThingCycleCollectorGlobal; 1.231 + 1.232 + JSZoneParticipant mJSZoneCycleCollectorGlobal; 1.233 + 1.234 + JSRuntime* mJSRuntime; 1.235 + 1.236 + nsDataHashtable<nsPtrHashKey<void>, nsScriptObjectTracer*> mJSHolders; 1.237 + 1.238 + nsTArray<nsISupports*> mDeferredSupports; 1.239 + typedef nsDataHashtable<nsFuncPtrHashKey<DeferredFinalizeFunction>, void*> 1.240 + DeferredFinalizerTable; 1.241 + DeferredFinalizerTable mDeferredFinalizerTable; 1.242 + 1.243 + nsRefPtr<IncrementalFinalizeRunnable> mFinalizeRunnable; 1.244 + 1.245 + nsCOMPtr<nsIException> mPendingException; 1.246 +}; 1.247 + 1.248 +} // namespace mozilla 1.249 + 1.250 +#endif // mozilla_CycleCollectedJSRuntime_h__