LLVM 22.0.0git
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/STLExtras.h"
16#include "llvm/Config/config.h"
19#include "llvm/Support/Errc.h"
20#include "llvm/Support/Error.h"
27#include <algorithm>
28#include <cassert>
29#include <cstring>
30#include <new>
31#include <sys/types.h>
32#include <system_error>
33#if !defined(_MSC_VER) && !defined(__MINGW32__)
34#include <unistd.h>
35#else
36#include <io.h>
37#endif
38
39using namespace llvm;
40
41//===----------------------------------------------------------------------===//
42// MemoryBuffer implementation itself.
43//===----------------------------------------------------------------------===//
44
46
47/// init - Initialize this MemoryBuffer as a reference to externally allocated
48/// memory, memory that we know is already null terminated.
49void MemoryBuffer::init(const char *BufStart, const char *BufEnd,
50 bool RequiresNullTerminator) {
51 assert((!RequiresNullTerminator || BufEnd[0] == 0) &&
52 "Buffer is not null terminated!");
53 BufferStart = BufStart;
54 BufferEnd = BufEnd;
55}
56
57//===----------------------------------------------------------------------===//
58// MemoryBufferMem implementation.
59//===----------------------------------------------------------------------===//
60
61/// CopyStringRef - Copies contents of a StringRef into a block of memory and
62/// null-terminates it.
63static void CopyStringRef(char *Memory, StringRef Data) {
64 if (!Data.empty())
65 memcpy(Memory, Data.data(), Data.size());
66 Memory[Data.size()] = 0; // Null terminate string.
67}
68
69namespace {
70struct NamedBufferAlloc {
71 const Twine &Name;
72 NamedBufferAlloc(const Twine &Name) : Name(Name) {}
73};
74} // namespace
75
76void *operator new(size_t N, const NamedBufferAlloc &Alloc) {
77 SmallString<256> NameBuf;
78 StringRef NameRef = Alloc.Name.toStringRef(NameBuf);
79
80 // We use malloc() and manually handle it returning null instead of calling
81 // operator new because we need all uses of NamedBufferAlloc to be
82 // deallocated with a call to free() due to needing to use malloc() in
83 // WritableMemoryBuffer::getNewUninitMemBuffer() to work around the out-of-
84 // memory handler installed by default in LLVM. See operator delete() member
85 // functions within this file for the paired call to free().
86 char *Mem =
87 static_cast<char *>(std::malloc(N + sizeof(size_t) + NameRef.size() + 1));
88 if (!Mem)
89 llvm::report_bad_alloc_error("Allocation failed");
90 *reinterpret_cast<size_t *>(Mem + N) = NameRef.size();
91 CopyStringRef(Mem + N + sizeof(size_t), NameRef);
92 return Mem;
93}
94
95namespace {
96/// MemoryBufferMem - Named MemoryBuffer pointing to a block of memory.
97template<typename MB>
98class MemoryBufferMem : public MB {
99public:
100 MemoryBufferMem(StringRef InputData, bool RequiresNullTerminator) {
101 MemoryBuffer::init(InputData.begin(), InputData.end(),
102 RequiresNullTerminator);
103 }
104
105 /// Disable sized deallocation for MemoryBufferMem, because it has
106 /// tail-allocated data.
107 void operator delete(void *p) { std::free(p); }
108
109 StringRef getBufferIdentifier() const override {
110 // The name is stored after the class itself.
111 return StringRef(reinterpret_cast<const char *>(this + 1) + sizeof(size_t),
112 *reinterpret_cast<const size_t *>(this + 1));
113 }
114
115 MemoryBuffer::BufferKind getBufferKind() const override {
117 }
118};
119} // namespace
120
121template <typename MB>
123getFileAux(const Twine &Filename, uint64_t MapSize, uint64_t Offset,
124 bool IsText, bool RequiresNullTerminator, bool IsVolatile,
125 std::optional<Align> Alignment);
126
127std::unique_ptr<MemoryBuffer>
129 bool RequiresNullTerminator) {
130 auto *Ret = new (NamedBufferAlloc(BufferName))
131 MemoryBufferMem<MemoryBuffer>(InputData, RequiresNullTerminator);
132 return std::unique_ptr<MemoryBuffer>(Ret);
133}
134
135std::unique_ptr<MemoryBuffer>
136MemoryBuffer::getMemBuffer(MemoryBufferRef Ref, bool RequiresNullTerminator) {
137 return std::unique_ptr<MemoryBuffer>(getMemBuffer(
138 Ref.getBuffer(), Ref.getBufferIdentifier(), RequiresNullTerminator));
139}
140
142getMemBufferCopyImpl(StringRef InputData, const Twine &BufferName) {
143 auto Buf =
144 WritableMemoryBuffer::getNewUninitMemBuffer(InputData.size(), BufferName);
145 if (!Buf)
147 // Calling memcpy with null src/dst is UB, and an empty StringRef is
148 // represented with {nullptr, 0}.
149 llvm::copy(InputData, Buf->getBufferStart());
150 return std::move(Buf);
151}
152
153std::unique_ptr<MemoryBuffer>
154MemoryBuffer::getMemBufferCopy(StringRef InputData, const Twine &BufferName) {
155 auto Buf = getMemBufferCopyImpl(InputData, BufferName);
156 if (Buf)
157 return std::move(*Buf);
158 return nullptr;
159}
160
162MemoryBuffer::getFileOrSTDIN(const Twine &Filename, bool IsText,
163 bool RequiresNullTerminator,
164 std::optional<Align> Alignment) {
166
167 SmallString<256> NameBuf;
168 StringRef NameRef = Filename.toStringRef(NameBuf);
169
170 if (NameRef == "-")
171 return getSTDIN();
172 return getFile(Filename, IsText, RequiresNullTerminator,
173 /*IsVolatile=*/false, Alignment);
174}
175
178 uint64_t Offset, bool IsVolatile,
179 std::optional<Align> Alignment) {
181
182 return getFileAux<MemoryBuffer>(FilePath, MapSize, Offset, /*IsText=*/false,
183 /*RequiresNullTerminator=*/false, IsVolatile,
184 Alignment);
185}
186
187//===----------------------------------------------------------------------===//
188// MemoryBuffer::getFile implementation.
189//===----------------------------------------------------------------------===//
190
191namespace {
192
193template <typename MB>
194constexpr sys::fs::mapped_file_region::mapmode Mapmode =
196template <>
197constexpr sys::fs::mapped_file_region::mapmode Mapmode<MemoryBuffer> =
199template <>
200constexpr sys::fs::mapped_file_region::mapmode Mapmode<WritableMemoryBuffer> =
202template <>
204 Mapmode<WriteThroughMemoryBuffer> = sys::fs::mapped_file_region::readwrite;
205
206/// Memory maps a file descriptor using sys::fs::mapped_file_region.
207///
208/// This handles converting the offset into a legal offset on the platform.
209template<typename MB>
210class MemoryBufferMMapFile : public MB {
212
213 static uint64_t getLegalMapOffset(uint64_t Offset) {
215 }
216
217 static uint64_t getLegalMapSize(uint64_t Len, uint64_t Offset) {
218 return Len + (Offset - getLegalMapOffset(Offset));
219 }
220
221 const char *getStart(uint64_t Len, uint64_t Offset) {
222 return MFR.const_data() + (Offset - getLegalMapOffset(Offset));
223 }
224
225public:
226 MemoryBufferMMapFile(bool RequiresNullTerminator, sys::fs::file_t FD, uint64_t Len,
227 uint64_t Offset, std::error_code &EC)
228 : MFR(FD, Mapmode<MB>, getLegalMapSize(Len, Offset),
229 getLegalMapOffset(Offset), EC) {
230 if (!EC) {
231 const char *Start = getStart(Len, Offset);
232 MemoryBuffer::init(Start, Start + Len, RequiresNullTerminator);
233 }
234 }
235
236 /// Disable sized deallocation for MemoryBufferMMapFile, because it has
237 /// tail-allocated data.
238 void operator delete(void *p) { std::free(p); }
239
240 StringRef getBufferIdentifier() const override {
241 // The name is stored after the class itself.
242 return StringRef(reinterpret_cast<const char *>(this + 1) + sizeof(size_t),
243 *reinterpret_cast<const size_t *>(this + 1));
244 }
245
246 MemoryBuffer::BufferKind getBufferKind() const override {
248 }
249
250 void dontNeedIfMmap() override { MFR.dontNeed(); }
251};
252} // namespace
253
257 if (Error E = sys::fs::readNativeFileToEOF(FD, Buffer))
258 return errorToErrorCode(std::move(E));
259 return getMemBufferCopyImpl(Buffer, BufferName);
260}
261
263MemoryBuffer::getFile(const Twine &Filename, bool IsText,
264 bool RequiresNullTerminator, bool IsVolatile,
265 std::optional<Align> Alignment) {
267
268 return getFileAux<MemoryBuffer>(Filename, /*MapSize=*/-1, /*Offset=*/0,
269 IsText, RequiresNullTerminator, IsVolatile,
270 Alignment);
271}
272
273template <typename MB>
275getOpenFileImpl(sys::fs::file_t FD, const Twine &Filename, uint64_t FileSize,
276 uint64_t MapSize, int64_t Offset, bool RequiresNullTerminator,
277 bool IsVolatile, std::optional<Align> Alignment);
278
279template <typename MB>
281getFileAux(const Twine &Filename, uint64_t MapSize, uint64_t Offset,
282 bool IsText, bool RequiresNullTerminator, bool IsVolatile,
283 std::optional<Align> Alignment) {
285 Filename, IsText ? sys::fs::OF_TextWithCRLF : sys::fs::OF_None);
286 if (!FDOrErr)
287 return errorToErrorCode(FDOrErr.takeError());
288 sys::fs::file_t FD = *FDOrErr;
289 auto Ret = getOpenFileImpl<MB>(FD, Filename, /*FileSize=*/-1, MapSize, Offset,
290 RequiresNullTerminator, IsVolatile, Alignment);
292 return Ret;
293}
294
296WritableMemoryBuffer::getFile(const Twine &Filename, bool IsVolatile,
297 std::optional<Align> Alignment) {
299
301 Filename, /*MapSize=*/-1, /*Offset=*/0, /*IsText=*/false,
302 /*RequiresNullTerminator=*/false, IsVolatile, Alignment);
303}
304
307 uint64_t Offset, bool IsVolatile,
308 std::optional<Align> Alignment) {
310
312 Filename, MapSize, Offset, /*IsText=*/false,
313 /*RequiresNullTerminator=*/false, IsVolatile, Alignment);
314}
315
316std::unique_ptr<WritableMemoryBuffer>
318 const Twine &BufferName,
319 std::optional<Align> Alignment) {
320 using MemBuffer = MemoryBufferMem<WritableMemoryBuffer>;
321
322 // Use 16-byte alignment if no alignment is specified.
323 Align BufAlign = Alignment.value_or(Align(16));
324
325 // Allocate space for the MemoryBuffer, the data and the name. It is important
326 // that MemoryBuffer and data are aligned so PointerIntPair works with them.
327 SmallString<256> NameBuf;
328 StringRef NameRef = BufferName.toStringRef(NameBuf);
329
330 size_t StringLen = sizeof(MemBuffer) + sizeof(size_t) + NameRef.size() + 1;
331 size_t RealLen = StringLen + Size + 1 + BufAlign.value();
332 if (RealLen <= Size) // Check for rollover.
333 return nullptr;
334 // We use a call to malloc() rather than a call to a non-throwing operator
335 // new() because LLVM unconditionally installs an out of memory new handler
336 // when exceptions are disabled. This new handler intentionally crashes to
337 // aid with debugging, but that makes non-throwing new calls unhelpful.
338 // See MemoryBufferMem::operator delete() for the paired call to free(), and
339 // llvm::install_out_of_memory_new_handler() for the installation of the
340 // custom new handler.
341 char *Mem = static_cast<char *>(std::malloc(RealLen));
342 if (!Mem)
343 return nullptr;
344
345 // The name is stored after the class itself.
346 *reinterpret_cast<size_t *>(Mem + sizeof(MemBuffer)) = NameRef.size();
347 CopyStringRef(Mem + sizeof(MemBuffer) + sizeof(size_t), NameRef);
348
349 // The buffer begins after the name and must be aligned.
350 char *Buf = (char *)alignAddr(Mem + StringLen, BufAlign);
351 Buf[Size] = 0; // Null terminate buffer.
352
353 auto *Ret = new (Mem) MemBuffer(StringRef(Buf, Size), true);
354 return std::unique_ptr<WritableMemoryBuffer>(Ret);
355}
356
357std::unique_ptr<WritableMemoryBuffer>
360 if (!SB)
361 return nullptr;
362 memset(SB->getBufferStart(), 0, Size);
363 return SB;
364}
365
367 size_t FileSize,
368 size_t MapSize,
369 off_t Offset,
370 bool RequiresNullTerminator,
371 int PageSize,
372 bool IsVolatile) {
373#if defined(__MVS__)
374 // zOS Enhanced ASCII auto convert does not support mmap.
375 return false;
376#endif
377
378 // mmap may leave the buffer without null terminator if the file size changed
379 // by the time the last page is mapped in, so avoid it if the file size is
380 // likely to change.
381 if (IsVolatile && RequiresNullTerminator)
382 return false;
383
384 // We don't use mmap for small files because this can severely fragment our
385 // address space.
386 if (MapSize < 4 * 4096 || MapSize < (unsigned)PageSize)
387 return false;
388
389 if (!RequiresNullTerminator)
390 return true;
391
392 // If we don't know the file size, use fstat to find out. fstat on an open
393 // file descriptor is cheaper than stat on a random path.
394 // FIXME: this chunk of code is duplicated, but it avoids a fstat when
395 // RequiresNullTerminator = false and MapSize != -1.
396 if (FileSize == size_t(-1)) {
398 if (sys::fs::status(FD, Status))
399 return false;
400 FileSize = Status.getSize();
401 }
402
403 // If we need a null terminator and the end of the map is inside the file,
404 // we cannot use mmap.
405 size_t End = Offset + MapSize;
406 assert(End <= FileSize);
407 if (End != FileSize)
408 return false;
409
410 // Don't try to map files that are exactly a multiple of the system page size
411 // if we need a null terminator.
412 if ((FileSize & (PageSize -1)) == 0)
413 return false;
414
415#if defined(__CYGWIN__)
416 // Don't try to map files that are exactly a multiple of the physical page size
417 // if we need a null terminator.
418 // FIXME: We should reorganize again getPageSize() on Win32.
419 if ((FileSize & (4096 - 1)) == 0)
420 return false;
421#endif
422
423 return true;
424}
425
427getReadWriteFile(const Twine &Filename, uint64_t FileSize, uint64_t MapSize,
431 if (!FDOrErr)
432 return errorToErrorCode(FDOrErr.takeError());
433 sys::fs::file_t FD = *FDOrErr;
434
435 // Default is to map the full file.
436 if (MapSize == uint64_t(-1)) {
437 // If we don't know the file size, use fstat to find out. fstat on an open
438 // file descriptor is cheaper than stat on a random path.
439 if (FileSize == uint64_t(-1)) {
441 std::error_code EC = sys::fs::status(FD, Status);
442 if (EC)
443 return EC;
444
445 // If this not a file or a block device (e.g. it's a named pipe
446 // or character device), we can't mmap it, so error out.
451
452 FileSize = Status.getSize();
453 }
454 MapSize = FileSize;
455 }
456
457 std::error_code EC;
458 std::unique_ptr<WriteThroughMemoryBuffer> Result(
459 new (NamedBufferAlloc(Filename))
460 MemoryBufferMMapFile<WriteThroughMemoryBuffer>(false, FD, MapSize,
461 Offset, EC));
462 if (EC)
463 return EC;
464 return std::move(Result);
465}
466
468WriteThroughMemoryBuffer::getFile(const Twine &Filename, int64_t FileSize) {
470
471 return getReadWriteFile(Filename, FileSize, FileSize, 0);
472}
473
474/// Map a subrange of the specified file as a WritableMemoryBuffer.
479
480 return getReadWriteFile(Filename, -1, MapSize, Offset);
481}
482
483template <typename MB>
485getOpenFileImpl(sys::fs::file_t FD, const Twine &Filename, uint64_t FileSize,
486 uint64_t MapSize, int64_t Offset, bool RequiresNullTerminator,
487 bool IsVolatile, std::optional<Align> Alignment) {
489
490 // Default is to map the full file.
491 if (MapSize == uint64_t(-1)) {
492 // If we don't know the file size, use fstat to find out. fstat on an open
493 // file descriptor is cheaper than stat on a random path.
494 if (FileSize == uint64_t(-1)) {
496 std::error_code EC = sys::fs::status(FD, Status);
497 if (EC)
498 return EC;
499
500 // If this not a file or a block device (e.g. it's a named pipe
501 // or character device), we can't trust the size. Create the memory
502 // buffer by copying off the stream.
506 return getMemoryBufferForStream(FD, Filename);
507
508 FileSize = Status.getSize();
509 }
510 MapSize = FileSize;
511 }
512
513 if (shouldUseMmap(FD, FileSize, MapSize, Offset, RequiresNullTerminator,
514 PageSize, IsVolatile)) {
515 std::error_code EC;
516 std::unique_ptr<MB> Result(
517 new (NamedBufferAlloc(Filename)) MemoryBufferMMapFile<MB>(
518 RequiresNullTerminator, FD, MapSize, Offset, EC));
519 if (!EC) {
520 // On at least Linux, and possibly on other systems, mmap may return pages
521 // from the page cache that are not properly filled with trailing zeroes,
522 // if some prior user of the page wrote non-zero bytes. Detect this and
523 // don't use mmap in that case.
524 if (!RequiresNullTerminator || *Result->getBufferEnd() == '\0')
525 return std::move(Result);
526 }
527 }
528
529#ifdef __MVS__
530 ErrorOr<bool> NeedsConversion = needConversion(Filename, FD);
531 if (std::error_code EC = NeedsConversion.getError())
532 return EC;
533 // File size may increase due to EBCDIC -> UTF-8 conversion, therefore we
534 // cannot trust the file size and we create the memory buffer by copying
535 // off the stream.
536 // Note: This only works with the assumption of reading a full file (i.e,
537 // Offset == 0 and MapSize == FileSize). Reading a file slice does not work.
538 if (*NeedsConversion && Offset == 0 && MapSize == FileSize)
539 return getMemoryBufferForStream(FD, Filename);
540#endif
541
542 auto Buf =
543 WritableMemoryBuffer::getNewUninitMemBuffer(MapSize, Filename, Alignment);
544 if (!Buf) {
545 // Failed to create a buffer. The only way it can fail is if
546 // new(std::nothrow) returns 0.
548 }
549
550 // Read until EOF, zero-initialize the rest.
551 MutableArrayRef<char> ToRead = Buf->getBuffer();
552 while (!ToRead.empty()) {
553 Expected<size_t> ReadBytes =
555 if (!ReadBytes)
556 return errorToErrorCode(ReadBytes.takeError());
557 if (*ReadBytes == 0) {
558 std::memset(ToRead.data(), 0, ToRead.size());
559 break;
560 }
561 ToRead = ToRead.drop_front(*ReadBytes);
562 Offset += *ReadBytes;
563 }
564
565 return std::move(Buf);
566}
567
570 uint64_t FileSize, bool RequiresNullTerminator,
571 bool IsVolatile, std::optional<Align> Alignment) {
573
574 return getOpenFileImpl<MemoryBuffer>(FD, Filename, FileSize, FileSize, 0,
575 RequiresNullTerminator, IsVolatile,
576 Alignment);
577}
578
580 sys::fs::file_t FD, const Twine &Filename, uint64_t MapSize, int64_t Offset,
581 bool IsVolatile, std::optional<Align> Alignment) {
582 assert(MapSize != uint64_t(-1));
583
585
586 return getOpenFileImpl<MemoryBuffer>(FD, Filename, -1, MapSize, Offset, false,
587 IsVolatile, Alignment);
588}
589
592
593 // Read in all of the data from stdin, we cannot mmap stdin.
594 //
595 // FIXME: That isn't necessarily true, we should try to mmap stdin and
596 // fallback if it fails.
598
600}
601
605
608 if (!FDOrErr)
609 return errorToErrorCode(FDOrErr.takeError());
610 sys::fs::file_t FD = *FDOrErr;
612 getMemoryBufferForStream(FD, Filename);
614 return Ret;
615}
616
619 StringRef Identifier = getBufferIdentifier();
620 return MemoryBufferRef(Data, Identifier);
621}
622
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
AMDGPU Prepare AGPR Alloc
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
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 bool shouldUseMmap(sys::fs::file_t FD, size_t FileSize, size_t MapSize, off_t Offset, bool RequiresNullTerminator, int PageSize, bool IsVolatile)
static ErrorOr< std::unique_ptr< MB > > getFileAux(const Twine &Filename, uint64_t MapSize, uint64_t Offset, bool IsText, bool RequiresNullTerminator, bool IsVolatile, std::optional< Align > Alignment)
static ErrorOr< std::unique_ptr< WriteThroughMemoryBuffer > > getReadWriteFile(const Twine &Filename, uint64_t FileSize, uint64_t MapSize, uint64_t Offset)
static ErrorOr< std::unique_ptr< MB > > getOpenFileImpl(sys::fs::file_t FD, const Twine &Filename, uint64_t FileSize, uint64_t MapSize, int64_t Offset, bool RequiresNullTerminator, bool IsVolatile, std::optional< Align > Alignment)
static ErrorOr< std::unique_ptr< WritableMemoryBuffer > > getMemBufferCopyImpl(StringRef InputData, const Twine &BufferName)
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< WritableMemoryBuffer > > getMemoryBufferForStream(sys::fs::file_t FD, const Twine &BufferName)
Provides a library for accessing information about this process and other processes on the operating ...
This file contains some templates that are useful if you are working with the STL at all.
This file defines the SmallString class.
size_t size() const
size - Get the array size.
Definition ArrayRef.h:142
bool empty() const
empty - Check if the array is empty.
Definition ArrayRef.h:137
Represents either an error or a value T.
Definition ErrorOr.h:56
std::error_code getError() const
Definition ErrorOr.h:152
Lightweight error class with error context and mandatory checking.
Definition Error.h:159
Tagged union holding either a T or a Error.
Definition Error.h:485
Error takeError()
Take ownership of the stored error.
Definition Error.h:612
static ErrorOr< std::unique_ptr< MemoryBuffer > > getOpenFile(sys::fs::file_t FD, const Twine &Filename, uint64_t FileSize, bool RequiresNullTerminator=true, bool IsVolatile=false, std::optional< Align > Alignment=std::nullopt)
Given an already-open file descriptor, read the file and return a MemoryBuffer.
static std::unique_ptr< MemoryBuffer > getMemBuffer(StringRef InputData, StringRef BufferName="", bool RequiresNullTerminator=true)
Open the specified memory range as a MemoryBuffer.
BufferKind
The kind of memory backing used to support the MemoryBuffer.
virtual StringRef getBufferIdentifier() const
Return an identifier for this buffer, typically the filename it was read from.
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 ErrorOr< std::unique_ptr< MemoryBuffer > > getOpenFileSlice(sys::fs::file_t FD, const Twine &Filename, uint64_t MapSize, int64_t Offset, bool IsVolatile=false, std::optional< Align > Alignment=std::nullopt)
Given an already-open file descriptor, map some slice of it into a MemoryBuffer.
void init(const char *BufStart, const char *BufEnd, bool RequiresNullTerminator)
init - Initialize this MemoryBuffer as a reference to externally allocated memory,...
StringRef getBuffer() const
static ErrorOr< std::unique_ptr< MemoryBuffer > > getFileAsStream(const Twine &Filename)
Read all of the specified file into a MemoryBuffer as a stream (i.e.
MemoryBufferRef getMemBufferRef() const
virtual ~MemoryBuffer()
static ErrorOr< std::unique_ptr< MemoryBuffer > > getFileOrSTDIN(const Twine &Filename, bool IsText=false, bool RequiresNullTerminator=true, std::optional< Align > Alignment=std::nullopt)
Open the specified file as a MemoryBuffer, or open stdin if the Filename is "-".
static ErrorOr< std::unique_ptr< MemoryBuffer > > getFileSlice(const Twine &Filename, uint64_t MapSize, uint64_t Offset, bool IsVolatile=false, std::optional< Align > Alignment=std::nullopt)
Map a subrange of the specified file as a MemoryBuffer.
static ErrorOr< std::unique_ptr< MemoryBuffer > > getFile(const Twine &Filename, bool IsText=false, bool RequiresNullTerminator=true, bool IsVolatile=false, std::optional< Align > Alignment=std::nullopt)
Open the specified file as a MemoryBuffer, returning a new MemoryBuffer if successful,...
static ErrorOr< std::unique_ptr< MemoryBuffer > > getSTDIN()
Read all of stdin into a file buffer, and return it.
MutableArrayRef - Represent a mutable reference to an array (0 or more elements consecutively in memo...
Definition ArrayRef.h:298
MutableArrayRef< T > drop_front(size_t N=1) const
Drop the first N elements of the array.
Definition ArrayRef.h:387
SmallString - A SmallString is just a SmallVector with methods and accessors that make it work better...
Definition SmallString.h:26
StringRef - Represent a constant reference to a string, i.e.
Definition StringRef.h:55
iterator begin() const
Definition StringRef.h:112
constexpr size_t size() const
size - Get the string size.
Definition StringRef.h:146
iterator end() const
Definition StringRef.h:114
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition Twine.h:82
StringRef toStringRef(SmallVectorImpl< char > &Out) const
This returns the twine as a single StringRef if it can be represented as such.
Definition Twine.h:461
The instances of the Type class are immutable: once they are created, they are never changed.
Definition Type.h:45
static LLVM_ABI std::unique_ptr< WritableMemoryBuffer > getNewMemBuffer(size_t Size, const Twine &BufferName="")
Allocate a new zero-initialized MemoryBuffer of the specified size.
static LLVM_ABI ErrorOr< std::unique_ptr< WritableMemoryBuffer > > getFile(const Twine &Filename, bool IsVolatile=false, std::optional< Align > Alignment=std::nullopt)
static LLVM_ABI std::unique_ptr< WritableMemoryBuffer > getNewUninitMemBuffer(size_t Size, const Twine &BufferName="", std::optional< Align > Alignment=std::nullopt)
Allocate a new MemoryBuffer of the specified size that is not initialized.
static LLVM_ABI ErrorOr< std::unique_ptr< WritableMemoryBuffer > > getFileSlice(const Twine &Filename, uint64_t MapSize, uint64_t Offset, bool IsVolatile=false, std::optional< Align > Alignment=std::nullopt)
Map a subrange of the specified file as a WritableMemoryBuffer.
static LLVM_ABI ErrorOr< std::unique_ptr< WriteThroughMemoryBuffer > > getFile(const Twine &Filename, int64_t FileSize=-1)
static LLVM_ABI 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.
This class provides various memory handling functions that manipulate MemoryBlock instances.
Definition Memory.h:54
static unsigned getPageSizeEstimate()
Get the process's estimated page size.
Definition Process.h:62
Represents the result of a call to sys::fs::status().
Definition FileSystem.h:222
This class represents a memory mapped file.
static LLVM_ABI int alignment()
@ priv
May modify via data, but changes are lost on destruction.
@ readonly
May only access map via const_data as read only.
@ readwrite
May access map via data and modify it. Written to path.
LLVM_ABI const char * const_data() const
Get a const view of the data.
Definition Path.cpp:1188
LLVM_ABI std::error_code closeFile(file_t &F)
Close the file object.
LLVM_ABI Error readNativeFileToEOF(file_t FileHandle, SmallVectorImpl< char > &Buffer, ssize_t ChunkSize=DefaultReadChunkSize)
Reads from FileHandle until EOF, appending to Buffer in chunks of size ChunkSize.
Definition Path.cpp:1193
@ OF_Text
The file should be opened in text mode on platforms like z/OS that make this distinction.
Definition FileSystem.h:755
@ OF_TextWithCRLF
The file should be opened in text mode and use a carriage linefeed '\r '.
Definition FileSystem.h:764
file_type
An enumeration for the file system's view of the type.
Definition FileSystem.h:62
@ CD_OpenExisting
CD_OpenExisting - When opening a file:
Definition FileSystem.h:737
LLVM_ABI Expected< size_t > readNativeFileSlice(file_t FileHandle, MutableArrayRef< char > Buf, uint64_t Offset)
Reads Buf.size() bytes from FileHandle at offset Offset into Buf.
Expected< file_t > openNativeFileForReadWrite(const Twine &Name, 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...
LLVM_ABI Expected< file_t > openNativeFileForRead(const Twine &Name, 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.
LLVM_ABI std::error_code status(const Twine &path, file_status &result, bool follow=true)
Get file status as if by POSIX stat().
LLVM_ABI file_t getStdinHandle()
Return an open handle to standard in.
void violationIfEnabled()
Definition IOSandbox.h:37
LLVM_ABI std::error_code ChangeStdinMode(fs::OpenFlags Flags)
This is an optimization pass for GlobalISel generic memory operations.
@ Offset
Definition DWP.cpp:532
std::error_code make_error_code(BitcodeError E)
@ not_enough_memory
Definition Errc.h:68
@ invalid_argument
Definition Errc.h:56
@ Ref
The access may reference the value stored in memory.
Definition ModRef.h:32
FunctionAddr VTableAddr uintptr_t uintptr_t Data
Definition InstrProf.h:189
OutputIt copy(R &&Range, OutputIt Out)
Definition STLExtras.h:1847
LLVM_ABI std::error_code errorToErrorCode(Error Err)
Helper for converting an ECError to a std::error_code.
Definition Error.cpp:117
LLVM_ABI void report_bad_alloc_error(const char *Reason, bool GenCrashDiag=true)
Reports a bad alloc error, calling any user defined bad alloc error handler.
uintptr_t alignAddr(const void *Addr, Align Alignment)
Aligns Addr to Alignment bytes, rounding up.
Definition Alignment.h:176
#define N
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition Alignment.h:39
constexpr uint64_t value() const
This is a hole in the type system and should not be abused.
Definition Alignment.h:77