LeakDetector.cpp revision ce36d55cf8d3239942e0e9c426c835f0c33a11e6
1//===-- LeakDetector.cpp - Implement LeakDetector interface ---------------===// 2// 3// The LLVM Compiler Infrastructure 4// 5// This file was developed by the LLVM research group and is distributed under 6// the University of Illinois Open Source License. See LICENSE.TXT for details. 7// 8//===----------------------------------------------------------------------===// 9// 10// This file implements the LeakDetector class. 11// 12//===----------------------------------------------------------------------===// 13 14#include "Support/LeakDetector.h" 15#include "llvm/Value.h" 16#include <iostream> 17#include <set> 18using namespace llvm; 19 20namespace { 21 template <class T> 22 struct PrinterTrait { 23 static void print(const T* P) { std::cerr << P; } 24 }; 25 26 template<> 27 struct PrinterTrait<Value> { 28 static void print(const Value* P) { std::cerr << *P; } 29 }; 30 31 template <typename T> 32 struct LeakDetectorImpl { 33 LeakDetectorImpl(const char* const name) : Cache(0), Name(name) { } 34 35 // Because the most common usage pattern, by far, is to add a 36 // garbage object, then remove it immediately, we optimize this 37 // case. When an object is added, it is not added to the set 38 // immediately, it is added to the CachedValue Value. If it is 39 // immediately removed, no set search need be performed. 40 void addGarbage(const T* o) { 41 if (Cache) { 42 assert(Ts.count(Cache) == 0 && "Object already in set!"); 43 Ts.insert(Cache); 44 } 45 Cache = o; 46 } 47 48 void removeGarbage(const T* o) { 49 if (o == Cache) 50 Cache = 0; // Cache hit 51 else 52 Ts.erase(o); 53 } 54 55 bool hasGarbage(const std::string& Message) { 56 addGarbage(0); // Flush the Cache 57 58 assert(Cache == 0 && "No value should be cached anymore!"); 59 60 if (!Ts.empty()) { 61 std::cerr 62 << "Leaked " << Name << " objects found: " << Message << ":\n"; 63 for (typename std::set<const T*>::iterator I = Ts.begin(), 64 E = Ts.end(); I != E; ++I) { 65 std::cerr << "\t"; 66 PrinterTrait<T>::print(*I); 67 std::cerr << "\n"; 68 } 69 std::cerr << '\n'; 70 71 // Clear out results so we don't get duplicate warnings on 72 // next call... 73 Ts.clear(); 74 return true; 75 } 76 return false; 77 } 78 79 private: 80 std::set<const T*> Ts; 81 const T* Cache; 82 const char* const Name; 83 }; 84 85 typedef LeakDetectorImpl<void> Objects; 86 typedef LeakDetectorImpl<Value> LLVMObjects; 87 88 Objects& getObjects() { 89 static Objects *o = 0; 90 if (o == 0) 91 o = new Objects("GENERIC"); 92 return *o; 93 } 94 95 LLVMObjects& getLLVMObjects() { 96 static LLVMObjects *o = 0; 97 if (o == 0) 98 o = new LLVMObjects("LLVM"); 99 return *o; 100 } 101} 102 103void LeakDetector::addGarbageObjectImpl(void *Object) { 104 getObjects().addGarbage(Object); 105} 106 107void LeakDetector::addGarbageObjectImpl(const Value *Object) { 108 getLLVMObjects().addGarbage(Object); 109} 110 111void LeakDetector::removeGarbageObjectImpl(void *Object) { 112 getObjects().removeGarbage(Object); 113} 114 115void LeakDetector::removeGarbageObjectImpl(const Value *Object) { 116 getLLVMObjects().removeGarbage(Object); 117} 118 119void LeakDetector::checkForGarbageImpl(const std::string &Message) { 120 // use non-short-circuit version so that both checks are performed 121 if (getObjects().hasGarbage(Message) | 122 getLLVMObjects().hasGarbage(Message)) 123 std::cerr << "\nThis is probably because you removed an object, but didn't " 124 "delete it. Please check your code for memory leaks.\n"; 125} 126