1 : /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 : /* ***** BEGIN LICENSE BLOCK *****
3 : * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 : *
5 : * The contents of this file are subject to the Mozilla Public License Version
6 : * 1.1 (the "License"); you may not use this file except in compliance with
7 : * the License. You may obtain a copy of the License at
8 : * http://www.mozilla.org/MPL/
9 : *
10 : * Software distributed under the License is distributed on an "AS IS" basis,
11 : * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 : * for the specific language governing rights and limitations under the
13 : * License.
14 : *
15 : * The Original Code is mozilla.org code.
16 : *
17 : * The Initial Developer of the Original Code is
18 : * The Mozilla Foundation.
19 : * Portions created by the Initial Developer are Copyright (C) 2006
20 : * the Initial Developer. All Rights Reserved.
21 : *
22 : * Contributor(s):
23 : *
24 : * Alternatively, the contents of this file may be used under the terms of
25 : * either of the GNU General Public License Version 2 or later (the "GPL"),
26 : * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 : * in which case the provisions of the GPL or the LGPL are applicable instead
28 : * of those above. If you wish to allow use of your version of this file only
29 : * under the terms of either the GPL or the LGPL, and not to allow others to
30 : * use your version of this file under the terms of the MPL, indicate your
31 : * decision by deleting the provisions above and replace them with the notice
32 : * and other provisions required by the GPL or the LGPL. If you do not delete
33 : * the provisions above, a recipient may use your version of this file under
34 : * the terms of any one of the MPL, the GPL or the LGPL.
35 : *
36 : * ***** END LICENSE BLOCK ***** */
37 :
38 : #ifndef nsCycleCollector_h__
39 : #define nsCycleCollector_h__
40 :
41 : // NOTE: If you use header files to define DEBUG_CC, you must do so here
42 : // *and* in nsCycleCollectionParticipant.h
43 : //#define DEBUG_CC
44 :
45 : class nsISupports;
46 : class nsICycleCollectorListener;
47 : class nsCycleCollectionParticipant;
48 : class nsCycleCollectionTraversalCallback;
49 :
50 : // An nsCycleCollectionLanguageRuntime is a per-language object that
51 : // implements language-specific aspects of the cycle collection task.
52 :
53 : struct nsCycleCollectionLanguageRuntime
54 2823 : {
55 : virtual nsresult BeginCycleCollection(nsCycleCollectionTraversalCallback &cb,
56 : bool explainLiveExpectedGarbage) = 0;
57 : virtual nsresult FinishTraverse() = 0;
58 : virtual nsresult FinishCycleCollection() = 0;
59 : virtual nsCycleCollectionParticipant *ToParticipant(void *p) = 0;
60 : #ifdef DEBUG_CC
61 : virtual void PrintAllReferencesTo(void *p) = 0;
62 : #endif
63 : };
64 :
65 : // Contains various stats about the cycle collection.
66 : class nsCycleCollectorResults
67 : {
68 : public:
69 48 : nsCycleCollectorResults() :
70 : mForcedGC(false), mVisitedRefCounted(0), mVisitedGCed(0),
71 48 : mFreedRefCounted(0), mFreedGCed(0) {}
72 : bool mForcedGC;
73 : PRUint32 mVisitedRefCounted;
74 : PRUint32 mVisitedGCed;
75 : PRUint32 mFreedRefCounted;
76 : PRUint32 mFreedGCed;
77 : };
78 :
79 : nsresult nsCycleCollector_startup();
80 :
81 : typedef void (*CC_BeforeUnlinkCallback)(void);
82 : void nsCycleCollector_setBeforeUnlinkCallback(CC_BeforeUnlinkCallback aCB);
83 :
84 : typedef void (*CC_ForgetSkippableCallback)(void);
85 : void nsCycleCollector_setForgetSkippableCallback(CC_ForgetSkippableCallback aCB);
86 :
87 : void nsCycleCollector_forgetSkippable(bool aRemoveChildlessNodes = false);
88 :
89 : #ifdef DEBUG_CC
90 : void nsCycleCollector_logPurpleRemoval(void* aObject);
91 : #endif
92 :
93 : void nsCycleCollector_collect(nsCycleCollectorResults *aResults,
94 : nsICycleCollectorListener *aListener);
95 : PRUint32 nsCycleCollector_suspectedCount();
96 : void nsCycleCollector_shutdownThreads();
97 : void nsCycleCollector_shutdown();
98 :
99 : // The JS runtime is special, it needs to call cycle collection during its GC.
100 : // If the JS runtime is registered nsCycleCollector_collect will call
101 : // nsCycleCollectionJSRuntime::Collect which will call
102 : // nsCycleCollector_doCollect, else nsCycleCollector_collect will call
103 : // nsCycleCollector_doCollect directly.
104 : struct nsCycleCollectionJSRuntime : public nsCycleCollectionLanguageRuntime
105 1404 : {
106 : /**
107 : * Called before/after transitioning to/from the main thread.
108 : *
109 : * NotifyLeaveMainThread may return 'false' to prevent the cycle collector
110 : * from leaving the main thread.
111 : */
112 : virtual bool NotifyLeaveMainThread() = 0;
113 : virtual void NotifyEnterCycleCollectionThread() = 0;
114 : virtual void NotifyLeaveCycleCollectionThread() = 0;
115 : virtual void NotifyEnterMainThread() = 0;
116 :
117 : /**
118 : * Should we force a JavaScript GC before a CC?
119 : */
120 : virtual bool NeedCollect() = 0;
121 :
122 : /**
123 : * Runs the JavaScript GC. |reason| is a gcreason::Reason from jsfriendapi.h.
124 : * |kind| is a nsGCType from nsIXPConnect.idl.
125 : */
126 : virtual void Collect(PRUint32 reason, PRUint32 kind) = 0;
127 : };
128 :
129 : #ifdef DEBUG
130 : void nsCycleCollector_DEBUG_shouldBeFreed(nsISupports *n);
131 : void nsCycleCollector_DEBUG_wasFreed(nsISupports *n);
132 : #endif
133 :
134 : // Helpers for interacting with language-identified scripts
135 :
136 : void nsCycleCollector_registerRuntime(PRUint32 langID, nsCycleCollectionLanguageRuntime *rt);
137 : nsCycleCollectionLanguageRuntime * nsCycleCollector_getRuntime(PRUint32 langID);
138 : void nsCycleCollector_forgetRuntime(PRUint32 langID);
139 :
140 : #define NS_CYCLE_COLLECTOR_LOGGER_CID \
141 : { 0x58be81b4, 0x39d2, 0x437c, \
142 : { 0x94, 0xea, 0xae, 0xde, 0x2c, 0x62, 0x08, 0xd3 } }
143 :
144 : extern nsresult
145 : nsCycleCollectorLoggerConstructor(nsISupports* outer,
146 : const nsIID& aIID,
147 : void* *aInstancePtr);
148 :
149 : #endif // nsCycleCollector_h__
|