Make ObjectFile and BitcodeReader always own the MemoryBuffer.

This allows us to just use a std::unique_ptr to store the pointer to the buffer.
The flip side is that they have to support releasing the buffer back to the
caller.

Overall this looks like a more efficient and less brittle api.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@211542 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Rafael Espindola
2014-06-23 21:53:12 +00:00
parent 7e7e89f178
commit 1f659329b6
23 changed files with 117 additions and 154 deletions

View File

@@ -13,6 +13,7 @@
#include "llvm/Bitcode/ReaderWriter.h"
#include "llvm/IR/LLVMContext.h"
#include "llvm/IR/GVMaterializer.h"
#include "llvm/IR/Mangler.h"
#include "llvm/IR/Module.h"
#include "llvm/Object/IRObjectFile.h"
@@ -21,10 +22,9 @@ using namespace llvm;
using namespace object;
IRObjectFile::IRObjectFile(MemoryBuffer *Object, std::error_code &EC,
LLVMContext &Context, bool BufferOwned)
: SymbolicFile(Binary::ID_IR, Object, BufferOwned) {
ErrorOr<Module *> MOrErr =
getLazyBitcodeModule(Object, Context, /*BufferOwned*/ false);
LLVMContext &Context)
: SymbolicFile(Binary::ID_IR, Object) {
ErrorOr<Module *> MOrErr = getLazyBitcodeModule(Object, Context);
if ((EC = MOrErr.getError()))
return;
@@ -38,6 +38,8 @@ IRObjectFile::IRObjectFile(MemoryBuffer *Object, std::error_code &EC,
Mang.reset(new Mangler(DL));
}
IRObjectFile::~IRObjectFile() { M->getMaterializer()->releaseBuffer(); }
static const GlobalValue &getGV(DataRefImpl &Symb) {
return *reinterpret_cast<GlobalValue*>(Symb.p & ~uintptr_t(3));
}
@@ -151,11 +153,11 @@ basic_symbol_iterator IRObjectFile::symbol_end_impl() const {
return basic_symbol_iterator(BasicSymbolRef(Ret, this));
}
ErrorOr<SymbolicFile *> llvm::object::SymbolicFile::createIRObjectFile(
MemoryBuffer *Object, LLVMContext &Context, bool BufferOwned) {
ErrorOr<SymbolicFile *>
llvm::object::SymbolicFile::createIRObjectFile(MemoryBuffer *Object,
LLVMContext &Context) {
std::error_code EC;
std::unique_ptr<IRObjectFile> Ret(
new IRObjectFile(Object, EC, Context, BufferOwned));
std::unique_ptr<IRObjectFile> Ret(new IRObjectFile(Object, EC, Context));
if (EC)
return EC;
return Ret.release();