#include "llvm/Support/CBindingWrapping.h"
#include "llvm/Support/Compiler.h"
#include "llvm/Support/DataTypes.h"
+#include <memory>
namespace llvm {
/// MemoryBuffer if successful, otherwise returning null. If FileSize is
/// specified, this means that the client knows that the file exists and that
/// it has the specified size.
- static error_code getFile(Twine Filename, OwningPtr<MemoryBuffer> &result,
+ static error_code getFile(Twine Filename, OwningPtr<MemoryBuffer> &Result,
+ int64_t FileSize = -1,
+ bool RequiresNullTerminator = true);
+ static error_code getFile(Twine Filename,
+ std::unique_ptr<MemoryBuffer> &Result,
int64_t FileSize = -1,
bool RequiresNullTerminator = true);
static error_code getOpenFileSlice(int FD, const char *Filename,
OwningPtr<MemoryBuffer> &Result,
uint64_t MapSize, int64_t Offset);
+ static error_code getOpenFileSlice(int FD, const char *Filename,
+ std::unique_ptr<MemoryBuffer> &Result,
+ uint64_t MapSize, int64_t Offset);
/// Given an already-open file descriptor, read the file and return a
/// MemoryBuffer.
OwningPtr<MemoryBuffer> &Result,
uint64_t FileSize,
bool RequiresNullTerminator = true);
+ static error_code getOpenFile(int FD, const char *Filename,
+ std::unique_ptr<MemoryBuffer> &Result,
+ uint64_t FileSize,
+ bool RequiresNullTerminator = true);
/// getMemBuffer - Open the specified memory range as a MemoryBuffer. Note
/// that InputData must be null terminated if RequiresNullTerminator is true.
/// getSTDIN - Read all of stdin into a file buffer, and return it.
/// If an error occurs, this returns null and sets ec.
- static error_code getSTDIN(OwningPtr<MemoryBuffer> &result);
+ static error_code getSTDIN(OwningPtr<MemoryBuffer> &Result);
+ static error_code getSTDIN(std::unique_ptr<MemoryBuffer> &Result);
/// getFileOrSTDIN - Open the specified file as a MemoryBuffer, or open stdin
/// if the Filename is "-". If an error occurs, this returns null and sets
/// ec.
static error_code getFileOrSTDIN(StringRef Filename,
- OwningPtr<MemoryBuffer> &result,
+ OwningPtr<MemoryBuffer> &Result,
+ int64_t FileSize = -1);
+ static error_code getFileOrSTDIN(StringRef Filename,
+ std::unique_ptr<MemoryBuffer> &Result,
int64_t FileSize = -1);
//===--------------------------------------------------------------------===//
#include "llvm/Object/Archive.h"
#include "llvm/ADT/APInt.h"
+#include "llvm/ADT/OwningPtr.h"
#include "llvm/ADT/SmallString.h"
#include "llvm/ADT/Twine.h"
#include "llvm/Support/Endian.h"
return object_error::success;
}
-error_code Archive::Child::getMemoryBuffer(OwningPtr<MemoryBuffer> &Result,
+error_code Archive::Child::getMemoryBuffer(std::unique_ptr<MemoryBuffer> &Result,
bool FullPath) const {
StringRef Name;
if (error_code ec = getName(Name))
return error_code::success();
}
-error_code Archive::Child::getAsBinary(OwningPtr<Binary> &Result,
+error_code Archive::Child::getMemoryBuffer(OwningPtr<MemoryBuffer> &Result,
+ bool FullPath) const {
+ std::unique_ptr<MemoryBuffer> MB;
+ error_code ec = getMemoryBuffer(MB, FullPath);
+ Result = std::move(MB);
+ return ec;
+}
+
+error_code Archive::Child::getAsBinary(std::unique_ptr<Binary> &Result,
LLVMContext *Context) const {
- OwningPtr<Binary> ret;
- OwningPtr<MemoryBuffer> Buff;
+ std::unique_ptr<Binary> ret;
+ std::unique_ptr<MemoryBuffer> Buff;
if (error_code ec = getMemoryBuffer(Buff))
return ec;
- ErrorOr<Binary *> BinaryOrErr = createBinary(Buff.take(), Context);
+ ErrorOr<Binary *> BinaryOrErr = createBinary(Buff.release(), Context);
if (error_code EC = BinaryOrErr.getError())
return EC;
Result.reset(BinaryOrErr.get());
return object_error::success;
}
+error_code Archive::Child::getAsBinary(OwningPtr<Binary> &Result,
+ LLVMContext *Context) const {
+ std::unique_ptr<Binary> B;
+ error_code ec = getAsBinary(B, Context);
+ Result = std::move(B);
+ return ec;
+}
+
ErrorOr<Archive*> Archive::create(MemoryBuffer *Source) {
error_code EC;
- OwningPtr<Archive> Ret(new Archive(Source, EC));
+ std::unique_ptr<Archive> Ret(new Archive(Source, EC));
if (EC)
return EC;
- return Ret.take();
+ return Ret.release();
}
Archive::Archive(MemoryBuffer *source, error_code &ec)
/// in *ErrStr with a reason. If stdin is empty, this API (unlike getSTDIN)
/// returns an empty buffer.
error_code MemoryBuffer::getFileOrSTDIN(StringRef Filename,
- OwningPtr<MemoryBuffer> &result,
+ std::unique_ptr<MemoryBuffer> &Result,
int64_t FileSize) {
if (Filename == "-")
- return getSTDIN(result);
- return getFile(Filename, result, FileSize);
+ return getSTDIN(Result);
+ return getFile(Filename, Result, FileSize);
}
+error_code MemoryBuffer::getFileOrSTDIN(StringRef Filename,
+ OwningPtr<MemoryBuffer> &Result,
+ int64_t FileSize) {
+ std::unique_ptr<MemoryBuffer> MB;
+ error_code ec = getFileOrSTDIN(Filename, MB, FileSize);
+ Result = std::move(MB);
+ return ec;
+}
+
+
//===----------------------------------------------------------------------===//
// MemoryBuffer::getFile implementation.
//===----------------------------------------------------------------------===//
static error_code getMemoryBufferForStream(int FD,
StringRef BufferName,
- OwningPtr<MemoryBuffer> &result) {
+ std::unique_ptr<MemoryBuffer> &Result) {
const ssize_t ChunkSize = 4096*4;
SmallString<ChunkSize> Buffer;
ssize_t ReadBytes;
Buffer.set_size(Buffer.size() + ReadBytes);
} while (ReadBytes != 0);
- result.reset(MemoryBuffer::getMemBufferCopy(Buffer, BufferName));
+ Result.reset(MemoryBuffer::getMemBufferCopy(Buffer, BufferName));
return error_code::success();
}
static error_code getFileAux(const char *Filename,
- OwningPtr<MemoryBuffer> &result, int64_t FileSize,
+ std::unique_ptr<MemoryBuffer> &Result,
+ int64_t FileSize,
bool RequiresNullTerminator);
error_code MemoryBuffer::getFile(Twine Filename,
- OwningPtr<MemoryBuffer> &result,
+ std::unique_ptr<MemoryBuffer> &Result,
int64_t FileSize,
bool RequiresNullTerminator) {
// Ensure the path is null terminated.
SmallString<256> PathBuf;
StringRef NullTerminatedName = Filename.toNullTerminatedStringRef(PathBuf);
- return getFileAux(NullTerminatedName.data(), result, FileSize,
+ return getFileAux(NullTerminatedName.data(), Result, FileSize,
RequiresNullTerminator);
}
+error_code MemoryBuffer::getFile(Twine Filename,
+ OwningPtr<MemoryBuffer> &Result,
+ int64_t FileSize,
+ bool RequiresNullTerminator) {
+ std::unique_ptr<MemoryBuffer> MB;
+ error_code ec = getFile(Filename, MB, FileSize, RequiresNullTerminator);
+ Result = std::move(MB);
+ return ec;
+}
+
static error_code getOpenFileImpl(int FD, const char *Filename,
- OwningPtr<MemoryBuffer> &Result,
+ std::unique_ptr<MemoryBuffer> &Result,
uint64_t FileSize, uint64_t MapSize,
int64_t Offset, bool RequiresNullTerminator);
static error_code getFileAux(const char *Filename,
- OwningPtr<MemoryBuffer> &result, int64_t FileSize,
+ std::unique_ptr<MemoryBuffer> &Result, int64_t FileSize,
bool RequiresNullTerminator) {
int FD;
error_code EC = sys::fs::openFileForRead(Filename, FD);
if (EC)
return EC;
- error_code ret = getOpenFileImpl(FD, Filename, result, FileSize, FileSize, 0,
+ error_code ret = getOpenFileImpl(FD, Filename, Result, FileSize, FileSize, 0,
RequiresNullTerminator);
close(FD);
return ret;
}
static error_code getOpenFileImpl(int FD, const char *Filename,
- OwningPtr<MemoryBuffer> &result,
+ std::unique_ptr<MemoryBuffer> &Result,
uint64_t FileSize, uint64_t MapSize,
int64_t Offset, bool RequiresNullTerminator) {
static int PageSize = sys::process::get_self()->page_size();
sys::fs::file_type Type = Status.type();
if (Type != sys::fs::file_type::regular_file &&
Type != sys::fs::file_type::block_file)
- return getMemoryBufferForStream(FD, Filename, result);
+ return getMemoryBufferForStream(FD, Filename, Result);
FileSize = Status.getSize();
}
if (shouldUseMmap(FD, FileSize, MapSize, Offset, RequiresNullTerminator,
PageSize)) {
error_code EC;
- result.reset(new (NamedBufferAlloc(Filename)) MemoryBufferMMapFile(
+ Result.reset(new (NamedBufferAlloc(Filename)) MemoryBufferMMapFile(
RequiresNullTerminator, FD, MapSize, Offset, EC));
if (!EC)
return error_code::success();
return make_error_code(errc::not_enough_memory);
}
- OwningPtr<MemoryBuffer> SB(Buf);
+ std::unique_ptr<MemoryBuffer> SB(Buf);
char *BufPtr = const_cast<char*>(SB->getBufferStart());
size_t BytesLeft = MapSize;
BufPtr += NumRead;
}
- result.swap(SB);
+ Result.swap(SB);
return error_code::success();
}
error_code MemoryBuffer::getOpenFile(int FD, const char *Filename,
- OwningPtr<MemoryBuffer> &Result,
+ std::unique_ptr<MemoryBuffer> &Result,
uint64_t FileSize,
bool RequiresNullTerminator) {
return getOpenFileImpl(FD, Filename, Result, FileSize, FileSize, 0,
RequiresNullTerminator);
}
+error_code MemoryBuffer::getOpenFile(int FD, const char *Filename,
+ OwningPtr<MemoryBuffer> &Result,
+ uint64_t FileSize,
+ bool RequiresNullTerminator) {
+ std::unique_ptr<MemoryBuffer> MB;
+ error_code ec = getOpenFileImpl(FD, Filename, MB, FileSize, FileSize, 0,
+ RequiresNullTerminator);
+ Result = std::move(MB);
+ return ec;
+}
+
error_code MemoryBuffer::getOpenFileSlice(int FD, const char *Filename,
- OwningPtr<MemoryBuffer> &Result,
+ std::unique_ptr<MemoryBuffer> &Result,
uint64_t MapSize, int64_t Offset) {
return getOpenFileImpl(FD, Filename, Result, -1, MapSize, Offset, false);
}
+error_code MemoryBuffer::getOpenFileSlice(int FD, const char *Filename,
+ OwningPtr<MemoryBuffer> &Result,
+ uint64_t MapSize, int64_t Offset) {
+ std::unique_ptr<MemoryBuffer> MB;
+ error_code ec = getOpenFileImpl(FD, Filename, MB, -1, MapSize, Offset, false);
+ Result = std::move(MB);
+ return ec;
+}
+
//===----------------------------------------------------------------------===//
// MemoryBuffer::getSTDIN implementation.
//===----------------------------------------------------------------------===//
-error_code MemoryBuffer::getSTDIN(OwningPtr<MemoryBuffer> &result) {
+error_code MemoryBuffer::getSTDIN(std::unique_ptr<MemoryBuffer> &Result) {
// Read in all of the data from stdin, we cannot mmap stdin.
//
// FIXME: That isn't necessarily true, we should try to mmap stdin and
// fallback if it fails.
sys::ChangeStdinToBinary();
- return getMemoryBufferForStream(0, "<stdin>", result);
+ return getMemoryBufferForStream(0, "<stdin>", Result);
+}
+
+error_code MemoryBuffer::getSTDIN(OwningPtr<MemoryBuffer> &Result) {
+ std::unique_ptr<MemoryBuffer> MB;
+ error_code ec = getSTDIN(MB);
+ Result = std::move(MB);
+ return ec;
}