LLVM API Documentation

MemoryBuffer.h

Go to the documentation of this file.
00001 //===--- MemoryBuffer.h - Memory Buffer Interface ---------------*- C++ -*-===//
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 defines the MemoryBuffer interface.
00011 //
00012 //===----------------------------------------------------------------------===//
00013 
00014 #ifndef LLVM_SUPPORT_MEMORYBUFFER_H
00015 #define LLVM_SUPPORT_MEMORYBUFFER_H
00016 
00017 #include "llvm/Support/DataTypes.h"
00018 #include <string>
00019 
00020 namespace llvm {
00021 
00022 /// MemoryBuffer - This interface provides simple read-only access to a block
00023 /// of memory, and provides simple methods for reading files and standard input
00024 /// into a memory buffer.  In addition to basic access to the characters in the
00025 /// file, this interface guarantees you can read one character past the end of
00026 /// @verbatim the file, and that this character will read as '\0'. @endverbatim
00027 class MemoryBuffer {
00028   const char *BufferStart; // Start of the buffer.
00029   const char *BufferEnd;   // End of the buffer.
00030 
00031   /// MustDeleteBuffer - True if we allocated this buffer.  If so, the
00032   /// destructor must know the delete[] it.
00033   bool MustDeleteBuffer;
00034 protected:
00035   MemoryBuffer() : MustDeleteBuffer(false) {}
00036   void init(const char *BufStart, const char *BufEnd);
00037   void initCopyOf(const char *BufStart, const char *BufEnd);
00038 public:
00039   virtual ~MemoryBuffer();
00040   
00041   const char *getBufferStart() const { return BufferStart; }
00042   const char *getBufferEnd() const   { return BufferEnd; }
00043   size_t getBufferSize() const { return BufferEnd-BufferStart; }
00044   
00045   /// getBufferIdentifier - Return an identifier for this buffer, typically the
00046   /// filename it was read from.
00047   virtual const char *getBufferIdentifier() const {
00048     return "Unknown buffer";
00049   }
00050 
00051   /// getFile - Open the specified file as a MemoryBuffer, returning a new
00052   /// MemoryBuffer if successful, otherwise returning null.  If FileSize is
00053   /// specified, this means that the client knows that the file exists and that
00054   /// it has the specified size.
00055   static MemoryBuffer *getFile(const char *Filename,
00056                                std::string *ErrStr = 0,
00057                                int64_t FileSize = -1);
00058 
00059   /// getMemBuffer - Open the specified memory range as a MemoryBuffer.  Note
00060   /// that EndPtr[0] must be a null byte and be accessible!
00061   static MemoryBuffer *getMemBuffer(const char *StartPtr, const char *EndPtr,
00062                                     const char *BufferName = "");
00063   
00064   /// getMemBufferCopy - Open the specified memory range as a MemoryBuffer,
00065   /// copying the contents and taking ownership of it.  This has no requirements
00066   /// on EndPtr[0].
00067   static MemoryBuffer *getMemBufferCopy(const char *StartPtr,const char *EndPtr,
00068                                         const char *BufferName = "");
00069   
00070   /// getNewMemBuffer - Allocate a new MemoryBuffer of the specified size that
00071   /// is completely initialized to zeros.  Note that the caller should
00072   /// initialize the memory allocated by this method.  The memory is owned by
00073   /// the MemoryBuffer object.
00074   static MemoryBuffer *getNewMemBuffer(size_t Size,
00075                                        const char *BufferName = "");
00076   
00077   /// getNewUninitMemBuffer - Allocate a new MemoryBuffer of the specified size
00078   /// that is not initialized.  Note that the caller should initialize the
00079   /// memory allocated by this method.  The memory is owned by the MemoryBuffer
00080   /// object.
00081   static MemoryBuffer *getNewUninitMemBuffer(size_t Size,
00082                                              const char *BufferName = "");
00083   
00084   /// getSTDIN - Read all of stdin into a file buffer, and return it.  This
00085   /// returns null if stdin is empty.
00086   static MemoryBuffer *getSTDIN();
00087   
00088   
00089   /// getFileOrSTDIN - Open the specified file as a MemoryBuffer, or open stdin
00090   /// if the Filename is "-".  If an error occurs, this returns null and fills
00091   /// in *ErrStr with a reason.  If stdin is empty, this API (unlike getSTDIN)
00092   /// returns an empty buffer.
00093   static MemoryBuffer *getFileOrSTDIN(const char *Filename,
00094                                       std::string *ErrStr = 0,
00095                                       int64_t FileSize = -1);
00096   
00097   /// getFileOrSTDIN - Open the specified file as a MemoryBuffer, or open stdin
00098   /// if the Filename is "-".  If an error occurs, this returns null and fills
00099   /// in *ErrStr with a reason.
00100   static MemoryBuffer *getFileOrSTDIN(const std::string &FN,
00101                                       std::string *ErrStr = 0,
00102                                       int64_t FileSize = -1) {
00103     return getFileOrSTDIN(FN.c_str(), ErrStr, FileSize);
00104   }
00105 };
00106 
00107 } // end namespace llvm
00108 
00109 #endif