summaryrefslogtreecommitdiffstats
path: root/llvm/lib/Object/Binary.cpp
diff options
context:
space:
mode:
authorRafael Espindola <rafael.espindola@gmail.com>2014-08-19 18:44:46 +0000
committerRafael Espindola <rafael.espindola@gmail.com>2014-08-19 18:44:46 +0000
commit48af1c2a1a5148bc6a143bc0d8650ef744f2f7c3 (patch)
treeb702bea9017a8fb461da713a083af53f596b95ef /llvm/lib/Object/Binary.cpp
parentf17f03e00e5dc20f2cdbfbd4cc36e47a8966d2f7 (diff)
downloadbcm5719-llvm-48af1c2a1a5148bc6a143bc0d8650ef744f2f7c3.tar.gz
bcm5719-llvm-48af1c2a1a5148bc6a143bc0d8650ef744f2f7c3.zip
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. llvm-svn: 216002
Diffstat (limited to 'llvm/lib/Object/Binary.cpp')
-rw-r--r--llvm/lib/Object/Binary.cpp37
1 files changed, 21 insertions, 16 deletions
diff --git a/llvm/lib/Object/Binary.cpp b/llvm/lib/Object/Binary.cpp
index 89e9d732ce9..d23ee590569 100644
--- a/llvm/lib/Object/Binary.cpp
+++ b/llvm/lib/Object/Binary.cpp
@@ -27,25 +27,22 @@ using namespace object;
Binary::~Binary() {}
-Binary::Binary(unsigned int Type, std::unique_ptr<MemoryBuffer> Source)
- : TypeID(Type), Data(std::move(Source)) {}
+Binary::Binary(unsigned int Type, MemoryBufferRef Source)
+ : TypeID(Type), Data(Source) {}
-StringRef Binary::getData() const {
- return Data->getBuffer();
-}
+StringRef Binary::getData() const { return Data.getBuffer(); }
-StringRef Binary::getFileName() const {
- return Data->getBufferIdentifier();
-}
+StringRef Binary::getFileName() const { return Data.getBufferIdentifier(); }
+
+MemoryBufferRef Binary::getMemoryBufferRef() const { return Data; }
-ErrorOr<std::unique_ptr<Binary>>
-object::createBinary(std::unique_ptr<MemoryBuffer> Buffer,
- LLVMContext *Context) {
- sys::fs::file_magic Type = sys::fs::identify_magic(Buffer->getBuffer());
+ErrorOr<std::unique_ptr<Binary>> object::createBinary(MemoryBufferRef Buffer,
+ LLVMContext *Context) {
+ sys::fs::file_magic Type = sys::fs::identify_magic(Buffer.getBuffer());
switch (Type) {
case sys::fs::file_magic::archive:
- return Archive::create(std::move(Buffer));
+ return Archive::create(Buffer);
case sys::fs::file_magic::elf_relocatable:
case sys::fs::file_magic::elf_executable:
case sys::fs::file_magic::elf_shared_object:
@@ -66,7 +63,7 @@ object::createBinary(std::unique_ptr<MemoryBuffer> Buffer,
case sys::fs::file_magic::bitcode:
return ObjectFile::createSymbolicFile(Buffer, Type, Context);
case sys::fs::file_magic::macho_universal_binary:
- return MachOUniversalBinary::create(std::move(Buffer));
+ return MachOUniversalBinary::create(Buffer);
case sys::fs::file_magic::unknown:
case sys::fs::file_magic::windows_resource:
// Unrecognized object file format.
@@ -75,10 +72,18 @@ object::createBinary(std::unique_ptr<MemoryBuffer> Buffer,
llvm_unreachable("Unexpected Binary File Type");
}
-ErrorOr<std::unique_ptr<Binary>> object::createBinary(StringRef Path) {
+ErrorOr<OwningBinary<Binary>> object::createBinary(StringRef Path) {
ErrorOr<std::unique_ptr<MemoryBuffer>> FileOrErr =
MemoryBuffer::getFileOrSTDIN(Path);
if (std::error_code EC = FileOrErr.getError())
return EC;
- return createBinary(std::move(*FileOrErr));
+ std::unique_ptr<MemoryBuffer> &Buffer = FileOrErr.get();
+
+ ErrorOr<std::unique_ptr<Binary>> BinOrErr =
+ createBinary(Buffer->getMemBufferRef());
+ if (std::error_code EC = BinOrErr.getError())
+ return EC;
+ std::unique_ptr<Binary> &Bin = BinOrErr.get();
+
+ return OwningBinary<Binary>(std::move(Bin), std::move(Buffer));
}
OpenPOWER on IntegriCloud