opnsense-src/contrib/llvm/lib/VMCore/LeakDetector.cpp

70 lines
2.3 KiB
C++
Raw Normal View History

2009-06-02 13:52:33 -04:00
//===-- LeakDetector.cpp - Implement LeakDetector interface ---------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This file implements the LeakDetector class.
//
//===----------------------------------------------------------------------===//
2009-10-14 13:57:32 -04:00
#include "LLVMContextImpl.h"
2009-06-02 13:52:33 -04:00
#include "llvm/Support/LeakDetector.h"
#include "llvm/ADT/SmallPtrSet.h"
#include "llvm/Support/Compiler.h"
2009-06-22 04:08:12 -04:00
#include "llvm/Support/ManagedStatic.h"
#include "llvm/Support/Mutex.h"
#include "llvm/Support/Threading.h"
2009-06-02 13:52:33 -04:00
#include "llvm/Value.h"
using namespace llvm;
2009-10-14 13:57:32 -04:00
static ManagedStatic<sys::SmartMutex<true> > ObjectsLock;
static ManagedStatic<LeakDetectorImpl<void> > Objects;
2009-06-02 13:52:33 -04:00
2009-10-14 13:57:32 -04:00
static void clearGarbage(LLVMContext &Context) {
Objects->clear();
Context.pImpl->LLVMObjects.clear();
2009-06-02 13:52:33 -04:00
}
void LeakDetector::addGarbageObjectImpl(void *Object) {
2009-10-14 13:57:32 -04:00
sys::SmartScopedLock<true> Lock(*ObjectsLock);
2009-06-22 04:08:12 -04:00
Objects->addGarbage(Object);
2009-06-02 13:52:33 -04:00
}
void LeakDetector::addGarbageObjectImpl(const Value *Object) {
2009-10-14 13:57:32 -04:00
LLVMContextImpl *pImpl = Object->getContext().pImpl;
pImpl->LLVMObjects.addGarbage(Object);
2009-06-02 13:52:33 -04:00
}
void LeakDetector::removeGarbageObjectImpl(void *Object) {
2009-10-14 13:57:32 -04:00
sys::SmartScopedLock<true> Lock(*ObjectsLock);
2009-06-22 04:08:12 -04:00
Objects->removeGarbage(Object);
2009-06-02 13:52:33 -04:00
}
void LeakDetector::removeGarbageObjectImpl(const Value *Object) {
2009-10-14 13:57:32 -04:00
LLVMContextImpl *pImpl = Object->getContext().pImpl;
pImpl->LLVMObjects.removeGarbage(Object);
2009-06-02 13:52:33 -04:00
}
2009-10-14 13:57:32 -04:00
void LeakDetector::checkForGarbageImpl(LLVMContext &Context,
const std::string &Message) {
LLVMContextImpl *pImpl = Context.pImpl;
sys::SmartScopedLock<true> Lock(*ObjectsLock);
2009-06-22 04:08:12 -04:00
Objects->setName("GENERIC");
2009-10-14 13:57:32 -04:00
pImpl->LLVMObjects.setName("LLVM");
2009-06-22 04:08:12 -04:00
2009-06-02 13:52:33 -04:00
// use non-short-circuit version so that both checks are performed
2009-06-22 04:08:12 -04:00
if (Objects->hasGarbage(Message) |
2009-10-14 13:57:32 -04:00
pImpl->LLVMObjects.hasGarbage(Message))
errs() << "\nThis is probably because you removed an object, but didn't "
<< "delete it. Please check your code for memory leaks.\n";
2009-06-02 13:52:33 -04:00
// Clear out results so we don't get duplicate warnings on
// next call...
2009-10-14 13:57:32 -04:00
clearGarbage(Context);
2009-06-02 13:52:33 -04:00
}