Chris Lattner | de69a4c | 2002-09-08 18:51:16 +0000 | [diff] [blame] | 1 | //===-- LeakDetector.cpp - Implement LeakDetector interface ---------------===// |
| 2 | // |
| 3 | // This file implements the LeakDetector class. |
| 4 | // |
| 5 | //===----------------------------------------------------------------------===// |
| 6 | |
| 7 | #include "Support/LeakDetector.h" |
| 8 | #include "llvm/Value.h" |
| 9 | #include <set> |
Chris Lattner | de69a4c | 2002-09-08 18:51:16 +0000 | [diff] [blame] | 10 | |
| 11 | // Lazily allocate set so that release build doesn't have to do anything. |
| 12 | static std::set<const void*> *Objects = 0; |
| 13 | static std::set<const Value*> *LLVMObjects = 0; |
| 14 | |
Chris Lattner | 3f6962e | 2002-09-19 19:22:11 +0000 | [diff] [blame] | 15 | // Because the most common usage pattern, by far, is to add a garbage object, |
| 16 | // then remove it immediately, we optimize this case. When an object is added, |
| 17 | // it is not added to the set immediately, it is added to the CachedValue Value. |
| 18 | // If it is immediately removed, no set search need be performed. |
| 19 | // |
| 20 | static const Value *CachedValue; |
| 21 | |
Chris Lattner | de69a4c | 2002-09-08 18:51:16 +0000 | [diff] [blame] | 22 | void LeakDetector::addGarbageObjectImpl(void *Object) { |
| 23 | if (Objects == 0) |
| 24 | Objects = new std::set<const void*>(); |
| 25 | assert(Objects->count(Object) == 0 && "Object already in set!"); |
| 26 | Objects->insert(Object); |
| 27 | } |
| 28 | |
| 29 | void LeakDetector::removeGarbageObjectImpl(void *Object) { |
| 30 | if (Objects) |
| 31 | Objects->erase(Object); |
| 32 | } |
| 33 | |
| 34 | void LeakDetector::addGarbageObjectImpl(const Value *Object) { |
Chris Lattner | 3f6962e | 2002-09-19 19:22:11 +0000 | [diff] [blame] | 35 | if (CachedValue) { |
| 36 | if (LLVMObjects == 0) |
| 37 | LLVMObjects = new std::set<const Value*>(); |
| 38 | assert(LLVMObjects->count(CachedValue) == 0 && "Object already in set!"); |
| 39 | LLVMObjects->insert(CachedValue); |
| 40 | } |
| 41 | CachedValue = Object; |
Chris Lattner | de69a4c | 2002-09-08 18:51:16 +0000 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | void LeakDetector::removeGarbageObjectImpl(const Value *Object) { |
Chris Lattner | 3f6962e | 2002-09-19 19:22:11 +0000 | [diff] [blame] | 45 | if (Object == CachedValue) |
| 46 | CachedValue = 0; // Cache hit! |
| 47 | else if (LLVMObjects) |
Chris Lattner | de69a4c | 2002-09-08 18:51:16 +0000 | [diff] [blame] | 48 | LLVMObjects->erase(Object); |
| 49 | } |
| 50 | |
| 51 | void LeakDetector::checkForGarbageImpl(const std::string &Message) { |
Chris Lattner | 3f6962e | 2002-09-19 19:22:11 +0000 | [diff] [blame] | 52 | if (CachedValue) // Flush the cache to the set... |
| 53 | addGarbageObjectImpl((Value*)0); |
| 54 | |
| 55 | assert(CachedValue == 0 && "No value should be cached anymore!"); |
| 56 | |
Chris Lattner | de69a4c | 2002-09-08 18:51:16 +0000 | [diff] [blame] | 57 | if ((Objects && !Objects->empty()) || (LLVMObjects && !LLVMObjects->empty())){ |
| 58 | std::cerr << "Leaked objects found: " << Message << "\n"; |
| 59 | |
| 60 | if (Objects && !Objects->empty()) { |
| 61 | std::cerr << " Non-Value objects leaked:"; |
| 62 | for (std::set<const void*>::iterator I = Objects->begin(), |
| 63 | E = Objects->end(); I != E; ++I) |
| 64 | std::cerr << " " << *I; |
| 65 | } |
| 66 | |
| 67 | if (LLVMObjects && !LLVMObjects->empty()) { |
| 68 | std::cerr << " LLVM Value subclasses leaked:"; |
| 69 | for (std::set<const Value*>::iterator I = LLVMObjects->begin(), |
| 70 | E = LLVMObjects->end(); I != E; ++I) |
| 71 | std::cerr << **I << "\n"; |
| 72 | } |
| 73 | |
| 74 | std::cerr << "This is probably because you removed an LLVM value " |
| 75 | << "(Instruction, BasicBlock, \netc), but didn't delete it. " |
| 76 | << "Please check your code for memory leaks.\n"; |
| 77 | |
| 78 | // Clear out results so we don't get duplicate warnings on next call... |
| 79 | delete Objects; delete LLVMObjects; |
| 80 | Objects = 0; LLVMObjects = 0; |
| 81 | } |
| 82 | } |