[lld] r224113 - Make File always take the ownership of a MemoryBuffer.

Rui Ueyama ruiu at google.com
Fri Dec 12 02:27:34 PST 2014


Author: ruiu
Date: Fri Dec 12 04:27:33 2014
New Revision: 224113

URL: http://llvm.org/viewvc/llvm-project?rev=224113&view=rev
Log:
Make File always take the ownership of a MemoryBuffer.

The documentation of parseFile() said that "the resulting File
object may take ownership of the MemoryBuffer." So, whether or not
the ownership of a MemoryBuffer would be taken was not clear.
A FileNode (a subclass of InputElement, which is being deprecated)
keeps the ownership if a File doesn't take it.

This patch makes File always take the ownership of a buffer.
Buffers lifespan is not always the same as File instances.
Files are able to deallocate buffers after parsing the contents.

Modified:
    lld/trunk/include/lld/Core/File.h
    lld/trunk/include/lld/Core/InputGraph.h
    lld/trunk/include/lld/Driver/CoreInputGraph.h
    lld/trunk/include/lld/Driver/DarwinInputGraph.h
    lld/trunk/include/lld/ReaderWriter/Reader.h
    lld/trunk/lib/Core/InputGraph.cpp
    lld/trunk/lib/Driver/DarwinInputGraph.cpp
    lld/trunk/lib/Driver/GnuLdInputGraph.cpp
    lld/trunk/lib/Driver/WinLinkInputGraph.cpp
    lld/trunk/lib/Passes/RoundTripNativePass.cpp
    lld/trunk/lib/Passes/RoundTripYAMLPass.cpp
    lld/trunk/lib/ReaderWriter/ELF/ELFReader.h
    lld/trunk/lib/ReaderWriter/ELF/Mips/MipsELFReader.h
    lld/trunk/lib/ReaderWriter/FileArchive.cpp
    lld/trunk/lib/ReaderWriter/MachO/File.h
    lld/trunk/lib/ReaderWriter/MachO/MachONormalizedFileBinaryReader.cpp
    lld/trunk/lib/ReaderWriter/Native/ReaderNative.cpp
    lld/trunk/lib/ReaderWriter/PECOFF/ReaderCOFF.cpp
    lld/trunk/lib/ReaderWriter/PECOFF/ReaderImportHeader.cpp
    lld/trunk/lib/ReaderWriter/Reader.cpp
    lld/trunk/lib/ReaderWriter/YAML/ReaderWriterYAML.cpp

Modified: lld/trunk/include/lld/Core/File.h
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/include/lld/Core/File.h?rev=224113&r1=224112&r2=224113&view=diff
==============================================================================
--- lld/trunk/include/lld/Core/File.h (original)
+++ lld/trunk/include/lld/Core/File.h Fri Dec 12 04:27:33 2014
@@ -19,6 +19,7 @@
 #include "llvm/ADT/Optional.h"
 #include "llvm/Support/ErrorHandling.h"
 #include <functional>
+#include <memory>
 #include <vector>
 
 namespace lld {
@@ -170,6 +171,15 @@ public:
     return _lastError.getValue();
   }
 
+  // Usually each file owns a std::unique_ptr<MemoryBuffer>.
+  // However, there's one special case. If a file is an archive file,
+  // the archive file and its children all shares the same memory buffer.
+  // This method is used by the ArchiveFile to give its children
+  // co-ownership of the buffer.
+  void setSharedMemoryBuffer(std::shared_ptr<MemoryBuffer> mb) {
+    _sharedMemoryBuffer = mb;
+  }
+
 protected:
   /// \brief only subclasses of File can be instantiated
   File(StringRef p, Kind kind)
@@ -236,6 +246,7 @@ private:
   StringRef _path;
   Kind              _kind;
   mutable uint64_t  _ordinal;
+  std::shared_ptr<MemoryBuffer> _sharedMemoryBuffer;
 };
 
 /// \brief A mutable File.

Modified: lld/trunk/include/lld/Core/InputGraph.h
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/include/lld/Core/InputGraph.h?rev=224113&r1=224112&r2=224113&view=diff
==============================================================================
--- lld/trunk/include/lld/Core/InputGraph.h (original)
+++ lld/trunk/include/lld/Core/InputGraph.h Fri Dec 12 04:27:33 2014
@@ -214,12 +214,8 @@ public:
   bool getReplacements(InputGraph::InputElementVectorT &result) override;
 
 protected:
