diff options
author | Rafael Espindola <rafael.espindola@gmail.com> | 2011-03-10 20:54:07 +0000 |
---|---|---|
committer | Rafael Espindola <rafael.espindola@gmail.com> | 2011-03-10 20:54:07 +0000 |
commit | cbe6a1ae86acc54ab5fa0b6692c3de44f532c611 (patch) | |
tree | 12f6b002f64581f4c020cc98b60098fcf478cb3e /llvm/lib/Support | |
parent | 4c0826c2365705e63b4e73cdb5092a94c0a84d5e (diff) | |
download | bcm5719-llvm-cbe6a1ae86acc54ab5fa0b6692c3de44f532c611.tar.gz bcm5719-llvm-cbe6a1ae86acc54ab5fa0b6692c3de44f532c611.zip |
Don't compute the file size if we don't need to.
llvm-svn: 127426
Diffstat (limited to 'llvm/lib/Support')
-rw-r--r-- | llvm/lib/Support/MemoryBuffer.cpp | 43 |
1 files changed, 29 insertions, 14 deletions
diff --git a/llvm/lib/Support/MemoryBuffer.cpp b/llvm/lib/Support/MemoryBuffer.cpp index 80d556cbf5b..2be0460149a 100644 --- a/llvm/lib/Support/MemoryBuffer.cpp +++ b/llvm/lib/Support/MemoryBuffer.cpp @@ -216,7 +216,8 @@ error_code MemoryBuffer::getFile(const char *Filename, return ret; } -static bool shouldUseMmap(size_t FileSize, +static bool shouldUseMmap(int FD, + size_t FileSize, size_t MapSize, off_t Offset, bool RequiresNullTerminator, @@ -229,6 +230,20 @@ static bool shouldUseMmap(size_t FileSize, if (!RequiresNullTerminator) return true; + + // 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)) { + struct stat FileInfo; + // TODO: This should use fstat64 when available. + if (fstat(FD, &FileInfo) == -1) { + return error_code(errno, posix_category()); + } + FileSize = FileInfo.st_size; + } + // 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; @@ -251,22 +266,22 @@ error_code MemoryBuffer::getOpenFile(int FD, const char *Filename, bool RequiresNullTerminator) { static int PageSize = sys::Process::GetPageSize(); - // 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. - if (FileSize == size_t(-1)) { - struct stat FileInfo; - // TODO: This should use fstat64 when available. - if (fstat(FD, &FileInfo) == -1) { - return error_code(errno, posix_category()); - } - FileSize = FileInfo.st_size; - } - // Default is to map the full file. - if (MapSize == size_t(-1)) + if (MapSize == size_t(-1)) { + // 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. + if (FileSize == size_t(-1)) { + struct stat FileInfo; + // TODO: This should use fstat64 when available. + if (fstat(FD, &FileInfo) == -1) { + return error_code(errno, posix_category()); + } + FileSize = FileInfo.st_size; + } MapSize = FileSize; + } - if (shouldUseMmap(FileSize, MapSize, Offset, RequiresNullTerminator, + if (shouldUseMmap(FD, FileSize, MapSize, Offset, RequiresNullTerminator, PageSize)) { off_t RealMapOffset = Offset & ~(PageSize - 1); off_t Delta = Offset - RealMapOffset; |