Upload Tizen:Base source
[toolchains/nspr.git] / mozilla / nsprpub / lib / msgc / src / prgcapi.c
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
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 the Netscape Portable Runtime (NSPR).
16  *
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.
21  *
22  * Contributor(s):
23  *
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.
35  *
36  * ***** END LICENSE BLOCK ***** */
37
38 #include <stdarg.h>
39 #include <string.h>
40 #include <stdio.h>
41 #include "prenv.h"
42 #include "prmem.h"
43 #include "prmon.h"
44 #include "prlog.h"
45 #include "prthread.h"
46 #include "private/pprthred.h"
47 #include "gcint.h"
48
49 /*
50 ** Generic GC implementation independent code for the NSPR GC
51 */
52
53 RootFinder *_pr_rootFinders;
54
55 CollectorType *_pr_collectorTypes;
56
57 /* GC State information */
58 GCInfo _pr_gcData;
59
60 GCBeginGCHook *_pr_beginGCHook;
61 void *_pr_beginGCHookArg;
62 GCBeginGCHook *_pr_endGCHook;
63 void *_pr_endGCHookArg;
64
65 GCBeginFinalizeHook *_pr_beginFinalizeHook;
66 void *_pr_beginFinalizeHookArg;
67 GCBeginFinalizeHook *_pr_endFinalizeHook;
68 void *_pr_endFinalizeHookArg;
69
70 FILE *_pr_dump_file;
71 int _pr_do_a_dump;
72 GCLockHook *_pr_GCLockHook;
73
74 extern PRLogModuleInfo *_pr_msgc_lm;
75
76 /************************************************************************/
77
78 static PRStatus PR_CALLBACK
79 pr_ScanOneThread(PRThread* t, void** addr, PRUword count, void* closure)
80 {
81     _pr_gcData.processRootBlock(addr, count);
82     return PR_SUCCESS;
83 }
84
85 /*
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.
90 */
91 static void PR_CALLBACK ScanThreads(void *arg)
92 {
93     PR_ScanStackPointers(pr_ScanOneThread, arg);
94 }
95
96 /************************************************************************/
97
98 PR_IMPLEMENT(GCInfo *) PR_GetGCInfo(void)
99 {
100     return &_pr_gcData;
101 }
102
103
104 PR_IMPLEMENT(PRInt32) PR_RegisterType(GCType *t)
105 {
106     CollectorType *ct, *ect;
107     int rv = -1;
108
109     LOCK_GC();
110     ct = &_pr_collectorTypes[0];
111     ect = &_pr_collectorTypes[FREE_MEMORY_TYPEIX];
112     for (; ct < ect; ct++) {
113         if (ct->flags == 0) {
114             ct->gctype = *t;
115             ct->flags = _GC_TYPE_BUSY;
116             if (0 != ct->gctype.finalize) {
117                 ct->flags |= _GC_TYPE_FINAL;
118             }
119             if (0 != ct->gctype.getWeakLinkOffset) {
120                 ct->flags |= _GC_TYPE_WEAK;
121             }
122             rv = ct - &_pr_collectorTypes[0];
123             break;
124         }
125     }
126     UNLOCK_GC();
127     return rv;
128 }
129
130 PR_IMPLEMENT(PRStatus) PR_RegisterRootFinder(
131     GCRootFinder f, char *name, void *arg)
132 {
133     RootFinder *rf = PR_NEWZAP(RootFinder);
134     if (rf) {
135             rf->func = f;
136             rf->name = name;
137             rf->arg = arg;
138
139             LOCK_GC();
140             rf->next = _pr_rootFinders;
141             _pr_rootFinders = rf;
142             UNLOCK_GC();
143             return PR_SUCCESS;
144     }
145     return PR_FAILURE;
146 }
147
148
149 PR_IMPLEMENT(int) PR_RegisterGCLockHook(GCLockHookFunc* f, void *arg)
150 {
151
152     GCLockHook *rf = 0;
153
154     rf = (GCLockHook*) calloc(1, sizeof(GCLockHook));
155     if (rf) {
156         rf->func = f;
157         rf->arg = arg;
158
159         LOCK_GC();
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;
165         }
166
167         rf->next = _pr_GCLockHook;
168         rf->prev = _pr_GCLockHook->prev;
169         _pr_GCLockHook->prev->next = rf;
170         _pr_GCLockHook->prev = rf;
171         UNLOCK_GC();
172         return 0;
173     }
174     return -1;
175 }
176
177 /*
178 PR_IMPLEMENT(void) PR_SetGCLockHook(GCLockHook *hook, void *arg)
179 {
180     LOCK_GC();
181     _pr_GCLockHook = hook;
182     _pr_GCLockHookArg2 = arg;
183     UNLOCK_GC();
184 }
185
186 PR_IMPLEMENT(void) PR_GetGCLockHook(GCLockHook **hook, void **arg)
187 {
188     LOCK_GC();
189     *hook = _pr_GCLockHook;
190     *arg = _pr_GCLockHookArg2;
191     UNLOCK_GC();
192 }
193 */
194
195   
196 PR_IMPLEMENT(void) PR_SetBeginGCHook(GCBeginGCHook *hook, void *arg)
197 {
198     LOCK_GC();
199     _pr_beginGCHook = hook;
200     _pr_beginGCHookArg = arg;
201     UNLOCK_GC();
202 }
203
204 PR_IMPLEMENT(void) PR_GetBeginGCHook(GCBeginGCHook **hook, void **arg)
205 {
206     LOCK_GC();
207     *hook = _pr_beginGCHook;
208     *arg = _pr_beginGCHookArg;
209     UNLOCK_GC();
210 }
211
212 PR_IMPLEMENT(void) PR_SetEndGCHook(GCEndGCHook *hook, void *arg)
213 {
214     LOCK_GC();
215     _pr_endGCHook = hook;
216     _pr_endGCHookArg = arg;
217     UNLOCK_GC();
218 }
219
220 PR_IMPLEMENT(void) PR_GetEndGCHook(GCEndGCHook **hook, void **arg)
221 {
222     LOCK_GC();
223     *hook = _pr_endGCHook;
224     *arg = _pr_endGCHookArg;
225     UNLOCK_GC();
226 }
227
228 PR_IMPLEMENT(void) PR_SetBeginFinalizeHook(GCBeginFinalizeHook *hook, void *arg)
229 {
230     LOCK_GC();
231     _pr_beginFinalizeHook = hook;
232     _pr_beginFinalizeHookArg = arg;
233     UNLOCK_GC();
234 }
235
236 PR_IMPLEMENT(void) PR_GetBeginFinalizeHook(GCBeginFinalizeHook **hook, 
237                                            void **arg)
238 {
239     LOCK_GC();
240     *hook = _pr_beginFinalizeHook;
241     *arg = _pr_beginFinalizeHookArg;
242     UNLOCK_GC();
243 }
244
245 PR_IMPLEMENT(void) PR_SetEndFinalizeHook(GCEndFinalizeHook *hook, void *arg)
246 {
247     LOCK_GC();
248     _pr_endFinalizeHook = hook;
249     _pr_endFinalizeHookArg = arg;
250     UNLOCK_GC();
251 }
252
253 PR_IMPLEMENT(void) PR_GetEndFinalizeHook(GCEndFinalizeHook **hook, void **arg)
254 {
255     LOCK_GC();
256     *hook = _pr_endFinalizeHook;
257     *arg = _pr_endFinalizeHookArg;
258     UNLOCK_GC();
259 }
260
261 #ifdef DEBUG
262 #include "prprf.h"
263
264 PR_IMPLEMENT(void) GCTrace(char *fmt, ...)
265 {       
266     va_list ap;
267     char buf[400];
268
269     va_start(ap, fmt);
270     PR_vsnprintf(buf, sizeof(buf), fmt, ap);
271     va_end(ap);
272     PR_LOG(_pr_msgc_lm, PR_LOG_ALWAYS, ("%s", buf));
273 }
274 #endif
275
276 void _PR_InitGC(PRWord flags)
277 {
278     static char firstTime = 1;
279
280     if (!firstTime) return;
281     firstTime = 0;
282
283     _MD_InitGC();
284
285     if (flags == 0) {
286         char *ev = PR_GetEnv("GCLOG");
287         if (ev && ev[0]) {
288             flags = atoi(ev);
289         }
290     }
291     _pr_gcData.flags = flags;
292
293     _pr_gcData.lock = PR_NewMonitor();
294
295     _pr_collectorTypes = (CollectorType*) PR_CALLOC(256 * sizeof(CollectorType));
296
297     PR_RegisterRootFinder(ScanThreads, "scan threads", 0);
298     PR_RegisterRootFinder(_PR_ScanFinalQueue, "scan final queue", 0);
299 }
300
301 extern void pr_FinalizeOnExit(void);
302
303 #ifdef DEBUG
304 #ifdef GC_STATS
305 PR_PUBLIC_API(void) PR_PrintGCAllocStats(void);
306 #endif 
307 #endif 
308   
309 PR_IMPLEMENT(void) 
310 PR_ShutdownGC(PRBool finalizeOnExit)
311 {
312     /* first finalize all the objects in the heap */
313     if (finalizeOnExit) {
314         pr_FinalizeOnExit();
315     }
316
317 #ifdef DEBUG
318 #ifdef GC_STATS
319     PR_PrintGCAllocStats();
320 #endif /* GC_STATS */
321 #endif /* DEBUG */
322
323     /* then the chance for any future allocations */
324
325     /* finally delete the gc heap */
326
327     /* write me */
328 }
329
330 /******************************************************************************/