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