Reapply my less-lock-contention-in-leak-detector patch, now with new files
[oota-llvm.git] / lib / VMCore / LeakDetector.cpp
1 //===-- LeakDetector.cpp - Implement LeakDetector interface ---------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements the LeakDetector class.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "LLVMContextImpl.h"
15 #include "llvm/Support/LeakDetector.h"
16 #include "llvm/ADT/SmallPtrSet.h"
17 #include "llvm/Support/Compiler.h"
18 #include "llvm/Support/ManagedStatic.h"
19 #include "llvm/Support/Streams.h"
20 #include "llvm/System/Mutex.h"
21 #include "llvm/System/Threading.h"
22 #include "llvm/Value.h"
23 using namespace llvm;
24
25 static ManagedStatic<sys::SmartMutex<true> > ObjectsLock;
26 static ManagedStatic<LeakDetectorImpl<void> > Objects;
27
28 static void clearGarbage(LLVMContext &Context) {
29   Objects->clear();
30   Context.pImpl->LLVMObjects.clear();
31 }
32
33 void LeakDetector::addGarbageObjectImpl(void *Object) {
34   sys::SmartScopedLock<true> Lock(*ObjectsLock);
35   Objects->addGarbage(Object);
36 }
37
38 void LeakDetector::addGarbageObjectImpl(const Value *Object) {
39   LLVMContextImpl *pImpl = Object->getContext().pImpl;
40   sys::SmartScopedLock<true> Lock(pImpl->LLVMObjectsLock);
41   pImpl->LLVMObjects.addGarbage(Object);
42 }
43
44 void LeakDetector::removeGarbageObjectImpl(void *Object) {
45   sys::SmartScopedLock<true> Lock(*ObjectsLock);
46   Objects->removeGarbage(Object);
47 }
48
49 void LeakDetector::removeGarbageObjectImpl(const Value *Object) {
50   LLVMContextImpl *pImpl = Object->getContext().pImpl;
51   sys::SmartScopedLock<true> Lock(pImpl->LLVMObjectsLock);
52   pImpl->LLVMObjects.removeGarbage(Object);
53 }
54
55 void LeakDetector::checkForGarbageImpl(LLVMContext &Context, 
56                                        const std::string &Message) {
57   LLVMContextImpl *pImpl = Context.pImpl;
58   sys::SmartScopedLock<true> Lock(*ObjectsLock);
59   sys::SmartScopedLock<true> CLock(pImpl->LLVMObjectsLock);
60   
61   Objects->setName("GENERIC");
62   pImpl->LLVMObjects.setName("LLVM");
63   
64   // use non-short-circuit version so that both checks are performed
65   if (Objects->hasGarbage(Message) |
66       pImpl->LLVMObjects.hasGarbage(Message))
67     cerr << "\nThis is probably because you removed an object, but didn't "
68          << "delete it.  Please check your code for memory leaks.\n";
69
70   // Clear out results so we don't get duplicate warnings on
71   // next call...
72   clearGarbage(Context);
73 }