summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorChris Lattner <sabre@nondot.org>2010-11-23 22:20:27 +0000
committerChris Lattner <sabre@nondot.org>2010-11-23 22:20:27 +0000
commit6bf4e6d8b20d25d9263d61d5f9ae1967806a0807 (patch)
tree167cb7e256d4703031d38b6867770573da11fe8d
parente78881314a3b915bce37917bb954a98b9afdf671 (diff)
downloadbcm5719-llvm-6bf4e6d8b20d25d9263d61d5f9ae1967806a0807.tar.gz
bcm5719-llvm-6bf4e6d8b20d25d9263d61d5f9ae1967806a0807.zip
add a MemoryBuffer::getOpenFile method, which turns an open
file descriptor into a MemoryBuffer (and closes the FD). llvm-svn: 120065
-rw-r--r--llvm/include/llvm/Support/MemoryBuffer.h13
-rw-r--r--llvm/lib/Support/MemoryBuffer.cpp7
2 files changed, 16 insertions, 4 deletions
diff --git a/llvm/include/llvm/Support/MemoryBuffer.h b/llvm/include/llvm/Support/MemoryBuffer.h
index 41d4f88bfa8..f29ddd1feaf 100644
--- a/llvm/include/llvm/Support/MemoryBuffer.h
+++ b/llvm/include/llvm/Support/MemoryBuffer.h
@@ -60,13 +60,18 @@ 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.
- static MemoryBuffer *getFile(StringRef Filename,
- std::string *ErrStr = 0,
+ static MemoryBuffer *getFile(StringRef Filename, std::string *ErrStr = 0,
int64_t FileSize = -1);
- static MemoryBuffer *getFile(const char *Filename,
- std::string *ErrStr = 0,
+ static MemoryBuffer *getFile(const char *Filename, std::string *ErrStr = 0,
int64_t FileSize = -1);
+ /// getOpenFile - Given an already-open file descriptor, read the file and
+ /// return a MemoryBuffer. This takes ownership of the descriptor,
+ /// immediately closing it after reading the file.
+ static MemoryBuffer *getOpenFile(int FD, const char *Filename,
+ std::string *ErrStr = 0,
+ int64_t FileSize = -1);
+
/// getMemBuffer - Open the specified memory range as a MemoryBuffer. Note
/// that InputData must be null terminated.
static MemoryBuffer *getMemBuffer(StringRef InputData,
diff --git a/llvm/lib/Support/MemoryBuffer.cpp b/llvm/lib/Support/MemoryBuffer.cpp
index da4685309d5..5b701a5c607 100644
--- a/llvm/lib/Support/MemoryBuffer.cpp
+++ b/llvm/lib/Support/MemoryBuffer.cpp
@@ -187,6 +187,7 @@ public:
MemoryBuffer *MemoryBuffer::getFile(StringRef Filename, std::string *ErrStr,
int64_t FileSize) {
+ // Ensure the path is null terminated.
SmallString<256> PathBuf(Filename.begin(), Filename.end());
return MemoryBuffer::getFile(PathBuf.c_str(), ErrStr, FileSize);
}
@@ -202,6 +203,12 @@ MemoryBuffer *MemoryBuffer::getFile(const char *Filename, std::string *ErrStr,
if (ErrStr) *ErrStr = sys::StrError();
return 0;
}
+
+ return getOpenFile(FD, Filename, ErrStr, FileSize);
+}
+
+MemoryBuffer *MemoryBuffer::getOpenFile(int FD, const char *Filename,
+ std::string *ErrStr, int64_t FileSize) {
FileCloser FC(FD); // Close FD on return.
// If we don't know the file size, use fstat to find out. fstat on an open
OpenPOWER on IntegriCloud