LLVM 20.0.0git
|
This interface provides simple read-only access to a block of memory, and provides simple methods for reading files and standard input into a memory buffer. More...
#include "llvm/Support/MemoryBuffer.h"
Public Types | |
enum | BufferKind { MemoryBuffer_Malloc , MemoryBuffer_MMap } |
The kind of memory backing used to support the MemoryBuffer. More... | |
Public Member Functions | |
MemoryBuffer (const MemoryBuffer &)=delete | |
MemoryBuffer & | operator= (const MemoryBuffer &)=delete |
virtual | ~MemoryBuffer () |
const char * | getBufferStart () const |
const char * | getBufferEnd () const |
size_t | getBufferSize () const |
StringRef | getBuffer () const |
virtual StringRef | getBufferIdentifier () const |
Return an identifier for this buffer, typically the filename it was read from. | |
virtual void | dontNeedIfMmap () |
For read-only MemoryBuffer_MMap, mark the buffer as unused in the near future and the kernel can free resources associated with it. | |
virtual BufferKind | getBufferKind () const =0 |
Return information on the memory mechanism used to support the MemoryBuffer. | |
MemoryBufferRef | getMemBufferRef () const |
Static Public Member Functions | |
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, otherwise returning null. | |
static ErrorOr< std::unique_ptr< MemoryBuffer > > | getFileAsStream (const Twine &Filename) |
Read all of the specified file into a MemoryBuffer as a stream (i.e. | |
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. | |
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. | |
static std::unique_ptr< MemoryBuffer > | getMemBuffer (MemoryBufferRef Ref, bool RequiresNullTerminator=true) |
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 > > | getSTDIN () |
Read all of stdin into a file buffer, and return it. | |
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. | |
Protected Member Functions | |
MemoryBuffer ()=default | |
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. | |
This interface provides simple read-only access to a block of memory, and provides simple methods for reading files and standard input into a memory buffer.
In addition to basic access to the characters in the file, this interface guarantees you can read one character past the end of the file, and that this character will read as '\0'.
The '\0' guarantee is needed to support an optimization – it's intended to be more efficient for clients which are reading all the data to stop reading when they encounter a '\0' than to continually check the file position to see if it has reached the end of the file.
Definition at line 51 of file MemoryBuffer.h.
The kind of memory backing used to support the MemoryBuffer.
Enumerator | |
---|---|
MemoryBuffer_Malloc | |
MemoryBuffer_MMap |
Definition at line 165 of file MemoryBuffer.h.
|
protecteddefault |
|
delete |
|
virtualdefault |
|
inlinevirtual |
For read-only MemoryBuffer_MMap, mark the buffer as unused in the near future and the kernel can free resources associated with it.
Further access is supported but may be expensive. This calls madvise(MADV_DONTNEED) on read-only file mappings on *NIX systems. This function should not be called on a writable buffer.
Definition at line 83 of file MemoryBuffer.h.
|
inline |
Definition at line 70 of file MemoryBuffer.h.
References getBufferSize().
Referenced by getMemBufferRef(), GetOrCreateOffsetCache(), llvm::TextCodeGenDataReader::hasFormat(), llvm::SpecialCaseList::parse(), llvm::GCOVBuffer::readGCDAFormat(), llvm::GCOVBuffer::readGCNOFormat(), llvm::streamFile(), llvm::TGLexer::TGLexer(), and llvm::ThinLTOCodeGenerator::writeGeneratedObject().
Definition at line 67 of file MemoryBuffer.h.
Referenced by llvm::FileCheck::CanonicalizeFile(), llvm::RuntimeDyldCheckerImpl::checkAllRulesInBuffer(), llvm::DiffFilesWithTolerance(), llvm::WritableMemoryBuffer::getBufferEnd(), llvm::WriteThroughMemoryBuffer::getBufferEnd(), llvm::SourceMgr::GetMessage(), llvm::CodeGenCoverage::parse(), and readBinaryIdsInternal().
|
inlinevirtual |
Return an identifier for this buffer, typically the filename it was read from.
Reimplemented in llvm::SmallVectorMemoryBuffer.
Definition at line 76 of file MemoryBuffer.h.
Referenced by llvm::sampleprof::SampleProfileReaderItaniumRemapper::applyRemapping(), llvm::pdb::InputFile::getFilePath(), getMemBufferRef(), llvm::SourceMgr::GetMessage(), llvm::SIMachineFunctionInfo::initializeBaseYamlFields(), llvm::MCContext::MCContext(), llvm::orc::objDesc(), and llvm::GCNTargetMachine::parseMachineFunctionInfo().
|
pure virtual |
Return information on the memory mechanism used to support the MemoryBuffer.
Implemented in llvm::SmallVectorMemoryBuffer.
|
inline |
Definition at line 68 of file MemoryBuffer.h.
Referenced by llvm::FileCheck::CanonicalizeFile(), llvm::DiffFilesWithTolerance(), getBuffer(), GetOrCreateOffsetCache(), llvm::IndexedCodeGenDataReader::hasFormat(), llvm::TextInstrProfReader::hasFormat(), llvm::RawInstrProfReader< IntPtrT >::hasFormat(), llvm::IndexedInstrProfReader::hasFormat(), llvm::memprof::RawMemProfReader::hasFormat(), and llvm::streamFile().
Definition at line 66 of file MemoryBuffer.h.
Referenced by llvm::FileCheck::CanonicalizeFile(), llvm::RuntimeDyldCheckerImpl::checkAllRulesInBuffer(), llvm::DiffFilesWithTolerance(), llvm::WritableMemoryBuffer::getBufferStart(), llvm::WriteThroughMemoryBuffer::getBufferStart(), llvm::SourceMgr::GetMessage(), llvm::IndexedCodeGenDataReader::hasFormat(), llvm::TextInstrProfReader::hasFormat(), llvm::RawInstrProfReader< IntPtrT >::hasFormat(), llvm::IndexedInstrProfReader::hasFormat(), llvm::memprof::RawMemProfReader::hasFormat(), llvm::CodeGenCoverage::parse(), and llvm::memprof::RawMemProfReader::peekBuildIds().
|
static |
Open the specified file as a MemoryBuffer, returning a new MemoryBuffer if successful, otherwise returning null.
IsText | Set to true to indicate that the file should be read in text mode. |
IsVolatile | Set to true to indicate that the contents of the file can change outside the user's control, e.g. when libclang tries to parse while the user is editing/updating the file or if the file is on an NFS. |
Alignment | Set to indicate that the buffer should be aligned to at least the specified alignment. |
Definition at line 261 of file MemoryBuffer.cpp.
Referenced by llvm::MachO::DylibReader::accumulateSourceLocFromDSYM(), llvm::LTOCodeGenerator::compileOptimized(), llvm::computeLTOCacheKey(), llvm::orc::COFFPlatform::Create(), llvm::LTOModule::createFromFile(), llvm::objcopy::coff::createGnuDebugLinkSectionContents(), llvm::object::ObjectFile::createObjectFile(), llvm::remarks::createYAMLParserFromMeta(), llvm::DiffFilesWithTolerance(), doList(), llvm::doSystemDiff(), llvm::findVCToolChainViaSetupConfig(), llvm::InstrProfCorrelator::get(), llvm::codegen::getBBSectionsMode(), llvm::object::Archive::Child::getBuffer(), getFileOrSTDIN(), llvm::getHeaders(), llvm::unittest::getInputFileDirectory(), llvm::identify_magic(), llvm::LTOModule::isBitcodeFile(), llvm::libDriverMain(), LLVMCreateMemoryBufferWithContentsOfFile(), llvm::orc::loadMachORelocatableObject(), llvm::OpenMPIRBuilder::loadOffloadInfoMetadata(), loadPdbFile(), llvm::orc::loadRelocatableObject(), llvm::MemProfContextDisambiguation::MemProfContextDisambiguation(), llvm::pdb::InputFile::open(), openFile(), llvm::SourceMgr::OpenIncludeFile(), llvm::orc::ExecutorNativePlatform::operator()(), llvm::SymbolRewriter::RewriteMapParser::parse(), parseCHRFilterFiles(), printSymbolizedStackTrace(), llvm::CtxProfAnalysis::run(), llvm::runFuzzerOnInputs(), llvm::TableGenMain(), llvm::lto::thinBackend(), and llvm::ifs::writeELFBinaryToFile().
|
static |
Read all of the specified file into a MemoryBuffer as a stream (i.e.
until EOF reached). This is useful for special files that look like a regular file but have 0 size (e.g. /proc/cpuinfo on Linux).
Definition at line 566 of file MemoryBuffer.cpp.
References llvm::sys::fs::closeFile(), llvm::errorToErrorCode(), getMemoryBufferForStream(), llvm::sys::fs::OF_None, llvm::sys::fs::openNativeFileForRead(), and llvm::Expected< T >::takeError().
Referenced by getProcCpuinfoContent().
|
static |
Open the specified file as a MemoryBuffer, or open stdin if the Filename is "-".
Definition at line 164 of file MemoryBuffer.cpp.
References getFile(), and getSTDIN().
Referenced by llvm::memprof::RawMemProfReader::create(), llvm::object::createBinary(), llvm::createMIRParserFromFile(), llvm::getLazyIRFileModule(), llvm::getModuleSummaryIndexForFile(), llvm::memprof::RawMemProfReader::hasFormat(), llvm::gsym::GsymReader::openFile(), llvm::parseAssemblyFile(), parseAssemblyFileWithIndex(), llvm::parseIRFile(), llvm::parseSummaryIndexAssemblyFile(), llvm::ReplayInlineAdvisor::ReplayInlineAdvisor(), llvm::ForceFunctionAttrsPass::run(), searchForObj(), and llvm::TableGenMain().
|
static |
Map a subrange of the specified file as a MemoryBuffer.
Definition at line 177 of file MemoryBuffer.cpp.
References llvm::Offset.
Referenced by llvm::orc::StaticLibraryDefinitionGenerator::Load(), and llvm::orc::loadMachORelocatableObjectFromUniversalBinary().
|
static |
Definition at line 138 of file MemoryBuffer.cpp.
References getMemBuffer(), and llvm::Ref.
|
static |
Open the specified memory range as a MemoryBuffer.
Note that InputData must be null terminated if RequiresNullTerminator is true.
Definition at line 130 of file MemoryBuffer.cpp.
Referenced by llvm::vfs::InMemoryFileSystem::addFileNoOwn(), getMemBuffer(), llvm::NewArchiveMember::getOldMember(), llvm::MIRParserImpl::initializeMachineFunction(), initializeRecordStreamer(), LLVMBinaryCopyMemoryBuffer(), LLVMCreateMemoryBufferWithMemoryRange(), loadBinaryFormat(), loadTestingFormat(), llvm::LTOModule::makeBuffer(), parseAssemblyInto(), llvm::parseConstantValue(), llvm::parseDIExpressionBodyAtBeginning(), llvm::parseModule(), parseSummaryIndexAssemblyInto(), llvm::parseType(), llvm::parseTypeAtBeginning(), llvm::ForceFunctionAttrsPass::run(), llvm::orc::COFFPlatform::setupJITDylib(), and llvm::orc::StaticLibraryDefinitionGenerator::tryToGenerate().
|
static |
Open the specified memory range as a MemoryBuffer, copying the contents and taking ownership of it.
InputData does not have to be null terminated.
Definition at line 156 of file MemoryBuffer.cpp.
References getMemBufferCopyImpl().
Referenced by llvm::object::OffloadFile::copy(), llvm::gsym::GsymReader::copyBuffer(), llvm::FileCheckPatternContext::defineCmdlineVariables(), emitDebugSectionImpl(), llvm::objcopy::wasm::handleArgs(), LLVMCreateMemoryBufferWithMemoryRangeCopy(), LLVMWriteBitcodeToMemoryBuffer(), llvm::localCache(), llvm::FileCheck::readCheckFile(), llvm::InstrProfWriter::writeBuffer(), and llvm::yaml::yaml2offload().
MemoryBufferRef MemoryBuffer::getMemBufferRef | ( | ) | const |
Definition at line 578 of file MemoryBuffer.cpp.
References llvm::Data, getBuffer(), and getBufferIdentifier().
Referenced by llvm::LTOModule::getProducerString(), and llvm::LTOModule::isBitcodeForTarget().
|
static |
Given an already-open file descriptor, read the file and return a MemoryBuffer.
IsVolatile | Set to true to indicate that the contents of the file can change outside the user's control, e.g. when libclang tries to parse while the user is editing/updating the file or if the file is on an NFS. |
Alignment | Set to indicate that the buffer should be aligned to at least the specified alignment. |
Definition at line 539 of file MemoryBuffer.cpp.
Referenced by llvm::NewArchiveMember::getFile(), llvm::localCache(), and llvm::streamFile().
|
static |
Given an already-open file descriptor, map some slice of it into a MemoryBuffer.
The slice is specified by an Offset
and MapSize
. Since this is in the middle of a file, the buffer is not null terminated.
Definition at line 547 of file MemoryBuffer.cpp.
References assert(), and llvm::Offset.
Referenced by llvm::LTOModule::createFromOpenFileSlice().
|
static |
Read all of stdin into a file buffer, and return it.
Definition at line 555 of file MemoryBuffer.cpp.
References llvm::sys::ChangeStdinMode(), getMemoryBufferForStream(), llvm::sys::fs::getStdinHandle(), and llvm::sys::fs::OF_Text.
Referenced by getFileOrSTDIN(), LLVMCreateMemoryBufferWithSTDIN(), llvm::setupMemoryBuffer(), and setupMemoryBuffer().
|
protected |
init - Initialize this MemoryBuffer as a reference to externally allocated memory, memory that we know is already null terminated.
Definition at line 51 of file MemoryBuffer.cpp.
References assert().
Referenced by llvm::SmallVectorMemoryBuffer::SmallVectorMemoryBuffer().
|
delete |