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-c/Support.h"
18 #include "llvm/ADT/Twine.h"
19 #include "llvm/Support/CBindingWrapping.h"
20 #include "llvm/Support/Compiler.h"
21 #include "llvm/Support/DataTypes.h"
27 template<class T> class OwningPtr;
29 /// MemoryBuffer - This interface provides simple read-only access to a block
30 /// of memory, and provides simple methods for reading files and standard input
31 /// into a memory buffer. In addition to basic access to the characters in the
32 /// file, this interface guarantees you can read one character past the end of
33 /// the file, and that this character will read as '\0'.
35 /// The '\0' guarantee is needed to support an optimization -- it's intended to
36 /// be more efficient for clients which are reading all the data to stop
37 /// reading when they encounter a '\0' than to continually check the file
38 /// position to see if it has reached the end of the file.
40 const char *BufferStart; // Start of the buffer.
41 const char *BufferEnd; // End of the buffer.
43 MemoryBuffer(const MemoryBuffer &) LLVM_DELETED_FUNCTION;
44 MemoryBuffer &operator=(const MemoryBuffer &) LLVM_DELETED_FUNCTION;
47 void init(const char *BufStart, const char *BufEnd,
48 bool RequiresNullTerminator);
50 virtual ~MemoryBuffer();
52 const char *getBufferStart() const { return BufferStart; }
53 const char *getBufferEnd() const { return BufferEnd; }
54 size_t getBufferSize() const { return BufferEnd-BufferStart; }
56 StringRef getBuffer() const {
57 return StringRef(BufferStart, getBufferSize());
60 /// getBufferIdentifier - Return an identifier for this buffer, typically the
61 /// filename it was read from.
62 virtual const char *getBufferIdentifier() const {
63 return "Unknown buffer";
66 /// getFile - Open the specified file as a MemoryBuffer, returning a new
67 /// MemoryBuffer if successful, otherwise returning null. If FileSize is
68 /// specified, this means that the client knows that the file exists and that
69 /// it has the specified size.
71 /// \param IsVolatile true indicates that the file may be changing often.
72 static error_code getFile(Twine Filename, OwningPtr<MemoryBuffer> &Result,
73 int64_t FileSize = -1,
74 bool RequiresNullTerminator = true,
75 bool IsVolatile = false);
76 static error_code getFile(Twine Filename,
77 std::unique_ptr<MemoryBuffer> &Result,
78 int64_t FileSize = -1,
79 bool RequiresNullTerminator = true,
80 bool IsVolatile = false);
82 /// Given an already-open file descriptor, map some slice of it into a
83 /// MemoryBuffer. The slice is specified by an \p Offset and \p MapSize.
84 /// Since this is in the middle of a file, the buffer is not null terminated.
86 /// \param IsVolatile true indicates that the file may be changing often.
87 static error_code getOpenFileSlice(int FD, const char *Filename,
88 OwningPtr<MemoryBuffer> &Result,
89 uint64_t MapSize, int64_t Offset,
90 bool IsVolatile = false);
91 static error_code getOpenFileSlice(int FD, const char *Filename,
92 std::unique_ptr<MemoryBuffer> &Result,
93 uint64_t MapSize, int64_t Offset,
94 bool IsVolatile = false);
96 /// Given an already-open file descriptor, read the file and return a
99 /// \param IsVolatile true indicates that the file may be changing often.
100 static error_code getOpenFile(int FD, const char *Filename,
101 OwningPtr<MemoryBuffer> &Result,
103 bool RequiresNullTerminator = true,
104 bool IsVolatile = false);
105 static error_code getOpenFile(int FD, const char *Filename,
106 std::unique_ptr<MemoryBuffer> &Result,
108 bool RequiresNullTerminator = true,
109 bool IsVolatile = false);
111 /// getMemBuffer - Open the specified memory range as a MemoryBuffer. Note
112 /// that InputData must be null terminated if RequiresNullTerminator is true.
113 static MemoryBuffer *getMemBuffer(StringRef InputData,
114 StringRef BufferName = "",
115 bool RequiresNullTerminator = true);
117 /// getMemBufferCopy - Open the specified memory range as a MemoryBuffer,
118 /// copying the contents and taking ownership of it. InputData does not
119 /// have to be null terminated.
120 static MemoryBuffer *getMemBufferCopy(StringRef InputData,
121 StringRef BufferName = "");
123 /// getNewMemBuffer - Allocate a new MemoryBuffer of the specified size that
124 /// is completely initialized to zeros. Note that the caller should
125 /// initialize the memory allocated by this method. The memory is owned by
126 /// the MemoryBuffer object.
127 static MemoryBuffer *getNewMemBuffer(size_t Size, StringRef BufferName = "");
129 /// getNewUninitMemBuffer - Allocate a new MemoryBuffer of the specified size
130 /// that is not initialized. Note that the caller should initialize the
131 /// memory allocated by this method. The memory is owned by the MemoryBuffer
133 static MemoryBuffer *getNewUninitMemBuffer(size_t Size,
134 StringRef BufferName = "");
136 /// getSTDIN - Read all of stdin into a file buffer, and return it.
137 /// If an error occurs, this returns null and sets ec.
138 static error_code getSTDIN(OwningPtr<MemoryBuffer> &Result);
139 static error_code getSTDIN(std::unique_ptr<MemoryBuffer> &Result);
142 /// getFileOrSTDIN - Open the specified file as a MemoryBuffer, or open stdin
143 /// if the Filename is "-". If an error occurs, this returns null and sets
145 static error_code getFileOrSTDIN(StringRef Filename,
146 OwningPtr<MemoryBuffer> &Result,
147 int64_t FileSize = -1);
148 static error_code getFileOrSTDIN(StringRef Filename,
149 std::unique_ptr<MemoryBuffer> &Result,
150 int64_t FileSize = -1);
152 //===--------------------------------------------------------------------===//
153 // Provided for performance analysis.
154 //===--------------------------------------------------------------------===//
156 /// The kind of memory backing used to support the MemoryBuffer.
162 /// Return information on the memory mechanism used to support the
164 virtual BufferKind getBufferKind() const = 0;
167 // Create wrappers for C Binding types (see CBindingWrapping.h).
168 DEFINE_SIMPLE_CONVERSION_FUNCTIONS(MemoryBuffer, LLVMMemoryBufferRef)
170 } // end namespace llvm