[llvm] r207118 - Fix memory leak of MCSymbolData in MCAsmStreamer.
David Blaikie
dblaikie at gmail.com
Thu Apr 24 07:33:37 PDT 2014
Author: dblaikie
Date: Thu Apr 24 09:33:36 2014
New Revision: 207118
URL: http://llvm.org/viewvc/llvm-project?rev=207118&view=rev
Log:
Fix memory leak of MCSymbolData in MCAsmStreamer.
Leak identified by LSan and reported by Kostya Serebryany.
Let's get a bit experimental here... in theory our minimum compiler
versions support unordered_map.
Modified:
llvm/trunk/lib/MC/MCAsmStreamer.cpp
Modified: llvm/trunk/lib/MC/MCAsmStreamer.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/MC/MCAsmStreamer.cpp?rev=207118&r1=207117&r2=207118&view=diff
==============================================================================
--- llvm/trunk/lib/MC/MCAsmStreamer.cpp (original)
+++ llvm/trunk/lib/MC/MCAsmStreamer.cpp Thu Apr 24 09:33:36 2014
@@ -9,6 +9,7 @@
#include "llvm/MC/MCStreamer.h"
#include "llvm/ADT/SmallString.h"
+#include "llvm/ADT/STLExtras.h"
#include "llvm/ADT/StringExtras.h"
#include "llvm/ADT/Twine.h"
#include "llvm/MC/MCAsmBackend.h"
@@ -31,6 +32,7 @@
#include "llvm/Support/MathExtras.h"
#include "llvm/Support/Path.h"
#include <cctype>
+#include <unordered_map>
using namespace llvm;
namespace {
@@ -57,7 +59,9 @@ private:
EHPrivateExtern = 1 << 2 };
DenseMap<const MCSymbol*, unsigned> FlagMap;
- DenseMap<const MCSymbol*, MCSymbolData*> SymbolMap;
+ // Using std::unordered_map to ensure pointers to MCSymbolData remain valid
+ // over insertions/removals from the SymbolMap.
+ std::unordered_map<const MCSymbol*, MCSymbolData> SymbolMap;
void EmitRegisterName(int64_t Register);
void EmitCFIStartProcImpl(MCDwarfFrameInfo &Frame) override;
@@ -76,7 +80,6 @@ public:
if (InstPrinter && IsVerboseAsm)
InstPrinter->setCommentStream(CommentStream);
}
- ~MCAsmStreamer() {}
inline void EmitEOL() {
// If we don't have any comments, just emit a \n.
@@ -1470,12 +1473,11 @@ void MCAsmStreamer::FinishImpl() {
}
MCSymbolData &MCAsmStreamer::getOrCreateSymbolData(const MCSymbol *Symbol) {
- MCSymbolData *&Entry = SymbolMap[Symbol];
-
- if (!Entry)
- Entry = new MCSymbolData(*Symbol, nullptr, 0, nullptr);
-
- return *Entry;
+ auto Iter = SymbolMap.find(Symbol);
+ if (Iter == SymbolMap.end())
+ Iter = SymbolMap.insert(
+ Iter, std::make_pair(Symbol, MCSymbolData(*Symbol, nullptr, 0)));
+ return Iter->second;
}
MCStreamer *llvm::createAsmStreamer(MCContext &Context,
More information about the llvm-commits
mailing list