Skip to content

Commit

Permalink
[Support/MemoryBuffer] Rename IsVolatile -> IsVolatileSize and add a …
Browse files Browse the repository at this point in the history
…comment about the use case for the new parameter.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@208026 91177308-0d34-0410-b5e6-96231b3b80d8
  • Loading branch information
akyrtzi committed May 6, 2014
1 parent 245e8bd commit 10222d2
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 31 deletions.
24 changes: 15 additions & 9 deletions include/llvm/Support/MemoryBuffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,45 +68,51 @@ class MemoryBuffer {
/// 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.
/// \param IsVolatileSize Set to true to indicate that the file size may be
/// changing, e.g. when libclang tries to parse while the user is
/// editing/updating the file.
static error_code getFile(Twine Filename, OwningPtr<MemoryBuffer> &Result,
int64_t FileSize = -1,
bool RequiresNullTerminator = true,
bool IsVolatile = false);
bool IsVolatileSize = false);
static error_code getFile(Twine Filename,
std::unique_ptr<MemoryBuffer> &Result,
int64_t FileSize = -1,
bool RequiresNullTerminator = true,
bool IsVolatile = false);
bool IsVolatileSize = 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.
/// \param IsVolatileSize Set to true to indicate that the file size may be
/// changing, e.g. when libclang tries to parse while the user is
/// editing/updating the file.
static error_code getOpenFileSlice(int FD, const char *Filename,
OwningPtr<MemoryBuffer> &Result,
uint64_t MapSize, int64_t Offset,
bool IsVolatile = false);
bool IsVolatileSize = false);
static error_code getOpenFileSlice(int FD, const char *Filename,
std::unique_ptr<MemoryBuffer> &Result,
uint64_t MapSize, int64_t Offset,
bool IsVolatile = false);
bool IsVolatileSize = 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.
/// \param IsVolatileSize Set to true to indicate that the file size may be
/// changing, e.g. when libclang tries to parse while the user is
/// editing/updating the file.
static error_code getOpenFile(int FD, const char *Filename,
OwningPtr<MemoryBuffer> &Result,
uint64_t FileSize,
bool RequiresNullTerminator = true,
bool IsVolatile = false);
bool IsVolatileSize = false);
static error_code getOpenFile(int FD, const char *Filename,
std::unique_ptr<MemoryBuffer> &Result,
uint64_t FileSize,
bool RequiresNullTerminator = true,
bool IsVolatile = false);
bool IsVolatileSize = false);

/// getMemBuffer - Open the specified memory range as a MemoryBuffer. Note
/// that InputData must be null terminated if RequiresNullTerminator is true.
Expand Down
45 changes: 23 additions & 22 deletions lib/Support/MemoryBuffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -253,28 +253,28 @@ static error_code getFileAux(const char *Filename,
std::unique_ptr<MemoryBuffer> &Result,
int64_t FileSize,
bool RequiresNullTerminator,
bool IsVolatile);
bool IsVolatileSize);

error_code MemoryBuffer::getFile(Twine Filename,
std::unique_ptr<MemoryBuffer> &Result,
int64_t FileSize,
bool RequiresNullTerminator,
bool IsVolatile) {
bool IsVolatileSize) {
// Ensure the path is null terminated.
SmallString<256> PathBuf;
StringRef NullTerminatedName = Filename.toNullTerminatedStringRef(PathBuf);
return getFileAux(NullTerminatedName.data(), Result, FileSize,
RequiresNullTerminator, IsVolatile);
RequiresNullTerminator, IsVolatileSize);
}

error_code MemoryBuffer::getFile(Twine Filename,
OwningPtr<MemoryBuffer> &Result,
int64_t FileSize,
bool RequiresNullTerminator,
bool IsVolatile) {
bool IsVolatileSize) {
std::unique_ptr<MemoryBuffer> MB;
error_code ec = getFile(Filename, MB, FileSize, RequiresNullTerminator,
IsVolatile);
IsVolatileSize);
Result = std::move(MB);
return ec;
}
Expand All @@ -283,19 +283,19 @@ 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,
bool IsVolatile);
bool IsVolatileSize);

