MemoryBuffer.h revision 4c842dda3939c6b9f83ba7e8e19e43445cd9a832
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/System/DataTypes.h" 19#include <string> 20#include <sys/stat.h> 21 22namespace llvm { 23 24/// MemoryBuffer - This interface provides simple read-only access to a block 25/// of memory, and provides simple methods for reading files and standard input 26/// into a memory buffer. In addition to basic access to the characters in the 27/// file, this interface guarantees you can read one character past the end of 28/// the file, and that this character will read as '\0'. 29class MemoryBuffer { 30 const char *BufferStart; // Start of the buffer. 31 const char *BufferEnd; // End of the buffer. 32 33 /// MustDeleteBuffer - True if we allocated this buffer. If so, the 34 /// destructor must know the delete[] it. 35 bool MustDeleteBuffer; 36protected: 37 MemoryBuffer() : MustDeleteBuffer(false) {} 38 void init(const char *BufStart, const char *BufEnd); 39 void initCopyOf(const char *BufStart, const char *BufEnd); 40public: 41 virtual ~MemoryBuffer(); 42 43 const char *getBufferStart() const { return BufferStart; } 44 const char *getBufferEnd() const { return BufferEnd; } 45 size_t getBufferSize() const { return BufferEnd-BufferStart; } 46 47 StringRef getBuffer() const { 48 return StringRef(BufferStart, getBufferSize()); 49 } 50 51 /// getBufferIdentifier - Return an identifier for this buffer, typically the 52 /// filename it was read from. 53 virtual const char *getBufferIdentifier() const { 54 return "Unknown buffer"; 55 } 56 57 /// getFile - Open the specified file as a MemoryBuffer, returning a new 58 /// MemoryBuffer if successful, otherwise returning null. If FileSize is 59 /// specified, this means that the client knows that the file exists and that 60 /// it has the specified size. 61 static MemoryBuffer *getFile(StringRef Filename, 62 std::string *ErrStr = 0, 63 int64_t FileSize = -1, 64 struct stat *FileInfo = 0); 65 66 /// getMemBuffer - Open the specified memory range as a MemoryBuffer. Note 67 /// that EndPtr[0] must be a null byte and be accessible! 68 static MemoryBuffer *getMemBuffer(StringRef InputData, 69 const char *BufferName = ""); 70 71 /// getMemBufferCopy - Open the specified memory range as a MemoryBuffer, 72 /// copying the contents and taking ownership of it. This has no requirements 73 /// on EndPtr[0]. 74 static MemoryBuffer *getMemBufferCopy(StringRef InputData, 75 const char *BufferName = ""); 76 77 /// getNewMemBuffer - Allocate a new MemoryBuffer of the specified size that 78 /// is completely initialized to zeros. Note that the caller should 79 /// initialize the memory allocated by this method. The memory is owned by 80 /// the MemoryBuffer object. 81 static MemoryBuffer *getNewMemBuffer(size_t Size, 82 const char *BufferName = ""); 83 84 /// getNewUninitMemBuffer - Allocate a new MemoryBuffer of the specified size 85 /// that is not initialized. Note that the caller should initialize the 86 /// memory allocated by this method. The memory is owned by the MemoryBuffer 87 /// object. 88 static MemoryBuffer *getNewUninitMemBuffer(size_t Size, 89 StringRef BufferName = ""); 90 91 /// getSTDIN - Read all of stdin into a file buffer, and return it. 92 static MemoryBuffer *getSTDIN(); 93 94 95 /// getFileOrSTDIN - Open the specified file as a MemoryBuffer, or open stdin 96 /// if the Filename is "-". If an error occurs, this returns null and fills 97 /// in *ErrStr with a reason. 98 static MemoryBuffer *getFileOrSTDIN(StringRef Filename, 99 std::string *ErrStr = 0, 100 int64_t FileSize = -1, 101 struct stat *FileInfo = 0); 102}; 103 104} // end namespace llvm 105 106#endif 107