summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorArgyrios Kyrtzidis <akyrtzi@gmail.com>2014-05-05 21:55:51 +0000
committerArgyrios Kyrtzidis <akyrtzi@gmail.com>2014-05-05 21:55:51 +0000
commit32b6099a7729dbd97dc1e8551808c5f3f3517af3 (patch)
tree70217e25fcb9b7c39573b5795668fad31159e357
parent73ebb5abafee2d695ddc4555225eec436b7e25a2 (diff)
downloadllvm-32b6099a7729dbd97dc1e8551808c5f3f3517af3.tar.gz
llvm-32b6099a7729dbd97dc1e8551808c5f3f3517af3.tar.bz2
llvm-32b6099a7729dbd97dc1e8551808c5f3f3517af3.tar.xz
[Support/MemoryBuffer] Introduce a boolean parameter (false by default) 'IsVolatile' for the open file functions.
This provides a hint that the file may be changing often so mmap is avoided. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@208007 91177308-0d34-0410-b5e6-96231b3b80d8
-rw-r--r--include/llvm/Support/MemoryBuffer.h24
-rw-r--r--lib/Support/MemoryBuffer.cpp50
2 files changed, 50 insertions, 24 deletions
diff --git a/include/llvm/Support/MemoryBuffer.h b/include/llvm/Support/MemoryBuffer.h
index 578c7e8eb8..268041e781 100644
--- a/include/llvm/Support/MemoryBuffer.h
+++ b/include/llvm/Support/MemoryBuffer.h
@@ -67,34 +67,46 @@ public:
/// MemoryBuffer if successful, otherwise returning null. If FileSize is
/// specified, this means that the client knows that the file exists and that
/// it has the specified size.
+ ///
+ /// \param IsVolatile true indicates that the file may be changing often.
static error_code getFile(Twine Filename, OwningPtr<MemoryBuffer> &Result,
int64_t FileSize = -1,
- bool RequiresNullTerminator = true);
+ bool RequiresNullTerminator = true,
+ bool IsVolatile = false);
static error_code getFile(Twine Filename,
std::unique_ptr<MemoryBuffer> &Result,
int64_t FileSize = -1,
- bool RequiresNullTerminator = true);
+ bool RequiresNullTerminator = true,
+ bool IsVolatile = false);
/// Given an already-open file descriptor, map some slice of it into a
/// MemoryBuffer. The slice is specified by an \p Offset and \p MapSize.
/// Since this is in the middle of a file, the buffer is not null terminated.
+ ///
+ /// \param IsVolatile true indicates that the file may be changing often.
static error_code getOpenFileSlice(int FD, const char *Filename,
OwningPtr<MemoryBuffer> &Result,
- uint64_t MapSize, int64_t Offset);
+ uint64_t MapSize, int64_t Offset,
+ bool IsVolatile = false);
static error_code getOpenFileSlice(int FD, const char *Filename,
std::unique_ptr<MemoryBuffer> &Result,
- uint64_t MapSize, int64_t Offset);
+ uint64_t MapSize, int64_t Offset,
+ bool IsVolatile = false);
/// Given an already-open file descriptor, read the file and return a
/// MemoryBuffer.
+ ///
+ /// \param IsVolatile true indicates that the file may be changing often.
static error_code getOpenFile(int FD, const char *Filename,
OwningPtr<MemoryBuffer> &Result,
uint64_t FileSize,
- bool RequiresNullTerminator = true);
+ bool RequiresNullTerminator = true,
+ bool IsVolatile = false);
static error_code getOpenFile(int FD, const char *Filename,
std::unique_ptr<MemoryBuffer> &Result,
uint64_t FileSize,
- bool RequiresNullTerminator = true);
+ bool RequiresNullTerminator = true,
+ bool IsVolatile = false);
/// getMemBuffer - Open the specified memory range as a MemoryBuffer. Note
/// that InputData must be null terminated if RequiresNullTerminator is true.
diff --git a/lib/Support/MemoryBuffer.cpp b/lib/Support/MemoryBuffer.cpp
index 773ccfd6f7..dc615d2a42 100644
--- a/lib/Support/MemoryBuffer.cpp
+++ b/lib/Support/MemoryBuffer.cpp
@@ -252,25 +252,29 @@ static error_code getMemoryBufferForStream(int FD,
static error_code getFileAux(const char *Filename,
std::unique_ptr<MemoryBuffer> &Result,
int64_t FileSize,
- bool RequiresNullTerminator);
+ bool RequiresNullTerminator,
+ bool IsVolatile);
error_code MemoryBuffer::getFile(Twine Filename,
std::unique_ptr<MemoryBuffer> &Result,
int64_t FileSize,
- bool RequiresNullTerminator) {
+ bool RequiresNullTerminator,
+ bool IsVolatile) {
// Ensure the path is null terminated.
SmallString<256> PathBuf;
StringRef NullTerminatedName = Filename.toNullTerminatedStringRef(PathBuf);
return getFileAux(NullTerminatedName.data(), Result, FileSize,
- RequiresNullTerminator);
+ RequiresNullTerminator, IsVolatile);
}
error_code MemoryBuffer::getFile(Twine Filename,
OwningPtr<MemoryBuffer> &Result,
int64_t FileSize,
- bool RequiresNullTerminator) {
+ bool RequiresNullTerminator,
+ bool IsVolatile) {
std::unique_ptr<MemoryBuffer> MB;
- error_code ec = getFile(Filename, MB, FileSize, RequiresNullTerminator);
+ error_code ec = getFile(Filename, MB, FileSize, RequiresNullTerminator,
+ IsVolatile);
Result = std::move(MB);
return ec;
}
@@ -278,18 +282,20 @@ error_code MemoryBuffer::getFile(Twine Filename,
static error_code getOpenFileImpl(int FD, const char *Filename,
std::unique_ptr<MemoryBuffer> &Result,
uint64_t FileSize, uint64_t MapSize,
- int64_t Offset, bool RequiresNullTerminator);
+ int64_t Offset, bool RequiresNullTerminator,
+ bool IsVolatile);
static error_code getFileAux(const char *Filename,
std::unique_ptr<MemoryBuffer> &Result, int64_t FileSize,
- bool RequiresNullTerminator) {
+ bool RequiresNullTerminator,
+ bool IsVolatile) {
int FD;
error_code EC = sys::fs::openFileForRead(Filename, FD);
if (EC)
return EC;
error_code ret = getOpenFileImpl(FD, Filename, Result, FileSize, FileSize, 0,
- RequiresNullTerminator);
+ RequiresNullTerminator, IsVolatile);
close(FD);
return ret;
}
@@ -348,7 +354,8 @@ static bool shouldUseMmap(int FD,
static error_code getOpenFileImpl(int FD, const char *Filename,
std::unique_ptr<MemoryBuffer> &Result,
uint64_t FileSize, uint64_t MapSize,
- int64_t Offset, bool RequiresNullTerminator) {
+ int64_t Offset, bool RequiresNullTerminator,
+ bool IsVolatile) {
static int PageSize = sys::process::get_self()->page_size();
// Default is to map the full file.
@@ -374,7 +381,8 @@ static error_code getOpenFileImpl(int FD, const char *Filename,
MapSize = FileSize;
}
- if (shouldUseMmap(FD, FileSize, MapSize, Offset, RequiresNullTerminator,
+ if (!IsVolatile &&
+ shouldUseMmap(FD, FileSize, MapSize, Offset, RequiresNullTerminator,
PageSize)) {
error_code EC;
Result.reset(new (NamedBufferAlloc(Filename)) MemoryBufferMMapFile(
@@ -428,33 +436,39 @@ static error_code getOpenFileImpl(int FD, const char *Filename,
error_code MemoryBuffer::getOpenFile(int FD, const char *Filename,
std::unique_ptr<MemoryBuffer> &Result,
uint64_t FileSize,
- bool RequiresNullTerminator) {
+ bool RequiresNullTerminator,
+ bool IsVolatile) {
return getOpenFileImpl(FD, Filename, Result, FileSize, FileSize, 0,
- RequiresNullTerminator);
+ RequiresNullTerminator, IsVolatile);
}
error_code MemoryBuffer::getOpenFile(int FD, const char *Filename,
OwningPtr<MemoryBuffer> &Result,
uint64_t FileSize,
- bool RequiresNullTerminator) {
+ bool RequiresNullTerminator,
+ bool IsVolatile) {
std::unique_ptr<MemoryBuffer> MB;
error_code ec = getOpenFileImpl(FD, Filename, MB, FileSize, FileSize, 0,
- RequiresNullTerminator);
+ RequiresNullTerminator, IsVolatile);
Result = std::move(MB);
return ec;
}
error_code MemoryBuffer::getOpenFileSlice(int FD, const char *Filename,
std::unique_ptr<MemoryBuffer> &Result,
- uint64_t MapSize, int64_t Offset) {
- return getOpenFileImpl(FD, Filename, Result, -1, MapSize, Offset, false);
+ uint64_t MapSize, int64_t Offset,
+ bool IsVolatile) {
+ return getOpenFileImpl(FD, Filename, Result, -1, MapSize, Offset, false,
+ IsVolatile);
}
error_code MemoryBuffer::getOpenFileSlice(int FD, const char *Filename,
OwningPtr<MemoryBuffer> &Result,
- uint64_t MapSize, int64_t Offset) {
+ uint64_t MapSize, int64_t Offset,
+ bool IsVolatile) {
std::unique_ptr<MemoryBuffer> MB;
- error_code ec = getOpenFileImpl(FD, Filename, MB, -1, MapSize, Offset, false);
+ error_code ec = getOpenFileImpl(FD, Filename, MB, -1, MapSize, Offset, false,
+ IsVolatile);
Result = std::move(MB);
return ec;
}