diff options
| author | Owen Anderson <resistor@mac.com> | 2010-09-08 18:03:32 +0000 |
|---|---|---|
| committer | Owen Anderson <resistor@mac.com> | 2010-09-08 18:03:32 +0000 |
| commit | 8e89e41fafaa372175b7f5b1676b2a20916b88d7 (patch) | |
| tree | 02514e3a3aaca3d90b184ff55bdd94652ebb877c | |
| parent | 10b9b7b4bdeaa5e714542072092a61e6999af830 (diff) | |
| download | bcm5719-llvm-8e89e41fafaa372175b7f5b1676b2a20916b88d7.tar.gz bcm5719-llvm-8e89e41fafaa372175b7f5b1676b2a20916b88d7.zip | |
Clarify the ownership model of LLVMContext and Module. Namely, contexts own
modules are instantiated in them. If the context is deleted, all of its owned
modules are also deleted.
llvm-svn: 113374
| -rw-r--r-- | llvm/include/llvm/LLVMContext.h | 8 | ||||
| -rw-r--r-- | llvm/lib/VMCore/LLVMContext.cpp | 8 | ||||
| -rw-r--r-- | llvm/lib/VMCore/LLVMContextImpl.cpp | 10 | ||||
| -rw-r--r-- | llvm/lib/VMCore/LLVMContextImpl.h | 4 | ||||
| -rw-r--r-- | llvm/lib/VMCore/Module.cpp | 2 |
5 files changed, 32 insertions, 0 deletions
diff --git a/llvm/include/llvm/LLVMContext.h b/llvm/include/llvm/LLVMContext.h index 7cb6579aef6..76492b7102e 100644 --- a/llvm/include/llvm/LLVMContext.h +++ b/llvm/include/llvm/LLVMContext.h @@ -20,6 +20,7 @@ namespace llvm { class LLVMContextImpl; class StringRef; class Instruction; +class Module; template <typename T> class SmallVectorImpl; /// This is an important class for using LLVM in a threaded context. It @@ -37,6 +38,13 @@ public: LLVMContext(); ~LLVMContext(); + /// addModule - Register a module as being instantiated in this context. If + /// the context is deleted, the module will be deleted as well. + void addModule(Module*); + + /// removeModule - Unregister a module from this context. + void removeModule(Module*); + // Pinned metadata names, which always have the same value. This is a // compile-time performance optimization, not a correctness optimization. enum { diff --git a/llvm/lib/VMCore/LLVMContext.cpp b/llvm/lib/VMCore/LLVMContext.cpp index 563c651315a..60fb830e9b5 100644 --- a/llvm/lib/VMCore/LLVMContext.cpp +++ b/llvm/lib/VMCore/LLVMContext.cpp @@ -34,6 +34,14 @@ LLVMContext::LLVMContext() : pImpl(new LLVMContextImpl(*this)) { } LLVMContext::~LLVMContext() { delete pImpl; } +void LLVMContext::addModule(Module *M) { + pImpl->OwnedModules.insert(M); +} + +void LLVMContext::removeModule(Module *M) { + pImpl->OwnedModules.erase(M); +} + //===----------------------------------------------------------------------===// // Recoverable Backend Errors //===----------------------------------------------------------------------===// diff --git a/llvm/lib/VMCore/LLVMContextImpl.cpp b/llvm/lib/VMCore/LLVMContextImpl.cpp index 93a075f0fcc..610c5027c34 100644 --- a/llvm/lib/VMCore/LLVMContextImpl.cpp +++ b/llvm/lib/VMCore/LLVMContextImpl.cpp @@ -12,6 +12,7 @@ //===----------------------------------------------------------------------===// #include "LLVMContextImpl.h" +#include "llvm/Module.h" #include <algorithm> using namespace llvm; @@ -51,6 +52,15 @@ struct DropReferences { } LLVMContextImpl::~LLVMContextImpl() { + // NOTE: We need to delete the contents of OwnedModules, but we have to + // duplicate it into a temporary vector, because the destructor of Module + // will try to remove itself from OwnedModules set. This would cause + // iterator invalidation if we iterated on the set directly. + std::vector<Module*> Modules(OwnedModules.begin(), OwnedModules.end()); + for (std::vector<Module*>::iterator I = Modules.begin(), E = Modules.end(); + I != E; ++I) + delete *I; + std::for_each(ExprConstants.map_begin(), ExprConstants.map_end(), DropReferences()); std::for_each(ArrayConstants.map_begin(), ArrayConstants.map_end(), diff --git a/llvm/lib/VMCore/LLVMContextImpl.h b/llvm/lib/VMCore/LLVMContextImpl.h index 51b2992898c..6df804ab084 100644 --- a/llvm/lib/VMCore/LLVMContextImpl.h +++ b/llvm/lib/VMCore/LLVMContextImpl.h @@ -115,6 +115,10 @@ public: class LLVMContextImpl { public: + /// OwnedModules - The set of modules instantiated in this context, and which + /// will be automatically deleted if this context is deleted. + SmallPtrSet<Module*, 4> OwnedModules; + void *InlineAsmDiagHandler, *InlineAsmDiagContext; typedef DenseMap<DenseMapAPIntKeyInfo::KeyTy, ConstantInt*, diff --git a/llvm/lib/VMCore/Module.cpp b/llvm/lib/VMCore/Module.cpp index d7ddf96cb07..341e527acb5 100644 --- a/llvm/lib/VMCore/Module.cpp +++ b/llvm/lib/VMCore/Module.cpp @@ -62,9 +62,11 @@ Module::Module(StringRef MID, LLVMContext& C) ValSymTab = new ValueSymbolTable(); TypeSymTab = new TypeSymbolTable(); NamedMDSymTab = new StringMap<NamedMDNode *>(); + Context.addModule(this); } Module::~Module() { + Context.removeModule(this); dropAllReferences(); GlobalList.clear(); FunctionList.clear(); |

