29 template <
typename Base>
class MappedBlockStreamImpl :
public Base {
31 template <
typename...
Args>
32 MappedBlockStreamImpl(
Args &&... Params)
38 using Interval = std::pair<uint32_t, uint32_t>;
41 return std::make_pair(
std::max(I1.first, I2.first),
42 std::min(I1.second, I2.second));
49 : BlockSize(BlockSize), StreamLayout(Layout), MsfData(MsfData),
50 Allocator(Allocator) {}
55 return std::make_unique<MappedBlockStreamImpl<MappedBlockStream>>(
56 BlockSize, Layout, MsfData, Allocator);
62 assert(StreamIndex < Layout.
StreamMap.size() &&
"Invalid stream index");
66 return std::make_unique<MappedBlockStreamImpl<MappedBlockStream>>(
70 std::unique_ptr<MappedBlockStream>
80 std::unique_ptr<MappedBlockStream>
94 if (tryReadContiguously(Offset, Size, Buffer))
97 auto CacheIter = CacheMap.find(Offset);
98 if (CacheIter != CacheMap.end()) {
100 for (
auto &
Entry : CacheIter->second) {
102 Buffer =
Entry.slice(0, Size);
111 for (
auto &CacheItem : CacheMap) {
112 Interval RequestExtent = std::make_pair(Offset, Offset + Size);
115 if (CacheItem.first == Offset)
119 if (CacheItem.first >= Offset + Size)
124 if (CacheItem.second.empty())
127 auto CachedAlloc = CacheItem.second.back();
131 std::make_pair(CacheItem.first, CacheItem.first + CachedAlloc.size());
132 if (RequestExtent.first >= CachedExtent.first + CachedExtent.second)
138 if (Intersection != RequestExtent)
143 Buffer = CachedAlloc.
slice(CacheRangeOffset, Size);
151 uint8_t *WriteBuffer =
static_cast<uint8_t *
>(Allocator.
Allocate(Size, 8));
155 if (CacheIter != CacheMap.end()) {
156 CacheIter->second.emplace_back(WriteBuffer, Size);
158 std::vector<CacheEntry>
List;
159 List.emplace_back(WriteBuffer, Size);
160 CacheMap.insert(std::make_pair(Offset, List));
172 uint32_t First = Offset / BlockSize;
176 if (StreamLayout.
Blocks[Last] != StreamLayout.
Blocks[Last + 1] - 1)
181 uint32_t OffsetInFirstBlock = Offset % BlockSize;
182 uint32_t BytesFromFirstBlock = BlockSize - OffsetInFirstBlock;
183 uint32_t BlockSpan = Last - First + 1;
184 uint32_t ByteSpan = BytesFromFirstBlock + (BlockSpan - 1) * BlockSize;
188 if (
auto EC = MsfData.
readBytes(MsfOffset, BlockSize, BlockData))
191 BlockData = BlockData.drop_front(OffsetInFirstBlock);
209 uint32_t BlockNum = Offset / BlockSize;
210 uint32_t OffsetInBlock = Offset % BlockSize;
211 uint32_t BytesFromFirstBlock = std::min(Size, BlockSize - OffsetInBlock);
213 alignTo(Size - BytesFromFirstBlock, BlockSize) / BlockSize;
215 uint32_t RequiredContiguousBlocks = NumAdditionalBlocks + 1;
217 for (
uint32_t I = 0;
I < RequiredContiguousBlocks; ++
I, ++
E) {
218 if (StreamLayout.
Blocks[
I + BlockNum] != E)
230 if (
auto EC = MsfData.
readBytes(MsfOffset, BlockSize, BlockData)) {
234 BlockData = BlockData.
drop_front(OffsetInBlock);
241 uint32_t BlockNum = Offset / BlockSize;
242 uint32_t OffsetInBlock = Offset % BlockSize;
250 uint8_t *WriteBuffer = Buffer.
data();
251 while (BytesLeft > 0) {
256 if (
auto EC = MsfData.
readBytes(Offset, BlockSize, BlockData))
259 const uint8_t *ChunkStart = BlockData.
data() + OffsetInBlock;
260 uint32_t BytesInChunk = std::min(BytesLeft, BlockSize - OffsetInBlock);
261 ::memcpy(WriteBuffer + BytesWritten, ChunkStart, BytesInChunk);
263 BytesWritten += BytesInChunk;
264 BytesLeft -= BytesInChunk;
274 void MappedBlockStream::fixCacheAfterWrite(
uint32_t Offset,
280 for (
const auto &MapEntry : CacheMap) {
283 if (Offset + Data.
size() < MapEntry.first)
285 for (
const auto &Alloc : MapEntry.second) {
288 if (MapEntry.first + Alloc.size() <
Offset)
292 Interval WriteInterval = std::make_pair(Offset, Offset + Data.
size());
294 std::make_pair(MapEntry.first, MapEntry.first + Alloc.size());
297 auto Intersection =
intersect(WriteInterval, CachedInterval);
298 assert(Intersection.first <= Intersection.second);
300 uint32_t Length = Intersection.second - Intersection.first;
305 ::memcpy(Alloc.data() + DestOffset, Data.
data() + SrcOffset, Length);
313 : ReadInterface(BlockSize, Layout, MsfData, Allocator),
314 WriteInterface(MsfData) {}
316 std::unique_ptr<WritableMappedBlockStream>
321 return std::make_unique<MappedBlockStreamImpl<WritableMappedBlockStream>>(
325 std::unique_ptr<WritableMappedBlockStream>
330 assert(StreamIndex < Layout.
StreamMap.size() &&
"Invalid stream index");
337 std::unique_ptr<WritableMappedBlockStream>
347 std::unique_ptr<WritableMappedBlockStream>
366 std::vector<uint8_t> InitData(Layout.
SB->
BlockSize, 0xFF);
375 return ReadInterface.
readBytes(Offset, Size, Buffer);
398 while (BytesLeft > 0) {
403 const uint8_t *Chunk = Buffer.
data() + BytesWritten;
406 MsfOffset += OffsetInBlock;
407 if (
auto EC = WriteInterface.
writeBytes(MsfOffset, ChunkData))
410 BytesLeft -= BytesToWriteInChunk;
411 BytesWritten += BytesToWriteInChunk;
416 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)
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)
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")
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.
Align max(MaybeAlign Lhs, Align Rhs)
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)
LLVM_ATTRIBUTE_RETURNS_NONNULL LLVM_ATTRIBUTE_RETURNS_NOALIAS void * Allocate(size_t Size, Align Alignment)
Allocate space at the specified alignment.
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)
uint64_t alignTo(uint64_t Size, Align A)
Returns a multiple of A needed to store Size bytes.
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.