1 //===--- MemoryBuffer.h - Memory Buffer Interface ---------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the MemoryBuffer interface.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_SUPPORT_MEMORYBUFFER_H
15 #define LLVM_SUPPORT_MEMORYBUFFER_H
17 #include "llvm/ADT/StringRef.h"
18 #include "llvm/System/DataTypes.h"
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'.
30 /// The '\0' guarantee is needed to support an optimization -- it's intended to
31 /// be more efficient for clients which are reading all the data to stop
32 /// reading when they encounter a '\0' than to continually check the file
33 /// position to see if it has reached the end of the file.
35 const char *BufferStart; // Start of the buffer.
36 const char *BufferEnd; // End of the buffer.
38 /// MustDeleteBuffer - True if we allocated this buffer. If so, the
39 /// destructor must know the delete[] it.
40 bool MustDeleteBuffer;
42 MemoryBuffer() : MustDeleteBuffer(false) {}
43 void init(const char *BufStart, const char *BufEnd);
44 void initCopyOf(const char *BufStart, const char *BufEnd);
46 virtual ~MemoryBuffer();
48 const char *getBufferStart() const { return BufferStart; }
49 const char *getBufferEnd() const { return BufferEnd; }
50 size_t getBufferSize() const { return BufferEnd-BufferStart; }
52 StringRef getBuffer() const {
53 return StringRef(BufferStart, getBufferSize());
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";
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 MemoryBuffer *getFile(StringRef Filename,
67 std::string *ErrStr = 0,
68 int64_t FileSize = -1,
69 struct stat *FileInfo = 0);
71 /// getMemBuffer - Open the specified memory range as a MemoryBuffer. Note
72 /// that EndPtr[0] must be a null byte and be accessible!
73 static MemoryBuffer *getMemBuffer(StringRef InputData,
74 const char *BufferName = "");
76 /// getMemBufferCopy - Open the specified memory range as a MemoryBuffer,
77 /// copying the contents and taking ownership of it. This has no requirements
79 static MemoryBuffer *getMemBufferCopy(StringRef InputData,
80 const char *BufferName = "");
82 /// getNewMemBuffer - Allocate a new MemoryBuffer of the specified size that
83 /// is completely initialized to zeros. Note that the caller should
84 /// initialize the memory allocated by this method. The memory is owned by
85 /// the MemoryBuffer object.
86 static MemoryBuffer *getNewMemBuffer(size_t Size,
87 const char *BufferName = "");
89 /// getNewUninitMemBuffer - Allocate a new MemoryBuffer of the specified size
90 /// that is not initialized. Note that the caller should initialize the
91 /// memory allocated by this method. The memory is owned by the MemoryBuffer
93 static MemoryBuffer *getNewUninitMemBuffer(size_t Size,
94 StringRef BufferName = "");
96 /// getSTDIN - Read all of stdin into a file buffer, and return it.
97 /// If an error occurs, this returns null and fills in *ErrStr with a reason.
98 static MemoryBuffer *getSTDIN(std::string *ErrStr = 0);
101 /// getFileOrSTDIN - Open the specified file as a MemoryBuffer, or open stdin
102 /// if the Filename is "-". If an error occurs, this returns null and fills
103 /// in *ErrStr with a reason.
104 static MemoryBuffer *getFileOrSTDIN(StringRef Filename,
105 std::string *ErrStr = 0,
106 int64_t FileSize = -1,
107 struct stat *FileInfo = 0);
110 } // end namespace llvm