26 using namespace llvm::msf;
30 template <
typename Base>
class MappedBlockStreamImpl :
public Base {
32 template <
typename...
Args>
33 MappedBlockStreamImpl(
Args &&... Params)
34 :
Base(std::forward<
Args>(Params)...) {}
43 assert(FpmBlock == 1 || FpmBlock == 2);
44 while (NumFpmIntervals > 0) {
45 FpmLayout.
Blocks.push_back(FpmBlock);
52 using Interval = std::pair<uint32_t, uint32_t>;
55 return std::make_pair(
std::max(I1.first, I2.first),
63 : BlockSize(BlockSize), StreamLayout(Layout), MsfData(MsfData),
64 Allocator(Allocator) {}
69 return llvm::make_unique<MappedBlockStreamImpl<MappedBlockStream>>(
70 BlockSize, Layout, MsfData, Allocator);
76 assert(StreamIndex < Layout.
StreamMap.size() &&
"Invalid stream index");
80 return llvm::make_unique<MappedBlockStreamImpl<MappedBlockStream>>(
84 std::unique_ptr<MappedBlockStream>
94 std::unique_ptr<MappedBlockStream>
109 if (tryReadContiguously(Offset, Size, Buffer))
112 auto CacheIter = CacheMap.find(Offset);
113 if (CacheIter != CacheMap.end()) {
115 for (
auto &Entry : CacheIter->second) {
116 if (Entry.size() >=
Size) {
117 Buffer = Entry.
slice(0, Size);
126 for (
auto &CacheItem : CacheMap) {
127 Interval RequestExtent = std::make_pair(Offset, Offset + Size);
130 if (CacheItem.first == Offset)
134 if (CacheItem.first >= Offset + Size)
139 if (CacheItem.second.empty())
142 auto CachedAlloc = CacheItem.second.back();
146 std::make_pair(CacheItem.first, CacheItem.first + CachedAlloc.size());
147 if (RequestExtent.first >= CachedExtent.first + CachedExtent.second)
153 if (Intersection != RequestExtent)
158 Buffer = CachedAlloc.
slice(CacheRangeOffset, Size);
166 uint8_t *WriteBuffer =
static_cast<uint8_t *
>(Allocator.
Allocate(Size, 8));
170 if (CacheIter != CacheMap.end()) {
171 CacheIter->second.emplace_back(WriteBuffer, Size);
173 std::vector<CacheEntry>
List;
174 List.emplace_back(WriteBuffer, Size);
175 CacheMap.insert(std::make_pair(Offset, List));
187 uint32_t First = Offset / BlockSize;
191 if (StreamLayout.
Blocks[Last] != StreamLayout.
Blocks[Last + 1] - 1)
196 uint32_t OffsetInFirstBlock = Offset % BlockSize;
197 uint32_t BytesFromFirstBlock = BlockSize - OffsetInFirstBlock;
198 uint32_t BlockSpan = Last - First + 1;
199 uint32_t ByteSpan = BytesFromFirstBlock + (BlockSpan - 1) * BlockSize;
203 if (
auto EC = MsfData.
readBytes(MsfOffset, BlockSize, BlockData))
206 BlockData = BlockData.drop_front(OffsetInFirstBlock);
224 uint32_t BlockNum = Offset / BlockSize;
225 uint32_t OffsetInBlock = Offset % BlockSize;
228 alignTo(Size - BytesFromFirstBlock, BlockSize) / BlockSize;
230 uint32_t RequiredContiguousBlocks = NumAdditionalBlocks + 1;
232 for (
uint32_t I = 0;
I < RequiredContiguousBlocks; ++
I, ++
E) {
233 if (StreamLayout.
Blocks[
I + BlockNum] != E)
245 if (
auto EC = MsfData.
readBytes(MsfOffset, BlockSize, BlockData)) {
249 BlockData = BlockData.
drop_front(OffsetInBlock);
256 uint32_t BlockNum = Offset / BlockSize;
257 uint32_t OffsetInBlock = Offset % BlockSize;
265 uint8_t *WriteBuffer = Buffer.
data();
266 while (BytesLeft > 0) {
271 if (
auto EC = MsfData.
readBytes(Offset, BlockSize, BlockData))
274 const uint8_t *ChunkStart = BlockData.
data() + OffsetInBlock;
276 ::memcpy(WriteBuffer + BytesWritten, ChunkStart, BytesInChunk);
278 BytesWritten += BytesInChunk;
279 BytesLeft -= BytesInChunk;
289 void MappedBlockStream::fixCacheAfterWrite(
uint32_t Offset,
295 for (
const auto &MapEntry : CacheMap) {
298 if (Offset + Data.
size() < MapEntry.first)
300 for (
const auto &
Alloc : MapEntry.second) {
307 Interval WriteInterval = std::make_pair(Offset, Offset + Data.
size());
309 std::make_pair(MapEntry.first, MapEntry.first +
Alloc.size());
312 auto Intersection =
intersect(WriteInterval, CachedInterval);
313 assert(Intersection.first <= Intersection.second);
315 uint32_t Length = Intersection.second - Intersection.first;
320 ::memcpy(
Alloc.data() + DestOffset, Data.
data() + SrcOffset, Length);
328 : ReadInterface(BlockSize, Layout, MsfData, Allocator),
329 WriteInterface(MsfData) {}
331 std::unique_ptr<WritableMappedBlockStream>
336 return llvm::make_unique<MappedBlockStreamImpl<WritableMappedBlockStream>>(
340 std::unique_ptr<WritableMappedBlockStream>
345 assert(StreamIndex < Layout.
StreamMap.size() &&
"Invalid stream index");
352 std::unique_ptr<WritableMappedBlockStream>
362 std::unique_ptr<WritableMappedBlockStream>
373 return ReadInterface.
readBytes(Offset, Size, Buffer);
396 while (BytesLeft > 0) {
401 const uint8_t *Chunk = Buffer.
data() + BytesWritten;
404 MsfOffset += OffsetInBlock;
405 if (
auto EC = WriteInterface.
writeBytes(MsfOffset, ChunkData))
408 BytesLeft -= BytesToWriteInChunk;
409 BytesWritten += BytesToWriteInChunk;
414 ReadInterface.fixCacheAfterWrite(Offset, Buffer);
uint32_t getLength() override
Return the number of bytes of data in this stream.
static void initializeFpmStreamLayout(const MSFLayout &Layout, MSFStreamLayout &FpmLayout)
static std::unique_ptr< WritableMappedBlockStream > createDirectoryStream(const MSFLayout &Layout, WritableBinaryStreamRef MsfData, BumpPtrAllocator &Allocator)
GCNRegPressure max(const GCNRegPressure &P1, const GCNRegPressure &P2)
Error checkOffset(uint32_t Offset, uint32_t DataSize)
Interval Class - An Interval is a set of nodes defined such that every node in the interval has all o...
Maximum length of the test input libFuzzer tries to guess a good value based on the corpus and reports it always prefer smaller inputs during the corpus shuffle When libFuzzer itself reports a bug this exit code will be used If indicates the maximal total time in seconds to run the fuzzer minimizes the provided crash input Use with tries to cleanse the provided crash input to make it contain fewer original bytes Use with exact_artifact_path to specify the output Experimental Use value profile to guide fuzzing Number of jobs to run If min(jobs, NumberOfCpuCores()/2)\" is used.") FUZZER_FLAG_INT(reload
static std::unique_ptr< WritableMappedBlockStream > createStream(uint32_t BlockSize, const MSFStreamLayout &Layout, WritableBinaryStreamRef MsfData, BumpPtrAllocator &Allocator)
uint64_t blockToOffset(uint64_t BlockNumber, uint64_t BlockSize)
Error readBytes(uint32_t Offset, uint32_t Size, ArrayRef< uint8_t > &Buffer) override
Given an offset into the stream and a number of bytes, attempt to read the bytes and set the output A...
uint32_t getLength() override
Return the number of bytes of data in this stream.
Error commit() override
For buffered streams, commits changes to the backing store.
static Interval intersect(const Interval &I1, const Interval &I2)
uint64_t alignTo(uint64_t Value, uint64_t Align, uint64_t Skew=0)
Returns the next integer (mod 2**64) that is greater than or equal to Value and is a multiple of Alig...
Error writeBytes(uint32_t Offset, ArrayRef< uint8_t > Buffer) override
Attempt to write the given bytes into the stream at the desired offset.
Error readLongestContiguousChunk(uint32_t Offset, ArrayRef< uint8_t > &Buffer) override
Given an offset into the stream, read as much as possible without copying any data.
ArrayRef< T > slice(size_t N, size_t M) const
slice(n, m) - Chop off the first N elements of the array, and keep M elements in the array...
Describes the layout of a stream in an MSF layout.
static std::unique_ptr< MappedBlockStream > createIndexedStream(const MSFLayout &Layout, BinaryStreamRef MsfData, uint32_t StreamIndex, BumpPtrAllocator &Allocator)
Error readLongestContiguousChunk(uint32_t Offset, ArrayRef< uint8_t > &Buffer) override
Given an offset into the stream, read as much as possible without copying any data.
uint32_t getFpmIntervalLength(const MSFLayout &L)
std::vector< support::ulittle32_t > Blocks
uint32_t getNumBlocks() const
support::ulittle32_t BlockSize
size_t size() const
size - Get the array size.
static std::unique_ptr< MappedBlockStream > createDirectoryStream(const MSFLayout &Layout, BinaryStreamRef MsfData, BumpPtrAllocator &Allocator)
Allocate memory in an ever growing pool, as if by bump-pointer.
static std::unique_ptr< MappedBlockStream > createFpmStream(const MSFLayout &Layout, BinaryStreamRef MsfData, BumpPtrAllocator &Allocator)
Error writeBytes(uint32_t Offset, ArrayRef< uint8_t > Data) const
Given an Offset into this WritableBinaryStreamRef and some input data, writes the data to the underly...
constexpr char Args[]
Key for Kernel::Metadata::mArgs.
LLVM_ATTRIBUTE_RETURNS_NONNULL LLVM_ATTRIBUTE_RETURNS_NOALIAS void * Allocate(size_t Size, size_t Alignment)
Allocate space at the specified alignment.
static std::unique_ptr< MappedBlockStream > createStream(uint32_t BlockSize, const MSFStreamLayout &Layout, BinaryStreamRef MsfData, BumpPtrAllocator &Allocator)
static std::unique_ptr< WritableMappedBlockStream > createFpmStream(const MSFLayout &Layout, WritableBinaryStreamRef MsfData, BumpPtrAllocator &Allocator)
Error readBytes(uint32_t Offset, uint32_t Size, ArrayRef< uint8_t > &Buffer) override
Given an offset into the stream and a number of bytes, attempt to read the bytes and set the output A...
void consumeError(Error Err)
Consume a Error without doing anything.
uint32_t getNumFpmIntervals(const MSFLayout &L)
static const int BlockSize
Error commit()
For buffered streams, commits changes to the backing store.
static ErrorSuccess success()
Create a success value.
ArrayRef< support::ulittle32_t > DirectoryBlocks
BinaryStreamRef is to BinaryStream what ArrayRef is to an Array.
constexpr char Size[]
Key for Kernel::Arg::Metadata::mSize.
MappedBlockStream(uint32_t BlockSize, const MSFStreamLayout &StreamLayout, BinaryStreamRef MsfData, BumpPtrAllocator &Allocator)
static std::unique_ptr< WritableMappedBlockStream > createIndexedStream(const MSFLayout &Layout, WritableBinaryStreamRef MsfData, uint32_t StreamIndex, BumpPtrAllocator &Allocator)
WritableMappedBlockStream(uint32_t BlockSize, const MSFStreamLayout &StreamLayout, WritableBinaryStreamRef MsfData, BumpPtrAllocator &Allocator)
std::enable_if< std::is_unsigned< T >::value, T >::type AbsoluteDifference(T X, T Y)
Subtract two unsigned integers, X and Y, of type T and return the absolute value of the result...
std::vector< ArrayRef< support::ulittle32_t > > StreamMap
uint32_t getFullFpmByteSize(const MSFLayout &L)
ArrayRef< support::ulittle32_t > StreamSizes
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
ArrayRef< T > drop_front(size_t N=1) const
Drop the first N elements of the array.
support::ulittle32_t FreeBlockMapBlock
Error readBytes(uint32_t Offset, uint32_t Size, ArrayRef< uint8_t > &Buffer) const
Given an Offset into this StreamRef and a Size, return a reference to a buffer owned by the stream...
Lightweight error class with error context and mandatory checking.
uint32_t getBlockSize() const
support::ulittle32_t NumDirectoryBytes
const MSFStreamLayout & getStreamLayout() const