LLVM 17.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
15#include "llvm/Config/config.h"
17#include "llvm/Support/Errc.h"
18#include "llvm/Support/Error.h"
25#include <cassert>
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
36#ifdef __MVS__
38#endif
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 char *Mem = static_cast<char *>(operator new(N + sizeof(size_t) +
81 NameRef.size() + 1));
82 *reinterpret_cast<size_t *>(Mem + N) = NameRef.size();
83 CopyStringRef(Mem + N + sizeof(size_t), NameRef);
84 return Mem;
85}
86
87namespace {
88/// MemoryBufferMem - Named MemoryBuffer pointing to a block of memory.
89template<typename MB>
90class MemoryBufferMem : public MB {
91public:
92 MemoryBufferMem(StringRef InputData, bool RequiresNullTerminator) {
93 MemoryBuffer::init(InputData.begin(), InputData.end(),
94 RequiresNullTerminator);
95 }
96
97 /// Disable sized deallocation for MemoryBufferMem, because it has
98 /// tail-allocated data.
99 void operator delete(void *p) { ::operator delete(p); }
100
101 StringRef getBufferIdentifier() const override {
102 // The name is stored after the class itself.
103 return StringRef(reinterpret_cast<const char *>(this + 1) + sizeof(size_t),
104 *reinterpret_cast<const size_t *>(this + 1));
105 }
106
107 MemoryBuffer::BufferKind getBufferKind() const override {
109 }
110};
111} // namespace
112
113template <typename MB>
115getFileAux(const Twine &Filename, uint64_t MapSize, uint64_t Offset,
116 bool IsText, bool RequiresNullTerminator, bool IsVolatile,
117 std::optional<Align> Alignment);
118
119std::unique_ptr<MemoryBuffer>
121 bool RequiresNullTerminator) {
122 auto *Ret = new (NamedBufferAlloc(BufferName))
123 MemoryBufferMem<MemoryBuffer>(InputData, RequiresNullTerminator);
124 return std::unique_ptr<MemoryBuffer>(Ret);
125}
126
127std::unique_ptr<MemoryBuffer>
128MemoryBuffer::getMemBuffer(MemoryBufferRef Ref, bool RequiresNullTerminator) {
129 return std::unique_ptr<MemoryBuffer>(getMemBuffer(
130 Ref.getBuffer(), Ref.getBufferIdentifier(), RequiresNullTerminator));
131}
132
134getMemBufferCopyImpl(StringRef InputData, const Twine &BufferName) {
135 auto Buf = WritableMemoryBuffer::getNewUninitMemBuffer(InputData.size(), BufferName);
136 if (!Buf)
138 memcpy(Buf->getBufferStart(), InputData.data(), InputData.size());
139 return std::move(Buf);
140}
141
142std::unique_ptr<MemoryBuffer>
143MemoryBuffer::getMemBufferCopy(StringRef InputData, const Twine &BufferName) {
144 auto Buf = getMemBufferCopyImpl(InputData, BufferName);
145 if (Buf)
146 return std::move(*Buf);
147 return nullptr;
148}
149
151MemoryBuffer::getFileOrSTDIN(const Twine &Filename, bool IsText,
152 bool RequiresNullTerminator,
153 std::optional<Align> Alignment) {
154 SmallString<256> NameBuf;
155 StringRef NameRef = Filename.toStringRef(NameBuf);
156
157 if (NameRef == "-")
158 return getSTDIN();
159 return getFile(Filename, IsText, RequiresNullTerminator,
160 /*IsVolatile=*/false, Alignment);
161}
162
165 uint64_t Offset, bool IsVolatile,
166 std::optional<Align> Alignment) {
167 return getFileAux<MemoryBuffer>(FilePath, MapSize, Offset, /*IsText=*/false,
168 /*RequiresNullTerminator=*/false, IsVolatile,
169 Alignment);
170}
171
172//===----------------------------------------------------------------------===//
173// MemoryBuffer::getFile implementation.
174//===----------------------------------------------------------------------===//
175
176namespace {
177
178template <typename MB>
179constexpr sys::fs::mapped_file_region::mapmode Mapmode =
181template <>
182constexpr sys::fs::mapped_file_region::mapmode Mapmode<MemoryBuffer> =
184template <>
185constexpr sys::fs::mapped_file_region::mapmode Mapmode<WritableMemoryBuffer> =
187template <>
189 Mapmode<WriteThroughMemoryBuffer> = sys::fs::mapped_file_region::readwrite;
190
191/// Memory maps a file descriptor using sys::fs::mapped_file_region.
192///
193/// This handles converting the offset into a legal offset on the platform.
194template<typename MB>
195class MemoryBufferMMapFile : public MB {
197
198 static uint64_t getLegalMapOffset(uint64_t Offset) {
200 }
201
202 static uint64_t getLegalMapSize(uint64_t Len, uint64_t Offset) {
203 return Len + (Offset - getLegalMapOffset(Offset));
204 }
205
206 const char *getStart(uint64_t Len, uint64_t Offset) {
207 return MFR.const_data() + (Offset - getLegalMapOffset(Offset));
208 }
209
210public:
211 MemoryBufferMMapFile(bool RequiresNullTerminator, sys::fs::file_t FD, uint64_t Len,
212 uint64_t Offset, std::error_code &EC)
213 : MFR(FD, Mapmode<MB>, getLegalMapSize(Len, Offset),
214 getLegalMapOffset(Offset), EC) {
215 if (!EC) {
216 const char *Start = getStart(Len, Offset);
217 MemoryBuffer::init(Start, Start + Len, RequiresNullTerminator);
218 }
219 }
220
221 /// Disable sized deallocation for MemoryBufferMMapFile, because it has
222 /// tail-allocated data.
223 void operator delete(void *p) { ::operator delete(p); }
224
225 StringRef getBufferIdentifier() const override {
226 // The name is stored after the class itself.
227 return StringRef(reinterpret_cast<const char *>(this + 1) + sizeof(size_t),
228 *reinterpret_cast<const size_t *>(this + 1));
229 }
230
231 MemoryBuffer::BufferKind getBufferKind() const override {
233 }
234
235 void dontNeedIfMmap() override { MFR.dontNeed(); }
236};
237} // namespace
238
242 if (Error E = sys::fs::readNativeFileToEOF(FD, Buffer))
243 return errorToErrorCode(std::move(E));
244 return getMemBufferCopyImpl(Buffer, BufferName);
245}
246
248MemoryBuffer::getFile(const Twine &Filename, bool IsText,
249 bool RequiresNullTerminator, bool IsVolatile,
250 std::optional<Align> Alignment) {
251 return getFileAux<MemoryBuffer>(Filename, /*MapSize=*/-1, /*Offset=*/0,
252 IsText, RequiresNullTerminator, IsVolatile,
253 Alignment);
254}
255
256template <typename MB>
258getOpenFileImpl(sys::fs::file_t FD, const Twine &Filename, uint64_t FileSize,
259 uint64_t MapSize, int64_t Offset, bool RequiresNullTerminator,
260 bool IsVolatile, std::optional<Align> Alignment);
261
262template <typename MB>
264getFileAux(const Twine &Filename, uint64_t MapSize, uint64_t Offset,
265 bool IsText, bool RequiresNullTerminator, bool IsVolatile,
266 std::optional<Align> Alignment) {
268 Filename, IsText ? sys::fs::OF_TextWithCRLF : sys::fs::OF_None);
269 if (!FDOrErr)
270 return errorToErrorCode(FDOrErr.takeError());
271 sys::fs::file_t FD = *FDOrErr;
272 auto Ret = getOpenFileImpl<MB>(FD, Filename, /*FileSize=*/-1, MapSize, Offset,
273 RequiresNullTerminator, IsVolatile, Alignment);
275 return Ret;
276}
277
279WritableMemoryBuffer::getFile(const Twine &Filename, bool IsVolatile,
280 std::optional<Align> Alignment) {
281 return getFileAux<WritableMemoryBuffer>(
282 Filename, /*MapSize=*/-1, /*Offset=*/0, /*IsText=*/false,
283 /*RequiresNullTerminator=*/false, IsVolatile, Alignment);
284}
285
288 uint64_t Offset, bool IsVolatile,
289 std::optional<Align> Alignment) {
290 return getFileAux<WritableMemoryBuffer>(
291 Filename, MapSize, Offset, /*IsText=*/false,
292 /*RequiresNullTerminator=*/false, IsVolatile, Alignment);
293}
294
295std::unique_ptr<WritableMemoryBuffer>
297 const Twine &BufferName,
298 std::optional<Align> Alignment) {
299 using MemBuffer = MemoryBufferMem<WritableMemoryBuffer>;
300
301 // Use 16-byte alignment if no alignment is specified.
302 Align BufAlign = Alignment.value_or(Align(16));
303
304 // Allocate space for the MemoryBuffer, the data and the name. It is important
305 // that MemoryBuffer and data are aligned so PointerIntPair works with them.
306 SmallString<256> NameBuf;
307 StringRef NameRef = BufferName.toStringRef(NameBuf);
308
309 size_t StringLen = sizeof(MemBuffer) + sizeof(size_t) + NameRef.size() + 1;
310 size_t RealLen = StringLen + Size + 1 + BufAlign.value();
311 if (RealLen <= Size) // Check for rollover.
312 return nullptr;
313 char *Mem = static_cast<char*>(operator new(RealLen, std::nothrow));
314 if (!Mem)
315 return nullptr;
316
317 // The name is stored after the class itself.
318 *reinterpret_cast<size_t *>(Mem + sizeof(MemBuffer)) = NameRef.size();
319 CopyStringRef(Mem + sizeof(MemBuffer) + sizeof(size_t), NameRef);
320
321 // The buffer begins after the name and must be aligned.
322 char *Buf = (char *)alignAddr(Mem + StringLen, BufAlign);
323 Buf[Size] = 0; // Null terminate buffer.
324
325 auto *Ret = new (Mem) MemBuffer(StringRef(Buf, Size), true);
326 return std::unique_ptr<WritableMemoryBuffer>(Ret);
327}
328
329std::unique_ptr<WritableMemoryBuffer>
332 if (!SB)
333 return nullptr;
334 memset(SB->getBufferStart(), 0, Size);
335 return SB;
336}
337
339 size_t FileSize,
340 size_t MapSize,
341 off_t Offset,
342 bool RequiresNullTerminator,
343 int PageSize,
344 bool IsVolatile) {
345 // mmap may leave the buffer without null terminator if the file size changed
346 // by the time the last page is mapped in, so avoid it if the file size is
347 // likely to change.
348 if (IsVolatile && RequiresNullTerminator)
349 return false;
350
351 // We don't use mmap for small files because this can severely fragment our
352 // address space.
353 if (MapSize < 4 * 4096 || MapSize < (unsigned)PageSize)
354 return false;
355
356 if (!RequiresNullTerminator)
357 return true;
358
359 // If we don't know the file size, use fstat to find out. fstat on an open
360 // file descriptor is cheaper than stat on a random path.
361 // FIXME: this chunk of code is duplicated, but it avoids a fstat when
362 // RequiresNullTerminator = false and MapSize != -1.
363 if (FileSize == size_t(-1)) {
365 if (sys::fs::status(FD, Status))
366 return false;
367 FileSize = Status.getSize();
368 }
369
370 // If we need a null terminator and the end of the map is inside the file,
371 // we cannot use mmap.
372 size_t End = Offset + MapSize;
373 assert(End <= FileSize);
374 if (End != FileSize)
375 return false;
376
377 // Don't try to map files that are exactly a multiple of the system page size
378 // if we need a null terminator.
379 if ((FileSize & (PageSize -1)) == 0)
380 return false;
381
382#if defined(__CYGWIN__)
383 // Don't try to map files that are exactly a multiple of the physical page size
384 // if we need a null terminator.
385 // FIXME: We should reorganize again getPageSize() on Win32.
386 if ((FileSize & (4096 - 1)) == 0)
387 return false;
388#endif
389
390 return true;
391}
392
394getReadWriteFile(const Twine &Filename, uint64_t FileSize, uint64_t MapSize,
398 if (!FDOrErr)
399 return errorToErrorCode(FDOrErr.takeError());
400 sys::fs::file_t FD = *FDOrErr;
401
402 // Default is to map the full file.
403 if (MapSize == uint64_t(-1)) {
404 // If we don't know the file size, use fstat to find out. fstat on an open
405 // file descriptor is cheaper than stat on a random path.
406 if (FileSize == uint64_t(-1)) {
408 std::error_code EC = sys::fs::status(FD, Status);
409 if (EC)
410 return EC;
411
412 // If this not a file or a block device (e.g. it's a named pipe
413 // or character device), we can't mmap it, so error out.
418
419 FileSize = Status.getSize();
420 }
421 MapSize = FileSize;
422 }
423
424 std::error_code EC;
425 std::unique_ptr<WriteThroughMemoryBuffer> Result(
426 new (NamedBufferAlloc(Filename))
427 MemoryBufferMMapFile<WriteThroughMemoryBuffer>(false, FD, MapSize,
428 Offset, EC));
429 if (EC)
430 return EC;
431 return std::move(Result);
432}
433
435WriteThroughMemoryBuffer::getFile(const Twine &Filename, int64_t FileSize) {
436 return getReadWriteFile(Filename, FileSize, FileSize, 0);
437}
438
439/// Map a subrange of the specified file as a WritableMemoryBuffer.
443 return getReadWriteFile(Filename, -1, MapSize, Offset);
444}
445
446template <typename MB>
448getOpenFileImpl(sys::fs::file_t FD, const Twine &Filename, uint64_t FileSize,
449 uint64_t MapSize, int64_t Offset, bool RequiresNullTerminator,
450 bool IsVolatile, std::optional<Align> Alignment) {
452
453 // Default is to map the full file.
454 if (MapSize == uint64_t(-1)) {
455 // If we don't know the file size, use fstat to find out. fstat on an open
456 // file descriptor is cheaper than stat on a random path.
457 if (FileSize == uint64_t(-1)) {
459 std::error_code EC = sys::fs::status(FD, Status);
460 if (EC)
461 return EC;
462
463 // If this not a file or a block device (e.g. it's a named pipe
464 // or character device), we can't trust the size. Create the memory
465 // buffer by copying off the stream.
469 return getMemoryBufferForStream(FD, Filename);
470
471 FileSize = Status.getSize();
472 }
473 MapSize = FileSize;
474 }
475
476 if (shouldUseMmap(FD, FileSize, MapSize, Offset, RequiresNullTerminator,
477 PageSize, IsVolatile)) {
478 std::error_code EC;
479 std::unique_ptr<MB> Result(
480 new (NamedBufferAlloc(Filename)) MemoryBufferMMapFile<MB>(
481 RequiresNullTerminator, FD, MapSize, Offset, EC));
482 if (!EC)
483 return std::move(Result);
484 }
485
486#ifdef __MVS__
487 // Set codepage auto-conversion for z/OS.
488 if (auto EC = llvm::enableAutoConversion(FD))
489 return EC;
490#endif
491
492 auto Buf =
493 WritableMemoryBuffer::getNewUninitMemBuffer(MapSize, Filename, Alignment);
494 if (!Buf) {
495 // Failed to create a buffer. The only way it can fail is if
496 // new(std::nothrow) returns 0.
498 }
499
500 // Read until EOF, zero-initialize the rest.
501 MutableArrayRef<char> ToRead = Buf->getBuffer();
502 while (!ToRead.empty()) {
503 Expected<size_t> ReadBytes =
505 if (!ReadBytes)
506 return errorToErrorCode(ReadBytes.takeError());
507 if (*ReadBytes == 0) {
508 std::memset(ToRead.data(), 0, ToRead.size());
509 break;
510 }
511 ToRead = ToRead.drop_front(*ReadBytes);
512 Offset += *ReadBytes;
513 }
514
515 return std::move(Buf);
516}
517
520 uint64_t FileSize, bool RequiresNullTerminator,
521 bool IsVolatile, std::optional<Align> Alignment) {
522 return getOpenFileImpl<MemoryBuffer>(FD, Filename, FileSize, FileSize, 0,
523 RequiresNullTerminator, IsVolatile,
524 Alignment);
525}
526
528 sys::fs::file_t FD, const Twine &Filename, uint64_t MapSize, int64_t Offset,
529 bool IsVolatile, std::optional<Align> Alignment) {
530 assert(MapSize != uint64_t(-1));
531 return getOpenFileImpl<MemoryBuffer>(FD, Filename, -1, MapSize, Offset, false,
532 IsVolatile, Alignment);
533}
534
536 // Read in all of the data from stdin, we cannot mmap stdin.
537 //
538 // FIXME: That isn't necessarily true, we should try to mmap stdin and
539 // fallback if it fails.
541
543}
544
549 if (!FDOrErr)
550 return errorToErrorCode(FDOrErr.takeError());
551 sys::fs::file_t FD = *FDOrErr;
553 getMemoryBufferForStream(FD, Filename);
555 return Ret;
556}
557
560 StringRef Identifier = getBufferIdentifier();
561 return MemoryBufferRef(Data, Identifier);
562}
563
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
std::string Name
uint64_t Size
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 defines the SmallString class.
size_t size() const
size - Get the array size.
Definition: ArrayRef.h:163
bool empty() const
empty - Check if the array is empty.
Definition: ArrayRef.h:158
Represents either an error or a value T.
Definition: ErrorOr.h:56
Lightweight error class with error context and mandatory checking.
Definition: Error.h:156
Tagged union holding either a T or a Error.
Definition: Error.h:470
Error takeError()
Take ownership of the stored error.
Definition: Error.h:597
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:305
T * data() const
Definition: ArrayRef.h:352
MutableArrayRef< T > drop_front(size_t N=1) const
Drop the first N elements of the array.
Definition: ArrayRef.h:385
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
const char * data() const
data - Get a pointer to the start of the string (which may not be null terminated).
Definition: StringRef.h:131
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:473
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:1269
@ priv
May modify via data, but changes are lost on destruction.
Definition: FileSystem.h:1274
@ readonly
May only access map via const_data as read only.
Definition: FileSystem.h:1272
@ readwrite
May access map via data and modify it. Written to path.
Definition: FileSystem.h:1273
const char * const_data() const
Get a const view of the data.
Definition: Path.cpp:1169
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:1174
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:761
@ OF_TextWithCRLF
The file should be opened in text mode and use a carriage linefeed '\r '.
Definition: FileSystem.h:770
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:743
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:1126
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:406
std::error_code make_error_code(BitcodeError E)
@ Ref
The access may reference the value stored in memory.
std::error_code errorToErrorCode(Error Err)
Helper for converting an ECError to a std::error_code.
Definition: Error.cpp:98
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