From 48af1c2a1a5148bc6a143bc0d8650ef744f2f7c3 Mon Sep 17 00:00:00 2001 From: Rafael Espindola Date: Tue, 19 Aug 2014 18:44:46 +0000 Subject: 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 --- llvm/lib/Object/Object.cpp | 43 +++++++++++++++++++++++++------------------ 1 file changed, 25 insertions(+), 18 deletions(-) (limited to 'llvm/lib/Object/Object.cpp') diff --git a/llvm/lib/Object/Object.cpp b/llvm/lib/Object/Object.cpp index 2b3861b4bdc..4d3478bc14f 100644 --- a/llvm/lib/Object/Object.cpp +++ b/llvm/lib/Object/Object.cpp @@ -19,12 +19,13 @@ using namespace llvm; using namespace object; -inline ObjectFile *unwrap(LLVMObjectFileRef OF) { - return reinterpret_cast(OF); +inline OwningBinary *unwrap(LLVMObjectFileRef OF) { + return reinterpret_cast *>(OF); } -inline LLVMObjectFileRef wrap(const ObjectFile *OF) { - return reinterpret_cast(const_cast(OF)); +inline LLVMObjectFileRef wrap(const OwningBinary *OF) { + return reinterpret_cast( + const_cast *>(OF)); } inline section_iterator *unwrap(LLVMSectionIteratorRef SI) { @@ -61,10 +62,12 @@ wrap(const relocation_iterator *SI) { LLVMObjectFileRef LLVMCreateObjectFile(LLVMMemoryBufferRef MemBuf) { std::unique_ptr Buf(unwrap(MemBuf)); ErrorOr> ObjOrErr( - ObjectFile::createObjectFile(Buf)); - Buf.release(); - ObjectFile *Obj = ObjOrErr ? ObjOrErr.get().release() : nullptr; - return wrap(Obj); + ObjectFile::createObjectFile(Buf->getMemBufferRef())); + std::unique_ptr Obj; + if (ObjOrErr) + Obj = std::move(ObjOrErr.get()); + auto *Ret = new OwningBinary(std::move(Obj), std::move(Buf)); + return wrap(Ret); } void LLVMDisposeObjectFile(LLVMObjectFileRef ObjectFile) { @@ -72,8 +75,9 @@ void LLVMDisposeObjectFile(LLVMObjectFileRef ObjectFile) { } // ObjectFile Section iterators -LLVMSectionIteratorRef LLVMGetSections(LLVMObjectFileRef ObjectFile) { - section_iterator SI = unwrap(ObjectFile)->section_begin(); +LLVMSectionIteratorRef LLVMGetSections(LLVMObjectFileRef OF) { + OwningBinary *OB = unwrap(OF); + section_iterator SI = OB->getBinary()->section_begin(); return wrap(new section_iterator(SI)); } @@ -81,9 +85,10 @@ void LLVMDisposeSectionIterator(LLVMSectionIteratorRef SI) { delete unwrap(SI); } -LLVMBool LLVMIsSectionIteratorAtEnd(LLVMObjectFileRef ObjectFile, - LLVMSectionIteratorRef SI) { - return (*unwrap(SI) == unwrap(ObjectFile)->section_end()) ? 1 : 0; +LLVMBool LLVMIsSectionIteratorAtEnd(LLVMObjectFileRef OF, + LLVMSectionIteratorRef SI) { + OwningBinary *OB = unwrap(OF); + return (*unwrap(SI) == OB->getBinary()->section_end()) ? 1 : 0; } void LLVMMoveToNextSection(LLVMSectionIteratorRef SI) { @@ -97,8 +102,9 @@ void LLVMMoveToContainingSection(LLVMSectionIteratorRef Sect, } // ObjectFile Symbol iterators -LLVMSymbolIteratorRef LLVMGetSymbols(LLVMObjectFileRef ObjectFile) { - symbol_iterator SI = unwrap(ObjectFile)->symbol_begin(); +LLVMSymbolIteratorRef LLVMGetSymbols(LLVMObjectFileRef OF) { + OwningBinary *OB = unwrap(OF); + symbol_iterator SI = OB->getBinary()->symbol_begin(); return wrap(new symbol_iterator(SI)); } @@ -106,9 +112,10 @@ void LLVMDisposeSymbolIterator(LLVMSymbolIteratorRef SI) { delete unwrap(SI); } -LLVMBool LLVMIsSymbolIteratorAtEnd(LLVMObjectFileRef ObjectFile, - LLVMSymbolIteratorRef SI) { - return (*unwrap(SI) == unwrap(ObjectFile)->symbol_end()) ? 1 : 0; +LLVMBool LLVMIsSymbolIteratorAtEnd(LLVMObjectFileRef OF, + LLVMSymbolIteratorRef SI) { + OwningBinary *OB = unwrap(OF); + return (*unwrap(SI) == OB->getBinary()->symbol_end()) ? 1 : 0; } void LLVMMoveToNextSymbol(LLVMSymbolIteratorRef SI) { -- cgit v1.2.3