-  /// \brief Read the file into _buffer.
-  std::error_code getBuffer(StringRef filePath);
-
   StringRef _path;                       // The path of the Input file
   InputGraph::FileVectorT _files;        // A vector of lld File objects
-  std::unique_ptr<MemoryBuffer> _buffer; // Memory buffer to actual contents
 
   // The next file that would be processed by the resolver
   uint32_t _nextFileIndex;

Modified: lld/trunk/include/lld/Driver/CoreInputGraph.h
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/include/lld/Driver/CoreInputGraph.h?rev=224113&r1=224112&r2=224113&view=diff
==============================================================================
--- lld/trunk/include/lld/Driver/CoreInputGraph.h (original)
+++ lld/trunk/include/lld/Driver/CoreInputGraph.h Fri Dec 12 04:27:33 2014
@@ -44,8 +44,7 @@ public:
     if (std::error_code ec = mb.getError())
       return ec;
 
-    _buffer = std::move(mb.get());
-    return ctx.registry().parseFile(_buffer, _files);
+    return ctx.registry().parseFile(std::move(mb.get()), _files);
   }
 
   /// \brief Return the file that has to be processed by the resolver

Modified: lld/trunk/include/lld/Driver/DarwinInputGraph.h
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/include/lld/Driver/DarwinInputGraph.h?rev=224113&r1=224112&r2=224113&view=diff
==============================================================================
--- lld/trunk/include/lld/Driver/DarwinInputGraph.h (original)
+++ lld/trunk/include/lld/Driver/DarwinInputGraph.h Fri Dec 12 04:27:33 2014
@@ -53,7 +53,7 @@ public:
   }
 
 private:
- void narrowFatBuffer(StringRef filePath);
+  void narrowFatBuffer(std::unique_ptr<MemoryBuffer> &mb, StringRef filePath);
 
   MachOLinkingContext &_context;
   std::unique_ptr<const ArchiveLibraryFile> _archiveFile;

Modified: lld/trunk/include/lld/ReaderWriter/Reader.h
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/include/lld/ReaderWriter/Reader.h?rev=224113&r1=224112&r2=224113&view=diff
==============================================================================
--- lld/trunk/include/lld/ReaderWriter/Reader.h (original)
+++ lld/trunk/include/lld/ReaderWriter/Reader.h Fri Dec 12 04:27:33 2014
@@ -53,10 +53,9 @@ public:
 
   /// \brief Parse a supplied buffer (already filled with the contents of a
   /// file) and create a File object.
-  ///
-  /// The resulting File object may take ownership of the MemoryBuffer.
+  /// The resulting File object takes ownership of the MemoryBuffer.
   virtual std::error_code
-  parseFile(std::unique_ptr<MemoryBuffer> &mb, const class Registry &,
+  parseFile(std::unique_ptr<MemoryBuffer> mb, const class Registry &,
             std::vector<std::unique_ptr<File>> &result) const = 0;
 };
 
@@ -94,7 +93,7 @@ public:
 
   /// Walk the list of registered Readers and find one that can parse the
   /// supplied file and parse it.
