[lld] r264004 - Move ownership of Pass File's to the MachoLinkingContext.

Pete Cooper via llvm-commits llvm-commits at lists.llvm.org
Mon Mar 21 16:17:48 PDT 2016


Author: pete
Date: Mon Mar 21 18:17:47 2016
New Revision: 264004

URL: http://llvm.org/viewvc/llvm-project?rev=264004&view=rev
Log:
Move ownership of Pass File's to the MachoLinkingContext.

In trying to fix the leaks in the MachO lld codebase, we need to have
a better model for file and atom ownership.  Having the context own
everything seems like the simplest model, so change all the passes to
allocate File's on the context instead of owning files as a member.

Modified:
    lld/trunk/include/lld/ReaderWriter/MachOLinkingContext.h
    lld/trunk/lib/ReaderWriter/MachO/CompactUnwindPass.cpp
    lld/trunk/lib/ReaderWriter/MachO/GOTPass.cpp
    lld/trunk/lib/ReaderWriter/MachO/ObjCPass.cpp
    lld/trunk/lib/ReaderWriter/MachO/ShimPass.cpp
    lld/trunk/lib/ReaderWriter/MachO/StubsPass.cpp
    lld/trunk/lib/ReaderWriter/MachO/TLVPass.cpp

Modified: lld/trunk/include/lld/ReaderWriter/MachOLinkingContext.h
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/include/lld/ReaderWriter/MachOLinkingContext.h?rev=264004&r1=264003&r2=264004&view=diff
==============================================================================
--- lld/trunk/include/lld/ReaderWriter/MachOLinkingContext.h (original)
+++ lld/trunk/include/lld/ReaderWriter/MachOLinkingContext.h Mon Mar 21 18:17:47 2016
@@ -13,6 +13,7 @@
 #include "lld/Core/LinkingContext.h"
 #include "lld/Core/Reader.h"
 #include "lld/Core/Writer.h"
+#include "llvm/ADT/STLExtras.h"
 #include "llvm/ADT/StringMap.h"
 #include "llvm/ADT/StringSet.h"
 #include "llvm/Support/ErrorHandling.h"
@@ -93,6 +94,18 @@ public:
 
   void createImplicitFiles(std::vector<std::unique_ptr<File>> &) override;
 
+  /// Creates a new file which is owned by the context.  Returns a pointer to
+  /// the new file.
+  template <class T, class... Args>
+  typename std::enable_if<!std::is_array<T>::value, T *>::type
+  make_file(Args &&... args) const {
+    auto file = std::unique_ptr<T>(new T(std::forward<Args>(args)...));
+    auto *filePtr = file.get();
+    auto *ctx = const_cast<MachOLinkingContext *>(this);
+    ctx->getNodes().push_back(llvm::make_unique<FileNode>(std::move(file)));
+    return filePtr;
+  }
+
   uint32_t getCPUType() const;
   uint32_t getCPUSubType() const;
 

Modified: lld/trunk/lib/ReaderWriter/MachO/CompactUnwindPass.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/lib/ReaderWriter/MachO/CompactUnwindPass.cpp?rev=264004&r1=264003&r2=264004&view=diff
==============================================================================
--- lld/trunk/lib/ReaderWriter/MachO/CompactUnwindPass.cpp (original)
+++ lld/trunk/lib/ReaderWriter/MachO/CompactUnwindPass.cpp Mon Mar 21 18:17:47 2016
@@ -273,7 +273,7 @@ class CompactUnwindPass : public Pass {
 public:
   CompactUnwindPass(const MachOLinkingContext &context)
       : _ctx(context), _archHandler(_ctx.archHandler()),
-        _file("<mach-o Compact Unwind Pass>"),
+        _file(*_ctx.make_file<MachOFile>("<mach-o Compact Unwind Pass>")),
         _isBig(MachOLinkingContext::isBigEndian(_ctx.arch())) {
     _file.setOrdinal(_ctx.getNextOrdinalAndIncrement());
   }
@@ -567,7 +567,7 @@ private:
 
   const MachOLinkingContext &_ctx;
   mach_o::ArchHandler &_archHandler;
-  MachOFile _file;
+  MachOFile &_file;
   bool _isBig;
 };
 

