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/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 size_t 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 *Filename,
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 /// getMemBufferCopy - Open the specified memory range as a MemoryBuffer,
65 /// copying the contents and taking ownership of it. This has no requirements
67 static MemoryBuffer *getMemBufferCopy(const char *StartPtr,const char *EndPtr,
68 const char *BufferName = "");
70 /// getNewMemBuffer - Allocate a new MemoryBuffer of the specified size that
71 /// is completely initialized to zeros. Note that the caller should
72 /// initialize the memory allocated by this method. The memory is owned by
73 /// the MemoryBuffer object.
74 static MemoryBuffer *getNewMemBuffer(size_t Size,
75 const char *BufferName = "");
77 /// getNewUninitMemBuffer - Allocate a new MemoryBuffer of the specified size
78 /// that is not initialized. Note that the caller should initialize the
79 /// memory allocated by this method. The memory is owned by the MemoryBuffer
81 static MemoryBuffer *getNewUninitMemBuffer(size_t Size,
82 const char *BufferName = "");
84 /// getSTDIN - Read all of stdin into a file buffer, and return it. This
85 /// returns null if stdin is empty.
86 static MemoryBuffer *getSTDIN();
89 /// getFileOrSTDIN - Open the specified file as a MemoryBuffer, or open stdin
90 /// if the Filename is "-". If an error occurs, this returns null and fills
91 /// in *ErrStr with a reason. If stdin is empty, this API (unlike getSTDIN)
92 /// returns an empty buffer.
93 static MemoryBuffer *getFileOrSTDIN(const char *Filename,
94 std::string *ErrStr = 0,
95 int64_t FileSize = -1);
97 /// getFileOrSTDIN - Open the specified file as a MemoryBuffer, or open stdin
98 /// if the Filename is "-". If an error occurs, this returns null and fills
99 /// in *ErrStr with a reason.
100 static MemoryBuffer *getFileOrSTDIN(const std::string &FN,
101 std::string *ErrStr = 0,
102 int64_t FileSize = -1) {
103 return getFileOrSTDIN(FN.c_str(), ErrStr, FileSize);
107 } // end namespace llvm