LLVM  9.0.0svn
MemoryBuffer.cpp
Go to the documentation of this file.
1 //===--- MemoryBuffer.cpp - Memory Buffer implementation ------------------===//
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 implements the MemoryBuffer interface.
10 //
11 //===----------------------------------------------------------------------===//
12 
14 #include "llvm/ADT/SmallString.h"
15 #include "llvm/Config/config.h"
16 #include "llvm/Support/Errc.h"
17 #include "llvm/Support/Errno.h"
20 #include "llvm/Support/Path.h"
21 #include "llvm/Support/Process.h"
22 #include "llvm/Support/Program.h"
24 #include <cassert>
25 #include <cerrno>
26 #include <cstring>
27 #include <new>
28 #include <sys/types.h>
29 #include <system_error>
30 #if !defined(_MSC_VER) && !defined(__MINGW32__)
31 #include <unistd.h>
32 #else
33 #include <io.h>
34 #endif
35 using namespace llvm;
36 
37 //===----------------------------------------------------------------------===//
38 // MemoryBuffer implementation itself.
39 //===----------------------------------------------------------------------===//
40 
42 
43 /// init - Initialize this MemoryBuffer as a reference to externally allocated
44 /// memory, memory that we know is already null terminated.
45 void MemoryBuffer::init(const char *BufStart, const char *BufEnd,
46  bool RequiresNullTerminator) {
47  assert((!RequiresNullTerminator || BufEnd[0] == 0) &&
48  "Buffer is not null terminated!");
49  BufferStart = BufStart;
50  BufferEnd = BufEnd;
51 }
52 
53 //===----------------------------------------------------------------------===//
54 // MemoryBufferMem implementation.
55 //===----------------------------------------------------------------------===//
56 
57 /// CopyStringRef - Copies contents of a StringRef into a block of memory and
58 /// null-terminates it.
59 static void CopyStringRef(char *Memory, StringRef Data) {
60  if (!Data.empty())
61  memcpy(Memory, Data.data(), Data.size());
62  Memory[Data.size()] = 0; // Null terminate string.
63 }
64 
65 namespace {
66 struct NamedBufferAlloc {
67  const Twine &Name;
68  NamedBufferAlloc(const Twine &Name) : Name(Name) {}
69 };
70 }
71 
72 void *operator new(size_t N, const NamedBufferAlloc &Alloc) {
73  SmallString<256> NameBuf;
74  StringRef NameRef = Alloc.Name.toStringRef(NameBuf);
75 
76  char *Mem = static_cast<char *>(operator new(N + NameRef.size() + 1));
77  CopyStringRef(Mem + N, NameRef);
78  return Mem;
79 }
80 
81 namespace {
82 /// MemoryBufferMem - Named MemoryBuffer pointing to a block of memory.
83 template<typename MB>
84 class MemoryBufferMem : public MB {
85 public:
86  MemoryBufferMem(StringRef InputData, bool RequiresNullTerminator) {
87  MemoryBuffer::init(InputData.begin(), InputData.end(),
88  RequiresNullTerminator);
89  }
90 
91  /// Disable sized deallocation for MemoryBufferMem, because it has
92  /// tail-allocated data.
93  void operator delete(void *p) { ::operator delete(p); }
94 
95  StringRef getBufferIdentifier() const override {
96  // The name is stored after the class itself.
97  return StringRef(reinterpret_cast<const char *>(this + 1));
98  }
99 
100  MemoryBuffer::BufferKind getBufferKind() const override {
102  }
103 };
104 }
105 
106 template <typename MB>
108 getFileAux(const Twine &Filename, int64_t FileSize, uint64_t MapSize,
109  uint64_t Offset, bool RequiresNullTerminator, bool IsVolatile);
110 
111 std::unique_ptr<MemoryBuffer>
113  bool RequiresNullTerminator) {
114  auto *Ret = new (NamedBufferAlloc(BufferName))
115  MemoryBufferMem<MemoryBuffer>(InputData, RequiresNullTerminator);
116  return std::unique_ptr<MemoryBuffer>(Ret);
117 }
118 
119 std::unique_ptr<MemoryBuffer>
120 MemoryBuffer::getMemBuffer(MemoryBufferRef Ref, bool RequiresNullTerminator) {
121  return std::unique_ptr<MemoryBuffer>(getMemBuffer(
122  Ref.getBuffer(), Ref.getBufferIdentifier(), RequiresNullTerminator));
123 }
124 
126 getMemBufferCopyImpl(StringRef InputData, const Twine &BufferName) {
127  auto Buf = WritableMemoryBuffer::getNewUninitMemBuffer(InputData.size(), BufferName);
128  if (!Buf)
130  memcpy(Buf->getBufferStart(), InputData.data(), InputData.size());
131  return std::move(Buf);
132 }
133 
134 std::unique_ptr<MemoryBuffer>
135 MemoryBuffer::getMemBufferCopy(StringRef InputData, const Twine &BufferName) {
136  auto Buf = getMemBufferCopyImpl(InputData, BufferName);
137  if (Buf)
138  return std::move(*Buf);
139  return nullptr;
140 }
141 
143 MemoryBuffer::getFileOrSTDIN(const Twine &Filename, int64_t FileSize,
144  bool RequiresNullTerminator) {
145  SmallString<256> NameBuf;
146  StringRef NameRef = Filename.toStringRef(NameBuf);
147 
148  if (NameRef == "-")
149  return getSTDIN();
150  return getFile(Filename, FileSize, RequiresNullTerminator);
151 }
152 
154 MemoryBuffer::getFileSlice(const Twine &FilePath, uint64_t MapSize,
155  uint64_t Offset, bool IsVolatile) {
156  return getFileAux<MemoryBuffer>(FilePath, -1, MapSize, Offset, false,
157  IsVolatile);
158 }
159 
160 //===----------------------------------------------------------------------===//
161 // MemoryBuffer::getFile implementation.
162 //===----------------------------------------------------------------------===//
163 
164 namespace {
165 /// Memory maps a file descriptor using sys::fs::mapped_file_region.
166 ///
167 /// This handles converting the offset into a legal offset on the platform.
168 template<typename MB>
169 class MemoryBufferMMapFile : public MB {
171 
172  static uint64_t getLegalMapOffset(uint64_t Offset) {
173  return Offset & ~(sys::fs::mapped_file_region::alignment() - 1);
174  }
175 
176  static uint64_t getLegalMapSize(uint64_t Len, uint64_t Offset) {
177  return Len + (Offset - getLegalMapOffset(Offset));
178  }
179 
180  const char *getStart(uint64_t Len, uint64_t Offset) {
181  return MFR.const_data() + (Offset - getLegalMapOffset(Offset));
182  }
183 
184 public:
185  MemoryBufferMMapFile(bool RequiresNullTerminator, int FD, uint64_t Len,
186  uint64_t Offset, std::error_code &EC)
187  : MFR(FD, MB::Mapmode, getLegalMapSize(Len, Offset),
188  getLegalMapOffset(Offset), EC) {
189  if (!EC) {
190  const char *Start = getStart(Len, Offset);
191  MemoryBuffer::init(Start, Start + Len, RequiresNullTerminator);
192  }
193  }
194 
195  /// Disable sized deallocation for MemoryBufferMMapFile, because it has
196  /// tail-allocated data.
197  void operator delete(void *p) { ::operator delete(p); }
198 
199  StringRef getBufferIdentifier() const override {
200  // The name is stored after the class itself.
201  return StringRef(reinterpret_cast<const char *>(this + 1));
202  }
203 
204  MemoryBuffer::BufferKind getBufferKind() const override {
206  }
207 };
208 }
209 
211 getMemoryBufferForStream(int FD, const Twine &BufferName) {
212  const ssize_t ChunkSize = 4096*4;
213  SmallString<ChunkSize> Buffer;
214  ssize_t ReadBytes;
215  // Read into Buffer until we hit EOF.
216  do {
217  Buffer.reserve(Buffer.size() + ChunkSize);
218  ReadBytes = sys::RetryAfterSignal(-1, ::read, FD, Buffer.end(), ChunkSize);
219  if (ReadBytes == -1)
220  return std::error_code(errno, std::generic_category());
221  Buffer.set_size(Buffer.size() + ReadBytes);
222  } while (ReadBytes != 0);
223 
224  return getMemBufferCopyImpl(Buffer, BufferName);
225 }
226 
227 
229 MemoryBuffer::getFile(const Twine &Filename, int64_t FileSize,
230  bool RequiresNullTerminator, bool IsVolatile) {
231  return getFileAux<MemoryBuffer>(Filename, FileSize, FileSize, 0,
232  RequiresNullTerminator, IsVolatile);
233 }
234 
235 template <typename MB>
237 getOpenFileImpl(int FD, const Twine &Filename, uint64_t FileSize,
238  uint64_t MapSize, int64_t Offset, bool RequiresNullTerminator,
239  bool IsVolatile);
240 
241 template <typename MB>
243 getFileAux(const Twine &Filename, int64_t FileSize, uint64_t MapSize,
244  uint64_t Offset, bool RequiresNullTerminator, bool IsVolatile) {
245  int FD;
246  std::error_code EC = sys::fs::openFileForRead(Filename, FD, sys::fs::OF_None);
247 
248  if (EC)
249  return EC;
250 
251  auto Ret = getOpenFileImpl<MB>(FD, Filename, FileSize, MapSize, Offset,
252  RequiresNullTerminator, IsVolatile);
253  close(FD);
254  return Ret;
255 }
256 
258 WritableMemoryBuffer::getFile(const Twine &Filename, int64_t FileSize,
259  bool IsVolatile) {
260  return getFileAux<WritableMemoryBuffer>(Filename, FileSize, FileSize, 0,
261  /*RequiresNullTerminator*/ false,
262  IsVolatile);
263 }
264 
266 WritableMemoryBuffer::getFileSlice(const Twine &Filename, uint64_t MapSize,
267  uint64_t Offset, bool IsVolatile) {
268  return getFileAux<WritableMemoryBuffer>(Filename, -1, MapSize, Offset, false,
269  IsVolatile);
270 }
271 
272 std::unique_ptr<WritableMemoryBuffer>
274  using MemBuffer = MemoryBufferMem<WritableMemoryBuffer>;
275  // Allocate space for the MemoryBuffer, the data and the name. It is important
276  // that MemoryBuffer and data are aligned so PointerIntPair works with them.
277  // TODO: Is 16-byte alignment enough? We copy small object files with large
278  // alignment expectations into this buffer.
279  SmallString<256> NameBuf;
280  StringRef NameRef = BufferName.toStringRef(NameBuf);
281  size_t AlignedStringLen = alignTo(sizeof(MemBuffer) + NameRef.size() + 1, 16);
282  size_t RealLen = AlignedStringLen + Size + 1;
283  char *Mem = static_cast<char*>(operator new(RealLen, std::nothrow));
284  if (!Mem)
285  return nullptr;
286 
287  // The name is stored after the class itself.
288  CopyStringRef(Mem + sizeof(MemBuffer), NameRef);
289 
290  // The buffer begins after the name and must be aligned.
291  char *Buf = Mem + AlignedStringLen;
292  Buf[Size] = 0; // Null terminate buffer.
293 
294  auto *Ret = new (Mem) MemBuffer(StringRef(Buf, Size), true);
295  return std::unique_ptr<WritableMemoryBuffer>(Ret);
296 }
297 
298 std::unique_ptr<WritableMemoryBuffer>
300  auto SB = WritableMemoryBuffer::getNewUninitMemBuffer(Size, BufferName);
301  if (!SB)
302  return nullptr;
303  memset(SB->getBufferStart(), 0, Size);
304  return SB;
305 }
306 
307 static bool shouldUseMmap(int FD,
308  size_t FileSize,
309  size_t MapSize,
310  off_t Offset,
311  bool RequiresNullTerminator,
312  int PageSize,
313  bool IsVolatile) {
314  // mmap may leave the buffer without null terminator if the file size changed
315  // by the time the last page is mapped in, so avoid it if the file size is
316  // likely to change.
317  if (IsVolatile)
318  return false;
319 
320  // We don't use mmap for small files because this can severely fragment our
321  // address space.
322  if (MapSize < 4 * 4096 || MapSize < (unsigned)PageSize)
323  return false;
324 
325  if (!RequiresNullTerminator)
326  return true;
327 
328  // If we don't know the file size, use fstat to find out. fstat on an open
329  // file descriptor is cheaper than stat on a random path.
330  // FIXME: this chunk of code is duplicated, but it avoids a fstat when
331  // RequiresNullTerminator = false and MapSize != -1.
332  if (FileSize == size_t(-1)) {
334  if (sys::fs::status(FD, Status))
335  return false;
336  FileSize = Status.getSize();
337  }
338 
339  // If we need a null terminator and the end of the map is inside the file,
340  // we cannot use mmap.
341  size_t End = Offset + MapSize;
342  assert(End <= FileSize);
343  if (End != FileSize)
344  return false;
345 
346  // Don't try to map files that are exactly a multiple of the system page size
347  // if we need a null terminator.
348  if ((FileSize & (PageSize -1)) == 0)
349  return false;
350 
351 #if defined(__CYGWIN__)
352  // Don't try to map files that are exactly a multiple of the physical page size
353  // if we need a null terminator.
354  // FIXME: We should reorganize again getPageSize() on Win32.
355  if ((FileSize & (4096 - 1)) == 0)
356  return false;
357 #endif
358 
359  return true;
360 }
361 
363 getReadWriteFile(const Twine &Filename, uint64_t FileSize, uint64_t MapSize,
364  uint64_t Offset) {
365  int FD;
366  std::error_code EC = sys::fs::openFileForReadWrite(
368 
369  if (EC)
370  return EC;
371 
372  // Default is to map the full file.
373  if (MapSize == uint64_t(-1)) {
374  // If we don't know the file size, use fstat to find out. fstat on an open
375  // file descriptor is cheaper than stat on a random path.
376  if (FileSize == uint64_t(-1)) {
378  std::error_code EC = sys::fs::status(FD, Status);
379  if (EC)
380  return EC;
381 
382  // If this not a file or a block device (e.g. it's a named pipe
383  // or character device), we can't mmap it, so error out.
384  sys::fs::file_type Type = Status.type();
385  if (Type != sys::fs::file_type::regular_file &&
388 
389  FileSize = Status.getSize();
390  }
391  MapSize = FileSize;
392  }
393 
394  std::unique_ptr<WriteThroughMemoryBuffer> Result(
395  new (NamedBufferAlloc(Filename))
396  MemoryBufferMMapFile<WriteThroughMemoryBuffer>(false, FD, MapSize,
397  Offset, EC));
398  if (EC)
399  return EC;
400  return std::move(Result);
401 }
402 
404 WriteThroughMemoryBuffer::getFile(const Twine &Filename, int64_t FileSize) {
405  return getReadWriteFile(Filename, FileSize, FileSize, 0);
406 }
407 
408 /// Map a subrange of the specified file as a WritableMemoryBuffer.
410 WriteThroughMemoryBuffer::getFileSlice(const Twine &Filename, uint64_t MapSize,
411  uint64_t Offset) {
412  return getReadWriteFile(Filename, -1, MapSize, Offset);
413 }
414 
415 template <typename MB>
417 getOpenFileImpl(int FD, const Twine &Filename, uint64_t FileSize,
418  uint64_t MapSize, int64_t Offset, bool RequiresNullTerminator,
419  bool IsVolatile) {
421 
422  // Default is to map the full file.
423  if (MapSize == uint64_t(-1)) {
424  // If we don't know the file size, use fstat to find out. fstat on an open
425  // file descriptor is cheaper than stat on a random path.
426  if (FileSize == uint64_t(-1)) {
428  std::error_code EC = sys::fs::status(FD, Status);
429  if (EC)
430  return EC;
431 
432  // If this not a file or a block device (e.g. it's a named pipe
433  // or character device), we can't trust the size. Create the memory
434  // buffer by copying off the stream.
435  sys::fs::file_type Type = Status.type();
436  if (Type != sys::fs::file_type::regular_file &&
438  return getMemoryBufferForStream(FD, Filename);
439 
440  FileSize = Status.getSize();
441  }
442  MapSize = FileSize;
443  }
444 
445  if (shouldUseMmap(FD, FileSize, MapSize, Offset, RequiresNullTerminator,
446  PageSize, IsVolatile)) {
447  std::error_code EC;
448  std::unique_ptr<MB> Result(
449  new (NamedBufferAlloc(Filename)) MemoryBufferMMapFile<MB>(
450  RequiresNullTerminator, FD, MapSize, Offset, EC));
451  if (!EC)
452  return std::move(Result);
453  }
454 
455  auto Buf = WritableMemoryBuffer::getNewUninitMemBuffer(MapSize, Filename);
456  if (!Buf) {
457  // Failed to create a buffer. The only way it can fail is if
458  // new(std::nothrow) returns 0.
460  }
461 
462  char *BufPtr = Buf.get()->getBufferStart();
463 
464  size_t BytesLeft = MapSize;
465 #ifndef HAVE_PREAD
466  if (lseek(FD, Offset, SEEK_SET) == -1)
467  return std::error_code(errno, std::generic_category());
468 #endif
469 
470  while (BytesLeft) {
471 #ifdef HAVE_PREAD
472  ssize_t NumRead = sys::RetryAfterSignal(-1, ::pread, FD, BufPtr, BytesLeft,
473  MapSize - BytesLeft + Offset);
474 #else
475  ssize_t NumRead = sys::RetryAfterSignal(-1, ::read, FD, BufPtr, BytesLeft);
476 #endif
477  if (NumRead == -1) {
478  // Error while reading.
479  return std::error_code(errno, std::generic_category());
480  }
481  if (NumRead == 0) {
482  memset(BufPtr, 0, BytesLeft); // zero-initialize rest of the buffer.
483  break;
484  }
485  BytesLeft -= NumRead;
486  BufPtr += NumRead;
487  }
488 
489  return std::move(Buf);
490 }
491 
493 MemoryBuffer::getOpenFile(int FD, const Twine &Filename, uint64_t FileSize,
494  bool RequiresNullTerminator, bool IsVolatile) {
495  return getOpenFileImpl<MemoryBuffer>(FD, Filename, FileSize, FileSize, 0,
496  RequiresNullTerminator, IsVolatile);
497 }
498 
500 MemoryBuffer::getOpenFileSlice(int FD, const Twine &Filename, uint64_t MapSize,
501  int64_t Offset, bool IsVolatile) {
502  assert(MapSize != uint64_t(-1));
503  return getOpenFileImpl<MemoryBuffer>(FD, Filename, -1, MapSize, Offset, false,
504  IsVolatile);
505 }
506 
508  // Read in all of the data from stdin, we cannot mmap stdin.
509  //
510  // FIXME: That isn't necessarily true, we should try to mmap stdin and
511  // fallback if it fails.
513 
514  return getMemoryBufferForStream(0, "<stdin>");
515 }
516 
519  int FD;
520  std::error_code EC = sys::fs::openFileForRead(Filename, FD, sys::fs::OF_None);
521  if (EC)
522  return EC;
524  getMemoryBufferForStream(FD, Filename);
525  close(FD);
526  return Ret;
527 }
528 
531  StringRef Identifier = getBufferIdentifier();
532  return MemoryBufferRef(Data, Identifier);
533 }
534 
virtual StringRef getBufferIdentifier() const
Return an identifier for this buffer, typically the filename it was read from.
Definition: MemoryBuffer.h:69
void init(const char *BufStart, const char *BufEnd, bool RequiresNullTerminator)
init - Initialize this MemoryBuffer as a reference to externally allocated memory, memory that we know is already null terminated.
std::error_code openFileForReadWrite(const Twine &Name, int &ResultFD, CreationDisposition Disp, OpenFlags Flags, unsigned Mode=0666)
Opens the file with the given name in a write-only or read-write mode, returning its open file descri...
Definition: FileSystem.h:1012
static ErrorOr< std::unique_ptr< MemoryBuffer > > getOpenFileSlice(int 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.
Represents either an error or a value T.
Definition: ErrorOr.h:56
StringRef getBuffer() const
Definition: MemoryBuffer.h:63
This class represents lattice values for constants.
Definition: AllocatorList.h:23
std::error_code openFileForRead(const Twine &Name, int &ResultFD, OpenFlags Flags=OF_None, SmallVectorImpl< char > *RealPath=nullptr)
Opens the file with the given name in a read-only mode, returning its open file descriptor.
constexpr char IsVolatile[]
Key for Kernel::Arg::Metadata::mIsVolatile.
amdgpu Simplify well known AMD library false FunctionCallee Value const Twine & Name
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.
This class provides various memory handling functions that manipulate MemoryBlock instances...
Definition: Memory.h:53
static std::unique_ptr< MemoryBuffer > getMemBuffer(StringRef InputData, StringRef BufferName="", bool RequiresNullTerminator=true)
Open the specified memory range as a MemoryBuffer.
StringRef toStringRef(SmallVectorImpl< char > &Out) const
This returns the twine as a single StringRef if it can be represented as such.
Definition: Twine.h:455
This class represents a memory mapped file.
Definition: FileSystem.h:1095
uint64_t alignTo(uint64_t Value, uint64_t Align, uint64_t Skew=0)
Returns the next integer (mod 2**64) that is greater than or equal to Value and is a multiple of Alig...
Definition: MathExtras.h:684
void reserve(size_type N)
Definition: SmallVector.h:369
Represents the result of a call to sys::fs::status().
Definition: FileSystem.h:246
CD_OpenExisting - When opening a file:
Definition: FileSystem.h:730
const char * const_data() const
Get a const view of the data.
virtual BufferKind getBufferKind() const =0
Return information on the memory mechanism used to support the MemoryBuffer.
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:80
StringRef getBuffer() const
Definition: MemoryBuffer.h:272
static ErrorOr< std::unique_ptr< MB > > getOpenFileImpl(int FD, const Twine &Filename, uint64_t FileSize, uint64_t MapSize, int64_t Offset, bool RequiresNullTerminator, bool IsVolatile)
The access may reference the value stored in memory.
std::error_code make_error_code(BitcodeError E)
static ErrorOr< std::unique_ptr< WritableMemoryBuffer > > getFile(const Twine &Filename, int64_t FileSize=-1, bool IsVolatile=false)
LLVM_NODISCARD bool empty() const
empty - Check if the string is empty.
Definition: StringRef.h:126
static ErrorOr< std::unique_ptr< WritableMemoryBuffer > > getMemoryBufferForStream(int FD, const Twine &BufferName)
static std::unique_ptr< WritableMemoryBuffer > getNewMemBuffer(size_t Size, const Twine &BufferName="")
Allocate a new zero-initialized MemoryBuffer of the specified size.
LLVM_NODISCARD size_t size() const
size - Get the string size.
Definition: StringRef.h:130
static unsigned getPageSizeEstimate()
Get the process&#39;s estimated page size.
Definition: Process.h:56
static ErrorOr< std::unique_ptr< MB > > getFileAux(const Twine &Filename, int64_t FileSize, uint64_t MapSize, uint64_t Offset, bool RequiresNullTerminator, bool IsVolatile)
static void CopyStringRef(char *Memory, StringRef Data)
CopyStringRef - Copies contents of a StringRef into a block of memory and null-terminates it...
static ErrorOr< std::unique_ptr< MemoryBuffer > > getSTDIN()
Read all of stdin into a file buffer, and return it.
static ErrorOr< std::unique_ptr< WriteThroughMemoryBuffer > > getFile(const Twine &Filename, int64_t FileSize=-1)
The instances of the Type class are immutable: once they are created, they are never changed...
Definition: Type.h:45
static std::unique_ptr< WritableMemoryBuffer > getNewUninitMemBuffer(size_t Size, const Twine &BufferName="")
Allocate a new MemoryBuffer of the specified size that is not initialized.
std::error_code status(const Twine &path, file_status &result, bool follow=true)
Get file status as if by POSIX stat().
void set_size(size_t N)
Set the array size to N, which the current array must have enough capacity for.
Definition: SmallVector.h:66
auto RetryAfterSignal(const FailT &Fail, const Fun &F, const Args &... As) -> decltype(F(As...))
Definition: Errno.h:33
size_t size() const
Definition: SmallVector.h:52
MemoryBufferRef getMemBufferRef() const
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...
static bool shouldUseMmap(int FD, size_t FileSize, size_t MapSize, off_t Offset, bool RequiresNullTerminator, int PageSize, bool IsVolatile)
StringRef getBufferIdentifier() const
Definition: MemoryBuffer.h:274
iterator begin() const
Definition: StringRef.h:101
std::error_code ChangeStdinToBinary()
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.
static ErrorOr< std::unique_ptr< WriteThroughMemoryBuffer > > getReadWriteFile(const Twine &Filename, uint64_t FileSize, uint64_t MapSize, uint64_t Offset)
static ErrorOr< std::unique_ptr< MemoryBuffer > > getFileOrSTDIN(const Twine &Filename, int64_t FileSize=-1, bool RequiresNullTerminator=true)
Open the specified file as a MemoryBuffer, or open stdin if the Filename is "-".
static ErrorOr< std::unique_ptr< WritableMemoryBuffer > > getMemBufferCopyImpl(StringRef InputData, const Twine &BufferName)
Provides a library for accessing information about this process and other processes on the operating ...
file_type
An enumeration for the file system&#39;s view of the type.
Definition: FileSystem.h:66
virtual ~MemoryBuffer()
value_type read(const void *memory, endianness endian)
Read a value of a particular endianness from memory.
Definition: Endian.h:65
#define N
static ErrorOr< std::unique_ptr< MemoryBuffer > > getFileAsStream(const Twine &Filename)
Read all of the specified file into a MemoryBuffer as a stream (i.e.
uint32_t Size
Definition: Profile.cpp:46
BufferKind
The kind of memory backing used to support the MemoryBuffer.
Definition: MemoryBuffer.h:139
static cl::opt< int > PageSize("imp-null-check-page-size", cl::desc("The page size of the target in bytes"), cl::init(4096), cl::Hidden)
static ErrorOr< std::unique_ptr< MemoryBuffer > > getFile(const Twine &Filename, int64_t FileSize=-1, bool RequiresNullTerminator=true, bool IsVolatile=false)
Open the specified file as a MemoryBuffer, returning a new MemoryBuffer if successful, otherwise returning null.
LLVM_NODISCARD const char * data() const
data - Get a pointer to the start of the string (which may not be null terminated).
Definition: StringRef.h:122
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
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.
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:48
static ErrorOr< std::unique_ptr< MemoryBuffer > > getOpenFile(int 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.
iterator end() const
Definition: StringRef.h:103