Home | History | Annotate | Download | only in Support
      1 //===--- MemoryBuffer.h - Memory Buffer Interface ---------------*- C++ -*-===//
      2 //
      3 //                     The LLVM Compiler Infrastructure
      4 //
      5 // This file is distributed under the University of Illinois Open Source
      6 // License. See LICENSE.TXT for details.
      7 //
      8 //===----------------------------------------------------------------------===//
      9 //
     10 //  This file defines the MemoryBuffer interface.
     11 //
     12 //===----------------------------------------------------------------------===//
     13 
     14 #ifndef LLVM_SUPPORT_MEMORYBUFFER_H
     15 #define LLVM_SUPPORT_MEMORYBUFFER_H
     16 
     17 #include "llvm/ADT/StringRef.h"
     18 #include "llvm/Support/Compiler.h"
     19 #include "llvm/Support/DataTypes.h"
     20 
     21 namespace llvm {
     22 
     23 class error_code;
     24 template<class T> class OwningPtr;
     25 
     26 /// MemoryBuffer - This interface provides simple read-only access to a block
     27 /// of memory, and provides simple methods for reading files and standard input
     28 /// into a memory buffer.  In addition to basic access to the characters in the
     29 /// file, this interface guarantees you can read one character past the end of
     30 /// the file, and that this character will read as '\0'.
     31 ///
     32 /// The '\0' guarantee is needed to support an optimization -- it's intended to
     33 /// be more efficient for clients which are reading all the data to stop
     34 /// reading when they encounter a '\0' than to continually check the file
     35 /// position to see if it has reached the end of the file.
     36 class MemoryBuffer {
     37   const char *BufferStart; // Start of the buffer.
     38   const char *BufferEnd;   // End of the buffer.
     39 
     40   MemoryBuffer(const MemoryBuffer &) LLVM_DELETED_FUNCTION;
     41   MemoryBuffer &operator=(const MemoryBuffer &) LLVM_DELETED_FUNCTION;
     42 protected:
     43   MemoryBuffer() {}
     44   void init(const char *BufStart, const char *BufEnd,
     45             bool RequiresNullTerminator);
     46 public:
     47   virtual ~MemoryBuffer();
     48 
     49   const char *getBufferStart() const { return BufferStart; }
     50   const char *getBufferEnd() const   { return BufferEnd; }
     51   size_t getBufferSize() const { return BufferEnd-BufferStart; }
     52 
     53   StringRef getBuffer() const {
     54     return StringRef(BufferStart, getBufferSize());
     55   }
     56 
     57   /// getBufferIdentifier - Return an identifier for this buffer, typically the
     58   /// filename it was read from.
     59   virtual const char *getBufferIdentifier() const {
     60     return "Unknown buffer";
     61   }
     62 
     63   /// getFile - Open the specified file as a MemoryBuffer, returning a new
     64   /// MemoryBuffer if successful, otherwise returning null.  If FileSize is
     65   /// specified, this means that the client knows that the file exists and that
     66   /// it has the specified size.
     67   static error_code getFile(StringRef Filename, OwningPtr<MemoryBuffer> &result,
     68                             int64_t FileSize = -1,
     69                             bool RequiresNullTerminator = true);
     70   static error_code getFile(const char *Filename,
     71                             OwningPtr<MemoryBuffer> &result,
     72                             int64_t FileSize = -1,
     73                             bool RequiresNullTerminator = true);
     74 
     75   /// getOpenFile - Given an already-open file descriptor, read the file and
     76   /// return a MemoryBuffer.
     77   static error_code getOpenFile(int FD, const char *Filename,
     78                                 OwningPtr<MemoryBuffer> &result,
     79                                 uint64_t FileSize = -1,
     80                                 uint64_t MapSize = -1,
     81                                 int64_t Offset = 0,
     82                                 bool RequiresNullTerminator = true);
     83 
     84   /// getMemBuffer - Open the specified memory range as a MemoryBuffer.  Note
     85   /// that InputData must be null terminated if RequiresNullTerminator is true.
     86   static MemoryBuffer *getMemBuffer(StringRef InputData,
     87                                     StringRef BufferName = "",
     88                                     bool RequiresNullTerminator = true);
     89 
     90   /// getMemBufferCopy - Open the specified memory range as a MemoryBuffer,
     91   /// copying the contents and taking ownership of it.  InputData does not
     92   /// have to be null terminated.
     93   static MemoryBuffer *getMemBufferCopy(StringRef InputData,
     94                                         StringRef BufferName = "");
     95 
     96   /// getNewMemBuffer - Allocate a new MemoryBuffer of the specified size that
     97   /// is completely initialized to zeros.  Note that the caller should
     98   /// initialize the memory allocated by this method.  The memory is owned by
     99   /// the MemoryBuffer object.
    100   static MemoryBuffer *getNewMemBuffer(size_t Size, StringRef BufferName = "");
    101 
    102   /// getNewUninitMemBuffer - Allocate a new MemoryBuffer of the specified size
    103   /// that is not initialized.  Note that the caller should initialize the
    104   /// memory allocated by this method.  The memory is owned by the MemoryBuffer
    105   /// object.
    106   static MemoryBuffer *getNewUninitMemBuffer(size_t Size,
    107                                              StringRef BufferName = "");
    108 
    109   /// getSTDIN - Read all of stdin into a file buffer, and return it.
    110   /// If an error occurs, this returns null and sets ec.
    111   static error_code getSTDIN(OwningPtr<MemoryBuffer> &result);
    112 
    113 
    114   /// getFileOrSTDIN - Open the specified file as a MemoryBuffer, or open stdin
    115   /// if the Filename is "-".  If an error occurs, this returns null and sets
    116   /// ec.
    117   static error_code getFileOrSTDIN(StringRef Filename,
    118                                    OwningPtr<MemoryBuffer> &result,
    119                                    int64_t FileSize = -1);
    120   static error_code getFileOrSTDIN(const char *Filename,
    121                                    OwningPtr<MemoryBuffer> &result,
    122                                    int64_t FileSize = -1);
    123 
    124 
    125   //===--------------------------------------------------------------------===//
    126   // Provided for performance analysis.
    127   //===--------------------------------------------------------------------===//
    128 
    129   /// The kind of memory backing used to support the MemoryBuffer.
    130   enum BufferKind {
    131     MemoryBuffer_Malloc,
    132     MemoryBuffer_MMap
    133   };
    134 
    135   /// Return information on the memory mechanism used to support the
    136   /// MemoryBuffer.
    137   virtual BufferKind getBufferKind() const = 0;
    138 };
    139 
    140 } // end namespace llvm
    141 
    142 #endif
    143