Movatterモバイル変換


[0]ホーム

URL:


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

Generated on Thu Jul 17 2025 12:40:02 for LLVM by doxygen 1.9.6
[8]ページ先頭

©2009-2025 Movatter.jp