static error_code getFileAux(const char *Filename,
std::unique_ptr<MemoryBuffer> &Result, int64_t FileSize,
bool RequiresNullTerminator,
bool IsVolatile) {
bool IsVolatileSize) {
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, IsVolatile);
RequiresNullTerminator, IsVolatileSize);
close(FD);
return ret;
}
Expand All @@ -306,11 +306,11 @@ static bool shouldUseMmap(int FD,
off_t Offset,
bool RequiresNullTerminator,
int PageSize,
bool IsVolatile) {
bool IsVolatileSize) {
// mmap may leave the buffer without null terminator if the file size changed
// by the time the last page is mapped in, so avoid it if the file size is
// likely to change.
if (IsVolatile)
if (IsVolatileSize)
return false;

// We don't use mmap for small files because this can severely fragment our
Expand Down Expand Up @@ -362,7 +362,7 @@ 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,
bool IsVolatile) {
bool IsVolatileSize) {
static int PageSize = sys::process::get_self()->page_size();

// Default is to map the full file.
Expand All @@ -389,7 +389,7 @@ static error_code getOpenFileImpl(int FD, const char *Filename,
}

if (shouldUseMmap(FD, FileSize, MapSize, Offset, RequiresNullTerminator,
PageSize, IsVolatile)) {
PageSize, IsVolatileSize)) {
error_code EC;
Result.reset(new (NamedBufferAlloc(Filename)) MemoryBufferMMapFile(
RequiresNullTerminator, FD, MapSize, Offset, EC));
Expand Down Expand Up @@ -426,8 +426,9 @@ static error_code getOpenFileImpl(int FD, const char *Filename,
return error_code(errno, posix_category());
}
if (NumRead == 0) {
assert(IsVolatile && "We got inaccurate FileSize value or fstat reported "
"an invalid file size.");
assert(IsVolatileSize &&
"We got inaccurate FileSize value or fstat reported an invalid "
"file size.");
memset(BufPtr, 0, BytesLeft); // zero-initialize rest of the buffer.
break;
}
Expand All @@ -443,38 +444,38 @@ error_code MemoryBuffer::getOpenFile(int FD, const char *Filename,
std::unique_ptr<MemoryBuffer> &Result,
uint64_t FileSize,
bool RequiresNullTerminator,
bool IsVolatile) {
bool IsVolatileSize) {
return getOpenFileImpl(FD, Filename, Result, FileSize, FileSize, 0,
RequiresNullTerminator, IsVolatile);
RequiresNullTerminator, IsVolatileSize);
}

error_code MemoryBuffer::getOpenFile(int FD, const char *Filename,
OwningPtr<MemoryBuffer> &Result,
uint64_t FileSize,
bool RequiresNullTerminator,
bool IsVolatile) {
bool IsVolatileSize) {
std::unique_ptr<MemoryBuffer> MB;
error_code ec = getOpenFileImpl(FD, Filename, MB, FileSize, FileSize, 0,
RequiresNullTerminator, IsVolatile);
RequiresNullTerminator, IsVolatileSize);
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,
bool IsVolatile) {
bool IsVolatileSize) {
return getOpenFileImpl(FD, Filename, Result, -1, MapSize, Offset, false,
IsVolatile);
IsVolatileSize);
}

error_code MemoryBuffer::getOpenFileSlice(int FD, const char *Filename,
OwningPtr<MemoryBuffer> &Result,
uint64_t MapSize, int64_t Offset,
bool IsVolatile) {
bool IsVolatileSize) {
std::unique_ptr<MemoryBuffer> MB;
error_code ec = getOpenFileImpl(FD, Filename, MB, -1, MapSize, Offset, false,
IsVolatile);
IsVolatileSize);
Result = std::move(MB);
return ec;
}
Expand Down

0 comments on commit 10222d2

Please sign in to comment.