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