2014-05-05 21:30:39 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
2012-05-21 15:12:37 +04:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2007-01-05 01:31:26 +03:00
|
|
|
|
|
|
|
//
|
|
|
|
// This file implements a garbage-cycle collector based on the paper
|
2012-11-07 05:38:29 +04:00
|
|
|
//
|
2007-01-05 01:31:26 +03:00
|
|
|
// Concurrent Cycle Collection in Reference Counted Systems
|
|
|
|
// Bacon & Rajan (2001), ECOOP 2001 / Springer LNCS vol 2072
|
|
|
|
//
|
|
|
|
// We are not using the concurrent or acyclic cases of that paper; so
|
|
|
|
// the green, red and orange colors are not used.
|
|
|
|
//
|
|
|
|
// The collector is based on tracking pointers of four colors:
|
|
|
|
//
|
|
|
|
// Black nodes are definitely live. If we ever determine a node is
|
|
|
|
// black, it's ok to forget about, drop from our records.
|
|
|
|
//
|
|
|
|
// White nodes are definitely garbage cycles. Once we finish with our
|
|
|
|
// scanning, we unlink all the white nodes and expect that by
|
|
|
|
// unlinking them they will self-destruct (since a garbage cycle is
|
|
|
|
// only keeping itself alive with internal links, by definition).
|
|
|
|
//
|
2013-07-09 21:30:58 +04:00
|
|
|
// Snow-white is an addition to the original algorithm. Snow-white object
|
|
|
|
// has reference count zero and is just waiting for deletion.
|
|
|
|
//
|
2007-01-05 01:31:26 +03:00
|
|
|
// 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.
|
|
|
|
|
2013-12-06 22:17:20 +04:00
|
|
|
// Incremental cycle collection
|
|
|
|
//
|
|
|
|
// Beyond the simple state machine required to implement incremental
|
|
|
|
// collection, the CC needs to be able to compensate for things the browser
|
|
|
|
// is doing during the collection. There are two kinds of problems. For each
|
|
|
|
// of these, there are two cases to deal with: purple-buffered C++ objects
|
|
|
|
// and JS objects.
|
|
|
|
|
|
|
|
// The first problem is that an object in the CC's graph can become garbage.
|
|
|
|
// This is bad because the CC touches the objects in its graph at every
|
|
|
|
// stage of its operation.
|
|
|
|
//
|
|
|
|
// All cycle collected C++ objects that die during a cycle collection
|
|
|
|
// will end up actually getting deleted by the SnowWhiteKiller. Before
|
|
|
|
// the SWK deletes an object, it checks if an ICC is running, and if so,
|
|
|
|
// if the object is in the graph. If it is, the CC clears mPointer and
|
|
|
|
// mParticipant so it does not point to the raw object any more. Because
|
|
|
|
// objects could die any time the CC returns to the mutator, any time the CC
|
|
|
|
// accesses a PtrInfo it must perform a null check on mParticipant to
|
|
|
|
// ensure the object has not gone away.
|
|
|
|
//
|
|
|
|
// JS objects don't always run finalizers, so the CC can't remove them from
|
|
|
|
// the graph when they die. Fortunately, JS objects can only die during a GC,
|
|
|
|
// so if a GC is begun during an ICC, the browser synchronously finishes off
|
|
|
|
// the ICC, which clears the entire CC graph. If the GC and CC are scheduled
|
|
|
|
// properly, this should be rare.
|
|
|
|
//
|
|
|
|
// The second problem is that objects in the graph can be changed, say by
|
|
|
|
// being addrefed or released, or by having a field updated, after the object
|
2013-12-18 07:29:57 +04:00
|
|
|
// has been added to the graph. The problem is that ICC can miss a newly
|
|
|
|
// created reference to an object, and end up unlinking an object that is
|
|
|
|
// actually alive.
|
|
|
|
//
|
|
|
|
// The basic idea of the solution, from "An on-the-fly Reference Counting
|
|
|
|
// Garbage Collector for Java" by Levanoni and Petrank, is to notice if an
|
|
|
|
// object has had an additional reference to it created during the collection,
|
|
|
|
// and if so, don't collect it during the current collection. This avoids having
|
|
|
|
// to rerun the scan as in Bacon & Rajan 2001.
|
|
|
|
//
|
|
|
|
// For cycle collected C++ objects, we modify AddRef to place the object in
|
|
|
|
// the purple buffer, in addition to Release. Then, in the CC, we treat any
|
|
|
|
// objects in the purple buffer as being alive, after graph building has
|
|
|
|
// completed. Because they are in the purple buffer, they will be suspected
|
|
|
|
// in the next CC, so there's no danger of leaks. This is imprecise, because
|
|
|
|
// we will treat as live an object that has been Released but not AddRefed
|
|
|
|
// during graph building, but that's probably rare enough that the additional
|
|
|
|
// bookkeeping overhead is not worthwhile.
|
|
|
|
//
|
|
|
|
// For JS objects, the cycle collector is only looking at gray objects. If a
|
|
|
|
// gray object is touched during ICC, it will be made black by UnmarkGray.
|
|
|
|
// Thus, if a JS object has become black during the ICC, we treat it as live.
|
|
|
|
// Merged JS zones have to be handled specially: we scan all zone globals.
|
|
|
|
// If any are black, we treat the zone as being black.
|
2013-12-06 22:17:20 +04:00
|
|
|
|
2007-01-05 01:31:26 +03:00
|
|
|
|
2013-12-06 22:17:20 +04:00
|
|
|
// Safety
|
2007-01-05 01:31:26 +03:00
|
|
|
//
|
|
|
|
// An XPCOM object is either scan-safe or scan-unsafe, purple-safe or
|
|
|
|
// purple-unsafe.
|
|
|
|
//
|
2013-05-02 02:35:13 +04:00
|
|
|
// An nsISupports object is scan-safe if:
|
2007-01-05 01:31:26 +03:00
|
|
|
//
|
2013-05-02 02:35:13 +04:00
|
|
|
// - It can be QI'ed to |nsXPCOMCycleCollectionParticipant|, though
|
|
|
|
// this operation loses ISupports identity (like nsIClassInfo).
|
|
|
|
// - Additionally, 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).
|
|
|
|
//
|
2013-05-02 02:35:13 +04:00
|
|
|
// A non-nsISupports ("native") object is scan-safe by explicitly
|
|
|
|
// providing its nsCycleCollectionParticipant.
|
|
|
|
//
|
2007-01-05 01:31:26 +03:00
|
|
|
// An object is purple-safe if it satisfies the following properties:
|
|
|
|
//
|
2013-05-04 19:39:44 +04:00
|
|
|
// - The object is scan-safe.
|
2007-01-05 01:31:26 +03:00
|
|
|
//
|
|
|
|
// 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
|
2013-05-02 02:35:13 +04:00
|
|
|
// guarantee its purple-safety. The easiest way to ensure that an
|
|
|
|
// object is purple-safe is to use nsCycleCollectingAutoRefCnt.
|
2007-01-05 01:31:26 +03:00
|
|
|
//
|
|
|
|
// 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
|
2013-05-04 19:39:44 +04:00
|
|
|
// of those nodes.
|
2007-01-05 01:31:26 +03:00
|
|
|
//
|
|
|
|
// We do not |AddRef| or |Release| any objects during scanning. We
|
2013-05-02 02:35:13 +04:00
|
|
|
// rely on the purple-safety of the roots that call |suspect| to
|
|
|
|
// hold, such that we will clear the pointer from the purple buffer
|
|
|
|
// entry to the object 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.
|
2007-01-05 01:31:26 +03:00
|
|
|
//
|
2013-07-09 21:30:58 +04:00
|
|
|
// We *do* call |Root| and |Unroot| on every white object, on
|
2007-01-05 01:31:26 +03:00
|
|
|
// either side of the calls to |Unlink|. This keeps the set of white
|
|
|
|
// objects alive during the unlinking.
|
2013-05-04 19:39:44 +04:00
|
|
|
//
|
2007-01-05 01:31:26 +03:00
|
|
|
|
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
|
|
|
|
2013-12-09 06:52:54 +04:00
|
|
|
#include "mozilla/ArrayUtils.h"
|
2013-11-21 02:35:16 +04:00
|
|
|
#include "mozilla/AutoRestore.h"
|
|
|
|
#include "mozilla/CycleCollectedJSRuntime.h"
|
2015-01-18 21:59:21 +03:00
|
|
|
#include "mozilla/DebugOnly.h"
|
2014-01-15 00:23:59 +04:00
|
|
|
#include "mozilla/HoldDropJSObjects.h"
|
2011-10-11 09:50:08 +04:00
|
|
|
/* This must occur *after* base/process_util.h to avoid typedefs conflicts. */
|
2013-11-10 01:15:44 +04:00
|
|
|
#include "mozilla/LinkedList.h"
|
2014-12-09 01:45:13 +03:00
|
|
|
#include "mozilla/MemoryReporting.h"
|
|
|
|
#include "mozilla/SegmentedVector.h"
|
2011-10-11 09:50:08 +04:00
|
|
|
|
2007-01-05 01:31:26 +03:00
|
|
|
#include "nsCycleCollectionParticipant.h"
|
2013-05-21 00:08:11 +04:00
|
|
|
#include "nsCycleCollectionNoteRootCallback.h"
|
2007-01-05 01:31:26 +03:00
|
|
|
#include "nsDeque.h"
|
|
|
|
#include "nsCycleCollector.h"
|
|
|
|
#include "nsThreadUtils.h"
|
2014-03-26 20:57:38 +04:00
|
|
|
#include "nsXULAppAPI.h"
|
2007-01-05 01:31:26 +03:00
|
|
|
#include "prenv.h"
|
2007-03-06 00:07:51 +03:00
|
|
|
#include "nsPrintfCString.h"
|
2007-03-27 13:49:06 +04:00
|
|
|
#include "nsTArray.h"
|
2008-01-07 01:05:10 +03:00
|
|
|
#include "nsIConsoleService.h"
|
2012-06-06 03:51:58 +04:00
|
|
|
#include "mozilla/Attributes.h"
|
2010-08-12 04:03:23 +04:00
|
|
|
#include "nsICycleCollectorListener.h"
|
2011-07-09 02:49:31 +04:00
|
|
|
#include "nsIMemoryReporter.h"
|
2012-10-16 06:12:14 +04:00
|
|
|
#include "nsIFile.h"
|
2014-03-20 11:29:51 +04:00
|
|
|
#include "nsDumpUtils.h"
|
2011-03-29 00:05:48 +04:00
|
|
|
#include "xpcpublic.h"
|
2013-03-18 18:25:50 +04:00
|
|
|
#include "GeckoProfiler.h"
|
2013-07-30 18:25:31 +04:00
|
|
|
#include <stdint.h>
|
2007-01-05 01:31:26 +03:00
|
|
|
#include <stdio.h>
|
2010-11-12 01:52:30 +03:00
|
|
|
|
2012-10-26 17:32:10 +04:00
|
|
|
#include "mozilla/Likely.h"
|
2013-11-05 16:45:20 +04:00
|
|
|
#include "mozilla/PoisonIOInterposer.h"
|
2011-06-21 01:47:58 +04:00
|
|
|
#include "mozilla/Telemetry.h"
|
2013-08-20 15:48:22 +04:00
|
|
|
#include "mozilla/ThreadLocal.h"
|
2010-11-12 01:52:30 +03:00
|
|
|
|
|
|
|
using namespace mozilla;
|
|
|
|
|
2010-07-16 05:08:47 +04:00
|
|
|
//#define COLLECT_TIME_DEBUG
|
|
|
|
|
2013-04-25 19:42:44 +04:00
|
|
|
// Enable assertions that are useful for diagnosing errors in graph construction.
|
|
|
|
//#define DEBUG_CC_GRAPH
|
|
|
|
|
2009-10-23 08:47:27 +04:00
|
|
|
#define DEFAULT_SHUTDOWN_COLLECTIONS 5
|
2007-01-05 01:31:26 +03:00
|
|
|
|
2013-05-24 22:26:09 +04:00
|
|
|
// One to do the freeing, then another to detect there is no more work to do.
|
|
|
|
#define NORMAL_SHUTDOWN_COLLECTIONS 2
|
|
|
|
|
2012-12-20 02:35:50 +04:00
|
|
|
// Cycle collector environment variables
|
|
|
|
//
|
2014-02-21 02:27:03 +04:00
|
|
|
// MOZ_CC_LOG_ALL: If defined, always log cycle collector heaps.
|
2012-12-20 02:35:50 +04:00
|
|
|
//
|
2014-02-21 02:27:03 +04:00
|
|
|
// MOZ_CC_LOG_SHUTDOWN: If defined, log cycle collector heaps at shutdown.
|
2012-12-20 02:35:50 +04:00
|
|
|
//
|
2014-02-21 02:27:04 +04:00
|
|
|
// MOZ_CC_LOG_THREAD: If set to "main", only automatically log main thread
|
|
|
|
// CCs. If set to "worker", only automatically log worker CCs. If set to "all",
|
|
|
|
// log either. The default value is "all". This must be used with either
|
|
|
|
// MOZ_CC_LOG_ALL or MOZ_CC_LOG_SHUTDOWN for it to do anything.
|
|
|
|
//
|
2014-03-26 20:57:38 +04:00
|
|
|
// MOZ_CC_LOG_PROCESS: If set to "main", only automatically log main process
|
|
|
|
// CCs. If set to "content", only automatically log tab CCs. If set to
|
|
|
|
// "plugins", only automatically log plugin CCs. If set to "all", log
|
|
|
|
// everything. The default value is "all". This must be used with either
|
|
|
|
// MOZ_CC_LOG_ALL or MOZ_CC_LOG_SHUTDOWN for it to do anything.
|
|
|
|
//
|
2014-04-25 01:28:49 +04:00
|
|
|
// MOZ_CC_ALL_TRACES: If set to "all", any cycle collector
|
|
|
|
// logging done will be WantAllTraces, which disables
|
2012-12-20 02:35:50 +04:00
|
|
|
// various cycle collector optimizations to give a fuller picture of
|
2014-04-25 01:28:49 +04:00
|
|
|
// the heap. If set to "shutdown", only shutdown logging will be WantAllTraces.
|
|
|
|
// The default is none.
|
2012-12-20 02:35:50 +04:00
|
|
|
//
|
2014-02-21 02:27:03 +04:00
|
|
|
// MOZ_CC_RUN_DURING_SHUTDOWN: In non-DEBUG or builds, if this is set,
|
2013-02-27 01:34:32 +04:00
|
|
|
// run cycle collections at shutdown.
|
2013-02-22 06:10:59 +04:00
|
|
|
//
|
|
|
|
// MOZ_CC_LOG_DIRECTORY: The directory in which logs are placed (such as
|
2014-02-21 02:27:03 +04:00
|
|
|
// logs from MOZ_CC_LOG_ALL and MOZ_CC_LOG_SHUTDOWN, or other uses
|
2013-02-22 06:10:59 +04:00
|
|
|
// of nsICycleCollectorListener)
|
2012-09-28 21:11:33 +04:00
|
|
|
|
2007-01-05 01:31:26 +03:00
|
|
|
// Various parameters of this collector can be tuned using environment
|
|
|
|
// variables.
|
|
|
|
|
|
|
|
struct nsCycleCollectorParams
|
|
|
|
{
|
2014-05-05 21:30:39 +04:00
|
|
|
bool mLogAll;
|
|
|
|
bool mLogShutdown;
|
|
|
|
bool mAllTracesAll;
|
|
|
|
bool mAllTracesShutdown;
|
|
|
|
bool mLogThisThread;
|
|
|
|
|
|
|
|
nsCycleCollectorParams() :
|
2014-05-13 21:41:38 +04:00
|
|
|
mLogAll(PR_GetEnv("MOZ_CC_LOG_ALL") != nullptr),
|
|
|
|
mLogShutdown(PR_GetEnv("MOZ_CC_LOG_SHUTDOWN") != nullptr),
|
2014-05-05 21:30:39 +04:00
|
|
|
mAllTracesAll(false),
|
|
|
|
mAllTracesShutdown(false)
|
|
|
|
{
|
|
|
|
const char* logThreadEnv = PR_GetEnv("MOZ_CC_LOG_THREAD");
|
|
|
|
bool threadLogging = true;
|
|
|
|
if (logThreadEnv && !!strcmp(logThreadEnv, "all")) {
|
|
|
|
if (NS_IsMainThread()) {
|
|
|
|
threadLogging = !strcmp(logThreadEnv, "main");
|
|
|
|
} else {
|
|
|
|
threadLogging = !strcmp(logThreadEnv, "worker");
|
|
|
|
}
|
2007-01-05 01:31:26 +03:00
|
|
|
}
|
2014-02-21 02:27:03 +04:00
|
|
|
|
2014-05-05 21:30:39 +04:00
|
|
|
const char* logProcessEnv = PR_GetEnv("MOZ_CC_LOG_PROCESS");
|
|
|
|
bool processLogging = true;
|
|
|
|
if (logProcessEnv && !!strcmp(logProcessEnv, "all")) {
|
|
|
|
switch (XRE_GetProcessType()) {
|
|
|
|
case GeckoProcessType_Default:
|
|
|
|
processLogging = !strcmp(logProcessEnv, "main");
|
|
|
|
break;
|
|
|
|
case GeckoProcessType_Plugin:
|
|
|
|
processLogging = !strcmp(logProcessEnv, "plugins");
|
|
|
|
break;
|
|
|
|
case GeckoProcessType_Content:
|
|
|
|
processLogging = !strcmp(logProcessEnv, "content");
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
processLogging = false;
|
|
|
|
break;
|
|
|
|
}
|
2014-02-21 02:27:03 +04:00
|
|
|
}
|
2014-05-05 21:30:39 +04:00
|
|
|
mLogThisThread = threadLogging && processLogging;
|
2014-04-25 01:28:49 +04:00
|
|
|
|
2014-05-05 21:30:39 +04:00
|
|
|
const char* allTracesEnv = PR_GetEnv("MOZ_CC_ALL_TRACES");
|
|
|
|
if (allTracesEnv) {
|
|
|
|
if (!strcmp(allTracesEnv, "all")) {
|
|
|
|
mAllTracesAll = true;
|
|
|
|
} else if (!strcmp(allTracesEnv, "shutdown")) {
|
|
|
|
mAllTracesShutdown = true;
|
|
|
|
}
|
2014-04-25 01:28:49 +04:00
|
|
|
}
|
2014-05-05 21:30:39 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
bool LogThisCC(bool aIsShutdown)
|
|
|
|
{
|
|
|
|
return (mLogAll || (aIsShutdown && mLogShutdown)) && mLogThisThread;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool AllTracesThisCC(bool aIsShutdown)
|
|
|
|
{
|
|
|
|
return mAllTracesAll || (aIsShutdown && mAllTracesShutdown);
|
|
|
|
}
|
2007-01-05 01:31:26 +03:00
|
|
|
};
|
|
|
|
|
2012-02-12 20:02:01 +04:00
|
|
|
#ifdef COLLECT_TIME_DEBUG
|
|
|
|
class TimeLog
|
|
|
|
{
|
|
|
|
public:
|
2014-05-13 21:41:38 +04:00
|
|
|
TimeLog() : mLastCheckpoint(TimeStamp::Now())
|
|
|
|
{
|
|
|
|
}
|
2012-02-12 20:02:01 +04:00
|
|
|
|
2014-05-05 21:30:39 +04:00
|
|
|
void
|
|
|
|
Checkpoint(const char* aEvent)
|
|
|
|
{
|
|
|
|
TimeStamp now = TimeStamp::Now();
|
2014-05-07 04:25:27 +04:00
|
|
|
double dur = (now - mLastCheckpoint).ToMilliseconds();
|
|
|
|
if (dur >= 0.5) {
|
|
|
|
printf("cc: %s took %.1fms\n", aEvent, dur);
|
2012-02-12 20:02:01 +04:00
|
|
|
}
|
2014-05-05 21:30:39 +04:00
|
|
|
mLastCheckpoint = now;
|
|
|
|
}
|
2012-02-12 20:02:01 +04:00
|
|
|
|
|
|
|
private:
|
2014-05-05 21:30:39 +04:00
|
|
|
TimeStamp mLastCheckpoint;
|
2012-02-12 20:02:01 +04:00
|
|
|
};
|
|
|
|
#else
|
|
|
|
class TimeLog
|
|
|
|
{
|
|
|
|
public:
|
2014-05-13 21:41:38 +04:00
|
|
|
TimeLog()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
void Checkpoint(const char* aEvent)
|
|
|
|
{
|
|
|
|
}
|
2012-02-12 20:02:01 +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:
|
2014-05-05 21:30:39 +04:00
|
|
|
// 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.
|
2007-04-26 01:12:11 +04:00
|
|
|
|
2014-05-05 21:30:39 +04:00
|
|
|
EdgePool()
|
|
|
|
{
|
|
|
|
mSentinelAndBlocks[0].block = nullptr;
|
|
|
|
mSentinelAndBlocks[1].block = nullptr;
|
|
|
|
}
|
2008-01-10 17:10:03 +03:00
|
|
|
|
2014-05-05 21:30:39 +04:00
|
|
|
~EdgePool()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(!mSentinelAndBlocks[0].block &&
|
|
|
|
!mSentinelAndBlocks[1].block,
|
|
|
|
"Didn't call Clear()?");
|
|
|
|
}
|
2008-01-10 17:10:03 +03:00
|
|
|
|
2014-05-05 21:30:39 +04:00
|
|
|
void Clear()
|
|
|
|
{
|
2014-05-13 21:41:38 +04:00
|
|
|
Block* b = Blocks();
|
2014-05-05 21:30:39 +04:00
|
|
|
while (b) {
|
2014-05-13 21:41:38 +04:00
|
|
|
Block* next = b->Next();
|
2014-05-05 21:30:39 +04:00
|
|
|
delete b;
|
|
|
|
b = next;
|
2007-04-26 01:12:11 +04:00
|
|
|
}
|
|
|
|
|
2014-05-05 21:30:39 +04:00
|
|
|
mSentinelAndBlocks[0].block = nullptr;
|
|
|
|
mSentinelAndBlocks[1].block = nullptr;
|
|
|
|
}
|
|
|
|
|
2013-12-06 22:17:20 +04:00
|
|
|
#ifdef DEBUG
|
2014-05-05 21:30:39 +04:00
|
|
|
bool IsEmpty()
|
|
|
|
{
|
|
|
|
return !mSentinelAndBlocks[0].block &&
|
|
|
|
!mSentinelAndBlocks[1].block;
|
|
|
|
}
|
2013-12-06 22:17:20 +04:00
|
|
|
#endif
|
|
|
|
|
2007-04-26 01:12:11 +04:00
|
|
|
private:
|
2014-05-05 21:30:39 +04:00
|
|
|
struct Block;
|
2014-08-25 23:17:15 +04:00
|
|
|
union PtrInfoOrBlock
|
|
|
|
{
|
2014-05-05 21:30:39 +04:00
|
|
|
// Use a union to avoid reinterpret_cast and the ensuing
|
|
|
|
// potential aliasing bugs.
|
2014-05-13 21:41:38 +04:00
|
|
|
PtrInfo* ptrInfo;
|
|
|
|
Block* block;
|
2014-05-05 21:30:39 +04:00
|
|
|
};
|
2014-05-13 21:41:38 +04:00
|
|
|
struct Block
|
|
|
|
{
|
2014-05-05 21:30:39 +04:00
|
|
|
enum { BlockSize = 16 * 1024 };
|
|
|
|
|
|
|
|
PtrInfoOrBlock mPointers[BlockSize];
|
2014-05-13 21:41:38 +04:00
|
|
|
Block()
|
|
|
|
{
|
2014-05-05 21:30:39 +04:00
|
|
|
mPointers[BlockSize - 2].block = nullptr; // sentinel
|
|
|
|
mPointers[BlockSize - 1].block = nullptr; // next block pointer
|
|
|
|
}
|
2014-05-13 21:41:38 +04:00
|
|
|
Block*& Next()
|
|
|
|
{
|
|
|
|
return mPointers[BlockSize - 1].block;
|
|
|
|
}
|
|
|
|
PtrInfoOrBlock* Start()
|
|
|
|
{
|
|
|
|
return &mPointers[0];
|
|
|
|
}
|
|
|
|
PtrInfoOrBlock* End()
|
|
|
|
{
|
|
|
|
return &mPointers[BlockSize - 2];
|
|
|
|
}
|
2014-05-05 21:30:39 +04:00
|
|
|
};
|
2007-04-26 01:12:11 +04:00
|
|
|
|
2014-05-05 21:30:39 +04:00
|
|
|
// Store the null sentinel so that we can have valid iterators
|
|
|
|
// before adding any edges and without adding any blocks.
|
|
|
|
PtrInfoOrBlock mSentinelAndBlocks[2];
|
2007-04-26 01:12:11 +04:00
|
|
|
|
2014-05-13 21:41:38 +04:00
|
|
|
Block*& Blocks()
|
|
|
|
{
|
|
|
|
return mSentinelAndBlocks[1].block;
|
|
|
|
}
|
|
|
|
Block* Blocks() const
|
|
|
|
{
|
|
|
|
return mSentinelAndBlocks[1].block;
|
|
|
|
}
|
2007-04-26 01:12:11 +04:00
|
|
|
|
|
|
|
public:
|
2014-05-05 21:30:39 +04:00
|
|
|
class Iterator
|
|
|
|
{
|
|
|
|
public:
|
2014-08-25 23:17:15 +04:00
|
|
|
Iterator() : mPointer(nullptr) {}
|
|
|
|
explicit Iterator(PtrInfoOrBlock* aPointer) : mPointer(aPointer) {}
|
|
|
|
Iterator(const Iterator& aOther) : mPointer(aOther.mPointer) {}
|
2014-05-05 21:30:39 +04:00
|
|
|
|
|
|
|
Iterator& operator++()
|
2007-04-26 01:12:11 +04:00
|
|
|
{
|
2014-05-13 21:41:38 +04:00
|
|
|
if (!mPointer->ptrInfo) {
|
2014-05-05 21:30:39 +04:00
|
|
|
// Null pointer is a sentinel for link to the next block.
|
|
|
|
mPointer = (mPointer + 1)->block->mPointers;
|
|
|
|
}
|
|
|
|
++mPointer;
|
|
|
|
return *this;
|
|
|
|
}
|
2007-04-26 01:12:11 +04:00
|
|
|
|
2014-05-05 21:30:39 +04:00
|
|
|
PtrInfo* operator*() const
|
|
|
|
{
|
2014-05-13 21:41:38 +04:00
|
|
|
if (!mPointer->ptrInfo) {
|
2014-05-05 21:30:39 +04:00
|
|
|
// Null pointer is a sentinel for link to the next block.
|
|
|
|
return (mPointer + 1)->block->mPointers->ptrInfo;
|
|
|
|
}
|
|
|
|
return mPointer->ptrInfo;
|
|
|
|
}
|
|
|
|
bool operator==(const Iterator& aOther) const
|
2014-05-13 21:41:38 +04:00
|
|
|
{
|
|
|
|
return mPointer == aOther.mPointer;
|
|
|
|
}
|
2014-05-05 21:30:39 +04:00
|
|
|
bool operator!=(const Iterator& aOther) const
|
2014-05-13 21:41:38 +04:00
|
|
|
{
|
|
|
|
return mPointer != aOther.mPointer;
|
|
|
|
}
|
2007-04-26 01:12:11 +04:00
|
|
|
|
2013-04-25 19:42:44 +04:00
|
|
|
#ifdef DEBUG_CC_GRAPH
|
2014-05-05 21:30:39 +04:00
|
|
|
bool Initialized() const
|
|
|
|
{
|
|
|
|
return mPointer != nullptr;
|
|
|
|
}
|
2013-04-25 19:42:44 +04:00
|
|
|
#endif
|
|
|
|
|
2014-05-05 21:30:39 +04:00
|
|
|
private:
|
2014-05-13 21:41:38 +04:00
|
|
|
PtrInfoOrBlock* mPointer;
|
2014-05-05 21:30:39 +04:00
|
|
|
};
|
2007-04-26 01:12:11 +04:00
|
|
|
|
2014-05-05 21:30:39 +04:00
|
|
|
class Builder;
|
|
|
|
friend class Builder;
|
2014-05-13 21:41:38 +04:00
|
|
|
class Builder
|
|
|
|
{
|
2014-05-05 21:30:39 +04:00
|
|
|
public:
|
2014-07-28 21:19:06 +04:00
|
|
|
explicit Builder(EdgePool& aPool)
|
2014-05-13 21:41:38 +04:00
|
|
|
: mCurrent(&aPool.mSentinelAndBlocks[0])
|
|
|
|
, mBlockEnd(&aPool.mSentinelAndBlocks[0])
|
|
|
|
, mNextBlockPtr(&aPool.Blocks())
|
2014-05-05 21:30:39 +04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2014-05-13 21:41:38 +04:00
|
|
|
Iterator Mark()
|
|
|
|
{
|
|
|
|
return Iterator(mCurrent);
|
|
|
|
}
|
2014-05-05 21:30:39 +04:00
|
|
|
|
2014-05-13 21:41:38 +04:00
|
|
|
void Add(PtrInfo* aEdge)
|
|
|
|
{
|
2014-05-05 21:30:39 +04:00
|
|
|
if (mCurrent == mBlockEnd) {
|
2014-05-13 21:41:38 +04:00
|
|
|
Block* b = new Block();
|
2014-05-05 21:30:39 +04:00
|
|
|
*mNextBlockPtr = b;
|
|
|
|
mCurrent = b->Start();
|
|
|
|
mBlockEnd = b->End();
|
|
|
|
mNextBlockPtr = &b->Next();
|
|
|
|
}
|
|
|
|
(mCurrent++)->ptrInfo = aEdge;
|
|
|
|
}
|
|
|
|
private:
|
|
|
|
// mBlockEnd points to space for null sentinel
|
2014-05-13 21:41:38 +04:00
|
|
|
PtrInfoOrBlock* mCurrent;
|
|
|
|
PtrInfoOrBlock* mBlockEnd;
|
|
|
|
Block** mNextBlockPtr;
|
2014-05-05 21:30:39 +04:00
|
|
|
};
|
|
|
|
|
2014-05-13 21:41:38 +04:00
|
|
|
size_t SizeOfExcludingThis(MallocSizeOf aMallocSizeOf) const
|
|
|
|
{
|
2014-05-05 21:30:39 +04:00
|
|
|
size_t n = 0;
|
2014-05-13 21:41:38 +04:00
|
|
|
Block* b = Blocks();
|
2014-05-05 21:30:39 +04:00
|
|
|
while (b) {
|
|
|
|
n += aMallocSizeOf(b);
|
|
|
|
b = b->Next();
|
2011-07-09 02:49:31 +04:00
|
|
|
}
|
2014-05-05 21:30:39 +04:00
|
|
|
return n;
|
|
|
|
}
|
2007-04-26 01:12:11 +04:00
|
|
|
};
|
|
|
|
|
2013-04-25 19:42:44 +04:00
|
|
|
#ifdef DEBUG_CC_GRAPH
|
|
|
|
#define CC_GRAPH_ASSERT(b) MOZ_ASSERT(b)
|
|
|
|
#else
|
|
|
|
#define CC_GRAPH_ASSERT(b)
|
|
|
|
#endif
|
|
|
|
|
2013-09-10 19:29:45 +04:00
|
|
|
#define CC_TELEMETRY(_name, _value) \
|
|
|
|
PR_BEGIN_MACRO \
|
|
|
|
if (NS_IsMainThread()) { \
|
|
|
|
Telemetry::Accumulate(Telemetry::CYCLE_COLLECTOR##_name, _value); \
|
|
|
|
} else { \
|
|
|
|
Telemetry::Accumulate(Telemetry::CYCLE_COLLECTOR_WORKER##_name, _value); \
|
|
|
|
} \
|
|
|
|
PR_END_MACRO
|
|
|
|
|
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
|
|
|
|
{
|
2014-05-13 21:41:38 +04:00
|
|
|
void* mPointer;
|
|
|
|
nsCycleCollectionParticipant* mParticipant;
|
2014-05-05 21:30:39 +04:00
|
|
|
uint32_t mColor : 2;
|
|
|
|
uint32_t mInternalRefs : 30;
|
|
|
|
uint32_t mRefCount;
|
2011-06-10 01:55:04 +04:00
|
|
|
private:
|
2014-05-05 21:30:39 +04:00
|
|
|
EdgePool::Iterator mFirstChild;
|
2007-04-20 12:01:01 +04:00
|
|
|
|
2014-07-01 03:18:47 +04:00
|
|
|
static const uint32_t kInitialRefCount = UINT32_MAX - 1;
|
|
|
|
|
2011-06-10 01:55:04 +04:00
|
|
|
public:
|
2007-01-05 01:31:26 +03:00
|
|
|
|
2014-05-13 21:41:38 +04:00
|
|
|
PtrInfo(void* aPointer, nsCycleCollectionParticipant* aParticipant)
|
2014-05-05 21:30:39 +04:00
|
|
|
: mPointer(aPointer),
|
|
|
|
mParticipant(aParticipant),
|
|
|
|
mColor(grey),
|
|
|
|
mInternalRefs(0),
|
2014-07-01 03:18:47 +04:00
|
|
|
mRefCount(kInitialRefCount),
|
2014-05-05 21:30:39 +04:00
|
|
|
mFirstChild()
|
|
|
|
{
|
2014-05-14 20:45:50 +04:00
|
|
|
MOZ_ASSERT(aParticipant);
|
|
|
|
|
2014-05-05 21:30:39 +04:00
|
|
|
// We initialize mRefCount to a large non-zero value so
|
|
|
|
// that it doesn't look like a JS object to the cycle collector
|
|
|
|
// in the case where the object dies before being traversed.
|
2014-05-14 20:45:50 +04:00
|
|
|
MOZ_ASSERT(!IsGrayJS() && !IsBlackJS());
|
2014-05-05 21:30:39 +04:00
|
|
|
}
|
2007-04-20 12:01:01 +04:00
|
|
|
|
2014-05-05 21:30:39 +04:00
|
|
|
// Allow NodePool::Block's constructor to compile.
|
2014-05-13 21:41:38 +04:00
|
|
|
PtrInfo()
|
|
|
|
{
|
2014-05-05 21:30:39 +04:00
|
|
|
NS_NOTREACHED("should never be called");
|
|
|
|
}
|
2011-06-10 01:55:04 +04:00
|
|
|
|
2014-05-14 20:45:50 +04:00
|
|
|
bool IsGrayJS() const
|
|
|
|
{
|
|
|
|
return mRefCount == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool IsBlackJS() const
|
|
|
|
{
|
|
|
|
return mRefCount == UINT32_MAX;
|
|
|
|
}
|
|
|
|
|
2014-07-01 03:18:47 +04:00
|
|
|
bool WasTraversed() const
|
|
|
|
{
|
|
|
|
return mRefCount != kInitialRefCount;
|
|
|
|
}
|
|
|
|
|
2014-05-14 20:45:50 +04:00
|
|
|
EdgePool::Iterator FirstChild() const
|
2014-05-05 21:30:39 +04:00
|
|
|
{
|
|
|
|
CC_GRAPH_ASSERT(mFirstChild.Initialized());
|
|
|
|
return mFirstChild;
|
|
|
|
}
|
2011-06-10 01:55:04 +04:00
|
|
|
|
2014-05-05 21:30:39 +04:00
|
|
|
// this PtrInfo must be part of a NodePool
|
2014-05-14 20:45:50 +04:00
|
|
|
EdgePool::Iterator LastChild() const
|
2014-05-05 21:30:39 +04:00
|
|
|
{
|
|
|
|
CC_GRAPH_ASSERT((this + 1)->mFirstChild.Initialized());
|
|
|
|
return (this + 1)->mFirstChild;
|
|
|
|
}
|
2011-06-10 01:55:04 +04:00
|
|
|
|
2014-05-05 21:30:39 +04:00
|
|
|
void SetFirstChild(EdgePool::Iterator aFirstChild)
|
|
|
|
{
|
|
|
|
CC_GRAPH_ASSERT(aFirstChild.Initialized());
|
|
|
|
mFirstChild = aFirstChild;
|
|
|
|
}
|
2011-06-10 01:55:04 +04:00
|
|
|
|
2014-05-05 21:30:39 +04:00
|
|
|
// this PtrInfo must be part of a NodePool
|
|
|
|
void SetLastChild(EdgePool::Iterator aLastChild)
|
|
|
|
{
|
|
|
|
CC_GRAPH_ASSERT(aLastChild.Initialized());
|
|
|
|
(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:
|
2014-05-14 07:42:27 +04:00
|
|
|
// The -2 allows us to use |BlockSize + 1| for |mEntries|, and fit |mNext|,
|
|
|
|
// all without causing slop.
|
|
|
|
enum { BlockSize = 8 * 1024 - 2 };
|
2007-04-20 12:01:01 +04:00
|
|
|
|
2014-05-13 21:41:38 +04:00
|
|
|
struct Block
|
|
|
|
{
|
2014-05-05 21:30:39 +04:00
|
|
|
// We create and destroy Block using NS_Alloc/NS_Free rather
|
|
|
|
// than new and delete to avoid calling its constructor and
|
|
|
|
// destructor.
|
2014-05-13 21:41:38 +04:00
|
|
|
Block()
|
|
|
|
{
|
|
|
|
NS_NOTREACHED("should never be called");
|
2014-05-14 07:42:27 +04:00
|
|
|
|
|
|
|
// Ensure Block is the right size (see the comment on BlockSize above).
|
|
|
|
static_assert(
|
|
|
|
sizeof(Block) == 163824 || // 32-bit; equals 39.997 pages
|
|
|
|
sizeof(Block) == 262120, // 64-bit; equals 63.994 pages
|
|
|
|
"ill-sized NodePool::Block"
|
|
|
|
);
|
2014-05-13 21:41:38 +04:00
|
|
|
}
|
|
|
|
~Block()
|
|
|
|
{
|
|
|
|
NS_NOTREACHED("should never be called");
|
|
|
|
}
|
2008-03-18 02:11:08 +03:00
|
|
|
|
2014-05-05 21:30:39 +04:00
|
|
|
Block* mNext;
|
|
|
|
PtrInfo mEntries[BlockSize + 1]; // +1 to store last child of last node
|
|
|
|
};
|
2007-04-26 01:12:11 +04:00
|
|
|
|
|
|
|
public:
|
2014-05-05 21:30:39 +04:00
|
|
|
NodePool()
|
2014-05-13 21:41:38 +04:00
|
|
|
: mBlocks(nullptr)
|
|
|
|
, mLast(nullptr)
|
2014-05-05 21:30:39 +04:00
|
|
|
{
|
|
|
|
}
|
2007-04-26 01:12:11 +04:00
|
|
|
|
2014-05-05 21:30:39 +04:00
|
|
|
~NodePool()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(!mBlocks, "Didn't call Clear()?");
|
|
|
|
}
|
|
|
|
|
|
|
|
void Clear()
|
|
|
|
{
|
2014-05-13 21:41:38 +04:00
|
|
|
Block* b = mBlocks;
|
2014-05-05 21:30:39 +04:00
|
|
|
while (b) {
|
2014-05-13 21:41:38 +04:00
|
|
|
Block* n = b->mNext;
|
2014-05-05 21:30:39 +04:00
|
|
|
NS_Free(b);
|
|
|
|
b = n;
|
2008-01-10 17:10:03 +03:00
|
|
|
}
|
|
|
|
|
2014-05-05 21:30:39 +04:00
|
|
|
mBlocks = nullptr;
|
|
|
|
mLast = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
bool IsEmpty()
|
|
|
|
{
|
|
|
|
return !mBlocks && !mLast;
|
|
|
|
}
|
|
|
|
#endif
|
2008-01-10 17:10:03 +03:00
|
|
|
|
2014-05-05 21:30:39 +04:00
|
|
|
class Builder;
|
|
|
|
friend class Builder;
|
2014-05-13 21:41:38 +04:00
|
|
|
class Builder
|
|
|
|
{
|
2014-05-05 21:30:39 +04:00
|
|
|
public:
|
2014-07-28 21:19:06 +04:00
|
|
|
explicit Builder(NodePool& aPool)
|
2014-05-13 21:41:38 +04:00
|
|
|
: mNextBlock(&aPool.mBlocks)
|
|
|
|
, mNext(aPool.mLast)
|
|
|
|
, mBlockEnd(nullptr)
|
2014-05-05 21:30:39 +04:00
|
|
|
{
|
2014-05-13 21:41:38 +04:00
|
|
|
MOZ_ASSERT(!aPool.mBlocks && !aPool.mLast, "pool not empty");
|
2014-05-05 21:30:39 +04:00
|
|
|
}
|
2014-05-13 21:41:38 +04:00
|
|
|
PtrInfo* Add(void* aPointer, nsCycleCollectionParticipant* aParticipant)
|
2014-05-05 21:30:39 +04:00
|
|
|
{
|
|
|
|
if (mNext == mBlockEnd) {
|
2014-05-13 21:41:38 +04:00
|
|
|
Block* block = static_cast<Block*>(NS_Alloc(sizeof(Block)));
|
2014-05-05 21:30:39 +04:00
|
|
|
*mNextBlock = block;
|
|
|
|
mNext = block->mEntries;
|
|
|
|
mBlockEnd = block->mEntries + BlockSize;
|
|
|
|
block->mNext = nullptr;
|
|
|
|
mNextBlock = &block->mNext;
|
|
|
|
}
|
|
|
|
return new (mNext++) PtrInfo(aPointer, aParticipant);
|
2007-04-20 12:01:01 +04:00
|
|
|
}
|
2014-05-05 21:30:39 +04:00
|
|
|
private:
|
2014-05-13 21:41:38 +04:00
|
|
|
Block** mNextBlock;
|
|
|
|
PtrInfo*& mNext;
|
|
|
|
PtrInfo* mBlockEnd;
|
2014-05-05 21:30:39 +04:00
|
|
|
};
|
2007-04-20 12:01:01 +04:00
|
|
|
|
2014-05-05 21:30:39 +04:00
|
|
|
class Enumerator;
|
|
|
|
friend class Enumerator;
|
2014-05-13 21:41:38 +04:00
|
|
|
class Enumerator
|
|
|
|
{
|
2014-05-05 21:30:39 +04:00
|
|
|
public:
|
2014-07-28 21:19:06 +04:00
|
|
|
explicit Enumerator(NodePool& aPool)
|
2014-05-13 21:41:38 +04:00
|
|
|
: mFirstBlock(aPool.mBlocks)
|
|
|
|
, mCurBlock(nullptr)
|
|
|
|
, mNext(nullptr)
|
|
|
|
, mBlockEnd(nullptr)
|
|
|
|
, mLast(aPool.mLast)
|
2013-12-06 22:17:20 +04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2014-05-05 21:30:39 +04:00
|
|
|
bool IsDone() const
|
|
|
|
{
|
|
|
|
return mNext == mLast;
|
|
|
|
}
|
2007-04-20 12:01:01 +04:00
|
|
|
|
2014-05-05 21:30:39 +04:00
|
|
|
bool AtBlockEnd() const
|
|
|
|
{
|
|
|
|
return mNext == mBlockEnd;
|
|
|
|
}
|
2007-04-20 12:01:01 +04:00
|
|
|
|
2014-05-05 21:30:39 +04:00
|
|
|
PtrInfo* GetNext()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(!IsDone(), "calling GetNext when done");
|
|
|
|
if (mNext == mBlockEnd) {
|
2014-05-13 21:41:38 +04:00
|
|
|
Block* nextBlock = mCurBlock ? mCurBlock->mNext : mFirstBlock;
|
2014-05-05 21:30:39 +04:00
|
|
|
mNext = nextBlock->mEntries;
|
|
|
|
mBlockEnd = mNext + BlockSize;
|
|
|
|
mCurBlock = nextBlock;
|
|
|
|
}
|
|
|
|
return mNext++;
|
|
|
|
}
|
|
|
|
private:
|
|
|
|
// mFirstBlock is a reference to allow an Enumerator to be constructed
|
|
|
|
// for an empty graph.
|
2014-05-13 21:41:38 +04:00
|
|
|
Block*& mFirstBlock;
|
|
|
|
Block* mCurBlock;
|
2014-05-05 21:30:39 +04:00
|
|
|
// mNext is the next value we want to return, unless mNext == mBlockEnd
|
|
|
|
// NB: mLast is a reference to allow enumerating while building!
|
2014-05-13 21:41:38 +04:00
|
|
|
PtrInfo* mNext;
|
|
|
|
PtrInfo* mBlockEnd;
|
|
|
|
PtrInfo*& mLast;
|
2014-05-05 21:30:39 +04:00
|
|
|
};
|
2011-06-10 01:55:29 +04:00
|
|
|
|
2014-05-13 21:41:38 +04:00
|
|
|
size_t SizeOfExcludingThis(MallocSizeOf aMallocSizeOf) const
|
|
|
|
{
|
2014-05-05 21:30:39 +04:00
|
|
|
// We don't measure the things pointed to by mEntries[] because those
|
|
|
|
// pointers are non-owning.
|
|
|
|
size_t n = 0;
|
2014-05-13 21:41:38 +04:00
|
|
|
Block* b = mBlocks;
|
2014-05-05 21:30:39 +04:00
|
|
|
while (b) {
|
|
|
|
n += aMallocSizeOf(b);
|
|
|
|
b = b->mNext;
|
2011-07-09 02:49:31 +04:00
|
|
|
}
|
2014-05-05 21:30:39 +04:00
|
|
|
return n;
|
|
|
|
}
|
2011-07-09 02:49:31 +04:00
|
|
|
|
2007-04-26 01:12:11 +04:00
|
|
|
private:
|
2014-05-13 21:41:38 +04:00
|
|
|
Block* mBlocks;
|
|
|
|
PtrInfo* mLast;
|
2007-01-05 01:31:26 +03:00
|
|
|
};
|
|
|
|
|
2007-06-19 05:29:10 +04:00
|
|
|
|
2013-11-21 02:35:17 +04:00
|
|
|
// Declarations for mPtrToNodeMap.
|
|
|
|
|
|
|
|
struct PtrToNodeEntry : public PLDHashEntryHdr
|
|
|
|
{
|
2014-05-05 21:30:39 +04:00
|
|
|
// The key is mNode->mPointer
|
2014-05-13 21:41:38 +04:00
|
|
|
PtrInfo* mNode;
|
2013-11-21 02:35:17 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
static bool
|
2014-05-13 21:41:38 +04:00
|
|
|
PtrToNodeMatchEntry(PLDHashTable* aTable,
|
|
|
|
const PLDHashEntryHdr* aEntry,
|
|
|
|
const void* aKey)
|
2013-11-21 02:35:17 +04:00
|
|
|
{
|
2014-05-13 21:41:38 +04:00
|
|
|
const PtrToNodeEntry* n = static_cast<const PtrToNodeEntry*>(aEntry);
|
|
|
|
return n->mNode->mPointer == aKey;
|
2013-11-21 02:35:17 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static PLDHashTableOps PtrNodeOps = {
|
2014-05-05 21:30:39 +04:00
|
|
|
PL_DHashVoidPtrKeyStub,
|
|
|
|
PtrToNodeMatchEntry,
|
|
|
|
PL_DHashMoveEntryStub,
|
|
|
|
PL_DHashClearEntryStub,
|
|
|
|
nullptr
|
2013-11-21 02:35:17 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2011-11-24 16:35:56 +04:00
|
|
|
struct WeakMapping
|
|
|
|
{
|
2014-05-05 21:30:39 +04:00
|
|
|
// map and key will be null if the corresponding objects are GC marked
|
2014-05-13 21:41:38 +04:00
|
|
|
PtrInfo* mMap;
|
|
|
|
PtrInfo* mKey;
|
|
|
|
PtrInfo* mKeyDelegate;
|
|
|
|
PtrInfo* mVal;
|
2011-11-24 16:35:56 +04:00
|
|
|
};
|
|
|
|
|
2014-07-09 23:31:00 +04:00
|
|
|
class CCGraphBuilder;
|
2007-06-19 05:29:10 +04:00
|
|
|
|
2014-07-09 23:31:00 +04:00
|
|
|
struct CCGraph
|
2007-04-26 01:12:11 +04:00
|
|
|
{
|
2014-05-05 21:30:39 +04:00
|
|
|
NodePool mNodes;
|
|
|
|
EdgePool mEdges;
|
|
|
|
nsTArray<WeakMapping> mWeakMaps;
|
|
|
|
uint32_t mRootCount;
|
2007-04-26 01:12:11 +04:00
|
|
|
|
2013-11-21 02:35:17 +04:00
|
|
|
private:
|
2014-05-05 21:30:39 +04:00
|
|
|
PLDHashTable mPtrToNodeMap;
|
2013-11-21 02:35:17 +04:00
|
|
|
|
|
|
|
public:
|
2015-01-20 03:01:24 +03:00
|
|
|
CCGraph() : mRootCount(0) {}
|
2013-11-21 02:35:17 +04:00
|
|
|
|
2014-07-09 23:31:00 +04:00
|
|
|
~CCGraph()
|
2014-05-05 21:30:39 +04:00
|
|
|
{
|
2015-01-20 03:11:34 +03:00
|
|
|
if (mPtrToNodeMap.IsInitialized()) {
|
2014-05-05 21:30:39 +04:00
|
|
|
PL_DHashTableFinish(&mPtrToNodeMap);
|
2007-06-19 05:29:10 +04:00
|
|
|
}
|
2014-05-05 21:30:39 +04:00
|
|
|
}
|
2013-11-21 02:35:17 +04:00
|
|
|
|
2014-05-05 21:30:39 +04:00
|
|
|
void Init()
|
|
|
|
{
|
2014-07-09 23:31:00 +04:00
|
|
|
MOZ_ASSERT(IsEmpty(), "Failed to call CCGraph::Clear");
|
2015-01-14 03:42:13 +03:00
|
|
|
PL_DHashTableInit(&mPtrToNodeMap, &PtrNodeOps,
|
2014-08-06 17:31:21 +04:00
|
|
|
sizeof(PtrToNodeEntry), 16384);
|
2014-05-05 21:30:39 +04:00
|
|
|
}
|
2011-07-09 02:49:31 +04:00
|
|
|
|
2014-05-05 21:30:39 +04:00
|
|
|
void Clear()
|
|
|
|
{
|
|
|
|
mNodes.Clear();
|
|
|
|
mEdges.Clear();
|
|
|
|
mWeakMaps.Clear();
|
|
|
|
mRootCount = 0;
|
|
|
|
PL_DHashTableFinish(&mPtrToNodeMap);
|
|
|
|
}
|
2013-11-21 02:35:17 +04:00
|
|
|
|
2013-12-06 22:17:20 +04:00
|
|
|
#ifdef DEBUG
|
2014-05-05 21:30:39 +04:00
|
|
|
bool IsEmpty()
|
|
|
|
{
|
|
|
|
return mNodes.IsEmpty() && mEdges.IsEmpty() &&
|
|
|
|
mWeakMaps.IsEmpty() && mRootCount == 0 &&
|
2015-01-20 03:11:34 +03:00
|
|
|
!mPtrToNodeMap.IsInitialized();
|
2014-05-05 21:30:39 +04:00
|
|
|
}
|
2013-12-06 22:17:20 +04:00
|
|
|
#endif
|
|
|
|
|
2014-05-13 21:41:38 +04:00
|
|
|
PtrInfo* FindNode(void* aPtr);
|
|
|
|
PtrToNodeEntry* AddNodeToMap(void* aPtr);
|
|
|
|
void RemoveNodeFromMap(void* aPtr);
|
2013-11-21 02:35:17 +04:00
|
|
|
|
2014-05-05 21:30:39 +04:00
|
|
|
uint32_t MapCount() const
|
|
|
|
{
|
2014-08-26 03:56:33 +04:00
|
|
|
return mPtrToNodeMap.EntryCount();
|
2014-05-05 21:30:39 +04:00
|
|
|
}
|
2013-09-10 19:56:36 +04:00
|
|
|
|
2014-05-05 21:30:39 +04:00
|
|
|
void SizeOfExcludingThis(MallocSizeOf aMallocSizeOf,
|
2014-05-13 21:41:38 +04:00
|
|
|
size_t* aNodesSize, size_t* aEdgesSize,
|
|
|
|
size_t* aWeakMapsSize) const
|
|
|
|
{
|
2014-05-05 21:30:39 +04:00
|
|
|
*aNodesSize = mNodes.SizeOfExcludingThis(aMallocSizeOf);
|
|
|
|
*aEdgesSize = mEdges.SizeOfExcludingThis(aMallocSizeOf);
|
2011-07-09 02:49:31 +04:00
|
|
|
|
2014-05-05 21:30:39 +04:00
|
|
|
// We don't measure what the WeakMappings point to, because the
|
|
|
|
// pointers are non-owning.
|
|
|
|
*aWeakMapsSize = mWeakMaps.SizeOfExcludingThis(aMallocSizeOf);
|
|
|
|
}
|
2007-04-26 01:12:11 +04:00
|
|
|
};
|
2007-01-05 01:31:26 +03:00
|
|
|
|
2013-11-21 02:35:17 +04:00
|
|
|
PtrInfo*
|
2014-07-09 23:31:00 +04:00
|
|
|
CCGraph::FindNode(void* aPtr)
|
2013-11-21 02:35:17 +04:00
|
|
|
{
|
2014-05-13 21:41:38 +04:00
|
|
|
PtrToNodeEntry* e =
|
2015-01-23 08:06:55 +03:00
|
|
|
static_cast<PtrToNodeEntry*>(PL_DHashTableSearch(&mPtrToNodeMap, aPtr));
|
|
|
|
return e ? e->mNode : nullptr;
|
2013-11-21 02:35:17 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
PtrToNodeEntry*
|
2014-07-09 23:31:00 +04:00
|
|
|
CCGraph::AddNodeToMap(void* aPtr)
|
2013-11-21 02:35:17 +04:00
|
|
|
{
|
2014-12-05 20:38:32 +03:00
|
|
|
JS::AutoSuppressGCAnalysis suppress;
|
2015-02-03 01:48:58 +03:00
|
|
|
PtrToNodeEntry* e = static_cast<PtrToNodeEntry*>
|
|
|
|
(PL_DHashTableAdd(&mPtrToNodeMap, aPtr, fallible));
|
2015-02-11 01:39:49 +03:00
|
|
|
if (!e) {
|
|
|
|
// Caller should track OOMs
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
return e;
|
2013-11-21 02:35:17 +04:00
|
|
|
}
|
|
|
|
|
2013-12-06 22:17:20 +04:00
|
|
|
void
|
2014-07-09 23:31:00 +04:00
|
|
|
CCGraph::RemoveNodeFromMap(void* aPtr)
|
2013-12-06 22:17:20 +04:00
|
|
|
{
|
2015-01-06 05:26:50 +03:00
|
|
|
PL_DHashTableRemove(&mPtrToNodeMap, aPtr);
|
2013-12-06 22:17:20 +04:00
|
|
|
}
|
|
|
|
|
2013-11-21 02:35:17 +04:00
|
|
|
|
2014-05-13 21:41:38 +04:00
|
|
|
static nsISupports*
|
|
|
|
CanonicalizeXPCOMParticipant(nsISupports* aIn)
|
2012-08-24 20:50:06 +04:00
|
|
|
{
|
2014-05-05 21:30:39 +04:00
|
|
|
nsISupports* out;
|
2014-05-13 21:41:38 +04:00
|
|
|
aIn->QueryInterface(NS_GET_IID(nsCycleCollectionISupports),
|
|
|
|
reinterpret_cast<void**>(&out));
|
2014-05-05 21:30:39 +04:00
|
|
|
return out;
|
2012-08-24 20:50:06 +04:00
|
|
|
}
|
|
|
|
|
2009-06-01 23:22:18 +04:00
|
|
|
static inline void
|
2014-05-13 21:41:38 +04:00
|
|
|
ToParticipant(nsISupports* aPtr, nsXPCOMCycleCollectionParticipant** aCp);
|
2009-06-01 23:22:18 +04:00
|
|
|
|
2012-08-24 20:50:06 +04:00
|
|
|
static void
|
2014-05-13 21:41:38 +04:00
|
|
|
CanonicalizeParticipant(void** aParti, nsCycleCollectionParticipant** aCp)
|
2012-08-24 20:50:06 +04:00
|
|
|
{
|
2014-05-05 21:30:39 +04:00
|
|
|
// If the participant is null, this is an nsISupports participant,
|
|
|
|
// so we must QI to get the real participant.
|
2012-08-24 20:50:06 +04:00
|
|
|
|
2014-05-13 21:41:38 +04:00
|
|
|
if (!*aCp) {
|
|
|
|
nsISupports* nsparti = static_cast<nsISupports*>(*aParti);
|
2014-05-05 21:30:39 +04:00
|
|
|
nsparti = CanonicalizeXPCOMParticipant(nsparti);
|
|
|
|
NS_ASSERTION(nsparti,
|
|
|
|
"Don't add objects that don't participate in collection!");
|
2014-05-13 21:41:38 +04:00
|
|
|
nsXPCOMCycleCollectionParticipant* xcp;
|
2014-05-05 21:30:39 +04:00
|
|
|
ToParticipant(nsparti, &xcp);
|
2014-05-13 21:41:38 +04:00
|
|
|
*aParti = nsparti;
|
|
|
|
*aCp = xcp;
|
2014-05-05 21:30:39 +04:00
|
|
|
}
|
2012-08-24 20:50:06 +04:00
|
|
|
}
|
|
|
|
|
2014-05-13 21:41:38 +04:00
|
|
|
struct nsPurpleBufferEntry
|
|
|
|
{
|
2014-08-25 23:17:15 +04:00
|
|
|
union
|
|
|
|
{
|
2014-05-13 21:41:38 +04:00
|
|
|
void* mObject; // when low bit unset
|
|
|
|
nsPurpleBufferEntry* mNextInFreeList; // when low bit set
|
2013-11-26 23:29:59 +04:00
|
|
|
};
|
|
|
|
|
2014-05-13 21:41:38 +04:00
|
|
|
nsCycleCollectingAutoRefCnt* mRefCnt;
|
2013-11-26 23:29:59 +04:00
|
|
|
|
2014-05-13 21:41:38 +04:00
|
|
|
nsCycleCollectionParticipant* mParticipant; // nullptr for nsISupports
|
2013-11-26 23:29:59 +04:00
|
|
|
};
|
|
|
|
|
2013-07-27 14:48:45 +04:00
|
|
|
class nsCycleCollector;
|
|
|
|
|
2007-01-05 01:31:26 +03:00
|
|
|
struct nsPurpleBuffer
|
|
|
|
{
|
2009-05-07 00:46:04 +04:00
|
|
|
private:
|
2014-05-13 21:41:38 +04:00
|
|
|
struct Block
|
|
|
|
{
|
|
|
|
Block* mNext;
|
2014-05-05 21:30:39 +04:00
|
|
|
// Try to match the size of a jemalloc bucket, to minimize slop bytes.
|
|
|
|
// - On 32-bit platforms sizeof(nsPurpleBufferEntry) is 12, so mEntries
|
|
|
|
// is 16,380 bytes, which leaves 4 bytes for mNext.
|
|
|
|
// - On 64-bit platforms sizeof(nsPurpleBufferEntry) is 24, so mEntries
|
|
|
|
// is 32,544 bytes, which leaves 8 bytes for mNext.
|
|
|
|
nsPurpleBufferEntry mEntries[1365];
|
|
|
|
|
2014-05-13 21:41:38 +04:00
|
|
|
Block() : mNext(nullptr)
|
|
|
|
{
|
2014-05-05 21:30:39 +04:00
|
|
|
// Ensure Block is the right size (see above).
|
|
|
|
static_assert(
|
|
|
|
sizeof(Block) == 16384 || // 32-bit
|
|
|
|
sizeof(Block) == 32768, // 64-bit
|
|
|
|
"ill-sized nsPurpleBuffer::Block"
|
|
|
|
);
|
|
|
|
}
|
2013-04-30 21:41:23 +04:00
|
|
|
|
2014-08-25 23:17:15 +04:00
|
|
|
template<class PurpleVisitor>
|
2014-05-13 21:41:38 +04:00
|
|
|
void VisitEntries(nsPurpleBuffer& aBuffer, PurpleVisitor& aVisitor)
|
2014-05-05 21:30:39 +04:00
|
|
|
{
|
2014-05-13 21:41:38 +04:00
|
|
|
nsPurpleBufferEntry* eEnd = ArrayEnd(mEntries);
|
|
|
|
for (nsPurpleBufferEntry* e = mEntries; e != eEnd; ++e) {
|
2014-11-04 00:50:00 +03:00
|
|
|
MOZ_ASSERT(e->mObject, "There should be no null mObject when we iterate over the purple buffer");
|
|
|
|
if (!(uintptr_t(e->mObject) & uintptr_t(1)) && e->mObject) {
|
2014-05-05 21:30:39 +04:00
|
|
|
aVisitor.Visit(aBuffer, e);
|
2013-04-30 21:41:23 +04:00
|
|
|
}
|
2014-05-05 21:30:39 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
// This class wraps a linked list of the elements in the purple
|
|
|
|
// buffer.
|
2007-01-05 01:31:26 +03:00
|
|
|
|
2014-05-05 21:30:39 +04:00
|
|
|
uint32_t mCount;
|
|
|
|
Block mFirstBlock;
|
2014-05-13 21:41:38 +04:00
|
|
|
nsPurpleBufferEntry* mFreeList;
|
2009-05-07 00:46:04 +04:00
|
|
|
|
2013-04-30 21:41:23 +04:00
|
|
|
public:
|
2014-05-05 21:30:39 +04:00
|
|
|
nsPurpleBuffer()
|
|
|
|
{
|
|
|
|
InitBlocks();
|
|
|
|
}
|
2007-01-05 01:31:26 +03:00
|
|
|
|
2014-05-05 21:30:39 +04:00
|
|
|
~nsPurpleBuffer()
|
|
|
|
{
|
|
|
|
FreeBlocks();
|
|
|
|
}
|
2007-01-05 01:31:26 +03:00
|
|
|
|
2014-08-25 23:17:15 +04:00
|
|
|
template<class PurpleVisitor>
|
2014-05-13 21:41:38 +04:00
|
|
|
void VisitEntries(PurpleVisitor& aVisitor)
|
2014-05-05 21:30:39 +04:00
|
|
|
{
|
2014-05-13 21:41:38 +04:00
|
|
|
for (Block* b = &mFirstBlock; b; b = b->mNext) {
|
2014-05-05 21:30:39 +04:00
|
|
|
b->VisitEntries(*this, aVisitor);
|
2013-04-30 21:41:23 +04:00
|
|
|
}
|
2014-05-05 21:30:39 +04:00
|
|
|
}
|
2013-04-30 21:41:23 +04:00
|
|
|
|
2014-05-05 21:30:39 +04:00
|
|
|
void InitBlocks()
|
|
|
|
{
|
|
|
|
mCount = 0;
|
|
|
|
mFreeList = nullptr;
|
|
|
|
StartBlock(&mFirstBlock);
|
|
|
|
}
|
2007-04-21 21:43:19 +04:00
|
|
|
|
2014-05-13 21:41:38 +04:00
|
|
|
void StartBlock(Block* aBlock)
|
2014-05-05 21:30:39 +04:00
|
|
|
{
|
2015-02-10 01:34:50 +03:00
|
|
|
MOZ_ASSERT(!mFreeList, "should not have free list");
|
2007-01-05 01:31:26 +03:00
|
|
|
|
2014-05-05 21:30:39 +04:00
|
|
|
// Put all the entries in the block on the free list.
|
2014-05-13 21:41:38 +04:00
|
|
|
nsPurpleBufferEntry* entries = aBlock->mEntries;
|
2014-05-05 21:30:39 +04:00
|
|
|
mFreeList = entries;
|
|
|
|
for (uint32_t i = 1; i < ArrayLength(aBlock->mEntries); ++i) {
|
|
|
|
entries[i - 1].mNextInFreeList =
|
|
|
|
(nsPurpleBufferEntry*)(uintptr_t(entries + i) | 1);
|
2007-01-05 01:31:26 +03:00
|
|
|
}
|
2014-05-05 21:30:39 +04:00
|
|
|
entries[ArrayLength(aBlock->mEntries) - 1].mNextInFreeList =
|
|
|
|
(nsPurpleBufferEntry*)1;
|
|
|
|
}
|
2007-01-05 01:31:26 +03:00
|
|
|
|
2014-05-05 21:30:39 +04:00
|
|
|
void FreeBlocks()
|
|
|
|
{
|
2014-05-13 21:41:38 +04:00
|
|
|
if (mCount > 0) {
|
2014-05-05 21:30:39 +04:00
|
|
|
UnmarkRemainingPurple(&mFirstBlock);
|
2014-05-13 21:41:38 +04:00
|
|
|
}
|
|
|
|
Block* b = mFirstBlock.mNext;
|
2014-05-05 21:30:39 +04:00
|
|
|
while (b) {
|
2014-05-13 21:41:38 +04:00
|
|
|
if (mCount > 0) {
|
2014-05-05 21:30:39 +04:00
|
|
|
UnmarkRemainingPurple(b);
|
2014-05-13 21:41:38 +04:00
|
|
|
}
|
|
|
|
Block* next = b->mNext;
|
2014-05-05 21:30:39 +04:00
|
|
|
delete b;
|
|
|
|
b = next;
|
|
|
|
}
|
|
|
|
mFirstBlock.mNext = nullptr;
|
|
|
|
}
|
2013-04-30 21:41:23 +04:00
|
|
|
|
2014-05-05 21:30:39 +04:00
|
|
|
struct UnmarkRemainingPurpleVisitor
|
|
|
|
{
|
|
|
|
void
|
2014-05-13 21:41:38 +04:00
|
|
|
Visit(nsPurpleBuffer& aBuffer, nsPurpleBufferEntry* aEntry)
|
2013-04-30 21:41:23 +04:00
|
|
|
{
|
2014-05-05 21:30:39 +04:00
|
|
|
if (aEntry->mRefCnt) {
|
|
|
|
aEntry->mRefCnt->RemoveFromPurpleBuffer();
|
|
|
|
aEntry->mRefCnt = nullptr;
|
|
|
|
}
|
|
|
|
aEntry->mObject = nullptr;
|
|
|
|
--aBuffer.mCount;
|
2009-06-01 23:22:18 +04:00
|
|
|
}
|
2014-05-05 21:30:39 +04:00
|
|
|
};
|
2009-06-01 23:22:18 +04:00
|
|
|
|
2014-05-13 21:41:38 +04:00
|
|
|
void UnmarkRemainingPurple(Block* aBlock)
|
2014-05-05 21:30:39 +04:00
|
|
|
{
|
|
|
|
UnmarkRemainingPurpleVisitor visitor;
|
2014-05-13 21:41:38 +04:00
|
|
|
aBlock->VisitEntries(*this, visitor);
|
2014-05-05 21:30:39 +04:00
|
|
|
}
|
2009-05-07 00:46:04 +04:00
|
|
|
|
2014-07-09 23:31:00 +04:00
|
|
|
void SelectPointers(CCGraphBuilder& aBuilder);
|
2014-05-05 21:30:39 +04:00
|
|
|
|
|
|
|
// RemoveSkippable removes entries from the purple buffer synchronously
|
|
|
|
// (1) if aAsyncSnowWhiteFreeing is false and nsPurpleBufferEntry::mRefCnt is 0 or
|
|
|
|
// (2) if the object's nsXPCOMCycleCollectionParticipant::CanSkip() returns true or
|
|
|
|
// (3) if nsPurpleBufferEntry::mRefCnt->IsPurple() is false.
|
|
|
|
// (4) If removeChildlessNodes is true, then any nodes in the purple buffer
|
|
|
|
// that will have no children in the cycle collector graph will also be
|
|
|
|
// removed. CanSkip() may be run on these children.
|
|
|
|
void RemoveSkippable(nsCycleCollector* aCollector,
|
2014-05-13 21:41:38 +04:00
|
|
|
bool aRemoveChildlessNodes,
|
2014-05-05 21:30:39 +04:00
|
|
|
bool aAsyncSnowWhiteFreeing,
|
|
|
|
CC_ForgetSkippableCallback aCb);
|
|
|
|
|
|
|
|
MOZ_ALWAYS_INLINE nsPurpleBufferEntry* NewEntry()
|
|
|
|
{
|
|
|
|
if (MOZ_UNLIKELY(!mFreeList)) {
|
2014-05-13 21:41:38 +04:00
|
|
|
Block* b = new Block;
|
2014-05-05 21:30:39 +04:00
|
|
|
StartBlock(b);
|
2009-05-07 00:46:04 +04:00
|
|
|
|
2014-05-05 21:30:39 +04:00
|
|
|
// 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
|
|
|
}
|
2008-02-25 20:47:25 +03:00
|
|
|
|
2014-05-13 21:41:38 +04:00
|
|
|
nsPurpleBufferEntry* e = mFreeList;
|
2014-05-05 21:30:39 +04:00
|
|
|
mFreeList = (nsPurpleBufferEntry*)
|
|
|
|
(uintptr_t(mFreeList->mNextInFreeList) & ~uintptr_t(1));
|
|
|
|
return e;
|
|
|
|
}
|
2009-05-07 00:46:04 +04:00
|
|
|
|
2014-05-13 21:41:38 +04:00
|
|
|
MOZ_ALWAYS_INLINE void Put(void* aObject, nsCycleCollectionParticipant* aCp,
|
|
|
|
nsCycleCollectingAutoRefCnt* aRefCnt)
|
2014-05-05 21:30:39 +04:00
|
|
|
{
|
2014-05-13 21:41:38 +04:00
|
|
|
nsPurpleBufferEntry* e = NewEntry();
|
2009-05-07 00:46:04 +04:00
|
|
|
|
2014-05-05 21:30:39 +04:00
|
|
|
++mCount;
|
2009-05-07 00:46:04 +04:00
|
|
|
|
2014-05-13 21:41:38 +04:00
|
|
|
e->mObject = aObject;
|
2014-05-05 21:30:39 +04:00
|
|
|
e->mRefCnt = aRefCnt;
|
2014-05-13 21:41:38 +04:00
|
|
|
e->mParticipant = aCp;
|
2014-05-05 21:30:39 +04:00
|
|
|
}
|
2009-05-07 00:46:04 +04:00
|
|
|
|
2014-05-13 21:41:38 +04:00
|
|
|
void Remove(nsPurpleBufferEntry* aEntry)
|
2014-05-05 21:30:39 +04:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(mCount != 0, "must have entries");
|
2009-05-07 00:46:04 +04:00
|
|
|
|
2014-05-13 21:41:38 +04:00
|
|
|
if (aEntry->mRefCnt) {
|
|
|
|
aEntry->mRefCnt->RemoveFromPurpleBuffer();
|
|
|
|
aEntry->mRefCnt = nullptr;
|
2009-05-07 00:46:04 +04:00
|
|
|
}
|
2014-05-13 21:41:38 +04:00
|
|
|
aEntry->mNextInFreeList =
|
2014-05-05 21:30:39 +04:00
|
|
|
(nsPurpleBufferEntry*)(uintptr_t(mFreeList) | uintptr_t(1));
|
2014-05-13 21:41:38 +04:00
|
|
|
mFreeList = aEntry;
|
2011-07-09 02:49:31 +04:00
|
|
|
|
2014-05-05 21:30:39 +04:00
|
|
|
--mCount;
|
|
|
|
}
|
2012-11-07 05:38:29 +04:00
|
|
|
|
2014-05-05 21:30:39 +04:00
|
|
|
uint32_t Count() const
|
|
|
|
{
|
|
|
|
return mCount;
|
|
|
|
}
|
2011-07-09 02:49:31 +04:00
|
|
|
|
2014-05-05 21:30:39 +04:00
|
|
|
size_t SizeOfExcludingThis(MallocSizeOf aMallocSizeOf) const
|
|
|
|
{
|
|
|
|
size_t n = 0;
|
2012-11-07 05:38:29 +04:00
|
|
|
|
2014-05-05 21:30:39 +04:00
|
|
|
// Don't measure mFirstBlock because it's within |this|.
|
2014-05-13 21:41:38 +04:00
|
|
|
const Block* block = mFirstBlock.mNext;
|
2014-05-05 21:30:39 +04:00
|
|
|
while (block) {
|
|
|
|
n += aMallocSizeOf(block);
|
|
|
|
block = block->mNext;
|
2012-11-07 05:38:29 +04:00
|
|
|
}
|
2014-05-05 21:30:39 +04:00
|
|
|
|
|
|
|
// mFreeList is deliberately not measured because it points into
|
|
|
|
// the purple buffer, which is within mFirstBlock and thus within |this|.
|
|
|
|
//
|
|
|
|
// We also don't measure the things pointed to by mEntries[] because
|
|
|
|
// those pointers are non-owning.
|
|
|
|
|
|
|
|
return n;
|
|
|
|
}
|
2009-05-07 00:46:04 +04:00
|
|
|
};
|
2007-01-05 01:31:26 +03:00
|
|
|
|
2012-08-24 20:50:06 +04:00
|
|
|
static bool
|
2014-07-09 23:31:00 +04:00
|
|
|
AddPurpleRoot(CCGraphBuilder& aBuilder, void* aRoot,
|
2014-05-13 21:41:38 +04:00
|
|
|
nsCycleCollectionParticipant* aParti);
|
2012-08-24 20:50:06 +04:00
|
|
|
|
2013-04-30 21:41:23 +04:00
|
|
|
struct SelectPointersVisitor
|
2007-01-05 01:31:26 +03:00
|
|
|
{
|
2014-07-28 21:19:06 +04:00
|
|
|
explicit SelectPointersVisitor(CCGraphBuilder& aBuilder)
|
2014-05-05 21:30:39 +04:00
|
|
|
: mBuilder(aBuilder)
|
2014-05-13 21:41:38 +04:00
|
|
|
{
|
|
|
|
}
|
2013-04-30 21:41:23 +04:00
|
|
|
|
2014-05-05 21:30:39 +04:00
|
|
|
void
|
2014-05-13 21:41:38 +04:00
|
|
|
Visit(nsPurpleBuffer& aBuffer, nsPurpleBufferEntry* aEntry)
|
2014-05-05 21:30:39 +04:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(aEntry->mObject, "Null object in purple buffer");
|
|
|
|
MOZ_ASSERT(aEntry->mRefCnt->get() != 0,
|
|
|
|
"SelectPointersVisitor: snow-white object in the purple buffer");
|
|
|
|
if (!aEntry->mRefCnt->IsPurple() ||
|
|
|
|
AddPurpleRoot(mBuilder, aEntry->mObject, aEntry->mParticipant)) {
|
|
|
|
aBuffer.Remove(aEntry);
|
2009-05-07 00:46:04 +04:00
|
|
|
}
|
2014-05-05 21:30:39 +04:00
|
|
|
}
|
2009-05-07 00:46:04 +04:00
|
|
|
|
2013-04-30 21:41:23 +04:00
|
|
|
private:
|
2014-07-09 23:31:00 +04:00
|
|
|
CCGraphBuilder& mBuilder;
|
2013-04-30 21:41:23 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
void
|
2014-07-09 23:31:00 +04:00
|
|
|
nsPurpleBuffer::SelectPointers(CCGraphBuilder& aBuilder)
|
2013-04-30 21:41:23 +04:00
|
|
|
{
|
2014-05-05 21:30:39 +04:00
|
|
|
SelectPointersVisitor visitor(aBuilder);
|
|
|
|
VisitEntries(visitor);
|
2013-04-30 21:41:23 +04:00
|
|
|
|
2014-05-05 21:30:39 +04:00
|
|
|
NS_ASSERTION(mCount == 0, "AddPurpleRoot failed");
|
|
|
|
if (mCount == 0) {
|
|
|
|
FreeBlocks();
|
|
|
|
InitBlocks();
|
|
|
|
}
|
2007-01-05 01:31:26 +03:00
|
|
|
}
|
|
|
|
|
2014-08-25 23:17:15 +04:00
|
|
|
enum ccPhase
|
|
|
|
{
|
2014-05-05 21:30:39 +04:00
|
|
|
IdlePhase,
|
|
|
|
GraphBuildingPhase,
|
|
|
|
ScanAndCollectWhitePhase,
|
|
|
|
CleanupPhase
|
2013-12-03 22:47:46 +04:00
|
|
|
};
|
|
|
|
|
2014-08-25 23:17:15 +04:00
|
|
|
enum ccType
|
|
|
|
{
|
2014-05-05 21:30:39 +04:00
|
|
|
SliceCC, /* If a CC is in progress, continue it. Otherwise, start a new one. */
|
|
|
|
ManualCC, /* Explicitly triggered. */
|
|
|
|
ShutdownCC /* Shutdown CC, used for finding leaks. */
|
2013-04-30 03:41:41 +04:00
|
|
|
};
|
|
|
|
|
2013-06-03 14:14:42 +04:00
|
|
|
#ifdef MOZ_NUWA_PROCESS
|
|
|
|
#include "ipc/Nuwa.h"
|
|
|
|
#endif
|
|
|
|
|
2007-03-27 13:49:06 +04:00
|
|
|
////////////////////////////////////////////////////////////////////////
|
2012-05-03 23:28:11 +04:00
|
|
|
// Top level structure for the cycle collector.
|
2007-03-27 13:49:06 +04:00
|
|
|
////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2013-12-03 22:47:47 +04:00
|
|
|
typedef js::SliceBudget SliceBudget;
|
|
|
|
|
2014-01-15 00:23:59 +04:00
|
|
|
class JSPurpleBuffer;
|
|
|
|
|
2013-12-08 09:39:47 +04:00
|
|
|
class nsCycleCollector : public nsIMemoryReporter
|
2007-01-05 01:31:26 +03:00
|
|
|
{
|
2014-05-05 21:30:39 +04:00
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
NS_DECL_NSIMEMORYREPORTER
|
2013-11-26 03:57:53 +04:00
|
|
|
|
2014-05-05 21:30:39 +04:00
|
|
|
bool mActivelyCollecting;
|
|
|
|
bool mFreeingSnowWhite;
|
|
|
|
// mScanInProgress should be false when we're collecting white objects.
|
|
|
|
bool mScanInProgress;
|
|
|
|
CycleCollectorResults mResults;
|
|
|
|
TimeStamp mCollectionStart;
|
2007-01-05 01:31:26 +03:00
|
|
|
|
2014-05-13 21:41:38 +04:00
|
|
|
CycleCollectedJSRuntime* mJSRuntime;
|
2007-01-05 01:31:26 +03:00
|
|
|
|
2014-05-05 21:30:39 +04:00
|
|
|
ccPhase mIncrementalPhase;
|
2014-07-09 23:31:00 +04:00
|
|
|
CCGraph mGraph;
|
|
|
|
nsAutoPtr<CCGraphBuilder> mBuilder;
|
2014-05-05 21:30:39 +04:00
|
|
|
nsCOMPtr<nsICycleCollectorListener> mListener;
|
2008-01-10 17:10:03 +03:00
|
|
|
|
2015-01-18 21:59:21 +03:00
|
|
|
DebugOnly<void*> mThread;
|
2013-03-26 01:26:00 +04:00
|
|
|
|
2014-05-05 21:30:39 +04:00
|
|
|
nsCycleCollectorParams mParams;
|
2007-01-05 01:31:26 +03:00
|
|
|
|
2014-05-05 21:30:39 +04:00
|
|
|
uint32_t mWhiteNodeCount;
|
2008-02-15 16:12:55 +03:00
|
|
|
|
2014-05-05 21:30:39 +04:00
|
|
|
CC_BeforeUnlinkCallback mBeforeUnlinkCB;
|
|
|
|
CC_ForgetSkippableCallback mForgetSkippableCB;
|
2012-01-14 20:58:05 +04:00
|
|
|
|
2014-05-05 21:30:39 +04:00
|
|
|
nsPurpleBuffer mPurpleBuf;
|
2012-04-25 19:10:09 +04:00
|
|
|
|
2014-05-05 21:30:39 +04:00
|
|
|
uint32_t mUnmergedNeeded;
|
|
|
|
uint32_t mMergedInARow;
|
2013-04-30 03:41:41 +04:00
|
|
|
|
2015-01-18 21:59:21 +03:00
|
|
|
nsRefPtr<JSPurpleBuffer> mJSPurpleBuffer;
|
2014-01-15 00:23:59 +04:00
|
|
|
|
2014-07-01 02:11:53 +04:00
|
|
|
private:
|
|
|
|
virtual ~nsCycleCollector();
|
|
|
|
|
2013-03-26 01:26:00 +04:00
|
|
|
public:
|
2014-05-05 21:30:39 +04:00
|
|
|
nsCycleCollector();
|
2013-09-10 19:56:35 +04:00
|
|
|
|
2014-05-13 21:41:38 +04:00
|
|
|
void RegisterJSRuntime(CycleCollectedJSRuntime* aJSRuntime);
|
2014-05-05 21:30:39 +04:00
|
|
|
void ForgetJSRuntime();
|
2007-01-05 01:31:26 +03:00
|
|
|
|
2014-05-05 21:30:39 +04:00
|
|
|
void SetBeforeUnlinkCallback(CC_BeforeUnlinkCallback aBeforeUnlinkCB)
|
|
|
|
{
|
|
|
|
CheckThreadSafety();
|
|
|
|
mBeforeUnlinkCB = aBeforeUnlinkCB;
|
|
|
|
}
|
2013-03-26 01:26:00 +04:00
|
|
|
|
2014-05-05 21:30:39 +04:00
|
|
|
void SetForgetSkippableCallback(CC_ForgetSkippableCallback aForgetSkippableCB)
|
|
|
|
{
|
|
|
|
CheckThreadSafety();
|
|
|
|
mForgetSkippableCB = aForgetSkippableCB;
|
|
|
|
}
|
2013-03-26 01:26:00 +04:00
|
|
|
|
2014-05-13 21:41:38 +04:00
|
|
|
void Suspect(void* aPtr, nsCycleCollectionParticipant* aCp,
|
|
|
|
nsCycleCollectingAutoRefCnt* aRefCnt);
|
2014-05-05 21:30:39 +04:00
|
|
|
uint32_t SuspectedCount();
|
|
|
|
void ForgetSkippable(bool aRemoveChildlessNodes, bool aAsyncSnowWhiteFreeing);
|
|
|
|
bool FreeSnowWhite(bool aUntilNoSWInPurpleBuffer);
|
2009-05-07 00:46:04 +04:00
|
|
|
|
2014-05-05 21:30:39 +04:00
|
|
|
// This method assumes its argument is already canonicalized.
|
2014-05-13 21:41:38 +04:00
|
|
|
void RemoveObjectFromGraph(void* aPtr);
|
2013-12-06 22:17:20 +04:00
|
|
|
|
2014-05-05 21:30:39 +04:00
|
|
|
void PrepareForGarbageCollection();
|
2014-05-07 04:25:26 +04:00
|
|
|
void FinishAnyCurrentCollection();
|
2013-12-06 22:17:20 +04:00
|
|
|
|
2014-05-05 21:30:39 +04:00
|
|
|
bool Collect(ccType aCCType,
|
2014-05-13 21:41:38 +04:00
|
|
|
SliceBudget& aBudget,
|
2014-11-27 14:47:51 +03:00
|
|
|
nsICycleCollectorListener* aManualListener,
|
|
|
|
bool aPreferShorterSlices = false);
|
2014-05-05 21:30:39 +04:00
|
|
|
void Shutdown();
|
2013-09-10 19:56:35 +04:00
|
|
|
|
2014-05-05 21:30:39 +04:00
|
|
|
void SizeOfIncludingThis(mozilla::MallocSizeOf aMallocSizeOf,
|
2014-05-13 21:41:38 +04:00
|
|
|
size_t* aObjectSize,
|
|
|
|
size_t* aGraphNodesSize,
|
|
|
|
size_t* aGraphEdgesSize,
|
|
|
|
size_t* aWeakMapsSize,
|
|
|
|
size_t* aPurpleBufferSize) const;
|
2013-09-10 19:56:35 +04:00
|
|
|
|
2014-05-05 21:30:39 +04:00
|
|
|
JSPurpleBuffer* GetJSPurpleBuffer();
|
2013-09-10 19:56:35 +04:00
|
|
|
private:
|
2014-05-05 21:30:39 +04:00
|
|
|
void CheckThreadSafety();
|
|
|
|
void ShutdownCollect();
|
2010-11-12 01:52:30 +03:00
|
|
|
|
2014-05-05 21:30:39 +04:00
|
|
|
void FixGrayBits(bool aForceGC);
|
|
|
|
bool ShouldMergeZones(ccType aCCType);
|
2010-11-12 01:52:30 +03:00
|
|
|
|
2014-05-13 21:41:38 +04:00
|
|
|
void BeginCollection(ccType aCCType, nsICycleCollectorListener* aManualListener);
|
|
|
|
void MarkRoots(SliceBudget& aBudget);
|
2014-05-05 21:30:39 +04:00
|
|
|
void ScanRoots(bool aFullySynchGraphBuild);
|
|
|
|
void ScanIncrementalRoots();
|
2014-05-08 22:28:03 +04:00
|
|
|
void ScanWhiteNodes(bool aFullySynchGraphBuild);
|
|
|
|
void ScanBlackNodes();
|
2014-05-05 21:30:39 +04:00
|
|
|
void ScanWeakMaps();
|
2010-11-12 01:52:30 +03:00
|
|
|
|
2014-05-05 21:30:39 +04:00
|
|
|
// returns whether anything was collected
|
|
|
|
bool CollectWhite();
|
2013-07-09 21:30:58 +04:00
|
|
|
|
2014-05-05 21:30:39 +04:00
|
|
|
void CleanupAfterCollection();
|
2007-01-05 01:31:26 +03:00
|
|
|
};
|
|
|
|
|
2014-04-27 11:06:00 +04:00
|
|
|
NS_IMPL_ISUPPORTS(nsCycleCollector, nsIMemoryReporter)
|
2013-11-26 03:57:53 +04: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);
|
|
|
|
*/
|
2014-08-25 23:17:15 +04:00
|
|
|
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:
|
2014-05-05 21:30:39 +04:00
|
|
|
Visitor mVisitor;
|
2009-10-27 15:38:18 +03:00
|
|
|
|
2014-05-13 21:41:38 +04:00
|
|
|
void DoWalk(nsDeque& aQueue);
|
2007-01-05 01:31:26 +03:00
|
|
|
|
2014-05-13 21:41:38 +04:00
|
|
|
void CheckedPush(nsDeque& aQueue, PtrInfo* aPi)
|
2014-05-05 21:30:39 +04:00
|
|
|
{
|
2014-05-13 21:41:38 +04:00
|
|
|
if (!aPi) {
|
2014-05-05 21:30:39 +04:00
|
|
|
MOZ_CRASH();
|
2013-05-29 04:42:13 +04:00
|
|
|
}
|
2015-01-28 12:00:40 +03:00
|
|
|
if (!aQueue.Push(aPi, fallible)) {
|
2014-05-05 21:30:39 +04:00
|
|
|
mVisitor.Failed();
|
|
|
|
}
|
|
|
|
}
|
2013-05-29 04:42:13 +04:00
|
|
|
|
2007-01-05 01:31:26 +03:00
|
|
|
public:
|
2014-05-13 21:41:38 +04:00
|
|
|
void Walk(PtrInfo* aPi);
|
2014-07-09 23:31:00 +04:00
|
|
|
void WalkFromRoots(CCGraph& aGraph);
|
2014-05-05 21:30:39 +04:00
|
|
|
// copy-constructing the visitor should be cheap, and less
|
|
|
|
// indirection than using a reference
|
2014-07-28 21:19:06 +04:00
|
|
|
explicit GraphWalker(const Visitor aVisitor) : mVisitor(aVisitor)
|
2014-05-13 21:41:38 +04:00
|
|
|
{
|
|
|
|
}
|
2007-01-05 01:31:26 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////
|
2013-06-18 23:02:16 +04:00
|
|
|
// The static collector struct
|
2007-01-05 01:31:26 +03:00
|
|
|
////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2014-05-13 21:41:38 +04:00
|
|
|
struct CollectorData
|
|
|
|
{
|
2013-11-26 03:57:53 +04:00
|
|
|
nsRefPtr<nsCycleCollector> mCollector;
|
2013-06-18 23:02:16 +04:00
|
|
|
CycleCollectedJSRuntime* mRuntime;
|
|
|
|
};
|
|
|
|
|
|
|
|
static mozilla::ThreadLocal<CollectorData*> sCollectorData;
|
2007-01-05 01:31:26 +03:00
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////
|
|
|
|
// Utility functions
|
|
|
|
////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2012-03-16 08:13:08 +04:00
|
|
|
MOZ_NEVER_INLINE static void
|
2014-05-13 21:41:38 +04:00
|
|
|
Fault(const char* aMsg, const void* aPtr = nullptr)
|
2007-01-05 01:31:26 +03:00
|
|
|
{
|
2014-05-13 21:41:38 +04:00
|
|
|
if (aPtr) {
|
|
|
|
printf("Fault in cycle collector: %s (ptr: %p)\n", aMsg, aPtr);
|
|
|
|
} else {
|
|
|
|
printf("Fault in cycle collector: %s\n", aMsg);
|
|
|
|
}
|
2007-03-06 00:07:51 +03:00
|
|
|
|
2014-05-05 21:30:39 +04:00
|
|
|
NS_RUNTIMEABORT("cycle collector fault");
|
2007-01-05 01:31:26 +03:00
|
|
|
}
|
|
|
|
|
2007-07-06 02:38:38 +04:00
|
|
|
static void
|
2014-05-13 21:41:38 +04:00
|
|
|
Fault(const char* aMsg, PtrInfo* aPi)
|
2007-07-06 02:38:38 +04:00
|
|
|
{
|
2014-05-13 21:41:38 +04:00
|
|
|
Fault(aMsg, aPi->mPointer);
|
2007-07-06 02:38:38 +04:00
|
|
|
}
|
|
|
|
|
2007-06-07 02:04:26 +04:00
|
|
|
static inline void
|
2014-05-13 21:41:38 +04:00
|
|
|
ToParticipant(nsISupports* aPtr, nsXPCOMCycleCollectionParticipant** aCp)
|
2007-06-07 02:04:26 +04:00
|
|
|
{
|
2014-05-05 21:30:39 +04:00
|
|
|
// 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.
|
2014-05-13 21:41:38 +04:00
|
|
|
CallQueryInterface(aPtr, aCp);
|
2007-06-07 02:04:26 +04:00
|
|
|
}
|
|
|
|
|
2014-08-25 23:17:15 +04:00
|
|
|
template<class Visitor>
|
2012-02-21 00:13:29 +04:00
|
|
|
MOZ_NEVER_INLINE void
|
2014-05-13 21:41:38 +04:00
|
|
|
GraphWalker<Visitor>::Walk(PtrInfo* aPi)
|
2007-04-26 01:12:11 +04:00
|
|
|
{
|
2014-05-05 21:30:39 +04:00
|
|
|
nsDeque queue;
|
2014-05-13 21:41:38 +04:00
|
|
|
CheckedPush(queue, aPi);
|
2014-05-05 21:30:39 +04:00
|
|
|
DoWalk(queue);
|
2007-04-26 01:12:11 +04:00
|
|
|
}
|
|
|
|
|
2014-08-25 23:17:15 +04:00
|
|
|
template<class Visitor>
|
2012-02-21 00:13:29 +04:00
|
|
|
MOZ_NEVER_INLINE void
|
2014-07-09 23:31:00 +04:00
|
|
|
GraphWalker<Visitor>::WalkFromRoots(CCGraph& aGraph)
|
2007-04-26 01:12:11 +04:00
|
|
|
{
|
2014-05-05 21:30:39 +04:00
|
|
|
nsDeque queue;
|
|
|
|
NodePool::Enumerator etor(aGraph.mNodes);
|
|
|
|
for (uint32_t i = 0; i < aGraph.mRootCount; ++i) {
|
|
|
|
CheckedPush(queue, etor.GetNext());
|
|
|
|
}
|
|
|
|
DoWalk(queue);
|
2007-04-26 01:12:11 +04:00
|
|
|
}
|
|
|
|
|
2014-08-25 23:17:15 +04:00
|
|
|
template<class Visitor>
|
2012-02-21 00:13:29 +04:00
|
|
|
MOZ_NEVER_INLINE void
|
2014-05-13 21:41:38 +04:00
|
|
|
GraphWalker<Visitor>::DoWalk(nsDeque& aQueue)
|
2007-04-26 01:12:11 +04:00
|
|
|
{
|
2014-05-05 21:30:39 +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) {
|
2014-05-13 21:41:38 +04:00
|
|
|
PtrInfo* pi = static_cast<PtrInfo*>(aQueue.PopFront());
|
2014-05-05 21:30:39 +04:00
|
|
|
|
2014-07-01 03:18:47 +04:00
|
|
|
if (pi->WasTraversed() && mVisitor.ShouldVisitNode(pi)) {
|
2014-05-05 21:30:39 +04:00
|
|
|
mVisitor.VisitNode(pi);
|
|
|
|
for (EdgePool::Iterator child = pi->FirstChild(),
|
|
|
|
child_end = pi->LastChild();
|
|
|
|
child != child_end; ++child) {
|
|
|
|
CheckedPush(aQueue, *child);
|
|
|
|
}
|
2013-05-29 04:42:13 +04:00
|
|
|
}
|
2014-05-05 21:30:39 +04:00
|
|
|
}
|
2007-04-26 01:12:11 +04:00
|
|
|
}
|
|
|
|
|
2013-11-10 01:15:44 +04:00
|
|
|
struct CCGraphDescriber : public LinkedListElement<CCGraphDescriber>
|
2012-02-17 21:35:22 +04:00
|
|
|
{
|
|
|
|
CCGraphDescriber()
|
2014-05-13 21:41:38 +04:00
|
|
|
: mAddress("0x"), mCnt(0), mType(eUnknown)
|
2012-02-17 21:35:22 +04:00
|
|
|
{
|
2014-05-13 21:41:38 +04:00
|
|
|
}
|
|
|
|
|
2014-08-25 23:17:15 +04:00
|
|
|
enum Type
|
|
|
|
{
|
2012-02-17 21:35:22 +04:00
|
|
|
eRefCountedObject,
|
|
|
|
eGCedObject,
|
|
|
|
eGCMarkedObject,
|
|
|
|
eEdge,
|
|
|
|
eRoot,
|
|
|
|
eGarbage,
|
|
|
|
eUnknown
|
|
|
|
};
|
|
|
|
|
|
|
|
nsCString mAddress;
|
|
|
|
nsCString mName;
|
2013-11-12 17:53:51 +04:00
|
|
|
nsCString mCompartmentOrToAddress;
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t mCnt;
|
2012-02-17 21:35:22 +04:00
|
|
|
Type mType;
|
|
|
|
};
|
2007-04-26 01:12:11 +04:00
|
|
|
|
2014-05-13 21:13:00 +04:00
|
|
|
class nsCycleCollectorLogSinkToFile MOZ_FINAL : public nsICycleCollectorLogSink
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
|
|
|
|
nsCycleCollectorLogSinkToFile() :
|
|
|
|
mProcessIdentifier(base::GetCurrentProcId()),
|
|
|
|
mGCLog("gc-edges"), mCCLog("cc-edges")
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHOD GetFilenameIdentifier(nsAString& aIdentifier) MOZ_OVERRIDE
|
|
|
|
{
|
|
|
|
aIdentifier = mFilenameIdentifier;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHOD SetFilenameIdentifier(const nsAString& aIdentifier) MOZ_OVERRIDE
|
|
|
|
{
|
|
|
|
mFilenameIdentifier = aIdentifier;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHOD GetProcessIdentifier(int32_t* aIdentifier) MOZ_OVERRIDE
|
|
|
|
{
|
|
|
|
*aIdentifier = mProcessIdentifier;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHOD SetProcessIdentifier(int32_t aIdentifier) MOZ_OVERRIDE
|
|
|
|
{
|
|
|
|
mProcessIdentifier = aIdentifier;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHOD GetGcLog(nsIFile** aPath) MOZ_OVERRIDE
|
|
|
|
{
|
|
|
|
NS_IF_ADDREF(*aPath = mGCLog.mFile);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHOD GetCcLog(nsIFile** aPath) MOZ_OVERRIDE
|
|
|
|
{
|
|
|
|
NS_IF_ADDREF(*aPath = mCCLog.mFile);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHOD Open(FILE** aGCLog, FILE** aCCLog) MOZ_OVERRIDE
|
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
if (mGCLog.mStream || mCCLog.mStream) {
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
rv = OpenLog(&mGCLog);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
*aGCLog = mGCLog.mStream;
|
|
|
|
|
|
|
|
rv = OpenLog(&mCCLog);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
*aCCLog = mCCLog.mStream;
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHOD CloseGCLog() MOZ_OVERRIDE
|
|
|
|
{
|
|
|
|
if (!mGCLog.mStream) {
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
|
|
|
CloseLog(&mGCLog, NS_LITERAL_STRING("Garbage"));
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHOD CloseCCLog() MOZ_OVERRIDE
|
|
|
|
{
|
|
|
|
if (!mCCLog.mStream) {
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
|
|
|
CloseLog(&mCCLog, NS_LITERAL_STRING("Cycle"));
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
~nsCycleCollectorLogSinkToFile()
|
|
|
|
{
|
|
|
|
if (mGCLog.mStream) {
|
|
|
|
MozillaUnRegisterDebugFILE(mGCLog.mStream);
|
|
|
|
fclose(mGCLog.mStream);
|
|
|
|
}
|
|
|
|
if (mCCLog.mStream) {
|
|
|
|
MozillaUnRegisterDebugFILE(mCCLog.mStream);
|
|
|
|
fclose(mCCLog.mStream);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-25 23:17:15 +04:00
|
|
|
struct FileInfo
|
|
|
|
{
|
2014-05-13 21:13:00 +04:00
|
|
|
const char* const mPrefix;
|
|
|
|
nsCOMPtr<nsIFile> mFile;
|
|
|
|
FILE* mStream;
|
|
|
|
|
2014-07-28 21:19:06 +04:00
|
|
|
explicit FileInfo(const char* aPrefix) : mPrefix(aPrefix), mStream(nullptr) { }
|
2014-05-13 21:13:00 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a new file named something like aPrefix.$PID.$IDENTIFIER.log in
|
|
|
|
* $MOZ_CC_LOG_DIRECTORY or in the system's temp directory. No existing
|
|
|
|
* file will be overwritten; if aPrefix.$PID.$IDENTIFIER.log exists, we'll
|
|
|
|
* try a file named something like aPrefix.$PID.$IDENTIFIER-1.log, and so
|
|
|
|
* on.
|
|
|
|
*/
|
|
|
|
already_AddRefed<nsIFile> CreateTempFile(const char* aPrefix)
|
|
|
|
{
|
|
|
|
nsPrintfCString filename("%s.%d%s%s.log",
|
|
|
|
aPrefix,
|
|
|
|
mProcessIdentifier,
|
|
|
|
mFilenameIdentifier.IsEmpty() ? "" : ".",
|
|
|
|
NS_ConvertUTF16toUTF8(mFilenameIdentifier).get());
|
|
|
|
|
|
|
|
// Get the log directory either from $MOZ_CC_LOG_DIRECTORY or from
|
|
|
|
// the fallback directories in OpenTempFile. We don't use an nsCOMPtr
|
|
|
|
// here because OpenTempFile uses an in/out param and getter_AddRefs
|
|
|
|
// wouldn't work.
|
|
|
|
nsIFile* logFile = nullptr;
|
|
|
|
if (char* env = PR_GetEnv("MOZ_CC_LOG_DIRECTORY")) {
|
|
|
|
NS_NewNativeLocalFile(nsCString(env), /* followLinks = */ true,
|
|
|
|
&logFile);
|
|
|
|
}
|
|
|
|
|
|
|
|
// On Android or B2G, this function will open a file named
|
|
|
|
// aFilename under a memory-reporting-specific folder
|
|
|
|
// (/data/local/tmp/memory-reports). Otherwise, it will open a
|
|
|
|
// file named aFilename under "NS_OS_TEMP_DIR".
|
2014-08-25 23:17:15 +04:00
|
|
|
nsresult rv = nsDumpUtils::OpenTempFile(filename, &logFile,
|
2014-05-13 21:13:00 +04:00
|
|
|
NS_LITERAL_CSTRING("memory-reports"));
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
NS_IF_RELEASE(logFile);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return dont_AddRef(logFile);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult OpenLog(FileInfo* aLog)
|
|
|
|
{
|
|
|
|
// Initially create the log in a file starting with "incomplete-".
|
|
|
|
// We'll move the file and strip off the "incomplete-" once the dump
|
|
|
|
// completes. (We do this because we don't want scripts which poll
|
|
|
|
// the filesystem looking for GC/CC dumps to grab a file before we're
|
|
|
|
// finished writing to it.)
|
|
|
|
nsAutoCString incomplete;
|
|
|
|
incomplete += "incomplete-";
|
|
|
|
incomplete += aLog->mPrefix;
|
|
|
|
MOZ_ASSERT(!aLog->mFile);
|
|
|
|
aLog->mFile = CreateTempFile(incomplete.get());
|
2014-08-25 23:17:15 +04:00
|
|
|
if (NS_WARN_IF(!aLog->mFile)) {
|
2014-05-13 21:13:00 +04:00
|
|
|
return NS_ERROR_UNEXPECTED;
|
2014-08-25 23:17:15 +04:00
|
|
|
}
|
2014-05-13 21:13:00 +04:00
|
|
|
|
|
|
|
MOZ_ASSERT(!aLog->mStream);
|
|
|
|
aLog->mFile->OpenANSIFileDesc("w", &aLog->mStream);
|
2014-08-25 23:17:15 +04:00
|
|
|
if (NS_WARN_IF(!aLog->mStream)) {
|
2014-05-13 21:13:00 +04:00
|
|
|
return NS_ERROR_UNEXPECTED;
|
2014-08-25 23:17:15 +04:00
|
|
|
}
|
2014-05-13 21:13:00 +04:00
|
|
|
MozillaRegisterDebugFILE(aLog->mStream);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult CloseLog(FileInfo* aLog, const nsAString& aCollectorKind)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(aLog->mStream);
|
|
|
|
MOZ_ASSERT(aLog->mFile);
|
|
|
|
|
|
|
|
MozillaUnRegisterDebugFILE(aLog->mStream);
|
|
|
|
fclose(aLog->mStream);
|
|
|
|
aLog->mStream = nullptr;
|
|
|
|
|
|
|
|
// Strip off "incomplete-".
|
|
|
|
nsCOMPtr<nsIFile> logFileFinalDestination =
|
|
|
|
CreateTempFile(aLog->mPrefix);
|
2014-08-25 23:17:15 +04:00
|
|
|
if (NS_WARN_IF(!logFileFinalDestination)) {
|
2014-05-13 21:13:00 +04:00
|
|
|
return NS_ERROR_UNEXPECTED;
|
2014-08-25 23:17:15 +04:00
|
|
|
}
|
2014-05-13 21:13:00 +04:00
|
|
|
|
|
|
|
nsAutoString logFileFinalDestinationName;
|
|
|
|
logFileFinalDestination->GetLeafName(logFileFinalDestinationName);
|
2014-08-25 23:17:15 +04:00
|
|
|
if (NS_WARN_IF(logFileFinalDestinationName.IsEmpty())) {
|
2014-05-13 21:13:00 +04:00
|
|
|
return NS_ERROR_UNEXPECTED;
|
2014-08-25 23:17:15 +04:00
|
|
|
}
|
2014-05-13 21:13:00 +04:00
|
|
|
|
|
|
|
aLog->mFile->MoveTo(/* directory */ nullptr, logFileFinalDestinationName);
|
|
|
|
|
|
|
|
// Save the file path.
|
|
|
|
aLog->mFile = logFileFinalDestination;
|
|
|
|
|
|
|
|
// Log to the error console.
|
|
|
|
nsCOMPtr<nsIConsoleService> cs =
|
|
|
|
do_GetService(NS_CONSOLESERVICE_CONTRACTID);
|
|
|
|
if (cs) {
|
|
|
|
// Copy out the path.
|
|
|
|
nsAutoString logPath;
|
|
|
|
logFileFinalDestination->GetPath(logPath);
|
|
|
|
|
2014-08-25 23:17:15 +04:00
|
|
|
nsString msg = aCollectorKind +
|
|
|
|
NS_LITERAL_STRING(" Collector log dumped to ") + logPath;
|
2014-05-13 21:13:00 +04:00
|
|
|
cs->LogStringMessage(msg.get());
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
int32_t mProcessIdentifier;
|
|
|
|
nsString mFilenameIdentifier;
|
|
|
|
FileInfo mGCLog;
|
|
|
|
FileInfo mCCLog;
|
|
|
|
};
|
|
|
|
|
|
|
|
NS_IMPL_ISUPPORTS(nsCycleCollectorLogSinkToFile, nsICycleCollectorLogSink)
|
|
|
|
|
|
|
|
|
2012-06-06 03:51:58 +04:00
|
|
|
class nsCycleCollectorLogger MOZ_FINAL : public nsICycleCollectorListener
|
2010-08-12 04:03:23 +04:00
|
|
|
{
|
2014-07-01 02:11:53 +04:00
|
|
|
~nsCycleCollectorLogger()
|
|
|
|
{
|
|
|
|
ClearDescribers();
|
|
|
|
}
|
|
|
|
|
2010-08-12 04:03:23 +04:00
|
|
|
public:
|
2014-05-13 21:13:00 +04:00
|
|
|
nsCycleCollectorLogger()
|
|
|
|
: mLogSink(nsCycleCollector_createLogSink())
|
|
|
|
, mWantAllTraces(false)
|
|
|
|
, mDisableLog(false)
|
|
|
|
, mWantAfterProcessing(false)
|
|
|
|
, mCCLog(nullptr)
|
2014-05-05 21:30:39 +04:00
|
|
|
{
|
|
|
|
}
|
2014-05-13 21:13:00 +04:00
|
|
|
|
2014-05-05 21:30:39 +04:00
|
|
|
NS_DECL_ISUPPORTS
|
2010-08-12 04:03:23 +04:00
|
|
|
|
2014-05-05 21:30:39 +04:00
|
|
|
void SetAllTraces()
|
|
|
|
{
|
|
|
|
mWantAllTraces = true;
|
|
|
|
}
|
2012-09-28 21:11:33 +04:00
|
|
|
|
2015-01-02 09:09:28 +03:00
|
|
|
NS_IMETHOD AllTraces(nsICycleCollectorListener** aListener) MOZ_OVERRIDE
|
2014-05-05 21:30:39 +04:00
|
|
|
{
|
|
|
|
SetAllTraces();
|
|
|
|
NS_ADDREF(*aListener = this);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2012-01-02 01:48:42 +04:00
|
|
|
|
2015-01-02 09:09:28 +03:00
|
|
|
NS_IMETHOD GetWantAllTraces(bool* aAllTraces) MOZ_OVERRIDE
|
2014-05-05 21:30:39 +04:00
|
|
|
{
|
|
|
|
*aAllTraces = mWantAllTraces;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2012-02-17 21:35:22 +04:00
|
|
|
|
2015-01-02 09:09:28 +03:00
|
|
|
NS_IMETHOD GetDisableLog(bool* aDisableLog) MOZ_OVERRIDE
|
2014-05-05 21:30:39 +04:00
|
|
|
{
|
|
|
|
*aDisableLog = mDisableLog;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2012-02-17 21:35:22 +04:00
|
|
|
|
2015-01-02 09:09:28 +03:00
|
|
|
NS_IMETHOD SetDisableLog(bool aDisableLog) MOZ_OVERRIDE
|
2014-05-05 21:30:39 +04:00
|
|
|
{
|
|
|
|
mDisableLog = aDisableLog;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2012-02-17 21:35:22 +04:00
|
|
|
|
2015-01-02 09:09:28 +03:00
|
|
|
NS_IMETHOD GetWantAfterProcessing(bool* aWantAfterProcessing) MOZ_OVERRIDE
|
2014-05-05 21:30:39 +04:00
|
|
|
{
|
|
|
|
*aWantAfterProcessing = mWantAfterProcessing;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2012-02-17 21:35:22 +04:00
|
|
|
|
2015-01-02 09:09:28 +03:00
|
|
|
NS_IMETHOD SetWantAfterProcessing(bool aWantAfterProcessing) MOZ_OVERRIDE
|
2014-05-05 21:30:39 +04:00
|
|
|
{
|
|
|
|
mWantAfterProcessing = aWantAfterProcessing;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2012-01-02 01:48:42 +04:00
|
|
|
|
2015-01-02 09:09:28 +03:00
|
|
|
NS_IMETHOD GetLogSink(nsICycleCollectorLogSink** aLogSink) MOZ_OVERRIDE
|
2014-05-05 21:30:39 +04:00
|
|
|
{
|
2014-05-13 21:13:00 +04:00
|
|
|
NS_ADDREF(*aLogSink = mLogSink);
|
2014-05-05 21:30:39 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2012-10-16 06:12:14 +04:00
|
|
|
|
2015-01-02 09:09:28 +03:00
|
|
|
NS_IMETHOD SetLogSink(nsICycleCollectorLogSink* aLogSink) MOZ_OVERRIDE
|
2014-05-05 21:30:39 +04:00
|
|
|
{
|
2014-05-13 21:13:00 +04:00
|
|
|
if (!aLogSink) {
|
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
}
|
|
|
|
mLogSink = aLogSink;
|
2014-05-05 21:30:39 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-01-02 09:09:28 +03:00
|
|
|
NS_IMETHOD Begin() MOZ_OVERRIDE
|
2014-05-05 21:30:39 +04:00
|
|
|
{
|
2014-05-13 21:13:00 +04:00
|
|
|
nsresult rv;
|
|
|
|
|
2014-05-05 21:30:39 +04:00
|
|
|
mCurrentAddress.AssignLiteral("0x");
|
|
|
|
ClearDescribers();
|
|
|
|
if (mDisableLog) {
|
2014-02-01 02:43:08 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-05-13 21:13:00 +04:00
|
|
|
FILE* gcLog;
|
|
|
|
rv = mLogSink->Open(&gcLog, &mCCLog);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2014-05-05 21:30:39 +04:00
|
|
|
// Dump the JS heap.
|
2014-05-13 21:41:38 +04:00
|
|
|
CollectorData* data = sCollectorData.get();
|
|
|
|
if (data && data->mRuntime) {
|
2014-05-13 21:13:00 +04:00
|
|
|
data->mRuntime->DumpJSHeap(gcLog);
|
2014-05-13 21:41:38 +04:00
|
|
|
}
|
2014-05-13 21:13:00 +04:00
|
|
|
rv = mLogSink->CloseGCLog();
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2013-07-19 21:00:53 +04:00
|
|
|
|
2014-05-13 21:13:00 +04:00
|
|
|
fprintf(mCCLog, "# WantAllTraces=%s\n", mWantAllTraces ? "true" : "false");
|
2014-05-05 21:30:39 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2014-05-13 21:41:38 +04:00
|
|
|
NS_IMETHOD NoteRefCountedObject(uint64_t aAddress, uint32_t aRefCount,
|
2015-01-02 09:09:28 +03:00
|
|
|
const char* aObjectDescription) MOZ_OVERRIDE
|
2014-05-05 21:30:39 +04:00
|
|
|
{
|
|
|
|
if (!mDisableLog) {
|
2014-05-13 21:13:00 +04:00
|
|
|
fprintf(mCCLog, "%p [rc=%u] %s\n", (void*)aAddress, aRefCount,
|
2014-05-05 21:30:39 +04:00
|
|
|
aObjectDescription);
|
|
|
|
}
|
|
|
|
if (mWantAfterProcessing) {
|
|
|
|
CCGraphDescriber* d = new CCGraphDescriber();
|
|
|
|
mDescribers.insertBack(d);
|
|
|
|
mCurrentAddress.AssignLiteral("0x");
|
|
|
|
mCurrentAddress.AppendInt(aAddress, 16);
|
|
|
|
d->mType = CCGraphDescriber::eRefCountedObject;
|
|
|
|
d->mAddress = mCurrentAddress;
|
2014-05-13 21:41:38 +04:00
|
|
|
d->mCnt = aRefCount;
|
2014-05-05 21:30:39 +04:00
|
|
|
d->mName.Append(aObjectDescription);
|
2010-08-12 04:03:23 +04:00
|
|
|
}
|
2014-05-05 21:30:39 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
NS_IMETHOD NoteGCedObject(uint64_t aAddress, bool aMarked,
|
2014-05-13 21:41:38 +04:00
|
|
|
const char* aObjectDescription,
|
2015-01-02 09:09:28 +03:00
|
|
|
uint64_t aCompartmentAddress) MOZ_OVERRIDE
|
2014-05-05 21:30:39 +04:00
|
|
|
{
|
|
|
|
if (!mDisableLog) {
|
2014-05-13 21:13:00 +04:00
|
|
|
fprintf(mCCLog, "%p [gc%s] %s\n", (void*)aAddress,
|
2014-05-05 21:30:39 +04:00
|
|
|
aMarked ? ".marked" : "", aObjectDescription);
|
|
|
|
}
|
|
|
|
if (mWantAfterProcessing) {
|
|
|
|
CCGraphDescriber* d = new CCGraphDescriber();
|
|
|
|
mDescribers.insertBack(d);
|
|
|
|
mCurrentAddress.AssignLiteral("0x");
|
|
|
|
mCurrentAddress.AppendInt(aAddress, 16);
|
|
|
|
d->mType = aMarked ? CCGraphDescriber::eGCMarkedObject :
|
2014-08-25 23:17:15 +04:00
|
|
|
CCGraphDescriber::eGCedObject;
|
2014-05-05 21:30:39 +04:00
|
|
|
d->mAddress = mCurrentAddress;
|
|
|
|
d->mName.Append(aObjectDescription);
|
|
|
|
if (aCompartmentAddress) {
|
|
|
|
d->mCompartmentOrToAddress.AssignLiteral("0x");
|
|
|
|
d->mCompartmentOrToAddress.AppendInt(aCompartmentAddress, 16);
|
|
|
|
} else {
|
|
|
|
d->mCompartmentOrToAddress.SetIsVoid(true);
|
|
|
|
}
|
2011-06-22 21:41:17 +04:00
|
|
|
}
|
2014-05-05 21:30:39 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2015-01-02 09:09:28 +03:00
|
|
|
NS_IMETHOD NoteEdge(uint64_t aToAddress, const char* aEdgeName) MOZ_OVERRIDE
|
2014-05-05 21:30:39 +04:00
|
|
|
{
|
|
|
|
if (!mDisableLog) {
|
2014-05-13 21:13:00 +04:00
|
|
|
fprintf(mCCLog, "> %p %s\n", (void*)aToAddress, aEdgeName);
|
2014-05-05 21:30:39 +04:00
|
|
|
}
|
|
|
|
if (mWantAfterProcessing) {
|
|
|
|
CCGraphDescriber* d = new CCGraphDescriber();
|
|
|
|
mDescribers.insertBack(d);
|
|
|
|
d->mType = CCGraphDescriber::eEdge;
|
|
|
|
d->mAddress = mCurrentAddress;
|
|
|
|
d->mCompartmentOrToAddress.AssignLiteral("0x");
|
|
|
|
d->mCompartmentOrToAddress.AppendInt(aToAddress, 16);
|
|
|
|
d->mName.Append(aEdgeName);
|
2010-08-12 04:03:23 +04:00
|
|
|
}
|
2014-05-05 21:30:39 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
NS_IMETHOD NoteWeakMapEntry(uint64_t aMap, uint64_t aKey,
|
2015-01-02 09:09:28 +03:00
|
|
|
uint64_t aKeyDelegate, uint64_t aValue) MOZ_OVERRIDE
|
2014-05-05 21:30:39 +04:00
|
|
|
{
|
|
|
|
if (!mDisableLog) {
|
2014-05-13 21:13:00 +04:00
|
|
|
fprintf(mCCLog, "WeakMapEntry map=%p key=%p keyDelegate=%p value=%p\n",
|
2014-05-05 21:30:39 +04:00
|
|
|
(void*)aMap, (void*)aKey, (void*)aKeyDelegate, (void*)aValue);
|
2010-08-12 04:03:23 +04:00
|
|
|
}
|
2014-05-05 21:30:39 +04:00
|
|
|
// We don't support after-processing for weak map entries.
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2015-01-02 09:09:28 +03:00
|
|
|
NS_IMETHOD NoteIncrementalRoot(uint64_t aAddress) MOZ_OVERRIDE
|
2014-05-05 21:30:39 +04:00
|
|
|
{
|
|
|
|
if (!mDisableLog) {
|
2014-05-13 21:13:00 +04:00
|
|
|
fprintf(mCCLog, "IncrementalRoot %p\n", (void*)aAddress);
|
2013-07-26 19:12:51 +04:00
|
|
|
}
|
2014-05-05 21:30:39 +04:00
|
|
|
// We don't support after-processing for incremental roots.
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2015-01-02 09:09:28 +03:00
|
|
|
NS_IMETHOD BeginResults() MOZ_OVERRIDE
|
2014-05-05 21:30:39 +04:00
|
|
|
{
|
|
|
|
if (!mDisableLog) {
|
2014-05-13 21:13:00 +04:00
|
|
|
fputs("==========\n", mCCLog);
|
2014-03-15 03:07:07 +04:00
|
|
|
}
|
2014-05-05 21:30:39 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2015-01-02 09:09:28 +03:00
|
|
|
NS_IMETHOD DescribeRoot(uint64_t aAddress, uint32_t aKnownEdges) MOZ_OVERRIDE
|
2014-05-05 21:30:39 +04:00
|
|
|
{
|
|
|
|
if (!mDisableLog) {
|
2014-05-13 21:13:00 +04:00
|
|
|
fprintf(mCCLog, "%p [known=%u]\n", (void*)aAddress, aKnownEdges);
|
2010-08-12 04:03:23 +04:00
|
|
|
}
|
2014-05-05 21:30:39 +04:00
|
|
|
if (mWantAfterProcessing) {
|
|
|
|
CCGraphDescriber* d = new CCGraphDescriber();
|
|
|
|
mDescribers.insertBack(d);
|
|
|
|
d->mType = CCGraphDescriber::eRoot;
|
|
|
|
d->mAddress.AppendInt(aAddress, 16);
|
|
|
|
d->mCnt = aKnownEdges;
|
2010-08-12 04:03:23 +04:00
|
|
|
}
|
2014-05-05 21:30:39 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2015-01-02 09:09:28 +03:00
|
|
|
NS_IMETHOD DescribeGarbage(uint64_t aAddress) MOZ_OVERRIDE
|
2014-05-05 21:30:39 +04:00
|
|
|
{
|
|
|
|
if (!mDisableLog) {
|
2014-05-13 21:13:00 +04:00
|
|
|
fprintf(mCCLog, "%p [garbage]\n", (void*)aAddress);
|
2010-08-12 04:03:23 +04:00
|
|
|
}
|
2014-05-05 21:30:39 +04:00
|
|
|
if (mWantAfterProcessing) {
|
|
|
|
CCGraphDescriber* d = new CCGraphDescriber();
|
|
|
|
mDescribers.insertBack(d);
|
|
|
|
d->mType = CCGraphDescriber::eGarbage;
|
|
|
|
d->mAddress.AppendInt(aAddress, 16);
|
2010-08-12 04:03:23 +04:00
|
|
|
}
|
2014-05-05 21:30:39 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2015-01-02 09:09:28 +03:00
|
|
|
NS_IMETHOD End() MOZ_OVERRIDE
|
2014-05-05 21:30:39 +04:00
|
|
|
{
|
|
|
|
if (!mDisableLog) {
|
2014-05-13 21:13:00 +04:00
|
|
|
mCCLog = nullptr;
|
|
|
|
nsresult rv = mLogSink->CloseCCLog();
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2014-05-05 21:30:39 +04:00
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
NS_IMETHOD ProcessNext(nsICycleCollectorHandler* aHandler,
|
2015-01-02 09:09:28 +03:00
|
|
|
bool* aCanContinue) MOZ_OVERRIDE
|
2014-05-05 21:30:39 +04:00
|
|
|
{
|
2014-05-13 21:41:38 +04:00
|
|
|
if (NS_WARN_IF(!aHandler) || NS_WARN_IF(!mWantAfterProcessing)) {
|
2014-05-05 21:30:39 +04:00
|
|
|
return NS_ERROR_UNEXPECTED;
|
2014-05-13 21:41:38 +04:00
|
|
|
}
|
2014-05-05 21:30:39 +04:00
|
|
|
CCGraphDescriber* d = mDescribers.popFirst();
|
|
|
|
if (d) {
|
|
|
|
switch (d->mType) {
|
|
|
|
case CCGraphDescriber::eRefCountedObject:
|
|
|
|
aHandler->NoteRefCountedObject(d->mAddress,
|
|
|
|
d->mCnt,
|
|
|
|
d->mName);
|
|
|
|
break;
|
|
|
|
case CCGraphDescriber::eGCedObject:
|
|
|
|
case CCGraphDescriber::eGCMarkedObject:
|
|
|
|
aHandler->NoteGCedObject(d->mAddress,
|
|
|
|
d->mType ==
|
|
|
|
CCGraphDescriber::eGCMarkedObject,
|
|
|
|
d->mName,
|
|
|
|
d->mCompartmentOrToAddress);
|
|
|
|
break;
|
|
|
|
case CCGraphDescriber::eEdge:
|
|
|
|
aHandler->NoteEdge(d->mAddress,
|
|
|
|
d->mCompartmentOrToAddress,
|
|
|
|
d->mName);
|
|
|
|
break;
|
|
|
|
case CCGraphDescriber::eRoot:
|
|
|
|
aHandler->DescribeRoot(d->mAddress,
|
|
|
|
d->mCnt);
|
|
|
|
break;
|
|
|
|
case CCGraphDescriber::eGarbage:
|
|
|
|
aHandler->DescribeGarbage(d->mAddress);
|
|
|
|
break;
|
|
|
|
case CCGraphDescriber::eUnknown:
|
|
|
|
NS_NOTREACHED("CCGraphDescriber::eUnknown");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
delete d;
|
2012-02-17 21:35:22 +04:00
|
|
|
}
|
2014-05-05 21:30:39 +04:00
|
|
|
if (!(*aCanContinue = !mDescribers.isEmpty())) {
|
|
|
|
mCurrentAddress.AssignLiteral("0x");
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2010-08-12 04:03:23 +04:00
|
|
|
private:
|
2014-05-05 21:30:39 +04:00
|
|
|
void ClearDescribers()
|
|
|
|
{
|
|
|
|
CCGraphDescriber* d;
|
2014-05-13 21:41:38 +04:00
|
|
|
while ((d = mDescribers.popFirst())) {
|
2014-05-05 21:30:39 +04:00
|
|
|
delete d;
|
2013-11-10 01:15:44 +04:00
|
|
|
}
|
2014-05-05 21:30:39 +04:00
|
|
|
}
|
2013-11-10 01:15:44 +04:00
|
|
|
|
2014-05-13 21:13:00 +04:00
|
|
|
nsCOMPtr<nsICycleCollectorLogSink> mLogSink;
|
2014-05-05 21:30:39 +04:00
|
|
|
bool mWantAllTraces;
|
|
|
|
bool mDisableLog;
|
|
|
|
bool mWantAfterProcessing;
|
|
|
|
nsCString mCurrentAddress;
|
|
|
|
mozilla::LinkedList<CCGraphDescriber> mDescribers;
|
2014-05-13 21:13:00 +04:00
|
|
|
FILE* mCCLog;
|
2010-08-12 04:03:23 +04:00
|
|
|
};
|
|
|
|
|
2014-04-27 11:06:00 +04:00
|
|
|
NS_IMPL_ISUPPORTS(nsCycleCollectorLogger, nsICycleCollectorListener)
|
2010-08-12 04:03:23 +04:00
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsCycleCollectorLoggerConstructor(nsISupports* aOuter,
|
|
|
|
const nsIID& aIID,
|
2014-05-13 21:41:38 +04:00
|
|
|
void** aInstancePtr)
|
2010-08-12 04:03:23 +04:00
|
|
|
{
|
2014-05-13 21:41:38 +04:00
|
|
|
if (NS_WARN_IF(aOuter)) {
|
2014-05-05 21:30:39 +04:00
|
|
|
return NS_ERROR_NO_AGGREGATION;
|
2014-05-13 21:41:38 +04:00
|
|
|
}
|
2010-08-12 04:03:23 +04:00
|
|
|
|
2014-05-13 21:41:38 +04:00
|
|
|
nsISupports* logger = new nsCycleCollectorLogger();
|
2010-08-12 04:03:23 +04:00
|
|
|
|
2014-05-05 21:30:39 +04:00
|
|
|
return logger->QueryInterface(aIID, aInstancePtr);
|
2010-08-12 04:03:23 +04:00
|
|
|
}
|
|
|
|
|
2014-12-10 03:22:51 +03:00
|
|
|
static bool
|
|
|
|
GCThingIsGrayCCThing(JS::GCCellPtr thing)
|
|
|
|
{
|
|
|
|
return AddToCCKind(thing.kind()) &&
|
2014-12-05 20:38:34 +03:00
|
|
|
JS::GCThingIsMarkedGray(thing);
|
2014-12-10 03:22:51 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
|
|
|
ValueIsGrayCCThing(const JS::Value& value)
|
|
|
|
{
|
|
|
|
return AddToCCKind(value.gcKind()) &&
|
2014-12-05 20:38:34 +03:00
|
|
|
JS::GCThingIsMarkedGray(value.toGCCellPtr());
|
2014-12-10 03:22:51 +03:00
|
|
|
}
|
|
|
|
|
2007-04-26 01:12:11 +04:00
|
|
|
////////////////////////////////////////////////////////////////////////
|
|
|
|
// Bacon & Rajan's |MarkRoots| routine.
|
|
|
|
////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2014-08-05 21:33:55 +04:00
|
|
|
class CCGraphBuilder MOZ_FINAL : public nsCycleCollectionTraversalCallback,
|
2014-05-13 21:41:38 +04:00
|
|
|
public nsCycleCollectionNoteRootCallback
|
2007-04-26 01:12:11 +04:00
|
|
|
{
|
|
|
|
private:
|
2014-07-09 23:31:00 +04:00
|
|
|
CCGraph& mGraph;
|
2014-05-13 21:41:38 +04:00
|
|
|
CycleCollectorResults& mResults;
|
2014-05-05 21:30:39 +04:00
|
|
|
NodePool::Builder mNodeBuilder;
|
|
|
|
EdgePool::Builder mEdgeBuilder;
|
2014-05-13 21:41:38 +04:00
|
|
|
PtrInfo* mCurrPi;
|
|
|
|
nsCycleCollectionParticipant* mJSParticipant;
|
|
|
|
nsCycleCollectionParticipant* mJSZoneParticipant;
|
2014-05-05 21:30:39 +04:00
|
|
|
nsCString mNextEdgeName;
|
2015-01-18 21:59:21 +03:00
|
|
|
nsCOMPtr<nsICycleCollectorListener> mListener;
|
2014-05-05 21:30:39 +04:00
|
|
|
bool mMergeZones;
|
2015-02-11 01:39:49 +03:00
|
|
|
bool mRanOutOfMemory;
|
2014-12-20 21:35:23 +03:00
|
|
|
nsAutoPtr<NodePool::Enumerator> mCurrNode;
|
2007-04-26 01:12:11 +04:00
|
|
|
|
|
|
|
public:
|
2014-07-09 23:31:00 +04:00
|
|
|
CCGraphBuilder(CCGraph& aGraph,
|
2014-05-13 21:41:38 +04:00
|
|
|
CycleCollectorResults& aResults,
|
|
|
|
CycleCollectedJSRuntime* aJSRuntime,
|
|
|
|
nsICycleCollectorListener* aListener,
|
2014-05-05 21:30:39 +04:00
|
|
|
bool aMergeZones);
|
2014-07-09 23:31:00 +04:00
|
|
|
virtual ~CCGraphBuilder();
|
2014-05-05 21:30:39 +04:00
|
|
|
|
|
|
|
bool WantAllTraces() const
|
|
|
|
{
|
|
|
|
return nsCycleCollectionNoteRootCallback::WantAllTraces();
|
|
|
|
}
|
2013-05-21 00:08:11 +04:00
|
|
|
|
2014-12-20 21:35:23 +03:00
|
|
|
bool AddPurpleRoot(void* aRoot, nsCycleCollectionParticipant* aParti);
|
2013-05-25 02:00:36 +04:00
|
|
|
|
2014-12-20 21:35:23 +03:00
|
|
|
// This is called when all roots have been added to the graph, to prepare for BuildGraph().
|
|
|
|
void DoneAddingRoots();
|
|
|
|
|
|
|
|
// Do some work traversing nodes in the graph. Returns true if this graph building is finished.
|
|
|
|
bool BuildGraph(SliceBudget& aBudget);
|
|
|
|
|
2008-02-16 01:23:16 +03:00
|
|
|
private:
|
2014-12-20 21:35:23 +03:00
|
|
|
PtrInfo* AddNode(void* aPtr, nsCycleCollectionParticipant* aParticipant);
|
2014-12-20 21:35:23 +03:00
|
|
|
PtrInfo* AddWeakMapNode(JS::GCCellPtr aThing);
|
|
|
|
PtrInfo* AddWeakMapNode(JSObject* aObject);
|
|
|
|
|
2014-12-20 21:35:24 +03:00
|
|
|
void SetFirstChild()
|
|
|
|
{
|
|
|
|
mCurrPi->SetFirstChild(mEdgeBuilder.Mark());
|
|
|
|
}
|
|
|
|
|
2014-12-20 21:35:23 +03:00
|
|
|
void SetLastChild()
|
|
|
|
{
|
|
|
|
mCurrPi->SetLastChild(mEdgeBuilder.Mark());
|
|
|
|
}
|
|
|
|
|
2012-05-03 23:28:11 +04:00
|
|
|
public:
|
2014-05-05 21:30:39 +04:00
|
|
|
// nsCycleCollectionNoteRootCallback methods.
|
2014-05-13 21:41:38 +04:00
|
|
|
NS_IMETHOD_(void) NoteXPCOMRoot(nsISupports* aRoot);
|
|
|
|
NS_IMETHOD_(void) NoteJSRoot(void* aRoot);
|
|
|
|
NS_IMETHOD_(void) NoteNativeRoot(void* aRoot,
|
|
|
|
nsCycleCollectionParticipant* aParticipant);
|
2014-12-05 20:38:32 +03:00
|
|
|
NS_IMETHOD_(void) NoteWeakMapping(JSObject* aMap, JS::GCCellPtr aKey,
|
|
|
|
JSObject* aKdelegate, JS::GCCellPtr aVal);
|
2014-05-05 21:30:39 +04:00
|
|
|
|
|
|
|
// nsCycleCollectionTraversalCallback methods.
|
2014-05-13 21:41:38 +04:00
|
|
|
NS_IMETHOD_(void) DescribeRefCountedNode(nsrefcnt aRefCount,
|
|
|
|
const char* aObjName);
|
|
|
|
NS_IMETHOD_(void) DescribeGCedNode(bool aIsMarked, const char* aObjName,
|
2014-05-05 21:30:39 +04:00
|
|
|
uint64_t aCompartmentAddress);
|
|
|
|
|
2014-05-13 21:41:38 +04:00
|
|
|
NS_IMETHOD_(void) NoteXPCOMChild(nsISupports* aChild);
|
2014-12-05 20:38:34 +03:00
|
|
|
NS_IMETHOD_(void) NoteJSObject(JSObject* aChild);
|
|
|
|
NS_IMETHOD_(void) NoteJSScript(JSScript* aChild);
|
2014-05-13 21:41:38 +04:00
|
|
|
NS_IMETHOD_(void) NoteNativeChild(void* aChild,
|
|
|
|
nsCycleCollectionParticipant* aParticipant);
|
|
|
|
NS_IMETHOD_(void) NoteNextEdgeName(const char* aName);
|
2012-05-03 23:28:11 +04:00
|
|
|
|
2012-02-27 01:18:44 +04:00
|
|
|
private:
|
2014-12-05 20:38:34 +03:00
|
|
|
void NoteJSChild(JS::GCCellPtr aChild);
|
|
|
|
|
2014-05-13 21:41:38 +04:00
|
|
|
NS_IMETHOD_(void) NoteRoot(void* aRoot,
|
|
|
|
nsCycleCollectionParticipant* aParticipant)
|
2014-05-05 21:30:39 +04:00
|
|
|
{
|
2014-05-13 21:41:38 +04:00
|
|
|
MOZ_ASSERT(aRoot);
|
|
|
|
MOZ_ASSERT(aParticipant);
|
2012-05-03 23:28:11 +04:00
|
|
|
|
2014-05-13 21:41:38 +04:00
|
|
|
if (!aParticipant->CanSkipInCC(aRoot) || MOZ_UNLIKELY(WantAllTraces())) {
|
|
|
|
AddNode(aRoot, aParticipant);
|
2012-05-03 23:28:11 +04:00
|
|
|
}
|
2014-05-05 21:30:39 +04:00
|
|
|
}
|
2012-05-03 23:28:11 +04:00
|
|
|
|
2014-05-13 21:41:38 +04:00
|
|
|
NS_IMETHOD_(void) NoteChild(void* aChild, nsCycleCollectionParticipant* aCp,
|
|
|
|
nsCString aEdgeName)
|
2014-05-05 21:30:39 +04:00
|
|
|
{
|
2014-05-13 21:41:38 +04:00
|
|
|
PtrInfo* childPi = AddNode(aChild, aCp);
|
|
|
|
if (!childPi) {
|
2014-05-05 21:30:39 +04:00
|
|
|
return;
|
2014-05-13 21:41:38 +04:00
|
|
|
}
|
2014-05-05 21:30:39 +04:00
|
|
|
mEdgeBuilder.Add(childPi);
|
|
|
|
if (mListener) {
|
2014-05-13 21:41:38 +04:00
|
|
|
mListener->NoteEdge((uint64_t)aChild, aEdgeName.get());
|
2012-02-27 01:18:44 +04:00
|
|
|
}
|
2014-05-05 21:30:39 +04:00
|
|
|
++childPi->mInternalRefs;
|
|
|
|
}
|
2012-06-27 19:09:56 +04:00
|
|
|
|
2014-05-13 21:41:38 +04:00
|
|
|
JS::Zone* MergeZone(void* aGcthing)
|
|
|
|
{
|
2014-05-05 21:30:39 +04:00
|
|
|
if (!mMergeZones) {
|
|
|
|
return nullptr;
|
2012-06-27 19:09:56 +04:00
|
|
|
}
|
2014-07-28 13:39:36 +04:00
|
|
|
JS::Zone* zone = JS::GetTenuredGCThingZone(aGcthing);
|
2014-05-05 21:30:39 +04:00
|
|
|
if (js::IsSystemZone(zone)) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
return zone;
|
|
|
|
}
|
2007-04-26 01:12:11 +04:00
|
|
|
};
|
|
|
|
|
2014-07-09 23:31:00 +04:00
|
|
|
CCGraphBuilder::CCGraphBuilder(CCGraph& aGraph,
|
2014-05-13 21:41:38 +04:00
|
|
|
CycleCollectorResults& aResults,
|
|
|
|
CycleCollectedJSRuntime* aJSRuntime,
|
|
|
|
nsICycleCollectorListener* aListener,
|
2013-03-17 07:36:37 +04:00
|
|
|
bool aMergeZones)
|
2014-05-13 21:41:38 +04:00
|
|
|
: mGraph(aGraph)
|
|
|
|
, mResults(aResults)
|
|
|
|
, mNodeBuilder(aGraph.mNodes)
|
|
|
|
, mEdgeBuilder(aGraph.mEdges)
|
|
|
|
, mJSParticipant(nullptr)
|
|
|
|
, mJSZoneParticipant(nullptr)
|
|
|
|
, mListener(aListener)
|
|
|
|
, mMergeZones(aMergeZones)
|
2015-02-11 01:39:49 +03:00
|
|
|
, mRanOutOfMemory(false)
|
2014-05-05 21:30:39 +04:00
|
|
|
{
|
|
|
|
if (aJSRuntime) {
|
|
|
|
mJSParticipant = aJSRuntime->GCThingParticipant();
|
|
|
|
mJSZoneParticipant = aJSRuntime->ZoneParticipant();
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t flags = 0;
|
|
|
|
if (!flags && mListener) {
|
|
|
|
flags = nsCycleCollectionTraversalCallback::WANT_DEBUG_INFO;
|
|
|
|
bool all = false;
|
|
|
|
mListener->GetWantAllTraces(&all);
|
|
|
|
if (all) {
|
|
|
|
flags |= nsCycleCollectionTraversalCallback::WANT_ALL_TRACES;
|
|
|
|
mWantAllTraces = true; // for nsCycleCollectionNoteRootCallback
|
2010-08-12 04:03:23 +04:00
|
|
|
}
|
2014-05-05 21:30:39 +04:00
|
|
|
}
|
2012-01-02 01:48:42 +04:00
|
|
|
|
2014-05-05 21:30:39 +04:00
|
|
|
mFlags |= flags;
|
2012-06-27 19:09:56 +04:00
|
|
|
|
2014-05-05 21:30:39 +04:00
|
|
|
mMergeZones = mMergeZones && MOZ_LIKELY(!WantAllTraces());
|
2013-05-21 00:08:11 +04:00
|
|
|
|
2014-05-05 21:30:39 +04:00
|
|
|
MOZ_ASSERT(nsCycleCollectionNoteRootCallback::WantAllTraces() ==
|
|
|
|
nsCycleCollectionTraversalCallback::WantAllTraces());
|
2007-04-26 01:12:11 +04:00
|
|
|
}
|
|
|
|
|
2014-07-09 23:31:00 +04:00
|
|
|
CCGraphBuilder::~CCGraphBuilder()
|
2007-04-26 01:12:11 +04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
PtrInfo*
|
2014-07-09 23:31:00 +04:00
|
|
|
CCGraphBuilder::AddNode(void* aPtr, nsCycleCollectionParticipant* aParticipant)
|
2007-04-26 01:12:11 +04:00
|
|
|
{
|
2014-05-13 21:41:38 +04:00
|
|
|
PtrToNodeEntry* e = mGraph.AddNodeToMap(aPtr);
|
2015-02-11 01:39:49 +03:00
|
|
|
if (!e) {
|
|
|
|
mRanOutOfMemory = true;
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2014-05-13 21:41:38 +04:00
|
|
|
PtrInfo* result;
|
2014-05-05 21:30:39 +04:00
|
|
|
if (!e->mNode) {
|
|
|
|
// New entry.
|
|
|
|
result = mNodeBuilder.Add(aPtr, aParticipant);
|
|
|
|
e->mNode = result;
|
|
|
|
NS_ASSERTION(result, "mNodeBuilder.Add returned null");
|
|
|
|
} else {
|
|
|
|
result = e->mNode;
|
|
|
|
MOZ_ASSERT(result->mParticipant == aParticipant,
|
|
|
|
"nsCycleCollectionParticipant shouldn't change!");
|
|
|
|
}
|
|
|
|
return result;
|
2007-04-26 01:12:11 +04:00
|
|
|
}
|
|
|
|
|
2014-12-20 21:35:23 +03:00
|
|
|
bool
|
|
|
|
CCGraphBuilder::AddPurpleRoot(void* aRoot, nsCycleCollectionParticipant* aParti)
|
|
|
|
{
|
|
|
|
CanonicalizeParticipant(&aRoot, &aParti);
|
|
|
|
|
|
|
|
if (WantAllTraces() || !aParti->CanSkipInCC(aRoot)) {
|
|
|
|
PtrInfo* pinfo = AddNode(aRoot, aParti);
|
|
|
|
if (!pinfo) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-12-20 21:35:23 +03:00
|
|
|
void
|
|
|
|
CCGraphBuilder::DoneAddingRoots()
|
|
|
|
{
|
|
|
|
// We've finished adding roots, and everything in the graph is a root.
|
|
|
|
mGraph.mRootCount = mGraph.MapCount();
|
|
|
|
|
|
|
|
mCurrNode = new NodePool::Enumerator(mGraph.mNodes);
|
|
|
|
}
|
|
|
|
|
2014-12-20 21:35:23 +03:00
|
|
|
MOZ_NEVER_INLINE bool
|
2014-12-20 21:35:23 +03:00
|
|
|
CCGraphBuilder::BuildGraph(SliceBudget& aBudget)
|
|
|
|
{
|
|
|
|
const intptr_t kNumNodesBetweenTimeChecks = 1000;
|
|
|
|
const intptr_t kStep = SliceBudget::CounterReset / kNumNodesBetweenTimeChecks;
|
|
|
|
|
|
|
|
MOZ_ASSERT(mCurrNode);
|
|
|
|
|
|
|
|
while (!aBudget.isOverBudget() && !mCurrNode->IsDone()) {
|
|
|
|
PtrInfo* pi = mCurrNode->GetNext();
|
|
|
|
if (!pi) {
|
|
|
|
MOZ_CRASH();
|
|
|
|
}
|
|
|
|
|
2014-12-20 21:35:23 +03:00
|
|
|
mCurrPi = pi;
|
|
|
|
|
2014-12-20 21:35:24 +03:00
|
|
|
// We need to call SetFirstChild() even on deleted nodes, to set their
|
2014-12-20 21:35:23 +03:00
|
|
|
// firstChild() that may be read by a prior non-deleted neighbor.
|
2014-12-20 21:35:24 +03:00
|
|
|
SetFirstChild();
|
2014-12-20 21:35:23 +03:00
|
|
|
|
|
|
|
if (pi->mParticipant) {
|
|
|
|
nsresult rv = pi->mParticipant->Traverse(pi->mPointer, *this);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
Fault("script pointer traversal failed", pi);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-20 21:35:23 +03:00
|
|
|
if (mCurrNode->AtBlockEnd()) {
|
|
|
|
SetLastChild();
|
|
|
|
}
|
2014-12-20 21:35:23 +03:00
|
|
|
|
2014-12-20 21:35:23 +03:00
|
|
|
aBudget.step(kStep);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!mCurrNode->IsDone()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mGraph.mRootCount > 0) {
|
|
|
|
SetLastChild();
|
|
|
|
}
|
|
|
|
|
2015-02-11 01:39:49 +03:00
|
|
|
if (mRanOutOfMemory) {
|
|
|
|
MOZ_ASSERT(false, "Ran out of memory while building cycle collector graph");
|
|
|
|
CC_TELEMETRY(_OOM, true);
|
|
|
|
}
|
|
|
|
|
2014-12-20 21:35:23 +03:00
|
|
|
mCurrNode = nullptr;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2007-11-02 01:51:57 +03:00
|
|
|
NS_IMETHODIMP_(void)
|
2014-07-09 23:31:00 +04:00
|
|
|
CCGraphBuilder::NoteXPCOMRoot(nsISupports* aRoot)
|
2007-11-02 01:51:57 +03:00
|
|
|
{
|
2014-05-13 21:41:38 +04:00
|
|
|
aRoot = CanonicalizeXPCOMParticipant(aRoot);
|
|
|
|
NS_ASSERTION(aRoot,
|
2014-05-05 21:30:39 +04:00
|
|
|
"Don't add objects that don't participate in collection!");
|
2007-11-02 01:51:57 +03:00
|
|
|
|
2014-05-13 21:41:38 +04:00
|
|
|
nsXPCOMCycleCollectionParticipant* cp;
|
|
|
|
ToParticipant(aRoot, &cp);
|
2007-11-02 01:51:57 +03:00
|
|
|
|
2014-05-13 21:41:38 +04:00
|
|
|
NoteRoot(aRoot, cp);
|
2007-11-02 01:51:57 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP_(void)
|
2014-07-09 23:31:00 +04:00
|
|
|
CCGraphBuilder::NoteJSRoot(void* aRoot)
|
2007-11-02 01:51:57 +03:00
|
|
|
{
|
2014-05-13 21:41:38 +04:00
|
|
|
if (JS::Zone* zone = MergeZone(aRoot)) {
|
2014-05-05 21:30:39 +04:00
|
|
|
NoteRoot(zone, mJSZoneParticipant);
|
|
|
|
} else {
|
2014-05-13 21:41:38 +04:00
|
|
|
NoteRoot(aRoot, mJSParticipant);
|
2014-05-05 21:30:39 +04:00
|
|
|
}
|
2012-05-03 23:28:11 +04:00
|
|
|
}
|
2007-11-02 01:51:57 +03:00
|
|
|
|
2012-05-03 23:28:11 +04:00
|
|
|
NS_IMETHODIMP_(void)
|
2014-07-09 23:31:00 +04:00
|
|
|
CCGraphBuilder::NoteNativeRoot(void* aRoot,
|
2014-05-13 21:41:38 +04:00
|
|
|
nsCycleCollectionParticipant* aParticipant)
|
2012-05-03 23:28:11 +04:00
|
|
|
{
|
2014-05-13 21:41:38 +04:00
|
|
|
NoteRoot(aRoot, aParticipant);
|
2007-11-02 01:51:57 +03:00
|
|
|
}
|
|
|
|
|
2007-09-18 04:30:06 +04:00
|
|
|
NS_IMETHODIMP_(void)
|
2014-07-09 23:31:00 +04:00
|
|
|
CCGraphBuilder::DescribeRefCountedNode(nsrefcnt aRefCount, const char* aObjName)
|
2011-06-24 01:10:52 +04:00
|
|
|
{
|
2014-05-13 21:41:38 +04:00
|
|
|
if (aRefCount == 0) {
|
2014-05-05 21:30:39 +04:00
|
|
|
Fault("zero refcount", mCurrPi);
|
2014-05-13 21:41:38 +04:00
|
|
|
}
|
|
|
|
if (aRefCount == UINT32_MAX) {
|
2014-05-05 21:30:39 +04:00
|
|
|
Fault("overflowing refcount", mCurrPi);
|
2014-05-13 21:41:38 +04:00
|
|
|
}
|
2014-05-05 21:30:39 +04:00
|
|
|
mResults.mVisitedRefCounted++;
|
2011-06-22 21:41:17 +04:00
|
|
|
|
2014-05-05 21:30:39 +04:00
|
|
|
if (mListener) {
|
2014-05-13 21:41:38 +04:00
|
|
|
mListener->NoteRefCountedObject((uint64_t)mCurrPi->mPointer, aRefCount,
|
|
|
|
aObjName);
|
2014-05-05 21:30:39 +04:00
|
|
|
}
|
2011-06-22 21:41:17 +04:00
|
|
|
|
2014-12-20 21:35:24 +03:00
|
|
|
mCurrPi->mRefCount = aRefCount;
|
2011-06-24 01:10:52 +04:00
|
|
|
}
|
2007-07-06 02:38:38 +04:00
|
|
|
|
2011-06-24 01:10:52 +04:00
|
|
|
NS_IMETHODIMP_(void)
|
2014-07-09 23:31:00 +04:00
|
|
|
CCGraphBuilder::DescribeGCedNode(bool aIsMarked, const char* aObjName,
|
2013-11-12 17:53:51 +04:00
|
|
|
uint64_t aCompartmentAddress)
|
2011-06-24 01:10:52 +04:00
|
|
|
{
|
2014-05-13 21:41:38 +04:00
|
|
|
uint32_t refCount = aIsMarked ? UINT32_MAX : 0;
|
2014-05-05 21:30:39 +04:00
|
|
|
mResults.mVisitedGCed++;
|
2011-06-22 21:41:17 +04:00
|
|
|
|
2014-05-05 21:30:39 +04:00
|
|
|
if (mListener) {
|
2014-05-13 21:41:38 +04:00
|
|
|
mListener->NoteGCedObject((uint64_t)mCurrPi->mPointer, aIsMarked,
|
|
|
|
aObjName, aCompartmentAddress);
|
2014-05-05 21:30:39 +04:00
|
|
|
}
|
2011-06-22 21:41:17 +04:00
|
|
|
|
2014-12-20 21:35:24 +03:00
|
|
|
mCurrPi->mRefCount = refCount;
|
2007-01-05 01:31:26 +03:00
|
|
|
}
|
|
|
|
|
2007-09-18 04:30:06 +04:00
|
|
|
NS_IMETHODIMP_(void)
|
2014-07-09 23:31:00 +04:00
|
|
|
CCGraphBuilder::NoteXPCOMChild(nsISupports* aChild)
|
2007-01-05 01:31:26 +03:00
|
|
|
{
|
2014-05-05 21:30:39 +04:00
|
|
|
nsCString edgeName;
|
|
|
|
if (WantDebugInfo()) {
|
|
|
|
edgeName.Assign(mNextEdgeName);
|
|
|
|
mNextEdgeName.Truncate();
|
|
|
|
}
|
2014-05-13 21:41:38 +04:00
|
|
|
if (!aChild || !(aChild = CanonicalizeXPCOMParticipant(aChild))) {
|
2014-05-05 21:30:39 +04:00
|
|
|
return;
|
2014-05-13 21:41:38 +04:00
|
|
|
}
|
2007-01-09 04:33:02 +03:00
|
|
|
|
2014-05-13 21:41:38 +04:00
|
|
|
nsXPCOMCycleCollectionParticipant* cp;
|
|
|
|
ToParticipant(aChild, &cp);
|
|
|
|
if (cp && (!cp->CanSkipThis(aChild) || WantAllTraces())) {
|
|
|
|
NoteChild(aChild, cp, edgeName);
|
2014-05-05 21:30:39 +04:00
|
|
|
}
|
2007-01-05 01:31:26 +03:00
|
|
|
}
|
|
|
|
|
2007-09-18 04:30:06 +04:00
|
|
|
NS_IMETHODIMP_(void)
|
2014-07-09 23:31:00 +04:00
|
|
|
CCGraphBuilder::NoteNativeChild(void* aChild,
|
2014-05-13 21:41:38 +04:00
|
|
|
nsCycleCollectionParticipant* aParticipant)
|
2007-05-24 18:10:02 +04:00
|
|
|
{
|
2014-05-05 21:30:39 +04:00
|
|
|
nsCString edgeName;
|
|
|
|
if (WantDebugInfo()) {
|
|
|
|
edgeName.Assign(mNextEdgeName);
|
|
|
|
mNextEdgeName.Truncate();
|
|
|
|
}
|
2014-05-13 21:41:38 +04:00
|
|
|
if (!aChild) {
|
2014-05-05 21:30:39 +04:00
|
|
|
return;
|
2014-05-13 21:41:38 +04:00
|
|
|
}
|
2007-05-24 18:10:02 +04:00
|
|
|
|
2014-05-13 21:41:38 +04:00
|
|
|
MOZ_ASSERT(aParticipant, "Need a nsCycleCollectionParticipant!");
|
2014-12-13 00:42:21 +03:00
|
|
|
if (!aParticipant->CanSkipThis(aChild) || WantAllTraces()) {
|
|
|
|
NoteChild(aChild, aParticipant, edgeName);
|
|
|
|
}
|
2007-05-24 18:10:02 +04:00
|
|
|
}
|
|
|
|
|
2007-09-18 04:30:06 +04:00
|
|
|
NS_IMETHODIMP_(void)
|
2014-12-05 20:38:34 +03:00
|
|
|
CCGraphBuilder::NoteJSObject(JSObject* aChild)
|
|
|
|
{
|
|
|
|
return NoteJSChild(JS::GCCellPtr(aChild));
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP_(void)
|
|
|
|
CCGraphBuilder::NoteJSScript(JSScript* aChild)
|
|
|
|
{
|
|
|
|
return NoteJSChild(JS::GCCellPtr(aChild));
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
CCGraphBuilder::NoteJSChild(JS::GCCellPtr aChild)
|
2007-01-05 01:31:26 +03:00
|
|
|
{
|
2014-05-13 21:41:38 +04:00
|
|
|
if (!aChild) {
|
2014-05-05 21:30:39 +04:00
|
|
|
return;
|
|
|
|
}
|
2008-06-21 19:25:29 +04:00
|
|
|
|
2014-05-05 21:30:39 +04:00
|
|
|
nsCString edgeName;
|
|
|
|
if (MOZ_UNLIKELY(WantDebugInfo())) {
|
|
|
|
edgeName.Assign(mNextEdgeName);
|
|
|
|
mNextEdgeName.Truncate();
|
|
|
|
}
|
2007-01-09 04:33:02 +03:00
|
|
|
|
2014-12-10 03:22:51 +03:00
|
|
|
if (GCThingIsGrayCCThing(aChild) || MOZ_UNLIKELY(WantAllTraces())) {
|
2014-12-05 20:38:34 +03:00
|
|
|
if (JS::Zone* zone = MergeZone(aChild.asCell())) {
|
2014-05-05 21:30:39 +04:00
|
|
|
NoteChild(zone, mJSZoneParticipant, edgeName);
|
|
|
|
} else {
|
2014-12-05 20:38:34 +03:00
|
|
|
NoteChild(aChild.asCell(), mJSParticipant, edgeName);
|
2011-03-29 00:05:48 +04:00
|
|
|
}
|
2014-05-05 21:30:39 +04:00
|
|
|
}
|
2007-01-05 01:31:26 +03:00
|
|
|
}
|
|
|
|
|
2008-03-18 02:11:08 +03:00
|
|
|
NS_IMETHODIMP_(void)
|
2014-07-09 23:31:00 +04:00
|
|
|
CCGraphBuilder::NoteNextEdgeName(const char* aName)
|
2008-03-18 02:11:08 +03:00
|
|
|
{
|
2014-05-05 21:30:39 +04:00
|
|
|
if (WantDebugInfo()) {
|
2014-05-13 21:41:38 +04:00
|
|
|
mNextEdgeName = aName;
|
2014-05-05 21:30:39 +04:00
|
|
|
}
|
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*
|
2014-12-10 03:22:51 +03:00
|
|
|
CCGraphBuilder::AddWeakMapNode(JS::GCCellPtr aNode)
|
2011-11-24 16:35:56 +04:00
|
|
|
{
|
2014-05-13 21:41:38 +04:00
|
|
|
MOZ_ASSERT(aNode, "Weak map node should be non-null.");
|
2011-11-24 16:35:56 +04:00
|
|
|
|
2014-12-10 03:22:51 +03:00
|
|
|
if (!GCThingIsGrayCCThing(aNode) && !WantAllTraces()) {
|
2014-05-05 21:30:39 +04:00
|
|
|
return nullptr;
|
2014-05-13 21:41:38 +04:00
|
|
|
}
|
2011-11-24 16:35:56 +04:00
|
|
|
|
2014-12-10 03:22:51 +03:00
|
|
|
if (JS::Zone* zone = MergeZone(aNode.asCell())) {
|
2014-05-05 21:30:39 +04:00
|
|
|
return AddNode(zone, mJSZoneParticipant);
|
|
|
|
}
|
2014-12-10 03:22:51 +03:00
|
|
|
return AddNode(aNode.asCell(), mJSParticipant);
|
|
|
|
}
|
|
|
|
|
|
|
|
PtrInfo*
|
|
|
|
CCGraphBuilder::AddWeakMapNode(JSObject* aObject)
|
|
|
|
{
|
|
|
|
return AddWeakMapNode(JS::GCCellPtr(aObject));
|
2011-11-24 16:35:56 +04:00
|
|
|
}
|
|
|
|
|
2011-11-24 16:35:56 +04:00
|
|
|
NS_IMETHODIMP_(void)
|
2014-12-05 20:38:32 +03:00
|
|
|
CCGraphBuilder::NoteWeakMapping(JSObject* aMap, JS::GCCellPtr aKey,
|
|
|
|
JSObject* aKdelegate, JS::GCCellPtr aVal)
|
2011-11-24 16:35:56 +04:00
|
|
|
{
|
2014-05-05 21:30:39 +04:00
|
|
|
// Don't try to optimize away the entry here, as we've already attempted to
|
|
|
|
// do that in TraceWeakMapping in nsXPConnect.
|
2014-05-13 21:41:38 +04:00
|
|
|
WeakMapping* mapping = mGraph.mWeakMaps.AppendElement();
|
|
|
|
mapping->mMap = aMap ? AddWeakMapNode(aMap) : nullptr;
|
2014-12-10 03:22:51 +03:00
|
|
|
mapping->mKey = aKey ? AddWeakMapNode(aKey) : nullptr;
|
2014-05-13 21:41:38 +04:00
|
|
|
mapping->mKeyDelegate = aKdelegate ? AddWeakMapNode(aKdelegate) : mapping->mKey;
|
2014-12-10 03:22:51 +03:00
|
|
|
mapping->mVal = aVal ? AddWeakMapNode(aVal) : nullptr;
|
2014-05-05 21:30:39 +04:00
|
|
|
|
|
|
|
if (mListener) {
|
2014-12-05 20:38:32 +03:00
|
|
|
mListener->NoteWeakMapEntry((uint64_t)aMap, aKey.unsafeAsInteger(),
|
|
|
|
(uint64_t)aKdelegate, aVal.unsafeAsInteger());
|
2014-05-05 21:30:39 +04:00
|
|
|
}
|
2011-11-24 16:35:56 +04:00
|
|
|
}
|
|
|
|
|
2012-08-24 20:50:06 +04:00
|
|
|
static bool
|
2014-07-09 23:31:00 +04:00
|
|
|
AddPurpleRoot(CCGraphBuilder& aBuilder, void* aRoot,
|
2014-05-13 21:41:38 +04:00
|
|
|
nsCycleCollectionParticipant* aParti)
|
2012-08-24 20:50:06 +04:00
|
|
|
{
|
2014-12-20 21:35:23 +03:00
|
|
|
return aBuilder.AddPurpleRoot(aRoot, aParti);
|
2012-08-24 20:50:06 +04:00
|
|
|
}
|
|
|
|
|
2012-03-06 01:48:04 +04:00
|
|
|
// MayHaveChild() will be false after a Traverse if the object does
|
|
|
|
// not have any children the CC will visit.
|
|
|
|
class ChildFinder : public nsCycleCollectionTraversalCallback
|
|
|
|
{
|
|
|
|
public:
|
2014-05-13 21:41:38 +04:00
|
|
|
ChildFinder() : mMayHaveChild(false)
|
|
|
|
{
|
|
|
|
}
|
2014-05-05 21:30:39 +04:00
|
|
|
|
|
|
|
// The logic of the Note*Child functions must mirror that of their
|
2014-07-09 23:31:00 +04:00
|
|
|
// respective functions in CCGraphBuilder.
|
2014-05-13 21:41:38 +04:00
|
|
|
NS_IMETHOD_(void) NoteXPCOMChild(nsISupports* aChild);
|
|
|
|
NS_IMETHOD_(void) NoteNativeChild(void* aChild,
|
|
|
|
nsCycleCollectionParticipant* aHelper);
|
2014-12-05 20:38:34 +03:00
|
|
|
NS_IMETHOD_(void) NoteJSObject(JSObject* aChild);
|
|
|
|
NS_IMETHOD_(void) NoteJSScript(JSScript* aChild);
|
2014-05-13 21:41:38 +04:00
|
|
|
|
|
|
|
NS_IMETHOD_(void) DescribeRefCountedNode(nsrefcnt aRefcount,
|
|
|
|
const char* aObjname)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
NS_IMETHOD_(void) DescribeGCedNode(bool aIsMarked,
|
|
|
|
const char* aObjname,
|
|
|
|
uint64_t aCompartmentAddress)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
NS_IMETHOD_(void) NoteNextEdgeName(const char* aName)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
bool MayHaveChild()
|
|
|
|
{
|
2014-05-05 21:30:39 +04:00
|
|
|
return mMayHaveChild;
|
|
|
|
}
|
2012-03-06 01:48:04 +04:00
|
|
|
private:
|
2014-05-05 21:30:39 +04:00
|
|
|
bool mMayHaveChild;
|
2012-03-06 01:48:04 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
NS_IMETHODIMP_(void)
|
2014-05-13 21:41:38 +04:00
|
|
|
ChildFinder::NoteXPCOMChild(nsISupports* aChild)
|
2012-03-06 01:48:04 +04:00
|
|
|
{
|
2014-05-13 21:41:38 +04:00
|
|
|
if (!aChild || !(aChild = CanonicalizeXPCOMParticipant(aChild))) {
|
2014-05-05 21:30:39 +04:00
|
|
|
return;
|
2014-05-13 21:41:38 +04:00
|
|
|
}
|
|
|
|
nsXPCOMCycleCollectionParticipant* cp;
|
|
|
|
ToParticipant(aChild, &cp);
|
|
|
|
if (cp && !cp->CanSkip(aChild, true)) {
|
2014-05-05 21:30:39 +04:00
|
|
|
mMayHaveChild = true;
|
2014-05-13 21:41:38 +04:00
|
|
|
}
|
2012-07-26 19:16:28 +04:00
|
|
|
}
|
2012-03-06 01:48:04 +04:00
|
|
|
|
|
|
|
NS_IMETHODIMP_(void)
|
2014-05-13 21:41:38 +04:00
|
|
|
ChildFinder::NoteNativeChild(void* aChild,
|
|
|
|
nsCycleCollectionParticipant* aHelper)
|
2012-03-06 01:48:04 +04:00
|
|
|
{
|
2014-12-13 00:42:21 +03:00
|
|
|
if (!aChild) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
MOZ_ASSERT(aHelper, "Native child must have a participant");
|
|
|
|
if (!aHelper->CanSkip(aChild, true)) {
|
2014-05-05 21:30:39 +04:00
|
|
|
mMayHaveChild = true;
|
2014-05-13 21:41:38 +04:00
|
|
|
}
|
2012-07-26 19:16:28 +04:00
|
|
|
}
|
2012-03-06 01:48:04 +04:00
|
|
|
|
|
|
|
NS_IMETHODIMP_(void)
|
2014-12-05 20:38:34 +03:00
|
|
|
ChildFinder::NoteJSObject(JSObject* aChild)
|
|
|
|
{
|
2014-12-10 03:22:51 +03:00
|
|
|
if (aChild && JS::ObjectIsMarkedGray(aChild)) {
|
2014-12-05 20:38:34 +03:00
|
|
|
mMayHaveChild = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP_(void)
|
|
|
|
ChildFinder::NoteJSScript(JSScript* aChild)
|
2012-03-06 01:48:04 +04:00
|
|
|
{
|
2014-12-10 03:22:51 +03:00
|
|
|
if (aChild && JS::ScriptIsMarkedGray(aChild)) {
|
2014-05-05 21:30:39 +04:00
|
|
|
mMayHaveChild = true;
|
|
|
|
}
|
2012-07-26 19:16:28 +04:00
|
|
|
}
|
2012-03-06 01:48:04 +04:00
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
static bool
|
2014-05-13 21:41:38 +04:00
|
|
|
MayHaveChild(void* aObj, nsCycleCollectionParticipant* aCp)
|
2012-03-06 01:48:04 +04:00
|
|
|
{
|
2014-05-05 21:30:39 +04:00
|
|
|
ChildFinder cf;
|
2014-05-13 21:41:38 +04:00
|
|
|
aCp->Traverse(aObj, cf);
|
2014-05-05 21:30:39 +04:00
|
|
|
return cf.MayHaveChild();
|
2012-03-06 01:48:04 +04:00
|
|
|
}
|
|
|
|
|
2014-01-15 00:23:59 +04:00
|
|
|
// JSPurpleBuffer keeps references to GCThings which might affect the
|
|
|
|
// next cycle collection. It is owned only by itself and during unlink its
|
|
|
|
// self reference is broken down and the object ends up killing itself.
|
|
|
|
// If GC happens before CC, references to GCthings and the self reference are
|
|
|
|
// removed.
|
|
|
|
class JSPurpleBuffer
|
|
|
|
{
|
2014-07-01 02:11:53 +04:00
|
|
|
~JSPurpleBuffer()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(mValues.IsEmpty());
|
|
|
|
MOZ_ASSERT(mObjects.IsEmpty());
|
|
|
|
}
|
|
|
|
|
2014-01-15 00:23:59 +04:00
|
|
|
public:
|
2015-01-18 21:59:21 +03:00
|
|
|
explicit JSPurpleBuffer(nsRefPtr<JSPurpleBuffer>& aReferenceToThis)
|
2014-05-05 21:30:39 +04:00
|
|
|
: mReferenceToThis(aReferenceToThis)
|
2014-12-09 01:45:13 +03:00
|
|
|
, mValues(kSegmentSize)
|
|
|
|
, mObjects(kSegmentSize)
|
2014-05-05 21:30:39 +04:00
|
|
|
{
|
|
|
|
mReferenceToThis = this;
|
|
|
|
mozilla::HoldJSObjects(this);
|
|
|
|
}
|
2014-01-15 00:23:59 +04:00
|
|
|
|
2014-05-05 21:30:39 +04:00
|
|
|
void Destroy()
|
|
|
|
{
|
|
|
|
mReferenceToThis = nullptr;
|
|
|
|
mValues.Clear();
|
|
|
|
mObjects.Clear();
|
|
|
|
mozilla::DropJSObjects(this);
|
|
|
|
}
|
2014-01-15 00:23:59 +04:00
|
|
|
|
2014-05-05 21:30:39 +04:00
|
|
|
NS_INLINE_DECL_CYCLE_COLLECTING_NATIVE_REFCOUNTING(JSPurpleBuffer)
|
|
|
|
NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_NATIVE_CLASS(JSPurpleBuffer)
|
2014-01-15 00:23:59 +04:00
|
|
|
|
2015-01-18 21:59:21 +03:00
|
|
|
nsRefPtr<JSPurpleBuffer>& mReferenceToThis;
|
2014-08-01 01:43:45 +04:00
|
|
|
|
|
|
|
// These are raw pointers instead of Heap<T> because we only need Heap<T> for
|
|
|
|
// pointers which may point into the nursery. The purple buffer never contains
|
|
|
|
// pointers to the nursery because nursery gcthings can never be gray and only
|
|
|
|
// gray things can be inserted into the purple buffer.
|
2014-12-09 01:45:13 +03:00
|
|
|
static const size_t kSegmentSize = 512;
|
|
|
|
SegmentedVector<JS::Value, kSegmentSize, InfallibleAllocPolicy> mValues;
|
|
|
|
SegmentedVector<JSObject*, kSegmentSize, InfallibleAllocPolicy> mObjects;
|
2014-01-15 00:23:59 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_CLASS(JSPurpleBuffer)
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(JSPurpleBuffer)
|
2014-05-05 21:30:39 +04:00
|
|
|
tmp->Destroy();
|
2014-01-15 00:23:59 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_END
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(JSPurpleBuffer)
|
2014-05-05 21:30:39 +04:00
|
|
|
CycleCollectionNoteChild(cb, tmp, "self");
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_SCRIPT_OBJECTS
|
2014-01-15 00:23:59 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
|
|
|
|
|
2014-12-09 01:45:13 +03:00
|
|
|
#define NS_TRACE_SEGMENTED_ARRAY(_field, _type) \
|
|
|
|
{ \
|
|
|
|
for (auto iter = tmp->_field.Iter(); !iter.Done(); iter.Next()) { \
|
|
|
|
js::gc::CallTraceCallbackOnNonHeap<_type, TraceCallbacks>( \
|
|
|
|
&iter.Get(), aCallbacks, #_field, aClosure); \
|
|
|
|
} \
|
2014-08-01 01:43:45 +04:00
|
|
|
}
|
2014-01-15 00:23:59 +04:00
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRACE_BEGIN(JSPurpleBuffer)
|
2014-08-01 01:43:45 +04:00
|
|
|
NS_TRACE_SEGMENTED_ARRAY(mValues, JS::Value)
|
|
|
|
NS_TRACE_SEGMENTED_ARRAY(mObjects, JSObject*)
|
2014-01-15 00:23:59 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRACE_END
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_ROOT_NATIVE(JSPurpleBuffer, AddRef)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNROOT_NATIVE(JSPurpleBuffer, Release)
|
|
|
|
|
|
|
|
class SnowWhiteKiller : public TraceCallbacks
|
2012-01-14 20:58:05 +04:00
|
|
|
{
|
2014-11-10 01:57:09 +03:00
|
|
|
struct SnowWhiteObject
|
|
|
|
{
|
|
|
|
void* mPointer;
|
|
|
|
nsCycleCollectionParticipant* mParticipant;
|
|
|
|
nsCycleCollectingAutoRefCnt* mRefCnt;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Segments are 4 KiB on 32-bit and 8 KiB on 64-bit.
|
2014-12-09 01:45:13 +03:00
|
|
|
static const size_t kSegmentSize = sizeof(void*) * 1024;
|
|
|
|
typedef SegmentedVector<SnowWhiteObject, kSegmentSize, InfallibleAllocPolicy>
|
|
|
|
ObjectsVector;
|
2014-11-10 01:57:09 +03:00
|
|
|
|
2013-04-30 21:41:23 +04:00
|
|
|
public:
|
2014-05-13 21:41:38 +04:00
|
|
|
SnowWhiteKiller(nsCycleCollector* aCollector, uint32_t aMaxCount)
|
2014-05-05 21:30:39 +04:00
|
|
|
: mCollector(aCollector)
|
2014-12-09 01:45:13 +03:00
|
|
|
, mObjects(kSegmentSize)
|
2014-05-05 21:30:39 +04:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(mCollector, "Calling SnowWhiteKiller after nsCC went away");
|
|
|
|
}
|
2013-07-09 21:30:58 +04:00
|
|
|
|
2014-05-05 21:30:39 +04:00
|
|
|
~SnowWhiteKiller()
|
|
|
|
{
|
2014-12-09 01:45:13 +03:00
|
|
|
for (auto iter = mObjects.Iter(); !iter.Done(); iter.Next()) {
|
|
|
|
SnowWhiteObject& o = iter.Get();
|
|
|
|
if (!o.mRefCnt->get() && !o.mRefCnt->IsInPurpleBuffer()) {
|
|
|
|
mCollector->RemoveObjectFromGraph(o.mPointer);
|
|
|
|
o.mRefCnt->stabilizeForDeletion();
|
|
|
|
o.mParticipant->Trace(o.mPointer, *this, nullptr);
|
|
|
|
o.mParticipant->DeleteCycleCollectable(o.mPointer);
|
2014-05-05 21:30:39 +04:00
|
|
|
}
|
2013-07-09 21:30:58 +04:00
|
|
|
}
|
2014-05-05 21:30:39 +04:00
|
|
|
}
|
2013-07-09 21:30:58 +04:00
|
|
|
|
2014-05-05 21:30:39 +04:00
|
|
|
void
|
|
|
|
Visit(nsPurpleBuffer& aBuffer, nsPurpleBufferEntry* aEntry)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(aEntry->mObject, "Null object in purple buffer");
|
|
|
|
if (!aEntry->mRefCnt->get()) {
|
2014-05-13 21:41:38 +04:00
|
|
|
void* o = aEntry->mObject;
|
|
|
|
nsCycleCollectionParticipant* cp = aEntry->mParticipant;
|
2014-05-05 21:30:39 +04:00
|
|
|
CanonicalizeParticipant(&o, &cp);
|
|
|
|
SnowWhiteObject swo = { o, cp, aEntry->mRefCnt };
|
2014-12-09 01:45:13 +03:00
|
|
|
mObjects.InfallibleAppend(swo);
|
2014-11-10 01:57:09 +03:00
|
|
|
aBuffer.Remove(aEntry);
|
2013-07-09 21:30:58 +04:00
|
|
|
}
|
2014-05-05 21:30:39 +04:00
|
|
|
}
|
2013-07-09 21:30:58 +04:00
|
|
|
|
2014-05-05 21:30:39 +04:00
|
|
|
bool HasSnowWhiteObjects() const
|
|
|
|
{
|
2014-11-10 01:57:09 +03:00
|
|
|
return !mObjects.IsEmpty();
|
2014-05-05 21:30:39 +04:00
|
|
|
}
|
2013-12-06 22:17:20 +04:00
|
|
|
|
2014-05-05 21:30:39 +04:00
|
|
|
virtual void Trace(JS::Heap<JS::Value>* aValue, const char* aName,
|
|
|
|
void* aClosure) const
|
|
|
|
{
|
2014-12-10 03:22:51 +03:00
|
|
|
if (aValue->isMarkable() && ValueIsGrayCCThing(*aValue)) {
|
|
|
|
MOZ_ASSERT(!js::gc::IsInsideNursery(aValue->toGCThing()));
|
|
|
|
mCollector->GetJSPurpleBuffer()->mValues.InfallibleAppend(*aValue);
|
2014-01-15 00:23:59 +04:00
|
|
|
}
|
2014-05-05 21:30:39 +04:00
|
|
|
}
|
2014-01-15 00:23:59 +04:00
|
|
|
|
2014-05-05 21:30:39 +04:00
|
|
|
virtual void Trace(JS::Heap<jsid>* aId, const char* aName,
|
|
|
|
void* aClosure) const
|
|
|
|
{
|
|
|
|
}
|
2014-01-15 00:23:59 +04:00
|
|
|
|
2014-08-25 23:17:15 +04:00
|
|
|
void AppendJSObjectToPurpleBuffer(JSObject* obj) const
|
2014-08-01 01:43:45 +04:00
|
|
|
{
|
2014-12-10 03:22:51 +03:00
|
|
|
if (obj && JS::ObjectIsMarkedGray(obj)) {
|
2014-12-14 19:27:52 +03:00
|
|
|
MOZ_ASSERT(JS::ObjectIsTenured(obj));
|
2014-12-09 01:45:13 +03:00
|
|
|
mCollector->GetJSPurpleBuffer()->mObjects.InfallibleAppend(obj);
|
2014-08-01 01:43:45 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-05 21:30:39 +04:00
|
|
|
virtual void Trace(JS::Heap<JSObject*>* aObject, const char* aName,
|
|
|
|
void* aClosure) const
|
|
|
|
{
|
2014-08-01 01:43:45 +04:00
|
|
|
AppendJSObjectToPurpleBuffer(*aObject);
|
2014-05-05 21:30:39 +04:00
|
|
|
}
|
2014-01-15 00:23:59 +04:00
|
|
|
|
2014-05-05 21:30:39 +04:00
|
|
|
virtual void Trace(JS::TenuredHeap<JSObject*>* aObject, const char* aName,
|
|
|
|
void* aClosure) const
|
|
|
|
{
|
2014-08-01 01:43:45 +04:00
|
|
|
AppendJSObjectToPurpleBuffer(*aObject);
|
2014-05-05 21:30:39 +04:00
|
|
|
}
|
2014-03-03 20:53:42 +04:00
|
|
|
|
2014-05-05 21:30:39 +04:00
|
|
|
virtual void Trace(JS::Heap<JSString*>* aString, const char* aName,
|
|
|
|
void* aClosure) const
|
|
|
|
{
|
|
|
|
}
|
2014-01-15 00:23:59 +04:00
|
|
|
|
2014-05-05 21:30:39 +04:00
|
|
|
virtual void Trace(JS::Heap<JSScript*>* aScript, const char* aName,
|
|
|
|
void* aClosure) const
|
|
|
|
{
|
|
|
|
}
|
2014-01-15 00:23:59 +04:00
|
|
|
|
2014-05-05 21:30:39 +04:00
|
|
|
virtual void Trace(JS::Heap<JSFunction*>* aFunction, const char* aName,
|
|
|
|
void* aClosure) const
|
|
|
|
{
|
|
|
|
}
|
2014-01-15 00:23:59 +04:00
|
|
|
|
2013-07-09 21:30:58 +04:00
|
|
|
private:
|
2015-01-18 21:59:21 +03:00
|
|
|
nsRefPtr<nsCycleCollector> mCollector;
|
2014-12-09 01:45:13 +03:00
|
|
|
ObjectsVector mObjects;
|
2013-07-09 21:30:58 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
class RemoveSkippableVisitor : public SnowWhiteKiller
|
|
|
|
{
|
|
|
|
public:
|
2014-05-05 21:30:39 +04:00
|
|
|
RemoveSkippableVisitor(nsCycleCollector* aCollector,
|
|
|
|
uint32_t aMaxCount, bool aRemoveChildlessNodes,
|
|
|
|
bool aAsyncSnowWhiteFreeing,
|
|
|
|
CC_ForgetSkippableCallback aCb)
|
2014-05-13 21:41:38 +04:00
|
|
|
: SnowWhiteKiller(aCollector, aAsyncSnowWhiteFreeing ? 0 : aMaxCount)
|
|
|
|
, mRemoveChildlessNodes(aRemoveChildlessNodes)
|
|
|
|
, mAsyncSnowWhiteFreeing(aAsyncSnowWhiteFreeing)
|
|
|
|
, mDispatchedDeferredDeletion(false)
|
|
|
|
, mCallback(aCb)
|
|
|
|
{
|
|
|
|
}
|
2014-05-05 21:30:39 +04:00
|
|
|
|
|
|
|
~RemoveSkippableVisitor()
|
|
|
|
{
|
|
|
|
// Note, we must call the callback before SnowWhiteKiller calls
|
|
|
|
// DeleteCycleCollectable!
|
|
|
|
if (mCallback) {
|
|
|
|
mCallback();
|
2013-07-09 21:30:58 +04:00
|
|
|
}
|
2014-05-05 21:30:39 +04:00
|
|
|
if (HasSnowWhiteObjects()) {
|
|
|
|
// Effectively a continuation.
|
|
|
|
nsCycleCollector_dispatchDeferredDeletion(true);
|
|
|
|
}
|
|
|
|
}
|
2013-07-09 21:30:58 +04:00
|
|
|
|
2014-05-05 21:30:39 +04:00
|
|
|
void
|
2014-05-13 21:41:38 +04:00
|
|
|
Visit(nsPurpleBuffer& aBuffer, nsPurpleBufferEntry* aEntry)
|
2014-05-05 21:30:39 +04:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(aEntry->mObject, "null mObject in purple buffer");
|
|
|
|
if (!aEntry->mRefCnt->get()) {
|
|
|
|
if (!mAsyncSnowWhiteFreeing) {
|
|
|
|
SnowWhiteKiller::Visit(aBuffer, aEntry);
|
|
|
|
} else if (!mDispatchedDeferredDeletion) {
|
|
|
|
mDispatchedDeferredDeletion = true;
|
|
|
|
nsCycleCollector_dispatchDeferredDeletion(false);
|
|
|
|
}
|
|
|
|
return;
|
2012-01-14 20:58:05 +04:00
|
|
|
}
|
2014-05-13 21:41:38 +04:00
|
|
|
void* o = aEntry->mObject;
|
|
|
|
nsCycleCollectionParticipant* cp = aEntry->mParticipant;
|
2014-05-05 21:30:39 +04:00
|
|
|
CanonicalizeParticipant(&o, &cp);
|
|
|
|
if (aEntry->mRefCnt->IsPurple() && !cp->CanSkip(o, false) &&
|
|
|
|
(!mRemoveChildlessNodes || MayHaveChild(o, cp))) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
aBuffer.Remove(aEntry);
|
|
|
|
}
|
2013-04-30 21:41:23 +04:00
|
|
|
|
|
|
|
private:
|
2014-05-05 21:30:39 +04:00
|
|
|
bool mRemoveChildlessNodes;
|
|
|
|
bool mAsyncSnowWhiteFreeing;
|
|
|
|
bool mDispatchedDeferredDeletion;
|
|
|
|
CC_ForgetSkippableCallback mCallback;
|
2013-04-30 21:41:23 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
void
|
2013-07-27 14:48:45 +04:00
|
|
|
nsPurpleBuffer::RemoveSkippable(nsCycleCollector* aCollector,
|
|
|
|
bool aRemoveChildlessNodes,
|
|
|
|
bool aAsyncSnowWhiteFreeing,
|
2013-07-09 21:30:58 +04:00
|
|
|
CC_ForgetSkippableCallback aCb)
|
2013-04-30 21:41:23 +04:00
|
|
|
{
|
2014-05-05 21:30:39 +04:00
|
|
|
RemoveSkippableVisitor visitor(aCollector, Count(), aRemoveChildlessNodes,
|
|
|
|
aAsyncSnowWhiteFreeing, aCb);
|
|
|
|
VisitEntries(visitor);
|
2013-07-09 21:30:58 +04:00
|
|
|
}
|
|
|
|
|
2013-07-27 14:48:45 +04:00
|
|
|
bool
|
2013-07-09 21:30:58 +04:00
|
|
|
nsCycleCollector::FreeSnowWhite(bool aUntilNoSWInPurpleBuffer)
|
|
|
|
{
|
2014-05-05 21:30:39 +04:00
|
|
|
CheckThreadSafety();
|
2013-09-07 00:41:42 +04:00
|
|
|
|
2014-05-05 21:30:39 +04:00
|
|
|
if (mFreeingSnowWhite) {
|
|
|
|
return false;
|
|
|
|
}
|
2014-02-12 02:56:44 +04:00
|
|
|
|
2014-05-05 21:30:39 +04:00
|
|
|
AutoRestore<bool> ar(mFreeingSnowWhite);
|
|
|
|
mFreeingSnowWhite = true;
|
2014-02-12 02:56:44 +04:00
|
|
|
|
2014-05-05 21:30:39 +04:00
|
|
|
bool hadSnowWhiteObjects = false;
|
|
|
|
do {
|
|
|
|
SnowWhiteKiller visitor(this, mPurpleBuf.Count());
|
|
|
|
mPurpleBuf.VisitEntries(visitor);
|
|
|
|
hadSnowWhiteObjects = hadSnowWhiteObjects ||
|
|
|
|
visitor.HasSnowWhiteObjects();
|
|
|
|
if (!visitor.HasSnowWhiteObjects()) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} while (aUntilNoSWInPurpleBuffer);
|
|
|
|
return hadSnowWhiteObjects;
|
2013-07-09 21:30:58 +04:00
|
|
|
}
|
|
|
|
|
2013-05-04 19:39:44 +04:00
|
|
|
void
|
2013-07-27 14:48:45 +04:00
|
|
|
nsCycleCollector::ForgetSkippable(bool aRemoveChildlessNodes,
|
|
|
|
bool aAsyncSnowWhiteFreeing)
|
2012-01-14 20:58:05 +04:00
|
|
|
{
|
2014-05-05 21:30:39 +04:00
|
|
|
CheckThreadSafety();
|
2013-12-18 07:29:57 +04:00
|
|
|
|
2014-05-05 21:30:39 +04:00
|
|
|
// If we remove things from the purple buffer during graph building, we may
|
|
|
|
// lose track of an object that was mutated during graph building.
|
|
|
|
MOZ_ASSERT(mIncrementalPhase == IdlePhase);
|
2013-12-18 07:29:57 +04:00
|
|
|
|
2014-05-05 21:30:39 +04:00
|
|
|
if (mJSRuntime) {
|
|
|
|
mJSRuntime->PrepareForForgetSkippable();
|
|
|
|
}
|
2014-05-13 21:41:38 +04:00
|
|
|
MOZ_ASSERT(!mScanInProgress,
|
|
|
|
"Don't forget skippable or free snow-white while scan is in progress.");
|
2014-05-05 21:30:39 +04:00
|
|
|
mPurpleBuf.RemoveSkippable(this, aRemoveChildlessNodes,
|
|
|
|
aAsyncSnowWhiteFreeing, mForgetSkippableCB);
|
2012-01-14 20:58:05 +04:00
|
|
|
}
|
|
|
|
|
2012-02-21 00:13:29 +04:00
|
|
|
MOZ_NEVER_INLINE void
|
2014-05-13 21:41:38 +04:00
|
|
|
nsCycleCollector::MarkRoots(SliceBudget& aBudget)
|
2007-01-05 01:31:26 +03:00
|
|
|
{
|
2014-05-05 21:30:39 +04:00
|
|
|
TimeLog timeLog;
|
|
|
|
AutoRestore<bool> ar(mScanInProgress);
|
|
|
|
MOZ_ASSERT(!mScanInProgress);
|
|
|
|
mScanInProgress = true;
|
|
|
|
MOZ_ASSERT(mIncrementalPhase == GraphBuildingPhase);
|
2013-12-03 22:47:47 +04:00
|
|
|
|
2014-12-20 21:35:23 +03:00
|
|
|
bool doneBuilding = mBuilder->BuildGraph(aBudget);
|
2013-12-03 22:47:47 +04:00
|
|
|
|
2014-12-20 21:35:23 +03:00
|
|
|
if (!doneBuilding) {
|
2014-05-05 21:30:39 +04:00
|
|
|
timeLog.Checkpoint("MarkRoots()");
|
|
|
|
return;
|
|
|
|
}
|
2013-05-25 02:00:36 +04:00
|
|
|
|
2014-05-05 21:30:39 +04:00
|
|
|
mBuilder = nullptr;
|
|
|
|
mIncrementalPhase = ScanAndCollectWhitePhase;
|
|
|
|
timeLog.Checkpoint("MarkRoots()");
|
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
|
|
|
{
|
2014-05-13 21:41:38 +04:00
|
|
|
ScanBlackVisitor(uint32_t& aWhiteNodeCount, bool& aFailed)
|
2014-05-05 21:30:39 +04:00
|
|
|
: mWhiteNodeCount(aWhiteNodeCount), mFailed(aFailed)
|
|
|
|
{
|
|
|
|
}
|
2008-02-15 16:12:55 +03:00
|
|
|
|
2014-05-13 21:41:38 +04:00
|
|
|
bool ShouldVisitNode(PtrInfo const* aPi)
|
2014-05-05 21:30:39 +04:00
|
|
|
{
|
2014-05-13 21:41:38 +04:00
|
|
|
return aPi->mColor != black;
|
2014-05-05 21:30:39 +04:00
|
|
|
}
|
2007-01-05 01:31:26 +03:00
|
|
|
|
2014-05-13 21:41:38 +04:00
|
|
|
MOZ_NEVER_INLINE void VisitNode(PtrInfo* aPi)
|
2014-05-05 21:30:39 +04:00
|
|
|
{
|
2014-05-13 21:41:38 +04:00
|
|
|
if (aPi->mColor == white) {
|
2014-05-05 21:30:39 +04:00
|
|
|
--mWhiteNodeCount;
|
2014-05-13 21:41:38 +04:00
|
|
|
}
|
|
|
|
aPi->mColor = black;
|
2014-05-05 21:30:39 +04:00
|
|
|
}
|
2008-02-15 16:12:55 +03:00
|
|
|
|
2014-05-05 21:30:39 +04:00
|
|
|
void Failed()
|
|
|
|
{
|
|
|
|
mFailed = true;
|
|
|
|
}
|
2013-05-29 04:42:13 +04:00
|
|
|
|
|
|
|
private:
|
2014-05-13 21:41:38 +04:00
|
|
|
uint32_t& mWhiteNodeCount;
|
|
|
|
bool& mFailed;
|
2007-01-05 01:31:26 +03:00
|
|
|
};
|
|
|
|
|
2014-05-07 04:25:27 +04:00
|
|
|
static void
|
|
|
|
FloodBlackNode(uint32_t& aWhiteNodeCount, bool& aFailed, PtrInfo* aPi)
|
|
|
|
{
|
2014-08-25 23:17:15 +04:00
|
|
|
GraphWalker<ScanBlackVisitor>(ScanBlackVisitor(aWhiteNodeCount,
|
|
|
|
aFailed)).Walk(aPi);
|
|
|
|
MOZ_ASSERT(aPi->mColor == black || !aPi->WasTraversed(),
|
|
|
|
"FloodBlackNode should make aPi black");
|
2014-05-07 04:25:27 +04:00
|
|
|
}
|
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()
|
|
|
|
{
|
2014-05-05 21:30:39 +04:00
|
|
|
bool anyChanged;
|
|
|
|
bool failed = false;
|
|
|
|
do {
|
|
|
|
anyChanged = false;
|
|
|
|
for (uint32_t i = 0; i < mGraph.mWeakMaps.Length(); i++) {
|
2014-05-13 21:41:38 +04:00
|
|
|
WeakMapping* wm = &mGraph.mWeakMaps[i];
|
2014-05-05 21:30:39 +04:00
|
|
|
|
|
|
|
// If any of these are null, the original object was marked black.
|
|
|
|
uint32_t mColor = wm->mMap ? wm->mMap->mColor : black;
|
|
|
|
uint32_t kColor = wm->mKey ? wm->mKey->mColor : black;
|
|
|
|
uint32_t kdColor = wm->mKeyDelegate ? wm->mKeyDelegate->mColor : black;
|
|
|
|
uint32_t vColor = wm->mVal ? wm->mVal->mColor : black;
|
|
|
|
|
|
|
|
MOZ_ASSERT(mColor != grey, "Uncolored weak map");
|
|
|
|
MOZ_ASSERT(kColor != grey, "Uncolored weak map key");
|
|
|
|
MOZ_ASSERT(kdColor != grey, "Uncolored weak map key delegate");
|
|
|
|
MOZ_ASSERT(vColor != grey, "Uncolored weak map value");
|
|
|
|
|
|
|
|
if (mColor == black && kColor != black && kdColor == black) {
|
2014-05-07 04:25:27 +04:00
|
|
|
FloodBlackNode(mWhiteNodeCount, failed, wm->mKey);
|
2014-05-05 21:30:39 +04:00
|
|
|
anyChanged = true;
|
|
|
|
}
|
2013-05-29 04:42:13 +04:00
|
|
|
|
2014-05-05 21:30:39 +04:00
|
|
|
if (mColor == black && kColor == black && vColor != black) {
|
2014-05-07 04:25:27 +04:00
|
|
|
FloodBlackNode(mWhiteNodeCount, failed, wm->mVal);
|
2014-05-05 21:30:39 +04:00
|
|
|
anyChanged = true;
|
|
|
|
}
|
2013-05-29 04:42:13 +04:00
|
|
|
}
|
2014-05-05 21:30:39 +04:00
|
|
|
} while (anyChanged);
|
|
|
|
|
|
|
|
if (failed) {
|
|
|
|
MOZ_ASSERT(false, "Ran out of memory in ScanWeakMaps");
|
|
|
|
CC_TELEMETRY(_OOM, true);
|
|
|
|
}
|
2011-11-24 16:35:57 +04:00
|
|
|
}
|
|
|
|
|
2013-12-18 07:29:57 +04:00
|
|
|
// Flood black from any objects in the purple buffer that are in the CC graph.
|
|
|
|
class PurpleScanBlackVisitor
|
|
|
|
{
|
|
|
|
public:
|
2014-07-09 23:31:00 +04:00
|
|
|
PurpleScanBlackVisitor(CCGraph& aGraph, nsICycleCollectorListener* aListener,
|
2014-05-13 21:41:38 +04:00
|
|
|
uint32_t& aCount, bool& aFailed)
|
2014-05-05 21:30:39 +04:00
|
|
|
: mGraph(aGraph), mListener(aListener), mCount(aCount), mFailed(aFailed)
|
|
|
|
{
|
|
|
|
}
|
2013-12-18 07:29:57 +04:00
|
|
|
|
2014-05-05 21:30:39 +04:00
|
|
|
void
|
2014-05-13 21:41:38 +04:00
|
|
|
Visit(nsPurpleBuffer& aBuffer, nsPurpleBufferEntry* aEntry)
|
2014-05-05 21:30:39 +04:00
|
|
|
{
|
2014-05-13 21:41:38 +04:00
|
|
|
MOZ_ASSERT(aEntry->mObject,
|
|
|
|
"Entries with null mObject shouldn't be in the purple buffer.");
|
|
|
|
MOZ_ASSERT(aEntry->mRefCnt->get() != 0,
|
|
|
|
"Snow-white objects shouldn't be in the purple buffer.");
|
2013-12-18 07:29:57 +04:00
|
|
|
|
2014-05-13 21:41:38 +04:00
|
|
|
void* obj = aEntry->mObject;
|
2014-05-05 21:30:39 +04:00
|
|
|
if (!aEntry->mParticipant) {
|
|
|
|
obj = CanonicalizeXPCOMParticipant(static_cast<nsISupports*>(obj));
|
|
|
|
MOZ_ASSERT(obj, "Don't add objects that don't participate in collection!");
|
|
|
|
}
|
2013-12-18 07:29:57 +04:00
|
|
|
|
2014-05-13 21:41:38 +04:00
|
|
|
PtrInfo* pi = mGraph.FindNode(obj);
|
2014-05-05 21:30:39 +04:00
|
|
|
if (!pi) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
MOZ_ASSERT(pi->mParticipant, "No dead objects should be in the purple buffer.");
|
|
|
|
if (MOZ_UNLIKELY(mListener)) {
|
|
|
|
mListener->NoteIncrementalRoot((uint64_t)pi->mPointer);
|
|
|
|
}
|
|
|
|
if (pi->mColor == black) {
|
|
|
|
return;
|
2013-12-18 07:29:57 +04:00
|
|
|
}
|
2014-05-07 04:25:27 +04:00
|
|
|
FloodBlackNode(mCount, mFailed, pi);
|
2014-05-05 21:30:39 +04:00
|
|
|
}
|
2013-12-18 07:29:57 +04:00
|
|
|
|
|
|
|
private:
|
2014-07-09 23:31:00 +04:00
|
|
|
CCGraph& mGraph;
|
2015-01-18 21:59:21 +03:00
|
|
|
nsCOMPtr<nsICycleCollectorListener> mListener;
|
2014-05-13 21:41:38 +04:00
|
|
|
uint32_t& mCount;
|
|
|
|
bool& mFailed;
|
2013-12-18 07:29:57 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
// Objects that have been stored somewhere since the start of incremental graph building must
|
|
|
|
// be treated as live for this cycle collection, because we may not have accurate information
|
|
|
|
// about who holds references to them.
|
2007-01-05 01:31:26 +03:00
|
|
|
void
|
2013-12-18 07:29:57 +04:00
|
|
|
nsCycleCollector::ScanIncrementalRoots()
|
2007-01-05 01:31:26 +03:00
|
|
|
{
|
2014-05-05 21:30:39 +04:00
|
|
|
TimeLog timeLog;
|
|
|
|
|
|
|
|
// Reference counted objects:
|
|
|
|
// We cleared the purple buffer at the start of the current ICC, so if a
|
|
|
|
// refcounted object is purple, it may have been AddRef'd during the current
|
|
|
|
// ICC. (It may also have only been released.) If that is the case, we cannot
|
|
|
|
// be sure that the set of things pointing to the object in the CC graph
|
|
|
|
// is accurate. Therefore, for safety, we treat any purple objects as being
|
|
|
|
// live during the current CC. We don't remove anything from the purple
|
|
|
|
// buffer here, so these objects will be suspected and freed in the next CC
|
|
|
|
// if they are garbage.
|
|
|
|
bool failed = false;
|
2014-08-25 23:17:15 +04:00
|
|
|
PurpleScanBlackVisitor purpleScanBlackVisitor(mGraph, mListener,
|
|
|
|
mWhiteNodeCount, failed);
|
2014-05-05 21:30:39 +04:00
|
|
|
mPurpleBuf.VisitEntries(purpleScanBlackVisitor);
|
|
|
|
timeLog.Checkpoint("ScanIncrementalRoots::fix purple");
|
|
|
|
|
2014-07-01 03:18:46 +04:00
|
|
|
bool hasJSRuntime = !!mJSRuntime;
|
2014-08-25 23:17:15 +04:00
|
|
|
nsCycleCollectionParticipant* jsParticipant =
|
|
|
|
hasJSRuntime ? mJSRuntime->GCThingParticipant() : nullptr;
|
|
|
|
nsCycleCollectionParticipant* zoneParticipant =
|
|
|
|
hasJSRuntime ? mJSRuntime->ZoneParticipant() : nullptr;
|
2014-07-01 03:18:46 +04:00
|
|
|
bool hasListener = !!mListener;
|
2014-05-05 21:30:39 +04:00
|
|
|
|
2014-07-01 03:18:46 +04:00
|
|
|
NodePool::Enumerator etor(mGraph.mNodes);
|
|
|
|
while (!etor.IsDone()) {
|
|
|
|
PtrInfo* pi = etor.GetNext();
|
2014-05-05 21:30:39 +04:00
|
|
|
|
2014-07-01 03:18:46 +04:00
|
|
|
// As an optimization, if an object has already been determined to be live,
|
|
|
|
// don't consider it further. We can't do this if there is a listener,
|
|
|
|
// because the listener wants to know the complete set of incremental roots.
|
|
|
|
if (pi->mColor == black && MOZ_LIKELY(!hasListener)) {
|
|
|
|
continue;
|
|
|
|
}
|
2014-05-05 21:30:39 +04:00
|
|
|
|
2014-07-01 03:18:46 +04:00
|
|
|
// Garbage collected objects:
|
|
|
|
// If a GCed object was added to the graph with a refcount of zero, and is
|
|
|
|
// now marked black by the GC, it was probably gray before and was exposed
|
|
|
|
// to active JS, so it may have been stored somewhere, so it needs to be
|
|
|
|
// treated as live.
|
|
|
|
if (pi->IsGrayJS() && MOZ_LIKELY(hasJSRuntime)) {
|
2014-05-05 21:30:39 +04:00
|
|
|
// If the object is still marked gray by the GC, nothing could have gotten
|
|
|
|
// hold of it, so it isn't an incremental root.
|
|
|
|
if (pi->mParticipant == jsParticipant) {
|
2014-12-10 03:22:51 +03:00
|
|
|
JS::GCCellPtr ptr(pi->mPointer, js::GCThingTraceKind(pi->mPointer));
|
|
|
|
if (GCThingIsGrayCCThing(ptr)) {
|
2014-05-05 21:30:39 +04:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
} else if (pi->mParticipant == zoneParticipant) {
|
2014-05-13 21:41:38 +04:00
|
|
|
JS::Zone* zone = static_cast<JS::Zone*>(pi->mPointer);
|
2014-05-05 21:30:39 +04:00
|
|
|
if (js::ZoneGlobalsAreAllGray(zone)) {
|
|
|
|
continue;
|
2013-12-18 07:29:57 +04:00
|
|
|
}
|
2014-05-05 21:30:39 +04:00
|
|
|
} else {
|
|
|
|
MOZ_ASSERT(false, "Non-JS thing with 0 refcount? Treating as live.");
|
|
|
|
}
|
2014-07-01 03:18:47 +04:00
|
|
|
} else if (!pi->mParticipant && pi->WasTraversed()) {
|
|
|
|
// Dead traversed refcounted objects:
|
|
|
|
// If the object was traversed, it must have been alive at the start of
|
|
|
|
// the CC, and thus had a positive refcount. It is dead now, so its
|
|
|
|
// refcount must have decreased at some point during the CC. Therefore,
|
|
|
|
// it would be in the purple buffer if it wasn't dead, so treat it as an
|
|
|
|
// incremental root.
|
|
|
|
//
|
|
|
|
// This should not cause leaks because as the object died it should have
|
|
|
|
// released anything it held onto, which will add them to the purple
|
|
|
|
// buffer, which will cause them to be considered in the next CC.
|
2014-07-01 03:18:46 +04:00
|
|
|
} else {
|
|
|
|
continue;
|
|
|
|
}
|
2013-12-18 07:29:57 +04:00
|
|
|
|
2014-07-01 03:18:46 +04:00
|
|
|
// At this point, pi must be an incremental root.
|
2014-05-05 21:30:39 +04:00
|
|
|
|
2014-07-01 03:18:46 +04:00
|
|
|
// If there's a listener, tell it about this root. We don't bother with the
|
|
|
|
// optimization of skipping the Walk() if pi is black: it will just return
|
|
|
|
// without doing anything and there's no need to make this case faster.
|
2014-07-01 03:18:47 +04:00
|
|
|
if (MOZ_UNLIKELY(hasListener) && pi->mPointer) {
|
|
|
|
// Dead objects aren't logged. See bug 1031370.
|
2014-07-01 03:18:46 +04:00
|
|
|
mListener->NoteIncrementalRoot((uint64_t)pi->mPointer);
|
2013-12-18 07:29:57 +04:00
|
|
|
}
|
2014-05-05 21:30:39 +04:00
|
|
|
|
2014-07-01 03:18:46 +04:00
|
|
|
FloodBlackNode(mWhiteNodeCount, failed, pi);
|
2014-05-05 21:30:39 +04:00
|
|
|
}
|
|
|
|
|
2014-07-01 03:18:47 +04:00
|
|
|
timeLog.Checkpoint("ScanIncrementalRoots::fix nodes");
|
2014-07-01 03:18:46 +04:00
|
|
|
|
2014-05-05 21:30:39 +04:00
|
|
|
if (failed) {
|
|
|
|
NS_ASSERTION(false, "Ran out of memory in ScanIncrementalRoots");
|
|
|
|
CC_TELEMETRY(_OOM, true);
|
|
|
|
}
|
2013-12-18 07:29:57 +04:00
|
|
|
}
|
|
|
|
|
2014-05-08 22:28:03 +04:00
|
|
|
// Mark nodes white and make sure their refcounts are ok.
|
|
|
|
// No nodes are marked black during this pass to ensure that refcount
|
|
|
|
// checking is run on all nodes not marked black by ScanIncrementalRoots.
|
|
|
|
void
|
|
|
|
nsCycleCollector::ScanWhiteNodes(bool aFullySynchGraphBuild)
|
|
|
|
{
|
|
|
|
NodePool::Enumerator nodeEnum(mGraph.mNodes);
|
|
|
|
while (!nodeEnum.IsDone()) {
|
|
|
|
PtrInfo* pi = nodeEnum.GetNext();
|
|
|
|
if (pi->mColor == black) {
|
|
|
|
// Incremental roots can be in a nonsensical state, so don't
|
|
|
|
// check them. This will miss checking nodes that are merely
|
|
|
|
// reachable from incremental roots.
|
|
|
|
MOZ_ASSERT(!aFullySynchGraphBuild,
|
|
|
|
"In a synch CC, no nodes should be marked black early on.");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
MOZ_ASSERT(pi->mColor == grey);
|
|
|
|
|
2014-07-01 03:18:47 +04:00
|
|
|
if (!pi->WasTraversed()) {
|
|
|
|
// This node was deleted before it was traversed, so there's no reason
|
|
|
|
// to look at it.
|
|
|
|
MOZ_ASSERT(!pi->mParticipant, "Live nodes should all have been traversed");
|
2014-05-08 22:28:03 +04:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2014-05-14 20:45:50 +04:00
|
|
|
if (pi->mInternalRefs == pi->mRefCount || pi->IsGrayJS()) {
|
2014-05-08 22:28:03 +04:00
|
|
|
pi->mColor = white;
|
|
|
|
++mWhiteNodeCount;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (MOZ_LIKELY(pi->mInternalRefs < pi->mRefCount)) {
|
|
|
|
// This node will get marked black in the next pass.
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
Fault("Traversed refs exceed refcount", pi);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Any remaining grey nodes that haven't already been deleted must be alive,
|
|
|
|
// so mark them and their children black. Any nodes that are black must have
|
|
|
|
// already had their children marked black, so there's no need to look at them
|
|
|
|
// again. This pass may turn some white nodes to black.
|
|
|
|
void
|
|
|
|
nsCycleCollector::ScanBlackNodes()
|
|
|
|
{
|
|
|
|
bool failed = false;
|
|
|
|
NodePool::Enumerator nodeEnum(mGraph.mNodes);
|
|
|
|
while (!nodeEnum.IsDone()) {
|
|
|
|
PtrInfo* pi = nodeEnum.GetNext();
|
2014-07-01 03:18:47 +04:00
|
|
|
if (pi->mColor == grey && pi->WasTraversed()) {
|
2014-05-08 22:28:03 +04:00
|
|
|
FloodBlackNode(mWhiteNodeCount, failed, pi);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (failed) {
|
|
|
|
NS_ASSERTION(false, "Ran out of memory in ScanBlackNodes");
|
|
|
|
CC_TELEMETRY(_OOM, true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-18 07:29:57 +04:00
|
|
|
void
|
|
|
|
nsCycleCollector::ScanRoots(bool aFullySynchGraphBuild)
|
|
|
|
{
|
2014-05-05 21:30:39 +04:00
|
|
|
AutoRestore<bool> ar(mScanInProgress);
|
|
|
|
MOZ_ASSERT(!mScanInProgress);
|
|
|
|
mScanInProgress = true;
|
|
|
|
mWhiteNodeCount = 0;
|
|
|
|
MOZ_ASSERT(mIncrementalPhase == ScanAndCollectWhitePhase);
|
2008-02-15 16:12:55 +03:00
|
|
|
|
2014-05-05 21:30:39 +04:00
|
|
|
if (!aFullySynchGraphBuild) {
|
|
|
|
ScanIncrementalRoots();
|
|
|
|
}
|
2013-12-18 07:29:57 +04:00
|
|
|
|
2014-05-05 21:30:39 +04:00
|
|
|
TimeLog timeLog;
|
2014-05-08 22:28:03 +04:00
|
|
|
ScanWhiteNodes(aFullySynchGraphBuild);
|
|
|
|
timeLog.Checkpoint("ScanRoots::ScanWhiteNodes");
|
2013-12-18 07:29:57 +04:00
|
|
|
|
2014-05-08 22:28:03 +04:00
|
|
|
ScanBlackNodes();
|
|
|
|
timeLog.Checkpoint("ScanRoots::ScanBlackNodes");
|
2007-01-05 01:31:26 +03:00
|
|
|
|
2014-05-05 21:30:39 +04:00
|
|
|
// Scanning weak maps must be done last.
|
|
|
|
ScanWeakMaps();
|
|
|
|
timeLog.Checkpoint("ScanRoots::ScanWeakMaps");
|
2013-09-11 03:33:39 +04:00
|
|
|
|
2014-05-05 21:30:39 +04:00
|
|
|
if (mListener) {
|
|
|
|
mListener->BeginResults();
|
2013-09-11 03:33:40 +04:00
|
|
|
|
2014-05-05 21:30:39 +04:00
|
|
|
NodePool::Enumerator etor(mGraph.mNodes);
|
|
|
|
while (!etor.IsDone()) {
|
2014-05-13 21:41:38 +04:00
|
|
|
PtrInfo* pi = etor.GetNext();
|
2014-07-01 03:18:47 +04:00
|
|
|
if (!pi->WasTraversed()) {
|
2014-05-05 21:30:39 +04:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
switch (pi->mColor) {
|
|
|
|
case black:
|
2014-05-14 20:45:50 +04:00
|
|
|
if (!pi->IsGrayJS() && !pi->IsBlackJS() &&
|
2014-05-05 21:30:39 +04:00
|
|
|
pi->mInternalRefs != pi->mRefCount) {
|
|
|
|
mListener->DescribeRoot((uint64_t)pi->mPointer,
|
|
|
|
pi->mInternalRefs);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case white:
|
|
|
|
mListener->DescribeGarbage((uint64_t)pi->mPointer);
|
|
|
|
break;
|
|
|
|
case grey:
|
2014-07-01 03:18:47 +04:00
|
|
|
MOZ_ASSERT(false, "All traversed objects should be black or white");
|
2014-05-05 21:30:39 +04:00
|
|
|
break;
|
|
|
|
}
|
2013-09-11 03:33:39 +04:00
|
|
|
}
|
2014-05-05 21:30:39 +04:00
|
|
|
|
|
|
|
mListener->End();
|
|
|
|
mListener = nullptr;
|
|
|
|
timeLog.Checkpoint("ScanRoots::listener");
|
|
|
|
}
|
2007-01-05 01:31:26 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////
|
|
|
|
// Bacon & Rajan's |CollectWhite| routine, somewhat modified.
|
|
|
|
////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool
|
2013-09-11 03:33:40 +04:00
|
|
|
nsCycleCollector::CollectWhite()
|
2007-01-05 01:31:26 +03:00
|
|
|
{
|
2014-05-05 21:30:39 +04: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.
|
|
|
|
|
2014-11-12 03:02:34 +03:00
|
|
|
// Segments are 4 KiB on 32-bit and 8 KiB on 64-bit.
|
2014-12-09 01:45:13 +03:00
|
|
|
static const size_t kSegmentSize = sizeof(void*) * 1024;
|
|
|
|
SegmentedVector<PtrInfo*, kSegmentSize, InfallibleAllocPolicy>
|
|
|
|
whiteNodes(kSegmentSize);
|
2014-05-05 21:30:39 +04:00
|
|
|
TimeLog timeLog;
|
|
|
|
|
|
|
|
MOZ_ASSERT(mIncrementalPhase == ScanAndCollectWhitePhase);
|
|
|
|
|
2014-11-12 03:02:34 +03:00
|
|
|
uint32_t numWhiteNodes = 0;
|
2014-05-05 21:30:39 +04:00
|
|
|
uint32_t numWhiteGCed = 0;
|
2014-10-20 21:07:52 +04:00
|
|
|
uint32_t numWhiteJSZones = 0;
|
|
|
|
|
|
|
|
bool hasJSRuntime = !!mJSRuntime;
|
|
|
|
nsCycleCollectionParticipant* zoneParticipant =
|
|
|
|
hasJSRuntime ? mJSRuntime->ZoneParticipant() : nullptr;
|
2014-05-05 21:30:39 +04:00
|
|
|
|
|
|
|
NodePool::Enumerator etor(mGraph.mNodes);
|
2014-05-13 21:41:38 +04:00
|
|
|
while (!etor.IsDone()) {
|
|
|
|
PtrInfo* pinfo = etor.GetNext();
|
2014-05-05 21:30:39 +04:00
|
|
|
if (pinfo->mColor == white && pinfo->mParticipant) {
|
2014-05-14 20:45:50 +04:00
|
|
|
if (pinfo->IsGrayJS()) {
|
2014-05-05 21:30:39 +04:00
|
|
|
++numWhiteGCed;
|
2014-10-20 21:07:52 +04:00
|
|
|
if (MOZ_UNLIKELY(pinfo->mParticipant == zoneParticipant)) {
|
|
|
|
++numWhiteJSZones;
|
|
|
|
}
|
2014-10-25 02:06:56 +04:00
|
|
|
} else {
|
2014-12-09 01:45:13 +03:00
|
|
|
whiteNodes.InfallibleAppend(pinfo);
|
2014-10-25 02:06:56 +04:00
|
|
|
pinfo->mParticipant->Root(pinfo->mPointer);
|
2014-11-12 03:02:34 +03:00
|
|
|
++numWhiteNodes;
|
2014-05-05 21:30:39 +04:00
|
|
|
}
|
2007-04-26 01:12:11 +04:00
|
|
|
}
|
2014-05-05 21:30:39 +04:00
|
|
|
}
|
2012-02-24 08:16:37 +04:00
|
|
|
|
2014-10-25 02:06:56 +04:00
|
|
|
mResults.mFreedRefCounted += numWhiteNodes;
|
2014-05-05 21:30:39 +04:00
|
|
|
mResults.mFreedGCed += numWhiteGCed;
|
2014-10-20 21:07:52 +04:00
|
|
|
mResults.mFreedJSZones += numWhiteJSZones;
|
2012-02-24 08:16:37 +04:00
|
|
|
|
2014-05-05 21:30:39 +04:00
|
|
|
timeLog.Checkpoint("CollectWhite::Root");
|
2008-03-07 20:55:51 +03:00
|
|
|
|
2014-05-05 21:30:39 +04:00
|
|
|
if (mBeforeUnlinkCB) {
|
|
|
|
mBeforeUnlinkCB();
|
|
|
|
timeLog.Checkpoint("CollectWhite::BeforeUnlinkCB");
|
|
|
|
}
|
2008-03-07 20:55:51 +03:00
|
|
|
|
2014-10-25 02:06:56 +04:00
|
|
|
// Unlink() can trigger a GC, so do not touch any JS or anything
|
|
|
|
// else not in whiteNodes after here.
|
|
|
|
|
2014-12-09 01:45:13 +03:00
|
|
|
for (auto iter = whiteNodes.Iter(); !iter.Done(); iter.Next()) {
|
|
|
|
PtrInfo* pinfo = iter.Get();
|
|
|
|
MOZ_ASSERT(pinfo->mParticipant,
|
|
|
|
"Unlink shouldn't see objects removed from graph.");
|
|
|
|
pinfo->mParticipant->Unlink(pinfo->mPointer);
|
2012-11-28 04:56:06 +04:00
|
|
|
#ifdef DEBUG
|
2014-12-09 01:45:13 +03:00
|
|
|
if (mJSRuntime) {
|
|
|
|
mJSRuntime->AssertNoObjectsToTrace(pinfo->mPointer);
|
2014-11-12 03:02:34 +03:00
|
|
|
}
|
2014-12-09 01:45:13 +03:00
|
|
|
#endif
|
2014-05-05 21:30:39 +04:00
|
|
|
}
|
|
|
|
timeLog.Checkpoint("CollectWhite::Unlink");
|
2007-01-05 01:31:26 +03:00
|
|
|
|
2014-12-09 01:45:13 +03:00
|
|
|
for (auto iter = whiteNodes.Iter(); !iter.Done(); iter.Next()) {
|
|
|
|
PtrInfo* pinfo = iter.Get();
|
|
|
|
MOZ_ASSERT(pinfo->mParticipant,
|
|
|
|
"Unroot shouldn't see objects removed from graph.");
|
|
|
|
pinfo->mParticipant->Unroot(pinfo->mPointer);
|
2014-05-05 21:30:39 +04:00
|
|
|
}
|
|
|
|
timeLog.Checkpoint("CollectWhite::Unroot");
|
2007-01-05 01:31:26 +03:00
|
|
|
|
2014-05-05 21:30:39 +04:00
|
|
|
nsCycleCollector_dispatchDeferredDeletion(false);
|
2014-05-07 04:25:27 +04:00
|
|
|
timeLog.Checkpoint("CollectWhite::dispatchDeferredDeletion");
|
|
|
|
|
2014-05-05 21:30:39 +04:00
|
|
|
mIncrementalPhase = CleanupPhase;
|
2013-07-09 21:30:58 +04:00
|
|
|
|
2014-10-25 02:06:56 +04:00
|
|
|
return numWhiteNodes > 0 || numWhiteGCed > 0 || numWhiteJSZones > 0;
|
2007-01-05 01:31:26 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-01-15 04:26:47 +04:00
|
|
|
////////////////////////
|
2013-11-07 09:35:30 +04:00
|
|
|
// Memory reporting
|
2013-01-15 04:26:47 +04:00
|
|
|
////////////////////////
|
|
|
|
|
2013-12-08 09:39:47 +04:00
|
|
|
MOZ_DEFINE_MALLOC_SIZE_OF(CycleCollectorMallocSizeOf)
|
|
|
|
|
2013-11-07 09:35:30 +04:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsCycleCollector::CollectReports(nsIHandleReportCallback* aHandleReport,
|
2014-05-21 10:06:54 +04:00
|
|
|
nsISupports* aData, bool aAnonymize)
|
2013-11-07 09:35:30 +04:00
|
|
|
{
|
2014-05-05 21:30:39 +04:00
|
|
|
size_t objectSize, graphNodesSize, graphEdgesSize, weakMapsSize,
|
|
|
|
purpleBufferSize;
|
|
|
|
SizeOfIncludingThis(CycleCollectorMallocSizeOf,
|
|
|
|
&objectSize,
|
|
|
|
&graphNodesSize, &graphEdgesSize,
|
|
|
|
&weakMapsSize,
|
|
|
|
&purpleBufferSize);
|
2013-11-07 09:35:30 +04:00
|
|
|
|
|
|
|
#define REPORT(_path, _amount, _desc) \
|
|
|
|
do { \
|
|
|
|
size_t amount = _amount; /* evaluate |_amount| only once */ \
|
|
|
|
if (amount > 0) { \
|
|
|
|
nsresult rv; \
|
|
|
|
rv = aHandleReport->Callback(EmptyCString(), \
|
|
|
|
NS_LITERAL_CSTRING(_path), \
|
|
|
|
KIND_HEAP, UNITS_BYTES, _amount, \
|
|
|
|
NS_LITERAL_CSTRING(_desc), \
|
|
|
|
aData); \
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) \
|
|
|
|
return rv; \
|
|
|
|
} \
|
|
|
|
} while (0)
|
|
|
|
|
2014-05-05 21:30:39 +04:00
|
|
|
REPORT("explicit/cycle-collector/collector-object", objectSize,
|
|
|
|
"Memory used for the cycle collector object itself.");
|
2013-11-07 09:35:30 +04:00
|
|
|
|
2014-05-05 21:30:39 +04:00
|
|
|
REPORT("explicit/cycle-collector/graph-nodes", graphNodesSize,
|
|
|
|
"Memory used for the nodes of the cycle collector's graph. "
|
|
|
|
"This should be zero when the collector is idle.");
|
2013-11-07 09:35:30 +04:00
|
|
|
|
2014-05-05 21:30:39 +04:00
|
|
|
REPORT("explicit/cycle-collector/graph-edges", graphEdgesSize,
|
|
|
|
"Memory used for the edges of the cycle collector's graph. "
|
|
|
|
"This should be zero when the collector is idle.");
|
2013-11-07 09:35:30 +04:00
|
|
|
|
2014-05-05 21:30:39 +04:00
|
|
|
REPORT("explicit/cycle-collector/weak-maps", weakMapsSize,
|
|
|
|
"Memory used for the representation of weak maps in the "
|
|
|
|
"cycle collector's graph. "
|
|
|
|
"This should be zero when the collector is idle.");
|
2013-11-07 09:35:30 +04:00
|
|
|
|
2014-05-05 21:30:39 +04:00
|
|
|
REPORT("explicit/cycle-collector/purple-buffer", purpleBufferSize,
|
|
|
|
"Memory used for the cycle collector's purple buffer.");
|
2013-11-07 09:35:30 +04:00
|
|
|
|
|
|
|
#undef REPORT
|
|
|
|
|
2014-05-05 21:30:39 +04:00
|
|
|
return NS_OK;
|
2013-01-15 04:26:47 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2007-01-05 01:31:26 +03:00
|
|
|
////////////////////////////////////////////////////////////////////////
|
|
|
|
// Collector implementation
|
|
|
|
////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2013-08-13 21:45:32 +04:00
|
|
|
nsCycleCollector::nsCycleCollector() :
|
2014-05-05 21:30:39 +04:00
|
|
|
mActivelyCollecting(false),
|
|
|
|
mFreeingSnowWhite(false),
|
|
|
|
mScanInProgress(false),
|
|
|
|
mJSRuntime(nullptr),
|
|
|
|
mIncrementalPhase(IdlePhase),
|
|
|
|
mThread(NS_GetCurrentThread()),
|
|
|
|
mWhiteNodeCount(0),
|
|
|
|
mBeforeUnlinkCB(nullptr),
|
|
|
|
mForgetSkippableCB(nullptr),
|
|
|
|
mUnmergedNeeded(0),
|
2015-01-18 21:59:21 +03:00
|
|
|
mMergedInARow(0)
|
2007-01-05 01:31:26 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCycleCollector::~nsCycleCollector()
|
|
|
|
{
|
2014-05-05 21:30:39 +04:00
|
|
|
UnregisterWeakMemoryReporter(this);
|
2007-01-05 01:31:26 +03:00
|
|
|
}
|
|
|
|
|
2013-01-15 04:26:47 +04:00
|
|
|
void
|
2014-05-13 21:41:38 +04:00
|
|
|
nsCycleCollector::RegisterJSRuntime(CycleCollectedJSRuntime* aJSRuntime)
|
2007-01-05 01:31:26 +03:00
|
|
|
{
|
2014-05-13 21:41:38 +04:00
|
|
|
if (mJSRuntime) {
|
2014-05-05 21:30:39 +04:00
|
|
|
Fault("multiple registrations of cycle collector JS runtime", aJSRuntime);
|
2014-05-13 21:41:38 +04:00
|
|
|
}
|
2007-01-05 01:31:26 +03:00
|
|
|
|
2014-05-05 21:30:39 +04:00
|
|
|
mJSRuntime = aJSRuntime;
|
2013-01-15 04:26:47 +04:00
|
|
|
|
2014-05-05 21:30:39 +04:00
|
|
|
// We can't register as a reporter in nsCycleCollector() because that runs
|
|
|
|
// before the memory reporter manager is initialized. So we do it here
|
|
|
|
// instead.
|
|
|
|
static bool registered = false;
|
|
|
|
if (!registered) {
|
|
|
|
RegisterWeakMemoryReporter(this);
|
|
|
|
registered = true;
|
|
|
|
}
|
2007-01-05 01:31:26 +03:00
|
|
|
}
|
|
|
|
|
2013-01-15 04:26:47 +04:00
|
|
|
void
|
2012-05-03 23:28:11 +04:00
|
|
|
nsCycleCollector::ForgetJSRuntime()
|
2007-01-05 01:31:26 +03:00
|
|
|
{
|
2014-05-13 21:41:38 +04:00
|
|
|
if (!mJSRuntime) {
|
2014-05-05 21:30:39 +04:00
|
|
|
Fault("forgetting non-registered cycle collector JS runtime");
|
2014-05-13 21:41:38 +04:00
|
|
|
}
|
2012-05-03 23:28:10 +04:00
|
|
|
|
2014-05-05 21:30:39 +04:00
|
|
|
mJSRuntime = nullptr;
|
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
|
2014-05-13 21:41:38 +04:00
|
|
|
HasParticipant(void* aPtr, nsCycleCollectionParticipant* aParti)
|
2007-05-24 18:10:02 +04:00
|
|
|
{
|
2014-05-05 21:30:39 +04:00
|
|
|
if (aParti) {
|
|
|
|
return true;
|
|
|
|
}
|
2007-05-24 18:10:02 +04:00
|
|
|
|
2014-05-13 21:41:38 +04:00
|
|
|
nsXPCOMCycleCollectionParticipant* xcp;
|
2014-05-05 21:30:39 +04:00
|
|
|
ToParticipant(static_cast<nsISupports*>(aPtr), &xcp);
|
|
|
|
return xcp != nullptr;
|
2007-05-24 18:10:02 +04:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2013-10-17 18:05:13 +04:00
|
|
|
MOZ_ALWAYS_INLINE void
|
2014-05-13 21:41:38 +04:00
|
|
|
nsCycleCollector::Suspect(void* aPtr, nsCycleCollectionParticipant* aParti,
|
|
|
|
nsCycleCollectingAutoRefCnt* aRefCnt)
|
2009-05-07 00:46:04 +04:00
|
|
|
{
|
2014-05-05 21:30:39 +04:00
|
|
|
CheckThreadSafety();
|
2009-10-28 20:28:57 +03:00
|
|
|
|
2014-05-05 21:30:39 +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.
|
2009-05-07 00:46:04 +04:00
|
|
|
|
2014-05-05 21:30:39 +04:00
|
|
|
if (MOZ_UNLIKELY(mScanInProgress)) {
|
|
|
|
return;
|
|
|
|
}
|
2013-11-27 02:30:46 +04:00
|
|
|
|
2014-05-05 21:30:39 +04:00
|
|
|
MOZ_ASSERT(aPtr, "Don't suspect null pointers");
|
2009-05-07 00:46:04 +04:00
|
|
|
|
2014-05-05 21:30:39 +04:00
|
|
|
MOZ_ASSERT(HasParticipant(aPtr, aParti),
|
|
|
|
"Suspected nsISupports pointer must QI to nsXPCOMCycleCollectionParticipant");
|
2009-05-07 00:46:04 +04:00
|
|
|
|
2014-05-05 21:30:39 +04:00
|
|
|
mPurpleBuf.Put(aPtr, aParti, aRefCnt);
|
2009-05-07 00:46:04 +04:00
|
|
|
}
|
|
|
|
|
2013-03-26 01:26:00 +04:00
|
|
|
void
|
|
|
|
nsCycleCollector::CheckThreadSafety()
|
|
|
|
{
|
|
|
|
#ifdef DEBUG
|
2014-05-05 21:30:39 +04:00
|
|
|
nsIThread* currentThread = NS_GetCurrentThread();
|
|
|
|
// XXXkhuey we can be called so late in shutdown that NS_GetCurrentThread
|
|
|
|
// returns null (after the thread manager has shut down)
|
|
|
|
MOZ_ASSERT(mThread == currentThread || !currentThread);
|
2013-03-26 01:26:00 +04:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
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),
|
2013-05-04 19:39:44 +04:00
|
|
|
// and also when UnmarkGray has run out of stack. We also force GCs on shut
|
2011-06-16 02:53:00 +04:00
|
|
|
// down to collect cycles involving both DOM and JS.
|
|
|
|
void
|
2013-01-08 22:36:51 +04:00
|
|
|
nsCycleCollector::FixGrayBits(bool aForceGC)
|
2007-01-05 01:31:26 +03:00
|
|
|
{
|
2014-05-05 21:30:39 +04:00
|
|
|
CheckThreadSafety();
|
2011-06-16 02:53:00 +04:00
|
|
|
|
2014-05-13 21:41:38 +04:00
|
|
|
if (!mJSRuntime) {
|
2014-05-05 21:30:39 +04:00
|
|
|
return;
|
2014-05-13 21:41:38 +04:00
|
|
|
}
|
2011-06-14 00:24:23 +04:00
|
|
|
|
2014-05-05 21:30:39 +04:00
|
|
|
if (!aForceGC) {
|
|
|
|
mJSRuntime->FixWeakMappingGrayBits();
|
2013-01-08 22:36:51 +04:00
|
|
|
|
2014-05-07 04:25:26 +04:00
|
|
|
bool needGC = !mJSRuntime->AreGCGrayBitsValid();
|
2014-05-05 21:30:39 +04:00
|
|
|
// Only do a telemetry ping for non-shutdown CCs.
|
|
|
|
CC_TELEMETRY(_NEED_GC, needGC);
|
2014-05-13 21:41:38 +04:00
|
|
|
if (!needGC) {
|
2014-05-05 21:30:39 +04:00
|
|
|
return;
|
2014-05-13 21:41:38 +04:00
|
|
|
}
|
2014-05-05 21:30:39 +04:00
|
|
|
mResults.mForcedGC = true;
|
|
|
|
}
|
2011-06-16 02:53:00 +04:00
|
|
|
|
2014-05-05 21:30:39 +04:00
|
|
|
TimeLog timeLog;
|
2014-08-25 23:17:15 +04:00
|
|
|
mJSRuntime->GarbageCollect(aForceGC ? JS::gcreason::SHUTDOWN_CC :
|
|
|
|
JS::gcreason::CC_FORCED);
|
2014-05-05 21:30:39 +04:00
|
|
|
timeLog.Checkpoint("GC()");
|
2011-06-16 02:53:00 +04:00
|
|
|
}
|
|
|
|
|
2010-11-12 01:52:30 +03:00
|
|
|
void
|
|
|
|
nsCycleCollector::CleanupAfterCollection()
|
|
|
|
{
|
2014-05-07 04:25:27 +04:00
|
|
|
TimeLog timeLog;
|
2014-05-05 21:30:39 +04:00
|
|
|
MOZ_ASSERT(mIncrementalPhase == CleanupPhase);
|
|
|
|
mGraph.Clear();
|
2014-05-07 04:25:27 +04:00
|
|
|
timeLog.Checkpoint("CleanupAfterCollection::mGraph.Clear()");
|
2007-03-16 15:52:47 +03:00
|
|
|
|
2014-05-13 21:41:38 +04:00
|
|
|
uint32_t interval =
|
|
|
|
(uint32_t)((TimeStamp::Now() - mCollectionStart).ToMilliseconds());
|
2007-03-20 02:21:31 +03:00
|
|
|
#ifdef COLLECT_TIME_DEBUG
|
2014-05-13 21:41:38 +04:00
|
|
|
printf("cc: total cycle collector time was %ums in %u slices\n", interval,
|
|
|
|
mResults.mNumSlices);
|
2014-05-05 21:30:39 +04:00
|
|
|
printf("cc: visited %u ref counted and %u GCed objects, freed %d ref counted and %d GCed objects",
|
|
|
|
mResults.mVisitedRefCounted, mResults.mVisitedGCed,
|
|
|
|
mResults.mFreedRefCounted, mResults.mFreedGCed);
|
|
|
|
uint32_t numVisited = mResults.mVisitedRefCounted + mResults.mVisitedGCed;
|
|
|
|
if (numVisited > 1000) {
|
|
|
|
uint32_t numFreed = mResults.mFreedRefCounted + mResults.mFreedGCed;
|
|
|
|
printf(" (%d%%)", 100 * numFreed / numVisited);
|
|
|
|
}
|
|
|
|
printf(".\ncc: \n");
|
2007-03-20 02:21:31 +03:00
|
|
|
#endif
|
2014-05-07 04:25:27 +04:00
|
|
|
|
2014-05-05 21:30:39 +04:00
|
|
|
CC_TELEMETRY( , interval);
|
|
|
|
CC_TELEMETRY(_VISITED_REF_COUNTED, mResults.mVisitedRefCounted);
|
|
|
|
CC_TELEMETRY(_VISITED_GCED, mResults.mVisitedGCed);
|
|
|
|
CC_TELEMETRY(_COLLECTED, mWhiteNodeCount);
|
2014-05-07 04:25:27 +04:00
|
|
|
timeLog.Checkpoint("CleanupAfterCollection::telemetry");
|
2013-11-21 02:35:16 +04:00
|
|
|
|
2014-05-05 21:30:39 +04:00
|
|
|
if (mJSRuntime) {
|
|
|
|
mJSRuntime->EndCycleCollectionCallback(mResults);
|
2014-05-07 04:25:27 +04:00
|
|
|
timeLog.Checkpoint("CleanupAfterCollection::EndCycleCollectionCallback()");
|
2014-05-05 21:30:39 +04:00
|
|
|
}
|
|
|
|
mIncrementalPhase = IdlePhase;
|
2010-11-12 01:52:30 +03:00
|
|
|
}
|
|
|
|
|
2012-02-24 08:16:37 +04:00
|
|
|
void
|
2013-09-11 03:33:40 +04:00
|
|
|
nsCycleCollector::ShutdownCollect()
|
2010-11-12 01:52:30 +03:00
|
|
|
{
|
2014-05-05 21:30:39 +04:00
|
|
|
SliceBudget unlimitedBudget;
|
|
|
|
uint32_t i;
|
|
|
|
for (i = 0; i < DEFAULT_SHUTDOWN_COLLECTIONS; ++i) {
|
|
|
|
if (!Collect(ShutdownCC, unlimitedBudget, nullptr)) {
|
|
|
|
break;
|
2010-11-12 01:52:30 +03:00
|
|
|
}
|
2014-05-05 21:30:39 +04:00
|
|
|
}
|
|
|
|
NS_WARN_IF_FALSE(i < NORMAL_SHUTDOWN_COLLECTIONS, "Extra shutdown CC");
|
2007-11-02 01:51:57 +03:00
|
|
|
}
|
|
|
|
|
2013-12-03 22:47:47 +04:00
|
|
|
static void
|
2014-05-13 21:41:38 +04:00
|
|
|
PrintPhase(const char* aPhase)
|
2013-12-03 22:47:47 +04:00
|
|
|
{
|
|
|
|
#ifdef DEBUG_PHASES
|
2014-05-05 21:30:39 +04:00
|
|
|
printf("cc: begin %s on %s\n", aPhase,
|
|
|
|
NS_IsMainThread() ? "mainthread" : "worker");
|
2013-12-03 22:47:47 +04:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2013-09-07 03:15:10 +04:00
|
|
|
bool
|
2013-04-30 03:41:41 +04:00
|
|
|
nsCycleCollector::Collect(ccType aCCType,
|
2014-05-13 21:41:38 +04:00
|
|
|
SliceBudget& aBudget,
|
2014-11-27 14:47:51 +03:00
|
|
|
nsICycleCollectorListener* aManualListener,
|
|
|
|
bool aPreferShorterSlices)
|
2013-03-26 01:26:00 +04:00
|
|
|
{
|
2014-05-05 21:30:39 +04:00
|
|
|
CheckThreadSafety();
|
2013-08-13 21:45:32 +04:00
|
|
|
|
2014-05-05 21:30:39 +04:00
|
|
|
// This can legitimately happen in a few cases. See bug 383651.
|
|
|
|
if (mActivelyCollecting || mFreeingSnowWhite) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
mActivelyCollecting = true;
|
|
|
|
|
|
|
|
bool startedIdle = (mIncrementalPhase == IdlePhase);
|
|
|
|
bool collectedAny = false;
|
|
|
|
|
|
|
|
// If the CC started idle, it will call BeginCollection, which
|
|
|
|
// will do FreeSnowWhite, so it doesn't need to be done here.
|
|
|
|
if (!startedIdle) {
|
2014-05-07 04:25:27 +04:00
|
|
|
TimeLog timeLog;
|
2014-05-05 21:30:39 +04:00
|
|
|
FreeSnowWhite(true);
|
2014-05-07 04:25:27 +04:00
|
|
|
timeLog.Checkpoint("Collect::FreeSnowWhite");
|
2014-05-05 21:30:39 +04:00
|
|
|
}
|
|
|
|
|
2014-05-07 04:25:26 +04:00
|
|
|
++mResults.mNumSlices;
|
|
|
|
|
2014-11-27 14:47:51 +03:00
|
|
|
bool continueSlice = aBudget.isUnlimited() || !aPreferShorterSlices;
|
2014-05-05 21:30:39 +04:00
|
|
|
do {
|
|
|
|
switch (mIncrementalPhase) {
|
|
|
|
case IdlePhase:
|
|
|
|
PrintPhase("BeginCollection");
|
|
|
|
BeginCollection(aCCType, aManualListener);
|
|
|
|
break;
|
|
|
|
case GraphBuildingPhase:
|
|
|
|
PrintPhase("MarkRoots");
|
|
|
|
MarkRoots(aBudget);
|
2014-05-14 20:45:50 +04:00
|
|
|
|
|
|
|
// Only continue this slice if we're running synchronously or the
|
|
|
|
// next phase will probably be short, to reduce the max pause for this
|
|
|
|
// collection.
|
|
|
|
// (There's no need to check if we've finished graph building, because
|
|
|
|
// if we haven't, we've already exceeded our budget, and will finish
|
|
|
|
// this slice anyways.)
|
2014-11-27 14:47:51 +03:00
|
|
|
continueSlice = aBudget.isUnlimited() ||
|
|
|
|
(mResults.mNumSlices < 3 && !aPreferShorterSlices);
|
2014-05-05 21:30:39 +04:00
|
|
|
break;
|
|
|
|
case ScanAndCollectWhitePhase:
|
|
|
|
// We do ScanRoots and CollectWhite in a single slice to ensure
|
|
|
|
// that we won't unlink a live object if a weak reference is
|
|
|
|
// promoted to a strong reference after ScanRoots has finished.
|
|
|
|
// See bug 926533.
|
|
|
|
PrintPhase("ScanRoots");
|
|
|
|
ScanRoots(startedIdle);
|
|
|
|
PrintPhase("CollectWhite");
|
|
|
|
collectedAny = CollectWhite();
|
|
|
|
break;
|
|
|
|
case CleanupPhase:
|
|
|
|
PrintPhase("CleanupAfterCollection");
|
|
|
|
CleanupAfterCollection();
|
2014-05-14 20:45:50 +04:00
|
|
|
continueSlice = false;
|
2014-05-05 21:30:39 +04:00
|
|
|
break;
|
|
|
|
}
|
2014-05-14 20:45:50 +04:00
|
|
|
if (continueSlice) {
|
2014-10-22 16:13:00 +04:00
|
|
|
// Force SliceBudget::isOverBudget to check the time.
|
|
|
|
aBudget.step(SliceBudget::CounterReset);
|
|
|
|
continueSlice = !aBudget.isOverBudget();
|
2014-05-14 20:45:50 +04:00
|
|
|
}
|
|
|
|
} while (continueSlice);
|
2014-05-05 21:30:39 +04:00
|
|
|
|
|
|
|
// Clear mActivelyCollecting here to ensure that a recursive call to
|
|
|
|
// Collect() does something.
|
|
|
|
mActivelyCollecting = false;
|
|
|
|
|
|
|
|
if (aCCType != SliceCC && !startedIdle) {
|
|
|
|
// We were in the middle of an incremental CC (using its own listener).
|
|
|
|
// Somebody has forced a CC, so after having finished out the current CC,
|
|
|
|
// run the CC again using the new listener.
|
|
|
|
MOZ_ASSERT(mIncrementalPhase == IdlePhase);
|
|
|
|
if (Collect(aCCType, aBudget, aManualListener)) {
|
|
|
|
collectedAny = true;
|
2013-12-03 22:47:47 +04:00
|
|
|
}
|
2014-05-05 21:30:39 +04:00
|
|
|
}
|
2013-12-03 22:47:47 +04:00
|
|
|
|
2014-05-05 21:30:39 +04:00
|
|
|
MOZ_ASSERT_IF(aCCType != SliceCC, mIncrementalPhase == IdlePhase);
|
2013-12-03 22:47:46 +04:00
|
|
|
|
2014-05-05 21:30:39 +04:00
|
|
|
return collectedAny;
|
2013-03-26 01:26:00 +04:00
|
|
|
}
|
|
|
|
|
2013-12-06 22:17:20 +04:00
|
|
|
// Any JS objects we have in the graph could die when we GC, but we
|
|
|
|
// don't want to abandon the current CC, because the graph contains
|
|
|
|
// information about purple roots. So we synchronously finish off
|
|
|
|
// the current CC.
|
2013-12-22 18:58:19 +04:00
|
|
|
void
|
|
|
|
nsCycleCollector::PrepareForGarbageCollection()
|
2013-12-06 22:17:20 +04:00
|
|
|
{
|
2014-05-05 21:30:39 +04:00
|
|
|
if (mIncrementalPhase == IdlePhase) {
|
|
|
|
MOZ_ASSERT(mGraph.IsEmpty(), "Non-empty graph when idle");
|
|
|
|
MOZ_ASSERT(!mBuilder, "Non-null builder when idle");
|
|
|
|
if (mJSPurpleBuffer) {
|
|
|
|
mJSPurpleBuffer->Destroy();
|
2013-12-06 22:17:20 +04:00
|
|
|
}
|
2014-05-05 21:30:39 +04:00
|
|
|
return;
|
|
|
|
}
|
2013-12-06 22:17:20 +04:00
|
|
|
|
2014-05-07 04:25:26 +04:00
|
|
|
FinishAnyCurrentCollection();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsCycleCollector::FinishAnyCurrentCollection()
|
|
|
|
{
|
|
|
|
if (mIncrementalPhase == IdlePhase) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-05-05 21:30:39 +04:00
|
|
|
SliceBudget unlimitedBudget;
|
2014-05-07 04:25:26 +04:00
|
|
|
PrintPhase("FinishAnyCurrentCollection");
|
2014-05-05 21:30:39 +04:00
|
|
|
// Use SliceCC because we only want to finish the CC in progress.
|
|
|
|
Collect(SliceCC, unlimitedBudget, nullptr);
|
2014-10-25 02:06:55 +04:00
|
|
|
|
2015-01-22 01:35:54 +03:00
|
|
|
// It is only okay for Collect() to have failed to finish the
|
|
|
|
// current CC if we're reentering the CC at some point past
|
|
|
|
// graph building. We need to be past the point where the CC will
|
|
|
|
// look at JS objects so that it is safe to GC.
|
2014-10-25 02:06:55 +04:00
|
|
|
MOZ_ASSERT(mIncrementalPhase == IdlePhase ||
|
2015-01-22 01:35:54 +03:00
|
|
|
(mActivelyCollecting && mIncrementalPhase != GraphBuildingPhase),
|
|
|
|
"Reentered CC during graph building");
|
2013-12-06 22:17:20 +04:00
|
|
|
}
|
|
|
|
|
2013-04-30 03:41:41 +04:00
|
|
|
// Don't merge too many times in a row, and do at least a minimum
|
|
|
|
// number of unmerged CCs in a row.
|
|
|
|
static const uint32_t kMinConsecutiveUnmerged = 3;
|
|
|
|
static const uint32_t kMaxConsecutiveMerged = 3;
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool
|
2013-04-30 03:41:41 +04:00
|
|
|
nsCycleCollector::ShouldMergeZones(ccType aCCType)
|
|
|
|
{
|
2014-05-05 21:30:39 +04:00
|
|
|
if (!mJSRuntime) {
|
|
|
|
return false;
|
|
|
|
}
|
2013-04-30 03:41:41 +04:00
|
|
|
|
2014-05-05 21:30:39 +04:00
|
|
|
MOZ_ASSERT(mUnmergedNeeded <= kMinConsecutiveUnmerged);
|
|
|
|
MOZ_ASSERT(mMergedInARow <= kMaxConsecutiveMerged);
|
2013-04-30 03:41:41 +04:00
|
|
|
|
2014-05-05 21:30:39 +04:00
|
|
|
if (mMergedInARow == kMaxConsecutiveMerged) {
|
|
|
|
MOZ_ASSERT(mUnmergedNeeded == 0);
|
|
|
|
mUnmergedNeeded = kMinConsecutiveUnmerged;
|
|
|
|
}
|
2013-04-30 03:41:41 +04:00
|
|
|
|
2014-05-05 21:30:39 +04:00
|
|
|
if (mUnmergedNeeded > 0) {
|
|
|
|
mUnmergedNeeded--;
|
|
|
|
mMergedInARow = 0;
|
|
|
|
return false;
|
|
|
|
}
|
2013-04-30 03:41:41 +04:00
|
|
|
|
2014-05-05 21:30:39 +04:00
|
|
|
if (aCCType == SliceCC && mJSRuntime->UsefulToMergeZones()) {
|
|
|
|
mMergedInARow++;
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
mMergedInARow = 0;
|
|
|
|
return false;
|
|
|
|
}
|
2013-04-30 03:41:41 +04:00
|
|
|
}
|
|
|
|
|
2013-09-07 00:42:27 +04:00
|
|
|
void
|
2013-04-30 03:41:41 +04:00
|
|
|
nsCycleCollector::BeginCollection(ccType aCCType,
|
2014-05-13 21:41:38 +04:00
|
|
|
nsICycleCollectorListener* aManualListener)
|
2007-11-02 01:51:57 +03:00
|
|
|
{
|
2014-05-05 21:30:39 +04:00
|
|
|
TimeLog timeLog;
|
|
|
|
MOZ_ASSERT(mIncrementalPhase == IdlePhase);
|
2013-11-21 02:35:16 +04:00
|
|
|
|
2014-05-05 21:30:39 +04:00
|
|
|
mCollectionStart = TimeStamp::Now();
|
2013-11-21 02:35:16 +04:00
|
|
|
|
2014-05-05 21:30:39 +04:00
|
|
|
if (mJSRuntime) {
|
|
|
|
mJSRuntime->BeginCycleCollectionCallback();
|
|
|
|
timeLog.Checkpoint("BeginCycleCollectionCallback()");
|
|
|
|
}
|
2013-11-21 02:35:16 +04:00
|
|
|
|
2014-05-05 21:30:39 +04:00
|
|
|
bool isShutdown = (aCCType == ShutdownCC);
|
2013-09-11 03:33:41 +04:00
|
|
|
|
2014-05-05 21:30:39 +04:00
|
|
|
// Set up the listener for this CC.
|
|
|
|
MOZ_ASSERT_IF(isShutdown, !aManualListener);
|
|
|
|
MOZ_ASSERT(!mListener, "Forgot to clear a previous listener?");
|
|
|
|
mListener = aManualListener;
|
|
|
|
aManualListener = nullptr;
|
|
|
|
if (!mListener && mParams.LogThisCC(isShutdown)) {
|
|
|
|
nsRefPtr<nsCycleCollectorLogger> logger = new nsCycleCollectorLogger();
|
|
|
|
if (mParams.AllTracesThisCC(isShutdown)) {
|
|
|
|
logger->SetAllTraces();
|
2013-09-11 03:33:41 +04:00
|
|
|
}
|
2014-05-05 21:30:39 +04:00
|
|
|
mListener = logger.forget();
|
|
|
|
}
|
2013-09-11 03:33:41 +04:00
|
|
|
|
2014-05-05 21:30:39 +04:00
|
|
|
bool forceGC = isShutdown;
|
|
|
|
if (!forceGC && mListener) {
|
|
|
|
// On a WantAllTraces CC, force a synchronous global GC to prevent
|
|
|
|
// hijinks from ForgetSkippable and compartmental GCs.
|
|
|
|
mListener->GetWantAllTraces(&forceGC);
|
|
|
|
}
|
|
|
|
FixGrayBits(forceGC);
|
2013-09-11 03:33:41 +04:00
|
|
|
|
2014-05-05 21:30:39 +04:00
|
|
|
FreeSnowWhite(true);
|
2013-09-11 03:33:41 +04:00
|
|
|
|
2014-05-05 21:30:39 +04:00
|
|
|
if (mListener && NS_FAILED(mListener->Begin())) {
|
|
|
|
mListener = nullptr;
|
|
|
|
}
|
2012-02-12 20:02:01 +04:00
|
|
|
|
2014-05-05 21:30:39 +04:00
|
|
|
// Set up the data structures for building the graph.
|
|
|
|
mGraph.Init();
|
|
|
|
mResults.Init();
|
|
|
|
bool mergeZones = ShouldMergeZones(aCCType);
|
|
|
|
mResults.mMergedZones = mergeZones;
|
2013-04-30 03:41:41 +04:00
|
|
|
|
2014-05-05 21:30:39 +04:00
|
|
|
MOZ_ASSERT(!mBuilder, "Forgot to clear mBuilder");
|
2014-08-25 23:17:15 +04:00
|
|
|
mBuilder = new CCGraphBuilder(mGraph, mResults, mJSRuntime, mListener,
|
|
|
|
mergeZones);
|
2007-01-05 01:31:26 +03:00
|
|
|
|
2014-05-05 21:30:39 +04:00
|
|
|
if (mJSRuntime) {
|
|
|
|
mJSRuntime->TraverseRoots(*mBuilder);
|
|
|
|
timeLog.Checkpoint("mJSRuntime->TraverseRoots()");
|
|
|
|
}
|
2007-03-20 02:21:31 +03:00
|
|
|
|
2014-05-05 21:30:39 +04:00
|
|
|
AutoRestore<bool> ar(mScanInProgress);
|
|
|
|
MOZ_ASSERT(!mScanInProgress);
|
|
|
|
mScanInProgress = true;
|
|
|
|
mPurpleBuf.SelectPointers(*mBuilder);
|
|
|
|
timeLog.Checkpoint("SelectPointers()");
|
2007-03-20 02:21:31 +03:00
|
|
|
|
2014-12-20 21:35:23 +03:00
|
|
|
mBuilder->DoneAddingRoots();
|
2014-05-05 21:30:39 +04:00
|
|
|
mIncrementalPhase = GraphBuildingPhase;
|
2008-01-10 17:10:03 +03:00
|
|
|
}
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t
|
2008-02-25 20:47:25 +03:00
|
|
|
nsCycleCollector::SuspectedCount()
|
|
|
|
{
|
2014-05-05 21:30:39 +04:00
|
|
|
CheckThreadSafety();
|
|
|
|
return mPurpleBuf.Count();
|
2008-02-25 20:47:25 +03:00
|
|
|
}
|
|
|
|
|
2007-01-25 03:24:08 +03:00
|
|
|
void
|
|
|
|
nsCycleCollector::Shutdown()
|
|
|
|
{
|
2014-05-05 21:30:39 +04:00
|
|
|
CheckThreadSafety();
|
2013-09-07 00:41:42 +04:00
|
|
|
|
2014-05-05 21:30:39 +04:00
|
|
|
// Always delete snow white objects.
|
|
|
|
FreeSnowWhite(true);
|
2013-07-09 21:30:58 +04:00
|
|
|
|
2012-12-14 22:41:11 +04:00
|
|
|
#ifndef DEBUG
|
2014-05-05 21:30:39 +04:00
|
|
|
if (PR_GetEnv("MOZ_CC_RUN_DURING_SHUTDOWN"))
|
2012-12-14 22:41:11 +04:00
|
|
|
#endif
|
2014-05-05 21:30:39 +04:00
|
|
|
{
|
|
|
|
ShutdownCollect();
|
|
|
|
}
|
2007-01-25 03:24:08 +03:00
|
|
|
}
|
|
|
|
|
2013-12-06 22:17:20 +04:00
|
|
|
void
|
2014-05-13 21:41:38 +04:00
|
|
|
nsCycleCollector::RemoveObjectFromGraph(void* aObj)
|
2013-12-06 22:17:20 +04:00
|
|
|
{
|
2014-05-05 21:30:39 +04:00
|
|
|
if (mIncrementalPhase == IdlePhase) {
|
|
|
|
return;
|
|
|
|
}
|
2013-12-06 22:17:20 +04:00
|
|
|
|
2014-05-13 21:41:38 +04:00
|
|
|
if (PtrInfo* pinfo = mGraph.FindNode(aObj)) {
|
2014-05-05 21:30:39 +04:00
|
|
|
mGraph.RemoveNodeFromMap(aObj);
|
2013-12-06 22:17:20 +04:00
|
|
|
|
2014-05-05 21:30:39 +04:00
|
|
|
pinfo->mPointer = nullptr;
|
|
|
|
pinfo->mParticipant = nullptr;
|
|
|
|
}
|
2013-12-06 22:17:20 +04:00
|
|
|
}
|
|
|
|
|
2012-11-07 05:38:29 +04:00
|
|
|
void
|
2013-11-07 09:35:30 +04:00
|
|
|
nsCycleCollector::SizeOfIncludingThis(mozilla::MallocSizeOf aMallocSizeOf,
|
2014-05-13 21:41:38 +04:00
|
|
|
size_t* aObjectSize,
|
|
|
|
size_t* aGraphNodesSize,
|
|
|
|
size_t* aGraphEdgesSize,
|
|
|
|
size_t* aWeakMapsSize,
|
|
|
|
size_t* aPurpleBufferSize) const
|
2011-07-09 02:49:31 +04:00
|
|
|
{
|
2014-05-05 21:30:39 +04:00
|
|
|
*aObjectSize = aMallocSizeOf(this);
|
2012-11-07 05:38:29 +04:00
|
|
|
|
2014-05-05 21:30:39 +04:00
|
|
|
mGraph.SizeOfExcludingThis(aMallocSizeOf, aGraphNodesSize, aGraphEdgesSize,
|
|
|
|
aWeakMapsSize);
|
2012-11-07 05:38:29 +04:00
|
|
|
|
2014-05-05 21:30:39 +04:00
|
|
|
*aPurpleBufferSize = mPurpleBuf.SizeOfExcludingThis(aMallocSizeOf);
|
2012-11-07 05:38:29 +04:00
|
|
|
|
2014-05-05 21:30:39 +04:00
|
|
|
// These fields are deliberately not measured:
|
|
|
|
// - mJSRuntime: because it's non-owning and measured by JS reporters.
|
|
|
|
// - mParams: because it only contains scalars.
|
2011-07-09 02:49:31 +04:00
|
|
|
}
|
|
|
|
|
2014-01-15 00:23:59 +04:00
|
|
|
JSPurpleBuffer*
|
|
|
|
nsCycleCollector::GetJSPurpleBuffer()
|
|
|
|
{
|
|
|
|
if (!mJSPurpleBuffer) {
|
2014-08-01 01:43:45 +04:00
|
|
|
// The Release call here confuses the GC analysis.
|
|
|
|
JS::AutoSuppressGCAnalysis nogc;
|
2014-01-15 00:23:59 +04:00
|
|
|
// JSPurpleBuffer keeps itself alive, but we need to create it in such way
|
|
|
|
// that it ends up in the normal purple buffer. That happens when
|
|
|
|
// nsRefPtr goes out of the scope and calls Release.
|
|
|
|
nsRefPtr<JSPurpleBuffer> pb = new JSPurpleBuffer(mJSPurpleBuffer);
|
|
|
|
}
|
|
|
|
return mJSPurpleBuffer;
|
|
|
|
}
|
|
|
|
|
2007-01-05 01:31:26 +03:00
|
|
|
////////////////////////////////////////////////////////////////////////
|
|
|
|
// Module public API (exported in nsCycleCollector.h)
|
|
|
|
// Just functions that redirect into the singleton, once it's built.
|
|
|
|
////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2012-05-03 23:28:11 +04:00
|
|
|
void
|
2014-05-13 21:41:38 +04:00
|
|
|
nsCycleCollector_registerJSRuntime(CycleCollectedJSRuntime* aRt)
|
2007-01-05 01:31:26 +03:00
|
|
|
{
|
2014-05-13 21:41:38 +04:00
|
|
|
CollectorData* data = sCollectorData.get();
|
2013-03-26 01:26:00 +04:00
|
|
|
|
2014-05-05 21:30:39 +04:00
|
|
|
// We should have started the cycle collector by now.
|
|
|
|
MOZ_ASSERT(data);
|
|
|
|
MOZ_ASSERT(data->mCollector);
|
|
|
|
// But we shouldn't already have a runtime.
|
|
|
|
MOZ_ASSERT(!data->mRuntime);
|
2013-06-18 23:02:16 +04:00
|
|
|
|
2014-05-13 21:41:38 +04:00
|
|
|
data->mRuntime = aRt;
|
|
|
|
data->mCollector->RegisterJSRuntime(aRt);
|
2007-01-05 01:31:26 +03:00
|
|
|
}
|
|
|
|
|
2012-11-07 05:38:29 +04:00
|
|
|
void
|
2012-05-03 23:28:11 +04:00
|
|
|
nsCycleCollector_forgetJSRuntime()
|
2007-01-05 01:31:26 +03:00
|
|
|
{
|
2014-05-13 21:41:38 +04:00
|
|
|
CollectorData* data = sCollectorData.get();
|
2007-01-05 01:31:26 +03:00
|
|
|
|
2014-05-05 21:30:39 +04:00
|
|
|
// We should have started the cycle collector by now.
|
|
|
|
MOZ_ASSERT(data);
|
|
|
|
// And we shouldn't have already forgotten our runtime.
|
|
|
|
MOZ_ASSERT(data->mRuntime);
|
2009-05-07 00:46:04 +04:00
|
|
|
|
2014-05-05 21:30:39 +04:00
|
|
|
// But it may have shutdown already.
|
|
|
|
if (data->mCollector) {
|
|
|
|
data->mCollector->ForgetJSRuntime();
|
|
|
|
data->mRuntime = nullptr;
|
|
|
|
} else {
|
|
|
|
data->mRuntime = nullptr;
|
|
|
|
delete data;
|
|
|
|
sCollectorData.set(nullptr);
|
|
|
|
}
|
2009-05-07 00:46:04 +04:00
|
|
|
}
|
|
|
|
|
2013-09-09 07:28:50 +04:00
|
|
|
/* static */ CycleCollectedJSRuntime*
|
|
|
|
CycleCollectedJSRuntime::Get()
|
2013-08-30 13:47:19 +04:00
|
|
|
{
|
2014-05-05 21:30:39 +04:00
|
|
|
CollectorData* data = sCollectorData.get();
|
|
|
|
if (data) {
|
|
|
|
return data->mRuntime;
|
|
|
|
}
|
|
|
|
return nullptr;
|
2013-08-30 13:47:19 +04:00
|
|
|
}
|
|
|
|
|
2013-08-17 00:10:17 +04:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace cyclecollector {
|
|
|
|
|
2013-06-18 23:01:26 +04:00
|
|
|
void
|
2013-08-17 00:10:17 +04:00
|
|
|
HoldJSObjectsImpl(void* aHolder, nsScriptObjectTracer* aTracer)
|
2013-06-18 23:01:26 +04:00
|
|
|
{
|
2014-05-05 21:30:39 +04:00
|
|
|
CollectorData* data = sCollectorData.get();
|
2013-06-18 23:01:26 +04:00
|
|
|
|
2014-05-05 21:30:39 +04:00
|
|
|
// We should have started the cycle collector by now.
|
|
|
|
MOZ_ASSERT(data);
|
|
|
|
MOZ_ASSERT(data->mCollector);
|
|
|
|
// And we should have a runtime.
|
|
|
|
MOZ_ASSERT(data->mRuntime);
|
2013-06-18 23:01:26 +04:00
|
|
|
|
2014-05-05 21:30:39 +04:00
|
|
|
data->mRuntime->AddJSHolder(aHolder, aTracer);
|
2013-06-18 23:01:26 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2013-08-17 00:10:17 +04:00
|
|
|
HoldJSObjectsImpl(nsISupports* aHolder)
|
2013-06-18 23:01:26 +04:00
|
|
|
{
|
2014-05-05 21:30:39 +04:00
|
|
|
nsXPCOMCycleCollectionParticipant* participant;
|
|
|
|
CallQueryInterface(aHolder, &participant);
|
|
|
|
MOZ_ASSERT(participant, "Failed to QI to nsXPCOMCycleCollectionParticipant!");
|
|
|
|
MOZ_ASSERT(participant->CheckForRightISupports(aHolder),
|
|
|
|
"The result of QIing a JS holder should be the same as ToSupports");
|
2013-08-17 00:10:17 +04:00
|
|
|
|
2014-05-05 21:30:39 +04:00
|
|
|
HoldJSObjectsImpl(aHolder, participant);
|
2013-08-17 00:10:17 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
DropJSObjectsImpl(void* aHolder)
|
|
|
|
{
|
2014-05-05 21:30:39 +04:00
|
|
|
CollectorData* data = sCollectorData.get();
|
2013-06-18 23:01:26 +04:00
|
|
|
|
2014-05-05 21:30:39 +04:00
|
|
|
// We should have started the cycle collector by now, and not completely
|
|
|
|
// shut down.
|
|
|
|
MOZ_ASSERT(data);
|
|
|
|
// And we should have a runtime.
|
|
|
|
MOZ_ASSERT(data->mRuntime);
|
2013-06-18 23:01:26 +04:00
|
|
|
|
2014-05-05 21:30:39 +04:00
|
|
|
data->mRuntime->RemoveJSHolder(aHolder);
|
2013-06-18 23:01:26 +04:00
|
|
|
}
|
|
|
|
|
2013-08-17 00:10:17 +04:00
|
|
|
void
|
|
|
|
DropJSObjectsImpl(nsISupports* aHolder)
|
|
|
|
{
|
|
|
|
#ifdef DEBUG
|
2014-05-05 21:30:39 +04:00
|
|
|
nsXPCOMCycleCollectionParticipant* participant;
|
|
|
|
CallQueryInterface(aHolder, &participant);
|
|
|
|
MOZ_ASSERT(participant, "Failed to QI to nsXPCOMCycleCollectionParticipant!");
|
|
|
|
MOZ_ASSERT(participant->CheckForRightISupports(aHolder),
|
|
|
|
"The result of QIing a JS holder should be the same as ToSupports");
|
2013-08-17 00:10:17 +04:00
|
|
|
#endif
|
2014-05-05 21:30:39 +04:00
|
|
|
DropJSObjectsImpl(static_cast<void*>(aHolder));
|
2013-08-17 00:10:17 +04:00
|
|
|
}
|
|
|
|
|
2013-06-18 23:01:26 +04:00
|
|
|
#ifdef DEBUG
|
|
|
|
bool
|
2013-08-17 00:10:17 +04:00
|
|
|
IsJSHolder(void* aHolder)
|
2013-06-18 23:01:26 +04:00
|
|
|
{
|
2014-05-13 21:41:38 +04:00
|
|
|
CollectorData* data = sCollectorData.get();
|
2013-06-18 23:01:26 +04:00
|
|
|
|
2014-05-05 21:30:39 +04:00
|
|
|
// We should have started the cycle collector by now, and not completely
|
|
|
|
// shut down.
|
|
|
|
MOZ_ASSERT(data);
|
|
|
|
// And we should have a runtime.
|
|
|
|
MOZ_ASSERT(data->mRuntime);
|
2013-06-18 23:01:26 +04:00
|
|
|
|
2014-05-05 21:30:39 +04:00
|
|
|
return data->mRuntime->IsJSHolder(aHolder);
|
2013-06-18 23:01:26 +04:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2013-07-09 18:28:15 +04:00
|
|
|
void
|
2013-08-17 00:10:17 +04:00
|
|
|
DeferredFinalize(nsISupports* aSupports)
|
2013-07-09 18:28:15 +04:00
|
|
|
{
|
2014-05-13 21:41:38 +04:00
|
|
|
CollectorData* data = sCollectorData.get();
|
2013-07-09 18:28:15 +04:00
|
|
|
|
2014-05-05 21:30:39 +04:00
|
|
|
// We should have started the cycle collector by now, and not completely
|
|
|
|
// shut down.
|
|
|
|
MOZ_ASSERT(data);
|
|
|
|
// And we should have a runtime.
|
|
|
|
MOZ_ASSERT(data->mRuntime);
|
2013-07-09 18:28:15 +04:00
|
|
|
|
2014-05-05 21:30:39 +04:00
|
|
|
data->mRuntime->DeferredFinalize(aSupports);
|
2013-07-09 18:28:15 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2013-08-17 00:10:17 +04:00
|
|
|
DeferredFinalize(DeferredFinalizeAppendFunction aAppendFunc,
|
|
|
|
DeferredFinalizeFunction aFunc,
|
|
|
|
void* aThing)
|
2013-07-09 18:28:15 +04:00
|
|
|
{
|
2014-05-13 21:41:38 +04:00
|
|
|
CollectorData* data = sCollectorData.get();
|
2013-07-09 18:28:15 +04:00
|
|
|
|
2014-05-05 21:30:39 +04:00
|
|
|
// We should have started the cycle collector by now, and not completely
|
|
|
|
// shut down.
|
|
|
|
MOZ_ASSERT(data);
|
|
|
|
// And we should have a runtime.
|
|
|
|
MOZ_ASSERT(data->mRuntime);
|
2013-07-09 18:28:15 +04:00
|
|
|
|
2014-05-05 21:30:39 +04:00
|
|
|
data->mRuntime->DeferredFinalize(aAppendFunc, aFunc, aThing);
|
2013-07-09 18:28:15 +04:00
|
|
|
}
|
|
|
|
|
2013-08-17 00:10:17 +04:00
|
|
|
} // namespace cyclecollector
|
|
|
|
} // namespace mozilla
|
|
|
|
|
|
|
|
|
2013-10-17 18:05:13 +04:00
|
|
|
MOZ_NEVER_INLINE static void
|
2014-05-13 21:41:38 +04:00
|
|
|
SuspectAfterShutdown(void* aPtr, nsCycleCollectionParticipant* aCp,
|
2013-10-17 18:05:13 +04:00
|
|
|
nsCycleCollectingAutoRefCnt* aRefCnt,
|
|
|
|
bool* aShouldDelete)
|
|
|
|
{
|
2014-05-05 21:30:39 +04:00
|
|
|
if (aRefCnt->get() == 0) {
|
|
|
|
if (!aShouldDelete) {
|
|
|
|
// The CC is shut down, so we can't be in the middle of an ICC.
|
2014-05-13 21:41:38 +04:00
|
|
|
CanonicalizeParticipant(&aPtr, &aCp);
|
2014-05-05 21:30:39 +04:00
|
|
|
aRefCnt->stabilizeForDeletion();
|
2014-05-13 21:41:38 +04:00
|
|
|
aCp->DeleteCycleCollectable(aPtr);
|
2013-10-17 18:05:13 +04:00
|
|
|
} else {
|
2014-05-05 21:30:39 +04:00
|
|
|
*aShouldDelete = true;
|
2013-10-17 18:05:13 +04:00
|
|
|
}
|
2014-05-05 21:30:39 +04:00
|
|
|
} else {
|
|
|
|
// Make sure we'll get called again.
|
|
|
|
aRefCnt->RemoveFromPurpleBuffer();
|
|
|
|
}
|
2013-10-17 18:05:13 +04:00
|
|
|
}
|
|
|
|
|
2013-07-09 21:30:58 +04:00
|
|
|
void
|
2014-05-13 21:41:38 +04:00
|
|
|
NS_CycleCollectorSuspect3(void* aPtr, nsCycleCollectionParticipant* aCp,
|
|
|
|
nsCycleCollectingAutoRefCnt* aRefCnt,
|
2013-07-09 21:30:58 +04:00
|
|
|
bool* aShouldDelete)
|
2010-11-12 01:52:30 +03:00
|
|
|
{
|
2014-05-13 21:41:38 +04:00
|
|
|
CollectorData* data = sCollectorData.get();
|
2012-06-12 21:06:20 +04:00
|
|
|
|
2014-05-05 21:30:39 +04:00
|
|
|
// We should have started the cycle collector by now.
|
|
|
|
MOZ_ASSERT(data);
|
2010-11-12 01:52:30 +03:00
|
|
|
|
2014-05-05 21:30:39 +04:00
|
|
|
if (MOZ_LIKELY(data->mCollector)) {
|
2014-05-13 21:41:38 +04:00
|
|
|
data->mCollector->Suspect(aPtr, aCp, aRefCnt);
|
2014-05-05 21:30:39 +04:00
|
|
|
return;
|
|
|
|
}
|
2014-05-13 21:41:38 +04:00
|
|
|
SuspectAfterShutdown(aPtr, aCp, aRefCnt, aShouldDelete);
|
2013-03-26 01:26:00 +04:00
|
|
|
}
|
2010-11-12 01:52:30 +03:00
|
|
|
|
2013-03-26 01:26:00 +04:00
|
|
|
uint32_t
|
|
|
|
nsCycleCollector_suspectedCount()
|
|
|
|
{
|
2014-05-13 21:41:38 +04:00
|
|
|
CollectorData* data = sCollectorData.get();
|
2010-11-12 01:52:30 +03:00
|
|
|
|
2014-05-05 21:30:39 +04:00
|
|
|
// We should have started the cycle collector by now.
|
|
|
|
MOZ_ASSERT(data);
|
2013-06-18 23:02:16 +04:00
|
|
|
|
2014-05-05 21:30:39 +04:00
|
|
|
if (!data->mCollector) {
|
|
|
|
return 0;
|
|
|
|
}
|
2010-11-12 01:52:30 +03:00
|
|
|
|
2014-05-05 21:30:39 +04:00
|
|
|
return data->mCollector->SuspectedCount();
|
2013-03-26 01:26:00 +04:00
|
|
|
}
|
2010-11-12 01:52:30 +03:00
|
|
|
|
2013-03-26 01:26:00 +04:00
|
|
|
bool
|
|
|
|
nsCycleCollector_init()
|
2010-11-12 01:52:30 +03:00
|
|
|
{
|
2014-05-05 21:30:39 +04:00
|
|
|
MOZ_ASSERT(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
MOZ_ASSERT(!sCollectorData.initialized(), "Called twice!?");
|
2010-11-12 01:52:30 +03:00
|
|
|
|
2014-05-05 21:30:39 +04:00
|
|
|
return sCollectorData.init();
|
2013-03-26 01:26:00 +04:00
|
|
|
}
|
2010-11-12 01:52:30 +03:00
|
|
|
|
2013-08-13 21:45:32 +04:00
|
|
|
void
|
|
|
|
nsCycleCollector_startup()
|
2013-03-26 01:26:00 +04:00
|
|
|
{
|
2014-05-05 21:30:39 +04:00
|
|
|
MOZ_ASSERT(sCollectorData.initialized(),
|
|
|
|
"Forgot to call nsCycleCollector_init!");
|
|
|
|
if (sCollectorData.get()) {
|
|
|
|
MOZ_CRASH();
|
|
|
|
}
|
2010-11-12 01:52:30 +03:00
|
|
|
|
2014-05-05 21:30:39 +04:00
|
|
|
CollectorData* data = new CollectorData;
|
|
|
|
data->mCollector = new nsCycleCollector();
|
|
|
|
data->mRuntime = nullptr;
|
2010-11-12 01:52:30 +03:00
|
|
|
|
2014-05-05 21:30:39 +04:00
|
|
|
sCollectorData.set(data);
|
2010-11-12 01:52:30 +03:00
|
|
|
}
|
|
|
|
|
2012-01-14 20:58:05 +04:00
|
|
|
void
|
|
|
|
nsCycleCollector_setBeforeUnlinkCallback(CC_BeforeUnlinkCallback aCB)
|
|
|
|
{
|
2014-05-13 21:41:38 +04:00
|
|
|
CollectorData* data = sCollectorData.get();
|
2013-03-26 01:26:00 +04:00
|
|
|
|
2014-05-05 21:30:39 +04:00
|
|
|
// We should have started the cycle collector by now.
|
|
|
|
MOZ_ASSERT(data);
|
|
|
|
MOZ_ASSERT(data->mCollector);
|
2013-03-26 01:26:00 +04:00
|
|
|
|
2014-05-05 21:30:39 +04:00
|
|
|
data->mCollector->SetBeforeUnlinkCallback(aCB);
|
2012-01-14 20:58:05 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsCycleCollector_setForgetSkippableCallback(CC_ForgetSkippableCallback aCB)
|
|
|
|
{
|
2014-05-13 21:41:38 +04:00
|
|
|
CollectorData* data = sCollectorData.get();
|
2013-03-26 01:26:00 +04:00
|
|
|
|
2014-05-05 21:30:39 +04:00
|
|
|
// We should have started the cycle collector by now.
|
|
|
|
MOZ_ASSERT(data);
|
|
|
|
MOZ_ASSERT(data->mCollector);
|
2013-03-26 01:26:00 +04:00
|
|
|
|
2014-05-05 21:30:39 +04:00
|
|
|
data->mCollector->SetForgetSkippableCallback(aCB);
|
2012-01-14 20:58:05 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2013-07-27 14:48:45 +04:00
|
|
|
nsCycleCollector_forgetSkippable(bool aRemoveChildlessNodes,
|
|
|
|
bool aAsyncSnowWhiteFreeing)
|
2012-01-14 20:58:05 +04:00
|
|
|
{
|
2014-05-13 21:41:38 +04:00
|
|
|
CollectorData* data = sCollectorData.get();
|
2013-03-26 01:26:00 +04:00
|
|
|
|
2014-05-05 21:30:39 +04:00
|
|
|
// We should have started the cycle collector by now.
|
|
|
|
MOZ_ASSERT(data);
|
|
|
|
MOZ_ASSERT(data->mCollector);
|
2013-03-26 01:26:00 +04:00
|
|
|
|
2014-05-24 01:12:29 +04:00
|
|
|
PROFILER_LABEL("nsCycleCollector", "forgetSkippable",
|
2014-08-25 23:17:15 +04:00
|
|
|
js::ProfileEntry::Category::CC);
|
2014-05-24 01:12:29 +04:00
|
|
|
|
2014-05-05 21:30:39 +04:00
|
|
|
TimeLog timeLog;
|
|
|
|
data->mCollector->ForgetSkippable(aRemoveChildlessNodes,
|
|
|
|
aAsyncSnowWhiteFreeing);
|
|
|
|
timeLog.Checkpoint("ForgetSkippable()");
|
2012-01-14 20:58:05 +04:00
|
|
|
}
|
|
|
|
|
2013-07-09 21:30:58 +04:00
|
|
|
void
|
2013-08-04 03:55:39 +04:00
|
|
|
nsCycleCollector_dispatchDeferredDeletion(bool aContinuation)
|
2013-07-09 21:30:58 +04:00
|
|
|
{
|
2014-05-13 21:41:38 +04:00
|
|
|
CollectorData* data = sCollectorData.get();
|
2013-08-04 03:55:39 +04:00
|
|
|
|
2014-05-05 21:30:39 +04:00
|
|
|
if (!data || !data->mRuntime) {
|
|
|
|
return;
|
|
|
|
}
|
2013-08-04 03:55:39 +04:00
|
|
|
|
2014-05-05 21:30:39 +04:00
|
|
|
data->mRuntime->DispatchDeferredDeletion(aContinuation);
|
2013-08-04 03:55:39 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
nsCycleCollector_doDeferredDeletion()
|
|
|
|
{
|
2014-05-13 21:41:38 +04:00
|
|
|
CollectorData* data = sCollectorData.get();
|
2013-08-04 03:55:39 +04:00
|
|
|
|
2014-05-05 21:30:39 +04:00
|
|
|
// We should have started the cycle collector by now.
|
|
|
|
MOZ_ASSERT(data);
|
|
|
|
MOZ_ASSERT(data->mCollector);
|
|
|
|
MOZ_ASSERT(data->mRuntime);
|
2013-08-04 03:55:39 +04:00
|
|
|
|
2014-05-05 21:30:39 +04:00
|
|
|
return data->mCollector->FreeSnowWhite(false);
|
2013-07-09 21:30:58 +04:00
|
|
|
}
|
|
|
|
|
2014-05-13 21:13:00 +04:00
|
|
|
already_AddRefed<nsICycleCollectorLogSink>
|
|
|
|
nsCycleCollector_createLogSink()
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsICycleCollectorLogSink> sink = new nsCycleCollectorLogSinkToFile();
|
|
|
|
return sink.forget();
|
|
|
|
}
|
|
|
|
|
2012-02-24 08:16:37 +04:00
|
|
|
void
|
2014-05-13 21:41:38 +04:00
|
|
|
nsCycleCollector_collect(nsICycleCollectorListener* aManualListener)
|
2010-11-12 01:52:30 +03:00
|
|
|
{
|
2014-05-13 21:41:38 +04:00
|
|
|
CollectorData* data = sCollectorData.get();
|
2013-03-26 01:26:00 +04:00
|
|
|
|
2014-05-05 21:30:39 +04:00
|
|
|
// We should have started the cycle collector by now.
|
|
|
|
MOZ_ASSERT(data);
|
|
|
|
MOZ_ASSERT(data->mCollector);
|
2013-03-26 01:26:00 +04:00
|
|
|
|
2014-05-24 01:12:29 +04:00
|
|
|
PROFILER_LABEL("nsCycleCollector", "collect",
|
2014-08-25 23:17:15 +04:00
|
|
|
js::ProfileEntry::Category::CC);
|
2014-05-24 01:12:29 +04:00
|
|
|
|
2014-05-05 21:30:39 +04:00
|
|
|
SliceBudget unlimitedBudget;
|
|
|
|
data->mCollector->Collect(ManualCC, unlimitedBudget, aManualListener);
|
2013-11-21 02:35:17 +04:00
|
|
|
}
|
2011-01-14 13:06:09 +03:00
|
|
|
|
2013-11-21 02:35:17 +04:00
|
|
|
void
|
2014-11-27 14:47:51 +03:00
|
|
|
nsCycleCollector_collectSlice(SliceBudget& budget,
|
|
|
|
bool aPreferShorterSlices)
|
2013-11-21 02:35:17 +04:00
|
|
|
{
|
2014-05-13 21:41:38 +04:00
|
|
|
CollectorData* data = sCollectorData.get();
|
2013-11-21 02:35:17 +04:00
|
|
|
|
2014-05-05 21:30:39 +04:00
|
|
|
// We should have started the cycle collector by now.
|
|
|
|
MOZ_ASSERT(data);
|
|
|
|
MOZ_ASSERT(data->mCollector);
|
2011-01-14 13:06:09 +03:00
|
|
|
|
2014-05-24 01:12:29 +04:00
|
|
|
PROFILER_LABEL("nsCycleCollector", "collectSlice",
|
2014-08-25 23:17:15 +04:00
|
|
|
js::ProfileEntry::Category::CC);
|
2014-05-24 01:12:29 +04:00
|
|
|
|
2014-11-27 14:47:51 +03:00
|
|
|
data->mCollector->Collect(SliceCC, budget, nullptr, aPreferShorterSlices);
|
2010-11-12 01:52:30 +03:00
|
|
|
}
|
|
|
|
|
2013-12-06 22:17:20 +04:00
|
|
|
void
|
|
|
|
nsCycleCollector_prepareForGarbageCollection()
|
|
|
|
{
|
2014-05-13 21:41:38 +04:00
|
|
|
CollectorData* data = sCollectorData.get();
|
2013-12-06 22:17:20 +04:00
|
|
|
|
2014-05-05 21:30:39 +04:00
|
|
|
MOZ_ASSERT(data);
|
2013-12-06 22:17:20 +04:00
|
|
|
|
2014-05-05 21:30:39 +04:00
|
|
|
if (!data->mCollector) {
|
|
|
|
return;
|
|
|
|
}
|
2013-12-06 22:17:20 +04:00
|
|
|
|
2014-05-05 21:30:39 +04:00
|
|
|
data->mCollector->PrepareForGarbageCollection();
|
2013-12-06 22:17:20 +04:00
|
|
|
}
|
|
|
|
|
2014-05-07 04:25:26 +04:00
|
|
|
void
|
|
|
|
nsCycleCollector_finishAnyCurrentCollection()
|
|
|
|
{
|
2014-05-13 21:41:38 +04:00
|
|
|
CollectorData* data = sCollectorData.get();
|
2014-05-07 04:25:26 +04:00
|
|
|
|
2014-05-13 21:41:38 +04:00
|
|
|
MOZ_ASSERT(data);
|
2014-05-07 04:25:26 +04:00
|
|
|
|
2014-05-13 21:41:38 +04:00
|
|
|
if (!data->mCollector) {
|
|
|
|
return;
|
|
|
|
}
|
2014-05-07 04:25:26 +04:00
|
|
|
|
2014-05-13 21:41:38 +04:00
|
|
|
data->mCollector->FinishAnyCurrentCollection();
|
2014-05-07 04:25:26 +04:00
|
|
|
}
|
|
|
|
|
2010-11-12 01:52:30 +03:00
|
|
|
void
|
|
|
|
nsCycleCollector_shutdown()
|
|
|
|
{
|
2014-05-13 21:41:38 +04:00
|
|
|
CollectorData* data = sCollectorData.get();
|
2010-11-12 01:52:30 +03:00
|
|
|
|
2014-05-05 21:30:39 +04:00
|
|
|
if (data) {
|
|
|
|
MOZ_ASSERT(data->mCollector);
|
2014-05-24 01:12:29 +04:00
|
|
|
PROFILER_LABEL("nsCycleCollector", "shutdown",
|
2014-08-25 23:17:15 +04:00
|
|
|
js::ProfileEntry::Category::CC);
|
2014-05-24 01:12:29 +04:00
|
|
|
|
2014-05-05 21:30:39 +04:00
|
|
|
data->mCollector->Shutdown();
|
|
|
|
data->mCollector = nullptr;
|
|
|
|
if (!data->mRuntime) {
|
|
|
|
delete data;
|
|
|
|
sCollectorData.set(nullptr);
|
2010-11-12 01:52:30 +03:00
|
|
|
}
|
2014-05-05 21:30:39 +04:00
|
|
|
}
|
2010-11-12 01:52:30 +03:00
|
|
|
}
|