15 #include "llvm/Config/config.h" 28 #include <sys/types.h> 29 #include <system_error> 30 #if !defined(_MSC_VER) && !defined(__MINGW32__) 46 bool RequiresNullTerminator) {
47 assert((!RequiresNullTerminator || BufEnd[0] == 0) &&
48 "Buffer is not null terminated!");
49 BufferStart = BufStart;
61 memcpy(Memory, Data.
data(), Data.
size());
62 Memory[Data.
size()] = 0;
66 struct NamedBufferAlloc {
68 NamedBufferAlloc(
const Twine &Name) :
Name(Name) {}
72 void *
operator new(
size_t N,
const NamedBufferAlloc &Alloc) {
74 StringRef NameRef = Alloc.Name.toStringRef(NameBuf);
76 char *Mem =
static_cast<char *
>(
operator new(
N + NameRef.
size() + 1));
84 class MemoryBufferMem :
public MB {
86 MemoryBufferMem(
StringRef InputData,
bool RequiresNullTerminator) {
88 RequiresNullTerminator);
93 void operator delete(
void *p) { ::operator
delete(p); }
97 return StringRef(reinterpret_cast<const char *>(
this + 1));
106 template <
typename MB>
111 std::unique_ptr<MemoryBuffer>
113 bool RequiresNullTerminator) {
114 auto *
Ret =
new (NamedBufferAlloc(BufferName))
115 MemoryBufferMem<MemoryBuffer>(InputData, RequiresNullTerminator);
116 return std::unique_ptr<MemoryBuffer>(
Ret);
119 std::unique_ptr<MemoryBuffer>
130 memcpy(Buf->getBufferStart(), InputData.
data(), InputData.
size());
131 return std::move(Buf);
134 std::unique_ptr<MemoryBuffer>
138 return std::move(*Buf);
144 bool RequiresNullTerminator) {
150 return getFile(Filename, FileSize, RequiresNullTerminator);
155 uint64_t
Offset,
bool IsVolatile) {
156 return getFileAux<MemoryBuffer>(FilePath, -1, MapSize,
Offset,
false,
168 template<
typename MB>
169 class MemoryBufferMMapFile :
public MB {
172 static uint64_t getLegalMapOffset(uint64_t
Offset) {
176 static uint64_t getLegalMapSize(uint64_t Len, uint64_t Offset) {
177 return Len + (Offset - getLegalMapOffset(Offset));
180 const char *getStart(uint64_t Len, uint64_t Offset) {
181 return MFR.
const_data() + (Offset - getLegalMapOffset(Offset));
185 MemoryBufferMMapFile(
bool RequiresNullTerminator,
sys::fs::file_t FD, uint64_t Len,
186 uint64_t Offset, std::error_code &EC)
187 : MFR(FD, MB::Mapmode, getLegalMapSize(Len, Offset),
188 getLegalMapOffset(Offset), EC) {
190 const char *Start = getStart(Len, Offset);
197 void operator delete(
void *p) { ::operator
delete(p); }
201 return StringRef(reinterpret_cast<const char *>(
this + 1));
212 const ssize_t ChunkSize = 4096*4;
232 bool RequiresNullTerminator,
bool IsVolatile) {
233 return getFileAux<MemoryBuffer>(Filename, FileSize, FileSize, 0,
237 template <
typename MB>
240 uint64_t MapSize, int64_t
Offset,
bool RequiresNullTerminator,
243 template <
typename MB>
246 uint64_t
Offset,
bool RequiresNullTerminator,
bool IsVolatile) {
252 auto Ret = getOpenFileImpl<MB>(FD, Filename, FileSize, MapSize,
Offset,
261 return getFileAux<WritableMemoryBuffer>(Filename, FileSize, FileSize, 0,
268 uint64_t
Offset,
bool IsVolatile) {
269 return getFileAux<WritableMemoryBuffer>(Filename, -1, MapSize,
Offset,
false,
273 std::unique_ptr<WritableMemoryBuffer>
275 using MemBuffer = MemoryBufferMem<WritableMemoryBuffer>;
282 size_t AlignedStringLen =
alignTo(
sizeof(MemBuffer) + NameRef.
size() + 1, 16);
283 size_t RealLen = AlignedStringLen + Size + 1;
284 char *Mem =
static_cast<char*
>(
operator new(RealLen, std::nothrow));
292 char *Buf = Mem + AlignedStringLen;
295 auto *
Ret =
new (Mem) MemBuffer(
StringRef(Buf, Size),
true);
296 return std::unique_ptr<WritableMemoryBuffer>(
Ret);
299 std::unique_ptr<WritableMemoryBuffer>
304 memset(SB->getBufferStart(), 0,
Size);
312 bool RequiresNullTerminator,
323 if (MapSize < 4 * 4096 || MapSize < (
unsigned)PageSize)
326 if (!RequiresNullTerminator)
333 if (FileSize ==
size_t(-1)) {
342 size_t End = Offset + MapSize;
349 if ((FileSize & (PageSize -1)) == 0)
352 #if defined(__CYGWIN__) 356 if ((FileSize & (4096 - 1)) == 0)
373 if (MapSize == uint64_t(-1)) {
376 if (FileSize == uint64_t(-1)) {
395 std::unique_ptr<WriteThroughMemoryBuffer> Result(
396 new (NamedBufferAlloc(Filename))
397 MemoryBufferMMapFile<WriteThroughMemoryBuffer>(
false, FD, MapSize,
401 return std::move(Result);
416 template <
typename MB>
419 uint64_t MapSize, int64_t
Offset,
bool RequiresNullTerminator,
424 if (MapSize == uint64_t(-1)) {
427 if (FileSize == uint64_t(-1)) {
446 if (
shouldUseMmap(FD, FileSize, MapSize, Offset, RequiresNullTerminator,
447 PageSize, IsVolatile)) {
449 std::unique_ptr<MB> Result(
450 new (NamedBufferAlloc(Filename)) MemoryBufferMMapFile<MB>(
451 RequiresNullTerminator, FD, MapSize, Offset, EC));
453 return std::move(Result);
465 while (!ToRead.
empty()) {
470 if (*ReadBytes == 0) {
471 std::memset(ToRead.
data(), 0, ToRead.
size());
475 Offset += *ReadBytes;
478 return std::move(Buf);
483 bool RequiresNullTerminator,
bool IsVolatile) {
484 return getOpenFileImpl<MemoryBuffer>(FD, Filename, FileSize, FileSize, 0,
490 int64_t
Offset,
bool IsVolatile) {
491 assert(MapSize != uint64_t(-1));
492 return getOpenFileImpl<MemoryBuffer>(FD, Filename, -1, MapSize,
Offset,
false,
virtual StringRef getBufferIdentifier() const
Return an identifier for this buffer, typically the filename it was read from.
void init(const char *BufStart, const char *BufEnd, bool RequiresNullTerminator)
init - Initialize this MemoryBuffer as a reference to externally allocated memory, memory that we know is already null terminated.
Represents either an error or a value T.
MutableArrayRef< T > makeMutableArrayRef(T &OneElt)
Construct a MutableArrayRef from a single element.
StringRef getBuffer() const
This class represents lattice values for constants.
constexpr char IsVolatile[]
Key for Kernel::Arg::Metadata::mIsVolatile.
amdgpu Simplify well known AMD library false FunctionCallee Value const Twine & Name
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< WritableMemoryBuffer > > getFileSlice(const Twine &Filename, uint64_t MapSize, uint64_t Offset, bool IsVolatile=false)
Map a subrange of the specified file as a WritableMemoryBuffer.
This class provides various memory handling functions that manipulate MemoryBlock instances...
static std::unique_ptr< MemoryBuffer > getMemBuffer(StringRef InputData, StringRef BufferName="", bool RequiresNullTerminator=true)
Open the specified memory range as a MemoryBuffer.
StringRef toStringRef(SmallVectorImpl< char > &Out) const
This returns the twine as a single StringRef if it can be represented as such.
This class represents a memory mapped file.
~SmallVectorMemoryBuffer() override
Error takeError()
Take ownership of the stored error.
void reserve(size_type N)
Represents the result of a call to sys::fs::status().
CD_OpenExisting - When opening a file:
const char * const_data() const
Get a const view of the data.
Expected< size_t > readNativeFileSlice(file_t FileHandle, MutableArrayRef< char > Buf, uint64_t Offset)
Reads Buf.size() bytes from FileHandle at offset Offset into Buf.
virtual BufferKind getBufferKind() const =0
Return information on the memory mechanism used to support the MemoryBuffer.
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.
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
StringRef getBuffer() const
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.
The access may reference the value stored in memory.
std::error_code make_error_code(BitcodeError E)
static ErrorOr< std::unique_ptr< WritableMemoryBuffer > > getFile(const Twine &Filename, int64_t FileSize=-1, bool IsVolatile=false)
Tagged union holding either a T or a Error.
LLVM_NODISCARD bool empty() const
empty - Check if the string is empty.
MutableArrayRef< T > drop_front(size_t N=1) const
Drop the first N elements of the array.
static std::unique_ptr< WritableMemoryBuffer > getNewMemBuffer(size_t Size, const Twine &BufferName="")
Allocate a new zero-initialized MemoryBuffer of the specified size.
LLVM_NODISCARD size_t size() const
size - Get the string size.
static bool shouldUseMmap(sys::fs::file_t FD, size_t FileSize, size_t MapSize, off_t Offset, bool RequiresNullTerminator, int PageSize, bool IsVolatile)
static unsigned getPageSizeEstimate()
Get the process's estimated page size.
static ErrorOr< std::unique_ptr< MB > > getFileAux(const Twine &Filename, int64_t FileSize, uint64_t MapSize, uint64_t Offset, bool RequiresNullTerminator, bool IsVolatile)
file_t getStdinHandle()
Return an open handle to standard in.
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< MemoryBuffer > > getSTDIN()
Read all of stdin into a file buffer, and return it.
static ErrorOr< std::unique_ptr< WriteThroughMemoryBuffer > > getFile(const Twine &Filename, int64_t FileSize=-1)
MutableArrayRef - Represent a mutable reference to an array (0 or more elements consecutively in memo...
The instances of the Type class are immutable: once they are created, they are never changed...
static std::unique_ptr< WritableMemoryBuffer > getNewUninitMemBuffer(size_t Size, const Twine &BufferName="")
Allocate a new MemoryBuffer of the specified size that is not initialized.
std::error_code status(const Twine &path, file_status &result, bool follow=true)
Get file status as if by POSIX stat().
size_t size() const
size - Get the array size.
void set_size(size_t N)
Set the array size to N, which the current array must have enough capacity for.
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< size_t > readNativeFile(file_t FileHandle, MutableArrayRef< char > Buf)
Reads Buf.size() bytes from FileHandle into Buf.
static ErrorOr< std::unique_ptr< WritableMemoryBuffer > > getMemoryBufferForStream(sys::fs::file_t FD, const Twine &BufferName)
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.
std::error_code closeFile(file_t &F)
Close the file object.
MemoryBufferRef getMemBufferRef() const
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...
StringRef getBufferIdentifier() const
std::error_code ChangeStdinToBinary()
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.
static ErrorOr< std::unique_ptr< WriteThroughMemoryBuffer > > getReadWriteFile(const Twine &Filename, uint64_t FileSize, uint64_t MapSize, uint64_t Offset)
static ErrorOr< std::unique_ptr< MemoryBuffer > > getFileOrSTDIN(const Twine &Filename, int64_t FileSize=-1, bool RequiresNullTerminator=true)
Open the specified file as a MemoryBuffer, or open stdin if the Filename is "-".
static ErrorOr< std::unique_ptr< WritableMemoryBuffer > > getMemBufferCopyImpl(StringRef InputData, const Twine &BufferName)
uint64_t alignTo(uint64_t Size, Align A)
Returns a multiple of A needed to store Size bytes.
Provides a library for accessing information about this process and other processes on the operating ...
file_type
An enumeration for the file system's view of the type.
static ErrorOr< std::unique_ptr< MemoryBuffer > > getFileAsStream(const Twine &Filename)
Read all of the specified file into a MemoryBuffer as a stream (i.e.
BufferKind
The kind of memory backing used to support the 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)
static ErrorOr< std::unique_ptr< MemoryBuffer > > getFile(const Twine &Filename, int64_t FileSize=-1, bool RequiresNullTerminator=true, bool IsVolatile=false)
Open the specified file as a MemoryBuffer, returning a new MemoryBuffer if successful, otherwise returning null.
LLVM_NODISCARD const char * data() const
data - Get a pointer to the start of the string (which may not be null terminated).
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
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.
StringRef - Represent a constant reference to a string, i.e.
std::error_code errorToErrorCode(Error Err)
Helper for converting an ECError to a std::error_code.
bool empty() const
empty - Check if the array is empty.