From 99a669b1106f7078dc7a3e6937f5157bf5174446 Mon Sep 17 00:00:00 2001 From: Chris Lattner Date: Fri, 19 Nov 2004 16:39:44 +0000 Subject: [PATCH] Add hooks to free all memory allocated by the singleton factories in these files. Patch contributed by Morten Ofstad! llvm-svn: 17995 --- llvm/lib/VMCore/Constants.cpp | 36 +++++++++++++++++++++++++++++++++++ llvm/lib/VMCore/Type.cpp | 32 +++++++++++++++++++++++++++++++ 2 files changed, 68 insertions(+) diff --git a/llvm/lib/VMCore/Constants.cpp b/llvm/lib/VMCore/Constants.cpp index b5831ae26ea9..97e4fa565c7e 100644 --- a/llvm/lib/VMCore/Constants.cpp +++ b/llvm/lib/VMCore/Constants.cpp @@ -612,6 +612,16 @@ namespace { typedef std::map AbstractTypeMapTy; AbstractTypeMapTy AbstractTypeMap; + + friend void Constant::clearAllValueMaps(); + private: + void clear(std::vector &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 @@ const char *ConstantExpr::getOpcodeName() const { 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 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::iterator I = Constants.begin(), + E = Constants.end(); I != E; ++I) + (*I)->dropAllReferences(); + for (std::vector::iterator I = Constants.begin(), + E = Constants.end(); I != E; ++I) + (*I)->destroyConstantImpl(); + Constants.clear(); +} diff --git a/llvm/lib/VMCore/Type.cpp b/llvm/lib/VMCore/Type.cpp index 3f91f982c822..48fbb50f339a 100644 --- a/llvm/lib/VMCore/Type.cpp +++ b/llvm/lib/VMCore/Type.cpp @@ -656,6 +656,17 @@ class TypeMap { /// this map. /// std::multimap TypesByHash; + + friend void Type::clearAllTypeMaps(); + +private: + void clear(std::vector &DerivedTypes) { + for (typename std::map::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::iterator iterator; ~TypeMap() { print("ON EXIT"); } @@ -1301,4 +1312,25 @@ std::ostream &operator<<(std::ostream &OS, const Type &T) { } } +/// 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 DerivedTypes; + + FunctionTypes.clear(DerivedTypes); + PointerTypes.clear(DerivedTypes); + StructTypes.clear(DerivedTypes); + ArrayTypes.clear(DerivedTypes); + PackedTypes.clear(DerivedTypes); + + for(std::vector::iterator I = DerivedTypes.begin(), + E = DerivedTypes.end(); I != E; ++I) + (*I)->ContainedTys.clear(); + for(std::vector::iterator I = DerivedTypes.begin(), + E = DerivedTypes.end(); I != E; ++I) + delete *I; + DerivedTypes.clear(); +} + // vim: sw=2