2007-01-05 01:31:26 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
2007-04-26 01:12:11 +04:00
|
|
|
/* vim: set cindent tabstop=4 expandtab shiftwidth=4: */
|
2012-05-21 15:12:37 +04:00
|
|
|
/* 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/. */
|
2007-01-05 01:31:26 +03:00
|
|
|
|
|
|
|
//
|
|
|
|
// This file implements a garbage-cycle collector based on the paper
|
2012-11-07 05:38:29 +04:00
|
|
|
//
|
2007-01-05 01:31:26 +03:00
|
|
|
// Concurrent Cycle Collection in Reference Counted Systems
|
|
|
|
// Bacon & Rajan (2001), ECOOP 2001 / Springer LNCS vol 2072
|
|
|
|
//
|
|
|
|
// We are not using the concurrent or acyclic cases of that paper; so
|
|
|
|
// the green, red and orange colors are not used.
|
|
|
|
//
|
|
|
|
// The collector is based on tracking pointers of four colors:
|
|
|
|
//
|
|
|
|
// Black nodes are definitely live. If we ever determine a node is
|
|
|
|
// black, it's ok to forget about, drop from our records.
|
|
|
|
//
|
|
|
|
// White nodes are definitely garbage cycles. Once we finish with our
|
|
|
|
// scanning, we unlink all the white nodes and expect that by
|
|
|
|
// unlinking them they will self-destruct (since a garbage cycle is
|
|
|
|
// only keeping itself alive with internal links, by definition).
|
|
|
|
//
|
|
|
|
// Grey nodes are being scanned. Nodes that turn grey will turn
|
|
|
|
// either black if we determine that they're live, or white if we
|
|
|
|
// determine that they're a garbage cycle. After the main collection
|
|
|
|
// algorithm there should be no grey nodes.
|
|
|
|
//
|
|
|
|
// Purple nodes are *candidates* for being scanned. They are nodes we
|
|
|
|
// haven't begun scanning yet because they're not old enough, or we're
|
|
|
|
// still partway through the algorithm.
|
|
|
|
//
|
|
|
|
// XPCOM objects participating in garbage-cycle collection are obliged
|
|
|
|
// to inform us when they ought to turn purple; that is, when their
|
|
|
|
// refcount transitions from N+1 -> N, for nonzero N. Furthermore we
|
|
|
|
// require that *after* an XPCOM object has informed us of turning
|
|
|
|
// purple, they will tell us when they either transition back to being
|
|
|
|
// black (incremented refcount) or are ultimately deleted.
|
|
|
|
|
|
|
|
|
|
|
|
// Safety:
|
|
|
|
//
|
|
|
|
// An XPCOM object is either scan-safe or scan-unsafe, purple-safe or
|
|
|
|
// purple-unsafe.
|
|
|
|
//
|
|
|
|
// An object is scan-safe if:
|
|
|
|
//
|
2007-05-24 18:10:02 +04:00
|
|
|
// - It can be QI'ed to |nsXPCOMCycleCollectionParticipant|, though this
|
2007-01-05 01:31:26 +03:00
|
|
|
// operation loses ISupports identity (like nsIClassInfo).
|
|
|
|
// - The operation |traverse| on the resulting
|
2007-05-24 18:10:02 +04:00
|
|
|
// nsXPCOMCycleCollectionParticipant does not cause *any* refcount
|
2007-01-05 01:31:26 +03:00
|
|
|
// adjustment to occur (no AddRef / Release calls).
|
|
|
|
//
|
|
|
|
// An object is purple-safe if it satisfies the following properties:
|
|
|
|
//
|
|
|
|
// - The object is scan-safe.
|
|
|
|
// - If the object calls |nsCycleCollector::suspect(this)|,
|
|
|
|
// it will eventually call |nsCycleCollector::forget(this)|,
|
|
|
|
// exactly once per call to |suspect|, before being destroyed.
|
|
|
|
//
|
|
|
|
// When we receive a pointer |ptr| via
|
|
|
|
// |nsCycleCollector::suspect(ptr)|, we assume it is purple-safe. We
|
|
|
|
// can check the scan-safety, but have no way to ensure the
|
|
|
|
// purple-safety; objects must obey, or else the entire system falls
|
|
|
|
// apart. Don't involve an object in this scheme if you can't
|
|
|
|
// guarantee its purple-safety.
|
|
|
|
//
|
|
|
|
// When we have a scannable set of purple nodes ready, we begin
|
|
|
|
// our walks. During the walks, the nodes we |traverse| should only
|
|
|
|
// feed us more scan-safe nodes, and should not adjust the refcounts
|
|
|
|
// of those nodes.
|
|
|
|
//
|
|
|
|
// We do not |AddRef| or |Release| any objects during scanning. We
|
|
|
|
// rely on purple-safety of the roots that call |suspect| and
|
|
|
|
// |forget| to hold, such that we will forget about a purple pointer
|
|
|
|
// before it is destroyed. The pointers that are merely scan-safe,
|
|
|
|
// we hold only for the duration of scanning, and there should be no
|
|
|
|
// objects released from the scan-safe set during the scan (there
|
|
|
|
// should be no threads involved).
|
|
|
|
//
|
|
|
|
// We *do* call |AddRef| and |Release| on every white object, on
|
|
|
|
// either side of the calls to |Unlink|. This keeps the set of white
|
|
|
|
// objects alive during the unlinking.
|
|
|
|
//
|
|
|
|
|
2011-05-01 22:59:24 +04:00
|
|
|
#if !defined(__MINGW32__)
|
2007-01-05 01:31:26 +03:00
|
|
|
#ifdef WIN32
|
|
|
|
#include <crtdbg.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#endif
|
2007-01-23 03:39:25 +03:00
|
|
|
#endif
|
2007-01-05 01:31:26 +03:00
|
|
|
|
2011-10-07 00:22:43 +04:00
|
|
|
#include "base/process_util.h"
|
2011-10-11 09:50:08 +04:00
|
|
|
|
|
|
|
/* This must occur *after* base/process_util.h to avoid typedefs conflicts. */
|
|
|
|
#include "mozilla/Util.h"
|
|
|
|
|
2007-01-05 01:31:26 +03:00
|
|
|
#include "nsCycleCollectionParticipant.h"
|
2010-11-12 01:52:30 +03:00
|
|
|
#include "nsCycleCollectorUtils.h"
|
2007-01-05 01:31:26 +03:00
|
|
|
#include "nsIProgrammingLanguage.h"
|
|
|
|
#include "nsBaseHashtable.h"
|
|
|
|
#include "nsHashKeys.h"
|
|
|
|
#include "nsDeque.h"
|
|
|
|
#include "nsCycleCollector.h"
|
|
|
|
#include "nsThreadUtils.h"
|
|
|
|
#include "prenv.h"
|
|
|
|
#include "prprf.h"
|
|
|
|
#include "plstr.h"
|
2007-03-06 00:07:51 +03:00
|
|
|
#include "nsPrintfCString.h"
|
2007-03-27 13:49:06 +04:00
|
|
|
#include "nsTArray.h"
|
2007-05-11 00:21:12 +04:00
|
|
|
#include "nsIObserverService.h"
|
2008-01-07 01:05:10 +03:00
|
|
|
#include "nsIConsoleService.h"
|
2007-05-11 00:21:12 +04:00
|
|
|
#include "nsServiceManagerUtils.h"
|
2008-01-07 01:05:10 +03:00
|
|
|
#include "nsThreadUtils.h"
|
2009-01-22 07:15:34 +03:00
|
|
|
#include "nsTArray.h"
|
Bug 560095 - Use mozilla::services::GetObserverService(). r=biesi,dveditz,gavin,josh,jst,mrbkap,roc,sdwilsh,shaver,sicking,smontagu,surkov
2010-04-29 20:59:13 +04:00
|
|
|
#include "mozilla/Services.h"
|
2012-06-06 03:51:58 +04:00
|
|
|
#include "mozilla/Attributes.h"
|
2010-08-12 04:03:23 +04:00
|
|
|
#include "nsICycleCollectorListener.h"
|
2010-11-12 01:52:30 +03:00
|
|
|
#include "nsIXPConnect.h"
|
|
|
|
#include "nsIJSRuntimeService.h"
|
2011-07-09 02:49:31 +04:00
|
|
|
#include "nsIMemoryReporter.h"
|
2012-10-16 06:12:14 +04:00
|
|
|
#include "nsIFile.h"
|
|
|
|
#include "nsDirectoryServiceDefs.h"
|
2011-03-29 00:05:48 +04:00
|
|
|
#include "xpcpublic.h"
|
2012-01-02 02:34:00 +04:00
|
|
|
#include "nsXPCOMPrivate.h"
|
2012-02-01 01:47:47 +04:00
|
|
|
#include "sampler.h"
|
2007-01-05 01:31:26 +03:00
|
|
|
#include <stdio.h>
|
2008-03-18 02:11:08 +03:00
|
|
|
#include <string.h>
|
2007-01-05 01:31:26 +03:00
|
|
|
#ifdef WIN32
|
|
|
|
#include <io.h>
|
|
|
|
#include <process.h>
|
|
|
|
#endif
|
2012-10-31 18:20:26 +04:00
|
|
|
#ifdef ANDROID
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#endif
|
2007-01-05 01:31:26 +03:00
|
|
|
|
2010-11-12 01:52:30 +03:00
|
|
|
#ifdef XP_WIN
|
|
|
|
#include <windows.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "mozilla/CondVar.h"
|
2012-10-26 17:32:10 +04:00
|
|
|
#include "mozilla/Likely.h"
|
|
|
|
#include "mozilla/Mutex.h"
|
2012-04-12 04:17:44 +04:00
|
|
|
#include "mozilla/StandardInteger.h"
|
2011-06-21 01:47:58 +04:00
|
|
|
#include "mozilla/Telemetry.h"
|
2010-11-12 01:52:30 +03:00
|
|
|
|
|
|
|
using namespace mozilla;
|
|
|
|
|
2010-07-16 05:08:47 +04:00
|
|
|
//#define COLLECT_TIME_DEBUG
|
|
|
|
|
2009-10-23 08:47:27 +04:00
|
|
|
#define DEFAULT_SHUTDOWN_COLLECTIONS 5
|
2007-04-21 21:43:19 +04:00
|
|
|
#ifdef DEBUG_CC
|
|
|
|
#define SHUTDOWN_COLLECTIONS(params) params.mShutdownCollections
|
|
|
|
#else
|
|
|
|
#define SHUTDOWN_COLLECTIONS(params) DEFAULT_SHUTDOWN_COLLECTIONS
|
|
|
|
#endif
|
2007-01-05 01:31:26 +03:00
|
|
|
|
2010-11-12 01:52:30 +03:00
|
|
|
#if defined(XP_WIN)
|
|
|
|
// Defined in nsThreadManager.cpp.
|
|
|
|
extern DWORD gTLSThreadIDIndex;
|
|
|
|
#elif defined(NS_TLS)
|
|
|
|
// Defined in nsThreadManager.cpp.
|
|
|
|
extern NS_TLS mozilla::threads::ID gTLSThreadID;
|
|
|
|
#else
|
2012-07-30 18:20:58 +04:00
|
|
|
PRThread* gCycleCollectorThread = nullptr;
|
2010-11-12 01:52:30 +03:00
|
|
|
#endif
|
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
// If true, always log cycle collector graphs.
|
2011-09-29 10:19:26 +04:00
|
|
|
const bool gAlwaysLogCCGraphs = false;
|
2011-08-17 21:35:40 +04:00
|
|
|
|
2012-02-21 00:13:29 +04:00
|
|
|
MOZ_NEVER_INLINE void
|
|
|
|
CC_AbortIfNull(void *ptr)
|
|
|
|
{
|
|
|
|
if (!ptr)
|
2012-06-08 01:41:11 +04:00
|
|
|
MOZ_CRASH();
|
2012-02-21 00:13:29 +04:00
|
|
|
}
|
|
|
|
|
2007-01-05 01:31:26 +03:00
|
|
|
// Various parameters of this collector can be tuned using environment
|
|
|
|
// variables.
|
|
|
|
|
|
|
|
struct nsCycleCollectorParams
|
|
|
|
{
|
2011-09-29 10:19:26 +04:00
|
|
|
bool mDoNothing;
|
|
|
|
bool mLogGraphs;
|
2007-04-21 21:43:19 +04:00
|
|
|
#ifdef DEBUG_CC
|
2011-09-29 10:19:26 +04:00
|
|
|
bool mReportStats;
|
|
|
|
bool mLogPointers;
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t mShutdownCollections;
|
2007-04-21 21:43:19 +04:00
|
|
|
#endif
|
2007-01-05 01:31:26 +03:00
|
|
|
|
|
|
|
nsCycleCollectorParams() :
|
2007-04-21 21:43:19 +04:00
|
|
|
#ifdef DEBUG_CC
|
2007-01-05 01:31:26 +03:00
|
|
|
mDoNothing (PR_GetEnv("XPCOM_CC_DO_NOTHING") != NULL),
|
2011-08-17 21:35:40 +04:00
|
|
|
mLogGraphs (gAlwaysLogCCGraphs ||
|
|
|
|
PR_GetEnv("XPCOM_CC_DRAW_GRAPHS") != NULL),
|
2007-01-05 01:31:26 +03:00
|
|
|
mReportStats (PR_GetEnv("XPCOM_CC_REPORT_STATS") != NULL),
|
|
|
|
mLogPointers (PR_GetEnv("XPCOM_CC_LOG_POINTERS") != NULL),
|
|
|
|
|
2009-05-07 00:46:04 +04:00
|
|
|
mShutdownCollections(DEFAULT_SHUTDOWN_COLLECTIONS)
|
2007-04-21 21:43:19 +04:00
|
|
|
#else
|
2011-10-17 18:59:28 +04:00
|
|
|
mDoNothing (false),
|
2011-08-17 21:35:40 +04:00
|
|
|
mLogGraphs (gAlwaysLogCCGraphs)
|
2007-04-21 21:43:19 +04:00
|
|
|
#endif
|
2007-01-05 01:31:26 +03:00
|
|
|
{
|
2007-04-21 21:43:19 +04:00
|
|
|
#ifdef DEBUG_CC
|
2009-05-07 00:46:04 +04:00
|
|
|
char *s = PR_GetEnv("XPCOM_CC_SHUTDOWN_COLLECTIONS");
|
2007-03-16 15:52:47 +03:00
|
|
|
if (s)
|
|
|
|
PR_sscanf(s, "%d", &mShutdownCollections);
|
2007-04-21 21:43:19 +04:00
|
|
|
#endif
|
2007-01-05 01:31:26 +03:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2007-04-21 21:43:19 +04:00
|
|
|
#ifdef DEBUG_CC
|
2007-01-05 01:31:26 +03:00
|
|
|
// Various operations involving the collector are recorded in a
|
|
|
|
// statistics table. These are for diagnostics.
|
|
|
|
|
|
|
|
struct nsCycleCollectorStats
|
|
|
|
{
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t mFailedQI;
|
|
|
|
uint32_t mSuccessfulQI;
|
2007-01-05 01:31:26 +03:00
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t mVisitedNode;
|
|
|
|
uint32_t mWalkedGraph;
|
|
|
|
uint32_t mFreedBytes;
|
2007-01-05 01:31:26 +03:00
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t mSetColorBlack;
|
|
|
|
uint32_t mSetColorWhite;
|
2007-01-05 01:31:26 +03:00
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t mFailedUnlink;
|
|
|
|
uint32_t mCollectedNode;
|
2007-01-05 01:31:26 +03:00
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t mSuspectNode;
|
|
|
|
uint32_t mForgetNode;
|
2007-01-05 01:31:26 +03:00
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t mCollection;
|
2007-01-05 01:31:26 +03:00
|
|
|
|
|
|
|
nsCycleCollectorStats()
|
|
|
|
{
|
|
|
|
memset(this, 0, sizeof(nsCycleCollectorStats));
|
|
|
|
}
|
|
|
|
|
|
|
|
void Dump()
|
|
|
|
{
|
|
|
|
fprintf(stderr, "\f\n");
|
|
|
|
#define DUMP(entry) fprintf(stderr, "%30.30s: %-20.20d\n", #entry, entry)
|
|
|
|
DUMP(mFailedQI);
|
|
|
|
DUMP(mSuccessfulQI);
|
|
|
|
|
|
|
|
DUMP(mVisitedNode);
|
|
|
|
DUMP(mWalkedGraph);
|
|
|
|
DUMP(mFreedBytes);
|
|
|
|
|
|
|
|
DUMP(mSetColorBlack);
|
|
|
|
DUMP(mSetColorWhite);
|
|
|
|
|
|
|
|
DUMP(mFailedUnlink);
|
|
|
|
DUMP(mCollectedNode);
|
|
|
|
|
|
|
|
DUMP(mSuspectNode);
|
|
|
|
DUMP(mForgetNode);
|
|
|
|
|
|
|
|
DUMP(mCollection);
|
|
|
|
#undef DUMP
|
|
|
|
}
|
|
|
|
};
|
2007-04-21 21:43:19 +04:00
|
|
|
#endif
|
2007-01-05 01:31:26 +03:00
|
|
|
|
2007-04-21 21:43:19 +04:00
|
|
|
#ifdef DEBUG_CC
|
2011-09-29 10:19:26 +04:00
|
|
|
static bool nsCycleCollector_shouldSuppress(nsISupports *s);
|
2007-04-21 21:43:19 +04:00
|
|
|
#endif
|
2007-01-05 01:31:26 +03:00
|
|
|
|
2012-02-12 20:02:01 +04:00
|
|
|
#ifdef COLLECT_TIME_DEBUG
|
|
|
|
class TimeLog
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
TimeLog() : mLastCheckpoint(TimeStamp::Now()) {}
|
|
|
|
|
|
|
|
void
|
|
|
|
Checkpoint(const char* aEvent)
|
|
|
|
{
|
|
|
|
TimeStamp now = TimeStamp::Now();
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t dur = (uint32_t) ((now - mLastCheckpoint).ToMilliseconds());
|
2012-02-12 20:02:01 +04:00
|
|
|
if (dur > 0) {
|
|
|
|
printf("cc: %s took %dms\n", aEvent, dur);
|
|
|
|
}
|
|
|
|
mLastCheckpoint = now;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
TimeStamp mLastCheckpoint;
|
|
|
|
};
|
|
|
|
#else
|
|
|
|
class TimeLog
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
TimeLog() {}
|
|
|
|
void Checkpoint(const char* aEvent) {}
|
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2007-01-05 01:31:26 +03:00
|
|
|
////////////////////////////////////////////////////////////////////////
|
|
|
|
// Base types
|
|
|
|
////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2007-04-26 01:12:11 +04:00
|
|
|
struct PtrInfo;
|
|
|
|
|
|
|
|
class EdgePool
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
// EdgePool allocates arrays of void*, primarily to hold PtrInfo*.
|
|
|
|
// However, at the end of a block, the last two pointers are a null
|
|
|
|
// and then a void** pointing to the next block. This allows
|
|
|
|
// EdgePool::Iterators to be a single word but still capable of crossing
|
|
|
|
// block boundaries.
|
|
|
|
|
|
|
|
EdgePool()
|
|
|
|
{
|
2012-07-30 18:20:58 +04:00
|
|
|
mSentinelAndBlocks[0].block = nullptr;
|
|
|
|
mSentinelAndBlocks[1].block = nullptr;
|
2007-04-26 01:12:11 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
~EdgePool()
|
2008-01-10 17:10:03 +03:00
|
|
|
{
|
2012-11-15 11:32:39 +04:00
|
|
|
MOZ_ASSERT(!mSentinelAndBlocks[0].block &&
|
|
|
|
!mSentinelAndBlocks[1].block,
|
|
|
|
"Didn't call Clear()?");
|
2008-01-10 17:10:03 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void Clear()
|
2007-04-26 01:12:11 +04:00
|
|
|
{
|
|
|
|
Block *b = Blocks();
|
|
|
|
while (b) {
|
|
|
|
Block *next = b->Next();
|
|
|
|
delete b;
|
|
|
|
b = next;
|
|
|
|
}
|
2008-01-10 17:10:03 +03:00
|
|
|
|
2012-07-30 18:20:58 +04:00
|
|
|
mSentinelAndBlocks[0].block = nullptr;
|
|
|
|
mSentinelAndBlocks[1].block = nullptr;
|
2007-04-26 01:12:11 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
struct Block;
|
|
|
|
union PtrInfoOrBlock {
|
|
|
|
// Use a union to avoid reinterpret_cast and the ensuing
|
|
|
|
// potential aliasing bugs.
|
|
|
|
PtrInfo *ptrInfo;
|
|
|
|
Block *block;
|
|
|
|
};
|
|
|
|
struct Block {
|
2011-06-18 03:19:41 +04:00
|
|
|
enum { BlockSize = 16 * 1024 };
|
2007-04-26 01:12:11 +04:00
|
|
|
|
|
|
|
PtrInfoOrBlock mPointers[BlockSize];
|
|
|
|
Block() {
|
2012-07-30 18:20:58 +04:00
|
|
|
mPointers[BlockSize - 2].block = nullptr; // sentinel
|
|
|
|
mPointers[BlockSize - 1].block = nullptr; // next block pointer
|
2007-04-26 01:12:11 +04:00
|
|
|
}
|
2012-11-07 05:38:29 +04:00
|
|
|
Block*& Next() { return mPointers[BlockSize - 1].block; }
|
|
|
|
PtrInfoOrBlock* Start() { return &mPointers[0]; }
|
|
|
|
PtrInfoOrBlock* End() { return &mPointers[BlockSize - 2]; }
|
2007-04-26 01:12:11 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
// Store the null sentinel so that we can have valid iterators
|
|
|
|
// before adding any edges and without adding any blocks.
|
|
|
|
PtrInfoOrBlock mSentinelAndBlocks[2];
|
|
|
|
|
2012-11-07 05:38:29 +04:00
|
|
|
Block*& Blocks() { return mSentinelAndBlocks[1].block; }
|
|
|
|
Block* Blocks() const { return mSentinelAndBlocks[1].block; }
|
2007-04-26 01:12:11 +04:00
|
|
|
|
|
|
|
public:
|
|
|
|
class Iterator
|
|
|
|
{
|
|
|
|
public:
|
2012-07-30 18:20:58 +04:00
|
|
|
Iterator() : mPointer(nullptr) {}
|
2007-04-26 01:12:11 +04:00
|
|
|
Iterator(PtrInfoOrBlock *aPointer) : mPointer(aPointer) {}
|
|
|
|
Iterator(const Iterator& aOther) : mPointer(aOther.mPointer) {}
|
|
|
|
|
|
|
|
Iterator& operator++()
|
|
|
|
{
|
2012-07-30 18:20:58 +04:00
|
|
|
if (mPointer->ptrInfo == nullptr) {
|
2007-04-26 01:12:11 +04:00
|
|
|
// Null pointer is a sentinel for link to the next block.
|
|
|
|
mPointer = (mPointer + 1)->block->mPointers;
|
|
|
|
}
|
|
|
|
++mPointer;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
PtrInfo* operator*() const
|
|
|
|
{
|
2012-07-30 18:20:58 +04:00
|
|
|
if (mPointer->ptrInfo == nullptr) {
|
2007-04-26 01:12:11 +04:00
|
|
|
// Null pointer is a sentinel for link to the next block.
|
|
|
|
return (mPointer + 1)->block->mPointers->ptrInfo;
|
|
|
|
}
|
|
|
|
return mPointer->ptrInfo;
|
|
|
|
}
|
2011-09-29 10:19:26 +04:00
|
|
|
bool operator==(const Iterator& aOther) const
|
2007-04-26 01:12:11 +04:00
|
|
|
{ return mPointer == aOther.mPointer; }
|
2011-09-29 10:19:26 +04:00
|
|
|
bool operator!=(const Iterator& aOther) const
|
2007-04-26 01:12:11 +04:00
|
|
|
{ return mPointer != aOther.mPointer; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
PtrInfoOrBlock *mPointer;
|
|
|
|
};
|
|
|
|
|
|
|
|
class Builder;
|
|
|
|
friend class Builder;
|
|
|
|
class Builder {
|
|
|
|
public:
|
|
|
|
Builder(EdgePool &aPool)
|
|
|
|
: mCurrent(&aPool.mSentinelAndBlocks[0]),
|
|
|
|
mBlockEnd(&aPool.mSentinelAndBlocks[0]),
|
2012-11-07 05:38:29 +04:00
|
|
|
mNextBlockPtr(&aPool.Blocks())
|
2007-04-26 01:12:11 +04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2007-05-04 03:07:26 +04:00
|
|
|
Iterator Mark() { return Iterator(mCurrent); }
|
2007-04-26 01:12:11 +04:00
|
|
|
|
|
|
|
void Add(PtrInfo* aEdge) {
|
|
|
|
if (mCurrent == mBlockEnd) {
|
2007-05-04 03:07:26 +04:00
|
|
|
Block *b = new Block();
|
2007-04-26 01:12:11 +04:00
|
|
|
if (!b) {
|
|
|
|
// This means we just won't collect (some) cycles.
|
|
|
|
NS_NOTREACHED("out of memory, ignoring edges");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
*mNextBlockPtr = b;
|
|
|
|
mCurrent = b->Start();
|
|
|
|
mBlockEnd = b->End();
|
|
|
|
mNextBlockPtr = &b->Next();
|
|
|
|
}
|
|
|
|
(mCurrent++)->ptrInfo = aEdge;
|
|
|
|
}
|
|
|
|
private:
|
|
|
|
// mBlockEnd points to space for null sentinel
|
|
|
|
PtrInfoOrBlock *mCurrent, *mBlockEnd;
|
2007-05-04 03:07:26 +04:00
|
|
|
Block **mNextBlockPtr;
|
2007-04-26 01:12:11 +04:00
|
|
|
};
|
|
|
|
|
2012-11-07 05:38:29 +04:00
|
|
|
size_t SizeOfExcludingThis(nsMallocSizeOfFun aMallocSizeOf) const {
|
|
|
|
size_t n = 0;
|
|
|
|
Block *b = Blocks();
|
|
|
|
while (b) {
|
|
|
|
n += aMallocSizeOf(b);
|
|
|
|
b = b->Next();
|
|
|
|
}
|
|
|
|
return n;
|
2011-07-09 02:49:31 +04:00
|
|
|
}
|
2007-04-26 01:12:11 +04:00
|
|
|
};
|
|
|
|
|
2007-01-05 01:31:26 +03:00
|
|
|
enum NodeColor { black, white, grey };
|
|
|
|
|
2007-04-20 12:01:01 +04:00
|
|
|
// This structure should be kept as small as possible; we may expect
|
2011-06-18 03:19:41 +04:00
|
|
|
// hundreds of thousands of them to be allocated and touched
|
|
|
|
// repeatedly during each cycle collection.
|
2007-04-20 12:01:01 +04:00
|
|
|
|
2007-01-05 01:31:26 +03:00
|
|
|
struct PtrInfo
|
|
|
|
{
|
2007-04-26 01:12:11 +04:00
|
|
|
void *mPointer;
|
2007-05-24 18:10:02 +04:00
|
|
|
nsCycleCollectionParticipant *mParticipant;
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t mColor : 2;
|
|
|
|
uint32_t mInternalRefs : 30;
|
|
|
|
uint32_t mRefCount;
|
2011-06-10 01:55:04 +04:00
|
|
|
private:
|
|
|
|
EdgePool::Iterator mFirstChild;
|
2007-04-20 12:01:01 +04:00
|
|
|
|
2011-06-10 01:55:04 +04:00
|
|
|
public:
|
2007-04-21 21:43:19 +04:00
|
|
|
#ifdef DEBUG_CC
|
2010-08-18 11:46:54 +04:00
|
|
|
char *mName;
|
2007-04-20 12:01:01 +04:00
|
|
|
#endif
|
2007-01-05 01:31:26 +03:00
|
|
|
|
2012-05-03 23:28:11 +04:00
|
|
|
PtrInfo(void *aPointer, nsCycleCollectionParticipant *aParticipant)
|
2007-04-26 01:12:11 +04:00
|
|
|
: mPointer(aPointer),
|
2007-05-24 18:10:02 +04:00
|
|
|
mParticipant(aParticipant),
|
2007-04-26 01:12:11 +04:00
|
|
|
mColor(grey),
|
|
|
|
mInternalRefs(0),
|
|
|
|
mRefCount(0),
|
2011-06-10 01:55:04 +04:00
|
|
|
mFirstChild()
|
2007-04-21 21:43:19 +04:00
|
|
|
#ifdef DEBUG_CC
|
2012-08-27 21:41:04 +04:00
|
|
|
, mName(nullptr)
|
2007-04-20 12:01:01 +04:00
|
|
|
#endif
|
2007-04-26 01:12:11 +04:00
|
|
|
{
|
2012-05-03 23:28:10 +04:00
|
|
|
MOZ_ASSERT(aParticipant);
|
2007-04-26 01:12:11 +04:00
|
|
|
}
|
2007-04-20 12:01:01 +04:00
|
|
|
|
2010-08-18 11:46:54 +04:00
|
|
|
#ifdef DEBUG_CC
|
2008-03-18 02:11:08 +03:00
|
|
|
void Destroy() {
|
|
|
|
PL_strfree(mName);
|
|
|
|
}
|
2010-08-18 11:46:54 +04:00
|
|
|
#endif
|
2008-03-18 02:11:08 +03:00
|
|
|
|
|
|
|
// Allow NodePool::Block's constructor to compile.
|
|
|
|
PtrInfo() {
|
|
|
|
NS_NOTREACHED("should never be called");
|
|
|
|
}
|
2011-06-10 01:55:04 +04:00
|
|
|
|
|
|
|
EdgePool::Iterator FirstChild()
|
|
|
|
{
|
|
|
|
return mFirstChild;
|
|
|
|
}
|
|
|
|
|
|
|
|
// this PtrInfo must be part of a NodePool
|
|
|
|
EdgePool::Iterator LastChild()
|
|
|
|
{
|
|
|
|
return (this + 1)->mFirstChild;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SetFirstChild(EdgePool::Iterator aFirstChild)
|
|
|
|
{
|
|
|
|
mFirstChild = aFirstChild;
|
|
|
|
}
|
|
|
|
|
|
|
|
// this PtrInfo must be part of a NodePool
|
|
|
|
void SetLastChild(EdgePool::Iterator aLastChild)
|
|
|
|
{
|
|
|
|
(this + 1)->mFirstChild = aLastChild;
|
|
|
|
}
|
2007-04-26 01:12:11 +04:00
|
|
|
};
|
2007-04-20 12:01:01 +04:00
|
|
|
|
2007-04-26 01:12:11 +04:00
|
|
|
/**
|
|
|
|
* A structure designed to be used like a linked list of PtrInfo, except
|
|
|
|
* that allocates the PtrInfo 32K-at-a-time.
|
|
|
|
*/
|
|
|
|
class NodePool
|
2007-04-20 12:01:01 +04:00
|
|
|
{
|
2007-04-26 01:12:11 +04:00
|
|
|
private:
|
2011-06-18 03:19:41 +04:00
|
|
|
enum { BlockSize = 8 * 1024 }; // could be int template parameter
|
2007-04-20 12:01:01 +04:00
|
|
|
|
2007-04-26 01:12:11 +04:00
|
|
|
struct Block {
|
2008-03-18 02:11:08 +03:00
|
|
|
// We create and destroy Block using NS_Alloc/NS_Free rather
|
|
|
|
// than new and delete to avoid calling its constructor and
|
|
|
|
// destructor.
|
2012-11-07 05:38:29 +04:00
|
|
|
Block() { NS_NOTREACHED("should never be called"); }
|
2008-03-18 02:11:08 +03:00
|
|
|
~Block() { NS_NOTREACHED("should never be called"); }
|
|
|
|
|
2007-04-26 01:12:11 +04:00
|
|
|
Block* mNext;
|
2011-06-10 01:55:04 +04:00
|
|
|
PtrInfo mEntries[BlockSize + 1]; // +1 to store last child of last node
|
2007-04-26 01:12:11 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
public:
|
|
|
|
NodePool()
|
2012-07-30 18:20:58 +04:00
|
|
|
: mBlocks(nullptr),
|
2012-11-07 05:38:29 +04:00
|
|
|
mLast(nullptr)
|
2007-04-20 12:01:01 +04:00
|
|
|
{
|
|
|
|
}
|
2007-04-26 01:12:11 +04:00
|
|
|
|
|
|
|
~NodePool()
|
2008-01-10 17:10:03 +03:00
|
|
|
{
|
2012-11-15 11:32:39 +04:00
|
|
|
MOZ_ASSERT(!mBlocks, "Didn't call Clear()?");
|
2008-01-10 17:10:03 +03:00
|
|
|
}
|
|
|
|
|
2010-08-18 11:46:54 +04:00
|
|
|
void Clear()
|
2007-04-20 12:01:01 +04:00
|
|
|
{
|
2010-08-18 11:46:54 +04:00
|
|
|
#ifdef DEBUG_CC
|
|
|
|
{
|
2007-06-07 02:07:29 +04:00
|
|
|
Enumerator queue(*this);
|
|
|
|
while (!queue.IsDone()) {
|
2008-03-18 02:11:08 +03:00
|
|
|
queue.GetNext()->Destroy();
|
2007-06-07 02:07:29 +04:00
|
|
|
}
|
|
|
|
}
|
2010-08-18 11:46:54 +04:00
|
|
|
#endif
|
2007-04-26 01:12:11 +04:00
|
|
|
Block *b = mBlocks;
|
|
|
|
while (b) {
|
|
|
|
Block *n = b->mNext;
|
2008-03-18 02:11:08 +03:00
|
|
|
NS_Free(b);
|
2007-04-26 01:12:11 +04:00
|
|
|
b = n;
|
2007-04-24 03:34:33 +04:00
|
|
|
}
|
2008-01-10 17:10:03 +03:00
|
|
|
|
2012-07-30 18:20:58 +04:00
|
|
|
mBlocks = nullptr;
|
|
|
|
mLast = nullptr;
|
2007-04-20 12:01:01 +04:00
|
|
|
}
|
|
|
|
|
2007-04-26 01:12:11 +04:00
|
|
|
class Builder;
|
|
|
|
friend class Builder;
|
|
|
|
class Builder {
|
|
|
|
public:
|
|
|
|
Builder(NodePool& aPool)
|
|
|
|
: mNextBlock(&aPool.mBlocks),
|
|
|
|
mNext(aPool.mLast),
|
2012-11-07 05:38:29 +04:00
|
|
|
mBlockEnd(nullptr)
|
2007-04-26 01:12:11 +04:00
|
|
|
{
|
2012-11-15 11:32:39 +04:00
|
|
|
MOZ_ASSERT(aPool.mBlocks == nullptr && aPool.mLast == nullptr,
|
|
|
|
"pool not empty");
|
2007-04-26 01:12:11 +04:00
|
|
|
}
|
2012-05-03 23:28:11 +04:00
|
|
|
PtrInfo *Add(void *aPointer, nsCycleCollectionParticipant *aParticipant)
|
2007-04-26 01:12:11 +04:00
|
|
|
{
|
|
|
|
if (mNext == mBlockEnd) {
|
|
|
|
Block *block;
|
2008-03-18 02:11:08 +03:00
|
|
|
if (!(*mNextBlock = block =
|
|
|
|
static_cast<Block*>(NS_Alloc(sizeof(Block)))))
|
2012-07-30 18:20:58 +04:00
|
|
|
return nullptr;
|
2007-04-26 01:12:11 +04:00
|
|
|
mNext = block->mEntries;
|
|
|
|
mBlockEnd = block->mEntries + BlockSize;
|
2012-07-30 18:20:58 +04:00
|
|
|
block->mNext = nullptr;
|
2007-04-26 01:12:11 +04:00
|
|
|
mNextBlock = &block->mNext;
|
|
|
|
}
|
2012-05-03 23:28:11 +04:00
|
|
|
return new (mNext++) PtrInfo(aPointer, aParticipant);
|
2007-04-26 01:12:11 +04:00
|
|
|
}
|
|
|
|
private:
|
|
|
|
Block **mNextBlock;
|
|
|
|
PtrInfo *&mNext;
|
|
|
|
PtrInfo *mBlockEnd;
|
|
|
|
};
|
|
|
|
|
|
|
|
class Enumerator;
|
|
|
|
friend class Enumerator;
|
|
|
|
class Enumerator {
|
|
|
|
public:
|
|
|
|
Enumerator(NodePool& aPool)
|
|
|
|
: mFirstBlock(aPool.mBlocks),
|
2012-07-30 18:20:58 +04:00
|
|
|
mCurBlock(nullptr),
|
|
|
|
mNext(nullptr),
|
|
|
|
mBlockEnd(nullptr),
|
2007-04-26 01:12:11 +04:00
|
|
|
mLast(aPool.mLast)
|
|
|
|
{
|
|
|
|
}
|
2007-04-20 12:01:01 +04:00
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool IsDone() const
|
2007-04-26 01:12:11 +04:00
|
|
|
{
|
|
|
|
return mNext == mLast;
|
|
|
|
}
|
2007-04-20 12:01:01 +04:00
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool AtBlockEnd() const
|
2011-06-10 01:55:29 +04:00
|
|
|
{
|
|
|
|
return mNext == mBlockEnd;
|
|
|
|
}
|
|
|
|
|
2007-04-26 01:12:11 +04:00
|
|
|
PtrInfo* GetNext()
|
|
|
|
{
|
2012-11-15 11:32:39 +04:00
|
|
|
MOZ_ASSERT(!IsDone(), "calling GetNext when done");
|
2007-04-26 01:12:11 +04:00
|
|
|
if (mNext == mBlockEnd) {
|
|
|
|
Block *nextBlock = mCurBlock ? mCurBlock->mNext : mFirstBlock;
|
|
|
|
mNext = nextBlock->mEntries;
|
|
|
|
mBlockEnd = mNext + BlockSize;
|
|
|
|
mCurBlock = nextBlock;
|
|
|
|
}
|
|
|
|
return mNext++;
|
|
|
|
}
|
|
|
|
private:
|
|
|
|
Block *mFirstBlock, *mCurBlock;
|
|
|
|
// mNext is the next value we want to return, unless mNext == mBlockEnd
|
|
|
|
// NB: mLast is a reference to allow enumerating while building!
|
|
|
|
PtrInfo *mNext, *mBlockEnd, *&mLast;
|
|
|
|
};
|
2007-04-20 12:01:01 +04:00
|
|
|
|
2012-11-07 05:38:29 +04:00
|
|
|
size_t SizeOfExcludingThis(nsMallocSizeOfFun aMallocSizeOf) const {
|
|
|
|
// We don't measure the things pointed to by mEntries[] because those
|
|
|
|
// pointers are non-owning.
|
|
|
|
size_t n = 0;
|
|
|
|
Block *b = mBlocks;
|
|
|
|
while (b) {
|
|
|
|
n += aMallocSizeOf(b);
|
|
|
|
b = b->mNext;
|
|
|
|
}
|
|
|
|
return n;
|
2011-07-09 02:49:31 +04:00
|
|
|
}
|
|
|
|
|
2007-04-26 01:12:11 +04:00
|
|
|
private:
|
|
|
|
Block *mBlocks;
|
|
|
|
PtrInfo *mLast;
|
2007-01-05 01:31:26 +03:00
|
|
|
};
|
|
|
|
|
2007-06-19 05:29:10 +04:00
|
|
|
|
2011-11-24 16:35:56 +04:00
|
|
|
struct WeakMapping
|
|
|
|
{
|
|
|
|
// map and key will be null if the corresponding objects are GC marked
|
|
|
|
PtrInfo *mMap;
|
|
|
|
PtrInfo *mKey;
|
2012-10-18 05:22:46 +04:00
|
|
|
PtrInfo *mKeyDelegate;
|
2011-11-24 16:35:56 +04:00
|
|
|
PtrInfo *mVal;
|
|
|
|
};
|
|
|
|
|
2007-11-02 01:51:57 +03:00
|
|
|
class GCGraphBuilder;
|
2007-06-19 05:29:10 +04:00
|
|
|
|
2007-04-26 01:12:11 +04:00
|
|
|
struct GCGraph
|
|
|
|
{
|
|
|
|
NodePool mNodes;
|
|
|
|
EdgePool mEdges;
|
2011-11-24 16:35:56 +04:00
|
|
|
nsTArray<WeakMapping> mWeakMaps;
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t mRootCount;
|
2007-04-26 01:12:11 +04:00
|
|
|
|
2007-06-19 05:29:10 +04:00
|
|
|
GCGraph() : mRootCount(0) {
|
|
|
|
}
|
2012-11-07 05:38:29 +04:00
|
|
|
~GCGraph() {
|
2007-06-19 05:29:10 +04:00
|
|
|
}
|
2011-07-09 02:49:31 +04:00
|
|
|
|
2012-11-07 05:38:29 +04:00
|
|
|
void SizeOfExcludingThis(nsMallocSizeOfFun aMallocSizeOf,
|
|
|
|
size_t *aNodesSize, size_t *aEdgesSize) const {
|
|
|
|
*aNodesSize = mNodes.SizeOfExcludingThis(aMallocSizeOf);
|
|
|
|
*aEdgesSize = mEdges.SizeOfExcludingThis(aMallocSizeOf);
|
2011-07-09 02:49:31 +04:00
|
|
|
|
2012-11-07 05:38:29 +04:00
|
|
|
// These fields are deliberately not measured:
|
|
|
|
// - mWeakMaps entries, because the pointers are non-owning
|
|
|
|
}
|
2007-04-26 01:12:11 +04:00
|
|
|
};
|
2007-01-05 01:31:26 +03:00
|
|
|
|
2007-03-10 02:00:14 +03:00
|
|
|
// XXX Would be nice to have an nsHashSet<KeyType> API that has
|
|
|
|
// Add/Remove/Has rather than PutEntry/RemoveEntry/GetEntry.
|
2012-03-21 22:07:31 +04:00
|
|
|
typedef nsTHashtable<nsPtrHashKey<const void> > PointerSet;
|
2007-01-05 01:31:26 +03:00
|
|
|
|
2012-08-24 20:50:06 +04:00
|
|
|
static nsISupports *
|
|
|
|
CanonicalizeXPCOMParticipant(nsISupports *in)
|
|
|
|
{
|
|
|
|
nsISupports* out;
|
|
|
|
in->QueryInterface(NS_GET_IID(nsCycleCollectionISupports),
|
|
|
|
reinterpret_cast<void**>(&out));
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
2009-06-01 23:22:18 +04:00
|
|
|
static inline void
|
|
|
|
ToParticipant(nsISupports *s, nsXPCOMCycleCollectionParticipant **cp);
|
|
|
|
|
2012-08-24 20:50:06 +04:00
|
|
|
static void
|
|
|
|
CanonicalizeParticipant(void **parti, nsCycleCollectionParticipant **cp)
|
|
|
|
{
|
|
|
|
// If the participant is null, this is an nsISupports participant,
|
|
|
|
// so we must QI to get the real participant.
|
|
|
|
|
|
|
|
if (!*cp) {
|
|
|
|
nsISupports *nsparti = static_cast<nsISupports*>(*parti);
|
|
|
|
nsparti = CanonicalizeXPCOMParticipant(nsparti);
|
|
|
|
NS_ASSERTION(nsparti,
|
|
|
|
"Don't add objects that don't participate in collection!");
|
|
|
|
nsXPCOMCycleCollectionParticipant *xcp;
|
|
|
|
ToParticipant(nsparti, &xcp);
|
|
|
|
*parti = nsparti;
|
|
|
|
*cp = xcp;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-01-05 01:31:26 +03:00
|
|
|
struct nsPurpleBuffer
|
|
|
|
{
|
2009-05-07 00:46:04 +04:00
|
|
|
private:
|
|
|
|
struct Block {
|
|
|
|
Block *mNext;
|
2012-11-07 05:38:29 +04:00
|
|
|
// Try to match the size of a jemalloc bucket, to minimize slop bytes.
|
|
|
|
// - On 32-bit platforms sizeof(nsPurpleBufferEntry) is 12, so mEntries
|
|
|
|
// is 16,380 bytes, which leaves 4 bytes for mNext.
|
|
|
|
// - On 64-bit platforms sizeof(nsPurpleBufferEntry) is 24, so mEntries
|
|
|
|
// is 32,544 bytes, which leaves 8 bytes for mNext.
|
|
|
|
nsPurpleBufferEntry mEntries[1365];
|
|
|
|
|
|
|
|
Block() : mNext(nullptr) {
|
|
|
|
#ifndef DEBUG_CC
|
|
|
|
// Ensure Block is the right size (see above).
|
|
|
|
MOZ_STATIC_ASSERT(
|
|
|
|
sizeof(Block) == 16384 || // 32-bit
|
|
|
|
sizeof(Block) == 32768, // 64-bit
|
|
|
|
"ill-sized nsPurpleBuffer::Block"
|
|
|
|
);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
void StaticAsserts();
|
2009-05-07 00:46:04 +04:00
|
|
|
};
|
|
|
|
public:
|
|
|
|
// This class wraps a linked list of the elements in the purple
|
|
|
|
// buffer.
|
2007-01-05 01:31:26 +03:00
|
|
|
|
|
|
|
nsCycleCollectorParams &mParams;
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t mCount;
|
2009-05-07 00:46:04 +04:00
|
|
|
Block mFirstBlock;
|
|
|
|
nsPurpleBufferEntry *mFreeList;
|
|
|
|
|
2007-04-21 21:43:19 +04:00
|
|
|
#ifdef DEBUG_CC
|
2009-05-07 00:46:04 +04:00
|
|
|
PointerSet mNormalObjects; // duplicates our blocks
|
2007-01-05 01:31:26 +03:00
|
|
|
nsCycleCollectorStats &mStats;
|
2007-04-21 21:43:19 +04:00
|
|
|
#endif
|
2012-11-07 05:38:29 +04:00
|
|
|
|
2007-04-21 21:43:19 +04:00
|
|
|
#ifdef DEBUG_CC
|
2007-01-05 01:31:26 +03:00
|
|
|
nsPurpleBuffer(nsCycleCollectorParams ¶ms,
|
2012-11-07 05:38:29 +04:00
|
|
|
nsCycleCollectorStats &stats)
|
2007-01-05 01:31:26 +03:00
|
|
|
: mParams(params),
|
2009-05-07 00:46:04 +04:00
|
|
|
mStats(stats)
|
2007-01-05 01:31:26 +03:00
|
|
|
{
|
2009-05-07 00:46:04 +04:00
|
|
|
InitBlocks();
|
|
|
|
mNormalObjects.Init();
|
2007-01-05 01:31:26 +03:00
|
|
|
}
|
2007-04-21 21:43:19 +04:00
|
|
|
#else
|
2012-11-07 05:38:29 +04:00
|
|
|
nsPurpleBuffer(nsCycleCollectorParams ¶ms)
|
2009-05-07 00:46:04 +04:00
|
|
|
: mParams(params)
|
2007-04-21 21:43:19 +04:00
|
|
|
{
|
2009-05-07 00:46:04 +04:00
|
|
|
InitBlocks();
|
2007-04-21 21:43:19 +04:00
|
|
|
}
|
|
|
|
#endif
|
2007-01-05 01:31:26 +03:00
|
|
|
|
|
|
|
~nsPurpleBuffer()
|
|
|
|
{
|
2009-05-07 00:46:04 +04:00
|
|
|
FreeBlocks();
|
2007-01-05 01:31:26 +03:00
|
|
|
}
|
|
|
|
|
2009-05-07 00:46:04 +04:00
|
|
|
void InitBlocks()
|
2007-04-21 21:43:19 +04:00
|
|
|
{
|
2009-05-07 00:46:04 +04:00
|
|
|
mCount = 0;
|
2012-07-30 18:20:58 +04:00
|
|
|
mFreeList = nullptr;
|
2009-05-07 00:46:04 +04:00
|
|
|
StartBlock(&mFirstBlock);
|
2007-04-21 21:43:19 +04:00
|
|
|
}
|
|
|
|
|
2009-05-07 00:46:04 +04:00
|
|
|
void StartBlock(Block *aBlock)
|
2007-01-05 01:31:26 +03:00
|
|
|
{
|
2009-05-07 00:46:04 +04:00
|
|
|
NS_ABORT_IF_FALSE(!mFreeList, "should not have free list");
|
|
|
|
|
|
|
|
// Put all the entries in the block on the free list.
|
|
|
|
nsPurpleBufferEntry *entries = aBlock->mEntries;
|
|
|
|
mFreeList = entries;
|
2012-08-22 19:56:38 +04:00
|
|
|
for (uint32_t i = 1; i < ArrayLength(aBlock->mEntries); ++i) {
|
2009-05-07 00:46:04 +04:00
|
|
|
entries[i - 1].mNextInFreeList =
|
2012-04-12 04:17:44 +04:00
|
|
|
(nsPurpleBufferEntry*)(uintptr_t(entries + i) | 1);
|
2007-01-05 01:31:26 +03:00
|
|
|
}
|
2011-10-11 09:50:08 +04:00
|
|
|
entries[ArrayLength(aBlock->mEntries) - 1].mNextInFreeList =
|
2009-05-07 00:46:04 +04:00
|
|
|
(nsPurpleBufferEntry*)1;
|
2007-01-05 01:31:26 +03:00
|
|
|
}
|
|
|
|
|
2009-05-07 00:46:04 +04:00
|
|
|
void FreeBlocks()
|
2007-01-05 01:31:26 +03:00
|
|
|
{
|
2009-06-01 23:22:18 +04:00
|
|
|
if (mCount > 0)
|
|
|
|
UnmarkRemainingPurple(&mFirstBlock);
|
2012-11-07 05:38:29 +04:00
|
|
|
Block *b = mFirstBlock.mNext;
|
2009-05-07 00:46:04 +04:00
|
|
|
while (b) {
|
2009-06-01 23:22:18 +04:00
|
|
|
if (mCount > 0)
|
|
|
|
UnmarkRemainingPurple(b);
|
2009-05-07 00:46:04 +04:00
|
|
|
Block *next = b->mNext;
|
|
|
|
delete b;
|
|
|
|
b = next;
|
2007-01-05 01:31:26 +03:00
|
|
|
}
|
2012-07-30 18:20:58 +04:00
|
|
|
mFirstBlock.mNext = nullptr;
|
2007-01-05 01:31:26 +03:00
|
|
|
}
|
|
|
|
|
2009-06-01 23:22:18 +04:00
|
|
|
void UnmarkRemainingPurple(Block *b)
|
|
|
|
{
|
|
|
|
for (nsPurpleBufferEntry *e = b->mEntries,
|
2011-10-11 09:50:08 +04:00
|
|
|
*eEnd = ArrayEnd(b->mEntries);
|
2009-06-01 23:22:18 +04:00
|
|
|
e != eEnd; ++e) {
|
2012-04-12 04:17:44 +04:00
|
|
|
if (!(uintptr_t(e->mObject) & uintptr_t(1))) {
|
2009-06-01 23:22:18 +04:00
|
|
|
// This is a real entry (rather than something on the
|
|
|
|
// free list).
|
2009-10-28 20:28:57 +03:00
|
|
|
if (e->mObject) {
|
2012-08-24 20:50:06 +04:00
|
|
|
void *obj = e->mObject;
|
|
|
|
nsCycleCollectionParticipant *cp = e->mParticipant;
|
|
|
|
CanonicalizeParticipant(&obj, &cp);
|
|
|
|
cp->UnmarkIfPurple(obj);
|
2009-10-28 20:28:57 +03:00
|
|
|
}
|
2009-06-01 23:22:18 +04:00
|
|
|
|
|
|
|
if (--mCount == 0)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-05-07 00:46:04 +04:00
|
|
|
void SelectPointers(GCGraphBuilder &builder);
|
2007-01-05 01:31:26 +03:00
|
|
|
|
2012-01-14 20:58:05 +04:00
|
|
|
// RemoveSkippable removes entries from the purple buffer if
|
|
|
|
// nsPurpleBufferEntry::mObject is null or if the object's
|
2012-09-08 14:46:48 +04:00
|
|
|
// nsXPCOMCycleCollectionParticipant::CanSkip() returns true or
|
|
|
|
// if nsPurpleBufferEntry::mNotPurple is true.
|
2012-03-06 01:48:04 +04:00
|
|
|
// If removeChildlessNodes is true, then any nodes in the purple buffer
|
|
|
|
// that will have no children in the cycle collector graph will also be
|
|
|
|
// removed. CanSkip() may be run on these children.
|
|
|
|
void RemoveSkippable(bool removeChildlessNodes);
|
2012-01-14 20:58:05 +04:00
|
|
|
|
2009-05-07 00:46:04 +04:00
|
|
|
#ifdef DEBUG_CC
|
2011-09-29 10:19:26 +04:00
|
|
|
bool Exists(void *p) const
|
2007-01-05 01:31:26 +03:00
|
|
|
{
|
2012-08-27 21:28:38 +04:00
|
|
|
return mNormalObjects.GetEntry(p);
|
2007-01-05 01:31:26 +03:00
|
|
|
}
|
2009-05-07 00:46:04 +04:00
|
|
|
#endif
|
2007-01-05 01:31:26 +03:00
|
|
|
|
2009-05-07 00:46:04 +04:00
|
|
|
nsPurpleBufferEntry* NewEntry()
|
2007-01-05 01:31:26 +03:00
|
|
|
{
|
2009-05-07 00:46:04 +04:00
|
|
|
if (!mFreeList) {
|
|
|
|
Block *b = new Block;
|
|
|
|
if (!b) {
|
2012-07-30 18:20:58 +04:00
|
|
|
return nullptr;
|
2007-01-05 01:31:26 +03:00
|
|
|
}
|
2009-05-07 00:46:04 +04:00
|
|
|
StartBlock(b);
|
|
|
|
|
|
|
|
// Add the new block as the second block in the list.
|
|
|
|
b->mNext = mFirstBlock.mNext;
|
|
|
|
mFirstBlock.mNext = b;
|
2007-01-05 01:31:26 +03:00
|
|
|
}
|
2009-05-07 00:46:04 +04:00
|
|
|
|
|
|
|
nsPurpleBufferEntry *e = mFreeList;
|
|
|
|
mFreeList = (nsPurpleBufferEntry*)
|
2012-04-12 04:17:44 +04:00
|
|
|
(uintptr_t(mFreeList->mNextInFreeList) & ~uintptr_t(1));
|
2009-05-07 00:46:04 +04:00
|
|
|
return e;
|
2007-01-05 01:31:26 +03:00
|
|
|
}
|
2008-02-25 20:47:25 +03:00
|
|
|
|
2012-08-24 20:50:06 +04:00
|
|
|
nsPurpleBufferEntry* Put(void *p, nsCycleCollectionParticipant *cp)
|
2008-02-25 20:47:25 +03:00
|
|
|
{
|
2009-05-07 00:46:04 +04:00
|
|
|
nsPurpleBufferEntry *e = NewEntry();
|
|
|
|
if (!e) {
|
2012-07-30 18:20:58 +04:00
|
|
|
return nullptr;
|
2008-02-25 20:47:25 +03:00
|
|
|
}
|
2007-01-05 01:31:26 +03:00
|
|
|
|
2009-05-07 00:46:04 +04:00
|
|
|
++mCount;
|
|
|
|
|
|
|
|
e->mObject = p;
|
2012-08-24 20:50:06 +04:00
|
|
|
e->mParticipant = cp;
|
2012-09-08 14:46:48 +04:00
|
|
|
e->mNotPurple = false;
|
2007-01-05 01:31:26 +03:00
|
|
|
|
2009-05-07 00:46:04 +04:00
|
|
|
// Caller is responsible for filling in result's mRefCnt.
|
|
|
|
return e;
|
2007-01-05 01:31:26 +03:00
|
|
|
}
|
2009-05-07 00:46:04 +04:00
|
|
|
|
|
|
|
void Remove(nsPurpleBufferEntry *e)
|
|
|
|
{
|
2012-11-15 11:32:39 +04:00
|
|
|
MOZ_ASSERT(mCount != 0, "must have entries");
|
2009-05-07 00:46:04 +04:00
|
|
|
|
2007-04-21 21:43:19 +04:00
|
|
|
#ifdef DEBUG_CC
|
2009-05-07 00:46:04 +04:00
|
|
|
mNormalObjects.RemoveEntry(e->mObject);
|
2007-04-21 21:43:19 +04:00
|
|
|
#endif
|
2007-01-05 01:31:26 +03:00
|
|
|
|
2009-05-07 00:46:04 +04:00
|
|
|
e->mNextInFreeList =
|
2012-04-12 04:17:44 +04:00
|
|
|
(nsPurpleBufferEntry*)(uintptr_t(mFreeList) | uintptr_t(1));
|
2009-05-07 00:46:04 +04:00
|
|
|
mFreeList = e;
|
|
|
|
|
|
|
|
--mCount;
|
|
|
|
}
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t Count() const
|
2009-05-07 00:46:04 +04:00
|
|
|
{
|
|
|
|
return mCount;
|
|
|
|
}
|
2011-07-09 02:49:31 +04:00
|
|
|
|
2012-11-07 05:38:29 +04:00
|
|
|
size_t SizeOfExcludingThis(nsMallocSizeOfFun aMallocSizeOf) const
|
2011-07-09 02:49:31 +04:00
|
|
|
{
|
2012-11-07 05:38:29 +04:00
|
|
|
size_t n = 0;
|
|
|
|
|
|
|
|
// Don't measure mFirstBlock because it's within |this|.
|
|
|
|
const Block *block = mFirstBlock.mNext;
|
|
|
|
while (block) {
|
|
|
|
n += aMallocSizeOf(block);
|
|
|
|
block = block->mNext;
|
|
|
|
}
|
2011-07-09 02:49:31 +04:00
|
|
|
|
2012-11-07 05:38:29 +04:00
|
|
|
// These fields are deliberately not measured:
|
|
|
|
// - mParams: because it only contains scalars.
|
|
|
|
// - mFreeList: because it points into the purple buffer, which is
|
|
|
|
// within mFirstBlock and thus within |this|.
|
|
|
|
// - mNormalObjects, mStats: because they're DEBUG_CC-only.
|
|
|
|
//
|
|
|
|
// We also don't measure the things pointed to by mEntries[] because
|
|
|
|
// those pointers are non-owning.
|
|
|
|
|
|
|
|
return n;
|
|
|
|
}
|
2009-05-07 00:46:04 +04:00
|
|
|
};
|
2007-01-05 01:31:26 +03:00
|
|
|
|
2012-08-24 20:50:06 +04:00
|
|
|
static bool
|
|
|
|
AddPurpleRoot(GCGraphBuilder &builder, void *root, nsCycleCollectionParticipant *cp);
|
|
|
|
|
2007-01-05 01:31:26 +03:00
|
|
|
void
|
2009-05-07 00:46:04 +04:00
|
|
|
nsPurpleBuffer::SelectPointers(GCGraphBuilder &aBuilder)
|
2007-01-05 01:31:26 +03:00
|
|
|
{
|
2009-05-07 00:46:04 +04:00
|
|
|
#ifdef DEBUG_CC
|
2012-01-14 20:58:05 +04:00
|
|
|
// Can't use mCount here, since it may include null entries.
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t realCount = 0;
|
2012-01-14 20:58:05 +04:00
|
|
|
for (Block *b = &mFirstBlock; b; b = b->mNext) {
|
|
|
|
for (nsPurpleBufferEntry *e = b->mEntries,
|
|
|
|
*eEnd = ArrayEnd(b->mEntries);
|
|
|
|
e != eEnd; ++e) {
|
2012-04-12 04:17:44 +04:00
|
|
|
if (!(uintptr_t(e->mObject) & uintptr_t(1))) {
|
2012-09-08 14:46:48 +04:00
|
|
|
if (e->mObject && !e->mNotPurple) {
|
2012-01-14 20:58:05 +04:00
|
|
|
++realCount;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-27 21:28:38 +04:00
|
|
|
NS_ABORT_IF_FALSE(mNormalObjects.Count() == realCount,
|
2009-05-07 00:46:04 +04:00
|
|
|
"count out of sync");
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// Walk through all the blocks.
|
|
|
|
for (Block *b = &mFirstBlock; b; b = b->mNext) {
|
|
|
|
for (nsPurpleBufferEntry *e = b->mEntries,
|
2011-10-11 09:50:08 +04:00
|
|
|
*eEnd = ArrayEnd(b->mEntries);
|
2009-05-07 00:46:04 +04:00
|
|
|
e != eEnd; ++e) {
|
2012-04-12 04:17:44 +04:00
|
|
|
if (!(uintptr_t(e->mObject) & uintptr_t(1))) {
|
2009-05-07 00:46:04 +04:00
|
|
|
// This is a real entry (rather than something on the
|
|
|
|
// free list).
|
2012-09-08 14:46:48 +04:00
|
|
|
if (e->mObject && e->mNotPurple) {
|
|
|
|
void* o = e->mObject;
|
|
|
|
nsCycleCollectionParticipant* cp = e->mParticipant;
|
|
|
|
CanonicalizeParticipant(&o, &cp);
|
|
|
|
cp->UnmarkIfPurple(o);
|
|
|
|
Remove(e);
|
|
|
|
} else if (!e->mObject || AddPurpleRoot(aBuilder, e->mObject,
|
|
|
|
e->mParticipant)) {
|
2012-01-14 20:58:05 +04:00
|
|
|
Remove(e);
|
2009-05-07 00:46:04 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_WARN_IF_FALSE(mCount == 0, "AddPurpleRoot failed");
|
|
|
|
if (mCount == 0) {
|
|
|
|
FreeBlocks();
|
|
|
|
InitBlocks();
|
|
|
|
}
|
2007-01-05 01:31:26 +03:00
|
|
|
}
|
|
|
|
|
2007-03-27 13:49:06 +04:00
|
|
|
////////////////////////////////////////////////////////////////////////
|
2012-05-03 23:28:11 +04:00
|
|
|
// Top level structure for the cycle collector.
|
2007-03-27 13:49:06 +04:00
|
|
|
////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2007-01-05 01:31:26 +03:00
|
|
|
struct nsCycleCollector
|
|
|
|
{
|
2011-09-29 10:19:26 +04:00
|
|
|
bool mCollectionInProgress;
|
|
|
|
bool mScanInProgress;
|
|
|
|
bool mFollowupCollection;
|
2012-02-24 08:16:37 +04:00
|
|
|
nsCycleCollectorResults *mResults;
|
2011-05-12 22:22:25 +04:00
|
|
|
TimeStamp mCollectionStart;
|
2007-01-05 01:31:26 +03:00
|
|
|
|
2012-05-03 23:28:10 +04:00
|
|
|
nsCycleCollectionJSRuntime *mJSRuntime;
|
2007-01-05 01:31:26 +03:00
|
|
|
|
2008-01-10 17:10:03 +03:00
|
|
|
GCGraph mGraph;
|
|
|
|
|
2007-01-05 01:31:26 +03:00
|
|
|
nsCycleCollectorParams mParams;
|
|
|
|
|
2011-08-10 09:36:00 +04:00
|
|
|
nsTArray<PtrInfo*> *mWhiteNodes;
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t mWhiteNodeCount;
|
2008-02-15 16:12:55 +03:00
|
|
|
|
2011-06-11 00:33:06 +04:00
|
|
|
// mVisitedRefCounted and mVisitedGCed are only used for telemetry
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t mVisitedRefCounted;
|
|
|
|
uint32_t mVisitedGCed;
|
2011-06-11 00:33:06 +04:00
|
|
|
|
2012-01-14 20:58:05 +04:00
|
|
|
CC_BeforeUnlinkCallback mBeforeUnlinkCB;
|
|
|
|
CC_ForgetSkippableCallback mForgetSkippableCB;
|
|
|
|
|
2012-04-25 19:10:09 +04:00
|
|
|
nsPurpleBuffer mPurpleBuf;
|
|
|
|
|
2012-05-03 23:28:11 +04:00
|
|
|
void RegisterJSRuntime(nsCycleCollectionJSRuntime *aJSRuntime);
|
|
|
|
void ForgetJSRuntime();
|
2007-01-05 01:31:26 +03:00
|
|
|
|
2008-02-15 16:12:55 +03:00
|
|
|
void SelectPurple(GCGraphBuilder &builder);
|
2008-01-10 17:10:03 +03:00
|
|
|
void MarkRoots(GCGraphBuilder &builder);
|
|
|
|
void ScanRoots();
|
2011-11-24 16:35:57 +04:00
|
|
|
void ScanWeakMaps();
|
2011-06-22 21:41:17 +04:00
|
|
|
|
2012-03-06 01:48:04 +04:00
|
|
|
void ForgetSkippable(bool removeChildlessNodes);
|
2012-01-14 20:58:05 +04:00
|
|
|
|
2011-06-22 21:41:17 +04:00
|
|
|
// returns whether anything was collected
|
2011-09-29 10:19:26 +04:00
|
|
|
bool CollectWhite(nsICycleCollectorListener *aListener);
|
2007-01-05 01:31:26 +03:00
|
|
|
|
|
|
|
nsCycleCollector();
|
|
|
|
~nsCycleCollector();
|
|
|
|
|
2012-08-24 20:50:06 +04:00
|
|
|
nsPurpleBufferEntry* Suspect2(void *n, nsCycleCollectionParticipant *cp);
|
2011-09-29 10:19:26 +04:00
|
|
|
bool Forget2(nsPurpleBufferEntry *e);
|
2009-05-07 00:46:04 +04:00
|
|
|
|
2012-06-27 19:09:56 +04:00
|
|
|
void Collect(bool aMergeCompartments,
|
|
|
|
nsCycleCollectorResults *aResults,
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t aTryCollections,
|
2012-02-24 08:16:37 +04:00
|
|
|
nsICycleCollectorListener *aListener);
|
2010-11-12 01:52:30 +03:00
|
|
|
|
|
|
|
// Prepare for and cleanup after one or more collection(s).
|
2012-02-24 08:16:37 +04:00
|
|
|
bool PrepareForCollection(nsCycleCollectorResults *aResults,
|
|
|
|
nsTArray<PtrInfo*> *aWhiteNodes);
|
2011-09-29 10:19:26 +04:00
|
|
|
void GCIfNeeded(bool aForceGC);
|
2010-11-12 01:52:30 +03:00
|
|
|
void CleanupAfterCollection();
|
|
|
|
|
|
|
|
// Start and finish an individual collection.
|
2012-06-27 19:09:56 +04:00
|
|
|
bool BeginCollection(bool aMergeCompartments, nsICycleCollectorListener *aListener);
|
2011-09-29 10:19:26 +04:00
|
|
|
bool FinishCollection(nsICycleCollectorListener *aListener);
|
2010-11-12 01:52:30 +03:00
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t SuspectedCount();
|
2007-01-25 03:24:08 +03:00
|
|
|
void Shutdown();
|
2007-03-10 02:00:14 +03:00
|
|
|
|
2008-01-10 17:10:03 +03:00
|
|
|
void ClearGraph()
|
|
|
|
{
|
2010-08-18 11:46:54 +04:00
|
|
|
mGraph.mNodes.Clear();
|
2008-01-10 17:10:03 +03:00
|
|
|
mGraph.mEdges.Clear();
|
2011-11-24 16:35:56 +04:00
|
|
|
mGraph.mWeakMaps.Clear();
|
2008-01-10 17:10:03 +03:00
|
|
|
mGraph.mRootCount = 0;
|
|
|
|
}
|
|
|
|
|
2012-11-07 05:38:29 +04:00
|
|
|
void SizeOfIncludingThis(nsMallocSizeOfFun aMallocSizeOf,
|
|
|
|
size_t *aObjectSize,
|
|
|
|
size_t *aGraphNodesSize,
|
|
|
|
size_t *aGraphEdgesSize,
|
|
|
|
size_t *aWhiteNodeSize,
|
|
|
|
size_t *aPurpleBufferSize) const;
|
|
|
|
|
2007-04-21 21:43:19 +04:00
|
|
|
#ifdef DEBUG_CC
|
2010-11-12 01:52:30 +03:00
|
|
|
nsCycleCollectorStats mStats;
|
2007-04-21 21:43:19 +04:00
|
|
|
FILE *mPtrLog;
|
2012-04-25 19:10:09 +04:00
|
|
|
PointerSet mExpectedGarbage;
|
2007-04-21 21:43:19 +04:00
|
|
|
|
2012-09-08 14:46:48 +04:00
|
|
|
bool LogPurpleAddition(void* aObject, nsCycleCollectionParticipant *cp);
|
2012-01-14 20:58:05 +04:00
|
|
|
void LogPurpleRemoval(void* aObject);
|
|
|
|
|
2007-03-10 02:00:14 +03:00
|
|
|
void ShouldBeFreed(nsISupports *n);
|
|
|
|
void WasFreed(nsISupports *n);
|
|
|
|
#endif
|
2007-01-05 01:31:26 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2010-01-12 19:51:39 +03:00
|
|
|
/**
|
|
|
|
* GraphWalker is templatized over a Visitor class that must provide
|
|
|
|
* the following two methods:
|
|
|
|
*
|
2011-09-29 10:19:26 +04:00
|
|
|
* bool ShouldVisitNode(PtrInfo const *pi);
|
2010-01-12 19:51:39 +03:00
|
|
|
* void VisitNode(PtrInfo *pi);
|
|
|
|
*/
|
|
|
|
template <class Visitor>
|
2007-04-26 01:12:11 +04:00
|
|
|
class GraphWalker
|
2007-01-05 01:31:26 +03:00
|
|
|
{
|
2007-04-20 12:01:01 +04:00
|
|
|
private:
|
2010-01-12 19:51:39 +03:00
|
|
|
Visitor mVisitor;
|
2009-10-27 15:38:18 +03:00
|
|
|
|
2007-04-26 01:12:11 +04:00
|
|
|
void DoWalk(nsDeque &aQueue);
|
2007-01-05 01:31:26 +03:00
|
|
|
|
|
|
|
public:
|
2007-04-26 01:12:11 +04:00
|
|
|
void Walk(PtrInfo *s0);
|
|
|
|
void WalkFromRoots(GCGraph &aGraph);
|
2010-01-12 19:51:39 +03:00
|
|
|
// copy-constructing the visitor should be cheap, and less
|
|
|
|
// indirection than using a reference
|
|
|
|
GraphWalker(const Visitor aVisitor) : mVisitor(aVisitor) {}
|
2007-01-05 01:31:26 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////
|
|
|
|
// The static collector object
|
|
|
|
////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
|
2012-07-30 18:20:58 +04:00
|
|
|
static nsCycleCollector *sCollector = nullptr;
|
2012-11-07 05:38:29 +04:00
|
|
|
static nsIMemoryMultiReporter *sCollectorReporter = nullptr;
|
2007-01-05 01:31:26 +03:00
|
|
|
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////
|
|
|
|
// Utility functions
|
|
|
|
////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2012-03-16 08:13:08 +04:00
|
|
|
MOZ_NEVER_INLINE static void
|
2012-07-30 18:20:58 +04:00
|
|
|
Fault(const char *msg, const void *ptr=nullptr)
|
2007-01-05 01:31:26 +03:00
|
|
|
{
|
2012-03-16 08:13:08 +04:00
|
|
|
if (ptr)
|
|
|
|
printf("Fault in cycle collector: %s (ptr: %p)\n", msg, ptr);
|
|
|
|
else
|
|
|
|
printf("Fault in cycle collector: %s\n", msg);
|
2007-03-06 00:07:51 +03:00
|
|
|
|
2012-03-16 08:13:08 +04:00
|
|
|
NS_RUNTIMEABORT("cycle collector fault");
|
2007-01-05 01:31:26 +03:00
|
|
|
}
|
|
|
|
|
2007-07-06 02:38:38 +04:00
|
|
|
#ifdef DEBUG_CC
|
|
|
|
static void
|
|
|
|
Fault(const char *msg, PtrInfo *pi)
|
|
|
|
{
|
|
|
|
printf("Fault in cycle collector: %s\n"
|
|
|
|
" while operating on pointer %p %s\n",
|
|
|
|
msg, pi->mPointer, pi->mName);
|
|
|
|
if (pi->mInternalRefs) {
|
|
|
|
printf(" which has internal references from:\n");
|
2008-01-11 01:27:07 +03:00
|
|
|
NodePool::Enumerator queue(sCollector->mGraph.mNodes);
|
2007-07-06 02:38:38 +04:00
|
|
|
while (!queue.IsDone()) {
|
|
|
|
PtrInfo *ppi = queue.GetNext();
|
2011-06-10 01:55:04 +04:00
|
|
|
for (EdgePool::Iterator e = ppi->FirstChild(),
|
|
|
|
e_end = ppi->LastChild();
|
2007-07-06 02:38:38 +04:00
|
|
|
e != e_end; ++e) {
|
|
|
|
if (*e == pi) {
|
|
|
|
printf(" %p %s\n", ppi->mPointer, ppi->mName);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Fault(msg, pi->mPointer);
|
|
|
|
}
|
|
|
|
#else
|
2012-03-16 08:13:08 +04:00
|
|
|
static void
|
2007-07-06 02:38:38 +04:00
|
|
|
Fault(const char *msg, PtrInfo *pi)
|
|
|
|
{
|
|
|
|
Fault(msg, pi->mPointer);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2010-03-03 03:04:34 +03:00
|
|
|
static inline void
|
|
|
|
AbortIfOffMainThreadIfCheckFast()
|
2009-10-28 20:28:57 +03:00
|
|
|
{
|
2009-11-09 22:30:01 +03:00
|
|
|
#if defined(XP_WIN) || defined(NS_TLS)
|
2010-11-12 01:52:30 +03:00
|
|
|
if (!NS_IsMainThread() && !NS_IsCycleCollectorThread()) {
|
2010-03-03 03:04:34 +03:00
|
|
|
NS_RUNTIMEABORT("Main-thread-only object used off the main thread");
|
|
|
|
}
|
2009-10-28 20:28:57 +03:00
|
|
|
#endif
|
|
|
|
}
|
2007-04-26 01:12:11 +04:00
|
|
|
|
2007-06-07 02:04:26 +04:00
|
|
|
static inline void
|
|
|
|
ToParticipant(nsISupports *s, nsXPCOMCycleCollectionParticipant **cp)
|
|
|
|
{
|
|
|
|
// We use QI to move from an nsISupports to an
|
|
|
|
// nsXPCOMCycleCollectionParticipant, which is a per-class singleton helper
|
|
|
|
// object that implements traversal and unlinking logic for the nsISupports
|
|
|
|
// in question.
|
|
|
|
CallQueryInterface(s, cp);
|
|
|
|
#ifdef DEBUG_CC
|
|
|
|
if (cp)
|
|
|
|
++sCollector->mStats.mSuccessfulQI;
|
|
|
|
else
|
|
|
|
++sCollector->mStats.mFailedQI;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2010-01-12 19:51:39 +03:00
|
|
|
template <class Visitor>
|
2012-02-21 00:13:29 +04:00
|
|
|
MOZ_NEVER_INLINE void
|
2010-01-12 19:51:39 +03:00
|
|
|
GraphWalker<Visitor>::Walk(PtrInfo *s0)
|
2007-04-26 01:12:11 +04:00
|
|
|
{
|
|
|
|
nsDeque queue;
|
2012-02-21 00:13:29 +04:00
|
|
|
CC_AbortIfNull(s0);
|
2007-04-26 01:12:11 +04:00
|
|
|
queue.Push(s0);
|
|
|
|
DoWalk(queue);
|
|
|
|
}
|
|
|
|
|
2010-01-12 19:51:39 +03:00
|
|
|
template <class Visitor>
|
2012-02-21 00:13:29 +04:00
|
|
|
MOZ_NEVER_INLINE void
|
2010-01-12 19:51:39 +03:00
|
|
|
GraphWalker<Visitor>::WalkFromRoots(GCGraph& aGraph)
|
2007-04-26 01:12:11 +04:00
|
|
|
{
|
|
|
|
nsDeque queue;
|
|
|
|
NodePool::Enumerator etor(aGraph.mNodes);
|
2012-08-22 19:56:38 +04:00
|
|
|
for (uint32_t i = 0; i < aGraph.mRootCount; ++i) {
|
2012-02-21 00:13:29 +04:00
|
|
|
PtrInfo *pi = etor.GetNext();
|
|
|
|
CC_AbortIfNull(pi);
|
|
|
|
queue.Push(pi);
|
2007-04-26 01:12:11 +04:00
|
|
|
}
|
|
|
|
DoWalk(queue);
|
|
|
|
}
|
|
|
|
|
2010-01-12 19:51:39 +03:00
|
|
|
template <class Visitor>
|
2012-02-21 00:13:29 +04:00
|
|
|
MOZ_NEVER_INLINE void
|
2010-01-12 19:51:39 +03:00
|
|
|
GraphWalker<Visitor>::DoWalk(nsDeque &aQueue)
|
2007-04-26 01:12:11 +04:00
|
|
|
{
|
|
|
|
// Use a aQueue to match the breadth-first traversal used when we
|
|
|
|
// built the graph, for hopefully-better locality.
|
|
|
|
while (aQueue.GetSize() > 0) {
|
2007-07-08 11:08:04 +04:00
|
|
|
PtrInfo *pi = static_cast<PtrInfo*>(aQueue.PopFront());
|
2012-02-21 00:13:29 +04:00
|
|
|
CC_AbortIfNull(pi);
|
2007-04-26 01:12:11 +04:00
|
|
|
|
2010-01-12 19:51:39 +03:00
|
|
|
if (mVisitor.ShouldVisitNode(pi)) {
|
|
|
|
mVisitor.VisitNode(pi);
|
2011-06-10 01:55:04 +04:00
|
|
|
for (EdgePool::Iterator child = pi->FirstChild(),
|
|
|
|
child_end = pi->LastChild();
|
2010-07-30 13:57:19 +04:00
|
|
|
child != child_end; ++child) {
|
2012-02-21 00:13:29 +04:00
|
|
|
CC_AbortIfNull(*child);
|
2007-04-26 01:12:11 +04:00
|
|
|
aQueue.Push(*child);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
#ifdef DEBUG_CC
|
|
|
|
sCollector->mStats.mWalkedGraph++;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2012-02-17 21:35:22 +04:00
|
|
|
struct CCGraphDescriber
|
|
|
|
{
|
|
|
|
CCGraphDescriber()
|
|
|
|
: mAddress("0x"), mToAddress("0x"), mCnt(0), mType(eUnknown) {}
|
|
|
|
|
|
|
|
enum Type
|
|
|
|
{
|
|
|
|
eRefCountedObject,
|
|
|
|
eGCedObject,
|
|
|
|
eGCMarkedObject,
|
|
|
|
eEdge,
|
|
|
|
eRoot,
|
|
|
|
eGarbage,
|
|
|
|
eUnknown
|
|
|
|
};
|
|
|
|
|
|
|
|
nsCString mAddress;
|
|
|
|
nsCString mToAddress;
|
|
|
|
nsCString mName;
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t mCnt;
|
2012-02-17 21:35:22 +04:00
|
|
|
Type mType;
|
|
|
|
};
|
2007-04-26 01:12:11 +04:00
|
|
|
|
2012-06-06 03:51:58 +04:00
|
|
|
class nsCycleCollectorLogger MOZ_FINAL : public nsICycleCollectorListener
|
2010-08-12 04:03:23 +04:00
|
|
|
{
|
|
|
|
public:
|
2012-02-17 21:35:22 +04:00
|
|
|
nsCycleCollectorLogger() :
|
2012-07-30 18:20:58 +04:00
|
|
|
mStream(nullptr), mWantAllTraces(false),
|
2012-02-17 21:35:22 +04:00
|
|
|
mDisableLog(false), mWantAfterProcessing(false),
|
|
|
|
mNextIndex(0)
|
2010-08-12 04:03:23 +04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
~nsCycleCollectorLogger()
|
|
|
|
{
|
|
|
|
if (mStream) {
|
|
|
|
fclose(mStream);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
|
2012-01-02 01:48:42 +04:00
|
|
|
NS_IMETHOD AllTraces(nsICycleCollectorListener** aListener)
|
|
|
|
{
|
2012-02-17 21:35:22 +04:00
|
|
|
mWantAllTraces = true;
|
|
|
|
NS_ADDREF(*aListener = this);
|
|
|
|
return NS_OK;
|
2012-01-02 01:48:42 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHOD GetWantAllTraces(bool* aAllTraces)
|
|
|
|
{
|
2012-02-17 21:35:22 +04:00
|
|
|
*aAllTraces = mWantAllTraces;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHOD GetDisableLog(bool* aDisableLog)
|
|
|
|
{
|
|
|
|
*aDisableLog = mDisableLog;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHOD SetDisableLog(bool aDisableLog)
|
|
|
|
{
|
|
|
|
mDisableLog = aDisableLog;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHOD GetWantAfterProcessing(bool* aWantAfterProcessing)
|
|
|
|
{
|
|
|
|
*aWantAfterProcessing = mWantAfterProcessing;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHOD SetWantAfterProcessing(bool aWantAfterProcessing)
|
|
|
|
{
|
|
|
|
mWantAfterProcessing = aWantAfterProcessing;
|
|
|
|
return NS_OK;
|
2012-01-02 01:48:42 +04:00
|
|
|
}
|
|
|
|
|
2012-10-16 06:12:14 +04:00
|
|
|
NS_IMETHOD GetFilenameIdentifier(nsAString& aIdentifier)
|
|
|
|
{
|
|
|
|
aIdentifier = mFilenameIdentifier;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHOD SetFilenameIdentifier(const nsAString& aIdentifier)
|
|
|
|
{
|
|
|
|
mFilenameIdentifier = aIdentifier;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2010-08-12 04:03:23 +04:00
|
|
|
NS_IMETHOD Begin()
|
|
|
|
{
|
2012-02-17 21:35:22 +04:00
|
|
|
mCurrentAddress.AssignLiteral("0x");
|
|
|
|
mDescribers.Clear();
|
|
|
|
mNextIndex = 0;
|
|
|
|
if (mDisableLog) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2012-02-03 04:32:30 +04:00
|
|
|
|
2012-10-16 06:12:14 +04:00
|
|
|
// Initially create the log in a file starting with
|
|
|
|
// "incomplete-gc-edges". We'll move the file and strip off the
|
|
|
|
// "incomplete-" once the dump completes. (We do this because we don't
|
|
|
|
// want scripts which poll the filesystem looking for gc/cc dumps to
|
|
|
|
// grab a file before we're finished writing to it.)
|
|
|
|
nsCOMPtr<nsIFile> gcLogFile = CreateTempFile("incomplete-gc-edges");
|
|
|
|
NS_ENSURE_STATE(gcLogFile);
|
2012-02-03 04:32:30 +04:00
|
|
|
|
|
|
|
// Dump the JS heap.
|
2012-10-16 06:12:14 +04:00
|
|
|
FILE* gcLogANSIFile = nullptr;
|
|
|
|
gcLogFile->OpenANSIFileDesc("w", &gcLogANSIFile);
|
|
|
|
NS_ENSURE_STATE(gcLogANSIFile);
|
|
|
|
xpc::DumpJSHeap(gcLogANSIFile);
|
|
|
|
fclose(gcLogANSIFile);
|
|
|
|
|
|
|
|
// Strip off "incomplete-".
|
|
|
|
nsCOMPtr<nsIFile> gcLogFileFinalDestination =
|
|
|
|
CreateTempFile("gc-edges");
|
|
|
|
NS_ENSURE_STATE(gcLogFileFinalDestination);
|
|
|
|
|
|
|
|
nsAutoString gcLogFileFinalDestinationName;
|
|
|
|
gcLogFileFinalDestination->GetLeafName(gcLogFileFinalDestinationName);
|
|
|
|
NS_ENSURE_STATE(!gcLogFileFinalDestinationName.IsEmpty());
|
2010-08-12 04:03:23 +04:00
|
|
|
|
2012-10-16 06:12:14 +04:00
|
|
|
gcLogFile->MoveTo(/* directory */ nullptr, gcLogFileFinalDestinationName);
|
|
|
|
|
|
|
|
// Log to the error console.
|
2012-01-02 02:34:00 +04:00
|
|
|
nsCOMPtr<nsIConsoleService> cs =
|
|
|
|
do_GetService(NS_CONSOLESERVICE_CONTRACTID);
|
|
|
|
if (cs) {
|
2012-10-16 06:12:14 +04:00
|
|
|
nsAutoString gcLogPath;
|
|
|
|
gcLogFileFinalDestination->GetPath(gcLogPath);
|
2012-09-14 17:47:12 +04:00
|
|
|
|
2012-10-16 06:12:14 +04:00
|
|
|
nsString msg = NS_LITERAL_STRING("Garbage Collector log dumped to ") +
|
|
|
|
gcLogPath;
|
2012-09-14 17:47:12 +04:00
|
|
|
cs->LogStringMessage(msg.get());
|
2012-01-02 02:34:00 +04:00
|
|
|
}
|
|
|
|
|
2012-10-16 06:12:14 +04:00
|
|
|
// Open a file for dumping the CC graph. We again prefix with
|
|
|
|
// "incomplete-".
|
|
|
|
mOutFile = CreateTempFile("incomplete-cc-edges");
|
|
|
|
NS_ENSURE_STATE(mOutFile);
|
|
|
|
MOZ_ASSERT(!mStream);
|
|
|
|
mOutFile->OpenANSIFileDesc("w", &mStream);
|
|
|
|
NS_ENSURE_STATE(mStream);
|
|
|
|
|
2012-02-03 04:32:30 +04:00
|
|
|
return NS_OK;
|
2010-08-12 04:03:23 +04:00
|
|
|
}
|
2012-08-22 19:56:38 +04:00
|
|
|
NS_IMETHOD NoteRefCountedObject(uint64_t aAddress, uint32_t refCount,
|
2011-06-22 21:41:17 +04:00
|
|
|
const char *aObjectDescription)
|
|
|
|
{
|
2012-02-17 21:35:22 +04:00
|
|
|
if (!mDisableLog) {
|
|
|
|
fprintf(mStream, "%p [rc=%u] %s\n", (void*)aAddress, refCount,
|
|
|
|
aObjectDescription);
|
|
|
|
}
|
|
|
|
if (mWantAfterProcessing) {
|
|
|
|
CCGraphDescriber* d = mDescribers.AppendElement();
|
|
|
|
NS_ENSURE_TRUE(d, NS_ERROR_OUT_OF_MEMORY);
|
|
|
|
mCurrentAddress.AssignLiteral("0x");
|
|
|
|
mCurrentAddress.AppendInt(aAddress, 16);
|
|
|
|
d->mType = CCGraphDescriber::eRefCountedObject;
|
|
|
|
d->mAddress = mCurrentAddress;
|
|
|
|
d->mCnt = refCount;
|
|
|
|
d->mName.Append(aObjectDescription);
|
|
|
|
}
|
2011-06-22 21:41:17 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2012-08-22 19:56:38 +04:00
|
|
|
NS_IMETHOD NoteGCedObject(uint64_t aAddress, bool aMarked,
|
2011-06-22 21:41:17 +04:00
|
|
|
const char *aObjectDescription)
|
2010-08-12 04:03:23 +04:00
|
|
|
{
|
2012-02-17 21:35:22 +04:00
|
|
|
if (!mDisableLog) {
|
|
|
|
fprintf(mStream, "%p [gc%s] %s\n", (void*)aAddress,
|
|
|
|
aMarked ? ".marked" : "", aObjectDescription);
|
|
|
|
}
|
|
|
|
if (mWantAfterProcessing) {
|
|
|
|
CCGraphDescriber* d = mDescribers.AppendElement();
|
|
|
|
NS_ENSURE_TRUE(d, NS_ERROR_OUT_OF_MEMORY);
|
|
|
|
mCurrentAddress.AssignLiteral("0x");
|
|
|
|
mCurrentAddress.AppendInt(aAddress, 16);
|
|
|
|
d->mType = aMarked ? CCGraphDescriber::eGCMarkedObject :
|
|
|
|
CCGraphDescriber::eGCedObject;
|
|
|
|
d->mAddress = mCurrentAddress;
|
|
|
|
d->mName.Append(aObjectDescription);
|
|
|
|
}
|
2010-08-12 04:03:23 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2012-08-22 19:56:38 +04:00
|
|
|
NS_IMETHOD NoteEdge(uint64_t aToAddress, const char *aEdgeName)
|
2010-08-12 04:03:23 +04:00
|
|
|
{
|
2012-02-17 21:35:22 +04:00
|
|
|
if (!mDisableLog) {
|
|
|
|
fprintf(mStream, "> %p %s\n", (void*)aToAddress, aEdgeName);
|
|
|
|
}
|
|
|
|
if (mWantAfterProcessing) {
|
|
|
|
CCGraphDescriber* d = mDescribers.AppendElement();
|
|
|
|
NS_ENSURE_TRUE(d, NS_ERROR_OUT_OF_MEMORY);
|
|
|
|
d->mType = CCGraphDescriber::eEdge;
|
|
|
|
d->mAddress = mCurrentAddress;
|
|
|
|
d->mToAddress.AppendInt(aToAddress, 16);
|
|
|
|
d->mName.Append(aEdgeName);
|
|
|
|
}
|
2010-08-12 04:03:23 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2011-06-22 21:41:17 +04:00
|
|
|
NS_IMETHOD BeginResults()
|
2010-08-12 04:03:23 +04:00
|
|
|
{
|
2012-02-17 21:35:22 +04:00
|
|
|
if (!mDisableLog) {
|
|
|
|
fputs("==========\n", mStream);
|
|
|
|
}
|
2010-08-12 04:03:23 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2012-08-22 19:56:38 +04:00
|
|
|
NS_IMETHOD DescribeRoot(uint64_t aAddress, uint32_t aKnownEdges)
|
2010-08-12 04:03:23 +04:00
|
|
|
{
|
2012-02-17 21:35:22 +04:00
|
|
|
if (!mDisableLog) {
|
|
|
|
fprintf(mStream, "%p [known=%u]\n", (void*)aAddress, aKnownEdges);
|
|
|
|
}
|
|
|
|
if (mWantAfterProcessing) {
|
|
|
|
CCGraphDescriber* d = mDescribers.AppendElement();
|
|
|
|
NS_ENSURE_TRUE(d, NS_ERROR_OUT_OF_MEMORY);
|
|
|
|
d->mType = CCGraphDescriber::eRoot;
|
|
|
|
d->mAddress.AppendInt(aAddress, 16);
|
|
|
|
d->mCnt = aKnownEdges;
|
|
|
|
}
|
2010-08-12 04:03:23 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2012-08-22 19:56:38 +04:00
|
|
|
NS_IMETHOD DescribeGarbage(uint64_t aAddress)
|
2010-08-12 04:03:23 +04:00
|
|
|
{
|
2012-02-17 21:35:22 +04:00
|
|
|
if (!mDisableLog) {
|
|
|
|
fprintf(mStream, "%p [garbage]\n", (void*)aAddress);
|
|
|
|
}
|
|
|
|
if (mWantAfterProcessing) {
|
|
|
|
CCGraphDescriber* d = mDescribers.AppendElement();
|
|
|
|
NS_ENSURE_TRUE(d, NS_ERROR_OUT_OF_MEMORY);
|
|
|
|
d->mType = CCGraphDescriber::eGarbage;
|
|
|
|
d->mAddress.AppendInt(aAddress, 16);
|
|
|
|
}
|
2010-08-12 04:03:23 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
NS_IMETHOD End()
|
|
|
|
{
|
2012-02-17 21:35:22 +04:00
|
|
|
if (!mDisableLog) {
|
2012-10-16 06:12:14 +04:00
|
|
|
MOZ_ASSERT(mStream);
|
|
|
|
MOZ_ASSERT(mOutFile);
|
|
|
|
|
2012-02-17 21:35:22 +04:00
|
|
|
fclose(mStream);
|
2012-07-30 18:20:58 +04:00
|
|
|
mStream = nullptr;
|
2012-10-16 06:12:14 +04:00
|
|
|
|
|
|
|
// Strip off "incomplete-" from the log file's name.
|
|
|
|
nsCOMPtr<nsIFile> logFileFinalDestination =
|
|
|
|
CreateTempFile("cc-edges");
|
|
|
|
NS_ENSURE_STATE(logFileFinalDestination);
|
|
|
|
|
|
|
|
nsAutoString logFileFinalDestinationName;
|
|
|
|
logFileFinalDestination->GetLeafName(logFileFinalDestinationName);
|
|
|
|
NS_ENSURE_STATE(!logFileFinalDestinationName.IsEmpty());
|
|
|
|
|
|
|
|
mOutFile->MoveTo(/* directory = */ nullptr,
|
|
|
|
logFileFinalDestinationName);
|
|
|
|
mOutFile = nullptr;
|
|
|
|
|
|
|
|
// Log to the error console.
|
|
|
|
nsCOMPtr<nsIConsoleService> cs =
|
|
|
|
do_GetService(NS_CONSOLESERVICE_CONTRACTID);
|
|
|
|
if (cs) {
|
|
|
|
nsAutoString ccLogPath;
|
|
|
|
logFileFinalDestination->GetPath(ccLogPath);
|
|
|
|
|
|
|
|
nsString msg = NS_LITERAL_STRING("Cycle Collector log dumped to ") +
|
|
|
|
ccLogPath;
|
|
|
|
cs->LogStringMessage(msg.get());
|
|
|
|
}
|
2012-02-17 21:35:22 +04:00
|
|
|
}
|
2010-08-12 04:03:23 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2012-02-17 21:35:22 +04:00
|
|
|
NS_IMETHOD ProcessNext(nsICycleCollectorHandler* aHandler,
|
|
|
|
bool* aCanContinue)
|
|
|
|
{
|
|
|
|
NS_ENSURE_STATE(aHandler && mWantAfterProcessing);
|
|
|
|
if (mNextIndex < mDescribers.Length()) {
|
|
|
|
CCGraphDescriber& d = mDescribers[mNextIndex++];
|
|
|
|
switch (d.mType) {
|
|
|
|
case CCGraphDescriber::eRefCountedObject:
|
|
|
|
aHandler->NoteRefCountedObject(d.mAddress,
|
|
|
|
d.mCnt,
|
|
|
|
d.mName);
|
|
|
|
break;
|
|
|
|
case CCGraphDescriber::eGCedObject:
|
|
|
|
case CCGraphDescriber::eGCMarkedObject:
|
|
|
|
aHandler->NoteGCedObject(d.mAddress,
|
|
|
|
d.mType ==
|
|
|
|
CCGraphDescriber::eGCMarkedObject,
|
|
|
|
d.mName);
|
|
|
|
break;
|
|
|
|
case CCGraphDescriber::eEdge:
|
|
|
|
aHandler->NoteEdge(d.mAddress,
|
|
|
|
d.mToAddress,
|
|
|
|
d.mName);
|
|
|
|
break;
|
|
|
|
case CCGraphDescriber::eRoot:
|
|
|
|
aHandler->DescribeRoot(d.mAddress,
|
|
|
|
d.mCnt);
|
|
|
|
break;
|
|
|
|
case CCGraphDescriber::eGarbage:
|
|
|
|
aHandler->DescribeGarbage(d.mAddress);
|
|
|
|
break;
|
|
|
|
case CCGraphDescriber::eUnknown:
|
|
|
|
NS_NOTREACHED("CCGraphDescriber::eUnknown");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!(*aCanContinue = mNextIndex < mDescribers.Length())) {
|
|
|
|
mCurrentAddress.AssignLiteral("0x");
|
|
|
|
mDescribers.Clear();
|
|
|
|
mNextIndex = 0;
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2010-08-12 04:03:23 +04:00
|
|
|
private:
|
2012-10-16 06:12:14 +04:00
|
|
|
/**
|
|
|
|
* Create a new file named something like aPrefix.$PID.$IDENTIFIER.log in
|
|
|
|
* $MOZ_CC_LOG_DIRECTORY or in the system's temp directory. No existing
|
|
|
|
* file will be overwritten; if aPrefix.$PID.$IDENTIFIER.log exists, we'll
|
|
|
|
* try a file named something like aPrefix.$PID.$IDENTIFIER-1.log, and so
|
|
|
|
* on.
|
|
|
|
*/
|
|
|
|
already_AddRefed<nsIFile>
|
|
|
|
CreateTempFile(const char* aPrefix)
|
|
|
|
{
|
|
|
|
nsPrintfCString filename("%s.%d%s%s.log",
|
|
|
|
aPrefix,
|
|
|
|
base::GetCurrentProcId(),
|
|
|
|
mFilenameIdentifier.IsEmpty() ? "" : ".",
|
|
|
|
NS_ConvertUTF16toUTF8(mFilenameIdentifier).get());
|
|
|
|
|
|
|
|
// Get the log directory either from $MOZ_CC_LOG_DIRECTORY or from our
|
|
|
|
// platform's temp directory.
|
|
|
|
nsCOMPtr<nsIFile> logFile;
|
|
|
|
if (char* env = PR_GetEnv("MOZ_CC_LOG_DIRECTORY")) {
|
|
|
|
NS_NewNativeLocalFile(nsCString(env), /* followLinks = */ true,
|
|
|
|
getter_AddRefs(logFile));
|
|
|
|
} else {
|
|
|
|
// Ask NSPR to point us to the temp directory.
|
|
|
|
NS_GetSpecialDirectory(NS_OS_TEMP_DIR, getter_AddRefs(logFile));
|
|
|
|
}
|
|
|
|
NS_ENSURE_TRUE(logFile, nullptr);
|
|
|
|
|
|
|
|
nsresult rv = logFile->AppendNative(filename);
|
|
|
|
NS_ENSURE_SUCCESS(rv, nullptr);
|
|
|
|
|
2012-10-31 18:20:26 +04:00
|
|
|
rv = logFile->CreateUnique(nsIFile::NORMAL_FILE_TYPE, 0644);
|
2012-10-16 06:12:14 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, nullptr);
|
2012-10-31 18:20:26 +04:00
|
|
|
#ifdef ANDROID
|
|
|
|
{
|
|
|
|
// On android the default system umask is 0077 which makes these files
|
|
|
|
// unreadable to the shell user. In order to pull the dumps off a non-rooted
|
|
|
|
// device we need to chmod them to something world-readable.
|
|
|
|
nsAutoCString path;
|
|
|
|
rv = logFile->GetNativePath(path);
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
chmod(PromiseFlatCString(path).get(), 0644);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
2012-10-16 06:12:14 +04:00
|
|
|
|
|
|
|
return logFile.forget();
|
|
|
|
}
|
|
|
|
|
2010-08-12 04:03:23 +04:00
|
|
|
FILE *mStream;
|
2012-10-16 06:12:14 +04:00
|
|
|
nsCOMPtr<nsIFile> mOutFile;
|
2012-01-02 01:48:42 +04:00
|
|
|
bool mWantAllTraces;
|
2012-02-17 21:35:22 +04:00
|
|
|
bool mDisableLog;
|
|
|
|
bool mWantAfterProcessing;
|
2012-10-16 06:12:14 +04:00
|
|
|
nsString mFilenameIdentifier;
|
2012-02-17 21:35:22 +04:00
|
|
|
nsCString mCurrentAddress;
|
|
|
|
nsTArray<CCGraphDescriber> mDescribers;
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t mNextIndex;
|
2010-08-12 04:03:23 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
NS_IMPL_ISUPPORTS1(nsCycleCollectorLogger, nsICycleCollectorListener)
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsCycleCollectorLoggerConstructor(nsISupports* aOuter,
|
|
|
|
const nsIID& aIID,
|
|
|
|
void* *aInstancePtr)
|
|
|
|
{
|
|
|
|
NS_ENSURE_TRUE(!aOuter, NS_ERROR_NO_AGGREGATION);
|
|
|
|
|
|
|
|
nsISupports *logger = new nsCycleCollectorLogger();
|
|
|
|
|
|
|
|
return logger->QueryInterface(aIID, aInstancePtr);
|
|
|
|
}
|
|
|
|
|
2007-04-26 01:12:11 +04:00
|
|
|
////////////////////////////////////////////////////////////////////////
|
|
|
|
// Bacon & Rajan's |MarkRoots| routine.
|
|
|
|
////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
struct PtrToNodeEntry : public PLDHashEntryHdr
|
|
|
|
{
|
|
|
|
// The key is mNode->mPointer
|
|
|
|
PtrInfo *mNode;
|
|
|
|
};
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
static bool
|
2007-04-26 01:12:11 +04:00
|
|
|
PtrToNodeMatchEntry(PLDHashTable *table,
|
|
|
|
const PLDHashEntryHdr *entry,
|
|
|
|
const void *key)
|
|
|
|
{
|
2007-07-08 11:08:04 +04:00
|
|
|
const PtrToNodeEntry *n = static_cast<const PtrToNodeEntry*>(entry);
|
2007-04-26 01:12:11 +04:00
|
|
|
return n->mNode->mPointer == key;
|
|
|
|
}
|
|
|
|
|
|
|
|
static PLDHashTableOps PtrNodeOps = {
|
|
|
|
PL_DHashAllocTable,
|
|
|
|
PL_DHashFreeTable,
|
|
|
|
PL_DHashVoidPtrKeyStub,
|
|
|
|
PtrToNodeMatchEntry,
|
|
|
|
PL_DHashMoveEntryStub,
|
|
|
|
PL_DHashClearEntryStub,
|
|
|
|
PL_DHashFinalizeStub,
|
2012-07-30 18:20:58 +04:00
|
|
|
nullptr
|
2007-04-26 01:12:11 +04:00
|
|
|
};
|
|
|
|
|
2007-11-02 01:51:57 +03:00
|
|
|
class GCGraphBuilder : public nsCycleCollectionTraversalCallback
|
2007-04-26 01:12:11 +04:00
|
|
|
{
|
|
|
|
private:
|
|
|
|
NodePool::Builder mNodeBuilder;
|
|
|
|
EdgePool::Builder mEdgeBuilder;
|
2011-11-24 16:35:56 +04:00
|
|
|
nsTArray<WeakMapping> &mWeakMaps;
|
2007-04-26 01:12:11 +04:00
|
|
|
PLDHashTable mPtrToNodeMap;
|
|
|
|
PtrInfo *mCurrPi;
|
2012-05-03 23:28:10 +04:00
|
|
|
nsCycleCollectionParticipant *mJSParticipant;
|
2012-06-27 19:09:56 +04:00
|
|
|
nsCycleCollectionParticipant *mJSCompParticipant;
|
2008-03-18 02:11:08 +03:00
|
|
|
nsCString mNextEdgeName;
|
2011-01-14 13:06:09 +03:00
|
|
|
nsICycleCollectorListener *mListener;
|
2012-06-27 19:09:56 +04:00
|
|
|
bool mMergeCompartments;
|
2007-04-26 01:12:11 +04:00
|
|
|
|
|
|
|
public:
|
|
|
|
GCGraphBuilder(GCGraph &aGraph,
|
2012-05-03 23:28:10 +04:00
|
|
|
nsCycleCollectionJSRuntime *aJSRuntime,
|
2012-06-27 19:09:56 +04:00
|
|
|
nsICycleCollectorListener *aListener,
|
|
|
|
bool aMergeCompartments);
|
2007-04-26 01:12:11 +04:00
|
|
|
~GCGraphBuilder();
|
2010-02-28 13:23:59 +03:00
|
|
|
bool Initialized();
|
2007-04-26 01:12:11 +04:00
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t Count() const { return mPtrToNodeMap.entryCount; }
|
2007-04-26 01:12:11 +04:00
|
|
|
|
2007-05-24 18:10:02 +04:00
|
|
|
PtrInfo* AddNode(void *s, nsCycleCollectionParticipant *aParticipant);
|
2011-11-24 16:35:56 +04:00
|
|
|
PtrInfo* AddWeakMapNode(void* node);
|
2007-04-26 01:12:11 +04:00
|
|
|
void Traverse(PtrInfo* aPtrInfo);
|
2011-06-10 01:55:29 +04:00
|
|
|
void SetLastChild();
|
2007-04-26 01:12:11 +04:00
|
|
|
|
2008-02-16 01:23:16 +03:00
|
|
|
private:
|
2012-08-27 21:41:04 +04:00
|
|
|
void DescribeNode(uint32_t refCount, const char *objName)
|
2011-06-24 01:10:52 +04:00
|
|
|
{
|
2011-06-22 21:41:17 +04:00
|
|
|
mCurrPi->mRefCount = refCount;
|
2011-06-24 01:10:52 +04:00
|
|
|
#ifdef DEBUG_CC
|
|
|
|
mCurrPi->mName = PL_strdup(objName);
|
|
|
|
sCollector->mStats.mVisitedNode++;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2012-05-03 23:28:11 +04:00
|
|
|
public:
|
|
|
|
// nsCycleCollectionTraversalCallback methods.
|
2012-08-27 21:41:04 +04:00
|
|
|
NS_IMETHOD_(void) DescribeRefCountedNode(nsrefcnt refCount,
|
2011-06-24 01:10:52 +04:00
|
|
|
const char *objName);
|
2012-08-27 21:41:04 +04:00
|
|
|
NS_IMETHOD_(void) DescribeGCedNode(bool isMarked, const char *objName);
|
2012-05-03 23:28:11 +04:00
|
|
|
|
|
|
|
NS_IMETHOD_(void) NoteXPCOMRoot(nsISupports *root);
|
|
|
|
NS_IMETHOD_(void) NoteJSRoot(void *root);
|
|
|
|
NS_IMETHOD_(void) NoteNativeRoot(void *root, nsCycleCollectionParticipant *participant);
|
2012-05-03 23:28:10 +04:00
|
|
|
|
2007-09-18 04:30:06 +04:00
|
|
|
NS_IMETHOD_(void) NoteXPCOMChild(nsISupports *child);
|
2012-05-03 23:28:10 +04:00
|
|
|
NS_IMETHOD_(void) NoteJSChild(void *child);
|
2007-09-18 04:30:06 +04:00
|
|
|
NS_IMETHOD_(void) NoteNativeChild(void *child,
|
2012-02-27 01:18:44 +04:00
|
|
|
nsCycleCollectionParticipant *participant);
|
2012-05-03 23:28:10 +04:00
|
|
|
|
2008-03-18 02:11:08 +03:00
|
|
|
NS_IMETHOD_(void) NoteNextEdgeName(const char* name);
|
2012-10-18 05:22:46 +04:00
|
|
|
NS_IMETHOD_(void) NoteWeakMapping(void *map, void *key, void *kdelegate, void *val);
|
2012-05-03 23:28:11 +04:00
|
|
|
|
2012-02-27 01:18:44 +04:00
|
|
|
private:
|
2012-05-03 23:28:11 +04:00
|
|
|
NS_IMETHOD_(void) NoteRoot(void *root,
|
2012-05-03 23:28:11 +04:00
|
|
|
nsCycleCollectionParticipant *participant)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(root);
|
|
|
|
MOZ_ASSERT(participant);
|
|
|
|
|
2012-10-26 17:32:10 +04:00
|
|
|
if (!participant->CanSkipInCC(root) || MOZ_UNLIKELY(WantAllTraces())) {
|
2012-05-03 23:28:11 +04:00
|
|
|
AddNode(root, participant);
|
2012-05-03 23:28:11 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-27 01:18:44 +04:00
|
|
|
NS_IMETHOD_(void) NoteChild(void *child, nsCycleCollectionParticipant *cp,
|
2012-05-03 23:28:11 +04:00
|
|
|
nsCString edgeName)
|
2012-02-27 01:18:44 +04:00
|
|
|
{
|
2012-05-03 23:28:11 +04:00
|
|
|
PtrInfo *childPi = AddNode(child, cp);
|
2012-02-27 01:18:44 +04:00
|
|
|
if (!childPi)
|
|
|
|
return;
|
|
|
|
mEdgeBuilder.Add(childPi);
|
|
|
|
if (mListener) {
|
2012-08-22 19:56:38 +04:00
|
|
|
mListener->NoteEdge((uint64_t)child, edgeName.get());
|
2012-02-27 01:18:44 +04:00
|
|
|
}
|
|
|
|
++childPi->mInternalRefs;
|
|
|
|
}
|
2012-06-27 19:09:56 +04:00
|
|
|
|
|
|
|
JSCompartment *MergeCompartment(void *gcthing) {
|
|
|
|
if (!mMergeCompartments) {
|
2012-07-30 18:20:58 +04:00
|
|
|
return nullptr;
|
2012-06-27 19:09:56 +04:00
|
|
|
}
|
|
|
|
JSCompartment *comp = js::GetGCThingCompartment(gcthing);
|
|
|
|
if (js::IsSystemCompartment(comp)) {
|
2012-07-30 18:20:58 +04:00
|
|
|
return nullptr;
|
2012-06-27 19:09:56 +04:00
|
|
|
}
|
|
|
|
return comp;
|
|
|
|
}
|
2007-04-26 01:12:11 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
GCGraphBuilder::GCGraphBuilder(GCGraph &aGraph,
|
2012-05-03 23:28:10 +04:00
|
|
|
nsCycleCollectionJSRuntime *aJSRuntime,
|
2012-06-27 19:09:56 +04:00
|
|
|
nsICycleCollectorListener *aListener,
|
|
|
|
bool aMergeCompartments)
|
2007-04-26 01:12:11 +04:00
|
|
|
: mNodeBuilder(aGraph.mNodes),
|
|
|
|
mEdgeBuilder(aGraph.mEdges),
|
2011-11-24 16:35:56 +04:00
|
|
|
mWeakMaps(aGraph.mWeakMaps),
|
2012-07-30 18:20:58 +04:00
|
|
|
mJSParticipant(nullptr),
|
2012-06-27 19:09:56 +04:00
|
|
|
mJSCompParticipant(xpc_JSCompartmentParticipant()),
|
|
|
|
mListener(aListener),
|
|
|
|
mMergeCompartments(aMergeCompartments)
|
2007-04-26 01:12:11 +04:00
|
|
|
{
|
2012-07-30 18:20:58 +04:00
|
|
|
if (!PL_DHashTableInit(&mPtrToNodeMap, &PtrNodeOps, nullptr,
|
2007-04-26 01:12:11 +04:00
|
|
|
sizeof(PtrToNodeEntry), 32768))
|
2012-07-30 18:20:58 +04:00
|
|
|
mPtrToNodeMap.ops = nullptr;
|
2012-01-02 01:48:42 +04:00
|
|
|
|
2012-05-03 23:28:10 +04:00
|
|
|
if (aJSRuntime) {
|
|
|
|
mJSParticipant = aJSRuntime->GetParticipant();
|
|
|
|
}
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t flags = 0;
|
2012-01-02 01:48:42 +04:00
|
|
|
#ifdef DEBUG_CC
|
|
|
|
flags = nsCycleCollectionTraversalCallback::WANT_DEBUG_INFO |
|
|
|
|
nsCycleCollectionTraversalCallback::WANT_ALL_TRACES;
|
2009-07-09 05:10:29 +04:00
|
|
|
#endif
|
2012-01-02 01:48:42 +04:00
|
|
|
if (!flags && mListener) {
|
|
|
|
flags = nsCycleCollectionTraversalCallback::WANT_DEBUG_INFO;
|
|
|
|
bool all = false;
|
|
|
|
mListener->GetWantAllTraces(&all);
|
|
|
|
if (all) {
|
|
|
|
flags |= nsCycleCollectionTraversalCallback::WANT_ALL_TRACES;
|
|
|
|
}
|
2010-08-12 04:03:23 +04:00
|
|
|
}
|
2012-01-02 01:48:42 +04:00
|
|
|
|
|
|
|
mFlags |= flags;
|
2012-06-27 19:09:56 +04:00
|
|
|
|
2012-10-26 17:32:10 +04:00
|
|
|
mMergeCompartments = mMergeCompartments && MOZ_LIKELY(!WantAllTraces());
|
2007-04-26 01:12:11 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
GCGraphBuilder::~GCGraphBuilder()
|
|
|
|
{
|
|
|
|
if (mPtrToNodeMap.ops)
|
|
|
|
PL_DHashTableFinish(&mPtrToNodeMap);
|
|
|
|
}
|
|
|
|
|
2010-02-28 13:23:59 +03:00
|
|
|
bool
|
|
|
|
GCGraphBuilder::Initialized()
|
|
|
|
{
|
|
|
|
return !!mPtrToNodeMap.ops;
|
|
|
|
}
|
|
|
|
|
2007-04-26 01:12:11 +04:00
|
|
|
PtrInfo*
|
2012-05-03 23:28:11 +04:00
|
|
|
GCGraphBuilder::AddNode(void *s, nsCycleCollectionParticipant *aParticipant)
|
2007-04-26 01:12:11 +04:00
|
|
|
{
|
2007-07-08 11:08:04 +04:00
|
|
|
PtrToNodeEntry *e = static_cast<PtrToNodeEntry*>(PL_DHashTableOperate(&mPtrToNodeMap, s, PL_DHASH_ADD));
|
2009-07-17 00:06:48 +04:00
|
|
|
if (!e)
|
2012-07-30 18:20:58 +04:00
|
|
|
return nullptr;
|
2009-07-17 00:06:48 +04:00
|
|
|
|
2007-04-26 01:12:11 +04:00
|
|
|
PtrInfo *result;
|
|
|
|
if (!e->mNode) {
|
|
|
|
// New entry.
|
2012-05-03 23:28:11 +04:00
|
|
|
result = mNodeBuilder.Add(s, aParticipant);
|
2007-04-26 01:12:11 +04:00
|
|
|
if (!result) {
|
|
|
|
PL_DHashTableRawRemove(&mPtrToNodeMap, e);
|
2012-07-30 18:20:58 +04:00
|
|
|
return nullptr;
|
2007-04-26 01:12:11 +04:00
|
|
|
}
|
|
|
|
e->mNode = result;
|
|
|
|
} else {
|
|
|
|
result = e->mNode;
|
2012-11-15 11:32:39 +04:00
|
|
|
MOZ_ASSERT(result->mParticipant == aParticipant,
|
|
|
|
"nsCycleCollectionParticipant shouldn't change!");
|
2007-04-26 01:12:11 +04:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2012-02-21 00:13:29 +04:00
|
|
|
MOZ_NEVER_INLINE void
|
2007-04-26 01:12:11 +04:00
|
|
|
GCGraphBuilder::Traverse(PtrInfo* aPtrInfo)
|
|
|
|
{
|
|
|
|
mCurrPi = aPtrInfo;
|
|
|
|
|
|
|
|
#ifdef DEBUG_CC
|
2007-05-24 18:10:02 +04:00
|
|
|
if (!mCurrPi->mParticipant) {
|
2007-07-06 02:38:38 +04:00
|
|
|
Fault("unknown pointer during walk", aPtrInfo);
|
2007-04-26 01:12:11 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2011-06-10 01:55:04 +04:00
|
|
|
mCurrPi->SetFirstChild(mEdgeBuilder.Mark());
|
|
|
|
|
2007-05-24 18:10:02 +04:00
|
|
|
nsresult rv = aPtrInfo->mParticipant->Traverse(aPtrInfo->mPointer, *this);
|
2007-04-26 01:12:11 +04:00
|
|
|
if (NS_FAILED(rv)) {
|
2007-07-06 02:38:38 +04:00
|
|
|
Fault("script pointer traversal failed", aPtrInfo);
|
2007-04-26 01:12:11 +04:00
|
|
|
}
|
2011-06-10 01:55:29 +04:00
|
|
|
}
|
2007-04-26 01:12:11 +04:00
|
|
|
|
2011-06-10 01:55:29 +04:00
|
|
|
void
|
|
|
|
GCGraphBuilder::SetLastChild()
|
|
|
|
{
|
2011-06-10 01:55:04 +04:00
|
|
|
mCurrPi->SetLastChild(mEdgeBuilder.Mark());
|
2007-04-26 01:12:11 +04:00
|
|
|
}
|
|
|
|
|
2007-11-02 01:51:57 +03:00
|
|
|
NS_IMETHODIMP_(void)
|
|
|
|
GCGraphBuilder::NoteXPCOMRoot(nsISupports *root)
|
|
|
|
{
|
2012-08-24 20:50:06 +04:00
|
|
|
root = CanonicalizeXPCOMParticipant(root);
|
2007-11-02 01:51:57 +03:00
|
|
|
NS_ASSERTION(root,
|
|
|
|
"Don't add objects that don't participate in collection!");
|
|
|
|
|
|
|
|
#ifdef DEBUG_CC
|
|
|
|
if (nsCycleCollector_shouldSuppress(root))
|
|
|
|
return;
|
|
|
|
#endif
|
2012-08-24 20:50:06 +04:00
|
|
|
|
2007-11-02 01:51:57 +03:00
|
|
|
nsXPCOMCycleCollectionParticipant *cp;
|
|
|
|
ToParticipant(root, &cp);
|
|
|
|
|
2012-05-03 23:28:11 +04:00
|
|
|
NoteRoot(root, cp);
|
2007-11-02 01:51:57 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP_(void)
|
2012-05-03 23:28:11 +04:00
|
|
|
GCGraphBuilder::NoteJSRoot(void *root)
|
2007-11-02 01:51:57 +03:00
|
|
|
{
|
2012-06-27 19:09:56 +04:00
|
|
|
if (JSCompartment *comp = MergeCompartment(root)) {
|
|
|
|
NoteRoot(comp, mJSCompParticipant);
|
|
|
|
} else {
|
|
|
|
NoteRoot(root, mJSParticipant);
|
|
|
|
}
|
2012-05-03 23:28:11 +04:00
|
|
|
}
|
2007-11-02 01:51:57 +03:00
|
|
|
|
2012-05-03 23:28:11 +04:00
|
|
|
NS_IMETHODIMP_(void)
|
|
|
|
GCGraphBuilder::NoteNativeRoot(void *root, nsCycleCollectionParticipant *participant)
|
|
|
|
{
|
2012-05-03 23:28:11 +04:00
|
|
|
NoteRoot(root, participant);
|
2007-11-02 01:51:57 +03:00
|
|
|
}
|
|
|
|
|
2007-09-18 04:30:06 +04:00
|
|
|
NS_IMETHODIMP_(void)
|
2012-08-27 21:41:04 +04:00
|
|
|
GCGraphBuilder::DescribeRefCountedNode(nsrefcnt refCount, const char *objName)
|
2011-06-24 01:10:52 +04:00
|
|
|
{
|
|
|
|
if (refCount == 0)
|
|
|
|
Fault("zero refcount", mCurrPi);
|
2012-09-28 10:57:33 +04:00
|
|
|
if (refCount == UINT32_MAX)
|
2011-06-24 01:10:52 +04:00
|
|
|
Fault("overflowing refcount", mCurrPi);
|
2011-06-11 00:33:06 +04:00
|
|
|
sCollector->mVisitedRefCounted++;
|
2011-06-22 21:41:17 +04:00
|
|
|
|
|
|
|
if (mListener) {
|
2012-08-22 19:56:38 +04:00
|
|
|
mListener->NoteRefCountedObject((uint64_t)mCurrPi->mPointer, refCount,
|
2011-06-22 21:41:17 +04:00
|
|
|
objName);
|
|
|
|
}
|
|
|
|
|
2012-08-27 21:41:04 +04:00
|
|
|
DescribeNode(refCount, objName);
|
2011-06-24 01:10:52 +04:00
|
|
|
}
|
2007-07-06 02:38:38 +04:00
|
|
|
|
2011-06-24 01:10:52 +04:00
|
|
|
NS_IMETHODIMP_(void)
|
2012-08-27 21:41:04 +04:00
|
|
|
GCGraphBuilder::DescribeGCedNode(bool isMarked, const char *objName)
|
2011-06-24 01:10:52 +04:00
|
|
|
{
|
2012-09-28 10:57:33 +04:00
|
|
|
uint32_t refCount = isMarked ? UINT32_MAX : 0;
|
2011-06-11 00:33:06 +04:00
|
|
|
sCollector->mVisitedGCed++;
|
2011-06-22 21:41:17 +04:00
|
|
|
|
|
|
|
if (mListener) {
|
2012-08-22 19:56:38 +04:00
|
|
|
mListener->NoteGCedObject((uint64_t)mCurrPi->mPointer, isMarked,
|
2011-06-22 21:41:17 +04:00
|
|
|
objName);
|
|
|
|
}
|
|
|
|
|
2012-08-27 21:41:04 +04:00
|
|
|
DescribeNode(refCount, objName);
|
2007-01-05 01:31:26 +03:00
|
|
|
}
|
|
|
|
|
2007-09-18 04:30:06 +04:00
|
|
|
NS_IMETHODIMP_(void)
|
2007-04-26 01:12:11 +04:00
|
|
|
GCGraphBuilder::NoteXPCOMChild(nsISupports *child)
|
2007-01-05 01:31:26 +03:00
|
|
|
{
|
2010-08-12 04:03:23 +04:00
|
|
|
nsCString edgeName;
|
|
|
|
if (WantDebugInfo()) {
|
|
|
|
edgeName.Assign(mNextEdgeName);
|
|
|
|
mNextEdgeName.Truncate();
|
|
|
|
}
|
2012-08-24 20:50:06 +04:00
|
|
|
if (!child || !(child = CanonicalizeXPCOMParticipant(child)))
|
|
|
|
return;
|
2007-01-09 04:33:02 +03:00
|
|
|
|
2007-04-21 21:43:19 +04:00
|
|
|
#ifdef DEBUG_CC
|
2007-05-24 18:10:02 +04:00
|
|
|
if (nsCycleCollector_shouldSuppress(child))
|
|
|
|
return;
|
2007-04-21 21:43:19 +04:00
|
|
|
#endif
|
2012-08-24 20:50:06 +04:00
|
|
|
|
2007-05-24 18:10:02 +04:00
|
|
|
nsXPCOMCycleCollectionParticipant *cp;
|
|
|
|
ToParticipant(child, &cp);
|
2012-01-30 04:45:08 +04:00
|
|
|
if (cp && (!cp->CanSkipThis(child) || WantAllTraces())) {
|
2012-05-03 23:28:11 +04:00
|
|
|
NoteChild(child, cp, edgeName);
|
2007-01-05 01:31:26 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-09-18 04:30:06 +04:00
|
|
|
NS_IMETHODIMP_(void)
|
2007-05-24 18:10:02 +04:00
|
|
|
GCGraphBuilder::NoteNativeChild(void *child,
|
|
|
|
nsCycleCollectionParticipant *participant)
|
|
|
|
{
|
2010-08-12 04:03:23 +04:00
|
|
|
nsCString edgeName;
|
|
|
|
if (WantDebugInfo()) {
|
|
|
|
edgeName.Assign(mNextEdgeName);
|
|
|
|
mNextEdgeName.Truncate();
|
|
|
|
}
|
2007-05-24 18:10:02 +04:00
|
|
|
if (!child)
|
|
|
|
return;
|
|
|
|
|
2012-11-15 11:32:39 +04:00
|
|
|
MOZ_ASSERT(participant, "Need a nsCycleCollectionParticipant!");
|
2012-05-03 23:28:11 +04:00
|
|
|
NoteChild(child, participant, edgeName);
|
2007-05-24 18:10:02 +04:00
|
|
|
}
|
|
|
|
|
2007-09-18 04:30:06 +04:00
|
|
|
NS_IMETHODIMP_(void)
|
2012-05-03 23:28:10 +04:00
|
|
|
GCGraphBuilder::NoteJSChild(void *child)
|
2007-01-05 01:31:26 +03:00
|
|
|
{
|
2012-05-03 23:28:10 +04:00
|
|
|
if (!child) {
|
2008-06-21 19:25:29 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-05-03 23:28:10 +04:00
|
|
|
nsCString edgeName;
|
2012-10-26 17:32:10 +04:00
|
|
|
if (MOZ_UNLIKELY(WantDebugInfo())) {
|
2012-05-03 23:28:10 +04:00
|
|
|
edgeName.Assign(mNextEdgeName);
|
|
|
|
mNextEdgeName.Truncate();
|
2007-04-21 21:43:19 +04:00
|
|
|
}
|
2007-01-09 04:33:02 +03:00
|
|
|
|
2012-10-26 17:32:10 +04:00
|
|
|
if (xpc_GCThingIsGrayCCThing(child) || MOZ_UNLIKELY(WantAllTraces())) {
|
2012-06-27 19:09:56 +04:00
|
|
|
if (JSCompartment *comp = MergeCompartment(child)) {
|
|
|
|
NoteChild(comp, mJSCompParticipant, edgeName);
|
|
|
|
} else {
|
|
|
|
NoteChild(child, mJSParticipant, edgeName);
|
|
|
|
}
|
2011-03-29 00:05:48 +04:00
|
|
|
}
|
2007-01-05 01:31:26 +03:00
|
|
|
}
|
|
|
|
|
2008-03-18 02:11:08 +03:00
|
|
|
NS_IMETHODIMP_(void)
|
|
|
|
GCGraphBuilder::NoteNextEdgeName(const char* name)
|
|
|
|
{
|
2010-08-12 04:03:23 +04:00
|
|
|
if (WantDebugInfo()) {
|
|
|
|
mNextEdgeName = name;
|
|
|
|
}
|
2009-07-09 05:10:29 +04:00
|
|
|
}
|
2008-03-18 02:11:08 +03:00
|
|
|
|
2011-11-24 16:35:56 +04:00
|
|
|
PtrInfo*
|
|
|
|
GCGraphBuilder::AddWeakMapNode(void *node)
|
|
|
|
{
|
2012-11-15 11:32:39 +04:00
|
|
|
MOZ_ASSERT(node, "Weak map node should be non-null.");
|
2011-11-24 16:35:56 +04:00
|
|
|
|
|
|
|
if (!xpc_GCThingIsGrayCCThing(node) && !WantAllTraces())
|
2012-07-30 18:20:58 +04:00
|
|
|
return nullptr;
|
2011-11-24 16:35:56 +04:00
|
|
|
|
2012-06-27 19:09:56 +04:00
|
|
|
if (JSCompartment *comp = MergeCompartment(node)) {
|
|
|
|
return AddNode(comp, mJSCompParticipant);
|
|
|
|
} else {
|
|
|
|
return AddNode(node, mJSParticipant);
|
|
|
|
}
|
2011-11-24 16:35:56 +04:00
|
|
|
}
|
|
|
|
|
2011-11-24 16:35:56 +04:00
|
|
|
NS_IMETHODIMP_(void)
|
2012-10-18 05:22:46 +04:00
|
|
|
GCGraphBuilder::NoteWeakMapping(void *map, void *key, void *kdelegate, void *val)
|
2011-11-24 16:35:56 +04:00
|
|
|
{
|
2011-11-24 16:35:56 +04:00
|
|
|
PtrInfo *valNode = AddWeakMapNode(val);
|
|
|
|
|
|
|
|
if (!valNode)
|
|
|
|
return;
|
|
|
|
|
|
|
|
WeakMapping *mapping = mWeakMaps.AppendElement();
|
2012-07-30 18:20:58 +04:00
|
|
|
mapping->mMap = map ? AddWeakMapNode(map) : nullptr;
|
|
|
|
mapping->mKey = key ? AddWeakMapNode(key) : nullptr;
|
2012-10-18 05:22:46 +04:00
|
|
|
mapping->mKeyDelegate = kdelegate ? AddWeakMapNode(kdelegate) : mapping->mKey;
|
2011-11-24 16:35:56 +04:00
|
|
|
mapping->mVal = valNode;
|
2011-11-24 16:35:56 +04:00
|
|
|
}
|
|
|
|
|
2012-08-24 20:50:06 +04:00
|
|
|
static bool
|
|
|
|
AddPurpleRoot(GCGraphBuilder &builder, void *root, nsCycleCollectionParticipant *cp)
|
|
|
|
{
|
|
|
|
CanonicalizeParticipant(&root, &cp);
|
|
|
|
|
|
|
|
if (builder.WantAllTraces() || !cp->CanSkipInCC(root)) {
|
|
|
|
PtrInfo *pinfo = builder.AddNode(root, cp);
|
|
|
|
if (!pinfo) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
cp->UnmarkIfPurple(root);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-03-06 01:48:04 +04:00
|
|
|
// MayHaveChild() will be false after a Traverse if the object does
|
|
|
|
// not have any children the CC will visit.
|
|
|
|
class ChildFinder : public nsCycleCollectionTraversalCallback
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
ChildFinder() : mMayHaveChild(false) {}
|
|
|
|
|
|
|
|
// The logic of the Note*Child functions must mirror that of their
|
|
|
|
// respective functions in GCGraphBuilder.
|
|
|
|
NS_IMETHOD_(void) NoteXPCOMChild(nsISupports *child);
|
|
|
|
NS_IMETHOD_(void) NoteNativeChild(void *child,
|
|
|
|
nsCycleCollectionParticipant *helper);
|
2012-05-03 23:28:10 +04:00
|
|
|
NS_IMETHOD_(void) NoteJSChild(void *child);
|
2012-03-06 01:48:04 +04:00
|
|
|
|
|
|
|
NS_IMETHOD_(void) DescribeRefCountedNode(nsrefcnt refcount,
|
2012-07-26 19:16:28 +04:00
|
|
|
const char *objname) {}
|
2012-03-06 01:48:04 +04:00
|
|
|
NS_IMETHOD_(void) DescribeGCedNode(bool ismarked,
|
2012-07-26 19:16:28 +04:00
|
|
|
const char *objname) {}
|
|
|
|
NS_IMETHOD_(void) NoteXPCOMRoot(nsISupports *root) {}
|
|
|
|
NS_IMETHOD_(void) NoteJSRoot(void *root) {}
|
2012-05-03 23:28:11 +04:00
|
|
|
NS_IMETHOD_(void) NoteNativeRoot(void *root,
|
2012-07-26 19:16:28 +04:00
|
|
|
nsCycleCollectionParticipant *helper) {}
|
|
|
|
NS_IMETHOD_(void) NoteNextEdgeName(const char* name) {}
|
2012-10-18 05:22:46 +04:00
|
|
|
NS_IMETHOD_(void) NoteWeakMapping(void *map, void *key, void *kdelegate, void *val) {}
|
2012-03-06 01:48:04 +04:00
|
|
|
bool MayHaveChild() {
|
|
|
|
return mMayHaveChild;
|
2012-07-26 19:16:28 +04:00
|
|
|
}
|
2012-03-06 01:48:04 +04:00
|
|
|
private:
|
|
|
|
bool mMayHaveChild;
|
|
|
|
};
|
|
|
|
|
|
|
|
NS_IMETHODIMP_(void)
|
|
|
|
ChildFinder::NoteXPCOMChild(nsISupports *child)
|
|
|
|
{
|
2012-08-24 20:50:06 +04:00
|
|
|
if (!child || !(child = CanonicalizeXPCOMParticipant(child)))
|
|
|
|
return;
|
2012-03-06 01:48:04 +04:00
|
|
|
nsXPCOMCycleCollectionParticipant *cp;
|
|
|
|
ToParticipant(child, &cp);
|
|
|
|
if (cp && !cp->CanSkip(child, true))
|
|
|
|
mMayHaveChild = true;
|
2012-07-26 19:16:28 +04:00
|
|
|
}
|
2012-03-06 01:48:04 +04:00
|
|
|
|
|
|
|
NS_IMETHODIMP_(void)
|
|
|
|
ChildFinder::NoteNativeChild(void *child,
|
|
|
|
nsCycleCollectionParticipant *helper)
|
|
|
|
{
|
|
|
|
if (child)
|
|
|
|
mMayHaveChild = true;
|
2012-07-26 19:16:28 +04:00
|
|
|
}
|
2012-03-06 01:48:04 +04:00
|
|
|
|
|
|
|
NS_IMETHODIMP_(void)
|
2012-05-03 23:28:10 +04:00
|
|
|
ChildFinder::NoteJSChild(void *child)
|
2012-03-06 01:48:04 +04:00
|
|
|
{
|
2012-05-03 23:28:10 +04:00
|
|
|
if (child && xpc_GCThingIsGrayCCThing(child)) {
|
|
|
|
mMayHaveChild = true;
|
2012-03-06 01:48:04 +04:00
|
|
|
}
|
2012-07-26 19:16:28 +04:00
|
|
|
}
|
2012-03-06 01:48:04 +04:00
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
static bool
|
2012-08-24 20:50:06 +04:00
|
|
|
MayHaveChild(void *o, nsCycleCollectionParticipant* cp)
|
2012-03-06 01:48:04 +04:00
|
|
|
{
|
|
|
|
ChildFinder cf;
|
|
|
|
cp->Traverse(o, cf);
|
|
|
|
return cf.MayHaveChild();
|
|
|
|
}
|
|
|
|
|
2012-01-14 20:58:05 +04:00
|
|
|
void
|
2012-03-06 01:48:04 +04:00
|
|
|
nsPurpleBuffer::RemoveSkippable(bool removeChildlessNodes)
|
2012-01-14 20:58:05 +04:00
|
|
|
{
|
|
|
|
// Walk through all the blocks.
|
|
|
|
for (Block *b = &mFirstBlock; b; b = b->mNext) {
|
|
|
|
for (nsPurpleBufferEntry *e = b->mEntries,
|
|
|
|
*eEnd = ArrayEnd(b->mEntries);
|
|
|
|
e != eEnd; ++e) {
|
2012-04-12 04:17:44 +04:00
|
|
|
if (!(uintptr_t(e->mObject) & uintptr_t(1))) {
|
2012-01-14 20:58:05 +04:00
|
|
|
// This is a real entry (rather than something on the
|
|
|
|
// free list).
|
|
|
|
if (e->mObject) {
|
2012-08-24 20:50:06 +04:00
|
|
|
void *o = e->mObject;
|
|
|
|
nsCycleCollectionParticipant *cp = e->mParticipant;
|
|
|
|
CanonicalizeParticipant(&o, &cp);
|
2012-09-08 14:46:48 +04:00
|
|
|
if (!e->mNotPurple && !cp->CanSkip(o, false) &&
|
2012-03-06 01:48:04 +04:00
|
|
|
(!removeChildlessNodes || MayHaveChild(o, cp))) {
|
2012-01-14 20:58:05 +04:00
|
|
|
continue;
|
|
|
|
}
|
2012-03-26 20:57:29 +04:00
|
|
|
cp->UnmarkIfPurple(o);
|
2012-01-14 20:58:05 +04:00
|
|
|
}
|
|
|
|
Remove(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-01-05 01:31:26 +03:00
|
|
|
void
|
2008-02-15 16:12:55 +03:00
|
|
|
nsCycleCollector::SelectPurple(GCGraphBuilder &builder)
|
2007-01-05 01:31:26 +03:00
|
|
|
{
|
2009-05-07 00:46:04 +04:00
|
|
|
mPurpleBuf.SelectPointers(builder);
|
2007-01-05 01:31:26 +03:00
|
|
|
}
|
|
|
|
|
2012-01-14 20:58:05 +04:00
|
|
|
void
|
2012-03-06 01:48:04 +04:00
|
|
|
nsCycleCollector::ForgetSkippable(bool removeChildlessNodes)
|
2012-01-14 20:58:05 +04:00
|
|
|
{
|
|
|
|
nsCOMPtr<nsIObserverService> obs = mozilla::services::GetObserverService();
|
|
|
|
if (obs) {
|
2012-07-30 18:20:58 +04:00
|
|
|
obs->NotifyObservers(nullptr, "cycle-collector-forget-skippable", nullptr);
|
2012-01-14 20:58:05 +04:00
|
|
|
}
|
2012-03-06 01:48:04 +04:00
|
|
|
mPurpleBuf.RemoveSkippable(removeChildlessNodes);
|
2012-01-14 20:58:05 +04:00
|
|
|
if (mForgetSkippableCB) {
|
|
|
|
mForgetSkippableCB();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-21 00:13:29 +04:00
|
|
|
MOZ_NEVER_INLINE void
|
2008-01-10 17:10:03 +03:00
|
|
|
nsCycleCollector::MarkRoots(GCGraphBuilder &builder)
|
2007-01-05 01:31:26 +03:00
|
|
|
{
|
2008-01-10 17:10:03 +03:00
|
|
|
mGraph.mRootCount = builder.Count();
|
2007-04-26 01:12:11 +04:00
|
|
|
|
|
|
|
// read the PtrInfo out of the graph that we are building
|
2008-01-10 17:10:03 +03:00
|
|
|
NodePool::Enumerator queue(mGraph.mNodes);
|
2007-04-26 01:12:11 +04:00
|
|
|
while (!queue.IsDone()) {
|
|
|
|
PtrInfo *pi = queue.GetNext();
|
2012-02-21 00:13:29 +04:00
|
|
|
CC_AbortIfNull(pi);
|
2007-04-26 01:12:11 +04:00
|
|
|
builder.Traverse(pi);
|
2011-06-10 01:55:29 +04:00
|
|
|
if (queue.AtBlockEnd())
|
|
|
|
builder.SetLastChild();
|
2007-01-05 01:31:26 +03:00
|
|
|
}
|
2011-06-10 01:55:29 +04:00
|
|
|
if (mGraph.mRootCount > 0)
|
|
|
|
builder.SetLastChild();
|
2007-01-05 01:31:26 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////
|
|
|
|
// Bacon & Rajan's |ScanRoots| routine.
|
|
|
|
////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
|
2010-01-12 19:51:39 +03:00
|
|
|
struct ScanBlackVisitor
|
2007-01-05 01:31:26 +03:00
|
|
|
{
|
2012-08-22 19:56:38 +04:00
|
|
|
ScanBlackVisitor(uint32_t &aWhiteNodeCount)
|
2010-01-12 19:51:39 +03:00
|
|
|
: mWhiteNodeCount(aWhiteNodeCount)
|
2008-02-15 16:12:55 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool ShouldVisitNode(PtrInfo const *pi)
|
2007-01-05 01:31:26 +03:00
|
|
|
{
|
2007-04-20 12:01:01 +04:00
|
|
|
return pi->mColor != black;
|
2007-01-05 01:31:26 +03:00
|
|
|
}
|
|
|
|
|
2012-02-21 00:13:29 +04:00
|
|
|
MOZ_NEVER_INLINE void VisitNode(PtrInfo *pi)
|
2007-11-02 01:51:57 +03:00
|
|
|
{
|
2008-02-15 16:12:55 +03:00
|
|
|
if (pi->mColor == white)
|
|
|
|
--mWhiteNodeCount;
|
2007-04-20 12:01:01 +04:00
|
|
|
pi->mColor = black;
|
2007-04-21 21:43:19 +04:00
|
|
|
#ifdef DEBUG_CC
|
2007-03-27 13:49:06 +04:00
|
|
|
sCollector->mStats.mSetColorBlack++;
|
2007-04-21 21:43:19 +04:00
|
|
|
#endif
|
2007-01-05 01:31:26 +03:00
|
|
|
}
|
2008-02-15 16:12:55 +03:00
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t &mWhiteNodeCount;
|
2007-01-05 01:31:26 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2010-01-12 19:51:39 +03:00
|
|
|
struct scanVisitor
|
2007-01-05 01:31:26 +03:00
|
|
|
{
|
2012-08-22 19:56:38 +04:00
|
|
|
scanVisitor(uint32_t &aWhiteNodeCount) : mWhiteNodeCount(aWhiteNodeCount)
|
2008-02-15 16:12:55 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool ShouldVisitNode(PtrInfo const *pi)
|
2007-01-05 01:31:26 +03:00
|
|
|
{
|
2007-04-20 12:01:01 +04:00
|
|
|
return pi->mColor == grey;
|
2007-01-05 01:31:26 +03:00
|
|
|
}
|
|
|
|
|
2012-02-21 00:13:29 +04:00
|
|
|
MOZ_NEVER_INLINE void VisitNode(PtrInfo *pi)
|
2007-01-05 01:31:26 +03:00
|
|
|
{
|
2007-11-02 01:51:57 +03:00
|
|
|
if (pi->mInternalRefs > pi->mRefCount && pi->mRefCount > 0)
|
2007-07-06 02:38:38 +04:00
|
|
|
Fault("traversed refs exceed refcount", pi);
|
2007-01-05 01:31:26 +03:00
|
|
|
|
2007-11-02 01:51:57 +03:00
|
|
|
if (pi->mInternalRefs == pi->mRefCount || pi->mRefCount == 0) {
|
2007-04-20 12:01:01 +04:00
|
|
|
pi->mColor = white;
|
2008-02-15 16:12:55 +03:00
|
|
|
++mWhiteNodeCount;
|
2007-04-21 21:43:19 +04:00
|
|
|
#ifdef DEBUG_CC
|
2007-03-27 13:49:06 +04:00
|
|
|
sCollector->mStats.mSetColorWhite++;
|
2007-04-21 21:43:19 +04:00
|
|
|
#endif
|
2007-01-05 01:31:26 +03:00
|
|
|
} else {
|
2010-01-12 19:51:39 +03:00
|
|
|
GraphWalker<ScanBlackVisitor>(ScanBlackVisitor(mWhiteNodeCount)).Walk(pi);
|
2012-11-15 11:32:39 +04:00
|
|
|
MOZ_ASSERT(pi->mColor == black,
|
|
|
|
"Why didn't ScanBlackVisitor make pi black?");
|
2007-01-05 01:31:26 +03:00
|
|
|
}
|
|
|
|
}
|
2008-02-15 16:12:55 +03:00
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t &mWhiteNodeCount;
|
2007-01-05 01:31:26 +03:00
|
|
|
};
|
|
|
|
|
2011-11-24 16:35:57 +04:00
|
|
|
// Iterate over the WeakMaps. If we mark anything while iterating
|
|
|
|
// over the WeakMaps, we must iterate over all of the WeakMaps again.
|
|
|
|
void
|
|
|
|
nsCycleCollector::ScanWeakMaps()
|
|
|
|
{
|
|
|
|
bool anyChanged;
|
|
|
|
do {
|
|
|
|
anyChanged = false;
|
2012-08-22 19:56:38 +04:00
|
|
|
for (uint32_t i = 0; i < mGraph.mWeakMaps.Length(); i++) {
|
2011-11-24 16:35:57 +04:00
|
|
|
WeakMapping *wm = &mGraph.mWeakMaps[i];
|
|
|
|
|
|
|
|
// If mMap or mKey are null, the original object was marked black.
|
2012-05-25 11:18:30 +04:00
|
|
|
uint32_t mColor = wm->mMap ? wm->mMap->mColor : black;
|
|
|
|
uint32_t kColor = wm->mKey ? wm->mKey->mColor : black;
|
2012-10-18 05:22:46 +04:00
|
|
|
uint32_t kdColor = wm->mKeyDelegate ? wm->mKeyDelegate->mColor : black;
|
2011-11-24 16:35:57 +04:00
|
|
|
PtrInfo *v = wm->mVal;
|
|
|
|
|
|
|
|
// All non-null weak mapping maps, keys and values are
|
|
|
|
// roots (in the sense of WalkFromRoots) in the cycle
|
|
|
|
// collector graph, and thus should have been colored
|
|
|
|
// either black or white in ScanRoots().
|
2012-10-18 05:22:46 +04:00
|
|
|
MOZ_ASSERT(mColor != grey, "Uncolored weak map");
|
|
|
|
MOZ_ASSERT(kColor != grey, "Uncolored weak map key");
|
|
|
|
MOZ_ASSERT(kdColor != grey, "Uncolored weak map key delegate");
|
|
|
|
MOZ_ASSERT(v->mColor != grey, "Uncolored weak map value");
|
|
|
|
|
|
|
|
if (mColor == black && kColor != black && kdColor == black) {
|
|
|
|
GraphWalker<ScanBlackVisitor>(ScanBlackVisitor(mWhiteNodeCount)).Walk(wm->mKey);
|
|
|
|
anyChanged = true;
|
|
|
|
}
|
2011-11-24 16:35:57 +04:00
|
|
|
|
|
|
|
if (mColor == black && kColor == black && v->mColor != black) {
|
|
|
|
GraphWalker<ScanBlackVisitor>(ScanBlackVisitor(mWhiteNodeCount)).Walk(v);
|
|
|
|
anyChanged = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} while (anyChanged);
|
|
|
|
}
|
|
|
|
|
2007-01-05 01:31:26 +03:00
|
|
|
void
|
2008-01-10 17:10:03 +03:00
|
|
|
nsCycleCollector::ScanRoots()
|
2007-01-05 01:31:26 +03:00
|
|
|
{
|
2008-02-15 16:12:55 +03:00
|
|
|
mWhiteNodeCount = 0;
|
|
|
|
|
2007-04-26 01:12:11 +04:00
|
|
|
// On the assumption that most nodes will be black, it's
|
|
|
|
// probably faster to use a GraphWalker than a
|
|
|
|
// NodePool::Enumerator.
|
2010-01-12 19:51:39 +03:00
|
|
|
GraphWalker<scanVisitor>(scanVisitor(mWhiteNodeCount)).WalkFromRoots(mGraph);
|
2007-01-05 01:31:26 +03:00
|
|
|
|
2011-11-24 16:35:57 +04:00
|
|
|
ScanWeakMaps();
|
|
|
|
|
2007-04-21 21:43:19 +04:00
|
|
|
#ifdef DEBUG_CC
|
2007-01-05 01:31:26 +03:00
|
|
|
// Sanity check: scan should have colored all grey nodes black or
|
|
|
|
// white. So we ensure we have no grey nodes at this point.
|
2008-01-10 17:10:03 +03:00
|
|
|
NodePool::Enumerator etor(mGraph.mNodes);
|
2007-04-26 01:12:11 +04:00
|
|
|
while (!etor.IsDone())
|
|
|
|
{
|
|
|
|
PtrInfo *pinfo = etor.GetNext();
|
|
|
|
if (pinfo->mColor == grey) {
|
2007-07-06 02:38:38 +04:00
|
|
|
Fault("valid grey node after scanning", pinfo);
|
2007-04-26 01:12:11 +04:00
|
|
|
}
|
|
|
|
}
|
2007-04-21 21:43:19 +04:00
|
|
|
#endif
|
2007-01-05 01:31:26 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////
|
|
|
|
// Bacon & Rajan's |CollectWhite| routine, somewhat modified.
|
|
|
|
////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool
|
2011-06-22 21:41:17 +04:00
|
|
|
nsCycleCollector::CollectWhite(nsICycleCollectorListener *aListener)
|
2007-01-05 01:31:26 +03:00
|
|
|
{
|
|
|
|
// Explanation of "somewhat modified": we have no way to collect the
|
|
|
|
// set of whites "all at once", we have to ask each of them to drop
|
|
|
|
// their outgoing links and assume this will cause the garbage cycle
|
|
|
|
// to *mostly* self-destruct (except for the reference we continue
|
|
|
|
// to hold).
|
|
|
|
//
|
|
|
|
// To do this "safely" we must make sure that the white nodes we're
|
|
|
|
// operating on are stable for the duration of our operation. So we
|
|
|
|
// make 3 sets of calls to language runtimes:
|
|
|
|
//
|
|
|
|
// - Root(whites), which should pin the whites in memory.
|
|
|
|
// - Unlink(whites), which drops outgoing links on each white.
|
|
|
|
// - Unroot(whites), which returns the whites to normal GC.
|
|
|
|
|
|
|
|
nsresult rv;
|
2012-02-12 20:02:01 +04:00
|
|
|
TimeLog timeLog;
|
2007-01-05 01:31:26 +03:00
|
|
|
|
2012-11-15 11:32:39 +04:00
|
|
|
MOZ_ASSERT(mWhiteNodes->IsEmpty(),
|
|
|
|
"FinishCollection wasn't called?");
|
2008-02-15 16:12:55 +03:00
|
|
|
|
|
|
|
mWhiteNodes->SetCapacity(mWhiteNodeCount);
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t numWhiteGCed = 0;
|
2007-01-05 01:31:26 +03:00
|
|
|
|
2008-01-10 17:10:03 +03:00
|
|
|
NodePool::Enumerator etor(mGraph.mNodes);
|
2007-04-26 01:12:11 +04:00
|
|
|
while (!etor.IsDone())
|
|
|
|
{
|
|
|
|
PtrInfo *pinfo = etor.GetNext();
|
2008-02-15 16:12:55 +03:00
|
|
|
if (pinfo->mColor == white && mWhiteNodes->AppendElement(pinfo)) {
|
2010-12-16 18:30:13 +03:00
|
|
|
rv = pinfo->mParticipant->Root(pinfo->mPointer);
|
2008-02-15 16:12:55 +03:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
Fault("Failed root call while unlinking", pinfo);
|
|
|
|
mWhiteNodes->RemoveElementAt(mWhiteNodes->Length() - 1);
|
2012-02-24 08:16:37 +04:00
|
|
|
} else if (pinfo->mRefCount == 0) {
|
|
|
|
// only JS objects have a refcount of 0
|
|
|
|
++numWhiteGCed;
|
2008-02-15 16:12:55 +03:00
|
|
|
}
|
2007-04-26 01:12:11 +04:00
|
|
|
}
|
|
|
|
}
|
2012-02-24 08:16:37 +04:00
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t count = mWhiteNodes->Length();
|
2012-11-15 11:32:39 +04:00
|
|
|
MOZ_ASSERT(numWhiteGCed <= count,
|
|
|
|
"More freed GCed nodes than total freed nodes.");
|
2012-02-24 08:16:37 +04:00
|
|
|
if (mResults) {
|
|
|
|
mResults->mFreedRefCounted += count - numWhiteGCed;
|
|
|
|
mResults->mFreedGCed += numWhiteGCed;
|
|
|
|
}
|
|
|
|
|
2012-02-12 20:02:01 +04:00
|
|
|
timeLog.Checkpoint("CollectWhite::Root");
|
2008-03-07 20:55:51 +03:00
|
|
|
|
2012-01-14 20:58:05 +04:00
|
|
|
if (mBeforeUnlinkCB) {
|
|
|
|
mBeforeUnlinkCB();
|
2012-02-12 20:02:01 +04:00
|
|
|
timeLog.Checkpoint("CollectWhite::BeforeUnlinkCB");
|
2012-01-14 20:58:05 +04:00
|
|
|
}
|
2008-03-07 20:55:51 +03:00
|
|
|
#if defined(DEBUG_CC) && !defined(__MINGW32__) && defined(WIN32)
|
|
|
|
struct _CrtMemState ms1, ms2;
|
|
|
|
_CrtMemCheckpoint(&ms1);
|
|
|
|
#endif
|
|
|
|
|
2011-06-22 21:41:17 +04:00
|
|
|
if (aListener) {
|
2012-08-22 19:56:38 +04:00
|
|
|
for (uint32_t i = 0; i < count; ++i) {
|
2011-06-22 21:41:17 +04:00
|
|
|
PtrInfo *pinfo = mWhiteNodes->ElementAt(i);
|
2012-08-22 19:56:38 +04:00
|
|
|
aListener->DescribeGarbage((uint64_t)pinfo->mPointer);
|
2011-06-22 21:41:17 +04:00
|
|
|
}
|
|
|
|
aListener->End();
|
|
|
|
}
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
for (uint32_t i = 0; i < count; ++i) {
|
2008-02-15 16:12:55 +03:00
|
|
|
PtrInfo *pinfo = mWhiteNodes->ElementAt(i);
|
2007-05-24 18:10:02 +04:00
|
|
|
rv = pinfo->mParticipant->Unlink(pinfo->mPointer);
|
|
|
|
if (NS_FAILED(rv)) {
|
2007-07-06 02:38:38 +04:00
|
|
|
Fault("Failed unlink call while unlinking", pinfo);
|
2007-04-21 21:43:19 +04:00
|
|
|
#ifdef DEBUG_CC
|
2007-05-24 18:10:02 +04:00
|
|
|
mStats.mFailedUnlink++;
|
2007-04-21 21:43:19 +04:00
|
|
|
#endif
|
2007-05-24 18:10:02 +04:00
|
|
|
}
|
|
|
|
else {
|
2007-04-21 21:43:19 +04:00
|
|
|
#ifdef DEBUG_CC
|
2007-05-24 18:10:02 +04:00
|
|
|
++mStats.mCollectedNode;
|
2007-04-21 21:43:19 +04:00
|
|
|
#endif
|
2007-01-05 01:31:26 +03:00
|
|
|
}
|
|
|
|
}
|
2012-02-12 20:02:01 +04:00
|
|
|
timeLog.Checkpoint("CollectWhite::Unlink");
|
2007-01-05 01:31:26 +03:00
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
for (uint32_t i = 0; i < count; ++i) {
|
2008-02-15 16:12:55 +03:00
|
|
|
PtrInfo *pinfo = mWhiteNodes->ElementAt(i);
|
2007-05-24 18:10:02 +04:00
|
|
|
rv = pinfo->mParticipant->Unroot(pinfo->mPointer);
|
|
|
|
if (NS_FAILED(rv))
|
2007-07-06 02:38:38 +04:00
|
|
|
Fault("Failed unroot call while unlinking", pinfo);
|
2007-01-05 01:31:26 +03:00
|
|
|
}
|
2012-02-12 20:02:01 +04:00
|
|
|
timeLog.Checkpoint("CollectWhite::Unroot");
|
2007-01-05 01:31:26 +03:00
|
|
|
|
2007-04-21 21:43:19 +04:00
|
|
|
#if defined(DEBUG_CC) && !defined(__MINGW32__) && defined(WIN32)
|
2007-01-05 01:31:26 +03:00
|
|
|
_CrtMemCheckpoint(&ms2);
|
|
|
|
if (ms2.lTotalCount < ms1.lTotalCount)
|
|
|
|
mStats.mFreedBytes += (ms1.lTotalCount - ms2.lTotalCount);
|
2007-04-21 21:43:19 +04:00
|
|
|
#endif
|
2007-06-07 02:09:00 +04:00
|
|
|
|
|
|
|
return count > 0;
|
2007-01-05 01:31:26 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////
|
|
|
|
// Collector implementation
|
|
|
|
////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2012-02-24 08:16:37 +04:00
|
|
|
nsCycleCollector::nsCycleCollector() :
|
2011-10-17 18:59:28 +04:00
|
|
|
mCollectionInProgress(false),
|
|
|
|
mScanInProgress(false),
|
2012-07-30 18:20:58 +04:00
|
|
|
mResults(nullptr),
|
|
|
|
mJSRuntime(nullptr),
|
|
|
|
mWhiteNodes(nullptr),
|
2008-02-15 16:12:55 +03:00
|
|
|
mWhiteNodeCount(0),
|
2011-06-11 00:33:06 +04:00
|
|
|
mVisitedRefCounted(0),
|
|
|
|
mVisitedGCed(0),
|
2012-07-30 18:20:58 +04:00
|
|
|
mBeforeUnlinkCB(nullptr),
|
|
|
|
mForgetSkippableCB(nullptr),
|
2007-04-21 21:43:19 +04:00
|
|
|
#ifdef DEBUG_CC
|
2007-01-05 01:31:26 +03:00
|
|
|
mPurpleBuf(mParams, mStats),
|
2012-07-30 18:20:58 +04:00
|
|
|
mPtrLog(nullptr)
|
2007-04-21 21:43:19 +04:00
|
|
|
#else
|
2012-04-25 19:10:09 +04:00
|
|
|
mPurpleBuf(mParams)
|
2007-04-21 21:43:19 +04:00
|
|
|
#endif
|
2007-01-05 01:31:26 +03:00
|
|
|
{
|
2007-04-21 21:43:19 +04:00
|
|
|
#ifdef DEBUG_CC
|
2007-03-10 02:00:14 +03:00
|
|
|
mExpectedGarbage.Init();
|
|
|
|
#endif
|
2007-01-05 01:31:26 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
nsCycleCollector::~nsCycleCollector()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
2012-05-03 23:28:11 +04:00
|
|
|
nsCycleCollector::RegisterJSRuntime(nsCycleCollectionJSRuntime *aJSRuntime)
|
2007-01-05 01:31:26 +03:00
|
|
|
{
|
|
|
|
if (mParams.mDoNothing)
|
|
|
|
return;
|
|
|
|
|
2012-05-03 23:28:11 +04:00
|
|
|
if (mJSRuntime)
|
|
|
|
Fault("multiple registrations of cycle collector JS runtime", aJSRuntime);
|
2007-01-05 01:31:26 +03:00
|
|
|
|
2012-05-03 23:28:11 +04:00
|
|
|
mJSRuntime = aJSRuntime;
|
2007-01-05 01:31:26 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2012-05-03 23:28:11 +04:00
|
|
|
nsCycleCollector::ForgetJSRuntime()
|
2007-01-05 01:31:26 +03:00
|
|
|
{
|
|
|
|
if (mParams.mDoNothing)
|
|
|
|
return;
|
|
|
|
|
2012-05-03 23:28:11 +04:00
|
|
|
if (!mJSRuntime)
|
|
|
|
Fault("forgetting non-registered cycle collector JS runtime");
|
2012-05-03 23:28:10 +04:00
|
|
|
|
2012-07-30 18:20:58 +04:00
|
|
|
mJSRuntime = nullptr;
|
2007-01-05 01:31:26 +03:00
|
|
|
}
|
|
|
|
|
2010-08-18 11:46:54 +04:00
|
|
|
#ifdef DEBUG_CC
|
2007-01-05 01:31:26 +03:00
|
|
|
|
|
|
|
class Suppressor :
|
|
|
|
public nsCycleCollectionTraversalCallback
|
|
|
|
{
|
|
|
|
protected:
|
|
|
|
static char *sSuppressionList;
|
2011-09-29 10:19:26 +04:00
|
|
|
static bool sInitialized;
|
|
|
|
bool mSuppressThisNode;
|
2007-01-05 01:31:26 +03:00
|
|
|
public:
|
|
|
|
Suppressor()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool shouldSuppress(nsISupports *s)
|
2007-01-05 01:31:26 +03:00
|
|
|
{
|
|
|
|
if (!sInitialized) {
|
|
|
|
sSuppressionList = PR_GetEnv("XPCOM_CC_SUPPRESS");
|
2011-10-17 18:59:28 +04:00
|
|
|
sInitialized = true;
|
2007-01-05 01:31:26 +03:00
|
|
|
}
|
2012-07-30 18:20:58 +04:00
|
|
|
if (sSuppressionList == nullptr) {
|
2011-10-17 18:59:28 +04:00
|
|
|
mSuppressThisNode = false;
|
2007-01-05 01:31:26 +03:00
|
|
|
} else {
|
|
|
|
nsresult rv;
|
2007-05-24 18:10:02 +04:00
|
|
|
nsXPCOMCycleCollectionParticipant *cp;
|
|
|
|
rv = CallQueryInterface(s, &cp);
|
2007-01-05 01:31:26 +03:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
Fault("checking suppression on wrong type of pointer", s);
|
2011-10-17 18:59:28 +04:00
|
|
|
return true;
|
2007-01-05 01:31:26 +03:00
|
|
|
}
|
|
|
|
cp->Traverse(s, *this);
|
|
|
|
}
|
|
|
|
return mSuppressThisNode;
|
|
|
|
}
|
|
|
|
|
2012-08-27 21:41:04 +04:00
|
|
|
NS_IMETHOD_(void) DescribeRefCountedNode(nsrefcnt refCount,
|
2011-06-24 01:10:52 +04:00
|
|
|
const char *objName)
|
|
|
|
{
|
2012-07-30 18:20:58 +04:00
|
|
|
mSuppressThisNode = (PL_strstr(sSuppressionList, objName) != nullptr);
|
2011-06-24 01:10:52 +04:00
|
|
|
}
|
|
|
|
|
2012-08-27 21:41:04 +04:00
|
|
|
NS_IMETHOD_(void) DescribeGCedNode(bool isMarked, const char *objName)
|
2007-01-05 01:31:26 +03:00
|
|
|
{
|
2012-07-30 18:20:58 +04:00
|
|
|
mSuppressThisNode = (PL_strstr(sSuppressionList, objName) != nullptr);
|
2007-01-05 01:31:26 +03:00
|
|
|
}
|
|
|
|
|
2012-05-03 23:28:11 +04:00
|
|
|
NS_IMETHOD_(void) NoteXPCOMRoot(nsISupports *root) {}
|
|
|
|
NS_IMETHOD_(void) NoteJSRoot(void *root) {}
|
|
|
|
NS_IMETHOD_(void) NoteNativeRoot(void *root,
|
|
|
|
nsCycleCollectionParticipant *participant) {}
|
2007-09-18 04:30:06 +04:00
|
|
|
NS_IMETHOD_(void) NoteXPCOMChild(nsISupports *child) {}
|
2012-05-03 23:28:10 +04:00
|
|
|
NS_IMETHOD_(void) NoteJSChild(void *child) {}
|
2007-09-18 04:30:06 +04:00
|
|
|
NS_IMETHOD_(void) NoteNativeChild(void *child,
|
2007-08-31 01:47:26 +04:00
|
|
|
nsCycleCollectionParticipant *participant) {}
|
2008-03-18 02:11:08 +03:00
|
|
|
NS_IMETHOD_(void) NoteNextEdgeName(const char* name) {}
|
2012-10-18 05:22:46 +04:00
|
|
|
NS_IMETHOD_(void) NoteWeakMapping(void *map, void *key, void *kdelegate, void *val) {}
|
2007-01-05 01:31:26 +03:00
|
|
|
};
|
|
|
|
|
2012-07-30 18:20:58 +04:00
|
|
|
char *Suppressor::sSuppressionList = nullptr;
|
2011-09-29 10:19:26 +04:00
|
|
|
bool Suppressor::sInitialized = false;
|
2007-01-05 01:31:26 +03:00
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
static bool
|
2007-01-05 01:31:26 +03:00
|
|
|
nsCycleCollector_shouldSuppress(nsISupports *s)
|
|
|
|
{
|
|
|
|
Suppressor supp;
|
|
|
|
return supp.shouldSuppress(s);
|
|
|
|
}
|
2007-04-21 21:43:19 +04:00
|
|
|
#endif
|
2007-01-05 01:31:26 +03:00
|
|
|
|
2007-05-24 18:10:02 +04:00
|
|
|
#ifdef DEBUG
|
2011-09-29 10:19:26 +04:00
|
|
|
static bool
|
2012-08-24 20:50:06 +04:00
|
|
|
nsCycleCollector_isScanSafe(void *s, nsCycleCollectionParticipant *cp)
|
2007-05-24 18:10:02 +04:00
|
|
|
{
|
|
|
|
if (!s)
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2007-05-24 18:10:02 +04:00
|
|
|
|
2012-08-24 20:50:06 +04:00
|
|
|
if (cp)
|
|
|
|
return true;
|
2007-05-24 18:10:02 +04:00
|
|
|
|
2012-08-24 20:50:06 +04:00
|
|
|
nsXPCOMCycleCollectionParticipant *xcp;
|
|
|
|
ToParticipant(static_cast<nsISupports*>(s), &xcp);
|
|
|
|
|
|
|
|
return xcp != nullptr;
|
2007-05-24 18:10:02 +04:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2009-05-07 00:46:04 +04:00
|
|
|
nsPurpleBufferEntry*
|
2012-08-24 20:50:06 +04:00
|
|
|
nsCycleCollector::Suspect2(void *n, nsCycleCollectionParticipant *cp)
|
2009-05-07 00:46:04 +04:00
|
|
|
{
|
2010-03-03 03:04:34 +03:00
|
|
|
AbortIfOffMainThreadIfCheckFast();
|
2009-10-28 20:28:57 +03:00
|
|
|
|
2009-05-07 00:46:04 +04:00
|
|
|
// Re-entering ::Suspect during collection used to be a fault, but
|
|
|
|
// we are canonicalizing nsISupports pointers using QI, so we will
|
|
|
|
// see some spurious refcount traffic here.
|
|
|
|
|
|
|
|
if (mScanInProgress)
|
2012-07-30 18:20:58 +04:00
|
|
|
return nullptr;
|
2009-05-07 00:46:04 +04:00
|
|
|
|
2012-11-15 11:32:39 +04:00
|
|
|
MOZ_ASSERT(nsCycleCollector_isScanSafe(n, cp),
|
|
|
|
"suspected a non-scansafe pointer");
|
2009-05-07 00:46:04 +04:00
|
|
|
|
|
|
|
if (mParams.mDoNothing)
|
2012-07-30 18:20:58 +04:00
|
|
|
return nullptr;
|
2009-05-07 00:46:04 +04:00
|
|
|
|
|
|
|
#ifdef DEBUG_CC
|
2012-09-08 14:46:48 +04:00
|
|
|
if (!LogPurpleAddition(n, cp))
|
2012-07-30 18:20:58 +04:00
|
|
|
return nullptr;
|
2009-05-07 00:46:04 +04:00
|
|
|
#endif
|
|
|
|
|
|
|
|
// Caller is responsible for filling in result's mRefCnt.
|
2012-08-24 20:50:06 +04:00
|
|
|
return mPurpleBuf.Put(n, cp);
|
2009-05-07 00:46:04 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool
|
2009-05-07 00:46:04 +04:00
|
|
|
nsCycleCollector::Forget2(nsPurpleBufferEntry *e)
|
|
|
|
{
|
2010-03-03 03:04:34 +03:00
|
|
|
AbortIfOffMainThreadIfCheckFast();
|
2009-10-28 20:28:57 +03:00
|
|
|
|
2009-05-07 00:46:04 +04:00
|
|
|
// Re-entering ::Forget during collection used to be a fault, but
|
|
|
|
// we are canonicalizing nsISupports pointers using QI, so we will
|
|
|
|
// see some spurious refcount traffic here.
|
|
|
|
|
|
|
|
if (mScanInProgress)
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2009-05-07 00:46:04 +04:00
|
|
|
|
|
|
|
#ifdef DEBUG_CC
|
2012-01-14 20:58:05 +04:00
|
|
|
LogPurpleRemoval(e->mObject);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
mPurpleBuf.Remove(e);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef DEBUG_CC
|
2012-09-08 14:46:48 +04:00
|
|
|
void
|
|
|
|
nsCycleCollector_logPurpleAddition(void* aObject,
|
|
|
|
nsCycleCollectionParticipant *cp)
|
|
|
|
{
|
|
|
|
if (sCollector) {
|
|
|
|
sCollector->LogPurpleAddition(aObject, cp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
nsCycleCollector::LogPurpleAddition(void* aObject,
|
|
|
|
nsCycleCollectionParticipant *cp)
|
|
|
|
{
|
|
|
|
|
|
|
|
if (mScanInProgress)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (mParams.mDoNothing)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
mStats.mSuspectNode++;
|
|
|
|
|
|
|
|
if (!cp &&
|
|
|
|
nsCycleCollector_shouldSuppress(static_cast<nsISupports *>(aObject)))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (mParams.mLogPointers) {
|
|
|
|
if (!mPtrLog)
|
|
|
|
mPtrLog = fopen("pointer_log", "w");
|
|
|
|
fprintf(mPtrLog, "S %p\n", static_cast<void*>(aObject));
|
|
|
|
}
|
|
|
|
|
|
|
|
mPurpleBuf.mNormalObjects.PutEntry(aObject);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-01-14 20:58:05 +04:00
|
|
|
void
|
|
|
|
nsCycleCollector_logPurpleRemoval(void* aObject)
|
|
|
|
{
|
|
|
|
if (sCollector) {
|
|
|
|
sCollector->LogPurpleRemoval(aObject);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsCycleCollector::LogPurpleRemoval(void* aObject)
|
|
|
|
{
|
|
|
|
AbortIfOffMainThreadIfCheckFast();
|
|
|
|
|
2009-05-07 00:46:04 +04:00
|
|
|
mStats.mForgetNode++;
|
|
|
|
|
|
|
|
if (mParams.mLogPointers) {
|
|
|
|
if (!mPtrLog)
|
|
|
|
mPtrLog = fopen("pointer_log", "w");
|
2012-01-14 20:58:05 +04:00
|
|
|
fprintf(mPtrLog, "F %p\n", aObject);
|
2009-05-07 00:46:04 +04:00
|
|
|
}
|
2012-01-14 20:58:05 +04:00
|
|
|
mPurpleBuf.mNormalObjects.RemoveEntry(aObject);
|
2007-01-05 01:31:26 +03:00
|
|
|
}
|
2007-04-21 21:43:19 +04:00
|
|
|
#endif
|
2007-01-05 01:31:26 +03:00
|
|
|
|
2011-06-16 02:53:00 +04:00
|
|
|
// The cycle collector uses the mark bitmap to discover what JS objects
|
|
|
|
// were reachable only from XPConnect roots that might participate in
|
|
|
|
// cycles. We ask the JS runtime whether we need to force a GC before
|
|
|
|
// this CC. It returns true on startup (before the mark bits have been set),
|
|
|
|
// and also when UnmarkGray has run out of stack. We also force GCs on shut
|
|
|
|
// down to collect cycles involving both DOM and JS.
|
|
|
|
void
|
2011-09-29 10:19:26 +04:00
|
|
|
nsCycleCollector::GCIfNeeded(bool aForceGC)
|
2007-01-05 01:31:26 +03:00
|
|
|
{
|
2012-11-15 11:32:39 +04:00
|
|
|
MOZ_ASSERT(NS_IsMainThread(),
|
|
|
|
"nsCycleCollector::GCIfNeeded() must be called on the main thread.");
|
2011-06-16 02:53:00 +04:00
|
|
|
|
|
|
|
if (mParams.mDoNothing)
|
|
|
|
return;
|
|
|
|
|
2012-05-03 23:28:10 +04:00
|
|
|
if (!mJSRuntime)
|
2011-06-16 02:53:00 +04:00
|
|
|
return;
|
2011-06-14 00:24:23 +04:00
|
|
|
|
2011-12-05 21:04:41 +04:00
|
|
|
if (!aForceGC) {
|
2012-05-03 23:28:10 +04:00
|
|
|
bool needGC = mJSRuntime->NeedCollect();
|
2011-12-05 21:04:41 +04:00
|
|
|
// Only do a telemetry ping for non-shutdown CCs.
|
|
|
|
Telemetry::Accumulate(Telemetry::CYCLE_COLLECTOR_NEED_GC, needGC);
|
|
|
|
if (!needGC)
|
|
|
|
return;
|
2012-02-24 08:16:37 +04:00
|
|
|
if (mResults)
|
|
|
|
mResults->mForcedGC = true;
|
2011-12-05 21:04:41 +04:00
|
|
|
}
|
2011-06-16 02:53:00 +04:00
|
|
|
|
2012-02-12 20:02:01 +04:00
|
|
|
TimeLog timeLog;
|
|
|
|
|
2012-05-03 23:28:10 +04:00
|
|
|
// mJSRuntime->Collect() must be called from the main thread,
|
2011-06-16 02:53:00 +04:00
|
|
|
// because it invokes XPCJSRuntime::GCCallback(cx, JSGC_BEGIN)
|
|
|
|
// which returns false if not in the main thread.
|
2012-07-01 01:16:32 +04:00
|
|
|
mJSRuntime->Collect(aForceGC ? js::gcreason::SHUTDOWN_CC : js::gcreason::CC_FORCED);
|
2012-02-12 20:02:01 +04:00
|
|
|
timeLog.Checkpoint("GC()");
|
2011-06-16 02:53:00 +04:00
|
|
|
}
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool
|
2012-02-24 08:16:37 +04:00
|
|
|
nsCycleCollector::PrepareForCollection(nsCycleCollectorResults *aResults,
|
|
|
|
nsTArray<PtrInfo*> *aWhiteNodes)
|
2011-06-16 02:53:00 +04:00
|
|
|
{
|
2007-06-08 14:17:21 +04:00
|
|
|
// This can legitimately happen in a few cases. See bug 383651.
|
|
|
|
if (mCollectionInProgress)
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2007-06-08 14:17:21 +04:00
|
|
|
|
2012-02-12 20:02:01 +04:00
|
|
|
TimeLog timeLog;
|
2010-04-30 23:40:59 +04:00
|
|
|
|
2011-05-12 22:22:25 +04:00
|
|
|
mCollectionStart = TimeStamp::Now();
|
2011-06-11 00:33:06 +04:00
|
|
|
mVisitedRefCounted = 0;
|
|
|
|
mVisitedGCed = 0;
|
2010-08-12 04:03:23 +04:00
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
mCollectionInProgress = true;
|
2007-06-08 14:17:21 +04:00
|
|
|
|
2007-05-11 00:21:12 +04:00
|
|
|
nsCOMPtr<nsIObserverService> obs =
|
Bug 560095 - Use mozilla::services::GetObserverService(). r=biesi,dveditz,gavin,josh,jst,mrbkap,roc,sdwilsh,shaver,sicking,smontagu,surkov
2010-04-29 20:59:13 +04:00
|
|
|
mozilla::services::GetObserverService();
|
|
|
|
if (obs)
|
2012-07-30 18:20:58 +04:00
|
|
|
obs->NotifyObservers(nullptr, "cycle-collector-begin", nullptr);
|
2007-05-11 00:21:12 +04:00
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
mFollowupCollection = false;
|
2010-07-20 10:31:01 +04:00
|
|
|
|
2012-02-24 08:16:37 +04:00
|
|
|
mResults = aResults;
|
2010-11-12 01:52:30 +03:00
|
|
|
mWhiteNodes = aWhiteNodes;
|
2007-11-02 01:51:57 +03:00
|
|
|
|
2012-02-12 20:02:01 +04:00
|
|
|
timeLog.Checkpoint("PrepareForCollection()");
|
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
return true;
|
2010-11-12 01:52:30 +03:00
|
|
|
}
|
2007-11-02 01:51:57 +03:00
|
|
|
|
2010-11-12 01:52:30 +03:00
|
|
|
void
|
|
|
|
nsCycleCollector::CleanupAfterCollection()
|
|
|
|
{
|
2012-07-30 18:20:58 +04:00
|
|
|
mWhiteNodes = nullptr;
|
2011-10-17 18:59:28 +04:00
|
|
|
mCollectionInProgress = false;
|
2007-03-16 15:52:47 +03:00
|
|
|
|
2008-01-10 00:14:08 +03:00
|
|
|
#ifdef XP_OS2
|
|
|
|
// Now that the cycle collector has freed some memory, we can try to
|
|
|
|
// force the C library to give back as much memory to the system as
|
|
|
|
// possible.
|
|
|
|
_heapmin();
|
|
|
|
#endif
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t interval = (uint32_t) ((TimeStamp::Now() - mCollectionStart).ToMilliseconds());
|
2007-03-20 02:21:31 +03:00
|
|
|
#ifdef COLLECT_TIME_DEBUG
|
2012-02-12 20:02:01 +04:00
|
|
|
printf("cc: total cycle collector time was %ums\n", interval);
|
2012-02-24 08:16:37 +04:00
|
|
|
if (mResults) {
|
|
|
|
printf("cc: visited %u ref counted and %u GCed objects, freed %d ref counted and %d GCed objects.\n",
|
|
|
|
mVisitedRefCounted, mVisitedGCed,
|
|
|
|
mResults->mFreedRefCounted, mResults->mFreedGCed);
|
|
|
|
} else {
|
|
|
|
printf("cc: visited %u ref counted and %u GCed objects, freed %d.\n",
|
|
|
|
mVisitedRefCounted, mVisitedGCed, mWhiteNodeCount);
|
|
|
|
}
|
2012-02-12 20:02:01 +04:00
|
|
|
printf("cc: \n");
|
2007-03-20 02:21:31 +03:00
|
|
|
#endif
|
2012-02-24 08:16:37 +04:00
|
|
|
if (mResults) {
|
|
|
|
mResults->mVisitedRefCounted = mVisitedRefCounted;
|
|
|
|
mResults->mVisitedGCed = mVisitedGCed;
|
2012-07-30 18:20:58 +04:00
|
|
|
mResults = nullptr;
|
2012-02-24 08:16:37 +04:00
|
|
|
}
|
2011-06-21 01:47:58 +04:00
|
|
|
Telemetry::Accumulate(Telemetry::CYCLE_COLLECTOR, interval);
|
2011-06-11 00:33:06 +04:00
|
|
|
Telemetry::Accumulate(Telemetry::CYCLE_COLLECTOR_VISITED_REF_COUNTED, mVisitedRefCounted);
|
|
|
|
Telemetry::Accumulate(Telemetry::CYCLE_COLLECTOR_VISITED_GCED, mVisitedGCed);
|
|
|
|
Telemetry::Accumulate(Telemetry::CYCLE_COLLECTOR_COLLECTED, mWhiteNodeCount);
|
2010-11-12 01:52:30 +03:00
|
|
|
}
|
|
|
|
|
2012-02-24 08:16:37 +04:00
|
|
|
void
|
2012-06-27 19:09:56 +04:00
|
|
|
nsCycleCollector::Collect(bool aMergeCompartments,
|
|
|
|
nsCycleCollectorResults *aResults,
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t aTryCollections,
|
2010-11-12 01:52:30 +03:00
|
|
|
nsICycleCollectorListener *aListener)
|
|
|
|
{
|
2011-08-10 09:36:00 +04:00
|
|
|
nsAutoTArray<PtrInfo*, 4000> whiteNodes;
|
2010-11-12 01:52:30 +03:00
|
|
|
|
2012-02-24 08:16:37 +04:00
|
|
|
if (!PrepareForCollection(aResults, &whiteNodes))
|
|
|
|
return;
|
2010-11-12 01:52:30 +03:00
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t totalCollections = 0;
|
2010-11-12 01:52:30 +03:00
|
|
|
while (aTryCollections > totalCollections) {
|
2011-06-16 02:53:00 +04:00
|
|
|
// Synchronous cycle collection. Always force a JS GC beforehand.
|
2011-10-17 18:59:28 +04:00
|
|
|
GCIfNeeded(true);
|
2011-10-14 04:37:18 +04:00
|
|
|
if (aListener && NS_FAILED(aListener->Begin()))
|
2012-07-30 18:20:58 +04:00
|
|
|
aListener = nullptr;
|
2012-06-27 19:09:56 +04:00
|
|
|
if (!(BeginCollection(aMergeCompartments, aListener) &&
|
2011-06-22 21:41:17 +04:00
|
|
|
FinishCollection(aListener)))
|
2010-11-12 01:52:30 +03:00
|
|
|
break;
|
|
|
|
|
|
|
|
++totalCollections;
|
|
|
|
}
|
|
|
|
|
|
|
|
CleanupAfterCollection();
|
2007-11-02 01:51:57 +03:00
|
|
|
}
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool
|
2012-06-27 19:09:56 +04:00
|
|
|
nsCycleCollector::BeginCollection(bool aMergeCompartments,
|
|
|
|
nsICycleCollectorListener *aListener)
|
2007-11-02 01:51:57 +03:00
|
|
|
{
|
2011-10-14 04:37:18 +04:00
|
|
|
// aListener should be Begin()'d before this
|
2012-02-12 20:02:01 +04:00
|
|
|
TimeLog timeLog;
|
|
|
|
|
2007-11-02 01:51:57 +03:00
|
|
|
if (mParams.mDoNothing)
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2007-11-02 01:51:57 +03:00
|
|
|
|
2012-06-27 19:09:56 +04:00
|
|
|
GCGraphBuilder builder(mGraph, mJSRuntime, aListener, aMergeCompartments);
|
2010-02-28 13:23:59 +03:00
|
|
|
if (!builder.Initialized())
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2007-01-05 01:31:26 +03:00
|
|
|
|
2012-05-03 23:28:11 +04:00
|
|
|
if (mJSRuntime) {
|
|
|
|
mJSRuntime->BeginCycleCollection(builder);
|
|
|
|
timeLog.Checkpoint("mJSRuntime->BeginCycleCollection()");
|
2007-11-02 01:51:57 +03:00
|
|
|
}
|
2007-03-20 02:21:31 +03:00
|
|
|
|
2007-06-07 02:06:56 +04:00
|
|
|
#ifdef DEBUG_CC
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t purpleStart = builder.Count();
|
2007-06-07 02:06:56 +04:00
|
|
|
#endif
|
2011-10-17 18:59:28 +04:00
|
|
|
mScanInProgress = true;
|
2008-02-15 16:12:55 +03:00
|
|
|
SelectPurple(builder);
|
2007-06-07 02:06:56 +04:00
|
|
|
#ifdef DEBUG_CC
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t purpleEnd = builder.Count();
|
2008-02-15 16:12:55 +03:00
|
|
|
|
|
|
|
if (purpleStart != purpleEnd) {
|
|
|
|
if (mParams.mLogPointers && !mPtrLog)
|
|
|
|
mPtrLog = fopen("pointer_log", "w");
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t i = 0;
|
2008-02-15 16:12:55 +03:00
|
|
|
NodePool::Enumerator queue(mGraph.mNodes);
|
|
|
|
while (i++ < purpleStart) {
|
|
|
|
queue.GetNext();
|
|
|
|
}
|
|
|
|
while (i++ < purpleEnd) {
|
|
|
|
mStats.mForgetNode++;
|
|
|
|
if (mParams.mLogPointers)
|
|
|
|
fprintf(mPtrLog, "F %p\n", queue.GetNext()->mPointer);
|
|
|
|
}
|
|
|
|
}
|
2007-06-07 02:06:56 +04:00
|
|
|
#endif
|
2007-03-16 15:52:47 +03:00
|
|
|
|
2012-02-12 20:02:01 +04:00
|
|
|
timeLog.Checkpoint("SelectPurple()");
|
2007-03-20 02:21:31 +03:00
|
|
|
|
2008-02-15 16:12:55 +03:00
|
|
|
if (builder.Count() > 0) {
|
2007-11-02 01:51:57 +03:00
|
|
|
// The main Bacon & Rajan collection algorithm.
|
2007-03-16 15:52:47 +03:00
|
|
|
|
2008-01-10 17:10:03 +03:00
|
|
|
MarkRoots(builder);
|
2012-02-12 20:02:01 +04:00
|
|
|
timeLog.Checkpoint("MarkRoots()");
|
2007-03-20 02:21:31 +03:00
|
|
|
|
2008-01-10 17:10:03 +03:00
|
|
|
ScanRoots();
|
2012-02-12 20:02:01 +04:00
|
|
|
timeLog.Checkpoint("ScanRoots()");
|
2007-03-20 02:21:31 +03:00
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
mScanInProgress = false;
|
2007-03-16 15:52:47 +03:00
|
|
|
|
2010-08-12 04:03:23 +04:00
|
|
|
if (aListener) {
|
2011-06-22 21:41:17 +04:00
|
|
|
aListener->BeginResults();
|
2010-08-12 04:03:23 +04:00
|
|
|
|
|
|
|
NodePool::Enumerator etor(mGraph.mNodes);
|
|
|
|
while (!etor.IsDone()) {
|
|
|
|
PtrInfo *pi = etor.GetNext();
|
2011-06-22 21:41:17 +04:00
|
|
|
if (pi->mColor == black &&
|
2012-09-28 10:57:33 +04:00
|
|
|
pi->mRefCount > 0 && pi->mRefCount < UINT32_MAX &&
|
2011-06-22 21:41:17 +04:00
|
|
|
pi->mInternalRefs != pi->mRefCount) {
|
2012-08-22 19:56:38 +04:00
|
|
|
aListener->DescribeRoot((uint64_t)pi->mPointer,
|
2011-06-22 21:41:17 +04:00
|
|
|
pi->mInternalRefs);
|
2010-08-12 04:03:23 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-06-07 02:06:56 +04:00
|
|
|
#ifdef DEBUG_CC
|
2007-11-02 01:51:57 +03:00
|
|
|
if (mFollowupCollection && purpleStart != purpleEnd) {
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t i = 0;
|
2008-01-10 17:10:03 +03:00
|
|
|
NodePool::Enumerator queue(mGraph.mNodes);
|
2007-11-02 01:51:57 +03:00
|
|
|
while (i++ < purpleStart) {
|
|
|
|
queue.GetNext();
|
|
|
|
}
|
|
|
|
while (i++ < purpleEnd) {
|
|
|
|
PtrInfo *pi = queue.GetNext();
|
|
|
|
if (pi->mColor == white) {
|
|
|
|
printf("nsCycleCollector: a later shutdown collection collected the additional\n"
|
|
|
|
" suspect %p %s\n"
|
|
|
|
" (which could be fixed by improving traversal)\n",
|
|
|
|
pi->mPointer, pi->mName);
|
2007-06-07 02:06:56 +04:00
|
|
|
}
|
2007-11-02 01:51:57 +03:00
|
|
|
}
|
|
|
|
}
|
2007-06-07 02:06:56 +04:00
|
|
|
#endif
|
2007-03-20 02:21:31 +03:00
|
|
|
|
2012-05-03 23:28:11 +04:00
|
|
|
if (mJSRuntime) {
|
|
|
|
mJSRuntime->FinishTraverse();
|
|
|
|
timeLog.Checkpoint("mJSRuntime->FinishTraverse()");
|
2010-11-12 01:52:30 +03:00
|
|
|
}
|
2012-05-03 23:28:11 +04:00
|
|
|
} else {
|
2011-10-17 18:59:28 +04:00
|
|
|
mScanInProgress = false;
|
2008-02-15 16:12:55 +03:00
|
|
|
}
|
2007-03-16 15:52:47 +03:00
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
return true;
|
2008-01-10 17:10:03 +03:00
|
|
|
}
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool
|
2011-06-22 21:41:17 +04:00
|
|
|
nsCycleCollector::FinishCollection(nsICycleCollectorListener *aListener)
|
2008-01-10 17:10:03 +03:00
|
|
|
{
|
2012-02-12 20:02:01 +04:00
|
|
|
TimeLog timeLog;
|
2011-09-29 10:19:26 +04:00
|
|
|
bool collected = CollectWhite(aListener);
|
2012-02-12 20:02:01 +04:00
|
|
|
timeLog.Checkpoint("CollectWhite()");
|
2008-02-25 20:47:25 +03:00
|
|
|
|
2007-04-21 21:43:19 +04:00
|
|
|
#ifdef DEBUG_CC
|
2007-11-02 01:51:57 +03:00
|
|
|
mStats.mCollection++;
|
|
|
|
if (mParams.mReportStats)
|
|
|
|
mStats.Dump();
|
2007-04-21 21:43:19 +04:00
|
|
|
#endif
|
2007-01-05 01:31:26 +03:00
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
mFollowupCollection = true;
|
2007-06-08 14:17:21 +04:00
|
|
|
|
2010-11-12 01:52:30 +03:00
|
|
|
#ifdef DEBUG_CC
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t i, count = mWhiteNodes->Length();
|
2010-11-12 01:52:30 +03:00
|
|
|
for (i = 0; i < count; ++i) {
|
|
|
|
PtrInfo *pinfo = mWhiteNodes->ElementAt(i);
|
2012-05-03 23:28:11 +04:00
|
|
|
if (mPurpleBuf.Exists(pinfo->mPointer)) {
|
2010-11-12 01:52:30 +03:00
|
|
|
printf("nsCycleCollector: %s object @%p is still alive after\n"
|
|
|
|
" calling RootAndUnlinkJSObjects, Unlink, and Unroot on"
|
|
|
|
" it! This probably\n"
|
|
|
|
" means the Unlink implementation was insufficient.\n",
|
|
|
|
pinfo->mName, pinfo->mPointer);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
mWhiteNodes->Clear();
|
|
|
|
ClearGraph();
|
2012-02-12 20:02:01 +04:00
|
|
|
timeLog.Checkpoint("ClearGraph()");
|
2010-11-12 01:52:30 +03:00
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
mParams.mDoNothing = false;
|
2010-11-12 01:52:30 +03:00
|
|
|
|
2008-01-10 17:10:03 +03:00
|
|
|
return collected;
|
2007-01-05 01:31:26 +03:00
|
|
|
}
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t
|
2008-02-25 20:47:25 +03:00
|
|
|
nsCycleCollector::SuspectedCount()
|
|
|
|
{
|
|
|
|
return mPurpleBuf.Count();
|
|
|
|
}
|
|
|
|
|
2007-01-25 03:24:08 +03:00
|
|
|
void
|
|
|
|
nsCycleCollector::Shutdown()
|
|
|
|
{
|
2009-05-07 00:46:04 +04:00
|
|
|
// Here we want to run a final collection and then permanently
|
|
|
|
// disable the collector because the program is shutting down.
|
2007-01-25 03:24:08 +03:00
|
|
|
|
2011-08-17 21:35:40 +04:00
|
|
|
nsCOMPtr<nsCycleCollectorLogger> listener;
|
|
|
|
if (mParams.mLogGraphs) {
|
|
|
|
listener = new nsCycleCollectorLogger();
|
|
|
|
}
|
2012-07-30 18:20:58 +04:00
|
|
|
Collect(false, nullptr, SHUTDOWN_COLLECTIONS(mParams), listener);
|
2007-03-16 15:52:47 +03:00
|
|
|
|
2007-04-21 21:43:19 +04:00
|
|
|
#ifdef DEBUG_CC
|
2012-07-30 18:20:58 +04:00
|
|
|
GCGraphBuilder builder(mGraph, mJSRuntime, nullptr, false);
|
2011-10-17 18:59:28 +04:00
|
|
|
mScanInProgress = true;
|
2008-02-15 16:12:55 +03:00
|
|
|
SelectPurple(builder);
|
2011-10-17 18:59:28 +04:00
|
|
|
mScanInProgress = false;
|
2008-02-15 16:12:55 +03:00
|
|
|
if (builder.Count() != 0) {
|
2007-03-16 15:52:47 +03:00
|
|
|
printf("Might have been able to release more cycles if the cycle collector would "
|
|
|
|
"run once more at shutdown.\n");
|
|
|
|
}
|
2008-02-15 16:12:55 +03:00
|
|
|
ClearGraph();
|
2007-03-10 02:00:14 +03:00
|
|
|
#endif
|
2011-10-17 18:59:28 +04:00
|
|
|
mParams.mDoNothing = true;
|
2007-01-25 03:24:08 +03:00
|
|
|
}
|
|
|
|
|
2007-04-21 21:43:19 +04:00
|
|
|
#ifdef DEBUG_CC
|
2007-03-10 02:00:14 +03:00
|
|
|
void
|
|
|
|
nsCycleCollector::ShouldBeFreed(nsISupports *n)
|
|
|
|
{
|
2010-07-17 07:41:28 +04:00
|
|
|
if (n) {
|
|
|
|
mExpectedGarbage.PutEntry(n);
|
|
|
|
}
|
2007-03-10 02:00:14 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsCycleCollector::WasFreed(nsISupports *n)
|
|
|
|
{
|
2010-07-17 07:41:28 +04:00
|
|
|
if (n) {
|
|
|
|
mExpectedGarbage.RemoveEntry(n);
|
|
|
|
}
|
2007-03-10 02:00:14 +03:00
|
|
|
}
|
|
|
|
#endif
|
2007-01-05 01:31:26 +03:00
|
|
|
|
2011-07-09 02:49:31 +04:00
|
|
|
|
|
|
|
////////////////////////
|
|
|
|
// Memory reporter
|
|
|
|
////////////////////////
|
|
|
|
|
2012-11-07 05:38:29 +04:00
|
|
|
NS_MEMORY_REPORTER_MALLOC_SIZEOF_FUN(CycleCollectorMallocSizeOf,
|
|
|
|
"cycle-collector")
|
|
|
|
|
|
|
|
void
|
|
|
|
nsCycleCollector::SizeOfIncludingThis(nsMallocSizeOfFun aMallocSizeOf,
|
|
|
|
size_t *aObjectSize,
|
|
|
|
size_t *aGraphNodesSize,
|
|
|
|
size_t *aGraphEdgesSize,
|
|
|
|
size_t *aWhiteNodeSize,
|
|
|
|
size_t *aPurpleBufferSize) const
|
2011-07-09 02:49:31 +04:00
|
|
|
{
|
2012-11-07 05:38:29 +04:00
|
|
|
*aObjectSize = aMallocSizeOf(this);
|
|
|
|
|
|
|
|
mGraph.SizeOfExcludingThis(aMallocSizeOf, aGraphNodesSize, aGraphEdgesSize);
|
|
|
|
|
|
|
|
// No need to measure what the entries point to; the pointers are
|
|
|
|
// non-owning.
|
|
|
|
*aWhiteNodeSize = mWhiteNodes
|
|
|
|
? mWhiteNodes->SizeOfIncludingThis(aMallocSizeOf)
|
|
|
|
: 0;
|
|
|
|
|
|
|
|
*aPurpleBufferSize = mPurpleBuf.SizeOfExcludingThis(aMallocSizeOf);
|
|
|
|
|
|
|
|
// These fields are deliberately not measured:
|
|
|
|
// - mResults: because it's tiny and only contains scalars.
|
|
|
|
// - mJSRuntime: because it's non-owning and measured by JS reporters.
|
|
|
|
// - mParams: because it only contains scalars.
|
|
|
|
// - mStats, mPtrLog, mExpectedGarbage: because they're DEBUG_CC-only.
|
2011-07-09 02:49:31 +04:00
|
|
|
}
|
|
|
|
|
2012-11-07 05:38:29 +04:00
|
|
|
class CycleCollectorMultiReporter MOZ_FINAL : public nsIMemoryMultiReporter
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
|
|
|
|
NS_IMETHOD GetName(nsACString &name)
|
|
|
|
{
|
|
|
|
name.AssignLiteral("cycle-collector");
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHOD CollectReports(nsIMemoryMultiReporterCallback *aCb,
|
|
|
|
nsISupports *aClosure)
|
|
|
|
{
|
|
|
|
if (!sCollector)
|
|
|
|
return NS_OK;
|
|
|
|
|
|
|
|
size_t objectSize, graphNodesSize, graphEdgesSize, whiteNodesSize,
|
|
|
|
purpleBufferSize;
|
|
|
|
sCollector->SizeOfIncludingThis(CycleCollectorMallocSizeOf,
|
|
|
|
&objectSize, &graphNodesSize,
|
|
|
|
&graphEdgesSize, &whiteNodesSize,
|
|
|
|
&purpleBufferSize);
|
|
|
|
|
|
|
|
#define REPORT(_path, _amount, _desc) \
|
|
|
|
do { \
|
|
|
|
size_t amount = _amount; /* evaluate |_amount| just once */ \
|
|
|
|
if (amount > 0) { \
|
|
|
|
nsresult rv; \
|
|
|
|
rv = aCb->Callback(EmptyCString(), NS_LITERAL_CSTRING(_path), \
|
|
|
|
nsIMemoryReporter::KIND_HEAP, \
|
|
|
|
nsIMemoryReporter::UNITS_BYTES, _amount, \
|
|
|
|
NS_LITERAL_CSTRING(_desc), aClosure); \
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv); \
|
|
|
|
} \
|
|
|
|
} while (0)
|
|
|
|
|
|
|
|
REPORT("explicit/cycle-collector/collector-object", objectSize,
|
|
|
|
"Memory used for the cycle collector object itself.");
|
|
|
|
|
|
|
|
REPORT("explicit/cycle-collector/graph-nodes", graphNodesSize,
|
|
|
|
"Memory used for the nodes of the cycle collector's graph. "
|
|
|
|
"This should be zero when the collector is idle.");
|
|
|
|
|
|
|
|
REPORT("explicit/cycle-collector/graph-edges", graphEdgesSize,
|
|
|
|
"Memory used for the edges of the cycle collector's graph. "
|
|
|
|
"This should be zero when the collector is idle.");
|
|
|
|
|
|
|
|
REPORT("explicit/cycle-collector/white-nodes", whiteNodesSize,
|
|
|
|
"Memory used for the cycle collector's white nodes array. "
|
|
|
|
"This should be zero when the collector is idle.");
|
|
|
|
|
|
|
|
REPORT("explicit/cycle-collector/purple-buffer", purpleBufferSize,
|
|
|
|
"Memory used for the cycle collector's purple buffer.");
|
2011-07-09 02:49:31 +04:00
|
|
|
|
2012-11-07 05:38:29 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHOD GetExplicitNonHeap(int64_t *n)
|
|
|
|
{
|
|
|
|
// This reporter does neither "explicit" nor NONHEAP measurements.
|
|
|
|
*n = 0;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
NS_IMPL_ISUPPORTS1(CycleCollectorMultiReporter, nsIMemoryMultiReporter)
|
2011-07-09 02:49:31 +04:00
|
|
|
|
2007-01-05 01:31:26 +03:00
|
|
|
////////////////////////////////////////////////////////////////////////
|
|
|
|
// Module public API (exported in nsCycleCollector.h)
|
|
|
|
// Just functions that redirect into the singleton, once it's built.
|
|
|
|
////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2012-05-03 23:28:11 +04:00
|
|
|
void
|
|
|
|
nsCycleCollector_registerJSRuntime(nsCycleCollectionJSRuntime *rt)
|
2007-01-05 01:31:26 +03:00
|
|
|
{
|
2011-09-29 10:19:26 +04:00
|
|
|
static bool regMemReport = true;
|
2007-03-27 13:49:06 +04:00
|
|
|
if (sCollector)
|
2012-05-03 23:28:11 +04:00
|
|
|
sCollector->RegisterJSRuntime(rt);
|
2011-07-09 02:49:31 +04:00
|
|
|
if (regMemReport) {
|
2011-10-17 18:59:28 +04:00
|
|
|
regMemReport = false;
|
2012-11-07 05:38:29 +04:00
|
|
|
sCollectorReporter = new CycleCollectorMultiReporter;
|
|
|
|
NS_RegisterMemoryMultiReporter(sCollectorReporter);
|
2011-07-09 02:49:31 +04:00
|
|
|
}
|
2007-01-05 01:31:26 +03:00
|
|
|
}
|
|
|
|
|
2012-11-07 05:38:29 +04:00
|
|
|
void
|
2012-05-03 23:28:11 +04:00
|
|
|
nsCycleCollector_forgetJSRuntime()
|
2007-01-05 01:31:26 +03:00
|
|
|
{
|
2007-03-27 13:49:06 +04:00
|
|
|
if (sCollector)
|
2012-05-03 23:28:11 +04:00
|
|
|
sCollector->ForgetJSRuntime();
|
2012-11-07 05:38:29 +04:00
|
|
|
if (sCollectorReporter) {
|
|
|
|
NS_UnregisterMemoryMultiReporter(sCollectorReporter);
|
|
|
|
sCollectorReporter = nullptr;
|
|
|
|
}
|
2007-01-05 01:31:26 +03:00
|
|
|
}
|
|
|
|
|
2009-05-07 00:46:04 +04:00
|
|
|
nsPurpleBufferEntry*
|
2012-08-24 20:50:06 +04:00
|
|
|
NS_CycleCollectorSuspect2(void *n, nsCycleCollectionParticipant *cp)
|
2009-05-07 00:46:04 +04:00
|
|
|
{
|
|
|
|
if (sCollector)
|
2012-08-24 20:50:06 +04:00
|
|
|
return sCollector->Suspect2(n, cp);
|
2012-07-30 18:20:58 +04:00
|
|
|
return nullptr;
|
2009-05-07 00:46:04 +04:00
|
|
|
}
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool
|
2009-05-07 00:46:04 +04:00
|
|
|
NS_CycleCollectorForget2(nsPurpleBufferEntry *e)
|
|
|
|
{
|
2011-10-17 18:59:28 +04:00
|
|
|
return sCollector ? sCollector->Forget2(e) : true;
|
2009-05-07 00:46:04 +04:00
|
|
|
}
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t
|
2008-02-25 20:47:25 +03:00
|
|
|
nsCycleCollector_suspectedCount()
|
|
|
|
{
|
|
|
|
return sCollector ? sCollector->SuspectedCount() : 0;
|
2007-03-27 13:49:06 +04:00
|
|
|
}
|
|
|
|
|
2007-03-10 02:00:14 +03:00
|
|
|
#ifdef DEBUG
|
|
|
|
void
|
|
|
|
nsCycleCollector_DEBUG_shouldBeFreed(nsISupports *n)
|
|
|
|
{
|
2007-04-21 21:43:19 +04:00
|
|
|
#ifdef DEBUG_CC
|
2007-03-27 13:49:06 +04:00
|
|
|
if (sCollector)
|
|
|
|
sCollector->ShouldBeFreed(n);
|
2007-04-21 21:43:19 +04:00
|
|
|
#endif
|
2007-03-10 02:00:14 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsCycleCollector_DEBUG_wasFreed(nsISupports *n)
|
|
|
|
{
|
2007-04-21 21:43:19 +04:00
|
|
|
#ifdef DEBUG_CC
|
2007-03-27 13:49:06 +04:00
|
|
|
if (sCollector)
|
|
|
|
sCollector->WasFreed(n);
|
2007-04-21 21:43:19 +04:00
|
|
|
#endif
|
2007-03-10 02:00:14 +03:00
|
|
|
}
|
|
|
|
#endif
|
2010-11-12 01:52:30 +03:00
|
|
|
|
|
|
|
class nsCycleCollectorRunner : public nsRunnable
|
|
|
|
{
|
|
|
|
nsCycleCollector *mCollector;
|
2011-01-14 13:06:09 +03:00
|
|
|
nsICycleCollectorListener *mListener;
|
2010-11-12 01:52:30 +03:00
|
|
|
Mutex mLock;
|
|
|
|
CondVar mRequest;
|
|
|
|
CondVar mReply;
|
2011-09-29 10:19:26 +04:00
|
|
|
bool mRunning;
|
|
|
|
bool mShutdown;
|
|
|
|
bool mCollected;
|
2012-06-27 19:09:56 +04:00
|
|
|
bool mMergeCompartments;
|
2010-11-12 01:52:30 +03:00
|
|
|
|
|
|
|
public:
|
|
|
|
NS_IMETHOD Run()
|
|
|
|
{
|
2012-06-12 21:06:20 +04:00
|
|
|
PR_SetCurrentThreadName("XPCOM CC");
|
|
|
|
|
2010-11-12 01:52:30 +03:00
|
|
|
#ifdef XP_WIN
|
|
|
|
TlsSetValue(gTLSThreadIDIndex,
|
|
|
|
(void*) mozilla::threads::CycleCollector);
|
|
|
|
#elif defined(NS_TLS)
|
|
|
|
gTLSThreadID = mozilla::threads::CycleCollector;
|
|
|
|
#else
|
|
|
|
gCycleCollectorThread = PR_GetCurrentThread();
|
|
|
|
#endif
|
|
|
|
|
2012-11-15 11:32:39 +04:00
|
|
|
MOZ_ASSERT(NS_IsCycleCollectorThread() && !NS_IsMainThread(),
|
|
|
|
"Wrong thread!");
|
2010-11-12 01:52:30 +03:00
|
|
|
|
|
|
|
MutexAutoLock autoLock(mLock);
|
|
|
|
|
2011-02-12 07:49:00 +03:00
|
|
|
if (mShutdown)
|
|
|
|
return NS_OK;
|
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
mRunning = true;
|
2010-11-12 01:52:30 +03:00
|
|
|
|
|
|
|
while (1) {
|
|
|
|
mRequest.Wait();
|
|
|
|
|
|
|
|
if (!mRunning) {
|
|
|
|
mReply.Notify();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-05-03 23:28:10 +04:00
|
|
|
mCollector->mJSRuntime->NotifyEnterCycleCollectionThread();
|
2012-06-27 19:09:56 +04:00
|
|
|
mCollected = mCollector->BeginCollection(mMergeCompartments, mListener);
|
2012-05-03 23:28:10 +04:00
|
|
|
mCollector->mJSRuntime->NotifyLeaveCycleCollectionThread();
|
2010-11-12 01:52:30 +03:00
|
|
|
|
|
|
|
mReply.Notify();
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCycleCollectorRunner(nsCycleCollector *collector)
|
|
|
|
: mCollector(collector),
|
2012-07-30 18:20:58 +04:00
|
|
|
mListener(nullptr),
|
2010-11-12 01:52:30 +03:00
|
|
|
mLock("cycle collector lock"),
|
|
|
|
mRequest(mLock, "cycle collector request condvar"),
|
|
|
|
mReply(mLock, "cycle collector reply condvar"),
|
2011-10-17 18:59:28 +04:00
|
|
|
mRunning(false),
|
|
|
|
mShutdown(false),
|
2012-06-27 19:09:56 +04:00
|
|
|
mCollected(false),
|
|
|
|
mMergeCompartments(false)
|
2010-11-12 01:52:30 +03:00
|
|
|
{
|
2012-11-15 11:32:39 +04:00
|
|
|
MOZ_ASSERT(NS_IsMainThread(), "Wrong thread!");
|
2010-11-12 01:52:30 +03:00
|
|
|
}
|
|
|
|
|
2012-06-27 19:09:56 +04:00
|
|
|
void Collect(bool aMergeCompartments,
|
|
|
|
nsCycleCollectorResults *aResults,
|
2012-02-24 08:16:37 +04:00
|
|
|
nsICycleCollectorListener *aListener)
|
2010-11-12 01:52:30 +03:00
|
|
|
{
|
2012-11-15 11:32:39 +04:00
|
|
|
MOZ_ASSERT(NS_IsMainThread(), "Wrong thread!");
|
2010-11-12 01:52:30 +03:00
|
|
|
|
2012-01-30 04:45:08 +04:00
|
|
|
// On a WantAllTraces CC, force a synchronous global GC to prevent
|
|
|
|
// hijinks from ForgetSkippable and compartmental GCs.
|
|
|
|
bool wantAllTraces = false;
|
|
|
|
if (aListener) {
|
|
|
|
aListener->GetWantAllTraces(&wantAllTraces);
|
|
|
|
}
|
|
|
|
mCollector->GCIfNeeded(wantAllTraces);
|
2011-06-22 02:06:42 +04:00
|
|
|
|
2010-11-12 01:52:30 +03:00
|
|
|
MutexAutoLock autoLock(mLock);
|
|
|
|
|
2011-02-23 11:08:36 +03:00
|
|
|
if (!mRunning)
|
2012-02-24 08:16:37 +04:00
|
|
|
return;
|
2010-11-12 01:52:30 +03:00
|
|
|
|
2011-08-10 09:36:00 +04:00
|
|
|
nsAutoTArray<PtrInfo*, 4000> whiteNodes;
|
2012-02-24 08:16:37 +04:00
|
|
|
if (!mCollector->PrepareForCollection(aResults, &whiteNodes))
|
|
|
|
return;
|
2010-11-12 01:52:30 +03:00
|
|
|
|
2012-11-15 11:32:39 +04:00
|
|
|
MOZ_ASSERT(!mListener, "Should have cleared this already!");
|
2011-10-14 04:37:18 +04:00
|
|
|
if (aListener && NS_FAILED(aListener->Begin()))
|
2012-07-30 18:20:58 +04:00
|
|
|
aListener = nullptr;
|
2010-11-12 01:52:30 +03:00
|
|
|
mListener = aListener;
|
2012-06-27 19:09:56 +04:00
|
|
|
mMergeCompartments = aMergeCompartments;
|
2010-11-12 01:52:30 +03:00
|
|
|
|
2012-05-03 23:28:10 +04:00
|
|
|
if (mCollector->mJSRuntime->NotifyLeaveMainThread()) {
|
2011-07-19 01:54:48 +04:00
|
|
|
mRequest.Notify();
|
|
|
|
mReply.Wait();
|
2012-05-03 23:28:10 +04:00
|
|
|
mCollector->mJSRuntime->NotifyEnterMainThread();
|
2011-07-19 01:54:48 +04:00
|
|
|
} else {
|
2012-06-27 19:09:56 +04:00
|
|
|
mCollected = mCollector->BeginCollection(aMergeCompartments, mListener);
|
2011-07-19 01:54:48 +04:00
|
|
|
}
|
2010-11-12 01:52:30 +03:00
|
|
|
|
2012-07-30 18:20:58 +04:00
|
|
|
mListener = nullptr;
|
2010-11-12 01:52:30 +03:00
|
|
|
|
|
|
|
if (mCollected) {
|
2012-02-24 08:16:37 +04:00
|
|
|
mCollector->FinishCollection(aListener);
|
2010-11-12 01:52:30 +03:00
|
|
|
mCollector->CleanupAfterCollection();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Shutdown()
|
|
|
|
{
|
2012-11-15 11:32:39 +04:00
|
|
|
MOZ_ASSERT(NS_IsMainThread(), "Wrong thread!");
|
2010-11-12 01:52:30 +03:00
|
|
|
|
|
|
|
MutexAutoLock autoLock(mLock);
|
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
mShutdown = true;
|
2011-02-12 07:49:00 +03:00
|
|
|
|
2010-11-12 01:52:30 +03:00
|
|
|
if (!mRunning)
|
|
|
|
return;
|
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
mRunning = false;
|
2010-11-12 01:52:30 +03:00
|
|
|
mRequest.Notify();
|
|
|
|
mReply.Wait();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Holds a reference.
|
|
|
|
static nsCycleCollectorRunner* sCollectorRunner;
|
|
|
|
|
|
|
|
// Holds a reference.
|
|
|
|
static nsIThread* sCollectorThread;
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsCycleCollector_startup()
|
|
|
|
{
|
2012-11-15 11:32:39 +04:00
|
|
|
MOZ_ASSERT(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
MOZ_ASSERT(!sCollector, "Forgot to call nsCycleCollector_shutdown?");
|
2010-11-12 01:52:30 +03:00
|
|
|
|
|
|
|
sCollector = new nsCycleCollector();
|
|
|
|
|
|
|
|
nsRefPtr<nsCycleCollectorRunner> runner =
|
|
|
|
new nsCycleCollectorRunner(sCollector);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIThread> thread;
|
2011-02-23 11:08:36 +03:00
|
|
|
nsresult rv = NS_NewThread(getter_AddRefs(thread), runner);
|
2010-11-12 01:52:30 +03:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
runner.swap(sCollectorRunner);
|
|
|
|
thread.swap(sCollectorThread);
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2012-01-14 20:58:05 +04:00
|
|
|
void
|
|
|
|
nsCycleCollector_setBeforeUnlinkCallback(CC_BeforeUnlinkCallback aCB)
|
|
|
|
{
|
|
|
|
if (sCollector) {
|
|
|
|
sCollector->mBeforeUnlinkCB = aCB;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsCycleCollector_setForgetSkippableCallback(CC_ForgetSkippableCallback aCB)
|
|
|
|
{
|
|
|
|
if (sCollector) {
|
|
|
|
sCollector->mForgetSkippableCB = aCB;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2012-03-06 01:48:04 +04:00
|
|
|
nsCycleCollector_forgetSkippable(bool aRemoveChildlessNodes)
|
2012-01-14 20:58:05 +04:00
|
|
|
{
|
|
|
|
if (sCollector) {
|
2012-02-01 01:47:47 +04:00
|
|
|
SAMPLE_LABEL("CC", "nsCycleCollector_forgetSkippable");
|
2012-02-12 20:02:01 +04:00
|
|
|
TimeLog timeLog;
|
2012-03-06 01:48:04 +04:00
|
|
|
sCollector->ForgetSkippable(aRemoveChildlessNodes);
|
2012-02-12 20:02:01 +04:00
|
|
|
timeLog.Checkpoint("ForgetSkippable()");
|
2012-01-14 20:58:05 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-24 08:16:37 +04:00
|
|
|
void
|
2012-06-27 19:09:56 +04:00
|
|
|
nsCycleCollector_collect(bool aMergeCompartments,
|
|
|
|
nsCycleCollectorResults *aResults,
|
2012-02-24 08:16:37 +04:00
|
|
|
nsICycleCollectorListener *aListener)
|
2010-11-12 01:52:30 +03:00
|
|
|
{
|
2012-11-15 11:32:39 +04:00
|
|
|
MOZ_ASSERT(NS_IsMainThread(), "Wrong thread!");
|
2012-02-01 01:47:47 +04:00
|
|
|
SAMPLE_LABEL("CC", "nsCycleCollector_collect");
|
2011-01-14 13:06:09 +03:00
|
|
|
nsCOMPtr<nsICycleCollectorListener> listener(aListener);
|
2011-08-17 21:35:40 +04:00
|
|
|
if (!aListener && sCollector && sCollector->mParams.mLogGraphs) {
|
2011-01-14 13:06:09 +03:00
|
|
|
listener = new nsCycleCollectorLogger();
|
|
|
|
}
|
|
|
|
|
2012-02-24 08:16:37 +04:00
|
|
|
if (sCollectorRunner) {
|
2012-06-27 19:09:56 +04:00
|
|
|
sCollectorRunner->Collect(aMergeCompartments, aResults, listener);
|
2012-02-24 08:16:37 +04:00
|
|
|
} else if (sCollector) {
|
2012-06-27 19:09:56 +04:00
|
|
|
sCollector->Collect(aMergeCompartments, aResults, 1, listener);
|
2012-02-24 08:16:37 +04:00
|
|
|
}
|
2010-11-12 01:52:30 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsCycleCollector_shutdownThreads()
|
|
|
|
{
|
2012-11-15 11:32:39 +04:00
|
|
|
MOZ_ASSERT(NS_IsMainThread(), "Wrong thread!");
|
2010-11-12 01:52:30 +03:00
|
|
|
if (sCollectorRunner) {
|
|
|
|
nsRefPtr<nsCycleCollectorRunner> runner;
|
|
|
|
runner.swap(sCollectorRunner);
|
|
|
|
runner->Shutdown();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sCollectorThread) {
|
|
|
|
nsCOMPtr<nsIThread> thread;
|
|
|
|
thread.swap(sCollectorThread);
|
|
|
|
thread->Shutdown();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsCycleCollector_shutdown()
|
|
|
|
{
|
2012-11-15 11:32:39 +04:00
|
|
|
MOZ_ASSERT(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
MOZ_ASSERT(!sCollectorRunner, "Should have finished before!");
|
|
|
|
MOZ_ASSERT(!sCollectorThread, "Should have finished before!");
|
2010-11-12 01:52:30 +03:00
|
|
|
|
|
|
|
if (sCollector) {
|
2012-02-01 01:47:47 +04:00
|
|
|
SAMPLE_LABEL("CC", "nsCycleCollector_shutdown");
|
2010-11-12 01:52:30 +03:00
|
|
|
sCollector->Shutdown();
|
|
|
|
delete sCollector;
|
2012-07-30 18:20:58 +04:00
|
|
|
sCollector = nullptr;
|
2010-11-12 01:52:30 +03:00
|
|
|
}
|
|
|
|
}
|