1 //===-- LeakDetector.cpp - Implement LeakDetector interface ---------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the LeakDetector class.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/Support/LeakDetector.h"
15 #include "llvm/ADT/SmallPtrSet.h"
16 #include "llvm/Support/Compiler.h"
17 #include "llvm/Support/ManagedStatic.h"
18 #include "llvm/Support/Streams.h"
19 #include "llvm/Support/Threading.h"
20 #include "llvm/System/RWMutex.h"
21 #include "llvm/Value.h"
26 struct VISIBILITY_HIDDEN PrinterTrait {
27 static void print(const T* P) { cerr << P; }
31 struct VISIBILITY_HIDDEN PrinterTrait<Value> {
32 static void print(const Value* P) { cerr << *P; }
35 ManagedStatic<sys::RWMutex> LeakDetectorLock;
38 struct VISIBILITY_HIDDEN LeakDetectorImpl {
39 explicit LeakDetectorImpl(const char* const name = "") :
40 Cache(0), Name(name) { }
47 void setName(const char* n) {
51 // Because the most common usage pattern, by far, is to add a
52 // garbage object, then remove it immediately, we optimize this
53 // case. When an object is added, it is not added to the set
54 // immediately, it is added to the CachedValue Value. If it is
55 // immediately removed, no set search need be performed.
56 void addGarbage(const T* o) {
57 if (llvm_is_multithreaded()) {
58 sys::ScopedWriter Writer(&*LeakDetectorLock);
60 assert(Ts.count(Cache) == 0 && "Object already in set!");
66 assert(Ts.count(Cache) == 0 && "Object already in set!");
73 void removeGarbage(const T* o) {
74 if (llvm_is_multithreaded()) {
75 sys::ScopedWriter Writer(&*LeakDetectorLock);
77 Cache = 0; // Cache hit
82 Cache = 0; // Cache hit
88 bool hasGarbage(const std::string& Message) {
89 addGarbage(0); // Flush the Cache
91 if (llvm_is_multithreaded()) LeakDetectorLock->reader_acquire();
92 assert(Cache == 0 && "No value should be cached anymore!");
95 cerr << "Leaked " << Name << " objects found: " << Message << ":\n";
96 for (typename SmallPtrSet<const T*, 8>::iterator I = Ts.begin(),
97 E = Ts.end(); I != E; ++I) {
99 PrinterTrait<T>::print(*I);
104 if (llvm_is_multithreaded()) LeakDetectorLock->reader_release();
108 if (llvm_is_multithreaded()) LeakDetectorLock->reader_release();
113 SmallPtrSet<const T*, 8> Ts;
118 static ManagedStatic<LeakDetectorImpl<void> > Objects;
119 static ManagedStatic<LeakDetectorImpl<Value> > LLVMObjects;
121 static void clearGarbage() {
123 LLVMObjects->clear();
127 void LeakDetector::addGarbageObjectImpl(void *Object) {
128 Objects->addGarbage(Object);
131 void LeakDetector::addGarbageObjectImpl(const Value *Object) {
132 LLVMObjects->addGarbage(Object);
135 void LeakDetector::removeGarbageObjectImpl(void *Object) {
136 Objects->removeGarbage(Object);
139 void LeakDetector::removeGarbageObjectImpl(const Value *Object) {
140 LLVMObjects->removeGarbage(Object);
143 void LeakDetector::checkForGarbageImpl(const std::string &Message) {
144 Objects->setName("GENERIC");
145 LLVMObjects->setName("LLVM");
147 // use non-short-circuit version so that both checks are performed
148 if (Objects->hasGarbage(Message) |
149 LLVMObjects->hasGarbage(Message))
150 cerr << "\nThis is probably because you removed an object, but didn't "
151 << "delete it. Please check your code for memory leaks.\n";
153 // Clear out results so we don't get duplicate warnings on