From 1b10191d18733d08b2d363bc43632652f73a9003 Mon Sep 17 00:00:00 2001 From: Chris Lattner Date: Mon, 15 Mar 2010 06:15:35 +0000 Subject: [PATCH] fix a memory leak yjasskin pointed out: MCSymbol is bump pointer allocated and thus not freed. This is cool except that it contains and std::string so the string data didn't get freed. In any case there is no reason to redundantly store the string data in the MCSymbol anyway, just make the MCSymbol ref the string data in the MCContext StringMap. llvm-svn: 98536 --- include/llvm/MC/MCContext.h | 4 ++-- include/llvm/MC/MCSymbol.h | 13 ++++++------- lib/MC/MCContext.cpp | 24 ++++++++++++++---------- 3 files changed, 22 insertions(+), 19 deletions(-) diff --git a/include/llvm/MC/MCContext.h b/include/llvm/MC/MCContext.h index c233fd2246f..85114e30995 100644 --- a/include/llvm/MC/MCContext.h +++ b/include/llvm/MC/MCContext.h @@ -65,8 +65,8 @@ namespace llvm { /// reference and return it. /// /// @param Name - The symbol name, which must be unique across all symbols. - MCSymbol *GetOrCreateSymbol(StringRef Name); - MCSymbol *GetOrCreateSymbol(const Twine &Name); + MCSymbol *GetOrCreateSymbol(StringRef Name, bool isTemporary = false); + MCSymbol *GetOrCreateSymbol(const Twine &Name, bool isTemporary = false); /// GetOrCreateTemporarySymbol - Create a new assembler temporary symbol /// with the specified @p Name if it doesn't exist or return the existing diff --git a/include/llvm/MC/MCSymbol.h b/include/llvm/MC/MCSymbol.h index d5c4d95c183..e41eb2ab885 100644 --- a/include/llvm/MC/MCSymbol.h +++ b/include/llvm/MC/MCSymbol.h @@ -14,9 +14,7 @@ #ifndef LLVM_MC_MCSYMBOL_H #define LLVM_MC_MCSYMBOL_H -#include #include "llvm/ADT/StringRef.h" -#include "llvm/System/DataTypes.h" namespace llvm { class MCExpr; @@ -38,8 +36,9 @@ namespace llvm { // FIXME: Use a PointerInt wrapper for this? static const MCSection *AbsolutePseudoSection; - /// Name - The name of the symbol. - std::string Name; + /// Name - The name of the symbol. The referred-to string data is actually + /// held by the StringMap that lives in MCContext. + StringRef Name; /// Section - The section the symbol is defined in. This is null for /// undefined symbols, and the special AbsolutePseudoSection value for @@ -56,14 +55,14 @@ namespace llvm { private: // MCContext creates and uniques these. friend class MCContext; - MCSymbol(StringRef _Name, bool _IsTemporary) - : Name(_Name), Section(0), Value(0), IsTemporary(_IsTemporary) {} + MCSymbol(StringRef name, bool isTemporary) + : Name(name), Section(0), Value(0), IsTemporary(isTemporary) {} MCSymbol(const MCSymbol&); // DO NOT IMPLEMENT void operator=(const MCSymbol&); // DO NOT IMPLEMENT public: /// getName - Get the symbol name. - const std::string &getName() const { return Name; } + StringRef getName() const { return Name; } /// @name Symbol Type /// @{ diff --git a/lib/MC/MCContext.cpp b/lib/MC/MCContext.cpp index ae9a30a95f4..70c89a2333a 100644 --- a/lib/MC/MCContext.cpp +++ b/lib/MC/MCContext.cpp @@ -23,18 +23,25 @@ MCContext::~MCContext() { // we don't need to free them here. } -MCSymbol *MCContext::GetOrCreateSymbol(StringRef Name) { +MCSymbol *MCContext::GetOrCreateSymbol(StringRef Name, bool isTemporary) { assert(!Name.empty() && "Normal symbols cannot be unnamed!"); - MCSymbol *&Entry = Symbols[Name]; - if (Entry) return Entry; + + // Do the lookup and get the entire StringMapEntry. We want access to the + // key if we are creating the entry. + StringMapEntry &Entry = Symbols.GetOrCreateValue(Name); + if (Entry.getValue()) return Entry.getValue(); - return Entry = new (*this) MCSymbol(Name, false); + // Ok, the entry doesn't already exist. Have the MCSymbol object itself refer + // to the copy of the string that is embedded in the StringMapEntry. + MCSymbol *Result = new (*this) MCSymbol(Entry.getKey(), isTemporary); + Entry.setValue(Result); + return Result; } -MCSymbol *MCContext::GetOrCreateSymbol(const Twine &Name) { +MCSymbol *MCContext::GetOrCreateSymbol(const Twine &Name, bool isTemporary) { SmallString<128> NameSV; Name.toVector(NameSV); - return GetOrCreateSymbol(NameSV.str()); + return GetOrCreateSymbol(NameSV.str(), isTemporary); } MCSymbol *MCContext::CreateTempSymbol() { @@ -50,10 +57,7 @@ MCSymbol *MCContext::GetOrCreateTemporarySymbol(StringRef Name) { return GetOrCreateTemporarySymbol(Twine(MAI.getPrivateGlobalPrefix()) + "tmp" + Twine(NextUniqueID++)); - // Otherwise create as usual. - MCSymbol *&Entry = Symbols[Name]; - if (Entry) return Entry; - return Entry = new (*this) MCSymbol(Name, true); + return GetOrCreateSymbol(Name, true); } MCSymbol *MCContext::GetOrCreateTemporarySymbol(const Twine &Name) {