As pointed out by Duncan, I accidentally dropped the first MemoryFence of the
[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/Support/Threading.h"
20 #include "llvm/System/RWMutex.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::RWMutex> 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       if (llvm_is_multithreaded()) {
58         sys::ScopedWriter Writer(&*LeakDetectorLock);
59         if (Cache) {
60           assert(Ts.count(Cache) == 0 && "Object already in set!");
61           Ts.insert(Cache);
62         }
63         Cache = o;
64       } else {
65         if (Cache) {
66           assert(Ts.count(Cache) == 0 && "Object already in set!");
67           Ts.insert(Cache);
68         }
69         Cache = o;
70       }
71     }
72
73     void removeGarbage(const T* o) {
74       if (llvm_is_multithreaded()) {
75         sys::ScopedWriter Writer(&*LeakDetectorLock);
76         if (o == Cache)
77           Cache = 0; // Cache hit
78         else
79           Ts.erase(o);
80       } else {
81         if (o == Cache)
82           Cache = 0; // Cache hit
83         else
84           Ts.erase(o);
85       }
86     }
87
88     bool hasGarbage(const std::string& Message) {
89       addGarbage(0); // Flush the Cache
90
91       if (llvm_is_multithreaded()) LeakDetectorLock->reader_acquire();
92       assert(Cache == 0 && "No value should be cached anymore!");
93
94       if (!Ts.empty()) {
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) {
98           cerr << "\t";
99           PrinterTrait<T>::print(*I);
100           cerr << "\n";
101         }
102         cerr << '\n';
103
104         if (llvm_is_multithreaded()) LeakDetectorLock->reader_release();
105         return true;
106       }
107       
108       if (llvm_is_multithreaded()) LeakDetectorLock->reader_release();
109       return false;
110     }
111
112   private:
113     SmallPtrSet<const T*, 8> Ts;
114     const T* Cache;
115     const char* Name;
116   };
117
118   static ManagedStatic<LeakDetectorImpl<void> > Objects;
119   static ManagedStatic<LeakDetectorImpl<Value> > LLVMObjects;
120
121   static void clearGarbage() {
122     Objects->clear();
123     LLVMObjects->clear();
124   }
125 }
126
127 void LeakDetector::addGarbageObjectImpl(void *Object) {
128   Objects->addGarbage(Object);
129 }
130
131 void LeakDetector::addGarbageObjectImpl(const Value *Object) {
132   LLVMObjects->addGarbage(Object);
133 }
134
135 void LeakDetector::removeGarbageObjectImpl(void *Object) {
136   Objects->removeGarbage(Object);
137 }
138
139 void LeakDetector::removeGarbageObjectImpl(const Value *Object) {
140   LLVMObjects->removeGarbage(Object);
141 }
142
143 void LeakDetector::checkForGarbageImpl(const std::string &Message) {
144   Objects->setName("GENERIC");
145   LLVMObjects->setName("LLVM");
146   
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";
152
153   // Clear out results so we don't get duplicate warnings on
154   // next call...
155   clearGarbage();
156 }