LLVM API Documentation

MemoryBuffer.cpp

Go to the documentation of this file.
00001 //===--- MemoryBuffer.cpp - Memory Buffer implementation ------------------===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 //  This file implements the MemoryBuffer interface.
00011 //
00012 //===----------------------------------------------------------------------===//
00013 
00014 #include "llvm/Support/MemoryBuffer.h"
00015 #include "llvm/ADT/OwningPtr.h"
00016 #include "llvm/ADT/SmallString.h"
00017 #include "llvm/System/Path.h"
00018 #include "llvm/System/Process.h"
00019 #include "llvm/System/Program.h"
00020 #include <cassert>
00021 #include <cstdio>
00022 #include <cstring>
00023 #include <cerrno>
00024 #include <sys/types.h>
00025 #include <sys/stat.h>
00026 #if !defined(_MSC_VER) && !defined(__MINGW32__)
00027 #include <unistd.h>
00028 #include <sys/uio.h>
00029 #else
00030 #include <io.h>
00031 #endif
00032 #include <fcntl.h>
00033 using namespace llvm;
00034 
00035 //===----------------------------------------------------------------------===//
00036 // MemoryBuffer implementation itself.
00037 //===----------------------------------------------------------------------===//
00038 
00039 MemoryBuffer::~MemoryBuffer() {
00040   if (MustDeleteBuffer)
00041     free((void*)BufferStart);
00042 }
00043 
00044 /// initCopyOf - Initialize this source buffer with a copy of the specified
00045 /// memory range.  We make the copy so that we can null terminate it
00046 /// successfully.
00047 void MemoryBuffer::initCopyOf(const char *BufStart, const char *BufEnd) {
00048   size_t Size = BufEnd-BufStart;
00049   BufferStart = (char *)malloc((Size+1) * sizeof(char));
00050   BufferEnd = BufferStart+Size;
00051   memcpy(const_cast<char*>(BufferStart), BufStart, Size);
00052   *const_cast<char*>(BufferEnd) = 0;   // Null terminate buffer.
00053   MustDeleteBuffer = true;
00054 }
00055 
00056 /// init - Initialize this MemoryBuffer as a reference to externally allocated
00057 /// memory, memory that we know is already null terminated.
00058 void MemoryBuffer::init(const char *BufStart, const char *BufEnd) {
00059   assert(BufEnd[0] == 0 && "Buffer is not null terminated!");
00060   BufferStart = BufStart;
00061   BufferEnd = BufEnd;
00062   MustDeleteBuffer = false;
00063 }
00064 
00065 //===----------------------------------------------------------------------===//
00066 // MemoryBufferMem implementation.
00067 //===----------------------------------------------------------------------===//
00068 
00069 namespace {
00070 class MemoryBufferMem : public MemoryBuffer {
00071   std::string FileID;
00072 public:
00073   MemoryBufferMem(const char *Start, const char *End, const char *FID,
00074                   bool Copy = false)
00075   : FileID(FID) {
00076     if (!Copy)
00077       init(Start, End);
00078     else
00079       initCopyOf(Start, End);
00080   }
00081   
00082   virtual const char *getBufferIdentifier() const {
00083     return FileID.c_str();
00084   }
00085 };
00086 }
00087 
00088 /// getMemBuffer - Open the specified memory range as a MemoryBuffer.  Note
00089 /// that EndPtr[0] must be a null byte and be accessible!
00090 MemoryBuffer *MemoryBuffer::getMemBuffer(const char *StartPtr, 
00091                                          const char *EndPtr,
00092                                          const char *BufferName) {
00093   return new MemoryBufferMem(StartPtr, EndPtr, BufferName);
00094 }
00095 
00096 /// getMemBufferCopy - Open the specified memory range as a MemoryBuffer,
00097 /// copying the contents and taking ownership of it.  This has no requirements
00098 /// on EndPtr[0].
00099 MemoryBuffer *MemoryBuffer::getMemBufferCopy(const char *StartPtr, 
00100                                              const char *EndPtr,
00101                                              const char *BufferName) {
00102   return new MemoryBufferMem(StartPtr, EndPtr, BufferName, true);
00103 }
00104 
00105 /// getNewUninitMemBuffer - Allocate a new MemoryBuffer of the specified size
00106 /// that is completely initialized to zeros.  Note that the caller should
00107 /// initialize the memory allocated by this method.  The memory is owned by
00108 /// the MemoryBuffer object.
00109 MemoryBuffer *MemoryBuffer::getNewUninitMemBuffer(size_t Size,
00110                                                   const char *BufferName) {
00111   char *Buf = (char *)malloc((Size+1) * sizeof(char));
00112   if (!Buf) return 0;
00113   Buf[Size] = 0;
00114   MemoryBufferMem *SB = new MemoryBufferMem(Buf, Buf+Size, BufferName);
00115   // The memory for this buffer is owned by the MemoryBuffer.
00116   SB->MustDeleteBuffer = true;
00117   return SB;
00118 }
00119 
00120 /// getNewMemBuffer - Allocate a new MemoryBuffer of the specified size that
00121 /// is completely initialized to zeros.  Note that the caller should
00122 /// initialize the memory allocated by this method.  The memory is owned by
00123 /// the MemoryBuffer object.
00124 MemoryBuffer *MemoryBuffer::getNewMemBuffer(size_t Size,
00125                                             const char *BufferName) {
00126   MemoryBuffer *SB = getNewUninitMemBuffer(Size, BufferName);
00127   if (!SB) return 0;
00128   memset(const_cast<char*>(SB->getBufferStart()), 0, Size+1);
00129   return SB;
00130 }
00131 
00132 
00133 /// getFileOrSTDIN - Open the specified file as a MemoryBuffer, or open stdin
00134 /// if the Filename is "-".  If an error occurs, this returns null and fills
00135 /// in *ErrStr with a reason.  If stdin is empty, this API (unlike getSTDIN)
00136 /// returns an empty buffer.
00137 MemoryBuffer *MemoryBuffer::getFileOrSTDIN(const char *Filename,
00138                                            std::string *ErrStr,
00139                                            int64_t FileSize) {
00140   if (Filename[0] != '-' || Filename[1] != 0)
00141     return getFile(Filename, ErrStr, FileSize);
00142   MemoryBuffer *M = getSTDIN();
00143   if (M) return M;
00144 
00145   // If stdin was empty, M is null.  Cons up an empty memory buffer now.
00146   const char *EmptyStr = "";
00147   return MemoryBuffer::getMemBuffer(EmptyStr, EmptyStr, "<stdin>");
00148 }
00149 
00150 //===----------------------------------------------------------------------===//
00151 // MemoryBuffer::getFile implementation.
00152 //===----------------------------------------------------------------------===//
00153 
00154 namespace {
00155 /// MemoryBufferMMapFile - This represents a file that was mapped in with the
00156 /// sys::Path::MapInFilePages method.  When destroyed, it calls the
00157 /// sys::Path::UnMapFilePages method.
00158 class MemoryBufferMMapFile : public MemoryBuffer {
00159   std::string Filename;
00160 public:
00161   MemoryBufferMMapFile(const char *filename, const char *Pages, uint64_t Size)
00162     : Filename(filename) {
00163     init(Pages, Pages+Size);
00164   }
00165   
00166   virtual const char *getBufferIdentifier() const {
00167     return Filename.c_str();
00168   }
00169     
00170   ~MemoryBufferMMapFile() {
00171     sys::Path::UnMapFilePages(getBufferStart(), getBufferSize());
00172   }
00173 };
00174 }
00175 
00176 MemoryBuffer *MemoryBuffer::getFile(const char *Filename, std::string *ErrStr,
00177                                     int64_t FileSize) {
00178   int OpenFlags = 0;
00179 #ifdef O_BINARY
00180   OpenFlags |= O_BINARY;  // Open input file in binary mode on win32.
00181 #endif
00182   int FD = ::open(Filename, O_RDONLY|OpenFlags);
00183   if (FD == -1) {
00184     if (ErrStr) *ErrStr = "could not open file";
00185     return 0;
00186   }
00187   
00188   // If we don't know the file size, use fstat to find out.  fstat on an open
00189   // file descriptor is cheaper than stat on a random path.
00190   if (FileSize == -1) {
00191     struct stat FileInfo;
00192     // TODO: This should use fstat64 when available.
00193     if (fstat(FD, &FileInfo) == -1) {
00194       if (ErrStr) *ErrStr = "could not get file length";
00195       ::close(FD);
00196       return 0;
00197     }
00198     FileSize = FileInfo.st_size;
00199   }
00200   
00201   
00202   // If the file is large, try to use mmap to read it in.  We don't use mmap
00203   // for small files, because this can severely fragment our address space. Also
00204   // don't try to map files that are exactly a multiple of the system page size,
00205   // as the file would not have the required null terminator.
00206   if (FileSize >= 4096*4 &&
00207       (FileSize & (sys::Process::GetPageSize()-1)) != 0) {
00208     if (const char *Pages = sys::Path::MapInFilePages(FD, FileSize)) {
00209       // Close the file descriptor, now that the whole file is in memory.
00210       ::close(FD);
00211       return new MemoryBufferMMapFile(Filename, Pages, FileSize);
00212     }
00213   }
00214 
00215   MemoryBuffer *Buf = MemoryBuffer::getNewUninitMemBuffer(FileSize, Filename);
00216   if (!Buf) {
00217     // Failed to create a buffer.
00218     if (ErrStr) *ErrStr = "could not allocate buffer";
00219     ::close(FD);
00220     return 0;
00221   }
00222 
00223   OwningPtr<MemoryBuffer> SB(Buf);
00224   char *BufPtr = const_cast<char*>(SB->getBufferStart());
00225   
00226   size_t BytesLeft = FileSize;
00227   while (BytesLeft) {
00228     ssize_t NumRead = ::read(FD, BufPtr, BytesLeft);
00229     if (NumRead != -1) {
00230       BytesLeft -= NumRead;
00231       BufPtr += NumRead;
00232     } else if (errno == EINTR) {
00233       // try again
00234     } else {
00235       // error reading.
00236       close(FD);
00237       if (ErrStr) *ErrStr = "error reading file data";
00238       return 0;
00239     }
00240   }
00241   close(FD);
00242   
00243   return SB.take();
00244 }
00245 
00246 //===----------------------------------------------------------------------===//
00247 // MemoryBuffer::getSTDIN implementation.
00248 //===----------------------------------------------------------------------===//
00249 
00250 namespace {
00251 class STDINBufferFile : public MemoryBuffer {
00252 public:
00253   virtual const char *getBufferIdentifier() const {
00254     return "<stdin>";
00255   }
00256 };
00257 }
00258 
00259 MemoryBuffer *MemoryBuffer::getSTDIN() {
00260   char Buffer[4096*4];
00261 
00262   std::vector<char> FileData;
00263 
00264   // Read in all of the data from stdin, we cannot mmap stdin.
00265   sys::Program::ChangeStdinToBinary();
00266   size_t ReadBytes;
00267   do {
00268     ReadBytes = fread(Buffer, sizeof(char), sizeof(Buffer), stdin);
00269     FileData.insert(FileData.end(), Buffer, Buffer+ReadBytes);
00270   } while (ReadBytes == sizeof(Buffer));
00271 
00272   FileData.push_back(0); // &FileData[Size] is invalid. So is &*FileData.end().
00273   size_t Size = FileData.size();
00274   if (Size <= 1)
00275     return 0;
00276   MemoryBuffer *B = new STDINBufferFile();
00277   B->initCopyOf(&FileData[0], &FileData[Size-1]);
00278   return B;
00279 }