Fix memory leak in the stackifier, due to the machinebasicblocks not holding
[oota-llvm.git] / lib / VMCore / LeakDetector.cpp
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 <set>
17 using namespace llvm;
18
19 // Lazily allocate set so that release build doesn't have to do anything.
20 static std::set<const void*> *Objects = 0;
21 static std::set<const Value*> *LLVMObjects = 0;
22
23 // Because the most common usage pattern, by far, is to add a garbage object,
24 // then remove it immediately, we optimize this case.  When an object is added,
25 // it is not added to the set immediately, it is added to the CachedValue Value.
26 // If it is immediately removed, no set search need be performed.
27 //
28 static const Value *CachedValue;
29
30 void LeakDetector::addGarbageObjectImpl(void *Object) {
31   if (Objects == 0)
32     Objects = new std::set<const void*>();
33   assert(Objects->count(Object) == 0 && "Object already in set!");
34   Objects->insert(Object);
35 }
36
37 void LeakDetector::removeGarbageObjectImpl(void *Object) {
38   if (Objects)
39     Objects->erase(Object);
40 }
41
42 void LeakDetector::addGarbageObjectImpl(const Value *Object) {
43   if (CachedValue) {
44     if (LLVMObjects == 0)
45       LLVMObjects = new std::set<const Value*>();
46     assert(LLVMObjects->count(CachedValue) == 0 && "Object already in set!");
47     LLVMObjects->insert(CachedValue);
48   }
49   CachedValue = Object;
50 }
51
52 void LeakDetector::removeGarbageObjectImpl(const Value *Object) {
53   if (Object == CachedValue)
54     CachedValue = 0;             // Cache hit!
55   else if (LLVMObjects)
56     LLVMObjects->erase(Object);
57 }
58
59 void LeakDetector::checkForGarbageImpl(const std::string &Message) {
60   if (CachedValue)  // Flush the cache to the set...
61     addGarbageObjectImpl((Value*)0);
62
63   assert(CachedValue == 0 && "No value should be cached anymore!");
64
65   if ((Objects && !Objects->empty()) || (LLVMObjects && !LLVMObjects->empty())){
66     std::cerr << "Leaked objects found: " << Message << "\n";
67
68     if (Objects && !Objects->empty()) {
69       std::cerr << "  Non-Value objects leaked:";
70       for (std::set<const void*>::iterator I = Objects->begin(),
71              E = Objects->end(); I != E; ++I)
72         std::cerr << " " << *I;
73     }
74
75     if (LLVMObjects && !LLVMObjects->empty()) {
76       std::cerr << "  LLVM Value subclasses leaked:";
77       for (std::set<const Value*>::iterator I = LLVMObjects->begin(),
78              E = LLVMObjects->end(); I != E; ++I)
79         std::cerr << **I << "\n";
80     }
81
82     std::cerr << "This is probably because you removed an LLVM value "
83               << "(Instruction, BasicBlock, \netc), but didn't delete it.  "
84               << "Please check your code for memory leaks.\n";
85
86     // Clear out results so we don't get duplicate warnings on next call...
87     delete Objects; delete LLVMObjects;
88     Objects = 0; LLVMObjects = 0;
89   }
90 }