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: */
|
2007-01-05 01:31:26 +03:00
|
|
|
/* ***** BEGIN LICENSE BLOCK *****
|
|
|
|
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
|
|
|
*
|
|
|
|
* The contents of this file are subject to the Mozilla Public License Version
|
|
|
|
* 1.1 (the "License"); you may not use this file except in compliance with
|
|
|
|
* the License. You may obtain a copy of the License at
|
|
|
|
* http://www.mozilla.org/MPL/
|
|
|
|
*
|
|
|
|
* Software distributed under the License is distributed on an "AS IS" basis,
|
|
|
|
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
|
|
|
* for the specific language governing rights and limitations under the
|
|
|
|
* License.
|
|
|
|
*
|
|
|
|
* The Original Code is mozilla.org code.
|
|
|
|
*
|
2007-02-14 20:33:38 +03:00
|
|
|
* The Initial Developer of the Original Code is
|
|
|
|
* The Mozilla Foundation.
|
|
|
|
* Portions created by the Initial Developer are Copyright (C) 2006
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
|
|
|
*
|
2007-01-05 01:31:26 +03:00
|
|
|
* Contributor(s):
|
2007-04-26 01:12:11 +04:00
|
|
|
* L. David Baron <dbaron@dbaron.org>, Mozilla Corporation
|
2007-01-05 01:31:26 +03:00
|
|
|
*
|
|
|
|
* Alternatively, the contents of this file may be used under the terms of
|
|
|
|
* either of the GNU General Public License Version 2 or later (the "GPL"),
|
|
|
|
* or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
|
|
|
* in which case the provisions of the GPL or the LGPL are applicable instead
|
|
|
|
* of those above. If you wish to allow use of your version of this file only
|
|
|
|
* under the terms of either the GPL or the LGPL, and not to allow others to
|
|
|
|
* use your version of this file under the terms of the MPL, indicate your
|
|
|
|
* decision by deleting the provisions above and replace them with the notice
|
|
|
|
* and other provisions required by the GPL or the LGPL. If you do not delete
|
|
|
|
* the provisions above, a recipient may use your version of this file under
|
|
|
|
* the terms of any one of the MPL, the GPL or the LGPL.
|
|
|
|
*
|
|
|
|
* ***** END LICENSE BLOCK ***** */
|
|
|
|
|
|
|
|
//
|
|
|
|
// This file implements a garbage-cycle collector based on the paper
|
|
|
|
//
|
|
|
|
// 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"
|
|
|
|
#include "prtime.h"
|
2007-03-06 00:07:51 +03:00
|
|
|
#include "nsPrintfCString.h"
|
2007-03-27 13:49:06 +04:00
|
|
|
#include "nsTArray.h"
|
2010-04-30 23:40:59 +04:00
|
|
|
#include "mozilla/FunctionTimer.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"
|
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"
|
2011-03-29 00:05:48 +04:00
|
|
|
#include "xpcpublic.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
|
|
|
|
|
2010-11-12 01:52:30 +03:00
|
|
|
#ifdef XP_WIN
|
|
|
|
#include <windows.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "mozilla/Mutex.h"
|
|
|
|
#include "mozilla/CondVar.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-07-09 05:10:29 +04:00
|
|
|
#ifdef DEBUG_CC
|
|
|
|
#define IF_DEBUG_CC_PARAM(_p) , _p
|
|
|
|
#define IF_DEBUG_CC_ONLY_PARAM(_p) _p
|
|
|
|
#else
|
|
|
|
#define IF_DEBUG_CC_PARAM(_p)
|
|
|
|
#define IF_DEBUG_CC_ONLY_PARAM(_p)
|
|
|
|
#endif
|
|
|
|
|
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
|
|
|
|
PRThread* gCycleCollectorThread = nsnull;
|
|
|
|
#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
|
|
|
|
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 mHookMalloc;
|
|
|
|
bool mFaultIsFatal;
|
|
|
|
bool mLogPointers;
|
2007-04-21 21:43:19 +04:00
|
|
|
PRUint32 mShutdownCollections;
|
|
|
|
#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),
|
|
|
|
mHookMalloc (PR_GetEnv("XPCOM_CC_HOOK_MALLOC") != NULL),
|
|
|
|
mFaultIsFatal (PR_GetEnv("XPCOM_CC_FAULT_IS_FATAL") != 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
|
|
|
|
{
|
|
|
|
PRUint32 mFailedQI;
|
|
|
|
PRUint32 mSuccessfulQI;
|
|
|
|
|
|
|
|
PRUint32 mVisitedNode;
|
|
|
|
PRUint32 mWalkedGraph;
|
|
|
|
PRUint32 mCollectedBytes;
|
|
|
|
PRUint32 mFreeCalls;
|
|
|
|
PRUint32 mFreedBytes;
|
|
|
|
|
|
|
|
PRUint32 mSetColorGrey;
|
|
|
|
PRUint32 mSetColorBlack;
|
|
|
|
PRUint32 mSetColorWhite;
|
|
|
|
|
|
|
|
PRUint32 mFailedUnlink;
|
|
|
|
PRUint32 mCollectedNode;
|
|
|
|
|
|
|
|
PRUint32 mSuspectNode;
|
|
|
|
PRUint32 mForgetNode;
|
|
|
|
PRUint32 mFreedWhilePurple;
|
|
|
|
|
|
|
|
PRUint32 mCollection;
|
|
|
|
|
|
|
|
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(mCollectedBytes);
|
|
|
|
DUMP(mFreeCalls);
|
|
|
|
DUMP(mFreedBytes);
|
|
|
|
|
|
|
|
DUMP(mSetColorGrey);
|
|
|
|
DUMP(mSetColorBlack);
|
|
|
|
DUMP(mSetColorWhite);
|
|
|
|
|
|
|
|
DUMP(mFailedUnlink);
|
|
|
|
DUMP(mCollectedNode);
|
|
|
|
|
|
|
|
DUMP(mSuspectNode);
|
|
|
|
DUMP(mForgetNode);
|
|
|
|
DUMP(mFreedWhilePurple);
|
|
|
|
|
|
|
|
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);
|
2008-01-05 06:57:09 +03:00
|
|
|
static void InitMemHook(void);
|
2007-04-21 21:43:19 +04:00
|
|
|
#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()
|
|
|
|
{
|
|
|
|
mSentinelAndBlocks[0].block = nsnull;
|
|
|
|
mSentinelAndBlocks[1].block = nsnull;
|
2011-07-09 02:49:31 +04:00
|
|
|
mNumBlocks = 0;
|
2007-04-26 01:12:11 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
~EdgePool()
|
2008-01-10 17:10:03 +03:00
|
|
|
{
|
|
|
|
NS_ASSERTION(!mSentinelAndBlocks[0].block &&
|
|
|
|
!mSentinelAndBlocks[1].block,
|
|
|
|
"Didn't call Clear()?");
|
|
|
|
}
|
|
|
|
|
|
|
|
void Clear()
|
2007-04-26 01:12:11 +04:00
|
|
|
{
|
|
|
|
Block *b = Blocks();
|
|
|
|
while (b) {
|
|
|
|
Block *next = b->Next();
|
|
|
|
delete b;
|
2011-07-09 02:49:31 +04:00
|
|
|
NS_ASSERTION(mNumBlocks > 0,
|
|
|
|
"Expected EdgePool mNumBlocks to be positive.");
|
|
|
|
mNumBlocks--;
|
2007-04-26 01:12:11 +04:00
|
|
|
b = next;
|
|
|
|
}
|
2008-01-10 17:10:03 +03:00
|
|
|
|
|
|
|
mSentinelAndBlocks[0].block = nsnull;
|
|
|
|
mSentinelAndBlocks[1].block = nsnull;
|
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() {
|
|
|
|
mPointers[BlockSize - 2].block = nsnull; // sentinel
|
|
|
|
mPointers[BlockSize - 1].block = nsnull; // next block pointer
|
|
|
|
}
|
|
|
|
Block*& Next()
|
|
|
|
{ return mPointers[BlockSize - 1].block; }
|
|
|
|
PtrInfoOrBlock* Start()
|
|
|
|
{ return &mPointers[0]; }
|
|
|
|
PtrInfoOrBlock* End()
|
|
|
|
{ return &mPointers[BlockSize - 2]; }
|
|
|
|
};
|
|
|
|
|
|
|
|
// Store the null sentinel so that we can have valid iterators
|
|
|
|
// before adding any edges and without adding any blocks.
|
|
|
|
PtrInfoOrBlock mSentinelAndBlocks[2];
|
2011-07-09 02:49:31 +04:00
|
|
|
PRUint32 mNumBlocks;
|
2007-04-26 01:12:11 +04:00
|
|
|
|
|
|
|
Block*& Blocks() { return mSentinelAndBlocks[1].block; }
|
|
|
|
|
|
|
|
public:
|
|
|
|
class Iterator
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
Iterator() : mPointer(nsnull) {}
|
|
|
|
Iterator(PtrInfoOrBlock *aPointer) : mPointer(aPointer) {}
|
|
|
|
Iterator(const Iterator& aOther) : mPointer(aOther.mPointer) {}
|
|
|
|
|
|
|
|
Iterator& operator++()
|
|
|
|
{
|
|
|
|
if (mPointer->ptrInfo == nsnull) {
|
|
|
|
// Null pointer is a sentinel for link to the next block.
|
|
|
|
mPointer = (mPointer + 1)->block->mPointers;
|
|
|
|
}
|
|
|
|
++mPointer;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
PtrInfo* operator*() const
|
|
|
|
{
|
|
|
|
if (mPointer->ptrInfo == nsnull) {
|
|
|
|
// 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]),
|
2011-07-09 02:49:31 +04:00
|
|
|
mNextBlockPtr(&aPool.Blocks()),
|
|
|
|
mNumBlocks(aPool.mNumBlocks)
|
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();
|
2011-07-09 02:49:31 +04:00
|
|
|
mNumBlocks++;
|
2007-04-26 01:12:11 +04:00
|
|
|
}
|
|
|
|
(mCurrent++)->ptrInfo = aEdge;
|
|
|
|
}
|
|
|
|
private:
|
|
|
|
// mBlockEnd points to space for null sentinel
|
|
|
|
PtrInfoOrBlock *mCurrent, *mBlockEnd;
|
2007-05-04 03:07:26 +04:00
|
|
|
Block **mNextBlockPtr;
|
2011-07-09 02:49:31 +04:00
|
|
|
PRUint32 &mNumBlocks;
|
2007-04-26 01:12:11 +04:00
|
|
|
};
|
|
|
|
|
2011-07-09 02:49:31 +04:00
|
|
|
size_t BlocksSize() const {
|
|
|
|
return sizeof(Block) * mNumBlocks;
|
|
|
|
}
|
|
|
|
|
2007-04-26 01:12:11 +04:00
|
|
|
};
|
|
|
|
|
2007-07-06 02:20:16 +04:00
|
|
|
#ifdef DEBUG_CC
|
|
|
|
|
|
|
|
struct ReversedEdge {
|
|
|
|
PtrInfo *mTarget;
|
2008-03-18 02:11:08 +03:00
|
|
|
nsCString *mEdgeName;
|
2007-07-06 02:20:16 +04:00
|
|
|
ReversedEdge *mNext;
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
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;
|
2007-04-20 12:01:01 +04:00
|
|
|
PRUint32 mColor : 2;
|
2007-04-24 03:34:33 +04:00
|
|
|
PRUint32 mInternalRefs : 30;
|
2007-11-02 01:51:57 +03:00
|
|
|
PRUint32 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
|
2007-01-05 01:31:26 +03:00
|
|
|
size_t mBytes;
|
2010-08-18 11:46:54 +04:00
|
|
|
char *mName;
|
2007-08-23 04:46:43 +04:00
|
|
|
PRUint32 mLangID;
|
2007-07-06 02:20:16 +04:00
|
|
|
|
|
|
|
// For finding roots in ExplainLiveExpectedGarbage (when there are
|
|
|
|
// missing calls to suspect or failures to unlink).
|
2007-06-26 00:36:24 +04:00
|
|
|
PRUint32 mSCCIndex; // strongly connected component
|
2007-07-06 02:20:16 +04:00
|
|
|
|
|
|
|
// For finding roots in ExplainLiveExpectedGarbage (when nodes
|
|
|
|
// expected to be garbage are black).
|
|
|
|
ReversedEdge* mReversedEdges; // linked list
|
2007-08-23 04:47:47 +04:00
|
|
|
PtrInfo* mShortestPathToExpectedGarbage;
|
2008-03-18 02:11:08 +03:00
|
|
|
nsCString* mShortestPathToExpectedGarbageEdgeName;
|
2010-08-18 11:46:54 +04:00
|
|
|
|
|
|
|
nsTArray<nsCString> mEdgeNames;
|
2007-04-20 12:01:01 +04:00
|
|
|
#endif
|
2007-01-05 01:31:26 +03:00
|
|
|
|
2007-08-23 04:46:43 +04:00
|
|
|
PtrInfo(void *aPointer, nsCycleCollectionParticipant *aParticipant
|
|
|
|
IF_DEBUG_CC_PARAM(PRUint32 aLangID)
|
|
|
|
)
|
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
|
2007-04-26 01:12:11 +04:00
|
|
|
, mBytes(0),
|
2010-08-18 11:46:54 +04:00
|
|
|
mName(nsnull),
|
2007-08-23 04:46:43 +04:00
|
|
|
mLangID(aLangID),
|
2007-07-06 02:20:16 +04:00
|
|
|
mSCCIndex(0),
|
2007-08-23 04:47:47 +04:00
|
|
|
mReversedEdges(nsnull),
|
2008-03-18 02:11:08 +03:00
|
|
|
mShortestPathToExpectedGarbage(nsnull),
|
|
|
|
mShortestPathToExpectedGarbageEdgeName(nsnull)
|
2007-04-20 12:01:01 +04:00
|
|
|
#endif
|
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);
|
2009-01-22 07:15:34 +03:00
|
|
|
mEdgeNames.~nsTArray<nsCString>();
|
2008-03-18 02:11:08 +03:00
|
|
|
}
|
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.
|
|
|
|
Block() { NS_NOTREACHED("should never be called"); }
|
|
|
|
~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()
|
|
|
|
: mBlocks(nsnull),
|
2011-07-09 02:49:31 +04:00
|
|
|
mLast(nsnull),
|
|
|
|
mNumBlocks(0)
|
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
|
|
|
{
|
|
|
|
NS_ASSERTION(!mBlocks, "Didn't call Clear()?");
|
|
|
|
}
|
|
|
|
|
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);
|
2011-07-09 02:49:31 +04:00
|
|
|
NS_ASSERTION(mNumBlocks > 0,
|
|
|
|
"Expected NodePool mNumBlocks to be positive.");
|
|
|
|
mNumBlocks--;
|
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
|
|
|
|
|
|
|
mBlocks = nsnull;
|
|
|
|
mLast = nsnull;
|
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),
|
2011-07-09 02:49:31 +04:00
|
|
|
mBlockEnd(nsnull),
|
|
|
|
mNumBlocks(aPool.mNumBlocks)
|
2007-04-26 01:12:11 +04:00
|
|
|
{
|
|
|
|
NS_ASSERTION(aPool.mBlocks == nsnull && aPool.mLast == nsnull,
|
|
|
|
"pool not empty");
|
|
|
|
}
|
2007-08-23 04:46:43 +04:00
|
|
|
PtrInfo *Add(void *aPointer, nsCycleCollectionParticipant *aParticipant
|
|
|
|
IF_DEBUG_CC_PARAM(PRUint32 aLangID)
|
|
|
|
)
|
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)))))
|
2007-04-26 01:12:11 +04:00
|
|
|
return nsnull;
|
|
|
|
mNext = block->mEntries;
|
|
|
|
mBlockEnd = block->mEntries + BlockSize;
|
2008-03-18 02:11:08 +03:00
|
|
|
block->mNext = nsnull;
|
2007-04-26 01:12:11 +04:00
|
|
|
mNextBlock = &block->mNext;
|
2011-07-09 02:49:31 +04:00
|
|
|
mNumBlocks++;
|
2007-04-26 01:12:11 +04:00
|
|
|
}
|
2007-08-23 04:46:43 +04:00
|
|
|
return new (mNext++) PtrInfo(aPointer, aParticipant
|
|
|
|
IF_DEBUG_CC_PARAM(aLangID)
|
|
|
|
);
|
2007-04-26 01:12:11 +04:00
|
|
|
}
|
|
|
|
private:
|
|
|
|
Block **mNextBlock;
|
|
|
|
PtrInfo *&mNext;
|
|
|
|
PtrInfo *mBlockEnd;
|
2011-07-09 02:49:31 +04:00
|
|
|
PRUint32 &mNumBlocks;
|
2007-04-26 01:12:11 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
class Enumerator;
|
|
|
|
friend class Enumerator;
|
|
|
|
class Enumerator {
|
|
|
|
public:
|
|
|
|
Enumerator(NodePool& aPool)
|
|
|
|
: mFirstBlock(aPool.mBlocks),
|
|
|
|
mCurBlock(nsnull),
|
|
|
|
mNext(nsnull),
|
|
|
|
mBlockEnd(nsnull),
|
|
|
|
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()
|
|
|
|
{
|
|
|
|
NS_ASSERTION(!IsDone(), "calling GetNext when done");
|
|
|
|
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
|
|
|
|
2011-07-09 02:49:31 +04:00
|
|
|
size_t BlocksSize() const {
|
|
|
|
return sizeof(Block) * mNumBlocks;
|
|
|
|
}
|
|
|
|
|
2007-04-26 01:12:11 +04:00
|
|
|
private:
|
|
|
|
Block *mBlocks;
|
|
|
|
PtrInfo *mLast;
|
2011-07-09 02:49:31 +04:00
|
|
|
PRUint32 mNumBlocks;
|
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;
|
|
|
|
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;
|
2007-04-26 01:12:11 +04:00
|
|
|
PRUint32 mRootCount;
|
2007-07-06 02:20:16 +04:00
|
|
|
#ifdef DEBUG_CC
|
|
|
|
ReversedEdge *mReversedEdges;
|
|
|
|
#endif
|
2007-04-26 01:12:11 +04:00
|
|
|
|
2007-06-19 05:29:10 +04:00
|
|
|
GCGraph() : mRootCount(0) {
|
|
|
|
}
|
|
|
|
~GCGraph() {
|
|
|
|
}
|
2011-07-09 02:49:31 +04:00
|
|
|
|
|
|
|
size_t BlocksSize() const {
|
|
|
|
return mNodes.BlocksSize() + mEdges.BlocksSize();
|
|
|
|
}
|
|
|
|
|
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.
|
2007-04-20 12:01:01 +04:00
|
|
|
typedef nsTHashtable<nsVoidPtrHashKey> PointerSet;
|
2007-01-05 01:31:26 +03:00
|
|
|
|
2009-06-01 23:22:18 +04:00
|
|
|
static inline void
|
|
|
|
ToParticipant(nsISupports *s, nsXPCOMCycleCollectionParticipant **cp);
|
|
|
|
|
2007-01-05 01:31:26 +03:00
|
|
|
struct nsPurpleBuffer
|
|
|
|
{
|
2009-05-07 00:46:04 +04:00
|
|
|
private:
|
|
|
|
struct Block {
|
|
|
|
Block *mNext;
|
2011-11-14 20:10:06 +04:00
|
|
|
nsPurpleBufferEntry mEntries[255];
|
2007-01-05 01:31:26 +03:00
|
|
|
|
2009-05-07 00:46:04 +04:00
|
|
|
Block() : mNext(nsnull) {}
|
|
|
|
};
|
|
|
|
public:
|
|
|
|
// This class wraps a linked list of the elements in the purple
|
|
|
|
// buffer.
|
2007-01-05 01:31:26 +03:00
|
|
|
|
|
|
|
nsCycleCollectorParams &mParams;
|
2011-07-09 02:49:31 +04:00
|
|
|
PRUint32 mNumBlocksAlloced;
|
2009-05-07 00:46:04 +04:00
|
|
|
PRUint32 mCount;
|
|
|
|
Block mFirstBlock;
|
|
|
|
nsPurpleBufferEntry *mFreeList;
|
|
|
|
|
|
|
|
// For objects compiled against Gecko 1.9 and 1.9.1.
|
|
|
|
PointerSet mCompatObjects;
|
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
|
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
|
|
|
nsPurpleBuffer(nsCycleCollectorParams ¶ms,
|
|
|
|
nsCycleCollectorStats &stats)
|
|
|
|
: 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();
|
|
|
|
mCompatObjects.Init();
|
2007-01-05 01:31:26 +03:00
|
|
|
}
|
2007-04-21 21:43:19 +04:00
|
|
|
#else
|
|
|
|
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();
|
|
|
|
mCompatObjects.Init();
|
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
|
|
|
{
|
2011-07-09 02:49:31 +04:00
|
|
|
mNumBlocksAlloced = 0;
|
2009-05-07 00:46:04 +04:00
|
|
|
mCount = 0;
|
|
|
|
mFreeList = nsnull;
|
|
|
|
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;
|
2011-10-11 09:50:08 +04:00
|
|
|
for (PRUint32 i = 1; i < ArrayLength(aBlock->mEntries); ++i) {
|
2009-05-07 00:46:04 +04:00
|
|
|
entries[i - 1].mNextInFreeList =
|
|
|
|
(nsPurpleBufferEntry*)(PRUword(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);
|
2009-05-07 00:46:04 +04:00
|
|
|
Block *b = mFirstBlock.mNext;
|
|
|
|
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;
|
2011-07-09 02:49:31 +04:00
|
|
|
NS_ASSERTION(mNumBlocksAlloced > 0,
|
|
|
|
"Expected positive mNumBlocksAlloced.");
|
|
|
|
mNumBlocksAlloced--;
|
2007-01-05 01:31:26 +03:00
|
|
|
}
|
2009-05-07 00:46:04 +04:00
|
|
|
mFirstBlock.mNext = nsnull;
|
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) {
|
|
|
|
if (!(PRUword(e->mObject) & PRUword(1))) {
|
|
|
|
// This is a real entry (rather than something on the
|
|
|
|
// free list).
|
2009-10-28 20:28:57 +03:00
|
|
|
if (e->mObject) {
|
|
|
|
nsXPCOMCycleCollectionParticipant *cp;
|
|
|
|
ToParticipant(e->mObject, &cp);
|
2009-06-01 23:22:18 +04:00
|
|
|
|
2009-10-28 20:28:57 +03:00
|
|
|
cp->UnmarkPurple(e->mObject);
|
|
|
|
}
|
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
|
|
|
|
2009-05-07 00:46:04 +04:00
|
|
|
#ifdef DEBUG_CC
|
|
|
|
void NoteAll(GCGraphBuilder &builder);
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool Exists(void *p) const
|
2007-01-05 01:31:26 +03:00
|
|
|
{
|
2009-05-07 00:46:04 +04:00
|
|
|
return mNormalObjects.GetEntry(p) || mCompatObjects.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) {
|
|
|
|
return nsnull;
|
2007-01-05 01:31:26 +03:00
|
|
|
}
|
2011-07-09 02:49:31 +04:00
|
|
|
mNumBlocksAlloced++;
|
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*)
|
|
|
|
(PRUword(mFreeList->mNextInFreeList) & ~PRUword(1));
|
|
|
|
return e;
|
2007-01-05 01:31:26 +03:00
|
|
|
}
|
2008-02-25 20:47:25 +03:00
|
|
|
|
2009-05-07 00:46:04 +04:00
|
|
|
nsPurpleBufferEntry* Put(nsISupports *p)
|
2008-02-25 20:47:25 +03:00
|
|
|
{
|
2009-05-07 00:46:04 +04:00
|
|
|
nsPurpleBufferEntry *e = NewEntry();
|
|
|
|
if (!e) {
|
|
|
|
return nsnull;
|
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;
|
|
|
|
|
2007-04-21 21:43:19 +04:00
|
|
|
#ifdef DEBUG_CC
|
2009-05-07 00:46:04 +04:00
|
|
|
mNormalObjects.PutEntry(p);
|
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
|
|
|
// 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)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(mCount != 0, "must have entries");
|
|
|
|
|
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 =
|
|
|
|
(nsPurpleBufferEntry*)(PRUword(mFreeList) | PRUword(1));
|
|
|
|
mFreeList = e;
|
|
|
|
|
|
|
|
--mCount;
|
|
|
|
}
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool PutCompatObject(nsISupports *p)
|
2009-05-07 00:46:04 +04:00
|
|
|
{
|
|
|
|
++mCount;
|
|
|
|
return !!mCompatObjects.PutEntry(p);
|
|
|
|
}
|
|
|
|
|
|
|
|
void RemoveCompatObject(nsISupports *p)
|
|
|
|
{
|
|
|
|
--mCount;
|
|
|
|
mCompatObjects.RemoveEntry(p);
|
|
|
|
}
|
|
|
|
|
|
|
|
PRUint32 Count() const
|
|
|
|
{
|
|
|
|
return mCount;
|
|
|
|
}
|
2011-07-09 02:49:31 +04:00
|
|
|
|
|
|
|
size_t BlocksSize() const
|
|
|
|
{
|
|
|
|
return sizeof(Block) * mNumBlocksAlloced;
|
|
|
|
}
|
|
|
|
|
2009-05-07 00:46:04 +04:00
|
|
|
};
|
2007-01-05 01:31:26 +03:00
|
|
|
|
2008-02-15 16:12:55 +03:00
|
|
|
struct CallbackClosure
|
|
|
|
{
|
|
|
|
CallbackClosure(nsPurpleBuffer *aPurpleBuffer, GCGraphBuilder &aBuilder)
|
|
|
|
: mPurpleBuffer(aPurpleBuffer),
|
|
|
|
mBuilder(aBuilder)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
nsPurpleBuffer *mPurpleBuffer;
|
|
|
|
GCGraphBuilder &mBuilder;
|
|
|
|
};
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
static bool
|
2008-02-15 16:12:55 +03:00
|
|
|
AddPurpleRoot(GCGraphBuilder &builder, nsISupports *root);
|
|
|
|
|
2008-10-10 19:04:34 +04:00
|
|
|
static PLDHashOperator
|
2009-05-07 00:46:04 +04:00
|
|
|
selectionCallback(nsVoidPtrHashKey* key, void* userArg)
|
2007-01-05 01:31:26 +03:00
|
|
|
{
|
2008-02-15 16:12:55 +03:00
|
|
|
CallbackClosure *closure = static_cast<CallbackClosure*>(userArg);
|
2009-05-07 00:46:04 +04:00
|
|
|
if (AddPurpleRoot(closure->mBuilder,
|
|
|
|
static_cast<nsISupports *>(
|
|
|
|
const_cast<void*>(key->GetKey()))))
|
2008-02-15 16:12:55 +03:00
|
|
|
return PL_DHASH_REMOVE;
|
|
|
|
|
2007-01-05 01:31:26 +03:00
|
|
|
return PL_DHASH_NEXT;
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
NS_ABORT_IF_FALSE(mCompatObjects.Count() + mNormalObjects.Count() ==
|
|
|
|
mCount,
|
|
|
|
"count out of sync");
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (mCompatObjects.Count()) {
|
|
|
|
mCount -= mCompatObjects.Count();
|
|
|
|
CallbackClosure closure(this, aBuilder);
|
|
|
|
mCompatObjects.EnumerateEntries(selectionCallback, &closure);
|
|
|
|
mCount += mCompatObjects.Count(); // in case of allocation failure
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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) {
|
|
|
|
if (!(PRUword(e->mObject) & PRUword(1))) {
|
|
|
|
// This is a real entry (rather than something on the
|
|
|
|
// free list).
|
2009-10-28 20:28:57 +03:00
|
|
|
if (!e->mObject || AddPurpleRoot(aBuilder, e->mObject)) {
|
2009-05-07 00:46:04 +04:00
|
|
|
#ifdef DEBUG_CC
|
|
|
|
mNormalObjects.RemoveEntry(e->mObject);
|
|
|
|
#endif
|
|
|
|
--mCount;
|
|
|
|
// Put this entry on the free list in case some
|
|
|
|
// call to AddPurpleRoot fails and we don't rebuild
|
|
|
|
// the free list below.
|
|
|
|
e->mNextInFreeList = (nsPurpleBufferEntry*)
|
|
|
|
(PRUword(mFreeList) | PRUword(1));
|
|
|
|
mFreeList = e;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
////////////////////////////////////////////////////////////////////////
|
|
|
|
// Implement the LanguageRuntime interface for C++/XPCOM
|
|
|
|
////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
|
|
|
|
struct nsCycleCollectionXPCOMRuntime :
|
|
|
|
public nsCycleCollectionLanguageRuntime
|
|
|
|
{
|
2010-07-20 10:31:01 +04:00
|
|
|
nsresult BeginCycleCollection(nsCycleCollectionTraversalCallback &cb,
|
|
|
|
bool explainLiveExpectedGarbage)
|
2007-03-27 13:49:06 +04:00
|
|
|
{
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2010-11-12 01:52:30 +03:00
|
|
|
nsresult FinishTraverse()
|
|
|
|
{
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2007-05-24 18:10:02 +04:00
|
|
|
nsresult FinishCycleCollection()
|
2007-03-27 13:49:06 +04:00
|
|
|
{
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2007-06-07 02:04:26 +04:00
|
|
|
inline nsCycleCollectionParticipant *ToParticipant(void *p);
|
2007-08-23 04:46:43 +04:00
|
|
|
|
|
|
|
#ifdef DEBUG_CC
|
|
|
|
virtual void PrintAllReferencesTo(void *p) {}
|
|
|
|
#endif
|
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;
|
2008-02-25 20:47:25 +03:00
|
|
|
PRUint32 mCollectedObjects;
|
2011-05-12 22:22:25 +04:00
|
|
|
TimeStamp mCollectionStart;
|
2007-01-05 01:31:26 +03:00
|
|
|
|
|
|
|
nsCycleCollectionLanguageRuntime *mRuntimes[nsIProgrammingLanguage::MAX+1];
|
2007-03-27 13:49:06 +04:00
|
|
|
nsCycleCollectionXPCOMRuntime mXPCOMRuntime;
|
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;
|
2008-02-15 16:12:55 +03:00
|
|
|
PRUint32 mWhiteNodeCount;
|
|
|
|
|
2011-06-11 00:33:06 +04:00
|
|
|
// mVisitedRefCounted and mVisitedGCed are only used for telemetry
|
|
|
|
PRUint32 mVisitedRefCounted;
|
|
|
|
PRUint32 mVisitedGCed;
|
|
|
|
|
2007-01-05 01:31:26 +03:00
|
|
|
nsPurpleBuffer mPurpleBuf;
|
2007-04-21 21:43:19 +04:00
|
|
|
|
2007-01-05 01:31:26 +03:00
|
|
|
void RegisterRuntime(PRUint32 langID,
|
|
|
|
nsCycleCollectionLanguageRuntime *rt);
|
2009-07-09 05:10:29 +04:00
|
|
|
nsCycleCollectionLanguageRuntime * GetRuntime(PRUint32 langID);
|
2007-01-05 01:31:26 +03:00
|
|
|
void ForgetRuntime(PRUint32 langID);
|
|
|
|
|
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
|
|
|
|
|
|
|
// 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();
|
|
|
|
|
2009-05-07 00:46:04 +04:00
|
|
|
// The first pair of Suspect and Forget functions are only used by
|
|
|
|
// old XPCOM binary components.
|
2011-09-29 10:19:26 +04:00
|
|
|
bool Suspect(nsISupports *n);
|
|
|
|
bool Forget(nsISupports *n);
|
2009-05-07 00:46:04 +04:00
|
|
|
nsPurpleBufferEntry* Suspect2(nsISupports *n);
|
2011-09-29 10:19:26 +04:00
|
|
|
bool Forget2(nsPurpleBufferEntry *e);
|
2009-05-07 00:46:04 +04:00
|
|
|
|
2010-08-12 04:03:23 +04:00
|
|
|
PRUint32 Collect(PRUint32 aTryCollections,
|
|
|
|
nsICycleCollectorListener *aListener);
|
2010-11-12 01:52:30 +03:00
|
|
|
|
|
|
|
// Prepare for and cleanup after one or more collection(s).
|
2011-09-29 10:19:26 +04:00
|
|
|
bool PrepareForCollection(nsTArray<PtrInfo*> *aWhiteNodes);
|
|
|
|
void GCIfNeeded(bool aForceGC);
|
2010-11-12 01:52:30 +03:00
|
|
|
void CleanupAfterCollection();
|
|
|
|
|
|
|
|
// Start and finish an individual collection.
|
2011-09-29 10:19:26 +04:00
|
|
|
bool BeginCollection(nsICycleCollectorListener *aListener);
|
|
|
|
bool FinishCollection(nsICycleCollectorListener *aListener);
|
2010-11-12 01:52:30 +03:00
|
|
|
|
2008-02-25 20:47:25 +03:00
|
|
|
PRUint32 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;
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
|
2007-11-02 01:51:57 +03:00
|
|
|
void Allocated(void *n, size_t sz);
|
|
|
|
void Freed(void *n);
|
2007-07-06 02:20:16 +04:00
|
|
|
|
2007-03-10 02:00:14 +03:00
|
|
|
void ExplainLiveExpectedGarbage();
|
2011-09-29 10:19:26 +04:00
|
|
|
bool CreateReversedEdges();
|
2008-01-10 17:10:03 +03:00
|
|
|
void DestroyReversedEdges();
|
2007-03-10 02:00:14 +03:00
|
|
|
void ShouldBeFreed(nsISupports *n);
|
|
|
|
void WasFreed(nsISupports *n);
|
|
|
|
PointerSet mExpectedGarbage;
|
|
|
|
#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
|
|
|
|
////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
|
2007-03-14 09:15:10 +03:00
|
|
|
static nsCycleCollector *sCollector = nsnull;
|
2007-01-05 01:31:26 +03:00
|
|
|
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////
|
|
|
|
// Utility functions
|
|
|
|
////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2008-01-07 01:05:10 +03:00
|
|
|
class CCRunnableFaultReport : public nsRunnable {
|
|
|
|
public:
|
|
|
|
CCRunnableFaultReport(const nsCString& report)
|
|
|
|
{
|
|
|
|
CopyUTF8toUTF16(report, mReport);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHOD Run() {
|
2011-12-09 23:36:17 +04:00
|
|
|
nsCOMPtr<nsIObserverService> obs = services::GetObserverService();
|
2008-01-07 01:05:10 +03:00
|
|
|
if (obs) {
|
|
|
|
obs->NotifyObservers(nsnull, "cycle-collector-fault",
|
|
|
|
mReport.get());
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIConsoleService> cons =
|
|
|
|
do_GetService(NS_CONSOLESERVICE_CONTRACTID);
|
|
|
|
if (cons) {
|
|
|
|
cons->LogStringMessage(mReport.get());
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
nsString mReport;
|
|
|
|
};
|
|
|
|
|
2007-01-05 01:31:26 +03:00
|
|
|
static void
|
|
|
|
Fault(const char *msg, const void *ptr=nsnull)
|
|
|
|
{
|
2007-04-21 21:43:19 +04:00
|
|
|
#ifdef DEBUG_CC
|
2007-03-27 13:49:06 +04:00
|
|
|
// This should be nearly impossible, but just in case.
|
|
|
|
if (!sCollector)
|
|
|
|
return;
|
2007-01-05 01:31:26 +03:00
|
|
|
|
2007-03-27 13:49:06 +04:00
|
|
|
if (sCollector->mParams.mFaultIsFatal) {
|
2007-01-05 01:31:26 +03:00
|
|
|
|
|
|
|
if (ptr)
|
|
|
|
printf("Fatal fault in cycle collector: %s (ptr: %p)\n", msg, ptr);
|
|
|
|
else
|
|
|
|
printf("Fatal fault in cycle collector: %s\n", msg);
|
|
|
|
|
2007-06-19 05:29:10 +04:00
|
|
|
exit(1);
|
|
|
|
}
|
2007-04-21 21:43:19 +04:00
|
|
|
#endif
|
2007-01-05 01:31:26 +03:00
|
|
|
|
2008-01-07 01:05:10 +03:00
|
|
|
nsPrintfCString str(256, "Fault in cycle collector: %s (ptr: %p)\n",
|
|
|
|
msg, ptr);
|
|
|
|
NS_NOTREACHED(str.get());
|
2007-03-06 00:07:51 +03:00
|
|
|
|
2007-01-05 01:31:26 +03:00
|
|
|
// When faults are not fatal, we assume we're running in a
|
|
|
|
// production environment and we therefore want to disable the
|
|
|
|
// collector on a fault. This will unfortunately cause the browser
|
|
|
|
// to leak pretty fast wherever creates cyclical garbage, but it's
|
|
|
|
// probably a better user experience than crashing. Besides, we
|
|
|
|
// *should* never hit a fault.
|
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
sCollector->mParams.mDoNothing = true;
|
2008-01-07 01:05:10 +03:00
|
|
|
|
|
|
|
// Report to observers off an event so we don't run JS under GC
|
|
|
|
// (which is where we might be right now).
|
|
|
|
nsCOMPtr<nsIRunnable> ev = new CCRunnableFaultReport(str);
|
2009-10-28 20:28:57 +03:00
|
|
|
NS_DispatchToMainThread(ev);
|
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
|
|
|
|
inline void
|
|
|
|
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
|
|
|
|
|
|
|
static nsISupports *
|
|
|
|
canonicalize(nsISupports *in)
|
|
|
|
{
|
2010-11-12 01:52:30 +03:00
|
|
|
nsISupports* child;
|
2007-04-26 01:12:11 +04:00
|
|
|
in->QueryInterface(NS_GET_IID(nsCycleCollectionISupports),
|
2010-11-12 01:52:30 +03:00
|
|
|
reinterpret_cast<void**>(&child));
|
|
|
|
return child;
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCycleCollectionParticipant *
|
|
|
|
nsCycleCollectionXPCOMRuntime::ToParticipant(void *p)
|
|
|
|
{
|
|
|
|
nsXPCOMCycleCollectionParticipant *cp;
|
2007-07-08 11:08:04 +04:00
|
|
|
::ToParticipant(static_cast<nsISupports*>(p), &cp);
|
2007-06-07 02:04:26 +04:00
|
|
|
return cp;
|
|
|
|
}
|
|
|
|
|
2007-04-26 01:12:11 +04:00
|
|
|
|
2010-01-12 19:51:39 +03:00
|
|
|
template <class Visitor>
|
2007-04-26 01:12:11 +04:00
|
|
|
void
|
2010-01-12 19:51:39 +03:00
|
|
|
GraphWalker<Visitor>::Walk(PtrInfo *s0)
|
2007-04-26 01:12:11 +04:00
|
|
|
{
|
|
|
|
nsDeque queue;
|
|
|
|
queue.Push(s0);
|
|
|
|
DoWalk(queue);
|
|
|
|
}
|
|
|
|
|
2010-01-12 19:51:39 +03:00
|
|
|
template <class Visitor>
|
2007-04-26 01:12:11 +04:00
|
|
|
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);
|
|
|
|
for (PRUint32 i = 0; i < aGraph.mRootCount; ++i) {
|
|
|
|
queue.Push(etor.GetNext());
|
|
|
|
}
|
|
|
|
DoWalk(queue);
|
|
|
|
}
|
|
|
|
|
2010-01-12 19:51:39 +03:00
|
|
|
template <class Visitor>
|
2007-04-26 01:12:11 +04:00
|
|
|
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());
|
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) {
|
2007-04-26 01:12:11 +04:00
|
|
|
aQueue.Push(*child);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
#ifdef DEBUG_CC
|
|
|
|
sCollector->mStats.mWalkedGraph++;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-08-12 04:03:23 +04:00
|
|
|
class nsCycleCollectorLogger : public nsICycleCollectorListener
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
nsCycleCollectorLogger() : mStream(nsnull)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
~nsCycleCollectorLogger()
|
|
|
|
{
|
|
|
|
if (mStream) {
|
|
|
|
fclose(mStream);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
|
|
|
|
NS_IMETHOD Begin()
|
|
|
|
{
|
|
|
|
char name[255];
|
2011-10-07 00:22:43 +04:00
|
|
|
sprintf(name, "cc-edges-%d.%d.log", ++gLogCounter, base::GetCurrentProcId());
|
2010-08-12 04:03:23 +04:00
|
|
|
mStream = fopen(name, "w");
|
|
|
|
|
|
|
|
return mStream ? NS_OK : NS_ERROR_FAILURE;
|
|
|
|
}
|
2011-06-22 21:41:17 +04:00
|
|
|
NS_IMETHOD NoteRefCountedObject(PRUint64 aAddress, PRUint32 refCount,
|
|
|
|
const char *aObjectDescription)
|
|
|
|
{
|
|
|
|
fprintf(mStream, "%p [rc=%u] %s\n", (void*)aAddress, refCount,
|
|
|
|
aObjectDescription);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2011-09-29 10:19:26 +04:00
|
|
|
NS_IMETHOD NoteGCedObject(PRUint64 aAddress, bool aMarked,
|
2011-06-22 21:41:17 +04:00
|
|
|
const char *aObjectDescription)
|
2010-08-12 04:03:23 +04:00
|
|
|
{
|
2011-06-22 21:41:17 +04:00
|
|
|
fprintf(mStream, "%p [gc%s] %s\n", (void*)aAddress,
|
|
|
|
aMarked ? ".marked" : "", aObjectDescription);
|
2010-08-12 04:03:23 +04:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2011-06-22 21:41:17 +04:00
|
|
|
NS_IMETHOD NoteEdge(PRUint64 aToAddress, const char *aEdgeName)
|
2010-08-12 04:03:23 +04:00
|
|
|
{
|
|
|
|
fprintf(mStream, "> %p %s\n", (void*)aToAddress, aEdgeName);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2011-06-22 21:41:17 +04:00
|
|
|
NS_IMETHOD BeginResults()
|
2010-08-12 04:03:23 +04:00
|
|
|
{
|
|
|
|
fputs("==========\n", mStream);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2011-06-22 21:41:17 +04:00
|
|
|
NS_IMETHOD DescribeRoot(PRUint64 aAddress, PRUint32 aKnownEdges)
|
2010-08-12 04:03:23 +04:00
|
|
|
{
|
2011-06-22 21:41:17 +04:00
|
|
|
fprintf(mStream, "%p [known=%u]\n", (void*)aAddress, aKnownEdges);
|
2010-08-12 04:03:23 +04:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2011-06-22 21:41:17 +04:00
|
|
|
NS_IMETHOD DescribeGarbage(PRUint64 aAddress)
|
2010-08-12 04:03:23 +04:00
|
|
|
{
|
2011-06-22 21:41:17 +04:00
|
|
|
fprintf(mStream, "%p [garbage]\n", (void*)aAddress);
|
2010-08-12 04:03:23 +04:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
NS_IMETHOD End()
|
|
|
|
{
|
|
|
|
fclose(mStream);
|
|
|
|
mStream = nsnull;
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
FILE *mStream;
|
|
|
|
|
|
|
|
static PRUint32 gLogCounter;
|
|
|
|
};
|
|
|
|
|
|
|
|
NS_IMPL_ISUPPORTS1(nsCycleCollectorLogger, nsICycleCollectorListener)
|
|
|
|
|
|
|
|
PRUint32 nsCycleCollectorLogger::gLogCounter = 0;
|
|
|
|
|
|
|
|
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,
|
|
|
|
nsnull
|
|
|
|
};
|
|
|
|
|
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;
|
|
|
|
nsCycleCollectionLanguageRuntime **mRuntimes; // weak, from nsCycleCollector
|
2008-03-18 02:11:08 +03:00
|
|
|
nsCString mNextEdgeName;
|
2011-01-14 13:06:09 +03:00
|
|
|
nsICycleCollectorListener *mListener;
|
2007-04-26 01:12:11 +04:00
|
|
|
|
|
|
|
public:
|
|
|
|
GCGraphBuilder(GCGraph &aGraph,
|
2010-08-12 04:03:23 +04:00
|
|
|
nsCycleCollectionLanguageRuntime **aRuntimes,
|
|
|
|
nsICycleCollectorListener *aListener);
|
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
|
|
|
|
|
|
|
PRUint32 Count() const { return mPtrToNodeMap.entryCount; }
|
|
|
|
|
2007-08-23 04:46:43 +04:00
|
|
|
#ifdef DEBUG_CC
|
|
|
|
PtrInfo* AddNode(void *s, nsCycleCollectionParticipant *aParticipant,
|
|
|
|
PRUint32 aLangID);
|
|
|
|
#else
|
2007-05-24 18:10:02 +04:00
|
|
|
PtrInfo* AddNode(void *s, nsCycleCollectionParticipant *aParticipant);
|
2007-08-23 04:46:43 +04:00
|
|
|
PtrInfo* AddNode(void *s, nsCycleCollectionParticipant *aParticipant,
|
|
|
|
PRUint32 aLangID)
|
|
|
|
{
|
2007-08-23 05:29:54 +04:00
|
|
|
return AddNode(s, aParticipant);
|
2007-08-23 04:46:43 +04:00
|
|
|
}
|
|
|
|
#endif
|
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
|
|
|
|
|
|
|
// nsCycleCollectionTraversalCallback methods.
|
2008-02-16 01:23:16 +03:00
|
|
|
NS_IMETHOD_(void) NoteXPCOMRoot(nsISupports *root);
|
|
|
|
|
|
|
|
private:
|
2011-06-24 01:10:52 +04:00
|
|
|
void DescribeNode(PRUint32 refCount,
|
|
|
|
size_t objSz,
|
|
|
|
const char *objName)
|
|
|
|
{
|
2011-06-22 21:41:17 +04:00
|
|
|
mCurrPi->mRefCount = refCount;
|
2011-06-24 01:10:52 +04:00
|
|
|
#ifdef DEBUG_CC
|
|
|
|
mCurrPi->mBytes = objSz;
|
|
|
|
mCurrPi->mName = PL_strdup(objName);
|
|
|
|
sCollector->mStats.mVisitedNode++;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHOD_(void) DescribeRefCountedNode(nsrefcnt refCount, size_t objSz,
|
|
|
|
const char *objName);
|
2011-09-29 10:19:26 +04:00
|
|
|
NS_IMETHOD_(void) DescribeGCedNode(bool isMarked, size_t objSz,
|
2011-06-24 01:10:52 +04:00
|
|
|
const char *objName);
|
2007-11-02 01:51:57 +03:00
|
|
|
NS_IMETHOD_(void) NoteRoot(PRUint32 langID, void *child,
|
|
|
|
nsCycleCollectionParticipant* participant);
|
2007-09-18 04:30:06 +04:00
|
|
|
NS_IMETHOD_(void) NoteXPCOMChild(nsISupports *child);
|
|
|
|
NS_IMETHOD_(void) NoteNativeChild(void *child,
|
2007-08-06 18:34:02 +04:00
|
|
|
nsCycleCollectionParticipant *participant);
|
2007-09-18 04:30:06 +04:00
|
|
|
NS_IMETHOD_(void) NoteScriptChild(PRUint32 langID, void *child);
|
2008-03-18 02:11:08 +03:00
|
|
|
NS_IMETHOD_(void) NoteNextEdgeName(const char* name);
|
2011-11-24 16:35:56 +04:00
|
|
|
NS_IMETHOD_(void) NoteWeakMapping(void *map, void *key, void *val);
|
2007-04-26 01:12:11 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
GCGraphBuilder::GCGraphBuilder(GCGraph &aGraph,
|
2010-08-12 04:03:23 +04:00
|
|
|
nsCycleCollectionLanguageRuntime **aRuntimes,
|
|
|
|
nsICycleCollectorListener *aListener)
|
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),
|
2010-08-12 04:03:23 +04:00
|
|
|
mRuntimes(aRuntimes),
|
|
|
|
mListener(aListener)
|
2007-04-26 01:12:11 +04:00
|
|
|
{
|
|
|
|
if (!PL_DHashTableInit(&mPtrToNodeMap, &PtrNodeOps, nsnull,
|
|
|
|
sizeof(PtrToNodeEntry), 32768))
|
|
|
|
mPtrToNodeMap.ops = nsnull;
|
2010-08-12 04:03:23 +04:00
|
|
|
// We want all edges and all info if DEBUG_CC is set or if we have a
|
|
|
|
// listener. Do we want them all the time?
|
|
|
|
#ifndef DEBUG_CC
|
|
|
|
if (mListener)
|
2009-07-09 05:10:29 +04:00
|
|
|
#endif
|
2010-08-12 04:03:23 +04:00
|
|
|
{
|
|
|
|
mFlags |= nsCycleCollectionTraversalCallback::WANT_DEBUG_INFO |
|
|
|
|
nsCycleCollectionTraversalCallback::WANT_ALL_TRACES;
|
|
|
|
}
|
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*
|
2007-08-23 04:46:43 +04:00
|
|
|
GCGraphBuilder::AddNode(void *s, nsCycleCollectionParticipant *aParticipant
|
|
|
|
IF_DEBUG_CC_PARAM(PRUint32 aLangID)
|
|
|
|
)
|
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)
|
|
|
|
return nsnull;
|
|
|
|
|
2007-04-26 01:12:11 +04:00
|
|
|
PtrInfo *result;
|
|
|
|
if (!e->mNode) {
|
|
|
|
// New entry.
|
2007-08-23 04:46:43 +04:00
|
|
|
result = mNodeBuilder.Add(s, aParticipant
|
|
|
|
IF_DEBUG_CC_PARAM(aLangID)
|
|
|
|
);
|
2007-04-26 01:12:11 +04:00
|
|
|
if (!result) {
|
|
|
|
PL_DHashTableRawRemove(&mPtrToNodeMap, e);
|
|
|
|
return nsnull;
|
|
|
|
}
|
|
|
|
e->mNode = result;
|
|
|
|
} else {
|
|
|
|
result = e->mNode;
|
2007-05-24 18:10:02 +04:00
|
|
|
NS_ASSERTION(result->mParticipant == aParticipant,
|
|
|
|
"nsCycleCollectionParticipant shouldn't change!");
|
2007-04-26 01:12:11 +04:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
root = canonicalize(root);
|
|
|
|
NS_ASSERTION(root,
|
|
|
|
"Don't add objects that don't participate in collection!");
|
|
|
|
|
|
|
|
#ifdef DEBUG_CC
|
|
|
|
if (nsCycleCollector_shouldSuppress(root))
|
|
|
|
return;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
nsXPCOMCycleCollectionParticipant *cp;
|
|
|
|
ToParticipant(root, &cp);
|
|
|
|
|
|
|
|
NoteRoot(nsIProgrammingLanguage::CPLUSPLUS, root, cp);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
NS_IMETHODIMP_(void)
|
|
|
|
GCGraphBuilder::NoteRoot(PRUint32 langID, void *root,
|
|
|
|
nsCycleCollectionParticipant* participant)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(root, "Don't add a null root!");
|
|
|
|
|
|
|
|
if (langID > nsIProgrammingLanguage::MAX || !mRuntimes[langID]) {
|
|
|
|
Fault("adding root for unregistered language", root);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
AddNode(root, participant, langID);
|
|
|
|
}
|
|
|
|
|
2007-09-18 04:30:06 +04:00
|
|
|
NS_IMETHODIMP_(void)
|
2011-06-24 01:10:52 +04:00
|
|
|
GCGraphBuilder::DescribeRefCountedNode(nsrefcnt refCount, size_t objSz,
|
|
|
|
const char *objName)
|
|
|
|
{
|
|
|
|
if (refCount == 0)
|
|
|
|
Fault("zero refcount", mCurrPi);
|
|
|
|
if (refCount == PR_UINT32_MAX)
|
|
|
|
Fault("overflowing refcount", mCurrPi);
|
2011-06-11 00:33:06 +04:00
|
|
|
sCollector->mVisitedRefCounted++;
|
2011-06-22 21:41:17 +04:00
|
|
|
|
|
|
|
if (mListener) {
|
|
|
|
mListener->NoteRefCountedObject((PRUint64)mCurrPi->mPointer, refCount,
|
|
|
|
objName);
|
|
|
|
}
|
|
|
|
|
2011-06-24 01:10:52 +04:00
|
|
|
DescribeNode(refCount, objSz, objName);
|
|
|
|
}
|
2007-07-06 02:38:38 +04:00
|
|
|
|
2011-06-24 01:10:52 +04:00
|
|
|
NS_IMETHODIMP_(void)
|
2011-09-29 10:19:26 +04:00
|
|
|
GCGraphBuilder::DescribeGCedNode(bool isMarked, size_t objSz,
|
2011-06-24 01:10:52 +04:00
|
|
|
const char *objName)
|
|
|
|
{
|
|
|
|
PRUint32 refCount = isMarked ? PR_UINT32_MAX : 0;
|
2011-06-11 00:33:06 +04:00
|
|
|
sCollector->mVisitedGCed++;
|
2011-06-22 21:41:17 +04:00
|
|
|
|
|
|
|
if (mListener) {
|
|
|
|
mListener->NoteGCedObject((PRUint64)mCurrPi->mPointer, isMarked,
|
|
|
|
objName);
|
|
|
|
}
|
|
|
|
|
2011-06-24 01:10:52 +04:00
|
|
|
DescribeNode(refCount, objSz, 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();
|
|
|
|
}
|
2007-05-24 18:10:02 +04:00
|
|
|
if (!child || !(child = canonicalize(child)))
|
2007-03-09 16:14:06 +03:00
|
|
|
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
|
2007-05-24 18:10:02 +04:00
|
|
|
|
|
|
|
nsXPCOMCycleCollectionParticipant *cp;
|
|
|
|
ToParticipant(child, &cp);
|
|
|
|
if (cp) {
|
2007-08-23 04:46:43 +04:00
|
|
|
PtrInfo *childPi = AddNode(child, cp, nsIProgrammingLanguage::CPLUSPLUS);
|
2007-04-20 12:01:01 +04:00
|
|
|
if (!childPi)
|
|
|
|
return;
|
2007-04-26 01:12:11 +04:00
|
|
|
mEdgeBuilder.Add(childPi);
|
2010-08-18 11:46:54 +04:00
|
|
|
#ifdef DEBUG_CC
|
|
|
|
mCurrPi->mEdgeNames.AppendElement(edgeName);
|
|
|
|
#endif
|
2010-08-12 04:03:23 +04:00
|
|
|
if (mListener) {
|
2011-06-22 21:41:17 +04:00
|
|
|
mListener->NoteEdge((PRUint64)child, edgeName.get());
|
2010-08-12 04:03:23 +04:00
|
|
|
}
|
2007-04-26 01:12:11 +04:00
|
|
|
++childPi->mInternalRefs;
|
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;
|
|
|
|
|
|
|
|
NS_ASSERTION(participant, "Need a nsCycleCollectionParticipant!");
|
|
|
|
|
2007-08-23 04:46:43 +04:00
|
|
|
PtrInfo *childPi = AddNode(child, participant, nsIProgrammingLanguage::CPLUSPLUS);
|
2007-05-24 18:10:02 +04:00
|
|
|
if (!childPi)
|
|
|
|
return;
|
|
|
|
mEdgeBuilder.Add(childPi);
|
2010-08-18 11:46:54 +04:00
|
|
|
#ifdef DEBUG_CC
|
|
|
|
mCurrPi->mEdgeNames.AppendElement(edgeName);
|
|
|
|
#endif
|
2010-08-12 04:03:23 +04:00
|
|
|
if (mListener) {
|
2011-06-22 21:41:17 +04:00
|
|
|
mListener->NoteEdge((PRUint64)child, edgeName.get());
|
2010-08-12 04:03:23 +04:00
|
|
|
}
|
2007-05-24 18:10:02 +04:00
|
|
|
++childPi->mInternalRefs;
|
|
|
|
}
|
|
|
|
|
2007-09-18 04:30:06 +04:00
|
|
|
NS_IMETHODIMP_(void)
|
2007-04-26 01:12:11 +04:00
|
|
|
GCGraphBuilder::NoteScriptChild(PRUint32 langID, void *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();
|
|
|
|
}
|
2007-01-05 01:31:26 +03:00
|
|
|
if (!child)
|
2007-03-09 16:14:06 +03:00
|
|
|
return;
|
2007-01-09 04:33:02 +03:00
|
|
|
|
2008-06-21 19:25:29 +04:00
|
|
|
if (langID > nsIProgrammingLanguage::MAX) {
|
|
|
|
Fault("traversing pointer for unknown language", child);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!mRuntimes[langID]) {
|
2008-06-21 21:47:46 +04:00
|
|
|
NS_WARNING("Not collecting cycles involving objects for scripting "
|
|
|
|
"languages that don't participate in cycle collection.");
|
2007-04-21 21:43:19 +04:00
|
|
|
return;
|
|
|
|
}
|
2007-01-09 04:33:02 +03:00
|
|
|
|
2011-03-29 00:05:48 +04:00
|
|
|
// skip over non-grey JS children
|
2011-05-04 20:26:44 +04:00
|
|
|
if (langID == nsIProgrammingLanguage::JAVASCRIPT &&
|
|
|
|
!xpc_GCThingIsGrayCCThing(child) && !WantAllTraces()) {
|
|
|
|
return;
|
2011-03-29 00:05:48 +04:00
|
|
|
}
|
|
|
|
|
2007-05-24 18:10:02 +04:00
|
|
|
nsCycleCollectionParticipant *cp = mRuntimes[langID]->ToParticipant(child);
|
|
|
|
if (!cp)
|
|
|
|
return;
|
|
|
|
|
2007-08-23 04:46:43 +04:00
|
|
|
PtrInfo *childPi = AddNode(child, cp, langID);
|
2007-04-20 12:01:01 +04:00
|
|
|
if (!childPi)
|
|
|
|
return;
|
2007-04-26 01:12:11 +04:00
|
|
|
mEdgeBuilder.Add(childPi);
|
2010-08-18 11:46:54 +04:00
|
|
|
#ifdef DEBUG_CC
|
|
|
|
mCurrPi->mEdgeNames.AppendElement(edgeName);
|
|
|
|
#endif
|
2010-08-12 04:03:23 +04:00
|
|
|
if (mListener) {
|
2011-06-22 21:41:17 +04:00
|
|
|
mListener->NoteEdge((PRUint64)child, edgeName.get());
|
2010-08-12 04:03:23 +04:00
|
|
|
}
|
2007-04-26 01:12:11 +04:00
|
|
|
++childPi->mInternalRefs;
|
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)
|
|
|
|
{
|
|
|
|
nsCycleCollectionParticipant *cp;
|
|
|
|
NS_ASSERTION(node, "Weak map node should be non-null.");
|
|
|
|
|
|
|
|
if (!xpc_GCThingIsGrayCCThing(node) && !WantAllTraces())
|
|
|
|
return nsnull;
|
|
|
|
|
|
|
|
cp = mRuntimes[nsIProgrammingLanguage::JAVASCRIPT]->ToParticipant(node);
|
|
|
|
NS_ASSERTION(cp, "Javascript runtime participant should be non-null.");
|
|
|
|
return AddNode(node, cp);
|
|
|
|
}
|
|
|
|
|
2011-11-24 16:35:56 +04:00
|
|
|
NS_IMETHODIMP_(void)
|
|
|
|
GCGraphBuilder::NoteWeakMapping(void *map, void *key, void *val)
|
|
|
|
{
|
2011-11-24 16:35:56 +04:00
|
|
|
PtrInfo *valNode = AddWeakMapNode(val);
|
|
|
|
|
|
|
|
if (!valNode)
|
|
|
|
return;
|
|
|
|
|
|
|
|
WeakMapping *mapping = mWeakMaps.AppendElement();
|
|
|
|
mapping->mMap = map ? AddWeakMapNode(map) : nsnull;
|
|
|
|
mapping->mKey = key ? AddWeakMapNode(key) : nsnull;
|
|
|
|
mapping->mVal = valNode;
|
2011-11-24 16:35:56 +04:00
|
|
|
}
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
static bool
|
2008-02-15 16:12:55 +03:00
|
|
|
AddPurpleRoot(GCGraphBuilder &builder, nsISupports *root)
|
|
|
|
{
|
|
|
|
root = canonicalize(root);
|
|
|
|
NS_ASSERTION(root,
|
|
|
|
"Don't add objects that don't participate in collection!");
|
|
|
|
|
|
|
|
nsXPCOMCycleCollectionParticipant *cp;
|
|
|
|
ToParticipant(root, &cp);
|
|
|
|
|
|
|
|
PtrInfo *pinfo = builder.AddNode(root, cp,
|
|
|
|
nsIProgrammingLanguage::CPLUSPLUS);
|
|
|
|
if (!pinfo) {
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2008-02-15 16:12:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
cp->UnmarkPurple(root);
|
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
return true;
|
2008-02-15 16:12:55 +03:00
|
|
|
}
|
2007-01-05 01:31:26 +03:00
|
|
|
|
2009-04-25 02:45:34 +04:00
|
|
|
#ifdef DEBUG_CC
|
|
|
|
static PLDHashOperator
|
2009-05-07 00:46:04 +04:00
|
|
|
noteAllCallback(nsVoidPtrHashKey* key, void* userArg)
|
2009-04-25 02:45:34 +04:00
|
|
|
{
|
|
|
|
GCGraphBuilder *builder = static_cast<GCGraphBuilder*>(userArg);
|
2009-05-07 00:46:04 +04:00
|
|
|
builder->NoteXPCOMRoot(
|
|
|
|
static_cast<nsISupports *>(const_cast<void*>(key->GetKey())));
|
2009-04-25 02:45:34 +04:00
|
|
|
return PL_DHASH_NEXT;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsPurpleBuffer::NoteAll(GCGraphBuilder &builder)
|
|
|
|
{
|
2009-05-07 00:46:04 +04:00
|
|
|
mCompatObjects.EnumerateEntries(noteAllCallback, &builder);
|
|
|
|
|
|
|
|
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) {
|
2009-10-28 20:28:57 +03:00
|
|
|
if (!(PRUword(e->mObject) & PRUword(1)) && e->mObject) {
|
2009-05-07 00:46:04 +04:00
|
|
|
builder.NoteXPCOMRoot(e->mObject);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-04-25 02:45:34 +04:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
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();
|
|
|
|
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
|
|
|
{
|
2010-01-12 19:51:39 +03:00
|
|
|
ScanBlackVisitor(PRUint32 &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 != black;
|
2007-01-05 01:31:26 +03:00
|
|
|
}
|
|
|
|
|
2007-04-26 01:12:11 +04:00
|
|
|
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
|
|
|
|
|
|
|
PRUint32 &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
|
|
|
{
|
2010-01-12 19:51:39 +03:00
|
|
|
scanVisitor(PRUint32 &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
|
|
|
}
|
|
|
|
|
2007-04-26 01:12:11 +04:00
|
|
|
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);
|
2007-04-20 12:01:01 +04:00
|
|
|
NS_ASSERTION(pi->mColor == black,
|
2010-01-12 19:51:39 +03:00
|
|
|
"Why didn't ScanBlackVisitor make pi black?");
|
2007-01-05 01:31:26 +03:00
|
|
|
}
|
|
|
|
}
|
2008-02-15 16:12:55 +03:00
|
|
|
|
|
|
|
PRUint32 &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;
|
|
|
|
for (PRUint32 i = 0; i < mGraph.mWeakMaps.Length(); i++) {
|
|
|
|
WeakMapping *wm = &mGraph.mWeakMaps[i];
|
|
|
|
|
|
|
|
// If mMap or mKey are null, the original object was marked black.
|
|
|
|
uint32 mColor = wm->mMap ? wm->mMap->mColor : black;
|
|
|
|
uint32 kColor = wm->mKey ? wm->mKey->mColor : black;
|
|
|
|
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().
|
|
|
|
NS_ASSERTION(mColor != grey, "Uncolored weak map");
|
|
|
|
NS_ASSERTION(kColor != grey, "Uncolored weak map key");
|
|
|
|
NS_ASSERTION(v->mColor != grey, "Uncolored weak map value");
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
2008-02-15 16:12:55 +03:00
|
|
|
NS_ASSERTION(mWhiteNodes->IsEmpty(),
|
|
|
|
"FinishCollection wasn't called?");
|
|
|
|
|
|
|
|
mWhiteNodes->SetCapacity(mWhiteNodeCount);
|
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);
|
|
|
|
}
|
2007-04-26 01:12:11 +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
|
|
|
|
|
2008-02-15 16:12:55 +03:00
|
|
|
PRUint32 i, count = mWhiteNodes->Length();
|
2011-06-22 21:41:17 +04:00
|
|
|
|
|
|
|
if (aListener) {
|
|
|
|
for (i = 0; i < count; ++i) {
|
|
|
|
PtrInfo *pinfo = mWhiteNodes->ElementAt(i);
|
|
|
|
aListener->DescribeGarbage((PRUint64)pinfo->mPointer);
|
|
|
|
}
|
|
|
|
aListener->End();
|
|
|
|
}
|
|
|
|
|
2007-05-24 18:10:02 +04:00
|
|
|
for (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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-05-24 18:10:02 +04:00
|
|
|
for (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
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2008-02-25 20:47:25 +03:00
|
|
|
mCollectedObjects += count;
|
2007-06-07 02:09:00 +04:00
|
|
|
return count > 0;
|
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
|
|
|
////////////////////////////////////////////////////////////////////////
|
|
|
|
// Memory-hooking stuff
|
|
|
|
// When debugging wild pointers, it sometimes helps to hook malloc and
|
|
|
|
// free. This stuff is disabled unless you set an environment variable.
|
|
|
|
////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
static bool hookedMalloc = false;
|
2007-01-05 01:31:26 +03:00
|
|
|
|
2008-01-22 03:17:41 +03:00
|
|
|
#if defined(__GLIBC__) && !defined(__UCLIBC__)
|
2007-01-05 01:31:26 +03:00
|
|
|
#include <malloc.h>
|
|
|
|
|
|
|
|
static void* (*old_memalign_hook)(size_t, size_t, const void *);
|
|
|
|
static void* (*old_realloc_hook)(void *, size_t, const void *);
|
|
|
|
static void* (*old_malloc_hook)(size_t, const void *);
|
|
|
|
static void (*old_free_hook)(void *, const void *);
|
|
|
|
|
|
|
|
static void* my_memalign_hook(size_t, size_t, const void *);
|
|
|
|
static void* my_realloc_hook(void *, size_t, const void *);
|
|
|
|
static void* my_malloc_hook(size_t, const void *);
|
|
|
|
static void my_free_hook(void *, const void *);
|
|
|
|
|
|
|
|
static inline void
|
|
|
|
install_old_hooks()
|
|
|
|
{
|
|
|
|
__memalign_hook = old_memalign_hook;
|
|
|
|
__realloc_hook = old_realloc_hook;
|
|
|
|
__malloc_hook = old_malloc_hook;
|
|
|
|
__free_hook = old_free_hook;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void
|
|
|
|
save_old_hooks()
|
|
|
|
{
|
|
|
|
// Glibc docs recommend re-saving old hooks on
|
|
|
|
// return from recursive calls. Strangely when
|
|
|
|
// we do this, we find ourselves in infinite
|
|
|
|
// recursion.
|
|
|
|
|
|
|
|
// old_memalign_hook = __memalign_hook;
|
|
|
|
// old_realloc_hook = __realloc_hook;
|
|
|
|
// old_malloc_hook = __malloc_hook;
|
|
|
|
// old_free_hook = __free_hook;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void
|
|
|
|
install_new_hooks()
|
|
|
|
{
|
|
|
|
__memalign_hook = my_memalign_hook;
|
|
|
|
__realloc_hook = my_realloc_hook;
|
|
|
|
__malloc_hook = my_malloc_hook;
|
|
|
|
__free_hook = my_free_hook;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void*
|
|
|
|
my_realloc_hook(void *ptr, size_t size, const void *caller)
|
|
|
|
{
|
2007-03-27 13:49:06 +04:00
|
|
|
void *result;
|
2007-01-05 01:31:26 +03:00
|
|
|
|
|
|
|
install_old_hooks();
|
|
|
|
result = realloc(ptr, size);
|
|
|
|
save_old_hooks();
|
|
|
|
|
2007-03-27 13:49:06 +04:00
|
|
|
if (sCollector) {
|
|
|
|
sCollector->Freed(ptr);
|
|
|
|
sCollector->Allocated(result, size);
|
|
|
|
}
|
2007-01-05 01:31:26 +03:00
|
|
|
|
|
|
|
install_new_hooks();
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void*
|
|
|
|
my_memalign_hook(size_t size, size_t alignment, const void *caller)
|
|
|
|
{
|
|
|
|
void *result;
|
|
|
|
|
|
|
|
install_old_hooks();
|
|
|
|
result = memalign(size, alignment);
|
|
|
|
save_old_hooks();
|
|
|
|
|
2007-03-27 13:49:06 +04:00
|
|
|
if (sCollector)
|
|
|
|
sCollector->Allocated(result, size);
|
2007-01-05 01:31:26 +03:00
|
|
|
|
|
|
|
install_new_hooks();
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
my_free_hook (void *ptr, const void *caller)
|
|
|
|
{
|
|
|
|
install_old_hooks();
|
|
|
|
free(ptr);
|
|
|
|
save_old_hooks();
|
|
|
|
|
2007-03-27 13:49:06 +04:00
|
|
|
if (sCollector)
|
|
|
|
sCollector->Freed(ptr);
|
2007-01-05 01:31:26 +03:00
|
|
|
|
|
|
|
install_new_hooks();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void*
|
|
|
|
my_malloc_hook (size_t size, const void *caller)
|
|
|
|
{
|
2007-03-27 13:49:06 +04:00
|
|
|
void *result;
|
2007-01-05 01:31:26 +03:00
|
|
|
|
|
|
|
install_old_hooks();
|
|
|
|
result = malloc (size);
|
|
|
|
save_old_hooks();
|
|
|
|
|
2007-03-27 13:49:06 +04:00
|
|
|
if (sCollector)
|
|
|
|
sCollector->Allocated(result, size);
|
2007-01-05 01:31:26 +03:00
|
|
|
|
|
|
|
install_new_hooks();
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
InitMemHook(void)
|
|
|
|
{
|
|
|
|
if (!hookedMalloc) {
|
|
|
|
save_old_hooks();
|
|
|
|
install_new_hooks();
|
2011-10-17 18:59:28 +04:00
|
|
|
hookedMalloc = true;
|
2007-01-05 01:31:26 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#elif defined(WIN32)
|
2007-01-23 03:39:25 +03:00
|
|
|
#ifndef __MINGW32__
|
2007-01-05 01:31:26 +03:00
|
|
|
|
|
|
|
static int
|
|
|
|
AllocHook(int allocType, void *userData, size_t size, int
|
|
|
|
blockType, long requestNumber, const unsigned char *filename, int
|
|
|
|
lineNumber)
|
|
|
|
{
|
|
|
|
if (allocType == _HOOK_FREE)
|
2007-03-27 13:49:06 +04:00
|
|
|
sCollector->Freed(userData);
|
2007-01-05 01:31:26 +03:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void InitMemHook(void)
|
|
|
|
{
|
|
|
|
if (!hookedMalloc) {
|
|
|
|
_CrtSetAllocHook (AllocHook);
|
2011-10-17 18:59:28 +04:00
|
|
|
hookedMalloc = true;
|
2007-01-05 01:31:26 +03:00
|
|
|
}
|
|
|
|
}
|
2007-01-23 03:39:25 +03:00
|
|
|
#endif // __MINGW32__
|
2007-01-05 01:31:26 +03:00
|
|
|
|
|
|
|
#elif 0 // defined(XP_MACOSX)
|
|
|
|
|
|
|
|
#include <malloc/malloc.h>
|
|
|
|
|
|
|
|
static void (*old_free)(struct _malloc_zone_t *zone, void *ptr);
|
|
|
|
|
|
|
|
static void
|
|
|
|
freehook(struct _malloc_zone_t *zone, void *ptr)
|
|
|
|
{
|
2007-03-27 13:49:06 +04:00
|
|
|
if (sCollector)
|
|
|
|
sCollector->Freed(ptr);
|
2007-01-05 01:31:26 +03:00
|
|
|
old_free(zone, ptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
InitMemHook(void)
|
|
|
|
{
|
|
|
|
if (!hookedMalloc) {
|
|
|
|
malloc_zone_t *default_zone = malloc_default_zone();
|
|
|
|
old_free = default_zone->free;
|
|
|
|
default_zone->free = freehook;
|
2011-10-17 18:59:28 +04:00
|
|
|
hookedMalloc = true;
|
2007-01-05 01:31:26 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
static void
|
|
|
|
InitMemHook(void)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // GLIBC / WIN32 / OSX
|
2007-04-21 21:43:19 +04:00
|
|
|
#endif // DEBUG_CC
|
2007-01-05 01:31:26 +03:00
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////
|
|
|
|
// Collector implementation
|
|
|
|
////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
nsCycleCollector::nsCycleCollector() :
|
2011-10-17 18:59:28 +04:00
|
|
|
mCollectionInProgress(false),
|
|
|
|
mScanInProgress(false),
|
2008-02-25 20:47:25 +03:00
|
|
|
mCollectedObjects(0),
|
2008-02-15 16:12:55 +03:00
|
|
|
mWhiteNodes(nsnull),
|
|
|
|
mWhiteNodeCount(0),
|
2011-06-11 00:33:06 +04:00
|
|
|
mVisitedRefCounted(0),
|
|
|
|
mVisitedGCed(0),
|
2007-04-21 21:43:19 +04:00
|
|
|
#ifdef DEBUG_CC
|
2007-01-05 01:31:26 +03:00
|
|
|
mPurpleBuf(mParams, mStats),
|
|
|
|
mPtrLog(nsnull)
|
2007-04-21 21:43:19 +04:00
|
|
|
#else
|
|
|
|
mPurpleBuf(mParams)
|
|
|
|
#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
|
|
|
|
2007-03-27 13:49:06 +04:00
|
|
|
memset(mRuntimes, 0, sizeof(mRuntimes));
|
|
|
|
mRuntimes[nsIProgrammingLanguage::CPLUSPLUS] = &mXPCOMRuntime;
|
2007-01-05 01:31:26 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
nsCycleCollector::~nsCycleCollector()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
nsCycleCollector::RegisterRuntime(PRUint32 langID,
|
|
|
|
nsCycleCollectionLanguageRuntime *rt)
|
|
|
|
{
|
|
|
|
if (mParams.mDoNothing)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (langID > nsIProgrammingLanguage::MAX)
|
|
|
|
Fault("unknown language runtime in registration");
|
|
|
|
|
|
|
|
if (mRuntimes[langID])
|
2007-03-06 00:07:51 +03:00
|
|
|
Fault("multiple registrations of language runtime", rt);
|
2007-01-05 01:31:26 +03:00
|
|
|
|
|
|
|
mRuntimes[langID] = rt;
|
|
|
|
}
|
|
|
|
|
2009-07-09 05:10:29 +04:00
|
|
|
nsCycleCollectionLanguageRuntime *
|
|
|
|
nsCycleCollector::GetRuntime(PRUint32 langID)
|
|
|
|
{
|
|
|
|
if (langID > nsIProgrammingLanguage::MAX)
|
|
|
|
return nsnull;
|
|
|
|
|
|
|
|
return mRuntimes[langID];
|
|
|
|
}
|
2007-01-05 01:31:26 +03:00
|
|
|
|
|
|
|
void
|
|
|
|
nsCycleCollector::ForgetRuntime(PRUint32 langID)
|
|
|
|
{
|
|
|
|
if (mParams.mDoNothing)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (langID > nsIProgrammingLanguage::MAX)
|
|
|
|
Fault("unknown language runtime in deregistration");
|
|
|
|
|
|
|
|
if (! mRuntimes[langID])
|
|
|
|
Fault("forgetting non-registered language runtime");
|
|
|
|
|
|
|
|
mRuntimes[langID] = nsnull;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
if (sSuppressionList == nsnull) {
|
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;
|
|
|
|
}
|
|
|
|
|
2011-06-24 01:10:52 +04:00
|
|
|
NS_IMETHOD_(void) DescribeRefCountedNode(nsrefcnt refCount, size_t objSz,
|
|
|
|
const char *objName)
|
|
|
|
{
|
|
|
|
mSuppressThisNode = (PL_strstr(sSuppressionList, objName) != nsnull);
|
|
|
|
}
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
NS_IMETHOD_(void) DescribeGCedNode(bool isMarked, size_t objSz,
|
2011-06-24 01:10:52 +04:00
|
|
|
const char *objName)
|
2007-01-05 01:31:26 +03:00
|
|
|
{
|
|
|
|
mSuppressThisNode = (PL_strstr(sSuppressionList, objName) != nsnull);
|
|
|
|
}
|
|
|
|
|
2007-11-02 01:51:57 +03:00
|
|
|
NS_IMETHOD_(void) NoteXPCOMRoot(nsISupports *root) {};
|
|
|
|
NS_IMETHOD_(void) NoteRoot(PRUint32 langID, void *root,
|
|
|
|
nsCycleCollectionParticipant* participant) {};
|
2007-09-18 04:30:06 +04:00
|
|
|
NS_IMETHOD_(void) NoteXPCOMChild(nsISupports *child) {}
|
|
|
|
NS_IMETHOD_(void) NoteScriptChild(PRUint32 langID, void *child) {}
|
|
|
|
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) {}
|
2011-11-24 16:35:56 +04:00
|
|
|
NS_IMETHOD_(void) NoteWeakMapping(void *map, void *key, void *val) {}
|
2007-01-05 01:31:26 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
char *Suppressor::sSuppressionList = nsnull;
|
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
|
2007-05-24 18:10:02 +04:00
|
|
|
nsCycleCollector_isScanSafe(nsISupports *s)
|
|
|
|
{
|
|
|
|
if (!s)
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2007-05-24 18:10:02 +04:00
|
|
|
|
|
|
|
nsXPCOMCycleCollectionParticipant *cp;
|
|
|
|
ToParticipant(s, &cp);
|
|
|
|
|
|
|
|
return cp != nsnull;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool
|
2007-11-02 01:51:57 +03:00
|
|
|
nsCycleCollector::Suspect(nsISupports *n)
|
2007-01-05 01:31:26 +03:00
|
|
|
{
|
2010-03-03 03:04:34 +03:00
|
|
|
AbortIfOffMainThreadIfCheckFast();
|
2009-10-28 20:28:57 +03:00
|
|
|
|
2007-01-09 04:33:02 +03: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)
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2007-01-09 04:33:02 +03:00
|
|
|
|
2007-04-18 14:10:35 +04:00
|
|
|
NS_ASSERTION(nsCycleCollector_isScanSafe(n),
|
|
|
|
"suspected a non-scansafe pointer");
|
2007-01-05 01:31:26 +03:00
|
|
|
|
|
|
|
if (mParams.mDoNothing)
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2007-01-05 01:31:26 +03:00
|
|
|
|
2007-04-18 14:10:35 +04:00
|
|
|
#ifdef DEBUG_CC
|
|
|
|
mStats.mSuspectNode++;
|
2007-01-05 01:31:26 +03:00
|
|
|
|
|
|
|
if (nsCycleCollector_shouldSuppress(n))
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2007-01-05 01:31:26 +03:00
|
|
|
|
2007-04-18 14:10:35 +04:00
|
|
|
#ifndef __MINGW32__
|
2007-01-05 01:31:26 +03:00
|
|
|
if (mParams.mHookMalloc)
|
|
|
|
InitMemHook();
|
2007-01-23 03:39:25 +03:00
|
|
|
#endif
|
2007-01-05 01:31:26 +03:00
|
|
|
|
|
|
|
if (mParams.mLogPointers) {
|
|
|
|
if (!mPtrLog)
|
|
|
|
mPtrLog = fopen("pointer_log", "w");
|
2007-07-08 11:08:04 +04:00
|
|
|
fprintf(mPtrLog, "S %p\n", static_cast<void*>(n));
|
2007-01-05 01:31:26 +03:00
|
|
|
}
|
2007-04-18 14:10:35 +04:00
|
|
|
#endif
|
|
|
|
|
2009-05-07 00:46:04 +04:00
|
|
|
return mPurpleBuf.PutCompatObject(n);
|
2007-01-05 01:31:26 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool
|
2007-01-05 01:31:26 +03:00
|
|
|
nsCycleCollector::Forget(nsISupports *n)
|
|
|
|
{
|
2010-03-03 03:04:34 +03:00
|
|
|
AbortIfOffMainThreadIfCheckFast();
|
2009-10-28 20:28:57 +03:00
|
|
|
|
2007-01-09 04:33:02 +03: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;
|
2007-01-09 04:33:02 +03:00
|
|
|
|
2007-01-05 01:31:26 +03:00
|
|
|
if (mParams.mDoNothing)
|
2011-10-17 18:59:28 +04:00
|
|
|
return true; // it's as good as forgotten
|
2007-01-05 01:31:26 +03:00
|
|
|
|
2007-04-21 21:43:19 +04:00
|
|
|
#ifdef DEBUG_CC
|
2007-04-18 14:10:35 +04:00
|
|
|
mStats.mForgetNode++;
|
2007-01-05 01:31:26 +03:00
|
|
|
|
2007-01-23 03:39:25 +03:00
|
|
|
#ifndef __MINGW32__
|
2007-01-05 01:31:26 +03:00
|
|
|
if (mParams.mHookMalloc)
|
|
|
|
InitMemHook();
|
2007-01-23 03:39:25 +03:00
|
|
|
#endif
|
2007-01-05 01:31:26 +03:00
|
|
|
|
|
|
|
if (mParams.mLogPointers) {
|
|
|
|
if (!mPtrLog)
|
|
|
|
mPtrLog = fopen("pointer_log", "w");
|
2007-07-08 11:08:04 +04:00
|
|
|
fprintf(mPtrLog, "F %p\n", static_cast<void*>(n));
|
2007-01-05 01:31:26 +03:00
|
|
|
}
|
2007-04-18 14:10:35 +04:00
|
|
|
#endif
|
|
|
|
|
2009-05-07 00:46:04 +04:00
|
|
|
mPurpleBuf.RemoveCompatObject(n);
|
2011-10-17 18:59:28 +04:00
|
|
|
return true;
|
2009-05-07 00:46:04 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
nsPurpleBufferEntry*
|
|
|
|
nsCycleCollector::Suspect2(nsISupports *n)
|
|
|
|
{
|
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)
|
|
|
|
return nsnull;
|
|
|
|
|
|
|
|
NS_ASSERTION(nsCycleCollector_isScanSafe(n),
|
|
|
|
"suspected a non-scansafe pointer");
|
|
|
|
|
|
|
|
if (mParams.mDoNothing)
|
|
|
|
return nsnull;
|
|
|
|
|
|
|
|
#ifdef DEBUG_CC
|
|
|
|
mStats.mSuspectNode++;
|
|
|
|
|
|
|
|
if (nsCycleCollector_shouldSuppress(n))
|
|
|
|
return nsnull;
|
|
|
|
|
|
|
|
#ifndef __MINGW32__
|
|
|
|
if (mParams.mHookMalloc)
|
|
|
|
InitMemHook();
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (mParams.mLogPointers) {
|
|
|
|
if (!mPtrLog)
|
|
|
|
mPtrLog = fopen("pointer_log", "w");
|
|
|
|
fprintf(mPtrLog, "S %p\n", static_cast<void*>(n));
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// Caller is responsible for filling in result's mRefCnt.
|
|
|
|
return mPurpleBuf.Put(n);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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
|
|
|
|
mStats.mForgetNode++;
|
|
|
|
|
|
|
|
#ifndef __MINGW32__
|
|
|
|
if (mParams.mHookMalloc)
|
|
|
|
InitMemHook();
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (mParams.mLogPointers) {
|
|
|
|
if (!mPtrLog)
|
|
|
|
mPtrLog = fopen("pointer_log", "w");
|
|
|
|
fprintf(mPtrLog, "F %p\n", static_cast<void*>(e->mObject));
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
mPurpleBuf.Remove(e);
|
2011-10-17 18:59:28 +04:00
|
|
|
return true;
|
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
|
|
|
void
|
|
|
|
nsCycleCollector::Allocated(void *n, size_t sz)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsCycleCollector::Freed(void *n)
|
|
|
|
{
|
|
|
|
mStats.mFreeCalls++;
|
|
|
|
|
2007-03-06 22:25:10 +03:00
|
|
|
if (!n) {
|
|
|
|
// Ignore null pointers coming through
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2007-01-05 01:31:26 +03:00
|
|
|
if (mPurpleBuf.Exists(n)) {
|
|
|
|
mStats.mForgetNode++;
|
|
|
|
mStats.mFreedWhilePurple++;
|
|
|
|
Fault("freed while purple", n);
|
|
|
|
|
|
|
|
if (mParams.mLogPointers) {
|
|
|
|
if (!mPtrLog)
|
|
|
|
mPtrLog = fopen("pointer_log", "w");
|
|
|
|
fprintf(mPtrLog, "R %p\n", n);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
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
|
|
|
{
|
2011-06-14 00:24:23 +04:00
|
|
|
NS_ASSERTION(NS_IsMainThread(),
|
2011-06-16 02:53:00 +04:00
|
|
|
"nsCycleCollector::GCIfNeeded() must be called on the main thread.");
|
|
|
|
|
|
|
|
if (mParams.mDoNothing)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (!mRuntimes[nsIProgrammingLanguage::JAVASCRIPT])
|
|
|
|
return;
|
2011-06-14 00:24:23 +04:00
|
|
|
|
2011-06-16 02:53:00 +04:00
|
|
|
nsCycleCollectionJSRuntime* rt =
|
|
|
|
static_cast<nsCycleCollectionJSRuntime*>
|
|
|
|
(mRuntimes[nsIProgrammingLanguage::JAVASCRIPT]);
|
|
|
|
if (!rt->NeedCollect() && !aForceGC)
|
|
|
|
return;
|
|
|
|
|
|
|
|
#ifdef COLLECT_TIME_DEBUG
|
|
|
|
PRTime start = PR_Now();
|
|
|
|
#endif
|
|
|
|
// rt->Collect() must be called from the main thread,
|
|
|
|
// because it invokes XPCJSRuntime::GCCallback(cx, JSGC_BEGIN)
|
|
|
|
// which returns false if not in the main thread.
|
|
|
|
rt->Collect();
|
|
|
|
#ifdef COLLECT_TIME_DEBUG
|
|
|
|
printf("cc: GC() took %lldms\n", (PR_Now() - start) / PR_USEC_PER_MSEC);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool
|
2011-08-10 09:36:00 +04:00
|
|
|
nsCycleCollector::PrepareForCollection(nsTArray<PtrInfo*> *aWhiteNodes)
|
2011-06-16 02:53:00 +04:00
|
|
|
{
|
2007-04-21 21:43:19 +04:00
|
|
|
#if defined(DEBUG_CC) && !defined(__MINGW32__)
|
2007-03-16 15:52:47 +03:00
|
|
|
if (!mParams.mDoNothing && mParams.mHookMalloc)
|
|
|
|
InitMemHook();
|
2007-01-23 03:39:25 +03:00
|
|
|
#endif
|
2007-01-05 01:31:26 +03: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
|
|
|
|
2010-04-30 23:40:59 +04:00
|
|
|
NS_TIME_FUNCTION;
|
|
|
|
|
2007-03-20 02:21:31 +03:00
|
|
|
#ifdef COLLECT_TIME_DEBUG
|
2010-11-12 01:52:30 +03:00
|
|
|
printf("cc: nsCycleCollector::PrepareForCollection()\n");
|
2010-08-12 04:03:23 +04:00
|
|
|
#endif
|
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)
|
2007-05-11 00:21:12 +04:00
|
|
|
obs->NotifyObservers(nsnull, "cycle-collector-begin", nsnull);
|
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
mFollowupCollection = false;
|
2008-02-25 20:47:25 +03:00
|
|
|
mCollectedObjects = 0;
|
2010-07-20 10:31:01 +04:00
|
|
|
|
2010-11-12 01:52:30 +03:00
|
|
|
mWhiteNodes = aWhiteNodes;
|
2007-11-02 01:51:57 +03:00
|
|
|
|
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()
|
|
|
|
{
|
2008-02-15 16:12:55 +03:00
|
|
|
mWhiteNodes = nsnull;
|
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
|
|
|
|
|
2011-05-12 22:22:25 +04:00
|
|
|
PRUint32 interval((TimeStamp::Now() - mCollectionStart).ToMilliseconds());
|
2007-03-20 02:21:31 +03:00
|
|
|
#ifdef COLLECT_TIME_DEBUG
|
2011-05-12 22:22:25 +04:00
|
|
|
printf("cc: CleanupAfterCollection(), total time %ums\n", interval);
|
2007-03-20 02:21:31 +03:00
|
|
|
#endif
|
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);
|
2011-05-12 22:22:25 +04:00
|
|
|
|
2007-11-02 01:51:57 +03:00
|
|
|
#ifdef DEBUG_CC
|
|
|
|
ExplainLiveExpectedGarbage();
|
|
|
|
#endif
|
2010-11-12 01:52:30 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
PRUint32
|
|
|
|
nsCycleCollector::Collect(PRUint32 aTryCollections,
|
|
|
|
nsICycleCollectorListener *aListener)
|
|
|
|
{
|
2011-08-10 09:36:00 +04:00
|
|
|
nsAutoTArray<PtrInfo*, 4000> whiteNodes;
|
2010-11-12 01:52:30 +03:00
|
|
|
|
2011-06-16 02:53:00 +04:00
|
|
|
if (!PrepareForCollection(&whiteNodes))
|
2010-11-12 01:52:30 +03:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
PRUint32 totalCollections = 0;
|
|
|
|
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()))
|
|
|
|
aListener = nsnull;
|
2011-06-22 21:41:17 +04:00
|
|
|
if (!(BeginCollection(aListener) &&
|
|
|
|
FinishCollection(aListener)))
|
2010-11-12 01:52:30 +03:00
|
|
|
break;
|
|
|
|
|
|
|
|
++totalCollections;
|
|
|
|
}
|
|
|
|
|
|
|
|
CleanupAfterCollection();
|
|
|
|
|
2008-02-25 20:47:25 +03:00
|
|
|
return mCollectedObjects;
|
2007-11-02 01:51:57 +03:00
|
|
|
}
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool
|
2011-06-14 00:24:23 +04:00
|
|
|
nsCycleCollector::BeginCollection(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
|
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
|
|
|
|
2010-08-12 04:03:23 +04:00
|
|
|
GCGraphBuilder builder(mGraph, mRuntimes, aListener);
|
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
|
|
|
|
2007-03-20 02:21:31 +03:00
|
|
|
#ifdef COLLECT_TIME_DEBUG
|
2007-11-02 01:51:57 +03:00
|
|
|
PRTime now = PR_Now();
|
2007-03-20 02:21:31 +03:00
|
|
|
#endif
|
2007-11-02 01:51:57 +03:00
|
|
|
for (PRUint32 i = 0; i <= nsIProgrammingLanguage::MAX; ++i) {
|
|
|
|
if (mRuntimes[i])
|
2011-10-17 18:59:28 +04:00
|
|
|
mRuntimes[i]->BeginCycleCollection(builder, false);
|
2007-11-02 01:51:57 +03:00
|
|
|
}
|
2007-03-20 02:21:31 +03:00
|
|
|
|
|
|
|
#ifdef COLLECT_TIME_DEBUG
|
2007-11-02 01:51:57 +03:00
|
|
|
printf("cc: mRuntimes[*]->BeginCycleCollection() took %lldms\n",
|
|
|
|
(PR_Now() - now) / PR_USEC_PER_MSEC);
|
|
|
|
|
|
|
|
now = PR_Now();
|
2007-03-20 02:21:31 +03:00
|
|
|
#endif
|
|
|
|
|
2007-06-07 02:06:56 +04:00
|
|
|
#ifdef DEBUG_CC
|
2008-02-15 16:12:55 +03:00
|
|
|
PRUint32 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
|
2008-02-15 16:12:55 +03:00
|
|
|
PRUint32 purpleEnd = builder.Count();
|
|
|
|
|
|
|
|
if (purpleStart != purpleEnd) {
|
|
|
|
#ifndef __MINGW32__
|
|
|
|
if (mParams.mHookMalloc)
|
|
|
|
InitMemHook();
|
|
|
|
#endif
|
|
|
|
if (mParams.mLogPointers && !mPtrLog)
|
|
|
|
mPtrLog = fopen("pointer_log", "w");
|
|
|
|
|
|
|
|
PRUint32 i = 0;
|
|
|
|
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
|
|
|
|
2007-03-20 02:21:31 +03:00
|
|
|
#ifdef COLLECT_TIME_DEBUG
|
2007-11-02 01:51:57 +03:00
|
|
|
printf("cc: SelectPurple() took %lldms\n",
|
|
|
|
(PR_Now() - now) / PR_USEC_PER_MSEC);
|
2007-03-20 02:21:31 +03:00
|
|
|
#endif
|
|
|
|
|
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
|
|
|
|
2007-03-20 02:21:31 +03:00
|
|
|
#ifdef COLLECT_TIME_DEBUG
|
2007-11-02 01:51:57 +03:00
|
|
|
now = PR_Now();
|
2007-03-20 02:21:31 +03:00
|
|
|
#endif
|
2007-11-02 01:51:57 +03:00
|
|
|
|
2008-01-10 17:10:03 +03:00
|
|
|
MarkRoots(builder);
|
2007-03-20 02:21:31 +03:00
|
|
|
|
|
|
|
#ifdef COLLECT_TIME_DEBUG
|
2007-11-02 01:51:57 +03:00
|
|
|
{
|
|
|
|
PRTime then = PR_Now();
|
|
|
|
printf("cc: MarkRoots() took %lldms\n",
|
|
|
|
(then - now) / PR_USEC_PER_MSEC);
|
|
|
|
now = then;
|
|
|
|
}
|
2007-03-20 02:21:31 +03:00
|
|
|
#endif
|
|
|
|
|
2008-01-10 17:10:03 +03:00
|
|
|
ScanRoots();
|
2007-03-16 15:52:47 +03:00
|
|
|
|
2007-03-20 02:21:31 +03:00
|
|
|
#ifdef COLLECT_TIME_DEBUG
|
2007-11-02 01:51:57 +03:00
|
|
|
printf("cc: ScanRoots() took %lldms\n",
|
|
|
|
(PR_Now() - now) / PR_USEC_PER_MSEC);
|
2007-03-20 02:21:31 +03:00
|
|
|
#endif
|
|
|
|
|
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 &&
|
|
|
|
pi->mRefCount > 0 && pi->mRefCount < PR_UINT32_MAX &&
|
|
|
|
pi->mInternalRefs != pi->mRefCount) {
|
|
|
|
aListener->DescribeRoot((PRUint64)pi->mPointer,
|
|
|
|
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) {
|
|
|
|
PRUint32 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
|
|
|
|
2010-11-12 01:52:30 +03:00
|
|
|
for (PRUint32 i = 0; i <= nsIProgrammingLanguage::MAX; ++i) {
|
|
|
|
if (mRuntimes[i])
|
|
|
|
mRuntimes[i]->FinishTraverse();
|
|
|
|
}
|
2007-11-02 01:51:57 +03:00
|
|
|
}
|
2008-02-15 16:12:55 +03: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
|
|
|
{
|
2008-02-25 20:47:25 +03:00
|
|
|
#ifdef COLLECT_TIME_DEBUG
|
|
|
|
PRTime now = PR_Now();
|
|
|
|
#endif
|
2010-11-12 01:52:30 +03:00
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool collected = CollectWhite(aListener);
|
2008-01-10 17:10:03 +03:00
|
|
|
|
2008-02-25 20:47:25 +03:00
|
|
|
#ifdef COLLECT_TIME_DEBUG
|
|
|
|
printf("cc: CollectWhite() took %lldms\n",
|
|
|
|
(PR_Now() - now) / PR_USEC_PER_MSEC);
|
|
|
|
#endif
|
|
|
|
|
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
|
|
|
|
2007-11-02 01:51:57 +03:00
|
|
|
for (PRUint32 i = 0; i <= nsIProgrammingLanguage::MAX; ++i) {
|
|
|
|
if (mRuntimes[i])
|
|
|
|
mRuntimes[i]->FinishCycleCollection();
|
2007-03-16 15:52:47 +03:00
|
|
|
}
|
2007-03-20 02:21:31 +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
|
|
|
|
// We wait until after FinishCollection to check the white nodes because
|
|
|
|
// some objects may outlive CollectWhite but then be freed by
|
|
|
|
// FinishCycleCollection (like XPConnect's deferred release of native
|
|
|
|
// objects).
|
|
|
|
PRUint32 i, count = mWhiteNodes->Length();
|
|
|
|
for (i = 0; i < count; ++i) {
|
|
|
|
PtrInfo *pinfo = mWhiteNodes->ElementAt(i);
|
|
|
|
if (pinfo->mLangID == nsIProgrammingLanguage::CPLUSPLUS &&
|
|
|
|
mPurpleBuf.Exists(pinfo->mPointer)) {
|
|
|
|
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();
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2008-02-25 20:47:25 +03:00
|
|
|
PRUint32
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
Collect(SHUTDOWN_COLLECTIONS(mParams), listener);
|
2007-03-16 15:52:47 +03:00
|
|
|
|
2007-04-21 21:43:19 +04:00
|
|
|
#ifdef DEBUG_CC
|
2010-08-12 04:03:23 +04:00
|
|
|
GCGraphBuilder builder(mGraph, mRuntimes, nsnull);
|
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
|
|
|
|
2008-10-10 19:04:34 +04:00
|
|
|
static PLDHashOperator
|
2007-04-20 12:01:01 +04:00
|
|
|
AddExpectedGarbage(nsVoidPtrHashKey *p, void *arg)
|
2007-03-10 02:00:14 +03:00
|
|
|
{
|
2008-02-15 16:12:55 +03:00
|
|
|
GCGraphBuilder *builder = static_cast<GCGraphBuilder*>(arg);
|
|
|
|
nsISupports *root =
|
|
|
|
static_cast<nsISupports*>(const_cast<void*>(p->GetKey()));
|
|
|
|
builder->NoteXPCOMRoot(root);
|
2007-03-10 02:00:14 +03:00
|
|
|
return PL_DHASH_NEXT;
|
|
|
|
}
|
|
|
|
|
2010-01-12 19:51:39 +03:00
|
|
|
struct SetSCCVisitor
|
2007-06-26 00:36:24 +04:00
|
|
|
{
|
2010-01-12 19:51:39 +03:00
|
|
|
SetSCCVisitor(PRUint32 aIndex) : mIndex(aIndex) {}
|
2011-09-29 10:19:26 +04:00
|
|
|
bool ShouldVisitNode(PtrInfo const *pi) { return pi->mSCCIndex == 0; }
|
2007-06-26 00:36:24 +04:00
|
|
|
void VisitNode(PtrInfo *pi) { pi->mSCCIndex = mIndex; }
|
|
|
|
private:
|
|
|
|
PRUint32 mIndex;
|
|
|
|
};
|
|
|
|
|
2010-01-12 19:51:39 +03:00
|
|
|
struct SetNonRootGreyVisitor
|
2007-06-26 00:36:24 +04:00
|
|
|
{
|
2011-09-29 10:19:26 +04:00
|
|
|
bool ShouldVisitNode(PtrInfo const *pi) { return pi->mColor == white; }
|
2007-06-26 00:36:24 +04:00
|
|
|
void VisitNode(PtrInfo *pi) { pi->mColor = grey; }
|
|
|
|
};
|
|
|
|
|
2008-03-27 20:09:51 +03:00
|
|
|
static void
|
|
|
|
PrintPathToExpectedGarbage(PtrInfo *pi)
|
|
|
|
{
|
|
|
|
printf(" An object expected to be garbage could be "
|
|
|
|
"reached from it by the path:\n");
|
|
|
|
for (PtrInfo *path = pi, *prev = nsnull; prev != path;
|
|
|
|
prev = path,
|
|
|
|
path = path->mShortestPathToExpectedGarbage) {
|
|
|
|
if (prev) {
|
|
|
|
nsCString *edgeName = prev
|
|
|
|
->mShortestPathToExpectedGarbageEdgeName;
|
|
|
|
printf(" via %s\n",
|
|
|
|
edgeName->IsEmpty() ? "<unknown edge>"
|
|
|
|
: edgeName->get());
|
|
|
|
}
|
|
|
|
printf(" %s %p\n", path->mName, path->mPointer);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-03-10 02:00:14 +03:00
|
|
|
void
|
|
|
|
nsCycleCollector::ExplainLiveExpectedGarbage()
|
|
|
|
{
|
|
|
|
if (mScanInProgress || mCollectionInProgress)
|
|
|
|
Fault("can't explain expected garbage during collection itself");
|
|
|
|
|
|
|
|
if (mParams.mDoNothing) {
|
|
|
|
printf("nsCycleCollector: not explaining expected garbage since\n"
|
|
|
|
" cycle collection disabled\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
mCollectionInProgress = true;
|
|
|
|
mScanInProgress = true;
|
2007-03-10 02:00:14 +03:00
|
|
|
|
2007-04-26 01:12:11 +04:00
|
|
|
{
|
2010-08-12 04:03:23 +04:00
|
|
|
GCGraphBuilder builder(mGraph, mRuntimes, nsnull);
|
2007-11-02 01:51:57 +03:00
|
|
|
|
2009-04-25 02:45:34 +04:00
|
|
|
// Instead of adding roots from the purple buffer, we add them
|
|
|
|
// from the list of nodes we were expected to collect.
|
|
|
|
// Put the expected garbage in *before* calling
|
|
|
|
// BeginCycleCollection so that we can separate the expected
|
|
|
|
// garbage from the NoteRoot calls in such a way that something
|
|
|
|
// that's in both is considered expected garbage.
|
|
|
|
mExpectedGarbage.EnumerateEntries(&AddExpectedGarbage, &builder);
|
|
|
|
|
|
|
|
PRUint32 expectedGarbageCount = builder.Count();
|
|
|
|
|
2007-11-02 01:51:57 +03:00
|
|
|
for (PRUint32 i = 0; i <= nsIProgrammingLanguage::MAX; ++i) {
|
|
|
|
if (mRuntimes[i])
|
2011-10-17 18:59:28 +04:00
|
|
|
mRuntimes[i]->BeginCycleCollection(builder, true);
|
2007-11-02 01:51:57 +03:00
|
|
|
}
|
|
|
|
|
2009-04-25 02:45:34 +04:00
|
|
|
// But just for extra information, add entries from the purple
|
|
|
|
// buffer too, since it may give us extra information about
|
|
|
|
// traversal deficiencies.
|
|
|
|
mPurpleBuf.NoteAll(builder);
|
|
|
|
|
2008-01-10 17:10:03 +03:00
|
|
|
MarkRoots(builder);
|
|
|
|
ScanRoots();
|
2007-03-10 02:00:14 +03:00
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
mScanInProgress = false;
|
2007-03-10 02:00:14 +03:00
|
|
|
|
2011-02-10 04:34:22 +03:00
|
|
|
for (PRUint32 i = 0; i <= nsIProgrammingLanguage::MAX; ++i) {
|
|
|
|
if (mRuntimes[i]) {
|
|
|
|
mRuntimes[i]->FinishTraverse();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool describeExtraRefcounts = false;
|
|
|
|
bool findCycleRoots = false;
|
2007-06-07 02:06:00 +04:00
|
|
|
{
|
2008-01-10 17:10:03 +03:00
|
|
|
NodePool::Enumerator queue(mGraph.mNodes);
|
2007-07-08 23:26:44 +04:00
|
|
|
PRUint32 i = 0;
|
2007-06-07 02:06:00 +04:00
|
|
|
while (!queue.IsDone()) {
|
|
|
|
PtrInfo *pi = queue.GetNext();
|
|
|
|
if (pi->mColor == white) {
|
2011-10-17 18:59:28 +04:00
|
|
|
findCycleRoots = true;
|
2007-06-07 02:06:00 +04:00
|
|
|
}
|
|
|
|
|
2009-04-25 02:45:34 +04:00
|
|
|
if (pi->mInternalRefs != pi->mRefCount &&
|
|
|
|
(i < expectedGarbageCount || i >= mGraph.mRootCount)) {
|
|
|
|
// This check isn't particularly useful anymore
|
|
|
|
// given that we need to enter this part for i >=
|
|
|
|
// mGraph.mRootCount and there are plenty of
|
|
|
|
// NoteRoot roots.
|
2011-10-17 18:59:28 +04:00
|
|
|
describeExtraRefcounts = true;
|
2007-06-07 02:06:00 +04:00
|
|
|
}
|
2007-07-08 23:26:44 +04:00
|
|
|
++i;
|
2007-04-26 01:12:11 +04:00
|
|
|
}
|
2007-06-07 02:06:00 +04:00
|
|
|
}
|
2007-03-10 02:00:14 +03:00
|
|
|
|
2008-03-27 20:09:51 +03:00
|
|
|
if ((describeExtraRefcounts || findCycleRoots) &&
|
|
|
|
CreateReversedEdges()) {
|
2007-07-06 02:20:16 +04:00
|
|
|
// Note that the external references may have been external
|
|
|
|
// to a different node in the cycle collection that just
|
|
|
|
// happened, if that different node was purple and then
|
|
|
|
// black.
|
|
|
|
|
|
|
|
// Use mSCCIndex temporarily to track whether we've reached
|
|
|
|
// nodes in the breadth-first search.
|
|
|
|
const PRUint32 INDEX_UNREACHED = 0;
|
|
|
|
const PRUint32 INDEX_REACHED = 1;
|
2008-01-10 17:10:03 +03:00
|
|
|
NodePool::Enumerator etor_clear(mGraph.mNodes);
|
2007-07-06 02:20:16 +04:00
|
|
|
while (!etor_clear.IsDone()) {
|
|
|
|
PtrInfo *pi = etor_clear.GetNext();
|
|
|
|
pi->mSCCIndex = INDEX_UNREACHED;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsDeque queue; // for breadth-first search
|
2008-01-10 17:10:03 +03:00
|
|
|
NodePool::Enumerator etor_roots(mGraph.mNodes);
|
|
|
|
for (PRUint32 i = 0; i < mGraph.mRootCount; ++i) {
|
2007-07-06 02:20:16 +04:00
|
|
|
PtrInfo *root_pi = etor_roots.GetNext();
|
2009-04-25 02:45:34 +04:00
|
|
|
if (i < expectedGarbageCount) {
|
2007-08-23 04:47:47 +04:00
|
|
|
root_pi->mSCCIndex = INDEX_REACHED;
|
|
|
|
root_pi->mShortestPathToExpectedGarbage = root_pi;
|
|
|
|
queue.Push(root_pi);
|
|
|
|
}
|
2007-07-06 02:20:16 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
while (queue.GetSize() > 0) {
|
|
|
|
PtrInfo *pi = (PtrInfo*)queue.PopFront();
|
|
|
|
for (ReversedEdge *e = pi->mReversedEdges; e; e = e->mNext) {
|
|
|
|
if (e->mTarget->mSCCIndex == INDEX_UNREACHED) {
|
|
|
|
e->mTarget->mSCCIndex = INDEX_REACHED;
|
2007-08-23 04:47:47 +04:00
|
|
|
PtrInfo *target = e->mTarget;
|
2008-03-18 02:11:08 +03:00
|
|
|
if (!target->mShortestPathToExpectedGarbage) {
|
2007-08-23 04:47:47 +04:00
|
|
|
target->mShortestPathToExpectedGarbage = pi;
|
2008-03-18 02:11:08 +03:00
|
|
|
target->mShortestPathToExpectedGarbageEdgeName =
|
|
|
|
e->mEdgeName;
|
|
|
|
}
|
2007-08-23 04:47:47 +04:00
|
|
|
queue.Push(target);
|
2007-07-06 02:20:16 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-11-02 01:51:57 +03:00
|
|
|
if (pi->mRefCount == PR_UINT32_MAX ||
|
|
|
|
(pi->mInternalRefs != pi->mRefCount && pi->mRefCount > 0)) {
|
|
|
|
if (pi->mRefCount == PR_UINT32_MAX) {
|
|
|
|
printf("nsCycleCollector: %s %p was not collected due "
|
|
|
|
"to \n"
|
|
|
|
" external references\n",
|
|
|
|
pi->mName, pi->mPointer);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
printf("nsCycleCollector: %s %p was not collected due "
|
|
|
|
"to %d\n"
|
|
|
|
" external references (%d total - %d known)\n",
|
|
|
|
pi->mName, pi->mPointer,
|
|
|
|
pi->mRefCount - pi->mInternalRefs,
|
|
|
|
pi->mRefCount, pi->mInternalRefs);
|
|
|
|
}
|
2007-08-23 04:47:47 +04:00
|
|
|
|
2008-03-27 20:09:51 +03:00
|
|
|
PrintPathToExpectedGarbage(pi);
|
2007-08-23 04:47:47 +04:00
|
|
|
|
2007-11-02 01:51:57 +03:00
|
|
|
if (pi->mRefCount == PR_UINT32_MAX) {
|
|
|
|
printf(" The known references to it were from:\n");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
printf(" The %d known references to it were from:\n",
|
|
|
|
pi->mInternalRefs);
|
|
|
|
}
|
2007-07-06 02:20:16 +04:00
|
|
|
for (ReversedEdge *e = pi->mReversedEdges;
|
|
|
|
e; e = e->mNext) {
|
2008-03-18 02:11:08 +03:00
|
|
|
printf(" %s %p",
|
2007-07-06 02:20:16 +04:00
|
|
|
e->mTarget->mName, e->mTarget->mPointer);
|
2008-03-18 02:11:08 +03:00
|
|
|
if (!e->mEdgeName->IsEmpty()) {
|
|
|
|
printf(" via %s", e->mEdgeName->get());
|
|
|
|
}
|
|
|
|
printf("\n");
|
2007-06-07 02:06:00 +04:00
|
|
|
}
|
2007-08-23 04:46:43 +04:00
|
|
|
mRuntimes[pi->mLangID]->PrintAllReferencesTo(pi->mPointer);
|
2007-06-07 02:06:00 +04:00
|
|
|
}
|
2007-04-26 01:12:11 +04:00
|
|
|
}
|
2007-07-06 02:20:16 +04:00
|
|
|
|
2008-03-27 20:09:51 +03:00
|
|
|
if (findCycleRoots) {
|
|
|
|
// NOTE: This code changes the white nodes that are not
|
|
|
|
// roots to gray.
|
2007-06-26 00:36:24 +04:00
|
|
|
|
2008-03-27 20:09:51 +03:00
|
|
|
// Put the nodes in post-order traversal order from a
|
|
|
|
// depth-first search.
|
|
|
|
nsDeque DFSPostOrder;
|
2007-06-26 00:36:24 +04:00
|
|
|
|
2008-03-27 20:09:51 +03:00
|
|
|
{
|
|
|
|
// Use mSCCIndex temporarily to track the DFS numbering:
|
|
|
|
const PRUint32 INDEX_UNREACHED = 0;
|
|
|
|
const PRUint32 INDEX_TRAVERSING = 1;
|
|
|
|
const PRUint32 INDEX_NUMBERED = 2;
|
2007-06-26 00:36:24 +04:00
|
|
|
|
2008-03-27 20:09:51 +03:00
|
|
|
NodePool::Enumerator etor_clear(mGraph.mNodes);
|
|
|
|
while (!etor_clear.IsDone()) {
|
|
|
|
PtrInfo *pi = etor_clear.GetNext();
|
|
|
|
pi->mSCCIndex = INDEX_UNREACHED;
|
|
|
|
}
|
2007-06-26 00:36:24 +04:00
|
|
|
|
2008-03-27 20:09:51 +03:00
|
|
|
nsDeque stack;
|
2007-06-26 00:36:24 +04:00
|
|
|
|
2008-03-27 20:09:51 +03:00
|
|
|
NodePool::Enumerator etor_roots(mGraph.mNodes);
|
|
|
|
for (PRUint32 i = 0; i < mGraph.mRootCount; ++i) {
|
|
|
|
PtrInfo *root_pi = etor_roots.GetNext();
|
|
|
|
stack.Push(root_pi);
|
|
|
|
}
|
2007-06-26 00:36:24 +04:00
|
|
|
|
2008-03-27 20:09:51 +03:00
|
|
|
while (stack.GetSize() > 0) {
|
|
|
|
PtrInfo *pi = (PtrInfo*)stack.Peek();
|
|
|
|
if (pi->mSCCIndex == INDEX_UNREACHED) {
|
|
|
|
pi->mSCCIndex = INDEX_TRAVERSING;
|
2011-06-10 01:55:04 +04:00
|
|
|
for (EdgePool::Iterator child = pi->FirstChild(),
|
|
|
|
child_end = pi->LastChild();
|
2008-03-27 20:09:51 +03:00
|
|
|
child != child_end; ++child) {
|
|
|
|
stack.Push(*child);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
stack.Pop();
|
|
|
|
// Somebody else might have numbered it already
|
|
|
|
// (since this is depth-first, not breadth-first).
|
|
|
|
// This happens if a node is pushed on the stack
|
|
|
|
// a second time while it is on the stack in
|
|
|
|
// UNREACHED state.
|
|
|
|
if (pi->mSCCIndex == INDEX_TRAVERSING) {
|
|
|
|
pi->mSCCIndex = INDEX_NUMBERED;
|
|
|
|
DFSPostOrder.Push(pi);
|
|
|
|
}
|
2007-06-26 00:36:24 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-03-27 20:09:51 +03:00
|
|
|
// Put the nodes into strongly-connected components.
|
|
|
|
{
|
|
|
|
NodePool::Enumerator etor_clear(mGraph.mNodes);
|
|
|
|
while (!etor_clear.IsDone()) {
|
|
|
|
PtrInfo *pi = etor_clear.GetNext();
|
|
|
|
pi->mSCCIndex = 0;
|
|
|
|
}
|
2007-06-26 00:36:24 +04:00
|
|
|
|
2008-03-27 20:09:51 +03:00
|
|
|
PRUint32 currentSCC = 1;
|
2007-06-26 00:36:24 +04:00
|
|
|
|
2008-03-27 20:09:51 +03:00
|
|
|
while (DFSPostOrder.GetSize() > 0) {
|
2010-01-12 19:51:39 +03:00
|
|
|
GraphWalker<SetSCCVisitor>(SetSCCVisitor(currentSCC)).Walk((PtrInfo*)DFSPostOrder.PopFront());
|
2008-03-27 20:09:51 +03:00
|
|
|
++currentSCC;
|
|
|
|
}
|
2007-06-26 00:36:24 +04:00
|
|
|
}
|
|
|
|
|
2008-03-27 20:09:51 +03:00
|
|
|
// Mark any white nodes reachable from other components as
|
|
|
|
// grey.
|
|
|
|
{
|
|
|
|
NodePool::Enumerator queue(mGraph.mNodes);
|
|
|
|
while (!queue.IsDone()) {
|
|
|
|
PtrInfo *pi = queue.GetNext();
|
|
|
|
if (pi->mColor != white)
|
|
|
|
continue;
|
2011-06-10 01:55:04 +04:00
|
|
|
for (EdgePool::Iterator child = pi->FirstChild(),
|
|
|
|
child_end = pi->LastChild();
|
2008-03-27 20:09:51 +03:00
|
|
|
child != child_end; ++child) {
|
|
|
|
if ((*child)->mSCCIndex != pi->mSCCIndex) {
|
2010-01-12 19:51:39 +03:00
|
|
|
GraphWalker<SetNonRootGreyVisitor>(SetNonRootGreyVisitor()).Walk(*child);
|
2008-03-27 20:09:51 +03:00
|
|
|
}
|
2007-06-26 00:36:24 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-03-27 20:09:51 +03:00
|
|
|
{
|
|
|
|
NodePool::Enumerator queue(mGraph.mNodes);
|
|
|
|
while (!queue.IsDone()) {
|
|
|
|
PtrInfo *pi = queue.GetNext();
|
|
|
|
if (pi->mColor == white) {
|
2009-05-07 00:46:04 +04:00
|
|
|
if (pi->mLangID ==
|
|
|
|
nsIProgrammingLanguage::CPLUSPLUS &&
|
|
|
|
mPurpleBuf.Exists(pi->mPointer)) {
|
2009-04-25 02:45:34 +04:00
|
|
|
printf(
|
|
|
|
"nsCycleCollector: %s %p in component %d\n"
|
|
|
|
" which was reference counted during the root/unlink/unroot phase of the\n"
|
|
|
|
" last collection was not collected due to failure to unlink (see other\n"
|
|
|
|
" warnings) or deficiency in traverse that causes cycles referenced only\n"
|
|
|
|
" from other cycles to require multiple rounds of cycle collection in which\n"
|
|
|
|
" this object was likely the reachable object\n",
|
|
|
|
pi->mName, pi->mPointer, pi->mSCCIndex);
|
|
|
|
} else {
|
|
|
|
printf(
|
|
|
|
"nsCycleCollector: %s %p in component %d\n"
|
|
|
|
" was not collected due to missing call to suspect, failure to unlink (see\n"
|
|
|
|
" other warnings), or deficiency in traverse that causes cycles referenced\n"
|
|
|
|
" only from other cycles to require multiple rounds of cycle collection\n",
|
|
|
|
pi->mName, pi->mPointer, pi->mSCCIndex);
|
|
|
|
}
|
2008-03-27 20:09:51 +03:00
|
|
|
if (pi->mShortestPathToExpectedGarbage)
|
|
|
|
PrintPathToExpectedGarbage(pi);
|
|
|
|
}
|
2007-06-26 00:36:24 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2008-03-27 20:09:51 +03:00
|
|
|
|
|
|
|
DestroyReversedEdges();
|
2007-06-26 00:36:24 +04:00
|
|
|
}
|
2007-04-26 01:12:11 +04:00
|
|
|
}
|
2007-03-10 02:00:14 +03:00
|
|
|
|
2008-01-10 17:10:03 +03:00
|
|
|
ClearGraph();
|
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
mCollectionInProgress = false;
|
2007-03-10 02:00:14 +03:00
|
|
|
|
|
|
|
for (PRUint32 i = 0; i <= nsIProgrammingLanguage::MAX; ++i) {
|
|
|
|
if (mRuntimes[i])
|
|
|
|
mRuntimes[i]->FinishCycleCollection();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool
|
2008-01-10 17:10:03 +03:00
|
|
|
nsCycleCollector::CreateReversedEdges()
|
2007-07-06 02:20:16 +04:00
|
|
|
{
|
|
|
|
// Count the edges in the graph.
|
|
|
|
PRUint32 edgeCount = 0;
|
2008-01-10 17:10:03 +03:00
|
|
|
NodePool::Enumerator countQueue(mGraph.mNodes);
|
2007-07-06 02:20:16 +04:00
|
|
|
while (!countQueue.IsDone()) {
|
|
|
|
PtrInfo *pi = countQueue.GetNext();
|
2011-06-10 01:55:04 +04:00
|
|
|
for (EdgePool::Iterator e = pi->FirstChild(), e_end = pi->LastChild();
|
2007-07-06 02:20:16 +04:00
|
|
|
e != e_end; ++e, ++edgeCount) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Allocate a pool to hold all of the edges.
|
2008-01-10 17:10:03 +03:00
|
|
|
mGraph.mReversedEdges = new ReversedEdge[edgeCount];
|
|
|
|
if (mGraph.mReversedEdges == nsnull) {
|
2007-07-06 02:20:16 +04:00
|
|
|
NS_NOTREACHED("allocation failure creating reversed edges");
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2007-07-06 02:20:16 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Fill in the reversed edges by scanning all forward edges.
|
2008-01-10 17:10:03 +03:00
|
|
|
ReversedEdge *current = mGraph.mReversedEdges;
|
|
|
|
NodePool::Enumerator buildQueue(mGraph.mNodes);
|
2007-07-06 02:20:16 +04:00
|
|
|
while (!buildQueue.IsDone()) {
|
|
|
|
PtrInfo *pi = buildQueue.GetNext();
|
2008-03-18 02:11:08 +03:00
|
|
|
PRInt32 i = 0;
|
2011-06-10 01:55:04 +04:00
|
|
|
for (EdgePool::Iterator e = pi->FirstChild(), e_end = pi->LastChild();
|
2007-07-06 02:20:16 +04:00
|
|
|
e != e_end; ++e) {
|
|
|
|
current->mTarget = pi;
|
2009-01-30 10:16:16 +03:00
|
|
|
current->mEdgeName = &pi->mEdgeNames[i];
|
2007-07-06 02:20:16 +04:00
|
|
|
current->mNext = (*e)->mReversedEdges;
|
|
|
|
(*e)->mReversedEdges = current;
|
|
|
|
++current;
|
2008-03-18 02:11:08 +03:00
|
|
|
++i;
|
2007-07-06 02:20:16 +04:00
|
|
|
}
|
|
|
|
}
|
2008-02-08 22:48:51 +03:00
|
|
|
NS_ASSERTION(current - mGraph.mReversedEdges == ptrdiff_t(edgeCount),
|
|
|
|
"misallocation");
|
2011-10-17 18:59:28 +04:00
|
|
|
return true;
|
2007-07-06 02:20:16 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2008-01-10 17:10:03 +03:00
|
|
|
nsCycleCollector::DestroyReversedEdges()
|
2007-07-06 02:20:16 +04:00
|
|
|
{
|
2008-01-10 17:10:03 +03:00
|
|
|
NodePool::Enumerator queue(mGraph.mNodes);
|
2007-07-06 02:20:16 +04:00
|
|
|
while (!queue.IsDone()) {
|
|
|
|
PtrInfo *pi = queue.GetNext();
|
|
|
|
pi->mReversedEdges = nsnull;
|
|
|
|
}
|
|
|
|
|
2008-01-10 17:10:03 +03:00
|
|
|
delete mGraph.mReversedEdges;
|
|
|
|
mGraph.mReversedEdges = nsnull;
|
2007-07-06 02:20:16 +04:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
////////////////////////
|
|
|
|
|
|
|
|
static PRInt64
|
|
|
|
ReportCycleCollectorMem()
|
|
|
|
{
|
|
|
|
if (!sCollector)
|
|
|
|
return 0;
|
|
|
|
PRInt64 size = sizeof(nsCycleCollector) +
|
|
|
|
sCollector->mPurpleBuf.BlocksSize() +
|
|
|
|
sCollector->mGraph.BlocksSize();
|
|
|
|
if (sCollector->mWhiteNodes)
|
|
|
|
size += sCollector->mWhiteNodes->Capacity() * sizeof(PtrInfo*);
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_MEMORY_REPORTER_IMPLEMENT(CycleCollector,
|
|
|
|
"explicit/cycle-collector",
|
|
|
|
KIND_HEAP,
|
|
|
|
UNITS_BYTES,
|
|
|
|
ReportCycleCollectorMem,
|
|
|
|
"Memory used by the cycle collector. This "
|
|
|
|
"includes the cycle collector structure, the "
|
|
|
|
"purple buffer, the graph, and the white nodes. "
|
|
|
|
"The latter two are expected to be empty when the "
|
|
|
|
"cycle collector is idle.")
|
|
|
|
|
|
|
|
|
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.
|
|
|
|
////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
void
|
|
|
|
nsCycleCollector_registerRuntime(PRUint32 langID,
|
|
|
|
nsCycleCollectionLanguageRuntime *rt)
|
|
|
|
{
|
2011-09-29 10:19:26 +04:00
|
|
|
static bool regMemReport = true;
|
2007-03-27 13:49:06 +04:00
|
|
|
if (sCollector)
|
|
|
|
sCollector->RegisterRuntime(langID, rt);
|
2011-07-09 02:49:31 +04:00
|
|
|
if (regMemReport) {
|
2011-10-17 18:59:28 +04:00
|
|
|
regMemReport = false;
|
2011-07-09 02:49:31 +04:00
|
|
|
NS_RegisterMemoryReporter(new NS_MEMORY_REPORTER_NAME(CycleCollector));
|
|
|
|
}
|
2007-01-05 01:31:26 +03:00
|
|
|
}
|
|
|
|
|
2009-07-09 05:10:29 +04:00
|
|
|
nsCycleCollectionLanguageRuntime *
|
|
|
|
nsCycleCollector_getRuntime(PRUint32 langID)
|
|
|
|
{
|
|
|
|
if (sCollector)
|
|
|
|
sCollector->GetRuntime(langID);
|
|
|
|
return nsnull;
|
|
|
|
}
|
2007-01-05 01:31:26 +03:00
|
|
|
|
|
|
|
void
|
|
|
|
nsCycleCollector_forgetRuntime(PRUint32 langID)
|
|
|
|
{
|
2007-03-27 13:49:06 +04:00
|
|
|
if (sCollector)
|
|
|
|
sCollector->ForgetRuntime(langID);
|
2007-01-05 01:31:26 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool
|
2007-08-06 18:34:02 +04:00
|
|
|
NS_CycleCollectorSuspect(nsISupports *n)
|
2007-01-05 01:31:26 +03:00
|
|
|
{
|
2007-03-27 13:49:06 +04:00
|
|
|
if (sCollector)
|
2007-06-27 21:51:48 +04:00
|
|
|
return sCollector->Suspect(n);
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2007-01-05 01:31:26 +03:00
|
|
|
}
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool
|
2007-08-06 18:34:02 +04:00
|
|
|
NS_CycleCollectorForget(nsISupports *n)
|
2007-01-05 01:31:26 +03:00
|
|
|
{
|
2011-10-17 18:59:28 +04:00
|
|
|
return sCollector ? sCollector->Forget(n) : true;
|
2007-01-05 01:31:26 +03:00
|
|
|
}
|
|
|
|
|
2009-05-07 00:46:04 +04:00
|
|
|
nsPurpleBufferEntry*
|
|
|
|
NS_CycleCollectorSuspect2(nsISupports *n)
|
|
|
|
{
|
|
|
|
if (sCollector)
|
|
|
|
return sCollector->Suspect2(n);
|
|
|
|
return nsnull;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2008-02-25 20:47:25 +03:00
|
|
|
PRUint32
|
|
|
|
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;
|
2010-11-12 01:52:30 +03:00
|
|
|
|
2011-07-02 01:11:31 +04:00
|
|
|
nsCycleCollectionJSRuntime *GetJSRuntime()
|
|
|
|
{
|
|
|
|
return static_cast<nsCycleCollectionJSRuntime*>
|
|
|
|
(mCollector->mRuntimes[nsIProgrammingLanguage::JAVASCRIPT]);
|
|
|
|
}
|
|
|
|
|
2010-11-12 01:52:30 +03:00
|
|
|
public:
|
|
|
|
NS_IMETHOD Run()
|
|
|
|
{
|
|
|
|
#ifdef XP_WIN
|
|
|
|
TlsSetValue(gTLSThreadIDIndex,
|
|
|
|
(void*) mozilla::threads::CycleCollector);
|
|
|
|
#elif defined(NS_TLS)
|
|
|
|
gTLSThreadID = mozilla::threads::CycleCollector;
|
|
|
|
#else
|
|
|
|
gCycleCollectorThread = PR_GetCurrentThread();
|
|
|
|
#endif
|
|
|
|
|
|
|
|
NS_ASSERTION(NS_IsCycleCollectorThread() && !NS_IsMainThread(),
|
|
|
|
"Wrong thread!");
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2011-07-02 01:11:31 +04:00
|
|
|
GetJSRuntime()->NotifyEnterCycleCollectionThread();
|
2011-06-14 00:24:23 +04:00
|
|
|
mCollected = mCollector->BeginCollection(mListener);
|
2011-07-02 01:11:31 +04:00
|
|
|
GetJSRuntime()->NotifyLeaveCycleCollectionThread();
|
2010-11-12 01:52:30 +03:00
|
|
|
|
|
|
|
mReply.Notify();
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCycleCollectorRunner(nsCycleCollector *collector)
|
|
|
|
: mCollector(collector),
|
2011-01-14 17:51:43 +03:00
|
|
|
mListener(nsnull),
|
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),
|
|
|
|
mCollected(false)
|
2010-11-12 01:52:30 +03:00
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
}
|
|
|
|
|
|
|
|
PRUint32 Collect(nsICycleCollectorListener* aListener)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
mCollector->GCIfNeeded(false);
|
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)
|
2010-11-12 01:52:30 +03:00
|
|
|
return 0;
|
|
|
|
|
2011-08-10 09:36:00 +04:00
|
|
|
nsAutoTArray<PtrInfo*, 4000> whiteNodes;
|
2011-06-16 02:53:00 +04:00
|
|
|
if (!mCollector->PrepareForCollection(&whiteNodes))
|
2010-11-12 01:52:30 +03:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
NS_ASSERTION(!mListener, "Should have cleared this already!");
|
2011-10-14 04:37:18 +04:00
|
|
|
if (aListener && NS_FAILED(aListener->Begin()))
|
|
|
|
aListener = nsnull;
|
2010-11-12 01:52:30 +03:00
|
|
|
mListener = aListener;
|
|
|
|
|
2011-07-02 01:11:31 +04:00
|
|
|
GetJSRuntime()->NotifyLeaveMainThread();
|
2010-11-12 01:52:30 +03:00
|
|
|
mRequest.Notify();
|
|
|
|
mReply.Wait();
|
2011-07-02 01:11:31 +04:00
|
|
|
GetJSRuntime()->NotifyEnterMainThread();
|
2010-11-12 01:52:30 +03:00
|
|
|
|
|
|
|
mListener = nsnull;
|
|
|
|
|
|
|
|
if (mCollected) {
|
2011-06-22 21:41:17 +04:00
|
|
|
mCollected = mCollector->FinishCollection(aListener);
|
2010-11-12 01:52:30 +03:00
|
|
|
|
|
|
|
mCollector->CleanupAfterCollection();
|
|
|
|
|
|
|
|
return mCollected ? mCollector->mCollectedObjects : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Shutdown()
|
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
|
|
|
|
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()
|
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
NS_ASSERTION(!sCollector, "Forgot to call nsCycleCollector_shutdown?");
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
PRUint32
|
|
|
|
nsCycleCollector_collect(nsICycleCollectorListener *aListener)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
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();
|
|
|
|
}
|
|
|
|
|
2010-11-12 01:52:30 +03:00
|
|
|
if (sCollectorRunner)
|
2011-01-14 13:06:09 +03:00
|
|
|
return sCollectorRunner->Collect(listener);
|
|
|
|
return sCollector ? sCollector->Collect(1, listener) : 0;
|
2010-11-12 01:52:30 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsCycleCollector_shutdownThreads()
|
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
if (sCollectorRunner) {
|
|
|
|
nsRefPtr<nsCycleCollectorRunner> runner;
|
|
|
|
runner.swap(sCollectorRunner);
|
|
|
|
runner->Shutdown();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sCollectorThread) {
|
|
|
|
nsCOMPtr<nsIThread> thread;
|
|
|
|
thread.swap(sCollectorThread);
|
|
|
|
thread->Shutdown();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsCycleCollector_shutdown()
|
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
NS_ASSERTION(!sCollectorRunner, "Should have finished before!");
|
|
|
|
NS_ASSERTION(!sCollectorThread, "Should have finished before!");
|
|
|
|
|
|
|
|
if (sCollector) {
|
|
|
|
sCollector->Shutdown();
|
|
|
|
delete sCollector;
|
|
|
|
sCollector = nsnull;
|
|
|
|
}
|
|
|
|
}
|