It turns out most of the thumb2 instructions are not allowed to touch SP. The semanti...
[llvm/avr.git] / lib / VMCore / LeakDetector.cpp
bloba6be1afed49b393eefa6dc8b0712457b5a29ead2
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.
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/System/RWMutex.h"
20 #include "llvm/System/Threading.h"
21 #include "llvm/Value.h"
22 using namespace llvm;
24 namespace {
25 template <class T>
26 struct VISIBILITY_HIDDEN PrinterTrait {
27 static void print(const T* P) { cerr << P; }
30 template<>
31 struct VISIBILITY_HIDDEN PrinterTrait<Value> {
32 static void print(const Value* P) { cerr << *P; }
35 ManagedStatic<sys::SmartRWMutex<true> > LeakDetectorLock;
37 template <typename T>
38 struct VISIBILITY_HIDDEN LeakDetectorImpl {
39 explicit LeakDetectorImpl(const char* const name = "") :
40 Cache(0), Name(name) { }
42 void clear() {
43 Cache = 0;
44 Ts.clear();
47 void setName(const char* n) {
48 Name = 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 sys::SmartScopedWriter<true> Writer(*LeakDetectorLock);
58 if (Cache) {
59 assert(Ts.count(Cache) == 0 && "Object already in set!");
60 Ts.insert(Cache);
62 Cache = o;
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);
73 bool hasGarbage(const std::string& Message) {
74 addGarbage(0); // Flush the Cache
76 sys::SmartScopedReader<true> Reader(*LeakDetectorLock);
77 assert(Cache == 0 && "No value should be cached anymore!");
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";
87 cerr << '\n';
89 return true;
92 return false;
95 private:
96 SmallPtrSet<const T*, 8> Ts;
97 const T* Cache;
98 const char* Name;
101 static ManagedStatic<LeakDetectorImpl<void> > Objects;
102 static ManagedStatic<LeakDetectorImpl<Value> > LLVMObjects;
104 static void clearGarbage() {
105 Objects->clear();
106 LLVMObjects->clear();
110 void LeakDetector::addGarbageObjectImpl(void *Object) {
111 Objects->addGarbage(Object);
114 void LeakDetector::addGarbageObjectImpl(const Value *Object) {
115 LLVMObjects->addGarbage(Object);
118 void LeakDetector::removeGarbageObjectImpl(void *Object) {
119 Objects->removeGarbage(Object);
122 void LeakDetector::removeGarbageObjectImpl(const Value *Object) {
123 LLVMObjects->removeGarbage(Object);
126 void LeakDetector::checkForGarbageImpl(const std::string &Message) {
127 Objects->setName("GENERIC");
128 LLVMObjects->setName("LLVM");
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";
136 // Clear out results so we don't get duplicate warnings on
137 // next call...
138 clearGarbage();