LLVM  16.0.0git
MemoryBuffer.h
Go to the documentation of this file.
1 //===--- MemoryBuffer.h - Memory Buffer Interface ---------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file defines the MemoryBuffer interface.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #ifndef LLVM_SUPPORT_MEMORYBUFFER_H
14 #define LLVM_SUPPORT_MEMORYBUFFER_H
15 
16 #include "llvm-c/Types.h"
17 #include "llvm/ADT/ArrayRef.h"
18 #include "llvm/ADT/StringRef.h"
19 #include "llvm/ADT/Twine.h"
21 #include "llvm/Support/ErrorOr.h"
23 #include <cstddef>
24 #include <cstdint>
25 #include <memory>
26 
27 namespace llvm {
28 namespace sys {
29 namespace fs {
30 // Duplicated from FileSystem.h to avoid a dependency.
31 #if defined(_WIN32)
32 // A Win32 HANDLE is a typedef of void*
33 using file_t = void *;
34 #else
35 using file_t = int;
36 #endif
37 } // namespace fs
38 } // namespace sys
39 
40 /// This interface provides simple read-only access to a block of memory, and
41 /// provides simple methods for reading files and standard input into a memory
42 /// buffer. In addition to basic access to the characters in the file, this
43 /// interface guarantees you can read one character past the end of the file,
44 /// and that this character will read as '\0'.
45 ///
46 /// The '\0' guarantee is needed to support an optimization -- it's intended to
47 /// be more efficient for clients which are reading all the data to stop
48 /// reading when they encounter a '\0' than to continually check the file
49 /// position to see if it has reached the end of the file.
50 class MemoryBuffer {
51  const char *BufferStart; // Start of the buffer.
52  const char *BufferEnd; // End of the buffer.
53 
54 protected:
55  MemoryBuffer() = default;
56 
57  void init(const char *BufStart, const char *BufEnd,
58  bool RequiresNullTerminator);
59 
60 public:
61  MemoryBuffer(const MemoryBuffer &) = delete;
62  MemoryBuffer &operator=(const MemoryBuffer &) = delete;
63  virtual ~MemoryBuffer();
64 
65  const char *getBufferStart() const { return BufferStart; }
66  const char *getBufferEnd() const { return BufferEnd; }
67  size_t getBufferSize() const { return BufferEnd-BufferStart; }
68 
69  StringRef getBuffer() const {
70  return StringRef(BufferStart, getBufferSize());
71  }
72 
73  /// Return an identifier for this buffer, typically the filename it was read
74  /// from.
75  virtual StringRef getBufferIdentifier() const { return "Unknown buffer"; }
76 
77  /// For read-only MemoryBuffer_MMap, mark the buffer as unused in the near
78  /// future and the kernel can free resources associated with it. Further
79  /// access is supported but may be expensive. This calls
80  /// madvise(MADV_DONTNEED) on read-only file mappings on *NIX systems. This
81  /// function should not be called on a writable buffer.
82  virtual void dontNeedIfMmap() {}
83 
84  /// Open the specified file as a MemoryBuffer, returning a new MemoryBuffer
85  /// if successful, otherwise returning null.
86  ///
87  /// \param IsText Set to true to indicate that the file should be read in
88  /// text mode.
89  ///
90  /// \param IsVolatile Set to true to indicate that the contents of the file
91  /// can change outside the user's control, e.g. when libclang tries to parse
92  /// while the user is editing/updating the file or if the file is on an NFS.
94  getFile(const Twine &Filename, bool IsText = false,
95  bool RequiresNullTerminator = true, bool IsVolatile = false);
96 
97  /// Read all of the specified file into a MemoryBuffer as a stream
98  /// (i.e. until EOF reached). This is useful for special files that
99  /// look like a regular file but have 0 size (e.g. /proc/cpuinfo on Linux).
101  getFileAsStream(const Twine &Filename);
102 
103  /// Given an already-open file descriptor, map some slice of it into a
104  /// MemoryBuffer. The slice is specified by an \p Offset and \p MapSize.
105  /// Since this is in the middle of a file, the buffer is not null terminated.
107  getOpenFileSlice(sys::fs::file_t FD, const Twine &Filename, uint64_t MapSize,
108  int64_t Offset, bool IsVolatile = false);
109 
110  /// Given an already-open file descriptor, read the file and return a
111  /// MemoryBuffer.
112  ///
113  /// \param IsVolatile Set to true to indicate that the contents of the file
114  /// can change outside the user's control, e.g. when libclang tries to parse
115  /// while the user is editing/updating the file or if the file is on an NFS.
117  getOpenFile(sys::fs::file_t FD, const Twine &Filename, uint64_t FileSize,
118  bool RequiresNullTerminator = true, bool IsVolatile = false);
119 
120  /// Open the specified memory range as a MemoryBuffer. Note that InputData
121  /// must be null terminated if RequiresNullTerminator is true.
122  static std::unique_ptr<MemoryBuffer>
123  getMemBuffer(StringRef InputData, StringRef BufferName = "",
124  bool RequiresNullTerminator = true);
125 
126  static std::unique_ptr<MemoryBuffer>
127  getMemBuffer(MemoryBufferRef Ref, bool RequiresNullTerminator = true);
128 
129  /// Open the specified memory range as a MemoryBuffer, copying the contents
130  /// and taking ownership of it. InputData does not have to be null terminated.
131  static std::unique_ptr<MemoryBuffer>
132  getMemBufferCopy(StringRef InputData, const Twine &BufferName = "");
133 
134  /// Read all of stdin into a file buffer, and return it.
136 
137  /// Open the specified file as a MemoryBuffer, or open stdin if the Filename
138  /// is "-".
140  getFileOrSTDIN(const Twine &Filename, bool IsText = false,
141  bool RequiresNullTerminator = true);
142 
143  /// Map a subrange of the specified file as a MemoryBuffer.
145  getFileSlice(const Twine &Filename, uint64_t MapSize, uint64_t Offset,
146  bool IsVolatile = false);
147 
148  //===--------------------------------------------------------------------===//
149  // Provided for performance analysis.
150  //===--------------------------------------------------------------------===//
151 
152  /// The kind of memory backing used to support the MemoryBuffer.
153  enum BufferKind {
156  };
157 
158  /// Return information on the memory mechanism used to support the
159  /// MemoryBuffer.
160  virtual BufferKind getBufferKind() const = 0;
161 
163 };
164 
165 /// This class is an extension of MemoryBuffer, which allows copy-on-write
166 /// access to the underlying contents. It only supports creation methods that
167 /// are guaranteed to produce a writable buffer. For example, mapping a file
168 /// read-only is not supported.
170 protected:
171  WritableMemoryBuffer() = default;
172 
173 public:
177 
178  // const_cast is well-defined here, because the underlying buffer is
179  // guaranteed to have been initialized with a mutable buffer.
180  char *getBufferStart() {
181  return const_cast<char *>(MemoryBuffer::getBufferStart());
182  }
183  char *getBufferEnd() {
184  return const_cast<char *>(MemoryBuffer::getBufferEnd());
185  }
187  return {getBufferStart(), getBufferEnd()};
188  }
189 
191  getFile(const Twine &Filename, bool IsVolatile = false);
192 
193  /// Map a subrange of the specified file as a WritableMemoryBuffer.
195  getFileSlice(const Twine &Filename, uint64_t MapSize, uint64_t Offset,
196  bool IsVolatile = false);
197 
198  /// Allocate a new MemoryBuffer of the specified size that is not initialized.
199  /// Note that the caller should initialize the memory allocated by this
200  /// method. The memory is owned by the MemoryBuffer object.
201  static std::unique_ptr<WritableMemoryBuffer>
202  getNewUninitMemBuffer(size_t Size, const Twine &BufferName = "");
203 
204  /// Allocate a new zero-initialized MemoryBuffer of the specified size. Note
205  /// that the caller need not initialize the memory allocated by this method.
206  /// The memory is owned by the MemoryBuffer object.
207  static std::unique_ptr<WritableMemoryBuffer>
208  getNewMemBuffer(size_t Size, const Twine &BufferName = "");
209 
210 private:
211  // Hide these base class factory function so one can't write
212  // WritableMemoryBuffer::getXXX()
213  // and be surprised that he got a read-only Buffer.
221 };
222 
223 /// This class is an extension of MemoryBuffer, which allows write access to
224 /// the underlying contents and committing those changes to the original source.
225 /// It only supports creation methods that are guaranteed to produce a writable
226 /// buffer. For example, mapping a file read-only is not supported.
228 protected:
229  WriteThroughMemoryBuffer() = default;
230 
231 public:
235 
236  // const_cast is well-defined here, because the underlying buffer is
237  // guaranteed to have been initialized with a mutable buffer.
238  char *getBufferStart() {
239  return const_cast<char *>(MemoryBuffer::getBufferStart());
240  }
241  char *getBufferEnd() {
242  return const_cast<char *>(MemoryBuffer::getBufferEnd());
243  }
245  return {getBufferStart(), getBufferEnd()};
246  }
247 
249  getFile(const Twine &Filename, int64_t FileSize = -1);
250 
251  /// Map a subrange of the specified file as a ReadWriteMemoryBuffer.
253  getFileSlice(const Twine &Filename, uint64_t MapSize, uint64_t Offset);
254 
255 private:
256  // Hide these base class factory function so one can't write
257  // WritableMemoryBuffer::getXXX()
258  // and be surprised that he got a read-only Buffer.
266 };
267 
268 // Create wrappers for C Binding types (see CBindingWrapping.h).
270 
271 } // end namespace llvm
272 
273 #endif // LLVM_SUPPORT_MEMORYBUFFER_H
llvm::sys::fs::file_t
int file_t
Definition: FileSystem.h:60
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
llvm::MemoryBuffer::getOpenFile
static ErrorOr< std::unique_ptr< MemoryBuffer > > getOpenFile(sys::fs::file_t FD, const Twine &Filename, uint64_t FileSize, bool RequiresNullTerminator=true, bool IsVolatile=false)
Given an already-open file descriptor, read the file and return a MemoryBuffer.
Definition: MemoryBuffer.cpp:498
llvm::WritableMemoryBuffer::getBufferEnd
char * getBufferEnd()
Definition: MemoryBuffer.h:183
llvm::MemoryBuffer::~MemoryBuffer
virtual ~MemoryBuffer()
llvm::WritableMemoryBuffer::getFile
static ErrorOr< std::unique_ptr< WritableMemoryBuffer > > getFile(const Twine &Filename, bool IsVolatile=false)
Definition: MemoryBuffer.cpp:267
StringRef.h
llvm::MemoryBuffer::getOpenFileSlice
static ErrorOr< std::unique_ptr< MemoryBuffer > > getOpenFileSlice(sys::fs::file_t FD, const Twine &Filename, uint64_t MapSize, int64_t Offset, bool IsVolatile=false)
Given an already-open file descriptor, map some slice of it into a MemoryBuffer.
Definition: MemoryBuffer.cpp:505
Types.h
llvm::MemoryBuffer::getSTDIN
static ErrorOr< std::unique_ptr< MemoryBuffer > > getSTDIN()
Read all of stdin into a file buffer, and return it.
Definition: MemoryBuffer.cpp:512
MemoryBufferRef.h
llvm::WriteThroughMemoryBuffer
This class is an extension of MemoryBuffer, which allows write access to the underlying contents and ...
Definition: MemoryBuffer.h:227
CBindingWrapping.h
llvm::MemoryBuffer::dontNeedIfMmap
virtual void dontNeedIfMmap()
For read-only MemoryBuffer_MMap, mark the buffer as unused in the near future and the kernel can free...
Definition: MemoryBuffer.h:82
llvm::MemoryBufferRef
Definition: MemoryBufferRef.h:22
llvm::MemoryBuffer
This interface provides simple read-only access to a block of memory, and provides simple methods for...
Definition: MemoryBuffer.h:50
llvm::MemoryBuffer::MemoryBuffer
MemoryBuffer()=default
llvm::MemoryBuffer::getFileAsStream
static ErrorOr< std::unique_ptr< MemoryBuffer > > getFileAsStream(const Twine &Filename)
Read all of the specified file into a MemoryBuffer as a stream (i.e.
Definition: MemoryBuffer.cpp:523
DEFINE_SIMPLE_CONVERSION_FUNCTIONS
#define DEFINE_SIMPLE_CONVERSION_FUNCTIONS(ty, ref)
Definition: CBindingWrapping.h:19
llvm::MemoryBuffer::getFile
static ErrorOr< std::unique_ptr< MemoryBuffer > > getFile(const Twine &Filename, bool IsText=false, bool RequiresNullTerminator=true, bool IsVolatile=false)
Open the specified file as a MemoryBuffer, returning a new MemoryBuffer if successful,...
Definition: MemoryBuffer.cpp:239
llvm::MemoryBuffer::getMemBuffer
static std::unique_ptr< MemoryBuffer > getMemBuffer(StringRef InputData, StringRef BufferName="", bool RequiresNullTerminator=true)
Open the specified memory range as a MemoryBuffer.
Definition: MemoryBuffer.cpp:115
llvm::MemoryBuffer::init
void init(const char *BufStart, const char *BufEnd, bool RequiresNullTerminator)
init - Initialize this MemoryBuffer as a reference to externally allocated memory,...
Definition: MemoryBuffer.cpp:48
llvm::MutableArrayRef< char >
llvm::MemoryBuffer::getBufferEnd
const char * getBufferEnd() const
Definition: MemoryBuffer.h:66
llvm::MemoryBuffer::getBufferSize
size_t getBufferSize() const
Definition: MemoryBuffer.h:67
Twine.h
int
Clang compiles this i1 i64 store i64 i64 store i64 i64 store i64 i64 store i64 align Which gets codegen d xmm0 movaps rbp movaps rbp movaps rbp movaps rbp rbp rbp rbp rbp It would be better to have movq s of instead of the movaps s LLVM produces ret int
Definition: README.txt:536
llvm::MemoryBuffer::MemoryBuffer_MMap
@ MemoryBuffer_MMap
Definition: MemoryBuffer.h:155
llvm::WritableMemoryBuffer::getNewMemBuffer
static std::unique_ptr< WritableMemoryBuffer > getNewMemBuffer(size_t Size, const Twine &BufferName="")
Allocate a new zero-initialized MemoryBuffer of the specified size.
Definition: MemoryBuffer.cpp:310
llvm::MemoryBuffer::getBuffer
StringRef getBuffer() const
Definition: MemoryBuffer.h:69
llvm::MemoryBuffer::getBufferIdentifier
virtual StringRef getBufferIdentifier() const
Return an identifier for this buffer, typically the filename it was read from.
Definition: MemoryBuffer.h:75
uint64_t
llvm::MemoryBuffer::operator=
MemoryBuffer & operator=(const MemoryBuffer &)=delete
llvm::MemoryBuffer::getFileSlice
static ErrorOr< std::unique_ptr< MemoryBuffer > > getFileSlice(const Twine &Filename, uint64_t MapSize, uint64_t Offset, bool IsVolatile=false)
Map a subrange of the specified file as a MemoryBuffer.
Definition: MemoryBuffer.cpp:158
llvm::MemoryBuffer::getMemBufferRef
MemoryBufferRef getMemBufferRef() const
Definition: MemoryBuffer.cpp:535
llvm::MemoryBuffer::BufferKind
BufferKind
The kind of memory backing used to support the MemoryBuffer.
Definition: MemoryBuffer.h:153
llvm::WriteThroughMemoryBuffer::WriteThroughMemoryBuffer
WriteThroughMemoryBuffer()=default
ErrorOr.h
llvm::MemoryBuffer::getFileOrSTDIN
static ErrorOr< std::unique_ptr< MemoryBuffer > > getFileOrSTDIN(const Twine &Filename, bool IsText=false, bool RequiresNullTerminator=true)
Open the specified file as a MemoryBuffer, or open stdin if the Filename is "-".
Definition: MemoryBuffer.cpp:146
ArrayRef.h
llvm::WritableMemoryBuffer::getNewUninitMemBuffer
static std::unique_ptr< WritableMemoryBuffer > getNewUninitMemBuffer(size_t Size, const Twine &BufferName="")
Allocate a new MemoryBuffer of the specified size that is not initialized.
Definition: MemoryBuffer.cpp:282
llvm::WriteThroughMemoryBuffer::getBuffer
MutableArrayRef< char > getBuffer()
Definition: MemoryBuffer.h:244
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
llvm::AMDGPU::HSAMD::Kernel::Arg::Key::IsVolatile
constexpr char IsVolatile[]
Key for Kernel::Arg::Metadata::mIsVolatile.
Definition: AMDGPUMetadata.h:199
llvm::WritableMemoryBuffer::WritableMemoryBuffer
WritableMemoryBuffer()=default
llvm::Twine
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:81
llvm::MemoryBuffer::getBufferStart
const char * getBufferStart() const
Definition: MemoryBuffer.h:65
llvm::WritableMemoryBuffer::getBufferStart
char * getBufferStart()
Definition: MemoryBuffer.h:180
llvm::WriteThroughMemoryBuffer::getBufferStart
char * getBufferStart()
Definition: MemoryBuffer.h:238
llvm::WriteThroughMemoryBuffer::getFileSlice
static ErrorOr< std::unique_ptr< WriteThroughMemoryBuffer > > getFileSlice(const Twine &Filename, uint64_t MapSize, uint64_t Offset)
Map a subrange of the specified file as a ReadWriteMemoryBuffer.
Definition: MemoryBuffer.cpp:421
llvm::ErrorOr
Represents either an error or a value T.
Definition: ErrorOr.h:56
llvm::WritableMemoryBuffer::getBuffer
MutableArrayRef< char > getBuffer()
Definition: MemoryBuffer.h:186
llvm::WritableMemoryBuffer::getFileSlice
static ErrorOr< std::unique_ptr< WritableMemoryBuffer > > getFileSlice(const Twine &Filename, uint64_t MapSize, uint64_t Offset, bool IsVolatile=false)
Map a subrange of the specified file as a WritableMemoryBuffer.
Definition: MemoryBuffer.cpp:274
llvm::WriteThroughMemoryBuffer::getFile
static ErrorOr< std::unique_ptr< WriteThroughMemoryBuffer > > getFile(const Twine &Filename, int64_t FileSize=-1)
Definition: MemoryBuffer.cpp:415
LLVMMemoryBufferRef
struct LLVMOpaqueMemoryBuffer * LLVMMemoryBufferRef
LLVM uses a polymorphic type hierarchy which C cannot represent, therefore parameters must be passed ...
Definition: Types.h:48
llvm::WriteThroughMemoryBuffer::getBufferEnd
char * getBufferEnd()
Definition: MemoryBuffer.h:241
llvm::WritableMemoryBuffer
This class is an extension of MemoryBuffer, which allows copy-on-write access to the underlying conte...
Definition: MemoryBuffer.h:169
llvm::MemoryBuffer::getMemBufferCopy
static std::unique_ptr< MemoryBuffer > getMemBufferCopy(StringRef InputData, const Twine &BufferName="")
Open the specified memory range as a MemoryBuffer, copying the contents and taking ownership of it.
Definition: MemoryBuffer.cpp:138
llvm::MemoryBuffer::MemoryBuffer_Malloc
@ MemoryBuffer_Malloc
Definition: MemoryBuffer.h:154
llvm::MemoryBuffer::getBufferKind
virtual BufferKind getBufferKind() const =0
Return information on the memory mechanism used to support the MemoryBuffer.