2007-04-29 14:58:52 +08:00
|
|
|
//===--- MemoryBuffer.cpp - Memory Buffer implementation ------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-30 04:36:04 +08:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2007-04-29 14:58:52 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file implements the MemoryBuffer interface.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/Support/MemoryBuffer.h"
|
2008-04-01 14:05:21 +08:00
|
|
|
#include "llvm/ADT/SmallString.h"
|
2011-11-22 20:31:53 +08:00
|
|
|
#include "llvm/Config/config.h"
|
2014-06-14 01:20:48 +08:00
|
|
|
#include "llvm/Support/Errc.h"
|
2010-11-30 02:16:10 +08:00
|
|
|
#include "llvm/Support/Errno.h"
|
2012-06-21 04:21:33 +08:00
|
|
|
#include "llvm/Support/FileSystem.h"
|
2012-12-04 00:50:05 +08:00
|
|
|
#include "llvm/Support/MathExtras.h"
|
2010-11-30 02:16:10 +08:00
|
|
|
#include "llvm/Support/Path.h"
|
|
|
|
#include "llvm/Support/Process.h"
|
|
|
|
#include "llvm/Support/Program.h"
|
2007-04-29 22:21:44 +08:00
|
|
|
#include <cassert>
|
2012-12-04 00:50:05 +08:00
|
|
|
#include <cerrno>
|
2007-04-29 14:58:52 +08:00
|
|
|
#include <cstring>
|
2010-12-20 04:43:38 +08:00
|
|
|
#include <new>
|
2012-12-04 00:50:05 +08:00
|
|
|
#include <sys/types.h>
|
2014-06-13 01:38:55 +08:00
|
|
|
#include <system_error>
|
2008-04-01 14:05:21 +08:00
|
|
|
#if !defined(_MSC_VER) && !defined(__MINGW32__)
|
|
|
|
#include <unistd.h>
|
|
|
|
#else
|
|
|
|
#include <io.h>
|
|
|
|
#endif
|
2007-04-29 14:58:52 +08:00
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// MemoryBuffer implementation itself.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2010-06-25 19:50:40 +08:00
|
|
|
MemoryBuffer::~MemoryBuffer() { }
|
2007-04-29 14:58:52 +08:00
|
|
|
|
|
|
|
/// init - Initialize this MemoryBuffer as a reference to externally allocated
|
|
|
|
/// memory, memory that we know is already null terminated.
|
2011-03-11 02:33:29 +08:00
|
|
|
void MemoryBuffer::init(const char *BufStart, const char *BufEnd,
|
|
|
|
bool RequiresNullTerminator) {
|
2011-03-18 10:55:51 +08:00
|
|
|
assert((!RequiresNullTerminator || BufEnd[0] == 0) &&
|
2011-03-11 02:33:29 +08:00
|
|
|
"Buffer is not null terminated!");
|
2007-04-29 14:58:52 +08:00
|
|
|
BufferStart = BufStart;
|
|
|
|
BufferEnd = BufEnd;
|
|
|
|
}
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// MemoryBufferMem implementation.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2010-06-25 19:50:40 +08:00
|
|
|
/// CopyStringRef - Copies contents of a StringRef into a block of memory and
|
|
|
|
/// null-terminates it.
|
|
|
|
static void CopyStringRef(char *Memory, StringRef Data) {
|
2015-08-04 09:00:56 +08:00
|
|
|
if (!Data.empty())
|
|
|
|
memcpy(Memory, Data.data(), Data.size());
|
2010-06-25 19:50:40 +08:00
|
|
|
Memory[Data.size()] = 0; // Null terminate string.
|
|
|
|
}
|
|
|
|
|
2013-03-30 23:23:08 +08:00
|
|
|
namespace {
|
2013-03-13 03:28:19 +08:00
|
|
|
struct NamedBufferAlloc {
|
2014-10-08 02:58:55 +08:00
|
|
|
const Twine &Name;
|
|
|
|
NamedBufferAlloc(const Twine &Name) : Name(Name) {}
|
2013-03-13 03:28:19 +08:00
|
|
|
};
|
2013-03-30 23:23:08 +08:00
|
|
|
}
|
2013-03-13 03:28:19 +08:00
|
|
|
|
|
|
|
void *operator new(size_t N, const NamedBufferAlloc &Alloc) {
|
2014-10-08 02:58:55 +08:00
|
|
|
SmallString<256> NameBuf;
|
|
|
|
StringRef NameRef = Alloc.Name.toStringRef(NameBuf);
|
|
|
|
|
|
|
|
char *Mem = static_cast<char *>(operator new(N + NameRef.size() + 1));
|
|
|
|
CopyStringRef(Mem + N, NameRef);
|
2013-03-13 03:28:19 +08:00
|
|
|
return Mem;
|
2010-06-25 19:50:40 +08:00
|
|
|
}
|
|
|
|
|
2007-04-29 14:58:52 +08:00
|
|
|
namespace {
|
2010-06-25 19:50:40 +08:00
|
|
|
/// MemoryBufferMem - Named MemoryBuffer pointing to a block of memory.
|
2007-04-29 14:58:52 +08:00
|
|
|
class MemoryBufferMem : public MemoryBuffer {
|
|
|
|
public:
|
2011-03-11 02:33:29 +08:00
|
|
|
MemoryBufferMem(StringRef InputData, bool RequiresNullTerminator) {
|
|
|
|
init(InputData.begin(), InputData.end(), RequiresNullTerminator);
|
2007-04-29 14:58:52 +08:00
|
|
|
}
|
2010-06-25 19:50:40 +08:00
|
|
|
|
2016-02-04 09:21:16 +08:00
|
|
|
/// Disable sized deallocation for MemoryBufferMem, because it has
|
|
|
|
/// tail-allocated data.
|
|
|
|
void operator delete(void *p) { ::operator delete(p); }
|
|
|
|
|
2016-10-02 00:38:28 +08:00
|
|
|
StringRef getBufferIdentifier() const override {
|
|
|
|
// The name is stored after the class itself.
|
|
|
|
return StringRef(reinterpret_cast<const char *>(this + 1));
|
2007-04-29 14:58:52 +08:00
|
|
|
}
|
2012-09-23 10:12:10 +08:00
|
|
|
|
2014-03-10 11:53:12 +08:00
|
|
|
BufferKind getBufferKind() const override {
|
2011-04-29 04:34:18 +08:00
|
|
|
return MemoryBuffer_Malloc;
|
|
|
|
}
|
2007-04-29 14:58:52 +08:00
|
|
|
};
|
2015-06-23 17:49:53 +08:00
|
|
|
}
|
2007-04-29 14:58:52 +08:00
|
|
|
|
2014-10-08 08:22:18 +08:00
|
|
|
static ErrorOr<std::unique_ptr<MemoryBuffer>>
|
|
|
|
getFileAux(const Twine &Filename, int64_t FileSize, uint64_t MapSize,
|
|
|
|
uint64_t Offset, bool RequiresNullTerminator, bool IsVolatileSize);
|
|
|
|
|
2014-08-28 04:03:13 +08:00
|
|
|
std::unique_ptr<MemoryBuffer>
|
|
|
|
MemoryBuffer::getMemBuffer(StringRef InputData, StringRef BufferName,
|
|
|
|
bool RequiresNullTerminator) {
|
|
|
|
auto *Ret = new (NamedBufferAlloc(BufferName))
|
2013-03-13 03:28:19 +08:00
|
|
|
MemoryBufferMem(InputData, RequiresNullTerminator);
|
2014-08-28 04:03:13 +08:00
|
|
|
return std::unique_ptr<MemoryBuffer>(Ret);
|
2007-04-29 14:58:52 +08:00
|
|
|
}
|
|
|
|
|
2014-08-27 05:49:01 +08:00
|
|
|
std::unique_ptr<MemoryBuffer>
|
|
|
|
MemoryBuffer::getMemBuffer(MemoryBufferRef Ref, bool RequiresNullTerminator) {
|
|
|
|
return std::unique_ptr<MemoryBuffer>(getMemBuffer(
|
|
|
|
Ref.getBuffer(), Ref.getBufferIdentifier(), RequiresNullTerminator));
|
|
|
|
}
|
|
|
|
|
2014-08-28 04:03:13 +08:00
|
|
|
std::unique_ptr<MemoryBuffer>
|
2014-10-08 02:58:55 +08:00
|
|
|
MemoryBuffer::getMemBufferCopy(StringRef InputData, const Twine &BufferName) {
|
2014-08-28 04:03:13 +08:00
|
|
|
std::unique_ptr<MemoryBuffer> Buf =
|
|
|
|
getNewUninitMemBuffer(InputData.size(), BufferName);
|
|
|
|
if (!Buf)
|
|
|
|
return nullptr;
|
2010-06-25 19:50:40 +08:00
|
|
|
memcpy(const_cast<char*>(Buf->getBufferStart()), InputData.data(),
|
|
|
|
InputData.size());
|
|
|
|
return Buf;
|
2007-10-10 05:46:38 +08:00
|
|
|
}
|
|
|
|
|
2014-08-28 04:03:13 +08:00
|
|
|
std::unique_ptr<MemoryBuffer>
|
2014-10-08 02:58:55 +08:00
|
|
|
MemoryBuffer::getNewUninitMemBuffer(size_t Size, const Twine &BufferName) {
|
2010-06-25 19:50:40 +08: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-17 02:18:12 +08:00
|
|
|
// TODO: Is 16-byte alignment enough? We copy small object files with large
|
|
|
|
// alignment expectations into this buffer.
|
2014-10-08 02:58:55 +08:00
|
|
|
SmallString<256> NameBuf;
|
|
|
|
StringRef NameRef = BufferName.toStringRef(NameBuf);
|
2010-06-25 19:50:40 +08:00
|
|
|
size_t AlignedStringLen =
|
2016-01-15 05:06:47 +08:00
|
|
|
alignTo(sizeof(MemoryBufferMem) + NameRef.size() + 1, 16);
|
2010-06-25 19:50:40 +08:00
|
|
|
size_t RealLen = AlignedStringLen + Size + 1;
|
|
|
|
char *Mem = static_cast<char*>(operator new(RealLen, std::nothrow));
|
2014-08-28 04:03:13 +08:00
|
|
|
if (!Mem)
|
|
|
|
return nullptr;
|
2010-06-25 19:50:40 +08:00
|
|
|
|
|
|
|
// The name is stored after the class itself.
|
2014-10-08 02:58:55 +08:00
|
|
|
CopyStringRef(Mem + sizeof(MemoryBufferMem), NameRef);
|
2010-06-25 19:50:40 +08:00
|
|
|
|
|
|
|
// The buffer begins after the name and must be aligned.
|
|
|
|
char *Buf = Mem + AlignedStringLen;
|
|
|
|
Buf[Size] = 0; // Null terminate buffer.
|
|
|
|
|
2014-08-28 04:03:13 +08:00
|
|
|
auto *Ret = new (Mem) MemoryBufferMem(StringRef(Buf, Size), true);
|
|
|
|
return std::unique_ptr<MemoryBuffer>(Ret);
|
2007-04-29 14:58:52 +08:00
|
|
|
}
|
|
|
|
|
2014-08-28 04:03:13 +08:00
|
|
|
std::unique_ptr<MemoryBuffer>
|
|
|
|
MemoryBuffer::getNewMemBuffer(size_t Size, StringRef BufferName) {
|
|
|
|
std::unique_ptr<MemoryBuffer> SB = getNewUninitMemBuffer(Size, BufferName);
|
|
|
|
if (!SB)
|
|
|
|
return nullptr;
|
2010-06-25 19:50:40 +08:00
|
|
|
memset(const_cast<char*>(SB->getBufferStart()), 0, Size);
|
2007-04-29 14:58:52 +08:00
|
|
|
return SB;
|
|
|
|
}
|
|
|
|
|
2014-07-07 01:43:13 +08:00
|
|
|
ErrorOr<std::unique_ptr<MemoryBuffer>>
|
2015-10-15 09:27:19 +08:00
|
|
|
MemoryBuffer::getFileOrSTDIN(const Twine &Filename, int64_t FileSize,
|
|
|
|
bool RequiresNullTerminator) {
|
2014-10-08 02:58:55 +08:00
|
|
|
SmallString<256> NameBuf;
|
|
|
|
StringRef NameRef = Filename.toStringRef(NameBuf);
|
|
|
|
|
|
|
|
if (NameRef == "-")
|
2014-07-07 01:43:13 +08:00
|
|
|
return getSTDIN();
|
2015-10-15 09:27:19 +08:00
|
|
|
return getFile(Filename, FileSize, RequiresNullTerminator);
|
2007-11-19 02:52:28 +08:00
|
|
|
}
|
|
|
|
|
2014-10-08 08:22:18 +08:00
|
|
|
ErrorOr<std::unique_ptr<MemoryBuffer>>
|
|
|
|
MemoryBuffer::getFileSlice(const Twine &FilePath, uint64_t MapSize,
|
|
|
|
uint64_t Offset) {
|
|
|
|
return getFileAux(FilePath, -1, MapSize, Offset, false, false);
|
|
|
|
}
|
|
|
|
|
2014-03-05 18:27:34 +08:00
|
|
|
|
2007-04-29 14:58:52 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2008-04-01 14:05:21 +08:00
|
|
|
// MemoryBuffer::getFile implementation.
|
2007-04-29 14:58:52 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
namespace {
|
2013-09-11 03:54:54 +08:00
|
|
|
/// \brief Memory maps a file descriptor using sys::fs::mapped_file_region.
|
2013-03-13 03:28:19 +08: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 19:50:40 +08:00
|
|
|
|
2013-03-13 03:28:19 +08:00
|
|
|
const char *getStart(uint64_t Len, uint64_t Offset) {
|
|
|
|
return MFR.const_data() + (Offset - getLegalMapOffset(Offset));
|
|
|
|
}
|
2011-03-11 02:33:29 +08:00
|
|
|
|
2013-03-13 03:28:19 +08:00
|
|
|
public:
|
|
|
|
MemoryBufferMMapFile(bool RequiresNullTerminator, int FD, uint64_t Len,
|
2014-12-13 06:27:53 +08:00
|
|
|
uint64_t Offset, std::error_code &EC)
|
2014-12-12 04:12:55 +08:00
|
|
|
: MFR(FD, sys::fs::mapped_file_region::readonly,
|
2013-03-13 03:28:19 +08:00
|
|
|
getLegalMapSize(Len, Offset), getLegalMapOffset(Offset), EC) {
|
|
|
|
if (!EC) {
|
|
|
|
const char *Start = getStart(Len, Offset);
|
|
|
|
init(Start, Start + Len, RequiresNullTerminator);
|
|
|
|
}
|
|
|
|
}
|
2011-03-11 02:33:29 +08:00
|
|
|
|
2016-02-04 09:21:16 +08:00
|
|
|
/// Disable sized deallocation for MemoryBufferMMapFile, because it has
|
|
|
|
/// tail-allocated data.
|
|
|
|
void operator delete(void *p) { ::operator delete(p); }
|
|
|
|
|
2016-10-02 00:38:28 +08:00
|
|
|
StringRef getBufferIdentifier() const override {
|
2013-03-13 03:28:19 +08:00
|
|
|
// The name is stored after the class itself.
|
2016-10-02 00:38:28 +08:00
|
|
|
return StringRef(reinterpret_cast<const char *>(this + 1));
|
2008-04-01 14:05:21 +08:00
|
|
|
}
|
2012-09-23 10:12:10 +08:00
|
|
|
|
2014-03-10 11:53:12 +08:00
|
|
|
BufferKind getBufferKind() const override {
|
2011-04-29 04:34:18 +08:00
|
|
|
return MemoryBuffer_MMap;
|
|
|
|
}
|
2007-04-29 14:58:52 +08:00
|
|
|
};
|
2015-06-23 17:49:53 +08:00
|
|
|
}
|
2007-04-29 14:58:52 +08:00
|
|
|
|
2014-07-07 01:43:13 +08:00
|
|
|
static ErrorOr<std::unique_ptr<MemoryBuffer>>
|
2014-10-08 02:58:55 +08:00
|
|
|
getMemoryBufferForStream(int FD, const Twine &BufferName) {
|
2012-11-06 05:55:40 +08: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;
|
2014-06-13 10:24:39 +08:00
|
|
|
return std::error_code(errno, std::generic_category());
|
2012-11-06 05:55:40 +08:00
|
|
|
}
|
|
|
|
Buffer.set_size(Buffer.size() + ReadBytes);
|
|
|
|
} while (ReadBytes != 0);
|
|
|
|
|
2014-08-28 04:03:13 +08:00
|
|
|
return MemoryBuffer::getMemBufferCopy(Buffer, BufferName);
|
2012-11-06 05:55:40 +08:00
|
|
|
}
|
|
|
|
|
2014-06-13 10:24:39 +08:00
|
|
|
|
2014-07-07 01:43:13 +08:00
|
|
|
ErrorOr<std::unique_ptr<MemoryBuffer>>
|
2014-10-08 02:58:55 +08:00
|
|
|
MemoryBuffer::getFile(const Twine &Filename, int64_t FileSize,
|
2014-07-07 01:43:13 +08:00
|
|
|
bool RequiresNullTerminator, bool IsVolatileSize) {
|
2014-10-08 08:22:18 +08:00
|
|
|
return getFileAux(Filename, FileSize, FileSize, 0,
|
|
|
|
RequiresNullTerminator, IsVolatileSize);
|
2010-06-25 00:25:50 +08:00
|
|
|
}
|
|
|
|
|
2014-07-07 01:43:13 +08:00
|
|
|
static ErrorOr<std::unique_ptr<MemoryBuffer>>
|
2014-10-08 02:58:55 +08:00
|
|
|
getOpenFileImpl(int FD, const Twine &Filename, uint64_t FileSize,
|
2014-07-07 01:43:13 +08:00
|
|
|
uint64_t MapSize, int64_t Offset, bool RequiresNullTerminator,
|
|
|
|
bool IsVolatileSize);
|
|
|
|
|
|
|
|
static ErrorOr<std::unique_ptr<MemoryBuffer>>
|
2014-10-08 08:22:18 +08:00
|
|
|
getFileAux(const Twine &Filename, int64_t FileSize, uint64_t MapSize,
|
|
|
|
uint64_t Offset, bool RequiresNullTerminator, bool IsVolatileSize) {
|
2013-07-17 03:44:17 +08:00
|
|
|
int FD;
|
2014-06-13 10:24:39 +08:00
|
|
|
std::error_code EC = sys::fs::openFileForRead(Filename, FD);
|
2013-07-17 03:44:17 +08:00
|
|
|
if (EC)
|
|
|
|
return EC;
|
2011-05-22 08:50:53 +08:00
|
|
|
|
2014-07-07 01:43:13 +08:00
|
|
|
ErrorOr<std::unique_ptr<MemoryBuffer>> Ret =
|
2014-10-08 08:22:18 +08:00
|
|
|
getOpenFileImpl(FD, Filename, FileSize, MapSize, Offset,
|
2014-06-13 10:24:39 +08:00
|
|
|
RequiresNullTerminator, IsVolatileSize);
|
2013-03-14 08:20:10 +08:00
|
|
|
close(FD);
|
2014-07-07 01:43:13 +08:00
|
|
|
return Ret;
|
2010-11-24 06:20:27 +08:00
|
|
|
}
|
2010-11-30 02:16:10 +08:00
|
|
|
|
2011-03-11 04:54:07 +08:00
|
|
|
static bool shouldUseMmap(int FD,
|
|
|
|
size_t FileSize,
|
2011-03-11 02:33:29 +08:00
|
|
|
size_t MapSize,
|
|
|
|
off_t Offset,
|
|
|
|
bool RequiresNullTerminator,
|
2014-05-06 08:51:45 +08:00
|
|
|
int PageSize,
|
2014-05-06 09:03:52 +08:00
|
|
|
bool IsVolatileSize) {
|
2014-05-06 08:51:45 +08:00
|
|
|
// mmap may leave the buffer without null terminator if the file size changed
|
|
|
|
// by the time the last page is mapped in, so avoid it if the file size is
|
|
|
|
// likely to change.
|
2014-05-06 09:03:52 +08:00
|
|
|
if (IsVolatileSize)
|
2014-05-06 08:51:45 +08:00
|
|
|
return false;
|
|
|
|
|
2011-03-11 02:33:29 +08:00
|
|
|
// We don't use mmap for small files because this can severely fragment our
|
|
|
|
// address space.
|
2013-08-22 18:23:52 +08:00
|
|
|
if (MapSize < 4 * 4096 || MapSize < (unsigned)PageSize)
|
2011-03-11 02:33:29 +08:00
|
|
|
return false;
|
|
|
|
|
|
|
|
if (!RequiresNullTerminator)
|
|
|
|
return true;
|
|
|
|
|
2011-03-11 04:54:07 +08: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 11:04:20 +08:00
|
|
|
sys::fs::file_status Status;
|
2014-05-09 16:57:32 +08:00
|
|
|
if (sys::fs::status(FD, Status))
|
|
|
|
return false;
|
2013-07-18 11:04:20 +08:00
|
|
|
FileSize = Status.getSize();
|
2011-03-11 04:54:07 +08:00
|
|
|
}
|
|
|
|
|
2011-03-11 02:33:29 +08: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;
|
|
|
|
|
|
|
|
// 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;
|
|
|
|
|
2014-08-04 09:43:37 +08:00
|
|
|
#if defined(__CYGWIN__)
|
|
|
|
// Don't try to map files that are exactly a multiple of the physical page size
|
|
|
|
// if we need a null terminator.
|
|
|
|
// FIXME: We should reorganize again getPageSize() on Win32.
|
|
|
|
if ((FileSize & (4096 - 1)) == 0)
|
|
|
|
return false;
|
|
|
|
#endif
|
|
|
|
|
2011-03-11 02:33:29 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-07-07 01:43:13 +08:00
|
|
|
static ErrorOr<std::unique_ptr<MemoryBuffer>>
|
2014-10-08 02:58:55 +08:00
|
|
|
getOpenFileImpl(int FD, const Twine &Filename, uint64_t FileSize,
|
2014-07-07 01:43:13 +08:00
|
|
|
uint64_t MapSize, int64_t Offset, bool RequiresNullTerminator,
|
|
|
|
bool IsVolatileSize) {
|
2014-12-05 00:59:36 +08:00
|
|
|
static int PageSize = sys::Process::getPageSize();
|
2011-03-11 02:33:29 +08:00
|
|
|
|
|
|
|
// Default is to map the full file.
|
2011-09-16 07:13:00 +08:00
|
|
|
if (MapSize == uint64_t(-1)) {
|
2011-03-11 04:54:07 +08: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 07:13:00 +08:00
|
|
|
if (FileSize == uint64_t(-1)) {
|
2013-07-18 11:04:20 +08:00
|
|
|
sys::fs::file_status Status;
|
2014-06-13 10:24:39 +08:00
|
|
|
std::error_code EC = sys::fs::status(FD, Status);
|
2013-07-18 11:04:20 +08:00
|
|
|
if (EC)
|
|
|
|
return EC;
|
2012-11-06 05:55:40 +08:00
|
|
|
|
2013-02-20 03:36:55 +08: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 11:04:20 +08:00
|
|
|
sys::fs::file_type Type = Status.type();
|
|
|
|
if (Type != sys::fs::file_type::regular_file &&
|
|
|
|
Type != sys::fs::file_type::block_file)
|
2014-07-07 01:43:13 +08:00
|
|
|
return getMemoryBufferForStream(FD, Filename);
|
2012-11-06 05:55:40 +08:00
|
|
|
|
2013-07-18 11:04:20 +08:00
|
|
|
FileSize = Status.getSize();
|
2011-03-11 04:54:07 +08:00
|
|
|
}
|
2011-03-11 02:33:29 +08:00
|
|
|
MapSize = FileSize;
|
2011-03-11 04:54:07 +08:00
|
|
|
}
|
2011-03-11 02:33:29 +08:00
|
|
|
|
2014-05-06 08:51:45 +08:00
|
|
|
if (shouldUseMmap(FD, FileSize, MapSize, Offset, RequiresNullTerminator,
|
2014-05-06 09:03:52 +08:00
|
|
|
PageSize, IsVolatileSize)) {
|
2014-06-13 10:24:39 +08:00
|
|
|
std::error_code EC;
|
2014-07-07 01:43:13 +08:00
|
|
|
std::unique_ptr<MemoryBuffer> Result(
|
|
|
|
new (NamedBufferAlloc(Filename))
|
|
|
|
MemoryBufferMMapFile(RequiresNullTerminator, FD, MapSize, Offset, EC));
|
2013-03-13 03:28:19 +08:00
|
|
|
if (!EC)
|
2014-07-07 01:43:13 +08:00
|
|
|
return std::move(Result);
|
2007-05-06 15:24:46 +08:00
|
|
|
}
|
2009-02-13 15:54:34 +08:00
|
|
|
|
2014-08-28 04:03:13 +08:00
|
|
|
std::unique_ptr<MemoryBuffer> Buf =
|
|
|
|
MemoryBuffer::getNewUninitMemBuffer(MapSize, Filename);
|
2009-02-13 15:54:34 +08:00
|
|
|
if (!Buf) {
|
2010-12-10 01:36:48 +08:00
|
|
|
// Failed to create a buffer. The only way it can fail is if
|
|
|
|
// new(std::nothrow) returns 0.
|
2014-06-14 01:20:48 +08:00
|
|
|
return make_error_code(errc::not_enough_memory);
|
2009-02-13 15:54:34 +08:00
|
|
|
}
|
|
|
|
|
2014-08-28 04:03:13 +08:00
|
|
|
char *BufPtr = const_cast<char *>(Buf->getBufferStart());
|
2010-04-01 22:35:22 +08:00
|
|
|
|
2011-03-11 02:33:29 +08:00
|
|
|
size_t BytesLeft = MapSize;
|
2011-11-22 20:31:53 +08:00
|
|
|
#ifndef HAVE_PREAD
|
2011-03-11 02:33:29 +08:00
|
|
|
if (lseek(FD, Offset, SEEK_SET) == -1)
|
2014-06-13 10:36:09 +08:00
|
|
|
return std::error_code(errno, std::generic_category());
|
2011-11-22 20:31:53 +08:00
|
|
|
#endif
|
2011-03-11 02:33:29 +08:00
|
|
|
|
2007-04-29 14:58:52 +08:00
|
|
|
while (BytesLeft) {
|
2011-11-22 20:31:53 +08:00
|
|
|
#ifdef HAVE_PREAD
|
|
|
|
ssize_t NumRead = ::pread(FD, BufPtr, BytesLeft, MapSize-BytesLeft+Offset);
|
|
|
|
#else
|
2007-04-29 14:58:52 +08:00
|
|
|
ssize_t NumRead = ::read(FD, BufPtr, BytesLeft);
|
2011-11-22 20:31:53 +08:00
|
|
|
#endif
|
2010-04-01 22:35:22 +08:00
|
|
|
if (NumRead == -1) {
|
|
|
|
if (errno == EINTR)
|
|
|
|
continue;
|
|
|
|
// Error while reading.
|
2014-06-13 10:24:39 +08:00
|
|
|
return std::error_code(errno, std::generic_category());
|
2007-04-29 14:58:52 +08:00
|
|
|
}
|
2012-03-14 04:18:42 +08:00
|
|
|
if (NumRead == 0) {
|
2014-05-06 08:51:45 +08:00
|
|
|
memset(BufPtr, 0, BytesLeft); // zero-initialize rest of the buffer.
|
2012-03-14 04:18:42 +08:00
|
|
|
break;
|
|
|
|
}
|
2010-04-01 22:35:22 +08:00
|
|
|
BytesLeft -= NumRead;
|
|
|
|
BufPtr += NumRead;
|
2007-04-29 14:58:52 +08:00
|
|
|
}
|
2010-04-01 22:35:22 +08:00
|
|
|
|
2014-08-28 04:03:13 +08:00
|
|
|
return std::move(Buf);
|
2007-04-29 14:58:52 +08:00
|
|
|
}
|
|
|
|
|
2014-07-07 01:43:13 +08:00
|
|
|
ErrorOr<std::unique_ptr<MemoryBuffer>>
|
2014-10-08 02:58:55 +08:00
|
|
|
MemoryBuffer::getOpenFile(int FD, const Twine &Filename, uint64_t FileSize,
|
2014-07-07 01:43:13 +08:00
|
|
|
bool RequiresNullTerminator, bool IsVolatileSize) {
|
|
|
|
return getOpenFileImpl(FD, Filename, FileSize, FileSize, 0,
|
2014-05-06 09:03:52 +08:00
|
|
|
RequiresNullTerminator, IsVolatileSize);
|
2013-07-24 04:25:01 +08:00
|
|
|
}
|
|
|
|
|
2014-07-07 01:43:13 +08:00
|
|
|
ErrorOr<std::unique_ptr<MemoryBuffer>>
|
2014-10-08 02:58:55 +08:00
|
|
|
MemoryBuffer::getOpenFileSlice(int FD, const Twine &Filename, uint64_t MapSize,
|
2014-10-08 03:09:05 +08:00
|
|
|
int64_t Offset) {
|
|
|
|
assert(MapSize != uint64_t(-1));
|
2014-07-07 01:43:13 +08:00
|
|
|
return getOpenFileImpl(FD, Filename, -1, MapSize, Offset, false,
|
2014-10-08 03:09:05 +08:00
|
|
|
/*IsVolatileSize*/ false);
|
2013-07-24 04:25:01 +08:00
|
|
|
}
|
|
|
|
|
2014-07-07 01:43:13 +08:00
|
|
|
ErrorOr<std::unique_ptr<MemoryBuffer>> MemoryBuffer::getSTDIN() {
|
2007-04-29 14:58:52 +08:00
|
|
|
// Read in all of the data from stdin, we cannot mmap stdin.
|
2009-11-10 08:43:58 +08:00
|
|
|
//
|
|
|
|
// FIXME: That isn't necessarily true, we should try to mmap stdin and
|
|
|
|
// fallback if it fails.
|
2013-06-13 04:58:35 +08:00
|
|
|
sys::ChangeStdinToBinary();
|
2007-08-09 04:01:58 +08:00
|
|
|
|
2014-07-07 01:43:13 +08:00
|
|
|
return getMemoryBufferForStream(0, "<stdin>");
|
2014-03-05 18:27:34 +08:00
|
|
|
}
|
2014-08-20 02:44:46 +08:00
|
|
|
|
2016-10-14 01:43:20 +08:00
|
|
|
ErrorOr<std::unique_ptr<MemoryBuffer>>
|
|
|
|
MemoryBuffer::getFileAsStream(const Twine &Filename) {
|
|
|
|
int FD;
|
|
|
|
std::error_code EC = sys::fs::openFileForRead(Filename, FD);
|
|
|
|
if (EC)
|
|
|
|
return EC;
|
|
|
|
ErrorOr<std::unique_ptr<MemoryBuffer>> Ret =
|
|
|
|
getMemoryBufferForStream(FD, Filename);
|
|
|
|
close(FD);
|
|
|
|
return Ret;
|
|
|
|
}
|
|
|
|
|
2014-08-20 02:44:46 +08:00
|
|
|
MemoryBufferRef MemoryBuffer::getMemBufferRef() const {
|
|
|
|
StringRef Data = getBuffer();
|
|
|
|
StringRef Identifier = getBufferIdentifier();
|
|
|
|
return MemoryBufferRef(Data, Identifier);
|
|
|
|
}
|