15#include "llvm/Config/config.h"
29#include <system_error>
30#if !defined(_MSC_VER) && !defined(__MINGW32__)
50 bool RequiresNullTerminator) {
51 assert((!RequiresNullTerminator || BufEnd[0] == 0) &&
52 "Buffer is not null terminated!");
53 BufferStart = BufStart;
70struct NamedBufferAlloc {
76void *
operator new(
size_t N,
const NamedBufferAlloc &
Alloc) {
80 char *Mem =
static_cast<char *
>(
operator new(
N +
sizeof(size_t) +
82 *
reinterpret_cast<size_t *
>(Mem +
N) = NameRef.
size();
90class MemoryBufferMem :
public MB {
92 MemoryBufferMem(
StringRef InputData,
bool RequiresNullTerminator) {
94 RequiresNullTerminator);
99 void operator delete(
void *p) { ::operator
delete(p); }
101 StringRef getBufferIdentifier()
const override {
103 return StringRef(
reinterpret_cast<const char *
>(
this + 1) +
sizeof(
size_t),
104 *
reinterpret_cast<const size_t *
>(
this + 1));
113template <
typename MB>
116 bool IsText,
bool RequiresNullTerminator,
bool IsVolatile,
117 std::optional<Align> Alignment);
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);
127std::unique_ptr<MemoryBuffer>
130 Ref.getBuffer(),
Ref.getBufferIdentifier(), RequiresNullTerminator));
138 memcpy(Buf->getBufferStart(), InputData.
data(), InputData.
size());
139 return std::move(Buf);
142std::unique_ptr<MemoryBuffer>
146 return std::move(*Buf);
152 bool RequiresNullTerminator,
153 std::optional<Align> Alignment) {
155 StringRef NameRef = Filename.toStringRef(NameBuf);
159 return getFile(Filename, IsText, RequiresNullTerminator,
166 std::optional<Align> Alignment) {
167 return getFileAux<MemoryBuffer>(FilePath, MapSize,
Offset,
false,
178template <
typename MB>
195class MemoryBufferMMapFile :
public MB {
213 : MFR(FD, Mapmode<MB>, getLegalMapSize(
Len,
Offset),
216 const char *Start = getStart(Len,
Offset);
223 void operator delete(
void *p) { ::operator
delete(p); }
225 StringRef getBufferIdentifier()
const override {
227 return StringRef(
reinterpret_cast<const char *
>(
this + 1) +
sizeof(
size_t),
228 *
reinterpret_cast<const size_t *
>(
this + 1));
235 void dontNeedIfMmap()
override { MFR.
dontNeed(); }
249 bool RequiresNullTerminator,
bool IsVolatile,
250 std::optional<Align> Alignment) {
251 return getFileAux<MemoryBuffer>(Filename, -1, 0,
252 IsText, RequiresNullTerminator, IsVolatile,
256template <
typename MB>
260 bool IsVolatile, std::optional<Align> Alignment);
262template <
typename MB>
265 bool IsText,
bool RequiresNullTerminator,
bool IsVolatile,
266 std::optional<Align> Alignment) {
272 auto Ret = getOpenFileImpl<MB>(FD, Filename, -1, MapSize,
Offset,
273 RequiresNullTerminator, IsVolatile, Alignment);
280 std::optional<Align> Alignment) {
281 return getFileAux<WritableMemoryBuffer>(
282 Filename, -1, 0,
false,
283 false, IsVolatile, Alignment);
289 std::optional<Align> Alignment) {
290 return getFileAux<WritableMemoryBuffer>(
291 Filename, MapSize,
Offset,
false,
292 false, IsVolatile, Alignment);
295std::unique_ptr<WritableMemoryBuffer>
297 const Twine &BufferName,
298 std::optional<Align> Alignment) {
299 using MemBuffer = MemoryBufferMem<WritableMemoryBuffer>;
302 Align BufAlign = Alignment.value_or(
Align(16));
309 size_t StringLen =
sizeof(MemBuffer) +
sizeof(
size_t) + NameRef.
size() + 1;
310 size_t RealLen = StringLen +
Size + 1 + BufAlign.
value();
313 char *Mem =
static_cast<char*
>(
operator new(RealLen, std::nothrow));
318 *
reinterpret_cast<size_t *
>(Mem +
sizeof(MemBuffer)) = NameRef.
size();
319 CopyStringRef(Mem +
sizeof(MemBuffer) +
sizeof(
size_t), NameRef);
322 char *Buf = (
char *)
alignAddr(Mem + StringLen, BufAlign);
326 return std::unique_ptr<WritableMemoryBuffer>(Ret);
329std::unique_ptr<WritableMemoryBuffer>
334 memset(SB->getBufferStart(), 0,
Size);
342 bool RequiresNullTerminator,
348 if (IsVolatile && RequiresNullTerminator)
353 if (MapSize < 4 * 4096 || MapSize < (
unsigned)
PageSize)
356 if (!RequiresNullTerminator)
363 if (FileSize ==
size_t(-1)) {
367 FileSize =
Status.getSize();
372 size_t End =
Offset + MapSize;
379 if ((FileSize & (
PageSize -1)) == 0)
382#if defined(__CYGWIN__)
386 if ((FileSize & (4096 - 1)) == 0)
419 FileSize =
Status.getSize();
425 std::unique_ptr<WriteThroughMemoryBuffer> Result(
426 new (NamedBufferAlloc(Filename))
427 MemoryBufferMMapFile<WriteThroughMemoryBuffer>(
false, FD, MapSize,
431 return std::move(Result);
446template <
typename MB>
450 bool IsVolatile, std::optional<Align> Alignment) {
471 FileSize =
Status.getSize();
479 std::unique_ptr<MB> Result(
480 new (NamedBufferAlloc(Filename)) MemoryBufferMMapFile<MB>(
481 RequiresNullTerminator, FD, MapSize,
Offset, EC));
483 return std::move(Result);
488 if (
auto EC = llvm::enableAutoConversion(FD))
502 while (!ToRead.
empty()) {
507 if (*ReadBytes == 0) {
508 std::memset(ToRead.
data(), 0, ToRead.
size());
515 return std::move(Buf);
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,
529 bool IsVolatile, std::optional<Align> Alignment) {
531 return getOpenFileImpl<MemoryBuffer>(FD, Filename, -1, MapSize,
Offset,
false,
532 IsVolatile, Alignment);
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
static cl::opt< int > PageSize("imp-null-check-page-size", cl::desc("The page size of the target in bytes"), cl::init(4096), cl::Hidden)
static bool shouldUseMmap(sys::fs::file_t FD, size_t FileSize, size_t MapSize, off_t Offset, bool RequiresNullTerminator, int PageSize, bool IsVolatile)
static ErrorOr< std::unique_ptr< MB > > getFileAux(const Twine &Filename, uint64_t MapSize, uint64_t Offset, bool IsText, bool RequiresNullTerminator, bool IsVolatile, std::optional< Align > Alignment)
static ErrorOr< std::unique_ptr< WriteThroughMemoryBuffer > > getReadWriteFile(const Twine &Filename, uint64_t FileSize, uint64_t MapSize, uint64_t Offset)
static ErrorOr< std::unique_ptr< MB > > getOpenFileImpl(sys::fs::file_t FD, const Twine &Filename, uint64_t FileSize, uint64_t MapSize, int64_t Offset, bool RequiresNullTerminator, bool IsVolatile, std::optional< Align > Alignment)
static ErrorOr< std::unique_ptr< WritableMemoryBuffer > > getMemBufferCopyImpl(StringRef InputData, const Twine &BufferName)
static void CopyStringRef(char *Memory, StringRef Data)
CopyStringRef - Copies contents of a StringRef into a block of memory and null-terminates it.
static ErrorOr< std::unique_ptr< WritableMemoryBuffer > > getMemoryBufferForStream(sys::fs::file_t FD, const Twine &BufferName)
Provides a library for accessing information about this process and other processes on the operating ...
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
This file defines the SmallString class.
size_t size() const
size - Get the array size.
bool empty() const
empty - Check if the array is empty.
Represents either an error or a value T.
Lightweight error class with error context and mandatory checking.
Tagged union holding either a T or a Error.
Error takeError()
Take ownership of the stored error.
static ErrorOr< std::unique_ptr< MemoryBuffer > > getOpenFile(sys::fs::file_t FD, const Twine &Filename, uint64_t FileSize, bool RequiresNullTerminator=true, bool IsVolatile=false, std::optional< Align > Alignment=std::nullopt)
Given an already-open file descriptor, read the file and return a MemoryBuffer.
static std::unique_ptr< MemoryBuffer > getMemBuffer(StringRef InputData, StringRef BufferName="", bool RequiresNullTerminator=true)
Open the specified memory range as a MemoryBuffer.
BufferKind
The kind of memory backing used to support the MemoryBuffer.
virtual StringRef getBufferIdentifier() const
Return an identifier for this buffer, typically the filename it was read from.
static std::unique_ptr< MemoryBuffer > getMemBufferCopy(StringRef InputData, const Twine &BufferName="")
Open the specified memory range as a MemoryBuffer, copying the contents and taking ownership of it.
static ErrorOr< std::unique_ptr< MemoryBuffer > > getOpenFileSlice(sys::fs::file_t FD, const Twine &Filename, uint64_t MapSize, int64_t Offset, bool IsVolatile=false, std::optional< Align > Alignment=std::nullopt)
Given an already-open file descriptor, map some slice of it into a MemoryBuffer.
void init(const char *BufStart, const char *BufEnd, bool RequiresNullTerminator)
init - Initialize this MemoryBuffer as a reference to externally allocated memory,...
StringRef getBuffer() const
static ErrorOr< std::unique_ptr< MemoryBuffer > > getFileAsStream(const Twine &Filename)
Read all of the specified file into a MemoryBuffer as a stream (i.e.
MemoryBufferRef getMemBufferRef() const
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...
MutableArrayRef< T > drop_front(size_t N=1) const
Drop the first N elements of the array.
SmallString - A SmallString is just a SmallVector with methods and accessors that make it work better...
~SmallVectorMemoryBuffer() override
StringRef - Represent a constant reference to a string, i.e.
constexpr size_t size() const
size - Get the string size.
const char * data() const
data - Get a pointer to the start of the string (which may not be null terminated).
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
StringRef toStringRef(SmallVectorImpl< char > &Out) const
This returns the twine as a single StringRef if it can be represented as such.
The instances of the Type class are immutable: once they are created, they are never changed.
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.
static unsigned getPageSizeEstimate()
Get the process's estimated page size.
Represents the result of a call to sys::fs::status().
This class represents a memory mapped file.
@ priv
May modify via data, but changes are lost on destruction.
@ readonly
May only access map via const_data as read only.
@ readwrite
May access map via data and modify it. Written to path.
const char * const_data() const
Get a const view of the data.
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.
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.
@ OF_TextWithCRLF
The file should be opened in text mode and use a carriage linefeed '\r '.
file_type
An enumeration for the file system's view of the type.
@ CD_OpenExisting
CD_OpenExisting - When opening a file:
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...
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.
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.
uintptr_t alignAddr(const void *Addr, Align Alignment)
Aligns Addr to Alignment bytes, rounding up.
This struct is a compact representation of a valid (non-zero power of two) alignment.
uint64_t value() const
This is a hole in the type system and should not be abused.