-  std::error_code parseFile(std::unique_ptr<MemoryBuffer> &mb,
+  std::error_code parseFile(std::unique_ptr<MemoryBuffer> mb,
                             std::vector<std::unique_ptr<File>> &result) const;
 
   /// Walk the list of registered kind tables to convert a Reference Kind

Modified: lld/trunk/lib/Core/InputGraph.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/lib/Core/InputGraph.cpp?rev=224113&r1=224112&r2=224113&view=diff
==============================================================================
--- lld/trunk/lib/Core/InputGraph.cpp (original)
+++ lld/trunk/lib/Core/InputGraph.cpp Fri Dec 12 04:27:33 2014
@@ -94,17 +94,6 @@ void InputGraph::skipGroup() {
     _nextElementIndex++;
 }
 
-/// \brief Read the file into _buffer.
-std::error_code FileNode::getBuffer(StringRef filePath) {
-  // Create a memory buffer
-  ErrorOr<std::unique_ptr<MemoryBuffer>> mb =
-      MemoryBuffer::getFileOrSTDIN(filePath);
-  if (std::error_code ec = mb.getError())
-    return ec;
-  _buffer = std::move(mb.get());
-  return std::error_code();
-}
-
 bool FileNode::getReplacements(InputGraph::InputElementVectorT &result) {
   if (_files.size() < 2)
     return false;

Modified: lld/trunk/lib/Driver/DarwinInputGraph.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/lib/Driver/DarwinInputGraph.cpp?rev=224113&r1=224112&r2=224113&view=diff
==============================================================================
--- lld/trunk/lib/Driver/DarwinInputGraph.cpp (original)
+++ lld/trunk/lib/Driver/DarwinInputGraph.cpp Fri Dec 12 04:27:33 2014
@@ -24,18 +24,20 @@ std::error_code MachOFileNode::parse(con
   ErrorOr<StringRef> filePath = getPath(ctx);
   if (std::error_code ec = filePath.getError())
     return ec;
-
-  if (std::error_code ec = getBuffer(*filePath))
+  ErrorOr<std::unique_ptr<MemoryBuffer>> mbOrErr =
+      MemoryBuffer::getFileOrSTDIN(*filePath);
+  if (std::error_code ec = mbOrErr.getError())
     return ec;
+  std::unique_ptr<MemoryBuffer> mb = std::move(mbOrErr.get());
 
   _context.addInputFileDependency(*filePath);
   if (ctx.logInputFiles())
     diagnostics << *filePath << "\n";
 
-  narrowFatBuffer(*filePath);
+  narrowFatBuffer(mb, *filePath);
 
   std::vector<std::unique_ptr<File>> parsedFiles;
-  if (std::error_code ec = ctx.registry().parseFile(_buffer, parsedFiles))
+  if (std::error_code ec = ctx.registry().parseFile(std::move(mb), parsedFiles))
     return ec;
   for (std::unique_ptr<File> &pf : parsedFiles) {
     // If file is a dylib, inform LinkingContext about it.
@@ -61,19 +63,20 @@ std::error_code MachOFileNode::parse(con
 
 /// If buffer contains a fat file, find required arch in fat buffer and
 /// switch buffer to point to just that required slice.
-void MachOFileNode::narrowFatBuffer(StringRef filePath) {
+void MachOFileNode::narrowFatBuffer(std::unique_ptr<MemoryBuffer> &mb,
+                                    StringRef filePath) {
   // Check if buffer is a "fat" file that contains needed arch.
   uint32_t offset;
   uint32_t size;
-  if (!_context.sliceFromFatFile(*_buffer, offset, size)) {
+  if (!_context.sliceFromFatFile(*mb, offset, size)) {
     return;
   }
   // Create new buffer containing just the needed slice.
   auto subuf = MemoryBuffer::getFileSlice(filePath, size, offset);
   if (subuf.getError())
     return;
-  // The assignment to _buffer will release previous buffer.
-  _buffer = std::move(subuf.get());
+  // The assignment to mb will release previous buffer.
+  mb = std::move(subuf.get());
 }
 
 

Modified: lld/trunk/lib/Driver/GnuLdInputGraph.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/lib/Driver/GnuLdInputGraph.cpp?rev=224113&r1=224112&r2=224113&view=diff
==============================================================================
--- lld/trunk/lib/Driver/GnuLdInputGraph.cpp (original)
+++ lld/trunk/lib/Driver/GnuLdInputGraph.cpp Fri Dec 12 04:27:33 2014
@@ -20,14 +20,17 @@ std::error_code ELFFileNode::parse(const
   ErrorOr<StringRef> filePath = getPath(ctx);
   if (std::error_code ec = filePath.getError())
     return ec;
-  if (std::error_code ec = getBuffer(*filePath))
+  ErrorOr<std::unique_ptr<MemoryBuffer>> mb =
+      MemoryBuffer::getFileOrSTDIN(*filePath);
+  if (std::error_code ec = mb.getError())
     return ec;
   if (ctx.logInputFiles())
     diagnostics << *filePath << "\n";
 
   if (_attributes._isWholeArchive) {
     std::vector<std::unique_ptr<File>> parsedFiles;
-    if (std::error_code ec = ctx.registry().parseFile(_buffer, parsedFiles))
+    if (std::error_code ec = ctx.registry().parseFile(
+            std::move(mb.get()), parsedFiles))
       return ec;
     assert(parsedFiles.size() == 1);
     std::unique_ptr<File> f(parsedFiles[0].release());
@@ -42,7 +45,7 @@ std::error_code ELFFileNode::parse(const
     _files.push_back(std::move(f));
     return std::error_code();
   }
-  return ctx.registry().parseFile(_buffer, _files);
+  return ctx.registry().parseFile(std::move(mb.get()), _files);
 }
 
 /// \brief Parse the GnuLD Script
@@ -51,13 +54,15 @@ std::error_code GNULdScript::parse(const
   ErrorOr<StringRef> filePath = getPath(ctx);
   if (std::error_code ec = filePath.getError())
     return ec;
-  if (std::error_code ec = getBuffer(*filePath))
+  ErrorOr<std::unique_ptr<MemoryBuffer>> mb =
+      MemoryBuffer::getFileOrSTDIN(*filePath);
+  if (std::error_code ec = mb.getError())
     return ec;
 
   if (ctx.logInputFiles())
     diagnostics << *filePath << "\n";
 
-  _lexer.reset(new script::Lexer(std::move(_buffer)));
+  _lexer.reset(new script::Lexer(std::move(mb.get())));
   _parser.reset(new script::Parser(*_lexer.get()));
 
   _linkerScript = _parser->parse();

Modified: lld/trunk/lib/Driver/WinLinkInputGraph.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/lib/Driver/WinLinkInputGraph.cpp?rev=224113&r1=224112&r2=224113&view=diff
==============================================================================
--- lld/trunk/lib/Driver/WinLinkInputGraph.cpp (original)
+++ lld/trunk/lib/Driver/WinLinkInputGraph.cpp Fri Dec 12 04:27:33 2014
@@ -27,7 +27,9 @@ std::error_code PECOFFFileNode::parse(co
     return ec;
   }
 
-  if (std::error_code ec = getBuffer(*filePath)) {
+  ErrorOr<std::unique_ptr<MemoryBuffer>> mb =
+      MemoryBuffer::getFileOrSTDIN(*filePath);
+  if (std::error_code ec = mb.getError()) {
     diagnostics << "Cannot open file: " << *filePath << "\n";
     return ec;
   }
@@ -35,7 +37,7 @@ std::error_code PECOFFFileNode::parse(co
   if (ctx.logInputFiles())
     diagnostics << *filePath << "\n";
 
-  return ctx.registry().parseFile(_buffer, _files);
+  return ctx.registry().parseFile(std::move(mb.get()), _files);
 }
 
 ErrorOr<File &> PECOFFFileNode::getNextFile() {

Modified: lld/trunk/lib/Passes/RoundTripNativePass.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/lib/Passes/RoundTripNativePass.cpp?rev=224113&r1=224112&r2=224113&view=diff
==============================================================================
--- lld/trunk/lib/Passes/RoundTripNativePass.cpp (original)
+++ lld/trunk/lib/Passes/RoundTripNativePass.cpp Fri Dec 12 04:27:33 2014
@@ -40,7 +40,8 @@ void RoundTripNativePass::perform(std::u
   if (!mb)
     return;
 
-  std::error_code ec = _context.registry().parseFile(mb.get(), _nativeFile);
+  std::error_code ec = _context.registry().parseFile(
+      std::move(mb.get()), _nativeFile);
   if (ec) {
     // Note: we need a way for Passes to report errors.
     llvm_unreachable("native reader not registered or read error");

Modified: lld/trunk/lib/Passes/RoundTripYAMLPass.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/lib/Passes/RoundTripYAMLPass.cpp?rev=224113&r1=224112&r2=224113&view=diff
==============================================================================
--- lld/trunk/lib/Passes/RoundTripYAMLPass.cpp (original)
+++ lld/trunk/lib/Passes/RoundTripYAMLPass.cpp Fri Dec 12 04:27:33 2014
@@ -40,7 +40,8 @@ void RoundTripYAMLPass::perform(std::uni
   if (!mb)
     return;
 
-  std::error_code ec = _context.registry().parseFile(mb.get(), _yamlFile);
+  std::error_code ec = _context.registry().parseFile(
+      std::move(mb.get()), _yamlFile);
   if (ec) {
     // Note: we need a way for Passes to report errors.
     llvm_unreachable("yaml reader not registered or read error");

Modified: lld/trunk/lib/ReaderWriter/ELF/ELFReader.h
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/lib/ReaderWriter/ELF/ELFReader.h?rev=224113&r1=224112&r2=224113&view=diff
==============================================================================
--- lld/trunk/lib/ReaderWriter/ELF/ELFReader.h (original)
+++ lld/trunk/lib/ReaderWriter/ELF/ELFReader.h Fri Dec 12 04:27:33 2014
@@ -33,7 +33,7 @@ public:
   }
 
   std::error_code
-  parseFile(std::unique_ptr<MemoryBuffer> &mb, const class Registry &,
+  parseFile(std::unique_ptr<MemoryBuffer> mb, const class Registry &,
             std::vector<std::unique_ptr<File>> &result) const override {
     std::size_t maxAlignment =
         1ULL << llvm::countTrailingZeros(uintptr_t(mb->getBufferStart()));
@@ -72,7 +72,7 @@ public:
   }
 
   std::error_code
-  parseFile(std::unique_ptr<MemoryBuffer> &mb, const class Registry &,
+  parseFile(std::unique_ptr<MemoryBuffer> mb, const class Registry &,
             std::vector<std::unique_ptr<File>> &result) const override {
     std::size_t maxAlignment =
         1ULL << llvm::countTrailingZeros(uintptr_t(mb->getBufferStart()));

Modified: lld/trunk/lib/ReaderWriter/ELF/Mips/MipsELFReader.h
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/lib/ReaderWriter/ELF/Mips/MipsELFReader.h?rev=224113&r1=224112&r2=224113&view=diff
==============================================================================
--- lld/trunk/lib/ReaderWriter/ELF/Mips/MipsELFReader.h (original)
+++ lld/trunk/lib/ReaderWriter/ELF/Mips/MipsELFReader.h Fri Dec 12 04:27:33 2014
@@ -49,12 +49,12 @@ public:
         _flagMerger(flagMerger) {}
 
   std::error_code
-  parseFile(std::unique_ptr<MemoryBuffer> &mb, const Registry &registry,
+  parseFile(std::unique_ptr<MemoryBuffer> mb, const Registry &registry,
             std::vector<std::unique_ptr<File>> &result) const override {
     auto &hdr = *elfHeader(*mb);
     if (std::error_code ec = _flagMerger.merge(hdr.getFileClass(), hdr.e_flags))
       return ec;
-    return BaseReaderType::parseFile(mb, registry, result);
+    return BaseReaderType::parseFile(std::move(mb), registry, result);
   }
 
 private:
@@ -72,12 +72,12 @@ public:
         _flagMerger(flagMerger) {}
 
   std::error_code
-  parseFile(std::unique_ptr<MemoryBuffer> &mb, const Registry &registry,
+  parseFile(std::unique_ptr<MemoryBuffer> mb, const Registry &registry,
             std::vector<std::unique_ptr<File>> &result) const override {
     auto &hdr = *elfHeader(*mb);
     if (std::error_code ec = _flagMerger.merge(hdr.getFileClass(), hdr.e_flags))
       return ec;
-    return BaseReaderType::parseFile(mb, registry, result);
+    return BaseReaderType::parseFile(std::move(mb), registry, result);
   }
 
 private:

Modified: lld/trunk/lib/ReaderWriter/FileArchive.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/lib/ReaderWriter/FileArchive.cpp?rev=224113&r1=224112&r2=224113&view=diff
==============================================================================
--- lld/trunk/lib/ReaderWriter/FileArchive.cpp (original)
+++ lld/trunk/lib/ReaderWriter/FileArchive.cpp Fri Dec 12 04:27:33 2014
@@ -33,10 +33,10 @@ namespace {
 /// \brief The FileArchive class represents an Archive Library file
 class FileArchive : public lld::ArchiveLibraryFile {
 public:
-  FileArchive(std::unique_ptr<MemoryBuffer> &mb, const Registry &reg,
+  FileArchive(std::unique_ptr<MemoryBuffer> mb, const Registry &reg,
               StringRef path, bool logLoading)
-      : ArchiveLibraryFile(path), _mb(mb), _registry(reg),
-        _logLoading(logLoading) {}
+      : ArchiveLibraryFile(path), _mb(std::shared_ptr<MemoryBuffer>(mb.release())),
+        _registry(reg), _logLoading(logLoading) {}
 
   std::error_code doParse() override {
     // Make Archive object which will be owned by FileArchive object.
@@ -153,17 +153,13 @@ private:
         mb.getBuffer(), memberPath, false));
 
     std::vector<std::unique_ptr<File>> files;
-    _registry.parseFile(memberMB, files);
+    _registry.parseFile(std::move(memberMB), files);
     assert(files.size() == 1);
     result = std::move(files[0]);
 
-    // Note: The object file parsers use getBufferIdentifier() from memberMB
-    // for the file path. And MemoryBuffer makes its own copy of the path.
-    // That means when if memberMB is destroyed, the lld:File objects will
-    // have a dangling reference for their path.  To fix that, all the
-    // MemoryBuffers for the archive members are owned by _memberBuffers.
-    _memberBuffers.push_back(std::move(memberMB));
-
+    // The memory buffer is co-owned by the archive file and the children,
+    // so that the bufffer is deallocated when all the members are destructed.
+    result->setSharedMemoryBuffer(_mb);
     return std::error_code();
   }
 
@@ -214,7 +210,7 @@ private:
   typedef std::unordered_map<StringRef, Archive::child_iterator> MemberMap;
   typedef std::set<const char *> InstantiatedSet;
 
-  std::unique_ptr<MemoryBuffer> &_mb;
+  std::shared_ptr<MemoryBuffer> _mb;
   const Registry &_registry;
   std::unique_ptr<Archive> _archive;
   mutable MemberMap _symbolMemberMap;
@@ -238,10 +234,11 @@ public:
   }
 
   std::error_code
-  parseFile(std::unique_ptr<MemoryBuffer> &mb, const Registry &reg,
+  parseFile(std::unique_ptr<MemoryBuffer> mb, const Registry &reg,
             std::vector<std::unique_ptr<File>> &result) const override {
+    StringRef path = mb->getBufferIdentifier();
     std::unique_ptr<FileArchive> file(
-        new FileArchive(mb, reg, mb->getBufferIdentifier(), _logLoading));
+        new FileArchive(std::move(mb), reg, path, _logLoading));
     result.push_back(std::move(file));
     return std::error_code();
   }

Modified: lld/trunk/lib/ReaderWriter/MachO/File.h
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/lib/ReaderWriter/MachO/File.h?rev=224113&r1=224112&r2=224113&view=diff
==============================================================================
--- lld/trunk/lib/ReaderWriter/MachO/File.h (original)
+++ lld/trunk/lib/ReaderWriter/MachO/File.h Fri Dec 12 04:27:33 2014
@@ -24,8 +24,8 @@ using lld::mach_o::normalized::Section;
 
 class MachOFile : public SimpleFile {
 public:
-  MachOFile(MemoryBuffer *mb, MachOLinkingContext *ctx)
-      : SimpleFile(mb->getBufferIdentifier()), _mb(mb), _ctx(ctx) {}
+  MachOFile(std::unique_ptr<MemoryBuffer> mb, MachOLinkingContext *ctx)
+      : SimpleFile(mb->getBufferIdentifier()), _mb(std::move(mb)), _ctx(ctx) {}
 
   MachOFile(StringRef path) : SimpleFile(path) {}
 
@@ -177,9 +177,7 @@ public:
 
   std::error_code doParse() override {
     // Convert binary file to normalized mach-o.
-    std::unique_ptr<MemoryBuffer>mb(_mb);
-    auto normFile = normalized::readBinary(mb, _ctx->arch());
-    mb.release();
+    auto normFile = normalized::readBinary(_mb, _ctx->arch());
     if (std::error_code ec = normFile.getError())
       return ec;
     // Convert normalized mach-o to atoms.
@@ -206,16 +204,17 @@ private:
                          std::vector<SectionOffsetAndAtom>>  SectionToAtoms;
   typedef llvm::StringMap<const lld::Atom *> NameToAtom;
 
-  MemoryBuffer           *_mb;
-  MachOLinkingContext    *_ctx;
-  SectionToAtoms          _sectionAtoms;
-  NameToAtom              _undefAtoms;
+  std::unique_ptr<MemoryBuffer> _mb;
+  MachOLinkingContext          *_ctx;
+  SectionToAtoms                _sectionAtoms;
+  NameToAtom                     _undefAtoms;
 };
 
 class MachODylibFile : public SharedLibraryFile {
 public:
-  MachODylibFile(MemoryBuffer *mb, MachOLinkingContext *ctx)
-      : SharedLibraryFile(mb->getBufferIdentifier()), _mb(mb), _ctx(ctx) {}
+  MachODylibFile(std::unique_ptr<MemoryBuffer> mb, MachOLinkingContext *ctx)
+      : SharedLibraryFile(mb->getBufferIdentifier()),
+        _mb(std::move(mb)), _ctx(ctx) {}
 
   MachODylibFile(StringRef path) : SharedLibraryFile(path) {}
 
@@ -275,9 +274,7 @@ public:
 
   std::error_code doParse() override {
     // Convert binary file to normalized mach-o.
-    std::unique_ptr<MemoryBuffer>mb(_mb);
-    auto normFile = normalized::readBinary(mb, _ctx->arch());
-    mb.release();
+    auto normFile = normalized::readBinary(_mb, _ctx->arch());
     if (std::error_code ec = normFile.getError())
       return ec;
     // Convert normalized mach-o to atoms.
@@ -328,7 +325,7 @@ private:
     bool                      weakDef;
   };
 
-  MemoryBuffer                              *_mb;
+  std::unique_ptr<MemoryBuffer>              _mb;
   MachOLinkingContext                       *_ctx;
   StringRef                                  _installName;
   uint32_t                                   _currentVersion;

Modified: lld/trunk/lib/ReaderWriter/MachO/MachONormalizedFileBinaryReader.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/lib/ReaderWriter/MachO/MachONormalizedFileBinaryReader.cpp?rev=224113&r1=224112&r2=224113&view=diff
==============================================================================
--- lld/trunk/lib/ReaderWriter/MachO/MachONormalizedFileBinaryReader.cpp (original)
+++ lld/trunk/lib/ReaderWriter/MachO/MachONormalizedFileBinaryReader.cpp Fri Dec 12 04:27:33 2014
@@ -520,9 +520,9 @@ public:
   }
 
   std::error_code
-  parseFile(std::unique_ptr<MemoryBuffer> &mb, const Registry &registry,
+  parseFile(std::unique_ptr<MemoryBuffer> mb, const Registry &registry,
             std::vector<std::unique_ptr<File>> &result) const override {
-    auto *file = new MachOFile(mb.get(), &_ctx);
+    auto *file = new MachOFile(std::move(mb), &_ctx);
     result.push_back(std::unique_ptr<MachOFile>(file));
     return std::error_code();
   }
@@ -547,9 +547,9 @@ public:
   }
 
   std::error_code
-  parseFile(std::unique_ptr<MemoryBuffer> &mb, const Registry &registry,
+  parseFile(std::unique_ptr<MemoryBuffer> mb, const Registry &registry,
             std::vector<std::unique_ptr<File>> &result) const override {
-    auto *file = new MachODylibFile(mb.get(), &_ctx);
+    auto *file = new MachODylibFile(std::move(mb), &_ctx);
     result.push_back(std::unique_ptr<MachODylibFile>(file));
     return std::error_code();
   }

Modified: lld/trunk/lib/ReaderWriter/Native/ReaderNative.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/lib/ReaderWriter/Native/ReaderNative.cpp?rev=224113&r1=224112&r2=224113&view=diff
==============================================================================
--- lld/trunk/lib/ReaderWriter/Native/ReaderNative.cpp (original)
+++ lld/trunk/lib/ReaderWriter/Native/ReaderNative.cpp Fri Dec 12 04:27:33 2014
@@ -999,7 +999,7 @@ public:
   }
 
   virtual std::error_code
-  parseFile(std::unique_ptr<MemoryBuffer> &mb, const class Registry &,
+  parseFile(std::unique_ptr<MemoryBuffer> mb, const class Registry &,
             std::vector<std::unique_ptr<File>> &result) const override {
     auto *file = new lld::native::File(std::move(mb));
     result.push_back(std::unique_ptr<File>(file));

Modified: lld/trunk/lib/ReaderWriter/PECOFF/ReaderCOFF.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/lib/ReaderWriter/PECOFF/ReaderCOFF.cpp?rev=224113&r1=224112&r2=224113&view=diff
==============================================================================
--- lld/trunk/lib/ReaderWriter/PECOFF/ReaderCOFF.cpp (original)
+++ lld/trunk/lib/ReaderWriter/PECOFF/ReaderCOFF.cpp Fri Dec 12 04:27:33 2014
@@ -1074,7 +1074,7 @@ public:
   }
 
   std::error_code
-  parseFile(std::unique_ptr<MemoryBuffer> &mb, const Registry &,
+  parseFile(std::unique_ptr<MemoryBuffer> mb, const Registry &,
             std::vector<std::unique_ptr<File>> &result) const override {
     // Parse the memory buffer as PECOFF file.
     auto *file = new FileCOFF(std::move(mb), _ctx);

Modified: lld/trunk/lib/ReaderWriter/PECOFF/ReaderImportHeader.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/lib/ReaderWriter/PECOFF/ReaderImportHeader.cpp?rev=224113&r1=224112&r2=224113&view=diff
==============================================================================
--- lld/trunk/lib/ReaderWriter/PECOFF/ReaderImportHeader.cpp (original)
+++ lld/trunk/lib/ReaderWriter/PECOFF/ReaderImportHeader.cpp Fri Dec 12 04:27:33 2014
@@ -325,7 +325,7 @@ public:
   }
 
   std::error_code
-  parseFile(std::unique_ptr<MemoryBuffer> &mb, const class Registry &,
+  parseFile(std::unique_ptr<MemoryBuffer> mb, const class Registry &,
             std::vector<std::unique_ptr<File> > &result) const override {
     auto *file = new FileImportLibrary(std::move(mb), _machine);
     result.push_back(std::unique_ptr<File>(file));

Modified: lld/trunk/lib/ReaderWriter/Reader.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/lib/ReaderWriter/Reader.cpp?rev=224113&r1=224112&r2=224113&view=diff
==============================================================================
--- lld/trunk/lib/ReaderWriter/Reader.cpp (original)
+++ lld/trunk/lib/ReaderWriter/Reader.cpp Fri Dec 12 04:27:33 2014
@@ -30,7 +30,7 @@ void Registry::add(std::unique_ptr<YamlI
 }
 
 std::error_code
-Registry::parseFile(std::unique_ptr<MemoryBuffer> &mb,
+Registry::parseFile(std::unique_ptr<MemoryBuffer> mb,
                     std::vector<std::unique_ptr<File>> &result) const {
   // Get file type.
   StringRef content(mb->getBufferStart(), mb->getBufferSize());
@@ -42,7 +42,7 @@ Registry::parseFile(std::unique_ptr<Memo
   for (const std::unique_ptr<Reader> &reader : _readers) {
     if (!reader->canParse(fileType, extension, *mb))
       continue;
-    if (std::error_code ec = reader->parseFile(mb, *this, result))
+    if (std::error_code ec = reader->parseFile(std::move(mb), *this, result))
       return ec;
     for (std::unique_ptr<File> &file : result)
       if (std::error_code ec = file->parse())

Modified: lld/trunk/lib/ReaderWriter/YAML/ReaderWriterYAML.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/lib/ReaderWriter/YAML/ReaderWriterYAML.cpp?rev=224113&r1=224112&r2=224113&view=diff
==============================================================================
--- lld/trunk/lib/ReaderWriter/YAML/ReaderWriterYAML.cpp (original)
+++ lld/trunk/lib/ReaderWriter/YAML/ReaderWriterYAML.cpp Fri Dec 12 04:27:33 2014
@@ -1325,14 +1325,12 @@ public:
   }
 
   std::error_code
-  parseFile(std::unique_ptr<MemoryBuffer> &mb, const class Registry &,
+  parseFile(std::unique_ptr<MemoryBuffer> mb, const class Registry &,
             std::vector<std::unique_ptr<File>> &result) const override {
-    // Note: we do not take ownership of the MemoryBuffer.  That is
-    // because yaml may produce multiple File objects, so there is no
-    // *one* File to take ownership.  Therefore, the yaml File objects
-    // produced must make copies of all strings that come from YAML I/O.
-    // Otherwise the strings will become invalid when this MemoryBuffer
-    // is deallocated.
+    // Note: we do not store the unique pointer to the MemoryBuffer,
+    // so the buffer will be deallocated at end of this function.
+    // That's OK since the YAML file contents are parsed and consumed
+    // in this function.
 
     // Create YAML Input Reader.
     YamlContext yamlContext;





More information about the llvm-commits mailing list