diff options
author | Rafael Espindola <rafael.espindola@gmail.com> | 2014-08-19 18:44:46 +0000 |
---|---|---|
committer | Rafael Espindola <rafael.espindola@gmail.com> | 2014-08-19 18:44:46 +0000 |
commit | 548f2b6e8fc5499fa8c9394fe7d110f50c487802 (patch) | |
tree | fac1350c700e14ca8f9ea779ceb6228caeaff1a0 /lib/Object/MachOUniversal.cpp | |
parent | 2ac376ba349bd1b4cc5cdc6bde24547e2824f061 (diff) |
Don't own the buffer in object::Binary.
Owning the buffer is somewhat inflexible. Some Binaries have sub Binaries
(like Archive) and we had to create dummy buffers just to handle that. It is
also a bad fit for IRObjectFile where the Module wants to own the buffer too.
Keeping this ownership would make supporting IR inside native objects
particularly painful.
This patch focuses in lib/Object. If something elsewhere used to own an Binary,
now it also owns a MemoryBuffer.
This patch introduces a few new types.
* MemoryBufferRef. This is just a pair of StringRefs for the data and name.
This is to MemoryBuffer as StringRef is to std::string.
* OwningBinary. A combination of Binary and a MemoryBuffer. This is needed
for convenience functions that take a filename and return both the
buffer and the Binary using that buffer.
The C api now uses OwningBinary to avoid any change in semantics. I will start
a new thread to see if we want to change it and how.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@216002 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/Object/MachOUniversal.cpp')
-rw-r--r-- | lib/Object/MachOUniversal.cpp | 22 |
1 files changed, 9 insertions, 13 deletions
diff --git a/lib/Object/MachOUniversal.cpp b/lib/Object/MachOUniversal.cpp index ece652b4c09..8d24403d033 100644 --- a/lib/Object/MachOUniversal.cpp +++ b/lib/Object/MachOUniversal.cpp @@ -72,9 +72,8 @@ MachOUniversalBinary::ObjectForArch::getAsObjectFile() const { if (Parent) { StringRef ParentData = Parent->getData(); StringRef ObjectData = ParentData.substr(Header.offset, Header.size); - std::string ObjectName = Parent->getFileName().str(); - std::unique_ptr<MemoryBuffer> ObjBuffer( - MemoryBuffer::getMemBuffer(ObjectData, ObjectName, false)); + StringRef ObjectName = Parent->getFileName().str(); + MemoryBufferRef ObjBuffer(ObjectData, ObjectName); return ObjectFile::createMachOObjectFile(ObjBuffer); } return object_error::parse_failed; @@ -86,10 +85,8 @@ std::error_code MachOUniversalBinary::ObjectForArch::getAsArchive( StringRef ParentData = Parent->getData(); StringRef ObjectData = ParentData.substr(Header.offset, Header.size); std::string ObjectName = Parent->getFileName().str(); - std::unique_ptr<MemoryBuffer> ObjBuffer( - MemoryBuffer::getMemBuffer(ObjectData, ObjectName, false)); - ErrorOr<std::unique_ptr<Archive>> Obj = - Archive::create(std::move(ObjBuffer)); + MemoryBufferRef ObjBuffer(ObjectData, ObjectName); + ErrorOr<std::unique_ptr<Archive>> Obj = Archive::create(ObjBuffer); if (std::error_code EC = Obj.getError()) return EC; Result = std::move(Obj.get()); @@ -101,20 +98,19 @@ std::error_code MachOUniversalBinary::ObjectForArch::getAsArchive( void MachOUniversalBinary::anchor() { } ErrorOr<MachOUniversalBinary *> -MachOUniversalBinary::create(std::unique_ptr<MemoryBuffer> Source) { +MachOUniversalBinary::create(MemoryBufferRef Source) { std::error_code EC; std::unique_ptr<MachOUniversalBinary> Ret( - new MachOUniversalBinary(std::move(Source), EC)); + new MachOUniversalBinary(Source, EC)); if (EC) return EC; return Ret.release(); } -MachOUniversalBinary::MachOUniversalBinary(std::unique_ptr<MemoryBuffer> Source, +MachOUniversalBinary::MachOUniversalBinary(MemoryBufferRef Source, std::error_code &ec) - : Binary(Binary::ID_MachOUniversalBinary, std::move(Source)), - NumberOfObjects(0) { - if (Data->getBufferSize() < sizeof(MachO::fat_header)) { + : Binary(Binary::ID_MachOUniversalBinary, Source), NumberOfObjects(0) { + if (Data.getBufferSize() < sizeof(MachO::fat_header)) { ec = object_error::invalid_file_type; return; } |