2007-04-29 06:58:52 +00:00
|
|
|
//===--- MemoryBuffer.cpp - Memory Buffer implementation ------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-29 20:36:04 +00:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2007-04-29 06:58:52 +00:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file implements the MemoryBuffer interface.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/Support/MemoryBuffer.h"
|
2008-04-01 06:05:21 +00:00
|
|
|
#include "llvm/ADT/OwningPtr.h"
|
|
|
|
#include "llvm/ADT/SmallString.h"
|
|
|
|
#include "llvm/System/Path.h"
|
2007-04-29 06:58:52 +00:00
|
|
|
#include "llvm/System/Process.h"
|
2007-05-07 15:21:46 +00:00
|
|
|
#include "llvm/System/Program.h"
|
2007-04-29 14:21:44 +00:00
|
|
|
#include <cassert>
|
2007-04-29 06:58:52 +00:00
|
|
|
#include <cstdio>
|
|
|
|
#include <cstring>
|
|
|
|
#include <cerrno>
|
2008-04-01 06:05:21 +00: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 08:53:22 +00:00
|
|
|
#include <fcntl.h>
|
2007-04-29 06:58:52 +00:00
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// MemoryBuffer implementation itself.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
MemoryBuffer::~MemoryBuffer() {
|
|
|
|
if (MustDeleteBuffer)
|
2009-02-13 08:24:55 +00:00
|
|
|
free((void*)BufferStart);
|
2007-04-29 06:58:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// initCopyOf - Initialize this source buffer with a copy of the specified
|
|
|
|
/// memory range. We make the copy so that we can null terminate it
|
|
|
|
/// successfully.
|
|
|
|
void MemoryBuffer::initCopyOf(const char *BufStart, const char *BufEnd) {
|
|
|
|
size_t Size = BufEnd-BufStart;
|
2009-02-13 08:24:55 +00:00
|
|
|
BufferStart = (char *)malloc((Size+1) * sizeof(char));
|
2007-04-29 06:58:52 +00:00
|
|
|
BufferEnd = BufferStart+Size;
|
|
|
|
memcpy(const_cast<char*>(BufferStart), BufStart, Size);
|
|
|
|
*const_cast<char*>(BufferEnd) = 0; // Null terminate buffer.
|
2007-05-11 00:43:26 +00:00
|
|
|
MustDeleteBuffer = true;
|
2007-04-29 06:58:52 +00: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;
|
|
|
|
MustDeleteBuffer = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// MemoryBufferMem implementation.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
class MemoryBufferMem : public MemoryBuffer {
|
|
|
|
std::string FileID;
|
|
|
|
public:
|
2009-11-10 00:43:58 +00:00
|
|
|
MemoryBufferMem(const char *Start, const char *End, StringRef FID,
|
2007-10-09 21:46:38 +00:00
|
|
|
bool Copy = false)
|
2007-04-29 06:58:52 +00:00
|
|
|
: FileID(FID) {
|
2007-10-09 21:46:38 +00:00
|
|
|
if (!Copy)
|
|
|
|
init(Start, End);
|
|
|
|
else
|
|
|
|
initCopyOf(Start, End);
|
2007-04-29 06:58:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
virtual const char *getBufferIdentifier() const {
|
|
|
|
return FileID.c_str();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/// getMemBuffer - Open the specified memory range as a MemoryBuffer. Note
|
|
|
|
/// that EndPtr[0] must be a null byte and be accessible!
|
|
|
|
MemoryBuffer *MemoryBuffer::getMemBuffer(const char *StartPtr,
|
|
|
|
const char *EndPtr,
|
|
|
|
const char *BufferName) {
|
|
|
|
return new MemoryBufferMem(StartPtr, EndPtr, BufferName);
|
|
|
|
}
|
|
|
|
|
2007-10-09 21:46:38 +00: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].
|
|
|
|
MemoryBuffer *MemoryBuffer::getMemBufferCopy(const char *StartPtr,
|
|
|
|
const char *EndPtr,
|
|
|
|
const char *BufferName) {
|
|
|
|
return new MemoryBufferMem(StartPtr, EndPtr, BufferName, true);
|
|
|
|
}
|
|
|
|
|
2007-04-29 06:58:52 +00:00
|
|
|
/// getNewUninitMemBuffer - 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.
|
2008-05-05 18:30:58 +00:00
|
|
|
MemoryBuffer *MemoryBuffer::getNewUninitMemBuffer(size_t Size,
|
2009-11-10 00:43:58 +00:00
|
|
|
StringRef BufferName) {
|
2009-02-13 08:24:55 +00:00
|
|
|
char *Buf = (char *)malloc((Size+1) * sizeof(char));
|
2009-02-13 07:54:34 +00:00
|
|
|
if (!Buf) return 0;
|
2007-04-29 06:58:52 +00:00
|
|
|
Buf[Size] = 0;
|
|
|
|
MemoryBufferMem *SB = new MemoryBufferMem(Buf, Buf+Size, BufferName);
|
|
|
|
// The memory for this buffer is owned by the MemoryBuffer.
|
|
|
|
SB->MustDeleteBuffer = true;
|
|
|
|
return SB;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// 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.
|
2008-05-05 18:30:58 +00:00
|
|
|
MemoryBuffer *MemoryBuffer::getNewMemBuffer(size_t Size,
|
2007-04-29 06:58:52 +00:00
|
|
|
const char *BufferName) {
|
|
|
|
MemoryBuffer *SB = getNewUninitMemBuffer(Size, BufferName);
|
2009-02-13 07:54:34 +00:00
|
|
|
if (!SB) return 0;
|
2007-04-29 06:58:52 +00:00
|
|
|
memset(const_cast<char*>(SB->getBufferStart()), 0, Size+1);
|
|
|
|
return SB;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-11-18 18:52:28 +00: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.
|
2009-11-10 00:43:58 +00:00
|
|
|
MemoryBuffer *MemoryBuffer::getFileOrSTDIN(StringRef Filename,
|
2007-11-18 18:52:28 +00:00
|
|
|
std::string *ErrStr,
|
|
|
|
int64_t FileSize) {
|
2009-11-10 00:43:58 +00:00
|
|
|
if (Filename == "-")
|
|
|
|
return getSTDIN();
|
|
|
|
return getFile(Filename, ErrStr, FileSize);
|
2007-11-18 18:52:28 +00:00
|
|
|
}
|
|
|
|
|
2007-04-29 06:58:52 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
2008-04-01 06:05:21 +00:00
|
|
|
// MemoryBuffer::getFile implementation.
|
2007-04-29 06:58:52 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
namespace {
|
2008-04-01 06:05:21 +00: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.
|
2007-04-29 06:58:52 +00:00
|
|
|
class MemoryBufferMMapFile : public MemoryBuffer {
|
2008-04-01 06:05:21 +00:00
|
|
|
std::string Filename;
|
2007-04-29 06:58:52 +00:00
|
|
|
public:
|
2009-11-10 00:43:58 +00:00
|
|
|
MemoryBufferMMapFile(StringRef filename, const char *Pages, uint64_t Size)
|
2008-04-01 06:05:21 +00:00
|
|
|
: Filename(filename) {
|
|
|
|
init(Pages, Pages+Size);
|
|
|
|
}
|
2007-04-29 06:58:52 +00:00
|
|
|
|
|
|
|
virtual const char *getBufferIdentifier() const {
|
2008-04-01 06:05:21 +00:00
|
|
|
return Filename.c_str();
|
2007-04-29 06:58:52 +00:00
|
|
|
}
|
|
|
|
|
2008-04-01 06:05:21 +00:00
|
|
|
~MemoryBufferMMapFile() {
|
|
|
|
sys::Path::UnMapFilePages(getBufferStart(), getBufferSize());
|
|
|
|
}
|
2007-04-29 06:58:52 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2009-11-10 00:43:58 +00:00
|
|
|
MemoryBuffer *MemoryBuffer::getFile(StringRef Filename, std::string *ErrStr,
|
2008-04-01 18:04:03 +00:00
|
|
|
int64_t FileSize) {
|
2008-04-01 06:05:21 +00:00
|
|
|
int OpenFlags = 0;
|
|
|
|
#ifdef O_BINARY
|
2008-04-01 22:09:20 +00:00
|
|
|
OpenFlags |= O_BINARY; // Open input file in binary mode on win32.
|
2008-04-01 06:05:21 +00:00
|
|
|
#endif
|
2009-11-10 00:43:58 +00:00
|
|
|
int FD = ::open(Filename.str().c_str(), O_RDONLY|OpenFlags);
|
2008-04-01 06:05:21 +00:00
|
|
|
if (FD == -1) {
|
|
|
|
if (ErrStr) *ErrStr = "could not open file";
|
|
|
|
return 0;
|
2007-04-29 06:58:52 +00:00
|
|
|
}
|
|
|
|
|
2008-04-01 06:05:21 +00: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.
|
2007-04-29 06:58:52 +00:00
|
|
|
if (FileSize == -1) {
|
2008-04-01 06:05:21 +00:00
|
|
|
struct stat FileInfo;
|
|
|
|
// TODO: This should use fstat64 when available.
|
|
|
|
if (fstat(FD, &FileInfo) == -1) {
|
|
|
|
if (ErrStr) *ErrStr = "could not get file length";
|
|
|
|
::close(FD);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
FileSize = FileInfo.st_size;
|
2007-04-29 06:58:52 +00:00
|
|
|
}
|
|
|
|
|
2008-04-01 06:05:21 +00: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 00:43:58 +00:00
|
|
|
//
|
|
|
|
// FIXME: Can we just mmap an extra page in the latter case?
|
2008-04-01 06:05:21 +00:00
|
|
|
if (FileSize >= 4096*4 &&
|
|
|
|
(FileSize & (sys::Process::GetPageSize()-1)) != 0) {
|
|
|
|
if (const char *Pages = sys::Path::MapInFilePages(FD, FileSize)) {
|
|
|
|
// Close the file descriptor, now that the whole file is in memory.
|
|
|
|
::close(FD);
|
2008-04-01 18:04:03 +00:00
|
|
|
return new MemoryBufferMMapFile(Filename, Pages, FileSize);
|
2008-04-01 06:05:21 +00:00
|
|
|
}
|
2007-05-06 07:24:46 +00:00
|
|
|
}
|
2009-02-13 07:54:34 +00:00
|
|
|
|
|
|
|
MemoryBuffer *Buf = MemoryBuffer::getNewUninitMemBuffer(FileSize, Filename);
|
|
|
|
if (!Buf) {
|
|
|
|
// Failed to create a buffer.
|
|
|
|
if (ErrStr) *ErrStr = "could not allocate buffer";
|
|
|
|
::close(FD);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
OwningPtr<MemoryBuffer> SB(Buf);
|
2007-04-29 06:58:52 +00:00
|
|
|
char *BufPtr = const_cast<char*>(SB->getBufferStart());
|
|
|
|
|
2008-05-05 18:30:58 +00:00
|
|
|
size_t BytesLeft = FileSize;
|
2007-04-29 06:58:52 +00:00
|
|
|
while (BytesLeft) {
|
|
|
|
ssize_t NumRead = ::read(FD, BufPtr, BytesLeft);
|
2009-11-03 19:10:22 +00:00
|
|
|
if (NumRead > 0) {
|
2007-04-29 06:58:52 +00:00
|
|
|
BytesLeft -= NumRead;
|
|
|
|
BufPtr += NumRead;
|
2009-11-04 20:50:23 +00:00
|
|
|
} else if (NumRead == -1 && errno == EINTR) {
|
2007-04-29 06:58:52 +00:00
|
|
|
// try again
|
|
|
|
} else {
|
|
|
|
// error reading.
|
|
|
|
close(FD);
|
2008-04-01 06:05:21 +00:00
|
|
|
if (ErrStr) *ErrStr = "error reading file data";
|
2007-04-29 06:58:52 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
close(FD);
|
|
|
|
|
2008-04-01 06:05:21 +00:00
|
|
|
return SB.take();
|
2007-04-29 06:58:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// MemoryBuffer::getSTDIN implementation.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
class STDINBufferFile : public MemoryBuffer {
|
|
|
|
public:
|
|
|
|
virtual const char *getBufferIdentifier() const {
|
|
|
|
return "<stdin>";
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
MemoryBuffer *MemoryBuffer::getSTDIN() {
|
|
|
|
char Buffer[4096*4];
|
2009-05-18 08:44:04 +00:00
|
|
|
|
2007-04-29 06:58:52 +00:00
|
|
|
std::vector<char> FileData;
|
2009-05-18 08:44:04 +00:00
|
|
|
|
2007-04-29 06:58:52 +00:00
|
|
|
// Read in all of the data from stdin, we cannot mmap stdin.
|
2009-11-10 00:43:58 +00:00
|
|
|
//
|
|
|
|
// FIXME: That isn't necessarily true, we should try to mmap stdin and
|
|
|
|
// fallback if it fails.
|
2007-05-07 15:21:46 +00:00
|
|
|
sys::Program::ChangeStdinToBinary();
|
2009-05-18 08:44:04 +00:00
|
|
|
size_t ReadBytes;
|
|
|
|
do {
|
|
|
|
ReadBytes = fread(Buffer, sizeof(char), sizeof(Buffer), stdin);
|
2007-04-29 06:58:52 +00:00
|
|
|
FileData.insert(FileData.end(), Buffer, Buffer+ReadBytes);
|
2009-05-18 08:44:04 +00:00
|
|
|
} while (ReadBytes == sizeof(Buffer));
|
2007-08-08 20:01:58 +00:00
|
|
|
|
2007-07-01 03:06:30 +00:00
|
|
|
FileData.push_back(0); // &FileData[Size] is invalid. So is &*FileData.end().
|
2007-04-29 06:58:52 +00:00
|
|
|
size_t Size = FileData.size();
|
|
|
|
MemoryBuffer *B = new STDINBufferFile();
|
2007-07-01 03:06:30 +00:00
|
|
|
B->initCopyOf(&FileData[0], &FileData[Size-1]);
|
2007-04-29 06:58:52 +00:00
|
|
|
return B;
|
|
|
|
}
|