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/OwningPtr.h"
|
|
|
|
#include "llvm/ADT/SmallString.h"
|
2010-06-25 19:50:40 +08:00
|
|
|
#include "llvm/Support/MathExtras.h"
|
2010-11-30 02:16:10 +08:00
|
|
|
#include "llvm/Support/Errno.h"
|
|
|
|
#include "llvm/Support/Path.h"
|
|
|
|
#include "llvm/Support/Process.h"
|
|
|
|
#include "llvm/Support/Program.h"
|
2010-12-10 01:36:48 +08:00
|
|
|
#include "llvm/Support/system_error.h"
|
2007-04-29 22:21:44 +08:00
|
|
|
#include <cassert>
|
2007-04-29 14:58:52 +08:00
|
|
|
#include <cstdio>
|
|
|
|
#include <cstring>
|
|
|
|
#include <cerrno>
|
2010-12-20 04:43:38 +08:00
|
|
|
#include <new>
|
2008-04-01 14:05:21 +08:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#if !defined(_MSC_VER) && !defined(__MINGW32__)
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/uio.h>
|
|
|
|
#else
|
|
|
|
#include <io.h>
|
|
|
|
#endif
|
2008-04-30 16:53:22 +08:00
|
|
|
#include <fcntl.h>
|
2007-04-29 14:58:52 +08:00
|
|
|
using namespace llvm;
|
|
|
|
|
2010-12-16 11:29:14 +08:00
|
|
|
namespace { const llvm::error_code success; }
|
|
|
|
|
2007-04-29 14:58:52 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// 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.
|
|
|
|
void MemoryBuffer::init(const char *BufStart, const char *BufEnd) {
|
|
|
|
assert(BufEnd[0] == 0 && "Buffer is not null terminated!");
|
|
|
|
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) {
|
|
|
|
memcpy(Memory, Data.data(), Data.size());
|
|
|
|
Memory[Data.size()] = 0; // Null terminate string.
|
|
|
|
}
|
|
|
|
|
|
|
|
/// GetNamedBuffer - Allocates a new MemoryBuffer with Name copied after it.
|
|
|
|
template <typename T>
|
|
|
|
static T* GetNamedBuffer(StringRef Buffer, StringRef Name) {
|
|
|
|
char *Mem = static_cast<char*>(operator new(sizeof(T) + Name.size() + 1));
|
|
|
|
CopyStringRef(Mem + sizeof(T), Name);
|
|
|
|
return new (Mem) T(Buffer);
|
|
|
|
}
|
|
|
|
|
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:
|
2010-06-25 19:50:40 +08:00
|
|
|
MemoryBufferMem(StringRef InputData) {
|
|
|
|
init(InputData.begin(), InputData.end());
|
2007-04-29 14:58:52 +08:00
|
|
|
}
|
2010-06-25 19:50:40 +08:00
|
|
|
|
2007-04-29 14:58:52 +08:00
|
|
|
virtual const char *getBufferIdentifier() const {
|
2010-06-25 19:50:40 +08:00
|
|
|
// The name is stored after the class itself.
|
|
|
|
return reinterpret_cast<const char*>(this + 1);
|
2007-04-29 14:58:52 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/// getMemBuffer - Open the specified memory range as a MemoryBuffer. Note
|
|
|
|
/// that EndPtr[0] must be a null byte and be accessible!
|
2010-04-06 06:42:30 +08:00
|
|
|
MemoryBuffer *MemoryBuffer::getMemBuffer(StringRef InputData,
|
2010-06-25 19:50:40 +08:00
|
|
|
StringRef BufferName) {
|
|
|
|
return GetNamedBuffer<MemoryBufferMem>(InputData, BufferName);
|
2007-04-29 14:58:52 +08:00
|
|
|
}
|
|
|
|
|
2007-10-10 05:46:38 +08: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 06:42:30 +08:00
|
|
|
MemoryBuffer *MemoryBuffer::getMemBufferCopy(StringRef InputData,
|
2010-06-25 19:50:40 +08: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-10 05:46:38 +08:00
|
|
|
}
|
|
|
|
|
2007-04-29 14:58:52 +08:00
|
|
|
/// getNewUninitMemBuffer - Allocate a new MemoryBuffer of the specified size
|
2010-06-25 19:50:40 +08: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-06 02:30:58 +08:00
|
|
|
MemoryBuffer *MemoryBuffer::getNewUninitMemBuffer(size_t Size,
|
2009-11-10 08:43:58 +08:00
|
|
|
StringRef 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.
|
|
|
|
size_t AlignedStringLen =
|
|
|
|
RoundUpToAlignment(sizeof(MemoryBufferMem) + BufferName.size() + 1,
|
|
|
|
sizeof(void*)); // TODO: Is sizeof(void*) enough?
|
|
|
|
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.
|
|
|
|
|
|
|
|
return new (Mem) MemoryBufferMem(StringRef(Buf, Size));
|
2007-04-29 14:58:52 +08: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 19:50:40 +08:00
|
|
|
MemoryBuffer *MemoryBuffer::getNewMemBuffer(size_t Size, StringRef BufferName) {
|
2007-04-29 14:58:52 +08:00
|
|
|
MemoryBuffer *SB = getNewUninitMemBuffer(Size, BufferName);
|
2009-02-13 15:54:34 +08:00
|
|
|
if (!SB) return 0;
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-11-19 02:52:28 +08: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 11:29:14 +08:00
|
|
|
error_code MemoryBuffer::getFileOrSTDIN(StringRef Filename,
|
|
|
|
OwningPtr<MemoryBuffer> &result,
|
|
|
|
int64_t FileSize) {
|
2009-11-10 08:43:58 +08:00
|
|
|
if (Filename == "-")
|
2010-12-16 11:29:14 +08:00
|
|
|
return getSTDIN(result);
|
|
|
|
return getFile(Filename, result, FileSize);
|
2007-11-19 02:52:28 +08:00
|
|
|
}
|
|
|
|
|
2010-12-16 11:29:14 +08:00
|
|
|
error_code MemoryBuffer::getFileOrSTDIN(const char *Filename,
|
|
|
|
OwningPtr<MemoryBuffer> &result,
|
|
|
|
int64_t FileSize) {
|
2010-06-25 00:25:50 +08:00
|
|
|
if (strcmp(Filename, "-") == 0)
|
2010-12-16 11:29:14 +08:00
|
|
|
return getSTDIN(result);
|
|
|
|
return getFile(Filename, result, FileSize);
|
2010-06-25 00:25:50 +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 {
|
2008-04-01 14:05:21 +08:00
|
|
|
/// MemoryBufferMMapFile - This represents a file that was mapped in with the
|
|
|
|
/// sys::Path::MapInFilePages method. When destroyed, it calls the
|
|
|
|
/// sys::Path::UnMapFilePages method.
|
2010-06-25 19:50:40 +08:00
|
|
|
class MemoryBufferMMapFile : public MemoryBufferMem {
|
2007-04-29 14:58:52 +08:00
|
|
|
public:
|
2010-06-25 19:50:40 +08:00
|
|
|
MemoryBufferMMapFile(StringRef Buffer)
|
|
|
|
: MemoryBufferMem(Buffer) { }
|
|
|
|
|
2008-04-01 14:05:21 +08:00
|
|
|
~MemoryBufferMMapFile() {
|
|
|
|
sys::Path::UnMapFilePages(getBufferStart(), getBufferSize());
|
|
|
|
}
|
2007-04-29 14:58:52 +08:00
|
|
|
};
|
2010-04-01 22:35:22 +08:00
|
|
|
|
|
|
|
/// FileCloser - RAII object to make sure an FD gets closed properly.
|
|
|
|
class FileCloser {
|
|
|
|
int FD;
|
|
|
|
public:
|
2010-06-22 21:53:29 +08:00
|
|
|
explicit FileCloser(int FD) : FD(FD) {}
|
2010-04-01 22:35:22 +08:00
|
|
|
~FileCloser() { ::close(FD); }
|
|
|
|
};
|
2007-04-29 14:58:52 +08:00
|
|
|
}
|
|
|
|
|
2010-12-16 11:29:14 +08:00
|
|
|
error_code MemoryBuffer::getFile(StringRef Filename,
|
|
|
|
OwningPtr<MemoryBuffer> &result,
|
|
|
|
int64_t FileSize) {
|
2010-11-24 06:20:27 +08:00
|
|
|
// Ensure the path is null terminated.
|
2010-06-25 00:25:50 +08:00
|
|
|
SmallString<256> PathBuf(Filename.begin(), Filename.end());
|
2010-12-16 11:29:14 +08:00
|
|
|
return MemoryBuffer::getFile(PathBuf.c_str(), result, FileSize);
|
2010-06-25 00:25:50 +08:00
|
|
|
}
|
|
|
|
|
2010-12-16 11:29:14 +08:00
|
|
|
error_code MemoryBuffer::getFile(const char *Filename,
|
|
|
|
OwningPtr<MemoryBuffer> &result,
|
|
|
|
int64_t FileSize) {
|
2010-06-25 00:25:50 +08:00
|
|
|
int OpenFlags = O_RDONLY;
|
2008-04-01 14:05:21 +08:00
|
|
|
#ifdef O_BINARY
|
2008-04-02 06:09:20 +08:00
|
|
|
OpenFlags |= O_BINARY; // Open input file in binary mode on win32.
|
2008-04-01 14:05:21 +08:00
|
|
|
#endif
|
2010-06-25 00:25:50 +08:00
|
|
|
int FD = ::open(Filename, OpenFlags);
|
2008-04-01 14:05:21 +08:00
|
|
|
if (FD == -1) {
|
2010-12-16 11:29:14 +08:00
|
|
|
return error_code(errno, posix_category());
|
2007-04-29 14:58:52 +08:00
|
|
|
}
|
2010-11-30 02:16:10 +08:00
|
|
|
|
2010-12-16 11:29:14 +08:00
|
|
|
return getOpenFile(FD, Filename, result, FileSize);
|
2010-11-24 06:20:27 +08:00
|
|
|
}
|
2010-11-30 02:16:10 +08:00
|
|
|
|
2010-12-16 11:29:14 +08:00
|
|
|
error_code MemoryBuffer::getOpenFile(int FD, const char *Filename,
|
|
|
|
OwningPtr<MemoryBuffer> &result,
|
|
|
|
int64_t FileSize) {
|
2010-04-01 22:35:22 +08:00
|
|
|
FileCloser FC(FD); // Close FD on return.
|
2010-11-30 02:16:10 +08:00
|
|
|
|
2008-04-01 14:05:21 +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.
|
2010-11-23 14:09:51 +08:00
|
|
|
if (FileSize == -1) {
|
|
|
|
struct stat FileInfo;
|
2008-04-01 14:05:21 +08:00
|
|
|
// TODO: This should use fstat64 when available.
|
2010-11-23 14:09:51 +08:00
|
|
|
if (fstat(FD, &FileInfo) == -1) {
|
2010-12-16 11:29:14 +08:00
|
|
|
return error_code(errno, posix_category());
|
2008-04-01 14:05:21 +08:00
|
|
|
}
|
2010-11-23 14:09:51 +08:00
|
|
|
FileSize = FileInfo.st_size;
|
2007-04-29 14:58:52 +08:00
|
|
|
}
|
2010-11-30 02:16:10 +08:00
|
|
|
|
|
|
|
|
2008-04-01 14:05:21 +08:00
|
|
|
// If the file is large, try to use mmap to read it in. We don't use mmap
|
|
|
|
// for small files, because this can severely fragment our address space. Also
|
|
|
|
// don't try to map files that are exactly a multiple of the system page size,
|
|
|
|
// as the file would not have the required null terminator.
|
2009-11-10 08:43:58 +08:00
|
|
|
//
|
|
|
|
// FIXME: Can we just mmap an extra page in the latter case?
|
2008-04-01 14:05:21 +08:00
|
|
|
if (FileSize >= 4096*4 &&
|
|
|
|
(FileSize & (sys::Process::GetPageSize()-1)) != 0) {
|
|
|
|
if (const char *Pages = sys::Path::MapInFilePages(FD, FileSize)) {
|
2010-12-16 11:29:14 +08:00
|
|
|
result.reset(GetNamedBuffer<MemoryBufferMMapFile>(
|
|
|
|
StringRef(Pages, FileSize), Filename));
|
|
|
|
return success;
|
2008-04-01 14:05:21 +08:00
|
|
|
}
|
2007-05-06 15:24:46 +08:00
|
|
|
}
|
2009-02-13 15:54:34 +08:00
|
|
|
|
|
|
|
MemoryBuffer *Buf = MemoryBuffer::getNewUninitMemBuffer(FileSize, Filename);
|
|
|
|
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.
|
2010-12-16 11:29:14 +08:00
|
|
|
return make_error_code(errc::not_enough_memory);
|
2009-02-13 15:54:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
OwningPtr<MemoryBuffer> SB(Buf);
|
2007-04-29 14:58:52 +08:00
|
|
|
char *BufPtr = const_cast<char*>(SB->getBufferStart());
|
2010-04-01 22:35:22 +08:00
|
|
|
|
2008-05-06 02:30:58 +08:00
|
|
|
size_t BytesLeft = FileSize;
|
2007-04-29 14:58:52 +08:00
|
|
|
while (BytesLeft) {
|
|
|
|
ssize_t NumRead = ::read(FD, BufPtr, BytesLeft);
|
2010-04-01 22:35:22 +08:00
|
|
|
if (NumRead == -1) {
|
|
|
|
if (errno == EINTR)
|
|
|
|
continue;
|
|
|
|
// Error while reading.
|
2010-12-16 11:29:14 +08:00
|
|
|
return error_code(errno, posix_category());
|
2010-04-01 22:35:22 +08:00
|
|
|
} else if (NumRead == 0) {
|
2010-04-01 22:39:55 +08:00
|
|
|
// We hit EOF early, truncate and terminate buffer.
|
2010-04-01 22:35:22 +08:00
|
|
|
Buf->BufferEnd = BufPtr;
|
2010-04-01 22:39:55 +08:00
|
|
|
*BufPtr = 0;
|
2010-12-16 11:29:14 +08:00
|
|
|
result.swap(SB);
|
|
|
|
return success;
|
2007-04-29 14:58:52 +08:00
|
|
|
}
|
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
|
|
|
|
2010-12-16 11:29:14 +08:00
|
|
|
result.swap(SB);
|
|
|
|
return success;
|
2007-04-29 14:58:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// MemoryBuffer::getSTDIN implementation.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2010-12-16 11:29:14 +08:00
|
|
|
error_code MemoryBuffer::getSTDIN(OwningPtr<MemoryBuffer> &result) {
|
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.
|
2007-05-07 23:21:46 +08:00
|
|
|
sys::Program::ChangeStdinToBinary();
|
2007-08-09 04:01:58 +08:00
|
|
|
|
2010-06-26 00:07:18 +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(0, Buffer.end(), ChunkSize);
|
|
|
|
if (ReadBytes == -1) {
|
|
|
|
if (errno == EINTR) continue;
|
2010-12-16 11:29:14 +08:00
|
|
|
return error_code(errno, posix_category());
|
2010-06-26 00:07:18 +08:00
|
|
|
}
|
|
|
|
Buffer.set_size(Buffer.size() + ReadBytes);
|
|
|
|
} while (ReadBytes != 0);
|
2010-05-28 01:31:51 +08:00
|
|
|
|
2010-12-16 11:29:14 +08:00
|
|
|
result.reset(getMemBufferCopy(Buffer, "<stdin>"));
|
|
|
|
return success;
|
2007-04-29 14:58:52 +08:00
|
|
|
}
|