[llvm-commits] CVS: llvm/lib/VMCore/Type.cpp Constants.cpp

Chris Lattner lattner at cs.uiuc.edu
Fri Nov 19 08:39:57 PST 2004



Changes in directory llvm/lib/VMCore:

Type.cpp updated: 1.118 -> 1.119
Constants.cpp updated: 1.111 -> 1.112
---
Log message:

Add hooks to free all memory allocated by the singleton factories in these
files.  Patch contributed by Morten Ofstad!


---
Diffs of the changes:  (+68 -0)

Index: llvm/lib/VMCore/Type.cpp
diff -u llvm/lib/VMCore/Type.cpp:1.118 llvm/lib/VMCore/Type.cpp:1.119
--- llvm/lib/VMCore/Type.cpp:1.118	Tue Nov 16 14:39:04 2004
+++ llvm/lib/VMCore/Type.cpp	Fri Nov 19 10:39:44 2004
@@ -656,6 +656,17 @@
   /// this map.
   ///
   std::multimap<unsigned, PATypeHolder> TypesByHash;
+
+  friend void Type::clearAllTypeMaps();
+
+private:
+  void clear(std::vector<Type *> &DerivedTypes) {
+    for (typename std::map<ValType, PATypeHolder>::iterator I = Map.begin(), 
+         E = Map.end(); I != E; ++I)
+      DerivedTypes.push_back(I->second.get());
+    TypesByHash.clear();
+    Map.clear();
+  }
 public:
   typedef typename std::map<ValType, PATypeHolder>::iterator iterator;
   ~TypeMap() { print("ON EXIT"); }
@@ -1301,4 +1312,25 @@
 }
 }
 
+/// clearAllTypeMaps - This method frees all internal memory used by the
+/// type subsystem, which can be used in environments where this memory is
+/// otherwise reported as a leak.
+void Type::clearAllTypeMaps() {
+  std::vector<Type *> DerivedTypes;
+
+  FunctionTypes.clear(DerivedTypes);
+  PointerTypes.clear(DerivedTypes);
+  StructTypes.clear(DerivedTypes);
+  ArrayTypes.clear(DerivedTypes);
+  PackedTypes.clear(DerivedTypes);
+
+  for(std::vector<Type *>::iterator I = DerivedTypes.begin(), 
+      E = DerivedTypes.end(); I != E; ++I)
+    (*I)->ContainedTys.clear();
+  for(std::vector<Type *>::iterator I = DerivedTypes.begin(),
+      E = DerivedTypes.end(); I != E; ++I)
+    delete *I;
+  DerivedTypes.clear();
+}
+
 // vim: sw=2


Index: llvm/lib/VMCore/Constants.cpp
diff -u llvm/lib/VMCore/Constants.cpp:1.111 llvm/lib/VMCore/Constants.cpp:1.112
--- llvm/lib/VMCore/Constants.cpp:1.111	Thu Oct 28 01:43:38 2004
+++ llvm/lib/VMCore/Constants.cpp	Fri Nov 19 10:39:44 2004
@@ -612,6 +612,16 @@
 
     typedef std::map<const TypeClass*, MapIterator> AbstractTypeMapTy;
     AbstractTypeMapTy AbstractTypeMap;
+
+    friend void Constant::clearAllValueMaps();
+  private:
+    void clear(std::vector<Constant *> &Constants) {
+      for(MapIterator I = Map.begin(); I != Map.end(); ++I)
+        Constants.push_back(I->second);
+      Map.clear();
+      AbstractTypeMap.clear();
+    }
+
   public:
     // getOrCreate - Return the specified constant from the map, creating it if
     // necessary.
@@ -1401,3 +1411,29 @@
   return Instruction::getOpcodeName(getOpcode());
 }
 
+/// clearAllValueMaps - This method frees all internal memory used by the
+/// constant subsystem, which can be used in environments where this memory
+/// is otherwise reported as a leak.
+void Constant::clearAllValueMaps() {
+  std::vector<Constant *> Constants;
+
+  DoubleConstants.clear(Constants);
+  FloatConstants.clear(Constants);
+  SIntConstants.clear(Constants);
+  UIntConstants.clear(Constants);
+  AggZeroConstants.clear(Constants);
+  ArrayConstants.clear(Constants);
+  StructConstants.clear(Constants);
+  PackedConstants.clear(Constants);
+  NullPtrConstants.clear(Constants);
+  UndefValueConstants.clear(Constants);
+  ExprConstants.clear(Constants);
+
+  for (std::vector<Constant *>::iterator I = Constants.begin(), 
+       E = Constants.end(); I != E; ++I)
+    (*I)->dropAllReferences();
+  for (std::vector<Constant *>::iterator I = Constants.begin(),
+       E = Constants.end(); I != E; ++I)
+    (*I)->destroyConstantImpl();
+  Constants.clear();
+}






More information about the llvm-commits mailing list