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
This commit is contained in:
Rafael Espindola
2014-08-19 18:44:46 +00:00
parent 2ac376ba34
commit 548f2b6e8f
48 changed files with 375 additions and 314 deletions

View File

@@ -109,7 +109,7 @@ Archive::Child Archive::Child::getNext() const {
const char *NextLoc = Data.data() + SpaceToSkip;
// Check to see if this is past the end of the archive.
if (NextLoc >= Parent->Data->getBufferEnd())
if (NextLoc >= Parent->Data.getBufferEnd())
return Child(Parent, nullptr);
return Child(Parent, NextLoc);
@@ -159,45 +159,36 @@ ErrorOr<StringRef> Archive::Child::getName() const {
return name;
}
ErrorOr<std::unique_ptr<MemoryBuffer>>
Archive::Child::getMemoryBuffer(bool FullPath) const {
ErrorOr<MemoryBufferRef> Archive::Child::getMemoryBufferRef() const {
ErrorOr<StringRef> NameOrErr = getName();
if (std::error_code EC = NameOrErr.getError())
return EC;
StringRef Name = NameOrErr.get();
SmallString<128> Path;
std::unique_ptr<MemoryBuffer> Ret(MemoryBuffer::getMemBuffer(
getBuffer(),
FullPath
? (Twine(Parent->getFileName()) + "(" + Name + ")").toStringRef(Path)
: Name,
false));
return std::move(Ret);
return MemoryBufferRef(getBuffer(), Name);
}
ErrorOr<std::unique_ptr<Binary>>
Archive::Child::getAsBinary(LLVMContext *Context) const {
ErrorOr<std::unique_ptr<MemoryBuffer>> BuffOrErr = getMemoryBuffer();
ErrorOr<MemoryBufferRef> BuffOrErr = getMemoryBufferRef();
if (std::error_code EC = BuffOrErr.getError())
return EC;
return createBinary(std::move(*BuffOrErr), Context);
return createBinary(BuffOrErr.get(), Context);
}
ErrorOr<std::unique_ptr<Archive>>
Archive::create(std::unique_ptr<MemoryBuffer> Source) {
ErrorOr<std::unique_ptr<Archive>> Archive::create(MemoryBufferRef Source) {
std::error_code EC;
std::unique_ptr<Archive> Ret(new Archive(std::move(Source), EC));
std::unique_ptr<Archive> Ret(new Archive(Source, EC));
if (EC)
return EC;
return std::move(Ret);
}
Archive::Archive(std::unique_ptr<MemoryBuffer> Source, std::error_code &ec)
: Binary(Binary::ID_Archive, std::move(Source)), SymbolTable(child_end()) {
Archive::Archive(MemoryBufferRef Source, std::error_code &ec)
: Binary(Binary::ID_Archive, Source), SymbolTable(child_end()) {
// Check for sufficient magic.
if (Data->getBufferSize() < 8 ||
StringRef(Data->getBufferStart(), 8) != Magic) {
if (Data.getBufferSize() < 8 ||
StringRef(Data.getBufferStart(), 8) != Magic) {
ec = object_error::invalid_file_type;
return;
}
@@ -311,13 +302,13 @@ Archive::Archive(std::unique_ptr<MemoryBuffer> Source, std::error_code &ec)
}
Archive::child_iterator Archive::child_begin(bool SkipInternal) const {
if (Data->getBufferSize() == 8) // empty archive.
if (Data.getBufferSize() == 8) // empty archive.
return child_end();
if (SkipInternal)
return FirstRegular;
const char *Loc = Data->getBufferStart() + strlen(Magic);
const char *Loc = Data.getBufferStart() + strlen(Magic);
Child c(this, Loc);
return c;
}