Back to index

lightning-sunbird  0.9+nobinonly
nsGarbageCollector.c
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
00002  *
00003  * ***** BEGIN LICENSE BLOCK *****
00004  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00005  *
00006  * The contents of this file are subject to the Mozilla Public License Version
00007  * 1.1 (the "License"); you may not use this file except in compliance with
00008  * the License. You may obtain a copy of the License at
00009  * http://www.mozilla.org/MPL/
00010  *
00011  * Software distributed under the License is distributed on an "AS IS" basis,
00012  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00013  * for the specific language governing rights and limitations under the
00014  * License.
00015  *
00016  * The Original Code is the Mozilla browser.
00017  *
00018  * The Initial Developer of the Original Code is
00019  * Netscape Communications, Inc.
00020  * Portions created by the Initial Developer are Copyright (C) 1999
00021  * the Initial Developer. All Rights Reserved.
00022  *
00023  * Contributor(s):
00024  *   Patrick Beard <beard@netscape.com>
00025  *
00026  * Alternatively, the contents of this file may be used under the terms of
00027  * either of the GNU General Public License Version 2 or later (the "GPL"),
00028  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00029  * in which case the provisions of the GPL or the LGPL are applicable instead
00030  * of those above. If you wish to allow use of your version of this file only
00031  * under the terms of either the GPL or the LGPL, and not to allow others to
00032  * use your version of this file under the terms of the MPL, indicate your
00033  * decision by deleting the provisions above and replace them with the notice
00034  * and other provisions required by the GPL or the LGPL. If you do not delete
00035  * the provisions above, a recipient may use your version of this file under
00036  * the terms of any one of the MPL, the GPL or the LGPL.
00037  *
00038  * ***** END LICENSE BLOCK ***** */
00039 
00040 /*
00041        nsGarbageCollector.c
00042  */
00043 
00044 #ifdef GC_LEAK_DETECTOR
00045 
00046 /* for FILE */
00047 #include <stdio.h>
00048 
00049 /* NSPR stuff */
00050 #include "generic_threads.h"
00051 #include "prthread.h"
00052 #include "prlock.h"
00053 
00054 /* Linux/Win32 export private NSPR files to include/private */
00055 #ifdef XP_MAC
00056 #include "pprthred.h"
00057 #else
00058 #include "private/pprthred.h"
00059 #endif
00060 
00061 #include "nsLeakDetector.h"
00062 
00063 extern FILE *GC_stdout, *GC_stderr;
00064 
00065 extern void GC_gcollect(void);
00066 extern void GC_clear_roots(void);
00067 
00068 static PRStatus PR_CALLBACK scanner(PRThread* t, void** baseAddr, PRUword count, void* closure)
00069 {
00070        char* begin = (char*)baseAddr;
00071        char* end = (char*)(baseAddr + count);
00072        GC_mark_range_proc marker = (GC_mark_range_proc) closure;
00073        marker(begin, end);
00074        return PR_SUCCESS;
00075 }
00076 
00077 static void mark_all_stacks(GC_mark_range_proc marker)
00078 {
00079        /* PR_ThreadScanStackPointers(PR_GetCurrentThread(), &scanner, marker); */
00080        PR_ScanStackPointers(&scanner, (void *)marker);
00081 }
00082 
00083 static void locker(void* mutex)
00084 {
00085        PR_Lock(mutex);
00086 }
00087 
00088 static void unlocker(void* mutex)
00089 {
00090        PR_Unlock(mutex);
00091 }
00092 
00093 static void stopper(void* unused)
00094 {
00095        PR_SuspendAll();
00096 }
00097 
00098 static void starter(void* unused)
00099 {
00100        PR_ResumeAll();
00101 }
00102 
00103 nsresult NS_InitGarbageCollector()
00104 {
00105        PRLock* mutex;
00106        
00107        /* redirect GC's stderr to catch startup leaks. */
00108        GC_stderr = fopen("StartupLeaks", "w");
00109 
00110        mutex = PR_NewLock();
00111        if (mutex == NULL)
00112               return NS_ERROR_FAILURE;
00113               
00114        GC_generic_init_threads(&mark_all_stacks, mutex,
00115                                                  &locker, &unlocker,
00116                                                  &stopper, &starter);
00117        
00118        return NS_OK;
00119 }
00120 
00121 nsresult NS_ShutdownGarbageCollector()
00122 {
00123        /* do anything you need to shut down the collector. */
00124        return NS_OK;
00125 }
00126 
00127 #endif /* GC_LEAK_DETECTOR */