LLVM API Documentation
00001 //===-- LeakDetector.cpp - Implement LeakDetector interface ---------------===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This file implements the LeakDetector class. 00011 // 00012 //===----------------------------------------------------------------------===// 00013 00014 #include "LLVMContextImpl.h" 00015 #include "llvm/Support/LeakDetector.h" 00016 #include "llvm/ADT/SmallPtrSet.h" 00017 #include "llvm/Support/Compiler.h" 00018 #include "llvm/Support/ManagedStatic.h" 00019 #include "llvm/Support/Streams.h" 00020 #include "llvm/System/Mutex.h" 00021 #include "llvm/System/Threading.h" 00022 #include "llvm/Value.h" 00023 using namespace llvm; 00024 00025 static ManagedStatic<sys::SmartMutex<true> > ObjectsLock; 00026 static ManagedStatic<LeakDetectorImpl<void> > Objects; 00027 00028 static void clearGarbage(LLVMContext &Context) { 00029 Objects->clear(); 00030 Context.pImpl->LLVMObjects.clear(); 00031 } 00032 00033 void LeakDetector::addGarbageObjectImpl(void *Object) { 00034 sys::SmartScopedLock<true> Lock(*ObjectsLock); 00035 Objects->addGarbage(Object); 00036 } 00037 00038 void LeakDetector::addGarbageObjectImpl(const Value *Object) { 00039 LLVMContextImpl *pImpl = Object->getContext().pImpl; 00040 sys::SmartScopedLock<true> Lock(pImpl->LLVMObjectsLock); 00041 pImpl->LLVMObjects.addGarbage(Object); 00042 } 00043 00044 void LeakDetector::removeGarbageObjectImpl(void *Object) { 00045 sys::SmartScopedLock<true> Lock(*ObjectsLock); 00046 Objects->removeGarbage(Object); 00047 } 00048 00049 void LeakDetector::removeGarbageObjectImpl(const Value *Object) { 00050 LLVMContextImpl *pImpl = Object->getContext().pImpl; 00051 sys::SmartScopedLock<true> Lock(pImpl->LLVMObjectsLock); 00052 pImpl->LLVMObjects.removeGarbage(Object); 00053 } 00054 00055 void LeakDetector::checkForGarbageImpl(LLVMContext &Context, 00056 const std::string &Message) { 00057 LLVMContextImpl *pImpl = Context.pImpl; 00058 sys::SmartScopedLock<true> Lock(*ObjectsLock); 00059 sys::SmartScopedLock<true> CLock(pImpl->LLVMObjectsLock); 00060 00061 Objects->setName("GENERIC"); 00062 pImpl->LLVMObjects.setName("LLVM"); 00063 00064 // use non-short-circuit version so that both checks are performed 00065 if (Objects->hasGarbage(Message) | 00066 pImpl->LLVMObjects.hasGarbage(Message)) 00067 cerr << "\nThis is probably because you removed an object, but didn't " 00068 << "delete it. Please check your code for memory leaks.\n"; 00069 00070 // Clear out results so we don't get duplicate warnings on 00071 // next call... 00072 clearGarbage(Context); 00073 }