summaryrefslogtreecommitdiffstats
path: root/llvm/lib/Object/Object.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/Object.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/Object.cpp')
-rw-r--r--llvm/lib/Object/Object.cpp43
1 files changed, 25 insertions, 18 deletions
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<ObjectFile*>(OF);
+inline OwningBinary<ObjectFile> *unwrap(LLVMObjectFileRef OF) {
+ return reinterpret_cast<OwningBinary<ObjectFile> *>(OF);
}
-inline LLVMObjectFileRef wrap(const ObjectFile *OF) {
- return reinterpret_cast<LLVMObjectFileRef>(const_cast<ObjectFile*>(OF));
+inline LLVMObjectFileRef wrap(const OwningBinary<ObjectFile> *OF) {
+ return reinterpret_cast<LLVMObjectFileRef>(
+ const_cast<OwningBinary<ObjectFile> *>(OF));
}
inline section_iterator *unwrap(LLVMSectionIteratorRef SI) {
@@ -61,10 +62,12 @@ wrap(const relocation_iterator *SI) {
LLVMObjectFileRef LLVMCreateObjectFile(LLVMMemoryBufferRef MemBuf) {
std::unique_ptr<MemoryBuffer> Buf(unwrap(MemBuf));
ErrorOr<std::unique_ptr<ObjectFile>> ObjOrErr(
- ObjectFile::createObjectFile(Buf));
- Buf.release();
- ObjectFile *Obj = ObjOrErr ? ObjOrErr.get().release() : nullptr;
- return wrap(Obj);
+ ObjectFile::createObjectFile(Buf->getMemBufferRef()));
+ std::unique_ptr<ObjectFile> Obj;
+ if (ObjOrErr)
+ Obj = std::move(ObjOrErr.get());
+ auto *Ret = new OwningBinary<ObjectFile>(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<ObjectFile> *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<ObjectFile> *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<ObjectFile> *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<ObjectFile> *OB = unwrap(OF);
+ return (*unwrap(SI) == OB->getBinary()->symbol_end()) ? 1 : 0;
}
void LLVMMoveToNextSymbol(LLVMSymbolIteratorRef SI) {
OpenPOWER on IntegriCloud