[21694] | 1 | /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- |
---|
| 2 | * |
---|
| 3 | * The contents of this file are subject to the Mozilla Public |
---|
| 4 | * License Version 1.1 (the "License"); you may not use this file |
---|
| 5 | * except in compliance with the License. You may obtain a copy of |
---|
| 6 | * the License at http://www.mozilla.org/MPL/ |
---|
| 7 | * |
---|
| 8 | * Software distributed under the License is distributed on an "AS |
---|
| 9 | * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or |
---|
| 10 | * implied. See the License for the specific language governing |
---|
| 11 | * rights and limitations under the License. |
---|
| 12 | * |
---|
| 13 | * The Original Code is the Mozilla browser. |
---|
| 14 | * |
---|
| 15 | * The Initial Developer of the Original Code is Netscape |
---|
| 16 | * Communications, Inc. Portions created by Netscape are |
---|
| 17 | * Copyright (C) 1999, Mozilla. All Rights Reserved. |
---|
| 18 | * |
---|
| 19 | * Contributor(s): |
---|
| 20 | * Patrick Beard <beard@netscape.com> |
---|
| 21 | */ |
---|
| 22 | |
---|
| 23 | /* |
---|
| 24 | nsGarbageCollector.c |
---|
| 25 | */ |
---|
| 26 | |
---|
| 27 | #ifdef GC_LEAK_DETECTOR |
---|
| 28 | |
---|
| 29 | /* for FILE */ |
---|
| 30 | #include <stdio.h> |
---|
| 31 | |
---|
| 32 | /* NSPR stuff */ |
---|
| 33 | #include "generic_threads.h" |
---|
| 34 | #include "prthread.h" |
---|
| 35 | #include "prlock.h" |
---|
| 36 | |
---|
| 37 | /* Linux/Win32 export private NSPR files to include/private */ |
---|
| 38 | #ifdef XP_MAC |
---|
| 39 | #include "pprthred.h" |
---|
| 40 | #else |
---|
| 41 | #include "private/pprthred.h" |
---|
| 42 | #endif |
---|
| 43 | |
---|
| 44 | #include "nsLeakDetector.h" |
---|
| 45 | |
---|
| 46 | extern FILE *GC_stdout, *GC_stderr; |
---|
| 47 | |
---|
| 48 | extern void GC_gcollect(void); |
---|
| 49 | extern void GC_clear_roots(void); |
---|
| 50 | |
---|
| 51 | static PRStatus PR_CALLBACK scanner(PRThread* t, void** baseAddr, PRUword count, void* closure) |
---|
| 52 | { |
---|
| 53 | char* begin = (char*)baseAddr; |
---|
| 54 | char* end = (char*)(baseAddr + count); |
---|
| 55 | GC_mark_range_proc marker = (GC_mark_range_proc) closure; |
---|
| 56 | marker(begin, end); |
---|
| 57 | return PR_SUCCESS; |
---|
| 58 | } |
---|
| 59 | |
---|
| 60 | static void mark_all_stacks(GC_mark_range_proc marker) |
---|
| 61 | { |
---|
| 62 | /* PR_ThreadScanStackPointers(PR_GetCurrentThread(), &scanner, marker); */ |
---|
| 63 | PR_ScanStackPointers(&scanner, (void *)marker); |
---|
| 64 | } |
---|
| 65 | |
---|
| 66 | static void locker(void* mutex) |
---|
| 67 | { |
---|
| 68 | PR_Lock(mutex); |
---|
| 69 | } |
---|
| 70 | |
---|
| 71 | static void unlocker(void* mutex) |
---|
| 72 | { |
---|
| 73 | PR_Unlock(mutex); |
---|
| 74 | } |
---|
| 75 | |
---|
| 76 | static void stopper(void* unused) |
---|
| 77 | { |
---|
| 78 | PR_SuspendAll(); |
---|
| 79 | } |
---|
| 80 | |
---|
| 81 | static void starter(void* unused) |
---|
| 82 | { |
---|
| 83 | PR_ResumeAll(); |
---|
| 84 | } |
---|
| 85 | |
---|
| 86 | nsresult NS_InitGarbageCollector() |
---|
| 87 | { |
---|
| 88 | PRLock* mutex; |
---|
| 89 | |
---|
| 90 | /* redirect GC's stderr to catch startup leaks. */ |
---|
| 91 | GC_stderr = fopen("StartupLeaks", "w"); |
---|
| 92 | |
---|
| 93 | mutex = PR_NewLock(); |
---|
| 94 | if (mutex == NULL) |
---|
| 95 | return NS_ERROR_FAILURE; |
---|
| 96 | |
---|
| 97 | GC_generic_init_threads(&mark_all_stacks, mutex, |
---|
| 98 | &locker, &unlocker, |
---|
| 99 | &stopper, &starter); |
---|
| 100 | |
---|
| 101 | return NS_OK; |
---|
| 102 | } |
---|
| 103 | |
---|
| 104 | nsresult NS_ShutdownGarbageCollector() |
---|
| 105 | { |
---|
| 106 | /* do anything you need to shut down the collector. */ |
---|
| 107 | return NS_OK; |
---|
| 108 | } |
---|
| 109 | |
---|
| 110 | #endif /* GC_LEAK_DETECTOR */ |
---|