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