2002-09-09 02:51:16 +08:00
|
|
|
//===-- LeakDetector.cpp - Implement LeakDetector interface ---------------===//
|
2005-04-22 07:48:37 +08:00
|
|
|
//
|
2003-10-21 03:43:21 +08:00
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-30 04:36:04 +08:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2005-04-22 07:48:37 +08:00
|
|
|
//
|
2003-10-21 03:43:21 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2002-09-09 02:51:16 +08:00
|
|
|
//
|
|
|
|
// This file implements the LeakDetector class.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2006-11-28 10:09:03 +08:00
|
|
|
#include "llvm/Support/LeakDetector.h"
|
2008-08-15 04:40:10 +08:00
|
|
|
#include "llvm/ADT/SmallPtrSet.h"
|
2006-12-07 09:30:32 +08:00
|
|
|
#include "llvm/Support/Compiler.h"
|
2009-06-18 05:56:05 +08:00
|
|
|
#include "llvm/Support/ManagedStatic.h"
|
2006-11-28 10:09:03 +08:00
|
|
|
#include "llvm/Support/Streams.h"
|
2009-06-18 05:56:05 +08:00
|
|
|
#include "llvm/System/RWMutex.h"
|
2009-06-19 00:54:52 +08:00
|
|
|
#include "llvm/System/Threading.h"
|
2002-09-09 02:51:16 +08:00
|
|
|
#include "llvm/Value.h"
|
2003-12-15 05:35:53 +08:00
|
|
|
using namespace llvm;
|
2003-11-12 06:41:34 +08:00
|
|
|
|
2004-02-15 07:33:39 +08:00
|
|
|
namespace {
|
2004-07-15 09:29:12 +08:00
|
|
|
template <class T>
|
2006-06-29 06:57:00 +08:00
|
|
|
struct VISIBILITY_HIDDEN PrinterTrait {
|
2006-12-07 09:30:32 +08:00
|
|
|
static void print(const T* P) { cerr << P; }
|
2004-07-15 09:29:12 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
template<>
|
2006-06-29 06:57:00 +08:00
|
|
|
struct VISIBILITY_HIDDEN PrinterTrait<Value> {
|
2006-12-07 09:30:32 +08:00
|
|
|
static void print(const Value* P) { cerr << *P; }
|
2004-07-15 09:29:12 +08:00
|
|
|
};
|
|
|
|
|
2009-06-19 05:35:56 +08:00
|
|
|
ManagedStatic<sys::SmartRWMutex<true> > LeakDetectorLock;
|
2009-06-18 05:56:05 +08:00
|
|
|
|
2004-02-15 07:33:39 +08:00
|
|
|
template <typename T>
|
2006-06-29 06:57:00 +08:00
|
|
|
struct VISIBILITY_HIDDEN LeakDetectorImpl {
|
2009-06-18 05:56:05 +08:00
|
|
|
explicit LeakDetectorImpl(const char* const name = "") :
|
|
|
|
Cache(0), Name(name) { }
|
2002-09-09 02:51:16 +08:00
|
|
|
|
2009-06-18 05:56:05 +08:00
|
|
|
void clear() {
|
|
|
|
Cache = 0;
|
|
|
|
Ts.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
void setName(const char* n) {
|
|
|
|
Name = n;
|
|
|
|
}
|
|
|
|
|
2004-02-15 07:33:39 +08:00
|
|
|
// Because the most common usage pattern, by far, is to add a
|
|
|
|
// garbage object, then remove it immediately, we optimize this
|
|
|
|
// case. When an object is added, it is not added to the set
|
|
|
|
// immediately, it is added to the CachedValue Value. If it is
|
|
|
|
// immediately removed, no set search need be performed.
|
|
|
|
void addGarbage(const T* o) {
|
2009-07-08 02:33:04 +08:00
|
|
|
sys::SmartScopedWriter<true> Writer(*LeakDetectorLock);
|
2009-06-19 05:35:56 +08:00
|
|
|
if (Cache) {
|
|
|
|
assert(Ts.count(Cache) == 0 && "Object already in set!");
|
|
|
|
Ts.insert(Cache);
|
2004-02-15 07:33:39 +08:00
|
|
|
}
|
2009-06-19 05:35:56 +08:00
|
|
|
Cache = o;
|
2004-02-15 07:33:39 +08:00
|
|
|
}
|
2002-09-09 02:51:16 +08:00
|
|
|
|
2004-02-15 07:33:39 +08:00
|
|
|
void removeGarbage(const T* o) {
|
2009-07-08 02:33:04 +08:00
|
|
|
sys::SmartScopedWriter<true> Writer(*LeakDetectorLock);
|
2009-06-19 05:35:56 +08:00
|
|
|
if (o == Cache)
|
|
|
|
Cache = 0; // Cache hit
|
|
|
|
else
|
|
|
|
Ts.erase(o);
|
2004-02-15 07:33:39 +08:00
|
|
|
}
|
2002-09-09 02:51:16 +08:00
|
|
|
|
2004-02-15 07:33:39 +08:00
|
|
|
bool hasGarbage(const std::string& Message) {
|
|
|
|
addGarbage(0); // Flush the Cache
|
2002-09-20 03:22:11 +08:00
|
|
|
|
2009-07-08 02:33:04 +08:00
|
|
|
sys::SmartScopedReader<true> Reader(*LeakDetectorLock);
|
2004-02-15 07:33:39 +08:00
|
|
|
assert(Cache == 0 && "No value should be cached anymore!");
|
2002-09-20 03:22:11 +08:00
|
|
|
|
2004-02-15 07:33:39 +08:00
|
|
|
if (!Ts.empty()) {
|
2006-12-07 09:30:32 +08:00
|
|
|
cerr << "Leaked " << Name << " objects found: " << Message << ":\n";
|
2008-08-15 04:40:10 +08:00
|
|
|
for (typename SmallPtrSet<const T*, 8>::iterator I = Ts.begin(),
|
2004-07-15 09:29:12 +08:00
|
|
|
E = Ts.end(); I != E; ++I) {
|
2006-12-07 09:30:32 +08:00
|
|
|
cerr << "\t";
|
2004-07-15 09:29:12 +08:00
|
|
|
PrinterTrait<T>::print(*I);
|
2006-12-07 09:30:32 +08:00
|
|
|
cerr << "\n";
|
2004-07-15 09:29:12 +08:00
|
|
|
}
|
2006-12-07 09:30:32 +08:00
|
|
|
cerr << '\n';
|
2002-09-09 02:51:16 +08:00
|
|
|
|
2004-02-15 07:33:39 +08:00
|
|
|
return true;
|
|
|
|
}
|
2009-06-18 05:56:05 +08:00
|
|
|
|
2004-02-15 07:33:39 +08:00
|
|
|
return false;
|
2002-09-09 02:51:16 +08:00
|
|
|
}
|
|
|
|
|
2004-02-15 07:33:39 +08:00
|
|
|
private:
|
2008-08-15 04:40:10 +08:00
|
|
|
SmallPtrSet<const T*, 8> Ts;
|
2004-02-16 07:33:48 +08:00
|
|
|
const T* Cache;
|
2009-06-18 05:56:05 +08:00
|
|
|
const char* Name;
|
2004-02-15 07:33:39 +08:00
|
|
|
};
|
|
|
|
|
2009-06-18 05:56:05 +08:00
|
|
|
static ManagedStatic<LeakDetectorImpl<void> > Objects;
|
|
|
|
static ManagedStatic<LeakDetectorImpl<Value> > LLVMObjects;
|
2004-11-20 01:09:48 +08:00
|
|
|
|
2008-04-11 05:11:47 +08:00
|
|
|
static void clearGarbage() {
|
2009-06-18 05:56:05 +08:00
|
|
|
Objects->clear();
|
|
|
|
LLVMObjects->clear();
|
2002-09-09 02:51:16 +08:00
|
|
|
}
|
|
|
|
}
|
2004-02-15 07:33:39 +08:00
|
|
|
|
|
|
|
void LeakDetector::addGarbageObjectImpl(void *Object) {
|
2009-06-18 05:56:05 +08:00
|
|
|
Objects->addGarbage(Object);
|
2004-02-15 07:33:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void LeakDetector::addGarbageObjectImpl(const Value *Object) {
|
2009-06-18 05:56:05 +08:00
|
|
|
LLVMObjects->addGarbage(Object);
|
2004-02-15 07:33:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void LeakDetector::removeGarbageObjectImpl(void *Object) {
|
2009-06-18 05:56:05 +08:00
|
|
|
Objects->removeGarbage(Object);
|
2004-02-15 07:33:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void LeakDetector::removeGarbageObjectImpl(const Value *Object) {
|
2009-06-18 05:56:05 +08:00
|
|
|
LLVMObjects->removeGarbage(Object);
|
2004-02-15 07:33:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void LeakDetector::checkForGarbageImpl(const std::string &Message) {
|
2009-06-18 05:56:05 +08:00
|
|
|
Objects->setName("GENERIC");
|
|
|
|
LLVMObjects->setName("LLVM");
|
|
|
|
|
2004-02-15 07:33:39 +08:00
|
|
|
// use non-short-circuit version so that both checks are performed
|
2009-06-18 05:56:05 +08:00
|
|
|
if (Objects->hasGarbage(Message) |
|
|
|
|
LLVMObjects->hasGarbage(Message))
|
2006-12-07 09:30:32 +08:00
|
|
|
cerr << "\nThis is probably because you removed an object, but didn't "
|
|
|
|
<< "delete it. Please check your code for memory leaks.\n";
|
2004-11-20 01:09:48 +08:00
|
|
|
|
|
|
|
// Clear out results so we don't get duplicate warnings on
|
|
|
|
// next call...
|
|
|
|
clearGarbage();
|
2004-02-15 07:33:39 +08:00
|
|
|
}
|