LLVM 20.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"
18#include "llvm/Support/Errc.h"
19#include "llvm/Support/Error.h"
26#include <algorithm>
27#include <cassert>
28#include <cstring>
29#include <new>
30#include <sys/types.h>
31#include <system_error>
32#if !defined(_MSC_VER) && !defined(__MINGW32__)
33#include <unistd.h>
34#else
35#include <io.h>
36#endif
37
38#ifdef __MVS__
40#endif
41using namespace llvm;
42
43//===----------------------------------------------------------------------===//
44// MemoryBuffer implementation itself.
45//===----------------------------------------------------------------------===//
46
48
49/// init - Initialize this MemoryBuffer as a reference to externally allocated
50/// memory, memory that we know is already null terminated.
51void MemoryBuffer::init(const char *BufStart, const char *BufEnd,
52 bool RequiresNullTerminator) {
53 assert((!RequiresNullTerminator || BufEnd[0] == 0) &&
54 "Buffer is not null terminated!");
55 BufferStart = BufStart;
56 BufferEnd = BufEnd;
57}
58
59//===----------------------------------------------------------------------===//
60// MemoryBufferMem implementation.
61//===----------------------------------------------------------------------===//
62
63/// CopyStringRef - Copies contents of a StringRef into a block of memory and
64/// null-terminates it.
65static void CopyStringRef(char *Memory, StringRef Data) {
66 if (!Data.empty())
67 memcpy(Memory, Data.data(), Data.size());
68 Memory[Data.size()] = 0; // Null terminate string.
69}
70
71namespace {
72struct NamedBufferAlloc {
73 const Twine &Name;
74 NamedBufferAlloc(const Twine &Name) : Name(Name) {}
75};
76} // namespace
77
78void *operator new(size_t N, const NamedBufferAlloc &Alloc) {
79 SmallString<256> NameBuf;
80 StringRef NameRef = Alloc.Name.toStringRef(NameBuf);
81
82 // We use malloc() and manually handle it returning null instead of calling
83 // operator new because we need all uses of NamedBufferAlloc to be
84 // deallocated with a call to free() due to needing to use malloc() in
85 // WritableMemoryBuffer::getNewUninitMemBuffer() to work around the out-of-
86 // memory handler installed by default in LLVM. See operator delete() member
87 // functions within this file for the paired call to free().
88 char *Mem =
89 static_cast<char *>(std::malloc(N + sizeof(size_t) + NameRef.size() + 1));
90 if (!Mem)
91 llvm::report_bad_alloc_error("Allocation failed");
92 *reinterpret_cast<size_t *>(Mem + N) = NameRef.size();
93 CopyStringRef(Mem + N + sizeof(size_t), NameRef);
94 return Mem;
95}
96
97namespace {
98/// MemoryBufferMem - Named MemoryBuffer pointing to a block of memory.
99template<typename MB>
100class MemoryBufferMem : public MB {
101public:
102 MemoryBufferMem(StringRef InputData, bool RequiresNullTerminator) {
103 MemoryBuffer::init(InputData.begin(), InputData.end(),
104 RequiresNullTerminator);
105 }
106
107 /// Disable sized deallocation for MemoryBufferMem, because it has
108 /// tail-allocated data.
109 void operator delete(void *p) { std::free(p); }
110
111 StringRef getBufferIdentifier() const override {
112 // The name is stored after the class itself.
113 return StringRef(reinterpret_cast<const char *>(this + 1) + sizeof(size_t),
114 *reinterpret_cast<const size_t *>(this + 1));
115 }
116
117 MemoryBuffer::BufferKind getBufferKind() const override {
119 }
120};
121} // namespace
122
123template <typename MB>
125getFileAux(const Twine &Filename, uint64_t MapSize, uint64_t Offset,
126 bool IsText, bool RequiresNullTerminator, bool IsVolatile,
127 std::optional<Align> Alignment);
128
129std::unique_ptr<MemoryBuffer>
131 bool RequiresNullTerminator) {
132 auto *Ret = new (NamedBufferAlloc(BufferName))
133 MemoryBufferMem<MemoryBuffer>(InputData, RequiresNullTerminator);
134 return std::unique_ptr<MemoryBuffer>(Ret);
135}
136
137std::unique_ptr<MemoryBuffer>
138MemoryBuffer::getMemBuffer(MemoryBufferRef Ref, bool RequiresNullTerminator) {
139 return std::unique_ptr<MemoryBuffer>(getMemBuffer(
140 Ref.getBuffer(), Ref.getBufferIdentifier(), RequiresNullTerminator));
141}
142
144getMemBufferCopyImpl(StringRef InputData, const Twine &BufferName) {
145 auto Buf =
146 WritableMemoryBuffer::getNewUninitMemBuffer(InputData.size(), BufferName);
147 if (!Buf)
149 // Calling memcpy with null src/dst is UB, and an empty StringRef is
150 // represented with {nullptr, 0}.
151 llvm::copy(InputData, Buf->getBufferStart());
152 return std::move(Buf);
153}
154
155std::unique_ptr<MemoryBuffer>
156MemoryBuffer::getMemBufferCopy(StringRef InputData, const Twine &BufferName) {
157 auto Buf = getMemBufferCopyImpl(InputData, BufferName);
158 if (Buf)
159 return std::move(*Buf);
160 return nullptr;
161}
162
164MemoryBuffer::getFileOrSTDIN(const Twine &Filename, bool IsText,
165 bool RequiresNullTerminator,
166 std::optional<Align> Alignment) {
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) {
180 return getFileAux<MemoryBuffer>(FilePath, MapSize, Offset, /*IsText=*/false,
181 /*RequiresNullTerminator=*/false, IsVolatile,
182 Alignment);
183}
184
185//===----------------------------------------------------------------------===//
186// MemoryBuffer::getFile implementation.
187//===----------------------------------------------------------------------===//
188
189namespace {
190
191template <typename MB>
192constexpr sys::fs::mapped_file_region::mapmode Mapmode =
194template <>
195constexpr sys::fs::mapped_file_region::mapmode Mapmode<MemoryBuffer> =
197template <>
198constexpr sys::fs::mapped_file_region::mapmode Mapmode<WritableMemoryBuffer> =
200template <>
202 Mapmode<WriteThroughMemoryBuffer> = sys::fs::mapped_file_region::readwrite;
203
204/// Memory maps a file descriptor using sys::fs::mapped_file_region.
205///
206/// This handles converting the offset into a legal offset on the platform.
207template<typename MB>
208class MemoryBufferMMapFile : public MB {
210
211 static uint64_t getLegalMapOffset(uint64_t Offset) {
213 }
214
215 static uint64_t getLegalMapSize(uint64_t Len, uint64_t Offset) {
216 return Len + (Offset - getLegalMapOffset(Offset));
217 }
218
219 const char *getStart(uint64_t Len, uint64_t Offset) {
220 return MFR.const_data() + (Offset - getLegalMapOffset(Offset));
221 }
222
223public:
224 MemoryBufferMMapFile(bool RequiresNullTerminator, sys::fs::file_t FD, uint64_t Len,
225 uint64_t Offset, std::error_code &EC)
226 : MFR(FD, Mapmode<MB>, getLegalMapSize(Len, Offset),
227 getLegalMapOffset(Offset), EC) {
228 if (!EC) {
229 const char *Start = getStart(Len, Offset);
230 MemoryBuffer::init(Start, Start + Len, RequiresNullTerminator);
231 }
232 }
233
234 /// Disable sized deallocation for MemoryBufferMMapFile, because it has
235 /// tail-allocated data.
236 void operator delete(void *p) { std::free(p); }
237
238 StringRef getBufferIdentifier() const override {
239 // The name is stored after the class itself.
240 return StringRef(reinterpret_cast<const char *>(this + 1) + sizeof(size_t),
241 *reinterpret_cast<const size_t *>(this + 1));
242 }
243
244 MemoryBuffer::BufferKind getBufferKind() const override {
246 }
247
248 void dontNeedIfMmap() override { MFR.dontNeed(); }
249};
250} // namespace
251
255 if (Error E = sys::fs::readNativeFileToEOF(FD, Buffer))
256 return errorToErrorCode(std::move(E));
257 return getMemBufferCopyImpl(Buffer, BufferName);
258}
259
261MemoryBuffer::getFile(const Twine &Filename, bool IsText,
262 bool RequiresNullTerminator, bool IsVolatile,
263 std::optional<Align> Alignment) {
264 return getFileAux<MemoryBuffer>(Filename, /*MapSize=*/-1, /*Offset=*/0,
265 IsText, RequiresNullTerminator, IsVolatile,
266 Alignment);
267}
268
269template <typename MB>
271getOpenFileImpl(sys::fs::file_t FD, const Twine &Filename, uint64_t FileSize,
272 uint64_t MapSize, int64_t Offset, bool RequiresNullTerminator,
273 bool IsVolatile, std::optional<Align> Alignment);
274
275template <typename MB>
277getFileAux(const Twine &Filename, uint64_t MapSize, uint64_t Offset,
278 bool IsText, bool RequiresNullTerminator, bool IsVolatile,
279 std::optional<Align> Alignment) {
281 Filename, IsText ? sys::fs::OF_TextWithCRLF : sys::fs::OF_None);
282 if (!FDOrErr)
283 return errorToErrorCode(FDOrErr.takeError());
284 sys::fs::file_t FD = *FDOrErr;
285 auto Ret = getOpenFileImpl<MB>(FD, Filename, /*FileSize=*/-1, MapSize, Offset,
286 RequiresNullTerminator, IsVolatile, Alignment);
288 return Ret;
289}
290
292WritableMemoryBuffer::getFile(const Twine &Filename, bool IsVolatile,
293 std::optional<Align> Alignment) {
294 return getFileAux<WritableMemoryBuffer>(
295 Filename, /*MapSize=*/-1, /*Offset=*/0, /*IsText=*/false,
296 /*RequiresNullTerminator=*/false, IsVolatile, Alignment);
297}
298
301 uint64_t Offset, bool IsVolatile,
302 std::optional<Align> Alignment) {
303 return getFileAux<WritableMemoryBuffer>(
304 Filename, MapSize, Offset, /*IsText=*/false,
305 /*RequiresNullTerminator=*/false, IsVolatile, Alignment);
306}
307
308std::unique_ptr<WritableMemoryBuffer>
310 const Twine &BufferName,
311 std::optional<Align> Alignment) {
312 using MemBuffer = MemoryBufferMem<WritableMemoryBuffer>;
313
314 // Use 16-byte alignment if no alignment is specified.
315 Align BufAlign = Alignment.value_or(Align(16));
316
317 // Allocate space for the MemoryBuffer, the data and the name. It is important
318 // that MemoryBuffer and data are aligned so PointerIntPair works with them.
319 SmallString<256> NameBuf;
320 StringRef NameRef = BufferName.toStringRef(NameBuf);
321
322 size_t StringLen = sizeof(MemBuffer) + sizeof(size_t) + NameRef.size() + 1;
323 size_t RealLen = StringLen + Size + 1 + BufAlign.value();
324 if (RealLen <= Size) // Check for rollover.
325 return nullptr;
326 // We use a call to malloc() rather than a call to a non-throwing operator
327 // new() because LLVM unconditionally installs an out of memory new handler
328 // when exceptions are disabled. This new handler intentionally crashes to
329 // aid with debugging, but that makes non-throwing new calls unhelpful.
330 // See MemoryBufferMem::operator delete() for the paired call to free(), and
331 // llvm::install_out_of_memory_new_handler() for the installation of the
332 // custom new handler.
333 char *Mem = static_cast<char *>(std::malloc(RealLen));
334 if (!Mem)
335 return nullptr;
336
337 // The name is stored after the class itself.
338 *reinterpret_cast<size_t *>(Mem + sizeof(MemBuffer)) = NameRef.size();
339 CopyStringRef(Mem + sizeof(MemBuffer) + sizeof(size_t), NameRef);
340
341 // The buffer begins after the name and must be aligned.
342 char *Buf = (char *)alignAddr(Mem + StringLen, BufAlign);
343 Buf[Size] = 0; // Null terminate buffer.
344
345 auto *Ret = new (Mem) MemBuffer(StringRef(Buf, Size), true);
346 return std::unique_ptr<WritableMemoryBuffer>(Ret);
347}
348
349std::unique_ptr<WritableMemoryBuffer>
352 if (!SB)
353 return nullptr;
354 memset(SB->getBufferStart(), 0, Size);
355 return SB;
356}
357
359 size_t FileSize,
360 size_t MapSize,
361 off_t Offset,
362 bool RequiresNullTerminator,
363 int PageSize,
364 bool IsVolatile) {
365 // mmap may leave the buffer without null terminator if the file size changed
366 // by the time the last page is mapped in, so avoid it if the file size is
367 // likely to change.
368 if (IsVolatile && RequiresNullTerminator)
369 return false;
370
371 // We don't use mmap for small files because this can severely fragment our
372 // address space.
373 if (MapSize < 4 * 4096 || MapSize < (unsigned)PageSize)
374 return false;
375
376 if (!RequiresNullTerminator)
377 return true;
378
379 // If we don't know the file size, use fstat to find out. fstat on an open
380 // file descriptor is cheaper than stat on a random path.
381 // FIXME: this chunk of code is duplicated, but it avoids a fstat when
382 // RequiresNullTerminator = false and MapSize != -1.
383 if (FileSize == size_t(-1)) {
385 if (sys::fs::status(FD, Status))
386 return false;
387 FileSize = Status.getSize();
388 }
389
390 // If we need a null terminator and the end of the map is inside the file,
391 // we cannot use mmap.
392 size_t End = Offset + MapSize;
393 assert(End <= FileSize);
394 if (End != FileSize)
395 return false;
396
397 // Don't try to map files that are exactly a multiple of the system page size
398 // if we need a null terminator.
399 if ((FileSize & (PageSize -1)) == 0)
400 return false;
401
402#if defined(__CYGWIN__)
403 // Don't try to map files that are exactly a multiple of the physical page size
404 // if we need a null terminator.
405 // FIXME: We should reorganize again getPageSize() on Win32.
406 if ((FileSize & (4096 - 1)) == 0)
407 return false;
408#endif
409
410 return true;
411}
412
414getReadWriteFile(const Twine &Filename, uint64_t FileSize, uint64_t MapSize,
418 if (!FDOrErr)
419 return errorToErrorCode(FDOrErr.takeError());
420 sys::fs::file_t FD = *FDOrErr;
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 mmap it, so error out.
438
439 FileSize = Status.getSize();
440 }
441 MapSize = FileSize;
442 }
443
444 std::error_code EC;
445 std::unique_ptr<WriteThroughMemoryBuffer> Result(
446 new (NamedBufferAlloc(Filename))
447 MemoryBufferMMapFile<WriteThroughMemoryBuffer>(false, FD, MapSize,
448 Offset, EC));
449 if (EC)
450 return EC;
451 return std::move(Result);
452}
453
455WriteThroughMemoryBuffer::getFile(const Twine &Filename, int64_t FileSize) {
456 return getReadWriteFile(Filename, FileSize, FileSize, 0);
457}
458
459/// Map a subrange of the specified file as a WritableMemoryBuffer.
463 return getReadWriteFile(Filename, -1, MapSize, Offset);
464}
465
466template <typename MB>
468getOpenFileImpl(sys::fs::file_t FD, const Twine &Filename, uint64_t FileSize,
469 uint64_t MapSize, int64_t Offset, bool RequiresNullTerminator,
470 bool IsVolatile, std::optional<Align> Alignment) {
472
473 // Default is to map the full file.
474 if (MapSize == uint64_t(-1)) {
475 // If we don't know the file size, use fstat to find out. fstat on an open
476 // file descriptor is cheaper than stat on a random path.
477 if (FileSize == uint64_t(-1)) {
479 std::error_code EC = sys::fs::status(FD, Status);
480 if (EC)
481 return EC;
482
483 // If this not a file or a block device (e.g. it's a named pipe
484 // or character device), we can't trust the size. Create the memory
485 // buffer by copying off the stream.
489 return getMemoryBufferForStream(FD, Filename);
490
491 FileSize = Status.getSize();
492 }
493 MapSize = FileSize;
494 }
495
496 if (shouldUseMmap(FD, FileSize, MapSize, Offset, RequiresNullTerminator,
497 PageSize, IsVolatile)) {
498 std::error_code EC;
499 std::unique_ptr<MB> Result(
500 new (NamedBufferAlloc(Filename)) MemoryBufferMMapFile<MB>(
501 RequiresNullTerminator, FD, MapSize, Offset, EC));
502 if (!EC)
503 return std::move(Result);
504 }
505
506#ifdef __MVS__
507 // Set codepage auto-conversion for z/OS.
508 if (auto EC = llvm::enableAutoConversion(FD))
509 return EC;
510#endif
511
512 auto Buf =
513 WritableMemoryBuffer::getNewUninitMemBuffer(MapSize, Filename, Alignment);
514 if (!Buf) {
515 // Failed to create a buffer. The only way it can fail is if
516 // new(std::nothrow) returns 0.
518 }
519
520 // Read until EOF, zero-initialize the rest.
521 MutableArrayRef<char> ToRead = Buf->getBuffer();
522 while (!ToRead.empty()) {
523 Expected<size_t> ReadBytes =
525 if (!ReadBytes)
526 return errorToErrorCode(ReadBytes.takeError());
527 if (*ReadBytes == 0) {
528 std::memset(ToRead.data(), 0, ToRead.size());
529 break;
530 }
531 ToRead = ToRead.drop_front(*ReadBytes);
532 Offset += *ReadBytes;
533 }
534
535 return std::move(Buf);
536}
537
540 uint64_t FileSize, bool RequiresNullTerminator,
541 bool IsVolatile, std::optional<Align> Alignment) {
542 return getOpenFileImpl<MemoryBuffer>(FD, Filename, FileSize, FileSize, 0,
543 RequiresNullTerminator, IsVolatile,
544 Alignment);
545}
546
548 sys::fs::file_t FD, const Twine &Filename, uint64_t MapSize, int64_t Offset,
549 bool IsVolatile, std::optional<Align> Alignment) {
550 assert(MapSize != uint64_t(-1));
551 return getOpenFileImpl<MemoryBuffer>(FD, Filename, -1, MapSize, Offset, false,
552 IsVolatile, Alignment);
553}
554
556 // Read in all of the data from stdin, we cannot mmap stdin.
557 //
558 // FIXME: That isn't necessarily true, we should try to mmap stdin and
559 // fallback if it fails.
561
563}
564
569 if (!FDOrErr)
570 return errorToErrorCode(FDOrErr.takeError());
571 sys::fs::file_t FD = *FDOrErr;
573 getMemoryBufferForStream(FD, Filename);
575 return Ret;
576}
577
580 StringRef Identifier = getBufferIdentifier();
581 return MemoryBufferRef(Data, Identifier);
582}
583
std::string Name
uint64_t Size
bool End
Definition: ELF_riscv.cpp:480
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 ...
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
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:165
bool empty() const
empty - Check if the array is empty.
Definition: ArrayRef.h:160
Represents either an error or a value T.
Definition: ErrorOr.h:56
Lightweight error class with error context and mandatory checking.
Definition: Error.h:160
Tagged union holding either a T or a Error.
Definition: Error.h:481
Error takeError()
Take ownership of the stored error.
Definition: Error.h:608
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.
Definition: MemoryBuffer.h:165
virtual StringRef getBufferIdentifier() const
Return an identifier for this buffer, typically the filename it was read from.
Definition: MemoryBuffer.h:76
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
Definition: MemoryBuffer.h:70
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:307
T * data() const
Definition: ArrayRef.h:354
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:50
iterator begin() const
Definition: StringRef.h:111
constexpr size_t size() const
size - Get the string size.
Definition: StringRef.h:137
iterator end() const
Definition: StringRef.h:113
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:81
StringRef toStringRef(SmallVectorImpl< char > &Out) const
This returns the twine as a single StringRef if it can be represented as such.
Definition: Twine.h:492
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 > getNewMemBuffer(size_t Size, const Twine &BufferName="")
Allocate a new zero-initialized MemoryBuffer of the specified size.
static ErrorOr< std::unique_ptr< WritableMemoryBuffer > > getFile(const Twine &Filename, bool IsVolatile=false, std::optional< Align > Alignment=std::nullopt)
static 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 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 ErrorOr< std::unique_ptr< WriteThroughMemoryBuffer > > getFile(const Twine &Filename, int64_t FileSize=-1)
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.
This class provides various memory handling functions that manipulate MemoryBlock instances.
Definition: Memory.h:52
static unsigned getPageSizeEstimate()
Get the process's estimated page size.
Definition: Process.h:61
Represents the result of a call to sys::fs::status().
Definition: FileSystem.h:225
This class represents a memory mapped file.
Definition: FileSystem.h:1266
@ priv
May modify via data, but changes are lost on destruction.
Definition: FileSystem.h:1271
@ readonly
May only access map via const_data as read only.
Definition: FileSystem.h:1269
@ readwrite
May access map via data and modify it. Written to path.
Definition: FileSystem.h:1270
const char * const_data() const
Get a const view of the data.
Definition: Path.cpp:1170
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:1175
std::error_code closeFile(file_t &F)
Close the file object.
Expected< size_t > readNativeFileSlice(file_t FileHandle, MutableArrayRef< char > Buf, uint64_t Offset)
Reads Buf.size() bytes from FileHandle at offset Offset into Buf.
std::error_code status(const Twine &path, file_status &result, bool follow=true)
Get file status as if by POSIX stat().
@ OF_Text
The file should be opened in text mode on platforms like z/OS that make this distinction.
Definition: FileSystem.h:758
@ OF_TextWithCRLF
The file should be opened in text mode and use a carriage linefeed '\r '.
Definition: FileSystem.h:767
file_type
An enumeration for the file system's view of the type.
Definition: FileSystem.h:65
@ CD_OpenExisting
CD_OpenExisting - When opening a file:
Definition: FileSystem.h:740
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...
Definition: FileSystem.h:1123
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.
file_t getStdinHandle()
Return an open handle to standard in.
std::error_code ChangeStdinMode(fs::OpenFlags Flags)
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
@ Offset
Definition: DWP.cpp:480
std::error_code make_error_code(BitcodeError E)
@ Ref
The access may reference the value stored in memory.
OutputIt copy(R &&Range, OutputIt Out)
Definition: STLExtras.h:1824
std::error_code errorToErrorCode(Error Err)
Helper for converting an ECError to a std::error_code.
Definition: Error.cpp:117
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:187
#define N
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition: Alignment.h:39
uint64_t value() const
This is a hole in the type system and should not be abused.
Definition: Alignment.h:85