diff options
Diffstat (limited to 'lib/Support/MemoryBuffer.cpp')
-rw-r--r-- | lib/Support/MemoryBuffer.cpp | 35 |
1 files changed, 24 insertions, 11 deletions
diff --git a/lib/Support/MemoryBuffer.cpp b/lib/Support/MemoryBuffer.cpp index cab45c7..dcd5529 100644 --- a/lib/Support/MemoryBuffer.cpp +++ b/lib/Support/MemoryBuffer.cpp @@ -177,7 +177,7 @@ error_code MemoryBuffer::getFileOrSTDIN(StringRef Filename, //===----------------------------------------------------------------------===// namespace { -/// \brief Memorry maps a file descriptor using sys::fs::mapped_file_region. +/// \brief Memory maps a file descriptor using sys::fs::mapped_file_region. /// /// This handles converting the offset into a legal offset on the platform. class MemoryBufferMMapFile : public MemoryBuffer { @@ -217,7 +217,7 @@ public: }; } -static error_code getMemoryBufferForStream(int FD, +static error_code getMemoryBufferForStream(int FD, StringRef BufferName, OwningPtr<MemoryBuffer> &result) { const ssize_t ChunkSize = 4096*4; @@ -238,14 +238,19 @@ static error_code getMemoryBufferForStream(int FD, return error_code::success(); } -error_code MemoryBuffer::getFile(StringRef Filename, +static error_code getFileAux(const char *Filename, + OwningPtr<MemoryBuffer> &result, int64_t FileSize, + bool RequiresNullTerminator); + +error_code MemoryBuffer::getFile(Twine Filename, OwningPtr<MemoryBuffer> &result, int64_t FileSize, bool RequiresNullTerminator) { // Ensure the path is null terminated. - SmallString<256> PathBuf(Filename.begin(), Filename.end()); - return MemoryBuffer::getFile(PathBuf.c_str(), result, FileSize, - RequiresNullTerminator); + SmallString<256> PathBuf; + StringRef NullTerminatedName = Filename.toNullTerminatedStringRef(PathBuf); + return getFileAux(NullTerminatedName.data(), result, FileSize, + RequiresNullTerminator); } static error_code getOpenFileImpl(int FD, const char *Filename, @@ -253,10 +258,9 @@ static error_code getOpenFileImpl(int FD, const char *Filename, uint64_t FileSize, uint64_t MapSize, int64_t Offset, bool RequiresNullTerminator); -error_code MemoryBuffer::getFile(const char *Filename, - OwningPtr<MemoryBuffer> &result, - int64_t FileSize, - bool RequiresNullTerminator) { +static error_code getFileAux(const char *Filename, + OwningPtr<MemoryBuffer> &result, int64_t FileSize, + bool RequiresNullTerminator) { int FD; error_code EC = sys::fs::openFileForRead(Filename, FD); if (EC) @@ -276,7 +280,7 @@ static bool shouldUseMmap(int FD, int PageSize) { // We don't use mmap for small files because this can severely fragment our // address space. - if (MapSize < 4096*4) + if (MapSize < 4 * 4096 || MapSize < (unsigned)PageSize) return false; if (!RequiresNullTerminator) @@ -302,6 +306,15 @@ static bool shouldUseMmap(int FD, if (End != FileSize) return false; +#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 + // 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) |