2013-06-18 23:02:07 +04: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 16:03:39 +04:00
|
|
|
#include "mozilla/MemoryReporting.h"
|
2013-06-18 23:02:15 +04:00
|
|
|
#include "jsprvtd.h"
|
2013-06-18 23:02:07 +04:00
|
|
|
#include "jsapi.h"
|
|
|
|
|
2013-07-09 18:28:15 +04:00
|
|
|
#include "nsCycleCollector.h"
|
2013-06-18 23:02:15 +04:00
|
|
|
#include "nsCycleCollectionParticipant.h"
|
2013-06-18 23:02:14 +04:00
|
|
|
#include "nsDataHashtable.h"
|
|
|
|
#include "nsHashKeys.h"
|
2013-07-09 18:28:15 +04:00
|
|
|
#include "nsTArray.h"
|
2013-06-18 23:02:14 +04:00
|
|
|
|
2013-06-18 23:02:15 +04:00
|
|
|
class nsCycleCollectionNoteRootCallback;
|
2013-06-18 23:02:14 +04:00
|
|
|
class nsScriptObjectTracer;
|
|
|
|
|
2013-06-18 23:02:07 +04:00
|
|
|
namespace mozilla {
|
|
|
|
|
2013-06-18 23:02:15 +04:00
|
|
|
class JSGCThingParticipant: public nsCycleCollectionParticipant
|
|
|
|
{
|
|
|
|
public:
|
2013-08-02 05:29:05 +04:00
|
|
|
NS_IMETHOD Root(void *n)
|
2013-06-18 23:02:15 +04:00
|
|
|
{
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-08-02 05:29:05 +04:00
|
|
|
NS_IMETHOD Unlink(void *n)
|
2013-06-18 23:02:15 +04:00
|
|
|
{
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-08-02 05:29:05 +04:00
|
|
|
NS_IMETHOD Unroot(void *n)
|
2013-06-18 23:02:15 +04:00
|
|
|
{
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-08-02 05:29:05 +04:00
|
|
|
NS_IMETHOD_(void) DeleteCycleCollectable(void *n)
|
2013-06-18 23:02:15 +04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2013-08-02 05:29:05 +04:00
|
|
|
NS_IMETHOD Traverse(void *n, nsCycleCollectionTraversalCallback &cb);
|
2013-06-18 23:02:15 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
class JSZoneParticipant : public nsCycleCollectionParticipant
|
|
|
|
{
|
|
|
|
public:
|
2013-08-02 05:29:05 +04:00
|
|
|
MOZ_CONSTEXPR JSZoneParticipant(): nsCycleCollectionParticipant() {}
|
2013-06-18 23:02:15 +04:00
|
|
|
|
2013-08-02 05:29:05 +04:00
|
|
|
NS_IMETHOD Root(void *p)
|
2013-06-18 23:02:15 +04:00
|
|
|
{
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-08-02 05:29:05 +04:00
|
|
|
NS_IMETHOD Unlink(void *p)
|
2013-06-18 23:02:15 +04:00
|
|
|
{
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-08-02 05:29:05 +04:00
|
|
|
NS_IMETHOD Unroot(void *p)
|
2013-06-18 23:02:15 +04:00
|
|
|
{
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-08-02 05:29:05 +04:00
|
|
|
NS_IMETHOD_(void) DeleteCycleCollectable(void *n)
|
2013-06-18 23:02:15 +04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2013-08-02 05:29:05 +04:00
|
|
|
NS_IMETHOD Traverse(void *p, nsCycleCollectionTraversalCallback &cb);
|
2013-06-18 23:02:15 +04:00
|
|
|
};
|
|
|
|
|
2013-07-09 18:28:15 +04:00
|
|
|
class IncrementalFinalizeRunnable;
|
|
|
|
|
2013-06-18 23:02:07 +04:00
|
|
|
class CycleCollectedJSRuntime
|
|
|
|
{
|
2013-06-18 23:02:15 +04:00
|
|
|
friend class JSGCThingParticipant;
|
|
|
|
friend class JSZoneParticipant;
|
2013-07-09 18:28:15 +04:00
|
|
|
friend class IncrementalFinalizeRunnable;
|
2013-06-18 23:02:07 +04:00
|
|
|
protected:
|
|
|
|
CycleCollectedJSRuntime(uint32_t aMaxbytes,
|
2013-06-18 23:02:15 +04:00
|
|
|
JSUseHelperThreads aUseHelperThreads,
|
|
|
|
bool aExpectRootedGlobals);
|
2013-06-18 23:02:07 +04:00
|
|
|
virtual ~CycleCollectedJSRuntime();
|
|
|
|
|
|
|
|
JSRuntime* Runtime() const
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(mJSRuntime);
|
|
|
|
return mJSRuntime;
|
|
|
|
}
|
|
|
|
|
2013-06-23 16:03:39 +04:00
|
|
|
size_t SizeOfExcludingThis(mozilla::MallocSizeOf aMallocSizeOf) const;
|
2013-06-18 23:02:16 +04:00
|
|
|
void UnmarkSkippableJSHolders();
|
|
|
|
|
2013-06-18 23:02:15 +04:00
|
|
|
virtual void TraverseAdditionalNativeRoots(nsCycleCollectionNoteRootCallback& aCb) = 0;
|
2013-06-18 23:02:16 +04:00
|
|
|
virtual void TraceAdditionalNativeGrayRoots(JSTracer* aTracer) = 0;
|
2013-06-18 23:02:15 +04:00
|
|
|
|
2013-07-09 18:28:15 +04:00
|
|
|
virtual void CustomGCCallback(JSGCStatus aStatus) {}
|
|
|
|
|
2013-06-18 23:02:15 +04:00
|
|
|
private:
|
2013-06-18 23:02:15 +04:00
|
|
|
|
|
|
|
void
|
|
|
|
DescribeGCThing(bool aIsMarked, void* aThing, JSGCTraceKind aTraceKind,
|
|
|
|
nsCycleCollectionTraversalCallback& aCb) const;
|
|
|
|
|
|
|
|
virtual bool
|
|
|
|
DescribeCustomObjects(JSObject* aObject, js::Class* aClasp,
|
|
|
|
char (&aName)[72]) const = 0;
|
|
|
|
|
|
|
|
void
|
|
|
|
NoteGCThingJSChildren(void* aThing, JSGCTraceKind aTraceKind,
|
|
|
|
nsCycleCollectionTraversalCallback& aCb) const;
|
|
|
|
|
|
|
|
void
|
|
|
|
NoteGCThingXPCOMChildren(js::Class* aClasp, JSObject* aObj,
|
|
|
|
nsCycleCollectionTraversalCallback& aCb) const;
|
|
|
|
|
|
|
|
virtual bool
|
|
|
|
NoteCustomGCThingXPCOMChildren(js::Class* aClasp, JSObject* aObj,
|
|
|
|
nsCycleCollectionTraversalCallback& aCb) const = 0;
|
|
|
|
|
|
|
|
|
|
|
|
enum TraverseSelect {
|
|
|
|
TRAVERSE_CPP,
|
|
|
|
TRAVERSE_FULL
|
|
|
|
};
|
|
|
|
|
|
|
|
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 23:02:15 +04:00
|
|
|
void MaybeTraverseGlobals(nsCycleCollectionNoteRootCallback& aCb) const;
|
|
|
|
|
|
|
|
void TraverseNativeRoots(nsCycleCollectionNoteRootCallback& aCb);
|
2013-06-18 23:02:15 +04:00
|
|
|
|
2013-06-18 23:02:15 +04:00
|
|
|
void MaybeTraceGlobals(JSTracer* aTracer) const;
|
|
|
|
|
2013-06-18 23:02:16 +04:00
|
|
|
|
|
|
|
static void TraceBlackJS(JSTracer* aTracer, void* aData);
|
2013-06-18 23:02:15 +04:00
|
|
|
static void TraceGrayJS(JSTracer* aTracer, void* aData);
|
2013-07-09 18:28:15 +04:00
|
|
|
static void GCCallback(JSRuntime* aRuntime, JSGCStatus aStatus, void* aData);
|
2013-06-18 23:02:15 +04:00
|
|
|
|
2013-06-18 23:02:16 +04:00
|
|
|
virtual void TraceNativeBlackRoots(JSTracer* aTracer) { };
|
|
|
|
void TraceNativeGrayRoots(JSTracer* aTracer);
|
2013-06-18 23:02:15 +04:00
|
|
|
|
2013-07-09 18:28:15 +04:00
|
|
|
enum DeferredFinalizeType {
|
|
|
|
FinalizeIncrementally,
|
|
|
|
FinalizeNow,
|
|
|
|
};
|
|
|
|
|
|
|
|
void FinalizeDeferredThings(DeferredFinalizeType aType);
|
|
|
|
|
|
|
|
void OnGC(JSGCStatus aStatus);
|
|
|
|
|
2013-06-18 23:02:14 +04:00
|
|
|
public:
|
|
|
|
void AddJSHolder(void* aHolder, nsScriptObjectTracer* aTracer);
|
|
|
|
void RemoveJSHolder(void* aHolder);
|
|
|
|
#ifdef DEBUG
|
|
|
|
bool TestJSHolder(void* aHolder);
|
|
|
|
void SetObjectToUnlink(void* aObject) { mObjectToUnlink = aObject; }
|
|
|
|
void AssertNoObjectsToTrace(void* aPossibleJSHolder);
|
|
|
|
#endif
|
|
|
|
|
2013-06-18 23:02:15 +04:00
|
|
|
// This returns the singleton nsCycleCollectionParticipant for JSContexts.
|
2013-06-18 23:02:15 +04:00
|
|
|
static nsCycleCollectionParticipant* JSContextParticipant();
|
|
|
|
|
2013-08-02 05:29:05 +04:00
|
|
|
nsCycleCollectionParticipant* GCThingParticipant();
|
|
|
|
nsCycleCollectionParticipant* ZoneParticipant();
|
2013-06-18 23:02:15 +04:00
|
|
|
|
|
|
|
bool NotifyLeaveMainThread() const;
|
|
|
|
void NotifyEnterCycleCollectionThread() const;
|
|
|
|
void NotifyLeaveCycleCollectionThread() const;
|
|
|
|
void NotifyEnterMainThread() const;
|
|
|
|
nsresult BeginCycleCollection(nsCycleCollectionNoteRootCallback &aCb);
|
2013-06-18 23:02:16 +04:00
|
|
|
bool UsefulToMergeZones() const;
|
2013-06-18 23:02:15 +04:00
|
|
|
void FixWeakMappingGrayBits() const;
|
|
|
|
bool NeedCollect() const;
|
2013-06-18 23:02:16 +04:00
|
|
|
void Collect(uint32_t reason) const;
|
2013-06-18 23:02:15 +04:00
|
|
|
|
2013-06-20 01:06:50 +04:00
|
|
|
virtual void PrepareForForgetSkippable() {}
|
|
|
|
virtual void PrepareForCollection() {}
|
|
|
|
|
2013-07-09 18:28:15 +04:00
|
|
|
void DeferredFinalize(DeferredFinalizeAppendFunction aAppendFunc,
|
|
|
|
DeferredFinalizeFunction aFunc,
|
|
|
|
void* aThing);
|
|
|
|
void DeferredFinalize(nsISupports* aSupports);
|
|
|
|
|
2013-06-18 23:02:07 +04:00
|
|
|
private:
|
2013-08-02 05:29:05 +04:00
|
|
|
JSGCThingParticipant mGCThingCycleCollectorGlobal;
|
2013-06-18 23:02:15 +04:00
|
|
|
|
2013-08-02 05:29:05 +04:00
|
|
|
JSZoneParticipant mJSZoneCycleCollectorGlobal;
|
2013-06-18 23:02:15 +04:00
|
|
|
|
2013-06-18 23:02:07 +04:00
|
|
|
JSRuntime* mJSRuntime;
|
2013-06-18 23:02:14 +04:00
|
|
|
|
2013-06-18 23:02:16 +04:00
|
|
|
nsDataHashtable<nsPtrHashKey<void>, nsScriptObjectTracer*> mJSHolders;
|
|
|
|
|
2013-07-09 18:28:15 +04:00
|
|
|
nsTArray<nsISupports*> mDeferredSupports;
|
|
|
|
typedef nsDataHashtable<nsFuncPtrHashKey<DeferredFinalizeFunction>, void*>
|
|
|
|
DeferredFinalizerTable;
|
|
|
|
DeferredFinalizerTable mDeferredFinalizerTable;
|
|
|
|
|
|
|
|
nsRefPtr<IncrementalFinalizeRunnable> mFinalizeRunnable;
|
|
|
|
|
2013-06-18 23:02:14 +04:00
|
|
|
#ifdef DEBUG
|
|
|
|
void* mObjectToUnlink;
|
2013-06-18 23:02:15 +04:00
|
|
|
bool mExpectUnrootedGlobals;
|
2013-06-18 23:02:14 +04:00
|
|
|
#endif
|
2013-06-18 23:02:07 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace mozilla
|
|
|
|
|
|
|
|
#endif // mozilla_CycleCollectedJSRuntime_h__
|