LLVM 19.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 char *Mem = static_cast<char *>(operator new(N + sizeof(size_t) +
83 NameRef.size() + 1));
84 *reinterpret_cast<size_t *>(Mem + N) = NameRef.size();
85 CopyStringRef(Mem + N + sizeof(size_t), NameRef);
86 return Mem;
87}
88
89namespace {
90/// MemoryBufferMem - Named MemoryBuffer pointing to a block of memory.
91template<typename MB>
92class MemoryBufferMem : public MB {
93public:
94 MemoryBufferMem(StringRef InputData, bool RequiresNullTerminator) {
95 MemoryBuffer::init(InputData.begin(), InputData.end(),
96 RequiresNullTerminator);
97 }
98
99 /// Disable sized deallocation for MemoryBufferMem, because it has
100 /// tail-allocated data.
101 void operator delete(void *p) { ::operator delete(p); }
102
103 StringRef getBufferIdentifier() const override {
104 // The name is stored after the class itself.
105 return StringRef(reinterpret_cast<const char *>(this + 1) + sizeof(size_t),
106 *reinterpret_cast<const size_t *>(this + 1));
107 }
108
109 MemoryBuffer::BufferKind getBufferKind() const override {
111 }
112};
113} // namespace
114
115template <typename MB>
117getFileAux(const Twine &Filename, uint64_t MapSize, uint64_t Offset,
118 bool IsText, bool RequiresNullTerminator, bool IsVolatile,
119 std::optional<Align> Alignment);
120
121std::unique_ptr<MemoryBuffer>
123 bool RequiresNullTerminator) {
124 auto *Ret = new (NamedBufferAlloc(BufferName))
125 MemoryBufferMem<MemoryBuffer>(InputData, RequiresNullTerminator);
126 return std::unique_ptr<MemoryBuffer>(Ret);
127}
128
129std::unique_ptr<MemoryBuffer>
130MemoryBuffer::getMemBuffer(MemoryBufferRef Ref, bool RequiresNullTerminator) {
131 return std::unique_ptr<MemoryBuffer>(getMemBuffer(
132 Ref.getBuffer(), Ref.getBufferIdentifier(), RequiresNullTerminator));
133}
134
136getMemBufferCopyImpl(StringRef InputData, const Twine &BufferName) {
137 auto Buf =
138 WritableMemoryBuffer::getNewUninitMemBuffer(InputData.size(), BufferName);
139 if (!Buf)
141 // Calling memcpy with null src/dst is UB, and an empty StringRef is
142 // represented with {nullptr, 0}.
143 llvm::copy(InputData, Buf->getBufferStart());
144 return std::move(Buf);
145}
146
147std::unique_ptr<MemoryBuffer>
148MemoryBuffer::getMemBufferCopy(StringRef InputData, const Twine &BufferName) {
149 auto Buf = getMemBufferCopyImpl(InputData, BufferName);
150 if (Buf)
151 return std::move(*Buf);
152 return nullptr;
153}
154
156MemoryBuffer::getFileOrSTDIN(const Twine &Filename, bool IsText,
157 bool RequiresNullTerminator,
158 std::optional<Align> Alignment) {
159 SmallString<256> NameBuf;
160 StringRef NameRef = Filename.toStringRef(NameBuf);
161
162 if (NameRef == "-")
163 return getSTDIN();
164 return getFile(Filename, IsText, RequiresNullTerminator,
165 /*IsVolatile=*/false, Alignment);
166}
167
170 uint64_t Offset, bool IsVolatile,
171 std::optional<Align> Alignment) {
172 return getFileAux<MemoryBuffer>(FilePath, MapSize, Offset, /*IsText=*/false,
173 /*RequiresNullTerminator=*/false, IsVolatile,
174 Alignment);
175}
176
177//===----------------------------------------------------------------------===//
178// MemoryBuffer::getFile implementation.
179//===----------------------------------------------------------------------===//
180
181namespace {
182
183template <typename MB>
184constexpr sys::fs::mapped_file_region::mapmode Mapmode =
186template <>
187constexpr sys::fs::mapped_file_region::mapmode Mapmode<MemoryBuffer> =
189template <>
190constexpr sys::fs::mapped_file_region::mapmode Mapmode<WritableMemoryBuffer> =
192template <>
194 Mapmode<WriteThroughMemoryBuffer> = sys::fs::mapped_file_region::readwrite;
195
196/// Memory maps a file descriptor using sys::fs::mapped_file_region.
197///
198/// This handles converting the offset into a legal offset on the platform.
199template<typename MB>
200class MemoryBufferMMapFile : public MB {
202
203 static uint64_t getLegalMapOffset(uint64_t Offset) {
205 }
206
207 static uint64_t getLegalMapSize(uint64_t Len, uint64_t Offset) {
208 return Len + (Offset - getLegalMapOffset(Offset));
209 }
210
211 const char *getStart(uint64_t Len, uint64_t Offset) {
212 return MFR.const_data() + (Offset - getLegalMapOffset(Offset));
213 }
214
215public:
216 MemoryBufferMMapFile(bool RequiresNullTerminator, sys::fs::file_t FD, uint64_t Len,
217 uint64_t Offset, std::error_code &EC)
218 : MFR(FD, Mapmode<MB>, getLegalMapSize(Len, Offset),
219 getLegalMapOffset(Offset), EC) {
220 if (!EC) {
221 const char *Start = getStart(Len, Offset);
222 MemoryBuffer::init(Start, Start + Len, RequiresNullTerminator);
223 }
224 }
225
226 /// Disable sized deallocation for MemoryBufferMMapFile, because it has
227 /// tail-allocated data.
228 void operator delete(void *p) { ::operator delete(p); }
229
230 StringRef getBufferIdentifier() const override {
231 // The name is stored after the class itself.
232 return StringRef(reinterpret_cast<const char *>(this + 1) + sizeof(size_t),
233 *reinterpret_cast<const size_t *>(this + 1));
234 }
235
236 MemoryBuffer::BufferKind getBufferKind() const override {
238 }
239
240 void dontNeedIfMmap() override { MFR.dontNeed(); }
241};
242} // namespace
243
247 if (Error E = sys::fs::readNativeFileToEOF(FD, Buffer))
248 return errorToErrorCode(std::move(E));
249 return getMemBufferCopyImpl(Buffer, BufferName);
250}
251
253MemoryBuffer::getFile(const Twine &Filename, bool IsText,
254 bool RequiresNullTerminator, bool IsVolatile,
255 std::optional<Align> Alignment) {
256 return getFileAux<MemoryBuffer>(Filename, /*MapSize=*/-1, /*Offset=*/0,
257 IsText, RequiresNullTerminator, IsVolatile,
258 Alignment);
259}
260
261template <typename MB>
263getOpenFileImpl(sys::fs::file_t FD, const Twine &Filename, uint64_t FileSize,
264 uint64_t MapSize, int64_t Offset, bool RequiresNullTerminator,
265 bool IsVolatile, std::optional<Align> Alignment);
266
267template <typename MB>
269getFileAux(const Twine &Filename, uint64_t MapSize, uint64_t Offset,
270 bool IsText, bool RequiresNullTerminator, bool IsVolatile,
271 std::optional<Align> Alignment) {
273 Filename, IsText ? sys::fs::OF_TextWithCRLF : sys::fs::OF_None);
274 if (!FDOrErr)
275 return errorToErrorCode(FDOrErr.takeError());
276 sys::fs::file_t FD = *FDOrErr;
277 auto Ret = getOpenFileImpl<MB>(FD, Filename, /*FileSize=*/-1, MapSize, Offset,
278 RequiresNullTerminator, IsVolatile, Alignment);
280 return Ret;
281}
282
284WritableMemoryBuffer::getFile(const Twine &Filename, bool IsVolatile,
285 std::optional<Align> Alignment) {
286 return getFileAux<WritableMemoryBuffer>(
287 Filename, /*MapSize=*/-1, /*Offset=*/0, /*IsText=*/false,
288 /*RequiresNullTerminator=*/false, IsVolatile, Alignment);
289}
290
293 uint64_t Offset, bool IsVolatile,
294 std::optional<Align> Alignment) {
295 return getFileAux<WritableMemoryBuffer>(
296 Filename, MapSize, Offset, /*IsText=*/false,
297 /*RequiresNullTerminator=*/false, IsVolatile, Alignment);
298}
299
300std::unique_ptr<WritableMemoryBuffer>
302 const Twine &BufferName,
303 std::optional<Align> Alignment) {
304 using MemBuffer = MemoryBufferMem<WritableMemoryBuffer>;
305
306 // Use 16-byte alignment if no alignment is specified.
307 Align BufAlign = Alignment.value_or(Align(16));
308
309 // Allocate space for the MemoryBuffer, the data and the name. It is important
310 // that MemoryBuffer and data are aligned so PointerIntPair works with them.
311 SmallString<256> NameBuf;
312 StringRef NameRef = BufferName.toStringRef(NameBuf);
313
314 size_t StringLen = sizeof(MemBuffer) + sizeof(size_t) + NameRef.size() + 1;
315 size_t RealLen = StringLen + Size + 1 + BufAlign.value();
316 if (RealLen <= Size) // Check for rollover.
317 return nullptr;
318 char *Mem = static_cast<char*>(operator new(RealLen, std::nothrow));
319 if (!Mem)
320 return nullptr;
321
322 // The name is stored after the class itself.
323 *reinterpret_cast<size_t *>(Mem + sizeof(MemBuffer)) = NameRef.size();
324 CopyStringRef(Mem + sizeof(MemBuffer) + sizeof(size_t), NameRef);
325
326 // The buffer begins after the name and must be aligned.
327 char *Buf = (char *)alignAddr(Mem + StringLen, BufAlign);
328 Buf[Size] = 0; // Null terminate buffer.
329
330 auto *Ret = new (Mem) MemBuffer(StringRef(Buf, Size), true);
331 return std::unique_ptr<WritableMemoryBuffer>(Ret);
332}
333
334std::unique_ptr<WritableMemoryBuffer>
337 if (!SB)
338 return nullptr;
339 memset(SB->getBufferStart(), 0, Size);
340 return SB;
341}
342
344 size_t FileSize,
345 size_t MapSize,
346 off_t Offset,
347 bool RequiresNullTerminator,
348 int PageSize,
349 bool IsVolatile) {
350 // mmap may leave the buffer without null terminator if the file size changed
351 // by the time the last page is mapped in, so avoid it if the file size is
352 // likely to change.
353 if (IsVolatile && RequiresNullTerminator)
354 return false;
355
356 // We don't use mmap for small files because this can severely fragment our
357 // address space.
358 if (MapSize < 4 * 4096 || MapSize < (unsigned)PageSize)
359 return false;
360
361 if (!RequiresNullTerminator)
362 return true;
363
364 // If we don't know the file size, use fstat to find out. fstat on an open
365 // file descriptor is cheaper than stat on a random path.
366 // FIXME: this chunk of code is duplicated, but it avoids a fstat when
367 // RequiresNullTerminator = false and MapSize != -1.
368 if (FileSize == size_t(-1)) {
370 if (sys::fs::status(FD, Status))
371 return false;
372 FileSize = Status.getSize();
373 }
374
375 // If we need a null terminator and the end of the map is inside the file,
376 // we cannot use mmap.
377 size_t End = Offset + MapSize;
378 assert(End <= FileSize);
379 if (End != FileSize)
380 return false;
381
382 // Don't try to map files that are exactly a multiple of the system page size
383 // if we need a null terminator.
384 if ((FileSize & (PageSize -1)) == 0)
385 return false;
386
387#if defined(__CYGWIN__)
388 // Don't try to map files that are exactly a multiple of the physical page size
389 // if we need a null terminator.
390 // FIXME: We should reorganize again getPageSize() on Win32.
391 if ((FileSize & (4096 - 1)) == 0)
392 return false;
393#endif
394
395 return true;
396}
397
399getReadWriteFile(const Twine &Filename, uint64_t FileSize, uint64_t MapSize,
403 if (!FDOrErr)
404 return errorToErrorCode(FDOrErr.takeError());
405 sys::fs::file_t FD = *FDOrErr;
406
407 // Default is to map the full file.
408 if (MapSize == uint64_t(-1)) {
409 // If we don't know the file size, use fstat to find out. fstat on an open
410 // file descriptor is cheaper than stat on a random path.
411 if (FileSize == uint64_t(-1)) {
413 std::error_code EC = sys::fs::status(FD, Status);
414 if (EC)
415 return EC;
416
417 // If this not a file or a block device (e.g. it's a named pipe
418 // or character device), we can't mmap it, so error out.
423
424 FileSize = Status.getSize();
425 }
426 MapSize = FileSize;
427 }
428
429 std::error_code EC;
430 std::unique_ptr<WriteThroughMemoryBuffer> Result(
431 new (NamedBufferAlloc(Filename))
432 MemoryBufferMMapFile<WriteThroughMemoryBuffer>(false, FD, MapSize,
433 Offset, EC));
434 if (EC)
435 return EC;
436 return std::move(Result);
437}
438
440WriteThroughMemoryBuffer::getFile(const Twine &Filename, int64_t FileSize) {
441 return getReadWriteFile(Filename, FileSize, FileSize, 0);
442}
443
444/// Map a subrange of the specified file as a WritableMemoryBuffer.
448 return getReadWriteFile(Filename, -1, MapSize, Offset);
449}
450
451template <typename MB>
453getOpenFileImpl(sys::fs::file_t FD, const Twine &Filename, uint64_t FileSize,
454 uint64_t MapSize, int64_t Offset, bool RequiresNullTerminator,
455 bool IsVolatile, std::optional<Align> Alignment) {
457
458 // Default is to map the full file.
459 if (MapSize == uint64_t(-1)) {
460 // If we don't know the file size, use fstat to find out. fstat on an open
461 // file descriptor is cheaper than stat on a random path.
462 if (FileSize == uint64_t(-1)) {
464 std::error_code EC = sys::fs::status(FD, Status);
465 if (EC)
466 return EC;
467
468 // If this not a file or a block device (e.g. it's a named pipe
469 // or character device), we can't trust the size. Create the memory
470 // buffer by copying off the stream.
474 return getMemoryBufferForStream(FD, Filename);
475
476 FileSize = Status.getSize();
477 }
478 MapSize = FileSize;
479 }
480
481 if (shouldUseMmap(FD, FileSize, MapSize, Offset, RequiresNullTerminator,
482 PageSize, IsVolatile)) {
483 std::error_code EC;
484 std::unique_ptr<MB> Result(
485 new (NamedBufferAlloc(Filename)) MemoryBufferMMapFile<MB>(
486 RequiresNullTerminator, FD, MapSize, Offset, EC));
487 if (!EC)
488 return std::move(Result);
489 }
490
491#ifdef __MVS__
492 // Set codepage auto-conversion for z/OS.
493 if (auto EC = llvm::enableAutoConversion(FD))
494 return EC;
495#endif
496
497 auto Buf =
498 WritableMemoryBuffer::getNewUninitMemBuffer(MapSize, Filename, Alignment);
499 if (!Buf) {
500 // Failed to create a buffer. The only way it can fail is if
501 // new(std::nothrow) returns 0.
503 }
504
505 // Read until EOF, zero-initialize the rest.
506 MutableArrayRef<char> ToRead = Buf->getBuffer();
507 while (!ToRead.empty()) {
508 Expected<size_t> ReadBytes =
510 if (!ReadBytes)
511 return errorToErrorCode(ReadBytes.takeError());
512 if (*ReadBytes == 0) {
513 std::memset(ToRead.data(), 0, ToRead.size());
514 break;
515 }
516 ToRead = ToRead.drop_front(*ReadBytes);
517 Offset += *ReadBytes;
518 }
519
520 return std::move(Buf);
521}
522
525 uint64_t FileSize, bool RequiresNullTerminator,
526 bool IsVolatile, std::optional<Align> Alignment) {
527 return getOpenFileImpl<MemoryBuffer>(FD, Filename, FileSize, FileSize, 0,
528 RequiresNullTerminator, IsVolatile,
529 Alignment);
530}
531
533 sys::fs::file_t FD, const Twine &Filename, uint64_t MapSize, int64_t Offset,
534 bool IsVolatile, std::optional<Align> Alignment) {
535 assert(MapSize != uint64_t(-1));
536 return getOpenFileImpl<MemoryBuffer>(FD, Filename, -1, MapSize, Offset, false,
537 IsVolatile, Alignment);
538}
539
541 // Read in all of the data from stdin, we cannot mmap stdin.
542 //
543 // FIXME: That isn't necessarily true, we should try to mmap stdin and
544 // fallback if it fails.
546
548}
549
554 if (!FDOrErr)
555 return errorToErrorCode(FDOrErr.takeError());
556 sys::fs::file_t FD = *FDOrErr;
558 getMemoryBufferForStream(FD, Filename);
560 return Ret;
561}
562
565 StringRef Identifier = getBufferIdentifier();
566 return MemoryBufferRef(Data, Identifier);
567}
568
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
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:474
Error takeError()
Take ownership of the stored error.
Definition: Error.h:601
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:226
This class represents a memory mapped file.
Definition: FileSystem.h:1267
@ priv
May modify via data, but changes are lost on destruction.
Definition: FileSystem.h:1272
@ readonly
May only access map via const_data as read only.
Definition: FileSystem.h:1270
@ readwrite
May access map via data and modify it. Written to path.
Definition: FileSystem.h:1271
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:759
@ OF_TextWithCRLF
The file should be opened in text mode and use a carriage linefeed '\r '.
Definition: FileSystem.h:768
file_type
An enumeration for the file system's view of the type.
Definition: FileSystem.h:66
@ CD_OpenExisting
CD_OpenExisting - When opening a file:
Definition: FileSystem.h:741
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:1124
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:456
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:1833
std::error_code errorToErrorCode(Error Err)
Helper for converting an ECError to a std::error_code.
Definition: Error.cpp:109
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