Modified: lld/trunk/lib/ReaderWriter/MachO/GOTPass.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/lib/ReaderWriter/MachO/GOTPass.cpp?rev=264004&r1=264003&r2=264004&view=diff
==============================================================================
--- lld/trunk/lib/ReaderWriter/MachO/GOTPass.cpp (original)
+++ lld/trunk/lib/ReaderWriter/MachO/GOTPass.cpp Mon Mar 21 18:17:47 2016
@@ -91,7 +91,7 @@ class GOTPass : public Pass {
 public:
   GOTPass(const MachOLinkingContext &context)
       : _ctx(context), _archHandler(_ctx.archHandler()),
-        _file("<mach-o GOT Pass>") {
+        _file(*_ctx.make_file<MachOFile>("<mach-o GOT Pass>")) {
     _file.setOrdinal(_ctx.getNextOrdinalAndIncrement());
   }
 
@@ -169,7 +169,7 @@ private:
 
   const MachOLinkingContext &_ctx;
   mach_o::ArchHandler                             &_archHandler;
-  MachOFile                                        _file;
+  MachOFile                                       &_file;
   llvm::DenseMap<const Atom*, const GOTEntryAtom*> _targetToGOT;
 };
 

Modified: lld/trunk/lib/ReaderWriter/MachO/ObjCPass.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/lib/ReaderWriter/MachO/ObjCPass.cpp?rev=264004&r1=264003&r2=264004&view=diff
==============================================================================
--- lld/trunk/lib/ReaderWriter/MachO/ObjCPass.cpp (original)
+++ lld/trunk/lib/ReaderWriter/MachO/ObjCPass.cpp Mon Mar 21 18:17:47 2016
@@ -93,7 +93,7 @@ class ObjCPass : public Pass {
 public:
   ObjCPass(const MachOLinkingContext &context)
       : _ctx(context),
-        _file("<mach-o objc pass>") {
+        _file(*_ctx.make_file<MachOFile>("<mach-o objc pass>")) {
     _file.setOrdinal(_ctx.getNextOrdinalAndIncrement());
   }
 
@@ -113,7 +113,7 @@ private:
   }
 
   const MachOLinkingContext   &_ctx;
-  MachOFile                   _file;
+  MachOFile                   &_file;
 };
 
 

Modified: lld/trunk/lib/ReaderWriter/MachO/ShimPass.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/lib/ReaderWriter/MachO/ShimPass.cpp?rev=264004&r1=264003&r2=264004&view=diff
==============================================================================
--- lld/trunk/lib/ReaderWriter/MachO/ShimPass.cpp (original)
+++ lld/trunk/lib/ReaderWriter/MachO/ShimPass.cpp Mon Mar 21 18:17:47 2016
@@ -42,7 +42,8 @@ class ShimPass : public Pass {
 public:
   ShimPass(const MachOLinkingContext &context)
       : _ctx(context), _archHandler(_ctx.archHandler()),
-        _stubInfo(_archHandler.stubInfo()), _file("<mach-o shim pass>") {
+        _stubInfo(_archHandler.stubInfo()),
+        _file(*_ctx.make_file<MachOFile>("<mach-o shim pass>")) {
     _file.setOrdinal(_ctx.getNextOrdinalAndIncrement());
   }
 
@@ -114,7 +115,7 @@ private:
   const MachOLinkingContext &_ctx;
   mach_o::ArchHandler                            &_archHandler;
   const ArchHandler::StubInfo                    &_stubInfo;
-  MachOFile                                       _file;
+  MachOFile                                      &_file;
   llvm::DenseMap<const Atom*, const DefinedAtom*> _targetToShim;
 };
 

Modified: lld/trunk/lib/ReaderWriter/MachO/StubsPass.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/lib/ReaderWriter/MachO/StubsPass.cpp?rev=264004&r1=264003&r2=264004&view=diff
==============================================================================
--- lld/trunk/lib/ReaderWriter/MachO/StubsPass.cpp (original)
+++ lld/trunk/lib/ReaderWriter/MachO/StubsPass.cpp Mon Mar 21 18:17:47 2016
@@ -200,7 +200,8 @@ class StubsPass : public Pass {
 public:
   StubsPass(const MachOLinkingContext &context)
       : _ctx(context), _archHandler(_ctx.archHandler()),
-        _stubInfo(_archHandler.stubInfo()), _file("<mach-o Stubs pass>") {
+        _stubInfo(_archHandler.stubInfo()),
+        _file(*_ctx.make_file<MachOFile>("<mach-o Stubs pass>")) {
     _file.setOrdinal(_ctx.getNextOrdinalAndIncrement());
   }
 
@@ -356,7 +357,7 @@ private:
   const MachOLinkingContext &_ctx;
   mach_o::ArchHandler                            &_archHandler;
   const ArchHandler::StubInfo                    &_stubInfo;
-  MachOFile                                       _file;
+  MachOFile                                      &_file;
   TargetToUses                                    _targetToUses;
 };
 

Modified: lld/trunk/lib/ReaderWriter/MachO/TLVPass.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/lib/ReaderWriter/MachO/TLVPass.cpp?rev=264004&r1=264003&r2=264004&view=diff
==============================================================================
--- lld/trunk/lib/ReaderWriter/MachO/TLVPass.cpp (original)
+++ lld/trunk/lib/ReaderWriter/MachO/TLVPass.cpp Mon Mar 21 18:17:47 2016
@@ -65,7 +65,7 @@ class TLVPass : public Pass {
 public:
   TLVPass(const MachOLinkingContext &context)
       : _ctx(context), _archHandler(_ctx.archHandler()),
-        _file("<mach-o TLV Pass>") {
+        _file(*_ctx.make_file<MachOFile>("<mach-o TLV pass>")) {
     _file.setOrdinal(_ctx.getNextOrdinalAndIncrement());
   }
 
@@ -126,7 +126,7 @@ private:
 
   const MachOLinkingContext &_ctx;
   mach_o::ArchHandler &_archHandler;
-  MachOFile _file;
+  MachOFile           &_file;
   llvm::DenseMap<const Atom*, const TLVPEntryAtom*> _targetToTLVP;
 };
 




More information about the llvm-commits mailing list