llvm.org GIT mirror llvm / c34ebf6
Reapply my less-lock-contention-in-leak-detector patch, now with new files actually added. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@79429 91177308-0d34-0410-b5e6-96231b3b80d8 Owen Anderson 10 years ago
4 changed file(s) with 122 addition(s) and 93 deletion(s). Raw diff Collapse all Expand all
5555 /// The specified message will be printed indicating when the check was
5656 /// performed.
5757 ///
58 static void checkForGarbage(const std::string &Message) {
58 static void checkForGarbage(LLVMContext &C, const std::string &Message) {
5959 #ifndef NDEBUG
60 checkForGarbageImpl(Message);
60 checkForGarbageImpl(C, Message);
6161 #endif
6262 }
6363
8282 static void removeGarbageObjectImpl(const Value *Object);
8383 static void addGarbageObjectImpl(void *Object);
8484 static void removeGarbageObjectImpl(void *Object);
85 static void checkForGarbageImpl(const std::string &Message);
85 static void checkForGarbageImpl(LLVMContext &C, const std::string &Message);
8686 };
8787
8888 } // End llvm namespace
1515 #define LLVM_LLVMCONTEXT_IMPL_H
1616
1717 #include "ConstantsContext.h"
18 #include "LeaksContext.h"
1819 #include "TypesContext.h"
1920 #include "llvm/LLVMContext.h"
2021 #include "llvm/Constants.h"
132133
133134 ConstantInt *TheTrueVal;
134135 ConstantInt *TheFalseVal;
136
137 // Lock used for guarding access to the leak detector
138 sys::SmartMutex LLVMObjectsLock;
139 LeakDetectorImpl LLVMObjects;
135140
136141 // Lock used for guarding access to the type maps.
137142 sys::SmartMutex TypeMapLock;
1010 //
1111 //===----------------------------------------------------------------------===//
1212
13 #include "LLVMContextImpl.h"
1314 #include "llvm/Support/LeakDetector.h"
1415 #include "llvm/ADT/SmallPtrSet.h"
1516 #include "llvm/Support/Compiler.h"
1617 #include "llvm/Support/ManagedStatic.h"
1718 #include "llvm/Support/Streams.h"
18 #include "llvm/System/RWMutex.h"
19 #include "llvm/System/Mutex.h"
1920 #include "llvm/System/Threading.h"
2021 #include "llvm/Value.h"
2122 using namespace llvm;
2223
23 namespace {
24 template
25 struct VISIBILITY_HIDDEN PrinterTrait {
26 static void print(const T* P) { cerr << P; }
27 };
24 static ManagedStatic > ObjectsLock;
25 static ManagedStatic > Objects;
2826
29 template<>
30 struct VISIBILITY_HIDDEN PrinterTrait {
31 static void print(const Value* P) { cerr << *P; }
32 };
33
34 ManagedStatic > LeakDetectorLock;
35
36 template
37 struct VISIBILITY_HIDDEN LeakDetectorImpl {
38 explicit LeakDetectorImpl(const char* const name = "") :
39 Cache(0), Name(name) { }
40
41 void clear() {
42 Cache = 0;
43 Ts.clear();
44 }
45
46 void setName(const char* n) {
47 Name = n;
48 }
49
50 // Because the most common usage pattern, by far, is to add a
51 // garbage object, then remove it immediately, we optimize this
52 // case. When an object is added, it is not added to the set
53 // immediately, it is added to the CachedValue Value. If it is
54 // immediately removed, no set search need be performed.
55 void addGarbage(const T* o) {
56 sys::SmartScopedWriter Writer(*LeakDetectorLock);
57 if (Cache) {
58 assert(Ts.count(Cache) == 0 && "Object already in set!");
59 Ts.insert(Cache);
60 }
61 Cache = o;
62 }
63
64 void removeGarbage(const T* o) {
65 sys::SmartScopedWriter Writer(*LeakDetectorLock);
66 if (o == Cache)
67 Cache = 0; // Cache hit
68 else
69 Ts.erase(o);
70 }
71
72 bool hasGarbage(const std::string& Message) {
73 addGarbage(0); // Flush the Cache
74
75 sys::SmartScopedReader Reader(*LeakDetectorLock);
76 assert(Cache == 0 && "No value should be cached anymore!");
77
78 if (!Ts.empty()) {
79 cerr << "Leaked " << Name << " objects found: " << Message << ":\n";
80 for (typename SmallPtrSet::iterator I = Ts.begin(),
81 E = Ts.end(); I != E; ++I) {
82 cerr << "\t";
83 PrinterTrait::print(*I);
84 cerr << "\n";
85 }
86 cerr << '\n';
87
88 return true;
89 }
90
91 return false;
92 }
93
94 private:
95 SmallPtrSet Ts;
96 const T* Cache;
97 const char* Name;
98 };
99
100 static ManagedStatic > Objects;
101 static ManagedStatic > LLVMObjects;
102
103 static void clearGarbage() {
104 Objects->clear();
105 LLVMObjects->clear();
106 }
27 static void clearGarbage(LLVMContext &Context) {
28 Objects->clear();
29 Context.pImpl->LLVMObjects.clear();
10730 }
10831
10932 void LeakDetector::addGarbageObjectImpl(void *Object) {
33 sys::SmartScopedLock Lock(*ObjectsLock);
11034 Objects->addGarbage(Object);
11135 }
11236
11337 void LeakDetector::addGarbageObjectImpl(const Value *Object) {
114 LLVMObjects->addGarbage(Object);
38 LLVMContextImpl *pImpl = Object->getContext().pImpl;
39 sys::SmartScopedLock Lock(pImpl->LLVMObjectsLock);
40 pImpl->LLVMObjects.addGarbage(Object);
11541 }
11642
11743 void LeakDetector::removeGarbageObjectImpl(void *Object) {
44 sys::SmartScopedLock Lock(*ObjectsLock);
11845 Objects->removeGarbage(Object);
11946 }
12047
12148 void LeakDetector::removeGarbageObjectImpl(const Value *Object) {
122 LLVMObjects->removeGarbage(Object);
49 LLVMContextImpl *pImpl = Object->getContext().pImpl;
50 sys::SmartScopedLock Lock(pImpl->LLVMObjectsLock);
51 pImpl->LLVMObjects.removeGarbage(Object);
12352 }
12453
125 void LeakDetector::checkForGarbageImpl(const std::string &Message) {
54 void LeakDetector::checkForGarbageImpl(LLVMContext &Context,
55 const std::string &Message) {
56 LLVMContextImpl *pImpl = Context.pImpl;
57 sys::SmartScopedLock Lock(*ObjectsLock);
58 sys::SmartScopedLock CLock(pImpl->LLVMObjectsLock);
59
12660 Objects->setName("GENERIC");
127 LLVMObjects->setName("LLVM");
61 pImpl->LLVMObjects.setName("LLVM");
12862
12963 // use non-short-circuit version so that both checks are performed
13064 if (Objects->hasGarbage(Message) |
131 LLVMObjects->hasGarbage(Message))
65 pImpl->LLVMObjects.hasGarbage(Message))
13266 cerr << "\nThis is probably because you removed an object, but didn't "
13367 << "delete it. Please check your code for memory leaks.\n";
13468
13569 // Clear out results so we don't get duplicate warnings on
13670 // next call...
137 clearGarbage();
71 clearGarbage(Context);
13872 }
0 //===---------------- ----LeaksContext.h - Implementation ------*- C++ -*--===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file defines various helper methods and classes used by
10 // LLVMContextImpl for leaks detectors.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "llvm/Value.h"
15 #include "llvm/Support/Streams.h"
16 #include "llvm/ADT/SmallPtrSet.h"
17
18 using namespace llvm;
19
20 template
21 struct PrinterTrait {
22 static void print(const T* P) { cerr << P; }
23 };
24
25 template<>
26 struct PrinterTrait {
27 static void print(const Value* P) { cerr << *P; }
28 };
29
30 template
31 struct LeakDetectorImpl {
32 explicit LeakDetectorImpl(const char* const name = "") :
33 Cache(0), Name(name) { }
34
35 void clear() {
36 Cache = 0;
37 Ts.clear();
38 }
39
40 void setName(const char* n) {
41 Name = n;
42 }
43
44 // Because the most common usage pattern, by far, is to add a
45 // garbage object, then remove it immediately, we optimize this
46 // case. When an object is added, it is not added to the set
47 // immediately, it is added to the CachedValue Value. If it is
48 // immediately removed, no set search need be performed.
49 void addGarbage(const T* o) {
50 if (Cache) {
51 assert(Ts.count(Cache) == 0 && "Object already in set!");
52 Ts.insert(Cache);
53 }
54 Cache = o;
55 }
56
57 void removeGarbage(const T* o) {
58 if (o == Cache)
59 Cache = 0; // Cache hit
60 else
61 Ts.erase(o);
62 }
63
64 bool hasGarbage(const std::string& Message) {
65 addGarbage(0); // Flush the Cache
66
67 assert(Cache == 0 && "No value should be cached anymore!");
68
69 if (!Ts.empty()) {
70 cerr << "Leaked " << Name << " objects found: " << Message << ":\n";
71 for (typename SmallPtrSet::iterator I = Ts.begin(),
72 E = Ts.end(); I != E; ++I) {
73 cerr << "\t";
74 PrinterTrait::print(*I);
75 cerr << "\n";
76 }
77 cerr << '\n';
78
79 return true;
80 }
81
82 return false;
83 }
84
85 private:
86 SmallPtrSet Ts;
87 const T* Cache;
88 const char* Name;
89 };