1 //===--- MemoryBuffer.h - Memory Buffer Interface ---------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by Chris Lattner and is distributed under
6 // the University of Illinois Open Source 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/Support/DataTypes.h"
22 /// MemoryBuffer - This interface provides simple read-only access to a block
23 /// of memory, and provides simple methods for reading files and standard input
24 /// into a memory buffer. In addition to basic access to the characters in the
25 /// file, this interface guarantees you can read one character past the end of
26 /// @verbatim the file, and that this character will read as '\0'. @endverbatim
28 const char *BufferStart; // Start of the buffer.
29 const char *BufferEnd; // End of the buffer.
31 /// MustDeleteBuffer - True if we allocated this buffer. If so, the
32 /// destructor must know the delete[] it.
33 bool MustDeleteBuffer;
35 MemoryBuffer() : MustDeleteBuffer(false) {}
36 void init(const char *BufStart, const char *BufEnd);
37 void initCopyOf(const char *BufStart, const char *BufEnd);
39 virtual ~MemoryBuffer();
41 const char *getBufferStart() const { return BufferStart; }
42 const char *getBufferEnd() const { return BufferEnd; }
43 unsigned getBufferSize() const { return BufferEnd-BufferStart; }
45 /// getBufferIdentifier - Return an identifier for this buffer, typically the
46 /// filename it was read from.
47 virtual const char *getBufferIdentifier() const {
48 return "Unknown buffer";
51 /// getFile - Open the specified file as a MemoryBuffer, returning a new
52 /// MemoryBuffer if successful, otherwise returning null. If FileSize is
53 /// specified, this means that the client knows that the file exists and that
54 /// it has the specified size.
55 static MemoryBuffer *getFile(const char *FilenameStart, unsigned FnSize,
56 std::string *ErrStr = 0,
57 int64_t FileSize = -1);
59 /// getMemBuffer - Open the specified memory range as a MemoryBuffer. Note
60 /// that EndPtr[0] must be a null byte and be accessible!
61 static MemoryBuffer *getMemBuffer(const char *StartPtr, const char *EndPtr,
62 const char *BufferName = "");
64 /// getNewMemBuffer - Allocate a new MemoryBuffer of the specified size that
65 /// is completely initialized to zeros. Note that the caller should
66 /// initialize the memory allocated by this method. The memory is owned by
67 /// the MemoryBuffer object.
68 static MemoryBuffer *getNewMemBuffer(unsigned Size,
69 const char *BufferName = "");
71 /// getNewUninitMemBuffer - Allocate a new MemoryBuffer of the specified size
72 /// that is not initialized. Note that the caller should initialize the
73 /// memory allocated by this method. The memory is owned by the MemoryBuffer
75 static MemoryBuffer *getNewUninitMemBuffer(unsigned Size,
76 const char *BufferName = "");
78 /// getSTDIN - Read all of stdin into a file buffer, and return it. This
79 /// returns null if stdin is empty.
80 static MemoryBuffer *getSTDIN();
83 /// getFileOrSTDIN - Open the specified file as a MemoryBuffer, or open stdin
84 /// if the Filename is "-". If an error occurs, this returns null and fills
85 /// in *ErrStr with a reason.
86 static MemoryBuffer *getFileOrSTDIN(const char *FilenameStart,unsigned FnSize,
87 std::string *ErrStr = 0,
88 int64_t FileSize = -1) {
89 if (FnSize == 1 && FilenameStart[0] == '-')
91 return getFile(FilenameStart, FnSize, ErrStr, FileSize);
94 /// getFileOrSTDIN - Open the specified file as a MemoryBuffer, or open stdin
95 /// if the Filename is "-". If an error occurs, this returns null and fills
96 /// in *ErrStr with a reason.
97 static MemoryBuffer *getFileOrSTDIN(const std::string &FN,
98 std::string *ErrStr = 0,
99 int64_t FileSize = -1) {
100 return getFileOrSTDIN(&FN[0], FN.size(), ErrStr, FileSize);
104 } // end namespace llvm