2013-06-18 12:02:07 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
#ifndef mozilla_CycleCollectedJSRuntime_h__
|
|
|
|
#define mozilla_CycleCollectedJSRuntime_h__
|
|
|
|
|
2013-06-23 05:03:39 -07:00
|
|
|
#include "mozilla/MemoryReporting.h"
|
2013-06-18 12:02:07 -07:00
|
|
|
#include "jsapi.h"
|
|
|
|
|
2013-07-09 07:28:15 -07:00
|
|
|
#include "nsCycleCollector.h"
|
2013-06-18 12:02:15 -07:00
|
|
|
#include "nsCycleCollectionParticipant.h"
|
2013-06-18 12:02:14 -07:00
|
|
|
#include "nsDataHashtable.h"
|
|
|
|
#include "nsHashKeys.h"
|
2013-07-09 07:28:15 -07:00
|
|
|
#include "nsTArray.h"
|
2013-06-18 12:02:14 -07:00
|
|
|
|
2013-06-18 12:02:15 -07:00
|
|
|
class nsCycleCollectionNoteRootCallback;
|
2013-09-08 20:28:50 -07:00
|
|
|
class nsIException;
|
2013-06-18 12:02:14 -07:00
|
|
|
|
2013-09-19 11:29:31 -07:00
|
|
|
namespace js {
|
|
|
|
class Class;
|
|
|
|
}
|
|
|
|
|
2013-06-18 12:02:07 -07:00
|
|
|
namespace mozilla {
|
|
|
|
|
2013-06-18 12:02:15 -07:00
|
|
|
class JSGCThingParticipant: public nsCycleCollectionParticipant
|
|
|
|
{
|
|
|
|
public:
|
2013-09-11 18:57:53 -07:00
|
|
|
NS_IMETHOD_(void) Root(void *n)
|
2013-06-18 12:02:15 -07:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2013-09-11 18:57:53 -07:00
|
|
|
NS_IMETHOD_(void) Unlink(void *n)
|
2013-06-18 12:02:15 -07:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2013-09-11 18:57:53 -07:00
|
|
|
NS_IMETHOD_(void) Unroot(void *n)
|
2013-06-18 12:02:15 -07:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2013-08-01 18:29:05 -07:00
|
|
|
NS_IMETHOD_(void) DeleteCycleCollectable(void *n)
|
2013-06-18 12:02:15 -07:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2013-08-01 18:29:05 -07:00
|
|
|
NS_IMETHOD Traverse(void *n, nsCycleCollectionTraversalCallback &cb);
|
2013-06-18 12:02:15 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
class JSZoneParticipant : public nsCycleCollectionParticipant
|
|
|
|
{
|
|
|
|
public:
|
2013-08-01 18:29:05 -07:00
|
|
|
MOZ_CONSTEXPR JSZoneParticipant(): nsCycleCollectionParticipant() {}
|
2013-06-18 12:02:15 -07:00
|
|
|
|
2013-09-11 18:57:53 -07:00
|
|
|
NS_IMETHOD_(void) Root(void *p)
|
2013-06-18 12:02:15 -07:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2013-09-11 18:57:53 -07:00
|
|
|
NS_IMETHOD_(void) Unlink(void *p)
|
2013-06-18 12:02:15 -07:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2013-09-11 18:57:53 -07:00
|
|
|
NS_IMETHOD_(void) Unroot(void *p)
|
2013-06-18 12:02:15 -07:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2013-08-01 18:29:05 -07:00
|
|
|
NS_IMETHOD_(void) DeleteCycleCollectable(void *n)
|
2013-06-18 12:02:15 -07:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2013-08-01 18:29:05 -07:00
|
|
|
NS_IMETHOD Traverse(void *p, nsCycleCollectionTraversalCallback &cb);
|
2013-06-18 12:02:15 -07:00
|
|
|
};
|
|
|
|
|
2013-07-09 07:28:15 -07:00
|
|
|
class IncrementalFinalizeRunnable;
|
|
|
|
|
2013-11-20 14:35:16 -08:00
|
|
|
// Contains various stats about the cycle collection.
|
|
|
|
struct CycleCollectorResults
|
|
|
|
{
|
|
|
|
void Init()
|
|
|
|
{
|
|
|
|
mForcedGC = false;
|
|
|
|
mMergedZones = false;
|
|
|
|
mVisitedRefCounted = 0;
|
|
|
|
mVisitedGCed = 0;
|
|
|
|
mFreedRefCounted = 0;
|
|
|
|
mFreedGCed = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool mForcedGC;
|
|
|
|
bool mMergedZones;
|
|
|
|
uint32_t mVisitedRefCounted;
|
|
|
|
uint32_t mVisitedGCed;
|
|
|
|
uint32_t mFreedRefCounted;
|
|
|
|
uint32_t mFreedGCed;
|
|
|
|
};
|
|
|
|
|
2013-06-18 12:02:07 -07:00
|
|
|
class CycleCollectedJSRuntime
|
|
|
|
{
|
2013-06-18 12:02:15 -07:00
|
|
|
friend class JSGCThingParticipant;
|
|
|
|
friend class JSZoneParticipant;
|
2013-07-09 07:28:15 -07:00
|
|
|
friend class IncrementalFinalizeRunnable;
|
2013-06-18 12:02:07 -07:00
|
|
|
protected:
|
2014-02-19 08:02:13 -08:00
|
|
|
CycleCollectedJSRuntime(JSRuntime* aParentRuntime,
|
|
|
|
uint32_t aMaxbytes,
|
2013-09-04 14:06:54 -07:00
|
|
|
JSUseHelperThreads aUseHelperThreads);
|
2013-06-18 12:02:07 -07:00
|
|
|
virtual ~CycleCollectedJSRuntime();
|
|
|
|
|
2013-06-23 05:03:39 -07:00
|
|
|
size_t SizeOfExcludingThis(mozilla::MallocSizeOf aMallocSizeOf) const;
|
2013-06-18 12:02:16 -07:00
|
|
|
void UnmarkSkippableJSHolders();
|
|
|
|
|
2013-08-03 16:55:40 -07:00
|
|
|
virtual void TraverseAdditionalNativeRoots(nsCycleCollectionNoteRootCallback& aCb) {}
|
|
|
|
virtual void TraceAdditionalNativeGrayRoots(JSTracer* aTracer) {}
|
2013-06-18 12:02:15 -07:00
|
|
|
|
2013-07-09 07:28:15 -07:00
|
|
|
virtual void CustomGCCallback(JSGCStatus aStatus) {}
|
2013-08-03 16:55:39 -07:00
|
|
|
virtual bool CustomContextCallback(JSContext* aCx, unsigned aOperation)
|
|
|
|
{
|
|
|
|
return true; // Don't block context creation.
|
|
|
|
}
|
2013-07-09 07:28:15 -07:00
|
|
|
|
2013-06-18 12:02:15 -07:00
|
|
|
private:
|
2013-06-18 12:02:15 -07:00
|
|
|
|
|
|
|
void
|
|
|
|
DescribeGCThing(bool aIsMarked, void* aThing, JSGCTraceKind aTraceKind,
|
|
|
|
nsCycleCollectionTraversalCallback& aCb) const;
|
|
|
|
|
|
|
|
virtual bool
|
2013-09-11 05:49:05 -07:00
|
|
|
DescribeCustomObjects(JSObject* aObject, const js::Class* aClasp,
|
2013-08-03 16:55:40 -07:00
|
|
|
char (&aName)[72]) const
|
|
|
|
{
|
|
|
|
return false; // We did nothing.
|
|
|
|
}
|
2013-06-18 12:02:15 -07:00
|
|
|
|
|
|
|
void
|
|
|
|
NoteGCThingJSChildren(void* aThing, JSGCTraceKind aTraceKind,
|
|
|
|
nsCycleCollectionTraversalCallback& aCb) const;
|
|
|
|
|
|
|
|
void
|
2013-09-11 05:49:05 -07:00
|
|
|
NoteGCThingXPCOMChildren(const js::Class* aClasp, JSObject* aObj,
|
2013-06-18 12:02:15 -07:00
|
|
|
nsCycleCollectionTraversalCallback& aCb) const;
|
|
|
|
|
|
|
|
virtual bool
|
2013-09-11 05:49:05 -07:00
|
|
|
NoteCustomGCThingXPCOMChildren(const js::Class* aClasp, JSObject* aObj,
|
2013-08-03 16:55:40 -07:00
|
|
|
nsCycleCollectionTraversalCallback& aCb) const
|
|
|
|
{
|
|
|
|
return false; // We did nothing.
|
|
|
|
}
|
2013-06-18 12:02:15 -07:00
|
|
|
|
|
|
|
enum TraverseSelect {
|
2014-05-05 10:30:39 -07:00
|
|
|
TRAVERSE_CPP,
|
|
|
|
TRAVERSE_FULL
|
2013-06-18 12:02:15 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
void
|
|
|
|
TraverseGCThing(TraverseSelect aTs, void* aThing,
|
|
|
|
JSGCTraceKind aTraceKind,
|
|
|
|
nsCycleCollectionTraversalCallback& aCb);
|
|
|
|
|
|
|
|
void
|
|
|
|
TraverseZone(JS::Zone* aZone, nsCycleCollectionTraversalCallback& aCb);
|
|
|
|
|
|
|
|
static void
|
|
|
|
TraverseObjectShim(void* aData, void* aThing);
|
|
|
|
|
2013-06-18 12:02:15 -07:00
|
|
|
void TraverseNativeRoots(nsCycleCollectionNoteRootCallback& aCb);
|
2013-06-18 12:02:15 -07:00
|
|
|
|
2013-06-18 12:02:16 -07:00
|
|
|
static void TraceBlackJS(JSTracer* aTracer, void* aData);
|
2013-06-18 12:02:15 -07:00
|
|
|
static void TraceGrayJS(JSTracer* aTracer, void* aData);
|
2013-07-09 07:28:15 -07:00
|
|
|
static void GCCallback(JSRuntime* aRuntime, JSGCStatus aStatus, void* aData);
|
2013-08-08 15:53:04 -07:00
|
|
|
static bool ContextCallback(JSContext* aCx, unsigned aOperation,
|
|
|
|
void* aData);
|
2013-06-18 12:02:15 -07:00
|
|
|
|
2013-06-18 12:02:16 -07:00
|
|
|
virtual void TraceNativeBlackRoots(JSTracer* aTracer) { };
|
|
|
|
void TraceNativeGrayRoots(JSTracer* aTracer);
|
2013-06-18 12:02:15 -07:00
|
|
|
|
2013-07-09 07:28:15 -07:00
|
|
|
enum DeferredFinalizeType {
|
|
|
|
FinalizeIncrementally,
|
|
|
|
FinalizeNow,
|
|
|
|
};
|
|
|
|
|
|
|
|
void FinalizeDeferredThings(DeferredFinalizeType aType);
|
|
|
|
|
|
|
|
void OnGC(JSGCStatus aStatus);
|
|
|
|
|
2013-06-18 12:02:14 -07:00
|
|
|
public:
|
|
|
|
void AddJSHolder(void* aHolder, nsScriptObjectTracer* aTracer);
|
|
|
|
void RemoveJSHolder(void* aHolder);
|
|
|
|
#ifdef DEBUG
|
2013-08-15 10:29:02 -07:00
|
|
|
bool IsJSHolder(void* aHolder);
|
2013-06-18 12:02:14 -07:00
|
|
|
void AssertNoObjectsToTrace(void* aPossibleJSHolder);
|
|
|
|
#endif
|
|
|
|
|
2013-09-08 20:28:50 -07:00
|
|
|
already_AddRefed<nsIException> GetPendingException() const;
|
|
|
|
void SetPendingException(nsIException* aException);
|
|
|
|
|
2013-08-01 18:29:05 -07:00
|
|
|
nsCycleCollectionParticipant* GCThingParticipant();
|
|
|
|
nsCycleCollectionParticipant* ZoneParticipant();
|
2013-06-18 12:02:15 -07:00
|
|
|
|
2014-01-07 17:33:47 -08:00
|
|
|
nsresult TraverseRoots(nsCycleCollectionNoteRootCallback &aCb);
|
2013-06-18 12:02:16 -07:00
|
|
|
bool UsefulToMergeZones() const;
|
2013-06-18 12:02:15 -07:00
|
|
|
void FixWeakMappingGrayBits() const;
|
|
|
|
bool NeedCollect() const;
|
2013-06-18 12:02:16 -07:00
|
|
|
void Collect(uint32_t reason) const;
|
2013-06-18 12:02:15 -07:00
|
|
|
|
2013-07-09 07:28:15 -07:00
|
|
|
void DeferredFinalize(DeferredFinalizeAppendFunction aAppendFunc,
|
|
|
|
DeferredFinalizeFunction aFunc,
|
|
|
|
void* aThing);
|
|
|
|
void DeferredFinalize(nsISupports* aSupports);
|
|
|
|
|
2013-08-03 16:55:39 -07:00
|
|
|
void DumpJSHeap(FILE* aFile);
|
2013-11-20 14:35:16 -08:00
|
|
|
|
|
|
|
virtual void PrepareForForgetSkippable() = 0;
|
|
|
|
virtual void BeginCycleCollectionCallback() = 0;
|
|
|
|
virtual void EndCycleCollectionCallback(CycleCollectorResults &aResults) = 0;
|
2013-08-03 16:55:39 -07:00
|
|
|
virtual void DispatchDeferredDeletion(bool aContinuation) = 0;
|
2013-08-03 16:55:39 -07:00
|
|
|
|
2013-08-30 02:47:19 -07:00
|
|
|
JSRuntime* Runtime() const
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(mJSRuntime);
|
|
|
|
return mJSRuntime;
|
|
|
|
}
|
2013-09-08 20:28:50 -07:00
|
|
|
|
|
|
|
// Get the current thread's CycleCollectedJSRuntime. Returns null if there
|
|
|
|
// isn't one.
|
2013-09-08 20:29:21 -07:00
|
|
|
static CycleCollectedJSRuntime* Get();
|
2013-09-08 20:28:50 -07:00
|
|
|
|
2013-06-18 12:02:07 -07:00
|
|
|
private:
|
2013-08-01 18:29:05 -07:00
|
|
|
JSGCThingParticipant mGCThingCycleCollectorGlobal;
|
2013-06-18 12:02:15 -07:00
|
|
|
|
2013-08-01 18:29:05 -07:00
|
|
|
JSZoneParticipant mJSZoneCycleCollectorGlobal;
|
2013-06-18 12:02:15 -07:00
|
|
|
|
2013-06-18 12:02:07 -07:00
|
|
|
JSRuntime* mJSRuntime;
|
2013-06-18 12:02:14 -07:00
|
|
|
|
2013-06-18 12:02:16 -07:00
|
|
|
nsDataHashtable<nsPtrHashKey<void>, nsScriptObjectTracer*> mJSHolders;
|
|
|
|
|
2013-07-09 07:28:15 -07:00
|
|
|
nsTArray<nsISupports*> mDeferredSupports;
|
|
|
|
typedef nsDataHashtable<nsFuncPtrHashKey<DeferredFinalizeFunction>, void*>
|
|
|
|
DeferredFinalizerTable;
|
|
|
|
DeferredFinalizerTable mDeferredFinalizerTable;
|
|
|
|
|
|
|
|
nsRefPtr<IncrementalFinalizeRunnable> mFinalizeRunnable;
|
|
|
|
|
2013-09-08 20:28:50 -07:00
|
|
|
nsCOMPtr<nsIException> mPendingException;
|
2013-06-18 12:02:07 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace mozilla
|
|
|
|
|
|
|
|
#endif // mozilla_CycleCollectedJSRuntime_h__
|