[llvm-commits] [llvm] r120065 - in /llvm/trunk: include/llvm/Support/MemoryBuffer.h lib/Support/MemoryBuffer.cpp

Chris Lattner sabre at nondot.org
Tue Nov 23 14:20:27 PST 2010


Author: lattner
Date: Tue Nov 23 16:20:27 2010
New Revision: 120065

URL: http://llvm.org/viewvc/llvm-project?rev=120065&view=rev
Log:
add a MemoryBuffer::getOpenFile method, which turns an open
file descriptor into a MemoryBuffer (and closes the FD).

Modified:
    llvm/trunk/include/llvm/Support/MemoryBuffer.h
    llvm/trunk/lib/Support/MemoryBuffer.cpp

Modified: llvm/trunk/include/llvm/Support/MemoryBuffer.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Support/MemoryBuffer.h?rev=120065&r1=120064&r2=120065&view=diff
==============================================================================
--- llvm/trunk/include/llvm/Support/MemoryBuffer.h (original)
+++ llvm/trunk/include/llvm/Support/MemoryBuffer.h Tue Nov 23 16:20:27 2010
@@ -60,13 +60,18 @@
   /// 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,

Modified: llvm/trunk/lib/Support/MemoryBuffer.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Support/MemoryBuffer.cpp?rev=120065&r1=120064&r2=120065&view=diff
==============================================================================
--- llvm/trunk/lib/Support/MemoryBuffer.cpp (original)
+++ llvm/trunk/lib/Support/MemoryBuffer.cpp Tue Nov 23 16:20:27 2010
@@ -187,6 +187,7 @@
 
 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 @@
     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





More information about the llvm-commits mailing list