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 const char *BufferStart; // Start of the buffer.
31 const char *BufferEnd; // End of the buffer.
33 /// MustDeleteBuffer - True if we allocated this buffer. If so, the
34 /// destructor must know the delete[] it.
35 bool MustDeleteBuffer;
37 MemoryBuffer() : MustDeleteBuffer(false) {}
38 void init(const char *BufStart, const char *BufEnd);
39 void initCopyOf(const char *BufStart, const char *BufEnd);
41 virtual ~MemoryBuffer();
43 const char *getBufferStart() const { return BufferStart; }
44 const char *getBufferEnd() const { return BufferEnd; }
45 size_t getBufferSize() const { return BufferEnd-BufferStart; }
47 StringRef getBuffer() const {
48 return StringRef(BufferStart, getBufferSize());
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";
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);
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 = "");
71 /// getMemBufferCopy - Open the specified memory range as a MemoryBuffer,
72 /// copying the contents and taking ownership of it. This has no requirements
74 static MemoryBuffer *getMemBufferCopy(StringRef InputData,
75 const char *BufferName = "");
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 = "");
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
88 static MemoryBuffer *getNewUninitMemBuffer(size_t Size,
89 StringRef BufferName = "");
91 /// getSTDIN - Read all of stdin into a file buffer, and return it.
92 static MemoryBuffer *getSTDIN();
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);
104 } // end namespace llvm