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/System/RWMutex.h"
20 #include "llvm/System/Threading.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::SmartRWMutex
<true> > 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 sys::SmartScopedWriter
<true> Writer(*LeakDetectorLock
);
59 assert(Ts
.count(Cache
) == 0 && "Object already in set!");
65 void removeGarbage(const T
* o
) {
66 sys::SmartScopedWriter
<true> Writer(*LeakDetectorLock
);
68 Cache
= 0; // Cache hit
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!");
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
) {
84 PrinterTrait
<T
>::print(*I
);
96 SmallPtrSet
<const T
*, 8> Ts
;
101 static ManagedStatic
<LeakDetectorImpl
<void> > Objects
;
102 static ManagedStatic
<LeakDetectorImpl
<Value
> > LLVMObjects
;
104 static void clearGarbage() {
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