Go to the documentation of this file.
15 #include "llvm/Config/config.h"
27 #include <sys/types.h>
28 #include <system_error>
29 #if !defined(_MSC_VER) && !defined(__MINGW32__)
49 bool RequiresNullTerminator) {
50 assert((!RequiresNullTerminator || BufEnd[0] == 0) &&
51 "Buffer is not null terminated!");
52 BufferStart = BufStart;
69 struct NamedBufferAlloc {
75 void *
operator new(
size_t N,
const NamedBufferAlloc &Alloc) {
77 StringRef NameRef = Alloc.Name.toStringRef(NameBuf);
79 char *Mem =
static_cast<char *
>(
operator new(
N + NameRef.
size() + 1));
87 class MemoryBufferMem :
public MB {
89 MemoryBufferMem(
StringRef InputData,
bool RequiresNullTerminator) {
91 RequiresNullTerminator);
96 void operator delete(
void *
p) { ::operator
delete(
p); }
98 StringRef getBufferIdentifier()
const override {
100 return StringRef(
reinterpret_cast<const char *
>(
this + 1));
109 template <
typename MB>
112 bool IsText,
bool RequiresNullTerminator,
bool IsVolatile);
114 std::unique_ptr<MemoryBuffer>
116 bool RequiresNullTerminator) {
117 auto *
Ret =
new (NamedBufferAlloc(BufferName))
118 MemoryBufferMem<MemoryBuffer>(InputData, RequiresNullTerminator);
119 return std::unique_ptr<MemoryBuffer>(
Ret);
122 std::unique_ptr<MemoryBuffer>
125 Ref.getBuffer(),
Ref.getBufferIdentifier(), RequiresNullTerminator));
133 memcpy(Buf->getBufferStart(), InputData.
data(), InputData.
size());
137 std::unique_ptr<MemoryBuffer>
147 bool RequiresNullTerminator) {
153 return getFile(Filename, IsText, RequiresNullTerminator,
160 return getFileAux<MemoryBuffer>(FilePath, MapSize, Offset,
false,
170 template <
typename MB>
186 template<
typename MB>
187 class MemoryBufferMMapFile :
public MB {
195 return Len + (Offset - getLegalMapOffset(Offset));
204 uint64_t Offset, std::error_code &EC)
205 : MFR(FD, Mapmode<MB>, getLegalMapSize(Len,
Offset),
208 const char *Start = getStart(Len, Offset);
215 void operator delete(
void *
p) { ::operator
delete(
p); }
217 StringRef getBufferIdentifier()
const override {
219 return StringRef(
reinterpret_cast<const char *
>(
this + 1));
226 void dontNeedIfMmap()
override { MFR.
dontNeed(); }
240 bool RequiresNullTerminator,
bool IsVolatile) {
241 return getFileAux<MemoryBuffer>(Filename, -1, 0,
245 template <
typename MB>
248 uint64_t MapSize, int64_t Offset,
bool RequiresNullTerminator,
251 template <
typename MB>
254 bool IsText,
bool RequiresNullTerminator,
bool IsVolatile) {
260 auto Ret = getOpenFileImpl<MB>(FD, Filename, -1, MapSize, Offset,
268 return getFileAux<WritableMemoryBuffer>(
269 Filename, -1, 0,
false,
276 return getFileAux<WritableMemoryBuffer>(
277 Filename, MapSize, Offset,
false,
281 std::unique_ptr<WritableMemoryBuffer>
283 using MemBuffer = MemoryBufferMem<WritableMemoryBuffer>;
290 size_t AlignedStringLen =
alignTo(
sizeof(MemBuffer) + NameRef.
size() + 1, 16);
291 size_t RealLen = AlignedStringLen + Size + 1;
294 char *Mem =
static_cast<char*
>(
operator new(RealLen, std::nothrow));
302 char *Buf = Mem + AlignedStringLen;
305 auto *
Ret =
new (Mem) MemBuffer(
StringRef(Buf, Size),
true);
306 return std::unique_ptr<WritableMemoryBuffer>(
Ret);
309 std::unique_ptr<WritableMemoryBuffer>
314 memset(SB->getBufferStart(), 0, Size);
322 bool RequiresNullTerminator,
333 if (MapSize < 4 * 4096 || MapSize < (
unsigned)
PageSize)
336 if (!RequiresNullTerminator)
343 if (FileSize ==
size_t(-1)) {
347 FileSize =
Status.getSize();
352 size_t End = Offset + MapSize;
359 if ((FileSize & (
PageSize -1)) == 0)
362 #if defined(__CYGWIN__)
366 if ((FileSize & (4096 - 1)) == 0)
399 FileSize =
Status.getSize();
405 std::unique_ptr<WriteThroughMemoryBuffer> Result(
406 new (NamedBufferAlloc(Filename))
407 MemoryBufferMMapFile<WriteThroughMemoryBuffer>(
false, FD, MapSize,
426 template <
typename MB>
429 uint64_t MapSize, int64_t Offset,
bool RequiresNullTerminator,
451 FileSize =
Status.getSize();
456 if (
shouldUseMmap(FD, FileSize, MapSize, Offset, RequiresNullTerminator,
459 std::unique_ptr<MB> Result(
460 new (NamedBufferAlloc(Filename)) MemoryBufferMMapFile<MB>(
461 RequiresNullTerminator, FD, MapSize, Offset, EC));
468 if (
auto EC = llvm::enableAutoConversion(FD))
481 while (!ToRead.
empty()) {
486 if (*ReadBytes == 0) {
487 std::memset(ToRead.
data(), 0, ToRead.
size());
491 Offset += *ReadBytes;
499 bool RequiresNullTerminator,
bool IsVolatile) {
500 return getOpenFileImpl<MemoryBuffer>(FD, Filename, FileSize, FileSize, 0,
508 return getOpenFileImpl<MemoryBuffer>(FD, Filename, -1, MapSize, Offset,
false,
uint64_t alignTo(uint64_t Size, Align A)
Returns a multiple of A needed to store Size bytes.
static ErrorOr< std::unique_ptr< WriteThroughMemoryBuffer > > getReadWriteFile(const Twine &Filename, uint64_t FileSize, uint64_t MapSize, uint64_t Offset)
This is an optimization pass for GlobalISel generic memory operations.
static ErrorOr< std::unique_ptr< MemoryBuffer > > getOpenFile(sys::fs::file_t FD, const Twine &Filename, uint64_t FileSize, bool RequiresNullTerminator=true, bool IsVolatile=false)
Given an already-open file descriptor, read the file and return a MemoryBuffer.
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< WritableMemoryBuffer > > getFile(const Twine &Filename, bool IsVolatile=false)
static ErrorOr< std::unique_ptr< MemoryBuffer > > getOpenFileSlice(sys::fs::file_t FD, const Twine &Filename, uint64_t MapSize, int64_t Offset, bool IsVolatile=false)
Given an already-open file descriptor, map some slice of it into a MemoryBuffer.
static ErrorOr< std::unique_ptr< MemoryBuffer > > getSTDIN()
Read all of stdin into a file buffer, and return it.
@ priv
May modify via data, but changes are lost on destruction.
The instances of the Type class are immutable: once they are created, they are never changed.
@ CD_OpenExisting
CD_OpenExisting - When opening a file:
Tagged union holding either a T or a Error.
the resulting code requires compare and branches when and if * p
bool empty() const
empty - Check if the array is empty.
@ readonly
May only access map via const_data as read only.
static ErrorOr< std::unique_ptr< WritableMemoryBuffer > > getMemBufferCopyImpl(StringRef InputData, const Twine &BufferName)
static ErrorOr< std::unique_ptr< MemoryBuffer > > getFileAsStream(const Twine &Filename)
Read all of the specified file into a MemoryBuffer as a stream (i.e.
MutableArrayRef< T > drop_front(size_t N=1) const
Drop the first N elements of the array.
static ErrorOr< std::unique_ptr< MemoryBuffer > > getFile(const Twine &Filename, bool IsText=false, bool RequiresNullTerminator=true, bool IsVolatile=false)
Open the specified file as a MemoryBuffer, returning a new MemoryBuffer if successful,...
static ErrorOr< std::unique_ptr< WritableMemoryBuffer > > getMemoryBufferForStream(sys::fs::file_t FD, const Twine &BufferName)
static std::unique_ptr< MemoryBuffer > getMemBuffer(StringRef InputData, StringRef BufferName="", bool RequiresNullTerminator=true)
Open the specified memory range as a MemoryBuffer.
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)
void init(const char *BufStart, const char *BufEnd, bool RequiresNullTerminator)
init - Initialize this MemoryBuffer as a reference to externally allocated memory,...
@ Ref
The access may reference the value stored in memory.
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
static std::unique_ptr< WritableMemoryBuffer > getNewMemBuffer(size_t Size, const Twine &BufferName="")
Allocate a new zero-initialized MemoryBuffer of the specified size.
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.
StringRef getBuffer() const
file_type
An enumeration for the file system's view of the type.
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.
@ OF_TextWithCRLF
The file should be opened in text mode and use a carriage linefeed '\r '.
virtual StringRef getBufferIdentifier() const
Return an identifier for this buffer, typically the filename it was read from.
static void CopyStringRef(char *Memory, StringRef Data)
CopyStringRef - Copies contents of a StringRef into a block of memory and null-terminates it.
Represents the result of a call to sys::fs::status().
@ OF_Text
The file should be opened in text mode on platforms like z/OS that make this distinction.
~SmallVectorMemoryBuffer() override
static ErrorOr< std::unique_ptr< MemoryBuffer > > getFileSlice(const Twine &Filename, uint64_t MapSize, uint64_t Offset, bool IsVolatile=false)
Map a subrange of the specified file as a MemoryBuffer.
compiles ldr LCPI1_0 ldr ldr mov lsr tst moveq r1 ldr LCPI1_1 and r0 bx lr It would be better to do something like to fold the shift into the conditional move
MemoryBufferRef getMemBufferRef() const
BufferKind
The kind of memory backing used to support the MemoryBuffer.
static ErrorOr< std::unique_ptr< MemoryBuffer > > getFileOrSTDIN(const Twine &Filename, bool IsText=false, bool RequiresNullTerminator=true)
Open the specified file as a MemoryBuffer, or open stdin if the Filename is "-".
const char * const_data() const
Get a const view of the data.
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...
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
static std::unique_ptr< WritableMemoryBuffer > getNewUninitMemBuffer(size_t Size, const Twine &BufferName="")
Allocate a new MemoryBuffer of the specified size that is not initialized.
<%struct.s * > cast struct s *S to sbyte *< sbyte * > sbyte uint cast struct s *agg result to sbyte *< sbyte * > sbyte uint cast struct s *memtmp to sbyte *< sbyte * > sbyte uint ret void llc ends up issuing two memcpy or custom lower memcpy(of small size) to be ldmia/stmia. I think option 2 is better but the current register allocator cannot allocate a chunk of registers at a time. A feasible temporary solution is to use specific physical registers at the lowering time for small(<
std::error_code closeFile(file_t &F)
Close the file object.
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)
static ErrorOr< std::unique_ptr< MB > > getFileAux(const Twine &Filename, uint64_t MapSize, uint64_t Offset, bool IsText, bool RequiresNullTerminator, bool IsVolatile)
@ readwrite
May access map via data and modify it. Written to path.
Expected< size_t > readNativeFileSlice(file_t FileHandle, MutableArrayRef< char > Buf, uint64_t Offset)
Reads Buf.size() bytes from FileHandle at offset Offset into Buf.
This class provides various memory handling functions that manipulate MemoryBlock instances.
StringRef - Represent a constant reference to a string, i.e.
constexpr char IsVolatile[]
Key for Kernel::Arg::Metadata::mIsVolatile.
file_t getStdinHandle()
Return an open handle to standard in.
std::error_code make_error_code(BitcodeError E)
constexpr LLVM_NODISCARD size_t size() const
size - Get the string size.
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
std::error_code ChangeStdinMode(fs::OpenFlags Flags)
Lightweight error class with error context and mandatory checking.
std::error_code errorToErrorCode(Error Err)
Helper for converting an ECError to a std::error_code.
Error takeError()
Take ownership of the stored error.
This class represents a memory mapped file.
std::error_code status(const Twine &path, file_status &result, bool follow=true)
Get file status as if by POSIX stat().
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.
Represents either an error or a value T.
size_t size() const
size - Get the array size.
static ErrorOr< std::unique_ptr< WritableMemoryBuffer > > getFileSlice(const Twine &Filename, uint64_t MapSize, uint64_t Offset, bool IsVolatile=false)
Map a subrange of the specified file as a WritableMemoryBuffer.
const LLVM_NODISCARD char * data() const
data - Get a pointer to the start of the string (which may not be null terminated).
static ErrorOr< std::unique_ptr< WriteThroughMemoryBuffer > > getFile(const Twine &Filename, int64_t FileSize=-1)
StringRef toStringRef(SmallVectorImpl< char > &Out) const
This returns the twine as a single StringRef if it can be represented as such.
static unsigned getPageSizeEstimate()
Get the process's estimated page size.
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.