30 template <
typename Base>
class MappedBlockStreamImpl :
public Base {
32 template <
typename...
Args>
33 MappedBlockStreamImpl(
Args &&... Params)
39 using Interval = std::pair<uint32_t, uint32_t>;
42 return std::make_pair(
std::max(I1.first, I2.first),
43 std::min(I1.second, I2.second));
50 : BlockSize(BlockSize), StreamLayout(Layout), MsfData(MsfData),
51 Allocator(Allocator) {}
56 return llvm::make_unique<MappedBlockStreamImpl<MappedBlockStream>>(
57 BlockSize, Layout, MsfData, Allocator);
63 assert(StreamIndex < Layout.
StreamMap.size() &&
"Invalid stream index");
67 return llvm::make_unique<MappedBlockStreamImpl<MappedBlockStream>>(
71 std::unique_ptr<MappedBlockStream>
81 std::unique_ptr<MappedBlockStream>
95 if (tryReadContiguously(Offset, Size, Buffer))
98 auto CacheIter = CacheMap.find(Offset);
99 if (CacheIter != CacheMap.end()) {
101 for (
auto &Entry : CacheIter->second) {
102 if (Entry.size() >=
Size) {
103 Buffer = Entry.
slice(0, Size);
112 for (
auto &CacheItem : CacheMap) {
113 Interval RequestExtent = std::make_pair(Offset, Offset + Size);
116 if (CacheItem.first == Offset)
120 if (CacheItem.first >= Offset + Size)
125 if (CacheItem.second.empty())
128 auto CachedAlloc = CacheItem.second.back();
132 std::make_pair(CacheItem.first, CacheItem.first + CachedAlloc.size());
133 if (RequestExtent.first >= CachedExtent.first + CachedExtent.second)
139 if (Intersection != RequestExtent)
144 Buffer = CachedAlloc.
slice(CacheRangeOffset, Size);
152 uint8_t *WriteBuffer =
static_cast<uint8_t *
>(Allocator.
Allocate(Size, 8));
156 if (CacheIter != CacheMap.end()) {
157 CacheIter->second.emplace_back(WriteBuffer, Size);
159 std::vector<CacheEntry>
List;
160 List.emplace_back(WriteBuffer, Size);
161 CacheMap.insert(std::make_pair(Offset, List));
173 uint32_t First = Offset / BlockSize;
177 if (StreamLayout.
Blocks[Last] != StreamLayout.
Blocks[Last + 1] - 1)
182 uint32_t OffsetInFirstBlock = Offset % BlockSize;
183 uint32_t BytesFromFirstBlock = BlockSize - OffsetInFirstBlock;
184 uint32_t BlockSpan = Last - First + 1;
185 uint32_t ByteSpan = BytesFromFirstBlock + (BlockSpan - 1) * BlockSize;
189 if (
auto EC = MsfData.
readBytes(MsfOffset, BlockSize, BlockData))
192 BlockData = BlockData.drop_front(OffsetInFirstBlock);
210 uint32_t BlockNum = Offset / BlockSize;
211 uint32_t OffsetInBlock = Offset % BlockSize;
212 uint32_t BytesFromFirstBlock = std::min(Size, BlockSize - OffsetInBlock);
214 alignTo(Size - BytesFromFirstBlock, BlockSize) / BlockSize;
216 uint32_t RequiredContiguousBlocks = NumAdditionalBlocks + 1;
218 for (
uint32_t I = 0;
I < RequiredContiguousBlocks; ++
I, ++
E) {
219 if (StreamLayout.
Blocks[
I + BlockNum] != E)
231 if (
auto EC = MsfData.
readBytes(MsfOffset, BlockSize, BlockData)) {
235 BlockData = BlockData.
drop_front(OffsetInBlock);
242 uint32_t BlockNum = Offset / BlockSize;
243 uint32_t OffsetInBlock = Offset % BlockSize;
251 uint8_t *WriteBuffer = Buffer.
data();
252 while (BytesLeft > 0) {
257 if (
auto EC = MsfData.
readBytes(Offset, BlockSize, BlockData))
260 const uint8_t *ChunkStart = BlockData.
data() + OffsetInBlock;
261 uint32_t BytesInChunk = std::min(BytesLeft, BlockSize - OffsetInBlock);
262 ::memcpy(WriteBuffer + BytesWritten, ChunkStart, BytesInChunk);
264 BytesWritten += BytesInChunk;
265 BytesLeft -= BytesInChunk;
275 void MappedBlockStream::fixCacheAfterWrite(
uint32_t Offset,
281 for (
const auto &MapEntry : CacheMap) {
284 if (Offset + Data.
size() < MapEntry.first)
286 for (
const auto &Alloc : MapEntry.second) {
289 if (MapEntry.first + Alloc.size() <
Offset)
293 Interval WriteInterval = std::make_pair(Offset, Offset + Data.
size());
295 std::make_pair(MapEntry.first, MapEntry.first + Alloc.size());
298 auto Intersection =
intersect(WriteInterval, CachedInterval);
299 assert(Intersection.first <= Intersection.second);
301 uint32_t Length = Intersection.second - Intersection.first;
306 ::memcpy(Alloc.data() + DestOffset, Data.
data() + SrcOffset, Length);
314 : ReadInterface(BlockSize, Layout, MsfData, Allocator),
315 WriteInterface(MsfData) {}
317 std::unique_ptr<WritableMappedBlockStream>
322 return llvm::make_unique<MappedBlockStreamImpl<WritableMappedBlockStream>>(
326 std::unique_ptr<WritableMappedBlockStream>
331 assert(StreamIndex < Layout.
StreamMap.size() &&
"Invalid stream index");
338 std::unique_ptr<WritableMappedBlockStream>
348 std::unique_ptr<WritableMappedBlockStream>
367 std::vector<uint8_t> InitData(Layout.
SB->
BlockSize, 0xFF);
376 return ReadInterface.
readBytes(Offset, Size, Buffer);
399 while (BytesLeft > 0) {
404 const uint8_t *Chunk = Buffer.
data() + BytesWritten;
407 MsfOffset += OffsetInBlock;
408 if (
auto EC = WriteInterface.
writeBytes(MsfOffset, ChunkData))
411 BytesLeft -= BytesToWriteInChunk;
412 BytesWritten += BytesToWriteInChunk;
417 ReadInterface.fixCacheAfterWrite(Offset, Buffer);
void cantFail(Error Err, const char *Msg=nullptr)
Report a fatal error if Err is a failure value.
uint32_t getLength() override
Return the number of bytes of data in this stream.
Error writeBytes(ArrayRef< uint8_t > Buffer)
Write the bytes specified in Buffer to the underlying stream.
static std::unique_ptr< WritableMappedBlockStream > createDirectoryStream(const MSFLayout &Layout, WritableBinaryStreamRef MsfData, BumpPtrAllocator &Allocator)
GCNRegPressure max(const GCNRegPressure &P1, const GCNRegPressure &P2)
This class represents lattice values for constants.
Interval Class - An Interval is a set of nodes defined such that every node in the interval has all o...
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 checkOffsetForWrite(uint32_t Offset, uint32_t DataSize)
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.
Describes the layout of a stream in an MSF layout.
MSFStreamLayout getFpmStreamLayout(const MSFLayout &Msf, bool IncludeUnusedFpmData=false, bool AltFpm=false)
Determine the layout of the FPM stream, given the MSF layout.
Error checkOffsetForRead(uint32_t Offset, uint32_t DataSize)
static std::unique_ptr< MappedBlockStream > createIndexedStream(const MSFLayout &Layout, BinaryStreamRef MsfData, uint32_t StreamIndex, BumpPtrAllocator &Allocator)
const MSFStreamLayout & getStreamLayout() const
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.
std::vector< support::ulittle32_t > Blocks
support::ulittle32_t BlockSize
static std::unique_ptr< MappedBlockStream > createDirectoryStream(const MSFLayout &Layout, BinaryStreamRef MsfData, BumpPtrAllocator &Allocator)
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...
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)
size_t size() const
size - Get the array size.
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
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)
uint32_t getNumBlocks() const
Provides write only access to a subclass of WritableBinaryStream.
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 bytesRemaining() const
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.
static std::unique_ptr< WritableMappedBlockStream > createFpmStream(const MSFLayout &Layout, WritableBinaryStreamRef MsfData, BumpPtrAllocator &Allocator, bool AltFpm=false)
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...
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)
ArrayRef< T > drop_front(size_t N=1) const
Drop the first N elements of the array.
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...
uint32_t getBlockSize() const
std::vector< ArrayRef< support::ulittle32_t > > StreamMap
ArrayRef< support::ulittle32_t > StreamSizes
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
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...
Lightweight error class with error context and mandatory checking.
support::ulittle32_t NumDirectoryBytes
constexpr char Args[]
Key for Kernel::Metadata::mArgs.