Implement changes from Chris's feedback.
[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 "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/System/RWMutex.h"
20 #include "llvm/System/Threading.h"
21 #include "llvm/Value.h"
22 using namespace llvm;
23
24 namespace {
25   template <class T>
26   struct VISIBILITY_HIDDEN PrinterTrait {
27     static void print(const T* P) { cerr << P; }
28   };
29
30   template<>
31   struct VISIBILITY_HIDDEN PrinterTrait<Value> {
32     static void print(const Value* P) { cerr << *P; }
33   };
34
35   ManagedStatic<sys::SmartRWMutex<true> > LeakDetectorLock;
36
37   template <typename T>
38   struct VISIBILITY_HIDDEN LeakDetectorImpl {
39     explicit LeakDetectorImpl(const char* const name = "") : 
40       Cache(0), Name(name) { }
41
42     void clear() {
43       Cache = 0;
44       Ts.clear();
45     }
46     
47     void setName(const char* n) { 
48       Name = n;
49     }
50     
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       sys::SmartScopedWriter<true> Writer(*LeakDetectorLock);
58       if (Cache) {
59         assert(Ts.count(Cache) == 0 && "Object already in set!");
60         Ts.insert(Cache);
61       }
62       Cache = o;
63     }
64
65     void removeGarbage(const T* o) {
66       sys::SmartScopedWriter<true> Writer(*LeakDetectorLock);
67       if (o == Cache)
68         Cache = 0; // Cache hit
69       else
70         Ts.erase(o);
71     }
72
73     bool hasGarbage(const std::string& Message) {
74       addGarbage(0); // Flush the Cache
75
76       sys::SmartScopedReader<true> Reader(*LeakDetectorLock);
77       assert(Cache == 0 && "No value should be cached anymore!");
78
79       if (!Ts.empty()) {
80         cerr << "Leaked " << Name << " objects found: " << Message << ":\n";
81         for (typename SmallPtrSet<const T*, 8>::iterator I = Ts.begin(),
82                E = Ts.end(); I != E; ++I) {
83           cerr << "\t";
84           PrinterTrait<T>::print(*I);
85           cerr << "\n";
86         }
87         cerr << '\n';
88
89         return true;
90       }
91       
92       return false;
93     }
94
95   private:
96     SmallPtrSet<const T*, 8> Ts;
97     const T* Cache;
98     const char* Name;
99   };
100
101   static ManagedStatic<LeakDetectorImpl<void> > Objects;
102   static ManagedStatic<LeakDetectorImpl<Value> > LLVMObjects;
103
104   static void clearGarbage() {
105     Objects->clear();
106     LLVMObjects->clear();
107   }
108 }
109
110 void LeakDetector::addGarbageObjectImpl(void *Object) {
111   Objects->addGarbage(Object);
112 }
113
114 void LeakDetector::addGarbageObjectImpl(const Value *Object) {
115   LLVMObjects->addGarbage(Object);
116 }
117
118 void LeakDetector::removeGarbageObjectImpl(void *Object) {
119   Objects->removeGarbage(Object);
120 }
121
122 void LeakDetector::removeGarbageObjectImpl(const Value *Object) {
123   LLVMObjects->removeGarbage(Object);
124 }
125
126 void LeakDetector::checkForGarbageImpl(const std::string &Message) {
127   Objects->setName("GENERIC");
128   LLVMObjects->setName("LLVM");
129   
130   // use non-short-circuit version so that both checks are performed
131   if (Objects->hasGarbage(Message) |
132       LLVMObjects->hasGarbage(Message))
133     cerr << "\nThis is probably because you removed an object, but didn't "
134          << "delete it.  Please check your code for memory leaks.\n";
135
136   // Clear out results so we don't get duplicate warnings on
137   // next call...
138   clearGarbage();
139 }