2007-04-29 08:58:52 +02:00
|
|
|
//===--- MemoryBuffer.cpp - Memory Buffer implementation ------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-29 21:36:04 +01:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2007-04-29 08:58:52 +02:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file implements the MemoryBuffer interface.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/Support/MemoryBuffer.h"
|
2008-04-01 08:05:21 +02:00
|
|
|
#include "llvm/ADT/OwningPtr.h"
|
|
|
|
#include "llvm/ADT/SmallString.h"
|
2011-11-22 13:31:53 +01:00
|
|
|
#include "llvm/Config/config.h"
|
2010-11-29 19:16:10 +01:00
|
|
|
#include "llvm/Support/Errno.h"
|
2012-06-20 22:21:33 +02:00
|
|
|
#include "llvm/Support/FileSystem.h"
|
2012-12-03 17:50:05 +01:00
|
|
|
#include "llvm/Support/MathExtras.h"
|
2010-11-29 19:16:10 +01:00
|
|
|
#include "llvm/Support/Path.h"
|
|
|
|
#include "llvm/Support/Process.h"
|
|
|
|
#include "llvm/Support/Program.h"
|
2010-12-09 18:36:48 +01:00
|
|
|
#include "llvm/Support/system_error.h"
|
2007-04-29 16:21:44 +02:00
|
|
|
#include <cassert>
|
2012-12-03 17:50:05 +01:00
|
|
|
#include <cerrno>
|
2007-04-29 08:58:52 +02:00
|
|
|
#include <cstdio>
|
|
|
|
#include <cstring>
|
2010-12-19 21:43:38 +01:00
|
|
|
#include <new>
|
2008-04-01 08:05:21 +02:00
|
|
|
#include <sys/stat.h>
|
2012-12-03 17:50:05 +01:00
|
|
|
#include <sys/types.h>
|
2008-04-01 08:05:21 +02:00
|
|
|
#if !defined(_MSC_VER) && !defined(__MINGW32__)
|
|
|
|
#include <unistd.h>
|
|
|
|
#else
|
|
|
|
#include <io.h>
|
2013-06-12 16:11:15 +02:00
|
|
|
// Simplistic definitinos of these macros for use in getOpenFile.
|
2013-02-19 23:38:58 +01:00
|
|
|
#ifndef S_ISREG
|
|
|
|
#define S_ISREG(x) (1)
|
|
|
|
#endif
|
|
|
|
#ifndef S_ISBLK
|
|
|
|
#define S_ISBLK(x) (0)
|
2012-11-06 18:08:09 +01:00
|
|
|
#endif
|
2008-04-01 08:05:21 +02:00
|
|
|
#endif
|
2007-04-29 08:58:52 +02:00
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// MemoryBuffer implementation itself.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2010-06-25 13:50:40 +02:00
|
|
|
MemoryBuffer::~MemoryBuffer() { }
|
2007-04-29 08:58:52 +02:00
|
|
|
|
|
|
|
/// init - Initialize this MemoryBuffer as a reference to externally allocated
|
|
|
|
/// memory, memory that we know is already null terminated.
|
2011-03-10 19:33:29 +01:00
|
|
|
void MemoryBuffer::init(const char *BufStart, const char *BufEnd,
|
|
|
|
bool RequiresNullTerminator) {
|
2011-03-18 03:55:51 +01:00
|
|
|
assert((!RequiresNullTerminator || BufEnd[0] == 0) &&
|
2011-03-10 19:33:29 +01:00
|
|
|
"Buffer is not null terminated!");
|
2007-04-29 08:58:52 +02:00
|
|
|
BufferStart = BufStart;
|
|
|
|
BufferEnd = BufEnd;
|
|
|
|
}
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// MemoryBufferMem implementation.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2010-06-25 13:50:40 +02:00
|
|
|
/// CopyStringRef - Copies contents of a StringRef into a block of memory and
|
|
|
|
/// null-terminates it.
|
|
|
|
static void CopyStringRef(char *Memory, StringRef Data) {
|
|
|
|
memcpy(Memory, Data.data(), Data.size());
|
|
|
|
Memory[Data.size()] = 0; // Null terminate string.
|
|
|
|
}
|
|
|
|
|
2013-03-30 16:23:08 +01:00
|
|
|
namespace {
|
2013-03-12 20:28:19 +01:00
|
|
|
struct NamedBufferAlloc {
|
|
|
|
StringRef Name;
|
|
|
|
NamedBufferAlloc(StringRef Name) : Name(Name) {}
|
|
|
|
};
|
2013-03-30 16:23:08 +01:00
|
|
|
}
|
2013-03-12 20:28:19 +01:00
|
|
|
|
|
|
|
void *operator new(size_t N, const NamedBufferAlloc &Alloc) {
|
|
|
|
char *Mem = static_cast<char *>(operator new(N + Alloc.Name.size() + 1));
|
|
|
|
CopyStringRef(Mem + N, Alloc.Name);
|
|
|
|
return Mem;
|
2010-06-25 13:50:40 +02:00
|
|
|
}
|
|
|
|
|
2007-04-29 08:58:52 +02:00
|
|
|
namespace {
|
2010-06-25 13:50:40 +02:00
|
|
|
/// MemoryBufferMem - Named MemoryBuffer pointing to a block of memory.
|
2007-04-29 08:58:52 +02:00
|
|
|
class MemoryBufferMem : public MemoryBuffer {
|
|
|
|
public:
|
2011-03-10 19:33:29 +01:00
|
|
|
MemoryBufferMem(StringRef InputData, bool RequiresNullTerminator) {
|
|
|
|
init(InputData.begin(), InputData.end(), RequiresNullTerminator);
|
2007-04-29 08:58:52 +02:00
|
|
|
}
|
2010-06-25 13:50:40 +02:00
|
|
|
|
2014-03-02 10:09:27 +01:00
|
|
|
virtual const char *getBufferIdentifier() const override {
|
2010-06-25 13:50:40 +02:00
|
|
|
// The name is stored after the class itself.
|
|
|
|
return reinterpret_cast<const char*>(this + 1);
|
2007-04-29 08:58:52 +02:00
|
|
|
}
|
2012-09-23 04:12:10 +02:00
|
|
|
|
2014-03-02 10:09:27 +01:00
|
|
|
virtual BufferKind getBufferKind() const override {
|
2011-04-28 22:34:18 +02:00
|
|
|
return MemoryBuffer_Malloc;
|
|
|
|
}
|
2007-04-29 08:58:52 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/// getMemBuffer - Open the specified memory range as a MemoryBuffer. Note
|
2011-05-22 02:50:53 +02:00
|
|
|
/// that InputData must be a null terminated if RequiresNullTerminator is true!
|
2010-04-06 00:42:30 +02:00
|
|
|
MemoryBuffer *MemoryBuffer::getMemBuffer(StringRef InputData,
|
2011-03-17 23:18:42 +01:00
|
|
|
StringRef BufferName,
|
|
|
|
bool RequiresNullTerminator) {
|
2013-03-12 20:28:19 +01:00
|
|
|
return new (NamedBufferAlloc(BufferName))
|
|
|
|
MemoryBufferMem(InputData, RequiresNullTerminator);
|
2007-04-29 08:58:52 +02:00
|
|
|
}
|
|
|
|
|
2007-10-09 23:46:38 +02:00
|
|
|
/// getMemBufferCopy - Open the specified memory range as a MemoryBuffer,
|
|
|
|
/// copying the contents and taking ownership of it. This has no requirements
|
|
|
|
/// on EndPtr[0].
|
2010-04-06 00:42:30 +02:00
|
|
|
MemoryBuffer *MemoryBuffer::getMemBufferCopy(StringRef InputData,
|
2010-06-25 13:50:40 +02:00
|
|
|
StringRef BufferName) {
|
|
|
|
MemoryBuffer *Buf = getNewUninitMemBuffer(InputData.size(), BufferName);
|
|
|
|
if (!Buf) return 0;
|
|
|
|
memcpy(const_cast<char*>(Buf->getBufferStart()), InputData.data(),
|
|
|
|
InputData.size());
|
|
|
|
return Buf;
|
2007-10-09 23:46:38 +02:00
|
|
|
}
|
|
|
|
|
2007-04-29 08:58:52 +02:00
|
|
|
/// getNewUninitMemBuffer - Allocate a new MemoryBuffer of the specified size
|
2010-06-25 13:50:40 +02:00
|
|
|
/// that is not initialized. Note that the caller should initialize the
|
|
|
|
/// memory allocated by this method. The memory is owned by the MemoryBuffer
|
|
|
|
/// object.
|
2008-05-05 20:30:58 +02:00
|
|
|
MemoryBuffer *MemoryBuffer::getNewUninitMemBuffer(size_t Size,
|
2009-11-10 01:43:58 +01:00
|
|
|
StringRef BufferName) {
|
2010-06-25 13:50:40 +02:00
|
|
|
// Allocate space for the MemoryBuffer, the data and the name. It is important
|
|
|
|
// that MemoryBuffer and data are aligned so PointerIntPair works with them.
|
2013-12-16 19:18:12 +01:00
|
|
|
// TODO: Is 16-byte alignment enough? We copy small object files with large
|
|
|
|
// alignment expectations into this buffer.
|
2010-06-25 13:50:40 +02:00
|
|
|
size_t AlignedStringLen =
|
2013-12-16 19:18:12 +01:00
|
|
|
RoundUpToAlignment(sizeof(MemoryBufferMem) + BufferName.size() + 1, 16);
|
2010-06-25 13:50:40 +02:00
|
|
|
size_t RealLen = AlignedStringLen + Size + 1;
|
|
|
|
char *Mem = static_cast<char*>(operator new(RealLen, std::nothrow));
|
|
|
|
if (!Mem) return 0;
|
|
|
|
|
|
|
|
// The name is stored after the class itself.
|
|
|
|
CopyStringRef(Mem + sizeof(MemoryBufferMem), BufferName);
|
|
|
|
|
|
|
|
// The buffer begins after the name and must be aligned.
|
|
|
|
char *Buf = Mem + AlignedStringLen;
|
|
|
|
Buf[Size] = 0; // Null terminate buffer.
|
|
|
|
|
2011-03-10 19:33:29 +01:00
|
|
|
return new (Mem) MemoryBufferMem(StringRef(Buf, Size), true);
|
2007-04-29 08:58:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// getNewMemBuffer - Allocate a new MemoryBuffer of the specified size that
|
|
|
|
/// is completely initialized to zeros. Note that the caller should
|
|
|
|
/// initialize the memory allocated by this method. The memory is owned by
|
|
|
|
/// the MemoryBuffer object.
|
2010-06-25 13:50:40 +02:00
|
|
|
MemoryBuffer *MemoryBuffer::getNewMemBuffer(size_t Size, StringRef BufferName) {
|
2007-04-29 08:58:52 +02:00
|
|
|
MemoryBuffer *SB = getNewUninitMemBuffer(Size, BufferName);
|
2009-02-13 08:54:34 +01:00
|
|
|
if (!SB) return 0;
|
2010-06-25 13:50:40 +02:00
|
|
|
memset(const_cast<char*>(SB->getBufferStart()), 0, Size);
|
2007-04-29 08:58:52 +02:00
|
|
|
return SB;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-11-18 19:52:28 +01:00
|
|
|
/// getFileOrSTDIN - Open the specified file as a MemoryBuffer, or open stdin
|
|
|
|
/// if the Filename is "-". If an error occurs, this returns null and fills
|
|
|
|
/// in *ErrStr with a reason. If stdin is empty, this API (unlike getSTDIN)
|
|
|
|
/// returns an empty buffer.
|
2010-12-16 04:29:14 +01:00
|
|
|
error_code MemoryBuffer::getFileOrSTDIN(StringRef Filename,
|
2014-03-05 11:27:34 +01:00
|
|
|
std::unique_ptr<MemoryBuffer> &Result,
|
2010-12-16 04:29:14 +01:00
|
|
|
int64_t FileSize) {
|
2009-11-10 01:43:58 +01:00
|
|
|
if (Filename == "-")
|
2014-03-05 11:27:34 +01:00
|
|
|
return getSTDIN(Result);
|
|
|
|
return getFile(Filename, Result, FileSize);
|
2007-11-18 19:52:28 +01:00
|
|
|
}
|
|
|
|
|
2014-03-05 11:27:34 +01:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-04-29 08:58:52 +02:00
|
|
|
//===----------------------------------------------------------------------===//
|
2008-04-01 08:05:21 +02:00
|
|
|
// MemoryBuffer::getFile implementation.
|
2007-04-29 08:58:52 +02:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
namespace {
|
2013-09-10 21:54:54 +02:00
|
|
|
/// \brief Memory maps a file descriptor using sys::fs::mapped_file_region.
|
2013-03-12 20:28:19 +01:00
|
|
|
///
|
|
|
|
/// This handles converting the offset into a legal offset on the platform.
|
|
|
|
class MemoryBufferMMapFile : public MemoryBuffer {
|
|
|
|
sys::fs::mapped_file_region MFR;
|
|
|
|
|
|
|
|
static uint64_t getLegalMapOffset(uint64_t Offset) {
|
|
|
|
return Offset & ~(sys::fs::mapped_file_region::alignment() - 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
static uint64_t getLegalMapSize(uint64_t Len, uint64_t Offset) {
|
|
|
|
return Len + (Offset - getLegalMapOffset(Offset));
|
|
|
|
}
|
2010-06-25 13:50:40 +02:00
|
|
|
|
2013-03-12 20:28:19 +01:00
|
|
|
const char *getStart(uint64_t Len, uint64_t Offset) {
|
|
|
|
return MFR.const_data() + (Offset - getLegalMapOffset(Offset));
|
|
|
|
}
|
2011-03-10 19:33:29 +01:00
|
|
|
|
2013-03-12 20:28:19 +01:00
|
|
|
public:
|
|
|
|
MemoryBufferMMapFile(bool RequiresNullTerminator, int FD, uint64_t Len,
|
|
|
|
uint64_t Offset, error_code EC)
|
2013-03-14 01:20:10 +01:00
|
|
|
: MFR(FD, false, sys::fs::mapped_file_region::readonly,
|
2013-03-12 20:28:19 +01:00
|
|
|
getLegalMapSize(Len, Offset), getLegalMapOffset(Offset), EC) {
|
|
|
|
if (!EC) {
|
|
|
|
const char *Start = getStart(Len, Offset);
|
|
|
|
init(Start, Start + Len, RequiresNullTerminator);
|
|
|
|
}
|
|
|
|
}
|
2011-03-10 19:33:29 +01:00
|
|
|
|
2014-03-02 10:09:27 +01:00
|
|
|
virtual const char *getBufferIdentifier() const override {
|
2013-03-12 20:28:19 +01:00
|
|
|
// The name is stored after the class itself.
|
|
|
|
return reinterpret_cast<const char *>(this + 1);
|
2008-04-01 08:05:21 +02:00
|
|
|
}
|
2012-09-23 04:12:10 +02:00
|
|
|
|
2014-03-02 10:09:27 +01:00
|
|
|
virtual BufferKind getBufferKind() const override {
|
2011-04-28 22:34:18 +02:00
|
|
|
return MemoryBuffer_MMap;
|
|
|
|
}
|
2007-04-29 08:58:52 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2013-09-04 16:12:12 +02:00
|
|
|
static error_code getMemoryBufferForStream(int FD,
|
2012-11-05 22:55:40 +01:00
|
|
|
StringRef BufferName,
|
2014-03-05 11:27:34 +01:00
|
|
|
std::unique_ptr<MemoryBuffer> &Result) {
|
2012-11-05 22:55:40 +01:00
|
|
|
const ssize_t ChunkSize = 4096*4;
|
|
|
|
SmallString<ChunkSize> Buffer;
|
|
|
|
ssize_t ReadBytes;
|
|
|
|
// Read into Buffer until we hit EOF.
|
|
|
|
do {
|
|
|
|
Buffer.reserve(Buffer.size() + ChunkSize);
|
|
|
|
ReadBytes = read(FD, Buffer.end(), ChunkSize);
|
|
|
|
if (ReadBytes == -1) {
|
|
|
|
if (errno == EINTR) continue;
|
|
|
|
return error_code(errno, posix_category());
|
|
|
|
}
|
|
|
|
Buffer.set_size(Buffer.size() + ReadBytes);
|
|
|
|
} while (ReadBytes != 0);
|
|
|
|
|
2014-03-05 11:27:34 +01:00
|
|
|
Result.reset(MemoryBuffer::getMemBufferCopy(Buffer, BufferName));
|
2012-11-05 22:55:40 +01:00
|
|
|
return error_code::success();
|
|
|
|
}
|
|
|
|
|
2013-10-25 21:06:52 +02:00
|
|
|
static error_code getFileAux(const char *Filename,
|
2014-03-05 11:27:34 +01:00
|
|
|
std::unique_ptr<MemoryBuffer> &Result,
|
|
|
|
int64_t FileSize,
|
2013-10-25 21:06:52 +02:00
|
|
|
bool RequiresNullTerminator);
|
|
|
|
|
|
|
|
error_code MemoryBuffer::getFile(Twine Filename,
|
2014-03-05 11:27:34 +01:00
|
|
|
std::unique_ptr<MemoryBuffer> &Result,
|
2011-03-22 20:20:47 +01:00
|
|
|
int64_t FileSize,
|
|
|
|
bool RequiresNullTerminator) {
|
2010-11-23 23:20:27 +01:00
|
|
|
// Ensure the path is null terminated.
|
2013-10-25 21:06:52 +02:00
|
|
|
SmallString<256> PathBuf;
|
|
|
|
StringRef NullTerminatedName = Filename.toNullTerminatedStringRef(PathBuf);
|
2014-03-05 11:27:34 +01:00
|
|
|
return getFileAux(NullTerminatedName.data(), Result, FileSize,
|
2013-10-25 21:06:52 +02:00
|
|
|
RequiresNullTerminator);
|
2010-06-24 18:25:50 +02:00
|
|
|
}
|
|
|
|
|
2014-03-05 11:27:34 +01:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2013-07-23 22:25:01 +02:00
|
|
|
static error_code getOpenFileImpl(int FD, const char *Filename,
|
2014-03-05 11:27:34 +01:00
|
|
|
std::unique_ptr<MemoryBuffer> &Result,
|
2013-07-23 22:25:01 +02:00
|
|
|
uint64_t FileSize, uint64_t MapSize,
|
|
|
|
int64_t Offset, bool RequiresNullTerminator);
|
|
|
|
|
2013-10-25 21:06:52 +02:00
|
|
|
static error_code getFileAux(const char *Filename,
|
2014-03-05 11:27:34 +01:00
|
|
|
std::unique_ptr<MemoryBuffer> &Result, int64_t FileSize,
|
2013-10-25 21:06:52 +02:00
|
|
|
bool RequiresNullTerminator) {
|
2013-07-16 21:44:17 +02:00
|
|
|
int FD;
|
|
|
|
error_code EC = sys::fs::openFileForRead(Filename, FD);
|
|
|
|
if (EC)
|
|
|
|
return EC;
|
2011-05-22 02:50:53 +02:00
|
|
|
|
2014-03-05 11:27:34 +01:00
|
|
|
error_code ret = getOpenFileImpl(FD, Filename, Result, FileSize, FileSize, 0,
|
2013-07-23 22:25:01 +02:00
|
|
|
RequiresNullTerminator);
|
2013-03-14 01:20:10 +01:00
|
|
|
close(FD);
|
2011-02-08 23:40:47 +01:00
|
|
|
return ret;
|
2010-11-23 23:20:27 +01:00
|
|
|
}
|
2010-11-29 19:16:10 +01:00
|
|
|
|
2011-03-10 21:54:07 +01:00
|
|
|
static bool shouldUseMmap(int FD,
|
|
|
|
size_t FileSize,
|
2011-03-10 19:33:29 +01:00
|
|
|
size_t MapSize,
|
|
|
|
off_t Offset,
|
|
|
|
bool RequiresNullTerminator,
|
|
|
|
int PageSize) {
|
|
|
|
// We don't use mmap for small files because this can severely fragment our
|
|
|
|
// address space.
|
2013-08-22 12:23:52 +02:00
|
|
|
if (MapSize < 4 * 4096 || MapSize < (unsigned)PageSize)
|
2011-03-10 19:33:29 +01:00
|
|
|
return false;
|
|
|
|
|
|
|
|
if (!RequiresNullTerminator)
|
|
|
|
return true;
|
|
|
|
|
2011-03-10 21:54:07 +01:00
|
|
|
|
|
|
|
// If we don't know the file size, use fstat to find out. fstat on an open
|
|
|
|
// file descriptor is cheaper than stat on a random path.
|
|
|
|
// FIXME: this chunk of code is duplicated, but it avoids a fstat when
|
|
|
|
// RequiresNullTerminator = false and MapSize != -1.
|
|
|
|
if (FileSize == size_t(-1)) {
|
2013-07-18 05:04:20 +02:00
|
|
|
sys::fs::file_status Status;
|
|
|
|
error_code EC = sys::fs::status(FD, Status);
|
|
|
|
if (EC)
|
|
|
|
return EC;
|
|
|
|
FileSize = Status.getSize();
|
2011-03-10 21:54:07 +01:00
|
|
|
}
|
|
|
|
|
2011-03-10 19:33:29 +01:00
|
|
|
// If we need a null terminator and the end of the map is inside the file,
|
|
|
|
// we cannot use mmap.
|
|
|
|
size_t End = Offset + MapSize;
|
|
|
|
assert(End <= FileSize);
|
|
|
|
if (End != FileSize)
|
|
|
|
return false;
|
|
|
|
|
2013-09-04 16:12:19 +02:00
|
|
|
#if defined(_WIN32) || defined(__CYGWIN__)
|
|
|
|
// Don't peek the next page if file is multiple of *physical* pagesize(4k)
|
|
|
|
// but is not multiple of AllocationGranularity(64k),
|
|
|
|
// when a null terminator is required.
|
|
|
|
// FIXME: It's not good to hardcode 4096 here. dwPageSize shows 4096.
|
|
|
|
if ((FileSize & (4096 - 1)) == 0)
|
|
|
|
return false;
|
|
|
|
#endif
|
|
|
|
|
2011-03-10 19:33:29 +01:00
|
|
|
// Don't try to map files that are exactly a multiple of the system page size
|
|
|
|
// if we need a null terminator.
|
|
|
|
if ((FileSize & (PageSize -1)) == 0)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-07-23 22:25:01 +02:00
|
|
|
static error_code getOpenFileImpl(int FD, const char *Filename,
|
2014-03-05 11:27:34 +01:00
|
|
|
std::unique_ptr<MemoryBuffer> &Result,
|
2013-07-23 22:25:01 +02:00
|
|
|
uint64_t FileSize, uint64_t MapSize,
|
|
|
|
int64_t Offset, bool RequiresNullTerminator) {
|
2013-01-01 00:31:56 +01:00
|
|
|
static int PageSize = sys::process::get_self()->page_size();
|
2011-03-10 19:33:29 +01:00
|
|
|
|
|
|
|
// Default is to map the full file.
|
2011-09-16 01:13:00 +02:00
|
|
|
if (MapSize == uint64_t(-1)) {
|
2011-03-10 21:54:07 +01:00
|
|
|
// If we don't know the file size, use fstat to find out. fstat on an open
|
|
|
|
// file descriptor is cheaper than stat on a random path.
|
2011-09-16 01:13:00 +02:00
|
|
|
if (FileSize == uint64_t(-1)) {
|
2013-07-18 05:04:20 +02:00
|
|
|
sys::fs::file_status Status;
|
|
|
|
error_code EC = sys::fs::status(FD, Status);
|
|
|
|
if (EC)
|
|
|
|
return EC;
|
2012-11-05 22:55:40 +01:00
|
|
|
|
2013-02-19 20:36:55 +01:00
|
|
|
// If this not a file or a block device (e.g. it's a named pipe
|
|
|
|
// or character device), we can't trust the size. Create the memory
|
|
|
|
// buffer by copying off the stream.
|
2013-07-18 05:04:20 +02:00
|
|
|
sys::fs::file_type Type = Status.type();
|
|
|
|
if (Type != sys::fs::file_type::regular_file &&
|
|
|
|
Type != sys::fs::file_type::block_file)
|
2014-03-05 11:27:34 +01:00
|
|
|
return getMemoryBufferForStream(FD, Filename, Result);
|
2012-11-05 22:55:40 +01:00
|
|
|
|
2013-07-18 05:04:20 +02:00
|
|
|
FileSize = Status.getSize();
|
2011-03-10 21:54:07 +01:00
|
|
|
}
|
2011-03-10 19:33:29 +01:00
|
|
|
MapSize = FileSize;
|
2011-03-10 21:54:07 +01:00
|
|
|
}
|
2011-03-10 19:33:29 +01:00
|
|
|
|
2011-03-10 21:54:07 +01:00
|
|
|
if (shouldUseMmap(FD, FileSize, MapSize, Offset, RequiresNullTerminator,
|
2011-03-10 19:33:29 +01:00
|
|
|
PageSize)) {
|
2013-03-12 20:28:19 +01:00
|
|
|
error_code EC;
|
2014-03-05 11:27:34 +01:00
|
|
|
Result.reset(new (NamedBufferAlloc(Filename)) MemoryBufferMMapFile(
|
2013-03-12 20:28:19 +01:00
|
|
|
RequiresNullTerminator, FD, MapSize, Offset, EC));
|
|
|
|
if (!EC)
|
2012-02-09 20:24:12 +01:00
|
|
|
return error_code::success();
|
2007-05-06 09:24:46 +02:00
|
|
|
}
|
2009-02-13 08:54:34 +01:00
|
|
|
|
2011-03-10 19:33:29 +01:00
|
|
|
MemoryBuffer *Buf = MemoryBuffer::getNewUninitMemBuffer(MapSize, Filename);
|
2009-02-13 08:54:34 +01:00
|
|
|
if (!Buf) {
|
2010-12-09 18:36:48 +01:00
|
|
|
// Failed to create a buffer. The only way it can fail is if
|
|
|
|
// new(std::nothrow) returns 0.
|
2010-12-16 04:29:14 +01:00
|
|
|
return make_error_code(errc::not_enough_memory);
|
2009-02-13 08:54:34 +01:00
|
|
|
}
|
|
|
|
|
2014-03-05 11:27:34 +01:00
|
|
|
std::unique_ptr<MemoryBuffer> SB(Buf);
|
2007-04-29 08:58:52 +02:00
|
|
|
char *BufPtr = const_cast<char*>(SB->getBufferStart());
|
2010-04-01 16:35:22 +02:00
|
|
|
|
2011-03-10 19:33:29 +01:00
|
|
|
size_t BytesLeft = MapSize;
|
2011-11-22 13:31:53 +01:00
|
|
|
#ifndef HAVE_PREAD
|
2011-03-10 19:33:29 +01:00
|
|
|
if (lseek(FD, Offset, SEEK_SET) == -1)
|
|
|
|
return error_code(errno, posix_category());
|
2011-11-22 13:31:53 +01:00
|
|
|
#endif
|
2011-03-10 19:33:29 +01:00
|
|
|
|
2007-04-29 08:58:52 +02:00
|
|
|
while (BytesLeft) {
|
2011-11-22 13:31:53 +01:00
|
|
|
#ifdef HAVE_PREAD
|
|
|
|
ssize_t NumRead = ::pread(FD, BufPtr, BytesLeft, MapSize-BytesLeft+Offset);
|
|
|
|
#else
|
2007-04-29 08:58:52 +02:00
|
|
|
ssize_t NumRead = ::read(FD, BufPtr, BytesLeft);
|
2011-11-22 13:31:53 +01:00
|
|
|
#endif
|
2010-04-01 16:35:22 +02:00
|
|
|
if (NumRead == -1) {
|
|
|
|
if (errno == EINTR)
|
|
|
|
continue;
|
|
|
|
// Error while reading.
|
2010-12-16 04:29:14 +01:00
|
|
|
return error_code(errno, posix_category());
|
2007-04-29 08:58:52 +02:00
|
|
|
}
|
2012-03-13 21:18:42 +01:00
|
|
|
if (NumRead == 0) {
|
|
|
|
assert(0 && "We got inaccurate FileSize value or fstat reported an "
|
|
|
|
"invalid file size.");
|
2012-04-05 06:23:56 +02:00
|
|
|
*BufPtr = '\0'; // null-terminate at the actual size.
|
2012-03-13 21:18:42 +01:00
|
|
|
break;
|
|
|
|
}
|
2010-04-01 16:35:22 +02:00
|
|
|
BytesLeft -= NumRead;
|
|
|
|
BufPtr += NumRead;
|
2007-04-29 08:58:52 +02:00
|
|
|
}
|
2010-04-01 16:35:22 +02:00
|
|
|
|
2014-03-05 11:27:34 +01:00
|
|
|
Result.swap(SB);
|
2012-02-09 20:24:12 +01:00
|
|
|
return error_code::success();
|
2007-04-29 08:58:52 +02:00
|
|
|
}
|
|
|
|
|
2013-07-23 22:25:01 +02:00
|
|
|
error_code MemoryBuffer::getOpenFile(int FD, const char *Filename,
|
2014-03-05 11:27:34 +01:00
|
|
|
std::unique_ptr<MemoryBuffer> &Result,
|
2013-07-23 22:25:01 +02:00
|
|
|
uint64_t FileSize,
|
|
|
|
bool RequiresNullTerminator) {
|
|
|
|
return getOpenFileImpl(FD, Filename, Result, FileSize, FileSize, 0,
|
|
|
|
RequiresNullTerminator);
|
|
|
|
}
|
|
|
|
|
2014-03-05 11:27:34 +01:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2013-07-23 22:25:01 +02:00
|
|
|
error_code MemoryBuffer::getOpenFileSlice(int FD, const char *Filename,
|
2014-03-05 11:27:34 +01:00
|
|
|
std::unique_ptr<MemoryBuffer> &Result,
|
2013-07-23 22:25:01 +02:00
|
|
|
uint64_t MapSize, int64_t Offset) {
|
|
|
|
return getOpenFileImpl(FD, Filename, Result, -1, MapSize, Offset, false);
|
|
|
|
}
|
|
|
|
|
2014-03-05 11:27:34 +01:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2007-04-29 08:58:52 +02:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// MemoryBuffer::getSTDIN implementation.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2014-03-05 11:27:34 +01:00
|
|
|
error_code MemoryBuffer::getSTDIN(std::unique_ptr<MemoryBuffer> &Result) {
|
2007-04-29 08:58:52 +02:00
|
|
|
// Read in all of the data from stdin, we cannot mmap stdin.
|
2009-11-10 01:43:58 +01:00
|
|
|
//
|
|
|
|
// FIXME: That isn't necessarily true, we should try to mmap stdin and
|
|
|
|
// fallback if it fails.
|
2013-06-12 22:58:35 +02:00
|
|
|
sys::ChangeStdinToBinary();
|
2007-08-08 22:01:58 +02:00
|
|
|
|
2014-03-05 11:27:34 +01:00
|
|
|
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;
|
2007-04-29 08:58:52 +02:00
|
|
|
}
|