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