stop leaking all named struct types with an empty name. Thanks
authorChris Lattner <sabre@nondot.org>
Wed, 13 Jul 2011 04:22:39 +0000 (04:22 +0000)
committerChris Lattner <sabre@nondot.org>
Wed, 13 Jul 2011 04:22:39 +0000 (04:22 +0000)
to Benjamin Kramer for steering me in the right direction here.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@135031 91177308-0d34-0410-b5e6-96231b3b80d8

lib/VMCore/LLVMContextImpl.cpp
lib/VMCore/LLVMContextImpl.h
lib/VMCore/Type.cpp

index 12cb2464b53b2f48c2b9ea1dbe1949bd8d16f0b8..2b6bb39167afced5ff674781b91fec755f7303d1 100644 (file)
@@ -81,14 +81,12 @@ LLVMContextImpl::~LLVMContextImpl() {
   SmallVector<MDNode*, 8> MDNodes;
   MDNodes.reserve(MDNodeSet.size() + NonUniquedMDNodes.size());
   for (FoldingSetIterator<MDNode> I = MDNodeSet.begin(), E = MDNodeSet.end();
-       I != E; ++I) {
+       I != E; ++I)
     MDNodes.push_back(&*I);
-  }
   MDNodes.append(NonUniquedMDNodes.begin(), NonUniquedMDNodes.end());
   for (SmallVectorImpl<MDNode *>::iterator I = MDNodes.begin(),
-         E = MDNodes.end(); I != E; ++I) {
+         E = MDNodes.end(); I != E; ++I)
     (*I)->destroy();
-  }
   assert(MDNodeSet.empty() && NonUniquedMDNodes.empty() &&
          "Destroying all MDNodes didn't empty the Context's sets.");
   // Destroy MDStrings.
@@ -103,7 +101,10 @@ LLVMContextImpl::~LLVMContextImpl() {
   DeleteContainerSeconds(PointerTypes);
   DeleteContainerSeconds(ASPointerTypes);
 
-  for (StringMap<StructType *>::iterator I = NamedStructTypes.begin(), E = NamedStructTypes.end(); I != E; ++I) {
+  for (StringMap<StructType *>::iterator I = NamedStructTypes.begin(),
+       E = NamedStructTypes.end(); I != E; ++I)
     delete I->getValue();
-  }
+  for (SmallPtrSet<StructType*, 16>::iterator I = EmptyNamedStructTypes.begin(),
+       E = EmptyNamedStructTypes.end(); I != E; ++I)
+    delete *I;
 }
index e36864b27b5d0f6cf5fdbc4360a44e76f81c790b..b26068d85f7035a81edaecbeb2c48e6e9e2676ad 100644 (file)
@@ -179,6 +179,7 @@ public:
   std::map<std::vector<Type*>, FunctionType*> FunctionTypes;
   std::map<std::vector<Type*>, StructType*> AnonStructTypes;
   StringMap<StructType*> NamedStructTypes;
+  SmallPtrSet<StructType*, 16> EmptyNamedStructTypes;
   unsigned NamedStructTypesUniqueID;
     
   DenseMap<std::pair<Type *, uint64_t>, ArrayType*> ArrayTypes;
index 10467a8d90088e0dcbc84ee7ae7736ea1e426d6f..dc5053acc24ca85480eed4478f958c8b4b8ce872 100644 (file)
@@ -412,7 +412,10 @@ void StructType::setBody(ArrayRef<Type*> Elements, bool isPacked) {
 
 StructType *StructType::createNamed(LLVMContext &Context, StringRef Name) {
   StructType *ST = new StructType(Context);
-  ST->setName(Name);
+  if (!Name.empty())
+    ST->setName(Name);
+  else
+    Context.pImpl->EmptyNamedStructTypes.insert(ST);
   return ST;
 }
 
@@ -423,11 +426,16 @@ void StructType::setName(StringRef Name) {
   if (SymbolTableEntry) {
     getContext().pImpl->NamedStructTypes.erase(getName());
     SymbolTableEntry = 0;
+  } else {
+    getContext().pImpl->EmptyNamedStructTypes.erase(this);
   }
   
   // If this is just removing the name, we're done.
-  if (Name.empty())
+  if (Name.empty()) {
+    // Keep track of types with no names so we can free them.
+    getContext().pImpl->EmptyNamedStructTypes.insert(this);
     return;
+  }
   
   // Look up the entry for the name.
   StringMapEntry<StructType*> *Entry =