1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
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/
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
15 * The Original Code is the Netscape Portable Runtime (NSPR).
17 * The Initial Developer of the Original Code is
18 * Netscape Communications Corporation.
19 * Portions created by the Initial Developer are Copyright (C) 1998-2000
20 * the Initial Developer. All Rights Reserved.
24 * Alternatively, the contents of this file may be used under the terms of
25 * either the GNU General Public License Version 2 or later (the "GPL"), or
26 * 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.
36 * ***** END LICENSE BLOCK ***** */
46 #include "private/pprthred.h"
50 ** Generic GC implementation independent code for the NSPR GC
53 RootFinder *_pr_rootFinders;
55 CollectorType *_pr_collectorTypes;
57 /* GC State information */
60 GCBeginGCHook *_pr_beginGCHook;
61 void *_pr_beginGCHookArg;
62 GCBeginGCHook *_pr_endGCHook;
63 void *_pr_endGCHookArg;
65 GCBeginFinalizeHook *_pr_beginFinalizeHook;
66 void *_pr_beginFinalizeHookArg;
67 GCBeginFinalizeHook *_pr_endFinalizeHook;
68 void *_pr_endFinalizeHookArg;
72 GCLockHook *_pr_GCLockHook;
74 extern PRLogModuleInfo *_pr_msgc_lm;
76 /************************************************************************/
78 static PRStatus PR_CALLBACK
79 pr_ScanOneThread(PRThread* t, void** addr, PRUword count, void* closure)
81 _pr_gcData.processRootBlock(addr, count);
86 ** Scan all of the threads C stack's and registers, looking for "root"
87 ** pointers into the GC heap. These are the objects that the GC cannot
88 ** move and are considered "live" by the GC. Caller has stopped all of
89 ** the threads from running.
91 static void PR_CALLBACK ScanThreads(void *arg)
93 PR_ScanStackPointers(pr_ScanOneThread, arg);
96 /************************************************************************/
98 PR_IMPLEMENT(GCInfo *) PR_GetGCInfo(void)
104 PR_IMPLEMENT(PRInt32) PR_RegisterType(GCType *t)
106 CollectorType *ct, *ect;
110 ct = &_pr_collectorTypes[0];
111 ect = &_pr_collectorTypes[FREE_MEMORY_TYPEIX];
112 for (; ct < ect; ct++) {
113 if (ct->flags == 0) {
115 ct->flags = _GC_TYPE_BUSY;
116 if (0 != ct->gctype.finalize) {
117 ct->flags |= _GC_TYPE_FINAL;
119 if (0 != ct->gctype.getWeakLinkOffset) {
120 ct->flags |= _GC_TYPE_WEAK;
122 rv = ct - &_pr_collectorTypes[0];
130 PR_IMPLEMENT(PRStatus) PR_RegisterRootFinder(
131 GCRootFinder f, char *name, void *arg)
133 RootFinder *rf = PR_NEWZAP(RootFinder);
140 rf->next = _pr_rootFinders;
141 _pr_rootFinders = rf;
149 PR_IMPLEMENT(int) PR_RegisterGCLockHook(GCLockHookFunc* f, void *arg)
154 rf = (GCLockHook*) calloc(1, sizeof(GCLockHook));
160 /* first dummy node */
161 if (! _pr_GCLockHook) {
162 _pr_GCLockHook = (GCLockHook*) calloc(1, sizeof(GCLockHook));
163 _pr_GCLockHook->next = _pr_GCLockHook;
164 _pr_GCLockHook->prev = _pr_GCLockHook;
167 rf->next = _pr_GCLockHook;
168 rf->prev = _pr_GCLockHook->prev;
169 _pr_GCLockHook->prev->next = rf;
170 _pr_GCLockHook->prev = rf;
178 PR_IMPLEMENT(void) PR_SetGCLockHook(GCLockHook *hook, void *arg)
181 _pr_GCLockHook = hook;
182 _pr_GCLockHookArg2 = arg;
186 PR_IMPLEMENT(void) PR_GetGCLockHook(GCLockHook **hook, void **arg)
189 *hook = _pr_GCLockHook;
190 *arg = _pr_GCLockHookArg2;
196 PR_IMPLEMENT(void) PR_SetBeginGCHook(GCBeginGCHook *hook, void *arg)
199 _pr_beginGCHook = hook;
200 _pr_beginGCHookArg = arg;
204 PR_IMPLEMENT(void) PR_GetBeginGCHook(GCBeginGCHook **hook, void **arg)
207 *hook = _pr_beginGCHook;
208 *arg = _pr_beginGCHookArg;
212 PR_IMPLEMENT(void) PR_SetEndGCHook(GCEndGCHook *hook, void *arg)
215 _pr_endGCHook = hook;
216 _pr_endGCHookArg = arg;
220 PR_IMPLEMENT(void) PR_GetEndGCHook(GCEndGCHook **hook, void **arg)
223 *hook = _pr_endGCHook;
224 *arg = _pr_endGCHookArg;
228 PR_IMPLEMENT(void) PR_SetBeginFinalizeHook(GCBeginFinalizeHook *hook, void *arg)
231 _pr_beginFinalizeHook = hook;
232 _pr_beginFinalizeHookArg = arg;
236 PR_IMPLEMENT(void) PR_GetBeginFinalizeHook(GCBeginFinalizeHook **hook,
240 *hook = _pr_beginFinalizeHook;
241 *arg = _pr_beginFinalizeHookArg;
245 PR_IMPLEMENT(void) PR_SetEndFinalizeHook(GCEndFinalizeHook *hook, void *arg)
248 _pr_endFinalizeHook = hook;
249 _pr_endFinalizeHookArg = arg;
253 PR_IMPLEMENT(void) PR_GetEndFinalizeHook(GCEndFinalizeHook **hook, void **arg)
256 *hook = _pr_endFinalizeHook;
257 *arg = _pr_endFinalizeHookArg;
264 PR_IMPLEMENT(void) GCTrace(char *fmt, ...)
270 PR_vsnprintf(buf, sizeof(buf), fmt, ap);
272 PR_LOG(_pr_msgc_lm, PR_LOG_ALWAYS, ("%s", buf));
276 void _PR_InitGC(PRWord flags)
278 static char firstTime = 1;
280 if (!firstTime) return;
286 char *ev = PR_GetEnv("GCLOG");
291 _pr_gcData.flags = flags;
293 _pr_gcData.lock = PR_NewMonitor();
295 _pr_collectorTypes = (CollectorType*) PR_CALLOC(256 * sizeof(CollectorType));
297 PR_RegisterRootFinder(ScanThreads, "scan threads", 0);
298 PR_RegisterRootFinder(_PR_ScanFinalQueue, "scan final queue", 0);
301 extern void pr_FinalizeOnExit(void);
305 PR_PUBLIC_API(void) PR_PrintGCAllocStats(void);
310 PR_ShutdownGC(PRBool finalizeOnExit)
312 /* first finalize all the objects in the heap */
313 if (finalizeOnExit) {
319 PR_PrintGCAllocStats();
320 #endif /* GC_STATS */
323 /* then the chance for any future allocations */
325 /* finally delete the gc heap */
330 /******************************************************************************/