llvm.org GIT mirror llvm / 30268be
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. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@113374 91177308-0d34-0410-b5e6-96231b3b80d8 Owen Anderson 9 years ago
5 changed file(s) with 32 addition(s) and 0 deletion(s). Raw diff Collapse all Expand all
1919 class LLVMContextImpl;
2020 class StringRef;
2121 class Instruction;
22 class Module;
2223 template class SmallVectorImpl;
2324
2425 /// This is an important class for using LLVM in a threaded context. It
3536 LLVMContextImpl *const pImpl;
3637 LLVMContext();
3738 ~LLVMContext();
39
40 /// addModule - Register a module as being instantiated in this context. If
41 /// the context is deleted, the module will be deleted as well.
42 void addModule(Module*);
43
44 /// removeModule - Unregister a module from this context.
45 void removeModule(Module*);
3846
3947 // Pinned metadata names, which always have the same value. This is a
4048 // compile-time performance optimization, not a correctness optimization.
3232 assert(DbgID == MD_dbg && "dbg kind id drifted"); (void)DbgID;
3333 }
3434 LLVMContext::~LLVMContext() { delete pImpl; }
35
36 void LLVMContext::addModule(Module *M) {
37 pImpl->OwnedModules.insert(M);
38 }
39
40 void LLVMContext::removeModule(Module *M) {
41 pImpl->OwnedModules.erase(M);
42 }
3543
3644 //===----------------------------------------------------------------------===//
3745 // Recoverable Backend Errors
1111 //===----------------------------------------------------------------------===//
1212
1313 #include "LLVMContextImpl.h"
14 #include "llvm/Module.h"
1415 #include
1516 using namespace llvm;
1617
5051 }
5152
5253 LLVMContextImpl::~LLVMContextImpl() {
54 // NOTE: We need to delete the contents of OwnedModules, but we have to
55 // duplicate it into a temporary vector, because the destructor of Module
56 // will try to remove itself from OwnedModules set. This would cause
57 // iterator invalidation if we iterated on the set directly.
58 std::vector Modules(OwnedModules.begin(), OwnedModules.end());
59 for (std::vector::iterator I = Modules.begin(), E = Modules.end();
60 I != E; ++I)
61 delete *I;
62
5363 std::for_each(ExprConstants.map_begin(), ExprConstants.map_end(),
5464 DropReferences());
5565 std::for_each(ArrayConstants.map_begin(), ArrayConstants.map_end(),
114114
115115 class LLVMContextImpl {
116116 public:
117 /// OwnedModules - The set of modules instantiated in this context, and which
118 /// will be automatically deleted if this context is deleted.
119 SmallPtrSet OwnedModules;
120
117121 void *InlineAsmDiagHandler, *InlineAsmDiagContext;
118122
119123 typedef DenseMap
6161 ValSymTab = new ValueSymbolTable();
6262 TypeSymTab = new TypeSymbolTable();
6363 NamedMDSymTab = new StringMap();
64 Context.addModule(this);
6465 }
6566
6667 Module::~Module() {
68 Context.removeModule(this);
6769 dropAllReferences();
6870 GlobalList.clear();
6971 FunctionList.clear();