10b57cec5SDimitry Andric //===- MappedBlockStream.cpp - Reads stream data from an MSF file ---------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric
90b57cec5SDimitry Andric #include "llvm/DebugInfo/MSF/MappedBlockStream.h"
100b57cec5SDimitry Andric #include "llvm/ADT/ArrayRef.h"
110b57cec5SDimitry Andric #include "llvm/DebugInfo/MSF/MSFCommon.h"
120b57cec5SDimitry Andric #include "llvm/Support/BinaryStreamWriter.h"
130b57cec5SDimitry Andric #include "llvm/Support/Error.h"
140b57cec5SDimitry Andric #include "llvm/Support/MathExtras.h"
150b57cec5SDimitry Andric #include <algorithm>
160b57cec5SDimitry Andric #include <cassert>
170b57cec5SDimitry Andric #include <cstdint>
180b57cec5SDimitry Andric #include <cstring>
190b57cec5SDimitry Andric #include <utility>
200b57cec5SDimitry Andric #include <vector>
210b57cec5SDimitry Andric
220b57cec5SDimitry Andric using namespace llvm;
230b57cec5SDimitry Andric using namespace llvm::msf;
240b57cec5SDimitry Andric
250b57cec5SDimitry Andric namespace {
260b57cec5SDimitry Andric
270b57cec5SDimitry Andric template <typename Base> class MappedBlockStreamImpl : public Base {
280b57cec5SDimitry Andric public:
290b57cec5SDimitry Andric template <typename... Args>
MappedBlockStreamImpl(Args &&...Params)300b57cec5SDimitry Andric MappedBlockStreamImpl(Args &&... Params)
310b57cec5SDimitry Andric : Base(std::forward<Args>(Params)...) {}
320b57cec5SDimitry Andric };
330b57cec5SDimitry Andric
340b57cec5SDimitry Andric } // end anonymous namespace
350b57cec5SDimitry Andric
36349cc55cSDimitry Andric using Interval = std::pair<uint64_t, uint64_t>;
370b57cec5SDimitry Andric
intersect(const Interval & I1,const Interval & I2)380b57cec5SDimitry Andric static Interval intersect(const Interval &I1, const Interval &I2) {
390b57cec5SDimitry Andric return std::make_pair(std::max(I1.first, I2.first),
400b57cec5SDimitry Andric std::min(I1.second, I2.second));
410b57cec5SDimitry Andric }
420b57cec5SDimitry Andric
MappedBlockStream(uint32_t BlockSize,const MSFStreamLayout & Layout,BinaryStreamRef MsfData,BumpPtrAllocator & Allocator)430b57cec5SDimitry Andric MappedBlockStream::MappedBlockStream(uint32_t BlockSize,
440b57cec5SDimitry Andric const MSFStreamLayout &Layout,
450b57cec5SDimitry Andric BinaryStreamRef MsfData,
460b57cec5SDimitry Andric BumpPtrAllocator &Allocator)
470b57cec5SDimitry Andric : BlockSize(BlockSize), StreamLayout(Layout), MsfData(MsfData),
480b57cec5SDimitry Andric Allocator(Allocator) {}
490b57cec5SDimitry Andric
createStream(uint32_t BlockSize,const MSFStreamLayout & Layout,BinaryStreamRef MsfData,BumpPtrAllocator & Allocator)500b57cec5SDimitry Andric std::unique_ptr<MappedBlockStream> MappedBlockStream::createStream(
510b57cec5SDimitry Andric uint32_t BlockSize, const MSFStreamLayout &Layout, BinaryStreamRef MsfData,
520b57cec5SDimitry Andric BumpPtrAllocator &Allocator) {
538bcb0991SDimitry Andric return std::make_unique<MappedBlockStreamImpl<MappedBlockStream>>(
540b57cec5SDimitry Andric BlockSize, Layout, MsfData, Allocator);
550b57cec5SDimitry Andric }
560b57cec5SDimitry Andric
createIndexedStream(const MSFLayout & Layout,BinaryStreamRef MsfData,uint32_t StreamIndex,BumpPtrAllocator & Allocator)570b57cec5SDimitry Andric std::unique_ptr<MappedBlockStream> MappedBlockStream::createIndexedStream(
580b57cec5SDimitry Andric const MSFLayout &Layout, BinaryStreamRef MsfData, uint32_t StreamIndex,
590b57cec5SDimitry Andric BumpPtrAllocator &Allocator) {
600b57cec5SDimitry Andric assert(StreamIndex < Layout.StreamMap.size() && "Invalid stream index");
610b57cec5SDimitry Andric MSFStreamLayout SL;
620b57cec5SDimitry Andric SL.Blocks = Layout.StreamMap[StreamIndex];
630b57cec5SDimitry Andric SL.Length = Layout.StreamSizes[StreamIndex];
648bcb0991SDimitry Andric return std::make_unique<MappedBlockStreamImpl<MappedBlockStream>>(
650b57cec5SDimitry Andric Layout.SB->BlockSize, SL, MsfData, Allocator);
660b57cec5SDimitry Andric }
670b57cec5SDimitry Andric
680b57cec5SDimitry Andric std::unique_ptr<MappedBlockStream>
createDirectoryStream(const MSFLayout & Layout,BinaryStreamRef MsfData,BumpPtrAllocator & Allocator)690b57cec5SDimitry Andric MappedBlockStream::createDirectoryStream(const MSFLayout &Layout,
700b57cec5SDimitry Andric BinaryStreamRef MsfData,
710b57cec5SDimitry Andric BumpPtrAllocator &Allocator) {
720b57cec5SDimitry Andric MSFStreamLayout SL;
730b57cec5SDimitry Andric SL.Blocks = Layout.DirectoryBlocks;
740b57cec5SDimitry Andric SL.Length = Layout.SB->NumDirectoryBytes;
750b57cec5SDimitry Andric return createStream(Layout.SB->BlockSize, SL, MsfData, Allocator);
760b57cec5SDimitry Andric }
770b57cec5SDimitry Andric
780b57cec5SDimitry Andric std::unique_ptr<MappedBlockStream>
createFpmStream(const MSFLayout & Layout,BinaryStreamRef MsfData,BumpPtrAllocator & Allocator)790b57cec5SDimitry Andric MappedBlockStream::createFpmStream(const MSFLayout &Layout,
800b57cec5SDimitry Andric BinaryStreamRef MsfData,
810b57cec5SDimitry Andric BumpPtrAllocator &Allocator) {
820b57cec5SDimitry Andric MSFStreamLayout SL(getFpmStreamLayout(Layout));
830b57cec5SDimitry Andric return createStream(Layout.SB->BlockSize, SL, MsfData, Allocator);
840b57cec5SDimitry Andric }
850b57cec5SDimitry Andric
readBytes(uint64_t Offset,uint64_t Size,ArrayRef<uint8_t> & Buffer)86349cc55cSDimitry Andric Error MappedBlockStream::readBytes(uint64_t Offset, uint64_t Size,
870b57cec5SDimitry Andric ArrayRef<uint8_t> &Buffer) {
880b57cec5SDimitry Andric // Make sure we aren't trying to read beyond the end of the stream.
890b57cec5SDimitry Andric if (auto EC = checkOffsetForRead(Offset, Size))
900b57cec5SDimitry Andric return EC;
910b57cec5SDimitry Andric
920b57cec5SDimitry Andric if (tryReadContiguously(Offset, Size, Buffer))
930b57cec5SDimitry Andric return Error::success();
940b57cec5SDimitry Andric
950b57cec5SDimitry Andric auto CacheIter = CacheMap.find(Offset);
960b57cec5SDimitry Andric if (CacheIter != CacheMap.end()) {
970b57cec5SDimitry Andric // Try to find an alloc that was large enough for this request.
980b57cec5SDimitry Andric for (auto &Entry : CacheIter->second) {
990b57cec5SDimitry Andric if (Entry.size() >= Size) {
1000b57cec5SDimitry Andric Buffer = Entry.slice(0, Size);
1010b57cec5SDimitry Andric return Error::success();
1020b57cec5SDimitry Andric }
1030b57cec5SDimitry Andric }
1040b57cec5SDimitry Andric }
1050b57cec5SDimitry Andric
1060b57cec5SDimitry Andric // We couldn't find a buffer that started at the correct offset (the most
1070b57cec5SDimitry Andric // common scenario). Try to see if there is a buffer that starts at some
1080b57cec5SDimitry Andric // other offset but overlaps the desired range.
1090b57cec5SDimitry Andric for (auto &CacheItem : CacheMap) {
1100b57cec5SDimitry Andric Interval RequestExtent = std::make_pair(Offset, Offset + Size);
1110b57cec5SDimitry Andric
1120b57cec5SDimitry Andric // We already checked this one on the fast path above.
1130b57cec5SDimitry Andric if (CacheItem.first == Offset)
1140b57cec5SDimitry Andric continue;
1150b57cec5SDimitry Andric // If the initial extent of the cached item is beyond the ending extent
1160b57cec5SDimitry Andric // of the request, there is no overlap.
1170b57cec5SDimitry Andric if (CacheItem.first >= Offset + Size)
1180b57cec5SDimitry Andric continue;
1190b57cec5SDimitry Andric
1200b57cec5SDimitry Andric // We really only have to check the last item in the list, since we append
1210b57cec5SDimitry Andric // in order of increasing length.
1220b57cec5SDimitry Andric if (CacheItem.second.empty())
1230b57cec5SDimitry Andric continue;
1240b57cec5SDimitry Andric
1250b57cec5SDimitry Andric auto CachedAlloc = CacheItem.second.back();
1260b57cec5SDimitry Andric // If the initial extent of the request is beyond the ending extent of
1270b57cec5SDimitry Andric // the cached item, there is no overlap.
1280b57cec5SDimitry Andric Interval CachedExtent =
1290b57cec5SDimitry Andric std::make_pair(CacheItem.first, CacheItem.first + CachedAlloc.size());
1300b57cec5SDimitry Andric if (RequestExtent.first >= CachedExtent.first + CachedExtent.second)
1310b57cec5SDimitry Andric continue;
1320b57cec5SDimitry Andric
1330b57cec5SDimitry Andric Interval Intersection = intersect(CachedExtent, RequestExtent);
1340b57cec5SDimitry Andric // Only use this if the entire request extent is contained in the cached
1350b57cec5SDimitry Andric // extent.
1360b57cec5SDimitry Andric if (Intersection != RequestExtent)
1370b57cec5SDimitry Andric continue;
1380b57cec5SDimitry Andric
139349cc55cSDimitry Andric uint64_t CacheRangeOffset =
1400b57cec5SDimitry Andric AbsoluteDifference(CachedExtent.first, Intersection.first);
1410b57cec5SDimitry Andric Buffer = CachedAlloc.slice(CacheRangeOffset, Size);
1420b57cec5SDimitry Andric return Error::success();
1430b57cec5SDimitry Andric }
1440b57cec5SDimitry Andric
1450b57cec5SDimitry Andric // Otherwise allocate a large enough buffer in the pool, memcpy the data
1460b57cec5SDimitry Andric // into it, and return an ArrayRef to that. Do not touch existing pool
1470b57cec5SDimitry Andric // allocations, as existing clients may be holding a pointer which must
1480b57cec5SDimitry Andric // not be invalidated.
1490b57cec5SDimitry Andric uint8_t *WriteBuffer = static_cast<uint8_t *>(Allocator.Allocate(Size, 8));
1500b57cec5SDimitry Andric if (auto EC = readBytes(Offset, MutableArrayRef<uint8_t>(WriteBuffer, Size)))
1510b57cec5SDimitry Andric return EC;
1520b57cec5SDimitry Andric
1530b57cec5SDimitry Andric if (CacheIter != CacheMap.end()) {
1540b57cec5SDimitry Andric CacheIter->second.emplace_back(WriteBuffer, Size);
1550b57cec5SDimitry Andric } else {
1560b57cec5SDimitry Andric std::vector<CacheEntry> List;
1570b57cec5SDimitry Andric List.emplace_back(WriteBuffer, Size);
1580b57cec5SDimitry Andric CacheMap.insert(std::make_pair(Offset, List));
1590b57cec5SDimitry Andric }
1600b57cec5SDimitry Andric Buffer = ArrayRef<uint8_t>(WriteBuffer, Size);
1610b57cec5SDimitry Andric return Error::success();
1620b57cec5SDimitry Andric }
1630b57cec5SDimitry Andric
readLongestContiguousChunk(uint64_t Offset,ArrayRef<uint8_t> & Buffer)164349cc55cSDimitry Andric Error MappedBlockStream::readLongestContiguousChunk(uint64_t Offset,
1650b57cec5SDimitry Andric ArrayRef<uint8_t> &Buffer) {
1660b57cec5SDimitry Andric // Make sure we aren't trying to read beyond the end of the stream.
1670b57cec5SDimitry Andric if (auto EC = checkOffsetForRead(Offset, 1))
1680b57cec5SDimitry Andric return EC;
1690b57cec5SDimitry Andric
170349cc55cSDimitry Andric uint64_t First = Offset / BlockSize;
171349cc55cSDimitry Andric uint64_t Last = First;
1720b57cec5SDimitry Andric
1730b57cec5SDimitry Andric while (Last < getNumBlocks() - 1) {
1740b57cec5SDimitry Andric if (StreamLayout.Blocks[Last] != StreamLayout.Blocks[Last + 1] - 1)
1750b57cec5SDimitry Andric break;
1760b57cec5SDimitry Andric ++Last;
1770b57cec5SDimitry Andric }
1780b57cec5SDimitry Andric
179349cc55cSDimitry Andric uint64_t OffsetInFirstBlock = Offset % BlockSize;
180349cc55cSDimitry Andric uint64_t BytesFromFirstBlock = BlockSize - OffsetInFirstBlock;
181349cc55cSDimitry Andric uint64_t BlockSpan = Last - First + 1;
182349cc55cSDimitry Andric uint64_t ByteSpan = BytesFromFirstBlock + (BlockSpan - 1) * BlockSize;
1830b57cec5SDimitry Andric
1840b57cec5SDimitry Andric ArrayRef<uint8_t> BlockData;
185349cc55cSDimitry Andric uint64_t MsfOffset = blockToOffset(StreamLayout.Blocks[First], BlockSize);
1860b57cec5SDimitry Andric if (auto EC = MsfData.readBytes(MsfOffset, BlockSize, BlockData))
1870b57cec5SDimitry Andric return EC;
1880b57cec5SDimitry Andric
1890b57cec5SDimitry Andric BlockData = BlockData.drop_front(OffsetInFirstBlock);
1900b57cec5SDimitry Andric Buffer = ArrayRef<uint8_t>(BlockData.data(), ByteSpan);
1910b57cec5SDimitry Andric return Error::success();
1920b57cec5SDimitry Andric }
1930b57cec5SDimitry Andric
getLength()194349cc55cSDimitry Andric uint64_t MappedBlockStream::getLength() { return StreamLayout.Length; }
1950b57cec5SDimitry Andric
tryReadContiguously(uint64_t Offset,uint64_t Size,ArrayRef<uint8_t> & Buffer)196349cc55cSDimitry Andric bool MappedBlockStream::tryReadContiguously(uint64_t Offset, uint64_t Size,
1970b57cec5SDimitry Andric ArrayRef<uint8_t> &Buffer) {
1980b57cec5SDimitry Andric if (Size == 0) {
1990b57cec5SDimitry Andric Buffer = ArrayRef<uint8_t>();
2000b57cec5SDimitry Andric return true;
2010b57cec5SDimitry Andric }
2020b57cec5SDimitry Andric // Attempt to fulfill the request with a reference directly into the stream.
2030b57cec5SDimitry Andric // This can work even if the request crosses a block boundary, provided that
2040b57cec5SDimitry Andric // all subsequent blocks are contiguous. For example, a 10k read with a 4k
2050b57cec5SDimitry Andric // block size can be filled with a reference if, from the starting offset,
2060b57cec5SDimitry Andric // 3 blocks in a row are contiguous.
207349cc55cSDimitry Andric uint64_t BlockNum = Offset / BlockSize;
208349cc55cSDimitry Andric uint64_t OffsetInBlock = Offset % BlockSize;
209349cc55cSDimitry Andric uint64_t BytesFromFirstBlock = std::min(Size, BlockSize - OffsetInBlock);
210349cc55cSDimitry Andric uint64_t NumAdditionalBlocks =
2110b57cec5SDimitry Andric alignTo(Size - BytesFromFirstBlock, BlockSize) / BlockSize;
2120b57cec5SDimitry Andric
213349cc55cSDimitry Andric uint64_t RequiredContiguousBlocks = NumAdditionalBlocks + 1;
214349cc55cSDimitry Andric uint64_t E = StreamLayout.Blocks[BlockNum];
215349cc55cSDimitry Andric for (uint64_t I = 0; I < RequiredContiguousBlocks; ++I, ++E) {
2160b57cec5SDimitry Andric if (StreamLayout.Blocks[I + BlockNum] != E)
2170b57cec5SDimitry Andric return false;
2180b57cec5SDimitry Andric }
2190b57cec5SDimitry Andric
2200b57cec5SDimitry Andric // Read out the entire block where the requested offset starts. Then drop
2210b57cec5SDimitry Andric // bytes from the beginning so that the actual starting byte lines up with
2220b57cec5SDimitry Andric // the requested starting byte. Then, since we know this is a contiguous
2230b57cec5SDimitry Andric // cross-block span, explicitly resize the ArrayRef to cover the entire
2240b57cec5SDimitry Andric // request length.
2250b57cec5SDimitry Andric ArrayRef<uint8_t> BlockData;
226349cc55cSDimitry Andric uint64_t FirstBlockAddr = StreamLayout.Blocks[BlockNum];
227349cc55cSDimitry Andric uint64_t MsfOffset = blockToOffset(FirstBlockAddr, BlockSize);
2280b57cec5SDimitry Andric if (auto EC = MsfData.readBytes(MsfOffset, BlockSize, BlockData)) {
2290b57cec5SDimitry Andric consumeError(std::move(EC));
2300b57cec5SDimitry Andric return false;
2310b57cec5SDimitry Andric }
2320b57cec5SDimitry Andric BlockData = BlockData.drop_front(OffsetInBlock);
2330b57cec5SDimitry Andric Buffer = ArrayRef<uint8_t>(BlockData.data(), Size);
2340b57cec5SDimitry Andric return true;
2350b57cec5SDimitry Andric }
2360b57cec5SDimitry Andric
readBytes(uint64_t Offset,MutableArrayRef<uint8_t> Buffer)237349cc55cSDimitry Andric Error MappedBlockStream::readBytes(uint64_t Offset,
2380b57cec5SDimitry Andric MutableArrayRef<uint8_t> Buffer) {
239349cc55cSDimitry Andric uint64_t BlockNum = Offset / BlockSize;
240349cc55cSDimitry Andric uint64_t OffsetInBlock = Offset % BlockSize;
2410b57cec5SDimitry Andric
2420b57cec5SDimitry Andric // Make sure we aren't trying to read beyond the end of the stream.
2430b57cec5SDimitry Andric if (auto EC = checkOffsetForRead(Offset, Buffer.size()))
2440b57cec5SDimitry Andric return EC;
2450b57cec5SDimitry Andric
246349cc55cSDimitry Andric uint64_t BytesLeft = Buffer.size();
247349cc55cSDimitry Andric uint64_t BytesWritten = 0;
2480b57cec5SDimitry Andric uint8_t *WriteBuffer = Buffer.data();
2490b57cec5SDimitry Andric while (BytesLeft > 0) {
250349cc55cSDimitry Andric uint64_t StreamBlockAddr = StreamLayout.Blocks[BlockNum];
2510b57cec5SDimitry Andric
2520b57cec5SDimitry Andric ArrayRef<uint8_t> BlockData;
253349cc55cSDimitry Andric uint64_t Offset = blockToOffset(StreamBlockAddr, BlockSize);
2540b57cec5SDimitry Andric if (auto EC = MsfData.readBytes(Offset, BlockSize, BlockData))
2550b57cec5SDimitry Andric return EC;
2560b57cec5SDimitry Andric
2570b57cec5SDimitry Andric const uint8_t *ChunkStart = BlockData.data() + OffsetInBlock;
258349cc55cSDimitry Andric uint64_t BytesInChunk = std::min(BytesLeft, BlockSize - OffsetInBlock);
2590b57cec5SDimitry Andric ::memcpy(WriteBuffer + BytesWritten, ChunkStart, BytesInChunk);
2600b57cec5SDimitry Andric
2610b57cec5SDimitry Andric BytesWritten += BytesInChunk;
2620b57cec5SDimitry Andric BytesLeft -= BytesInChunk;
2630b57cec5SDimitry Andric ++BlockNum;
2640b57cec5SDimitry Andric OffsetInBlock = 0;
2650b57cec5SDimitry Andric }
2660b57cec5SDimitry Andric
2670b57cec5SDimitry Andric return Error::success();
2680b57cec5SDimitry Andric }
2690b57cec5SDimitry Andric
invalidateCache()2700b57cec5SDimitry Andric void MappedBlockStream::invalidateCache() { CacheMap.shrink_and_clear(); }
2710b57cec5SDimitry Andric
fixCacheAfterWrite(uint64_t Offset,ArrayRef<uint8_t> Data) const272349cc55cSDimitry Andric void MappedBlockStream::fixCacheAfterWrite(uint64_t Offset,
2730b57cec5SDimitry Andric ArrayRef<uint8_t> Data) const {
2740b57cec5SDimitry Andric // If this write overlapped a read which previously came from the pool,
2750b57cec5SDimitry Andric // someone may still be holding a pointer to that alloc which is now invalid.
2760b57cec5SDimitry Andric // Compute the overlapping range and update the cache entry, so any
2770b57cec5SDimitry Andric // outstanding buffers are automatically updated.
2780b57cec5SDimitry Andric for (const auto &MapEntry : CacheMap) {
2790b57cec5SDimitry Andric // If the end of the written extent precedes the beginning of the cached
2800b57cec5SDimitry Andric // extent, ignore this map entry.
2810b57cec5SDimitry Andric if (Offset + Data.size() < MapEntry.first)
2820b57cec5SDimitry Andric continue;
2830b57cec5SDimitry Andric for (const auto &Alloc : MapEntry.second) {
2840b57cec5SDimitry Andric // If the end of the cached extent precedes the beginning of the written
2850b57cec5SDimitry Andric // extent, ignore this alloc.
2860b57cec5SDimitry Andric if (MapEntry.first + Alloc.size() < Offset)
2870b57cec5SDimitry Andric continue;
2880b57cec5SDimitry Andric
2890b57cec5SDimitry Andric // If we get here, they are guaranteed to overlap.
2900b57cec5SDimitry Andric Interval WriteInterval = std::make_pair(Offset, Offset + Data.size());
2910b57cec5SDimitry Andric Interval CachedInterval =
2920b57cec5SDimitry Andric std::make_pair(MapEntry.first, MapEntry.first + Alloc.size());
2930b57cec5SDimitry Andric // If they overlap, we need to write the new data into the overlapping
2940b57cec5SDimitry Andric // range.
2950b57cec5SDimitry Andric auto Intersection = intersect(WriteInterval, CachedInterval);
2960b57cec5SDimitry Andric assert(Intersection.first <= Intersection.second);
2970b57cec5SDimitry Andric
298349cc55cSDimitry Andric uint64_t Length = Intersection.second - Intersection.first;
299349cc55cSDimitry Andric uint64_t SrcOffset =
3000b57cec5SDimitry Andric AbsoluteDifference(WriteInterval.first, Intersection.first);
301349cc55cSDimitry Andric uint64_t DestOffset =
3020b57cec5SDimitry Andric AbsoluteDifference(CachedInterval.first, Intersection.first);
3030b57cec5SDimitry Andric ::memcpy(Alloc.data() + DestOffset, Data.data() + SrcOffset, Length);
3040b57cec5SDimitry Andric }
3050b57cec5SDimitry Andric }
3060b57cec5SDimitry Andric }
3070b57cec5SDimitry Andric
WritableMappedBlockStream(uint32_t BlockSize,const MSFStreamLayout & Layout,WritableBinaryStreamRef MsfData,BumpPtrAllocator & Allocator)3080b57cec5SDimitry Andric WritableMappedBlockStream::WritableMappedBlockStream(
3090b57cec5SDimitry Andric uint32_t BlockSize, const MSFStreamLayout &Layout,
3100b57cec5SDimitry Andric WritableBinaryStreamRef MsfData, BumpPtrAllocator &Allocator)
3110b57cec5SDimitry Andric : ReadInterface(BlockSize, Layout, MsfData, Allocator),
3120b57cec5SDimitry Andric WriteInterface(MsfData) {}
3130b57cec5SDimitry Andric
3140b57cec5SDimitry Andric std::unique_ptr<WritableMappedBlockStream>
createStream(uint32_t BlockSize,const MSFStreamLayout & Layout,WritableBinaryStreamRef MsfData,BumpPtrAllocator & Allocator)3150b57cec5SDimitry Andric WritableMappedBlockStream::createStream(uint32_t BlockSize,
3160b57cec5SDimitry Andric const MSFStreamLayout &Layout,
3170b57cec5SDimitry Andric WritableBinaryStreamRef MsfData,
3180b57cec5SDimitry Andric BumpPtrAllocator &Allocator) {
3198bcb0991SDimitry Andric return std::make_unique<MappedBlockStreamImpl<WritableMappedBlockStream>>(
3200b57cec5SDimitry Andric BlockSize, Layout, MsfData, Allocator);
3210b57cec5SDimitry Andric }
3220b57cec5SDimitry Andric
3230b57cec5SDimitry Andric std::unique_ptr<WritableMappedBlockStream>
createIndexedStream(const MSFLayout & Layout,WritableBinaryStreamRef MsfData,uint32_t StreamIndex,BumpPtrAllocator & Allocator)3240b57cec5SDimitry Andric WritableMappedBlockStream::createIndexedStream(const MSFLayout &Layout,
3250b57cec5SDimitry Andric WritableBinaryStreamRef MsfData,
3260b57cec5SDimitry Andric uint32_t StreamIndex,
3270b57cec5SDimitry Andric BumpPtrAllocator &Allocator) {
3280b57cec5SDimitry Andric assert(StreamIndex < Layout.StreamMap.size() && "Invalid stream index");
3290b57cec5SDimitry Andric MSFStreamLayout SL;
3300b57cec5SDimitry Andric SL.Blocks = Layout.StreamMap[StreamIndex];
3310b57cec5SDimitry Andric SL.Length = Layout.StreamSizes[StreamIndex];
3320b57cec5SDimitry Andric return createStream(Layout.SB->BlockSize, SL, MsfData, Allocator);
3330b57cec5SDimitry Andric }
3340b57cec5SDimitry Andric
3350b57cec5SDimitry Andric std::unique_ptr<WritableMappedBlockStream>
createDirectoryStream(const MSFLayout & Layout,WritableBinaryStreamRef MsfData,BumpPtrAllocator & Allocator)3360b57cec5SDimitry Andric WritableMappedBlockStream::createDirectoryStream(
3370b57cec5SDimitry Andric const MSFLayout &Layout, WritableBinaryStreamRef MsfData,
3380b57cec5SDimitry Andric BumpPtrAllocator &Allocator) {
3390b57cec5SDimitry Andric MSFStreamLayout SL;
3400b57cec5SDimitry Andric SL.Blocks = Layout.DirectoryBlocks;
3410b57cec5SDimitry Andric SL.Length = Layout.SB->NumDirectoryBytes;
3420b57cec5SDimitry Andric return createStream(Layout.SB->BlockSize, SL, MsfData, Allocator);
3430b57cec5SDimitry Andric }
3440b57cec5SDimitry Andric
3450b57cec5SDimitry Andric std::unique_ptr<WritableMappedBlockStream>
createFpmStream(const MSFLayout & Layout,WritableBinaryStreamRef MsfData,BumpPtrAllocator & Allocator,bool AltFpm)3460b57cec5SDimitry Andric WritableMappedBlockStream::createFpmStream(const MSFLayout &Layout,
3470b57cec5SDimitry Andric WritableBinaryStreamRef MsfData,
3480b57cec5SDimitry Andric BumpPtrAllocator &Allocator,
3490b57cec5SDimitry Andric bool AltFpm) {
3500b57cec5SDimitry Andric // We only want to give the user a stream containing the bytes of the FPM that
3510b57cec5SDimitry Andric // are actually valid, but we want to initialize all of the bytes, even those
3520b57cec5SDimitry Andric // that come from reserved FPM blocks where the entire block is unused. To do
3530b57cec5SDimitry Andric // this, we first create the full layout, which gives us a stream with all
3540b57cec5SDimitry Andric // bytes and all blocks, and initialize everything to 0xFF (all blocks in the
3550b57cec5SDimitry Andric // file are unused). Then we create the minimal layout (which contains only a
3560b57cec5SDimitry Andric // subset of the bytes previously initialized), and return that to the user.
3570b57cec5SDimitry Andric MSFStreamLayout MinLayout(getFpmStreamLayout(Layout, false, AltFpm));
3580b57cec5SDimitry Andric
3590b57cec5SDimitry Andric MSFStreamLayout FullLayout(getFpmStreamLayout(Layout, true, AltFpm));
3600b57cec5SDimitry Andric auto Result =
3610b57cec5SDimitry Andric createStream(Layout.SB->BlockSize, FullLayout, MsfData, Allocator);
3620b57cec5SDimitry Andric if (!Result)
3630b57cec5SDimitry Andric return Result;
3640b57cec5SDimitry Andric std::vector<uint8_t> InitData(Layout.SB->BlockSize, 0xFF);
3650b57cec5SDimitry Andric BinaryStreamWriter Initializer(*Result);
3660b57cec5SDimitry Andric while (Initializer.bytesRemaining() > 0)
3670b57cec5SDimitry Andric cantFail(Initializer.writeBytes(InitData));
3680b57cec5SDimitry Andric return createStream(Layout.SB->BlockSize, MinLayout, MsfData, Allocator);
3690b57cec5SDimitry Andric }
3700b57cec5SDimitry Andric
readBytes(uint64_t Offset,uint64_t Size,ArrayRef<uint8_t> & Buffer)371349cc55cSDimitry Andric Error WritableMappedBlockStream::readBytes(uint64_t Offset, uint64_t Size,
3720b57cec5SDimitry Andric ArrayRef<uint8_t> &Buffer) {
3730b57cec5SDimitry Andric return ReadInterface.readBytes(Offset, Size, Buffer);
3740b57cec5SDimitry Andric }
3750b57cec5SDimitry Andric
readLongestContiguousChunk(uint64_t Offset,ArrayRef<uint8_t> & Buffer)3760b57cec5SDimitry Andric Error WritableMappedBlockStream::readLongestContiguousChunk(
377349cc55cSDimitry Andric uint64_t Offset, ArrayRef<uint8_t> &Buffer) {
3780b57cec5SDimitry Andric return ReadInterface.readLongestContiguousChunk(Offset, Buffer);
3790b57cec5SDimitry Andric }
3800b57cec5SDimitry Andric
getLength()381349cc55cSDimitry Andric uint64_t WritableMappedBlockStream::getLength() {
3820b57cec5SDimitry Andric return ReadInterface.getLength();
3830b57cec5SDimitry Andric }
3840b57cec5SDimitry Andric
writeBytes(uint64_t Offset,ArrayRef<uint8_t> Buffer)385349cc55cSDimitry Andric Error WritableMappedBlockStream::writeBytes(uint64_t Offset,
3860b57cec5SDimitry Andric ArrayRef<uint8_t> Buffer) {
3870b57cec5SDimitry Andric // Make sure we aren't trying to write beyond the end of the stream.
3880b57cec5SDimitry Andric if (auto EC = checkOffsetForWrite(Offset, Buffer.size()))
3890b57cec5SDimitry Andric return EC;
3900b57cec5SDimitry Andric
391349cc55cSDimitry Andric uint64_t BlockNum = Offset / getBlockSize();
392349cc55cSDimitry Andric uint64_t OffsetInBlock = Offset % getBlockSize();
3930b57cec5SDimitry Andric
394349cc55cSDimitry Andric uint64_t BytesLeft = Buffer.size();
395349cc55cSDimitry Andric uint64_t BytesWritten = 0;
3960b57cec5SDimitry Andric while (BytesLeft > 0) {
397349cc55cSDimitry Andric uint64_t StreamBlockAddr = getStreamLayout().Blocks[BlockNum];
398349cc55cSDimitry Andric uint64_t BytesToWriteInChunk =
3990b57cec5SDimitry Andric std::min(BytesLeft, getBlockSize() - OffsetInBlock);
4000b57cec5SDimitry Andric
4010b57cec5SDimitry Andric const uint8_t *Chunk = Buffer.data() + BytesWritten;
4020b57cec5SDimitry Andric ArrayRef<uint8_t> ChunkData(Chunk, BytesToWriteInChunk);
403349cc55cSDimitry Andric uint64_t MsfOffset = blockToOffset(StreamBlockAddr, getBlockSize());
4040b57cec5SDimitry Andric MsfOffset += OffsetInBlock;
4050b57cec5SDimitry Andric if (auto EC = WriteInterface.writeBytes(MsfOffset, ChunkData))
4060b57cec5SDimitry Andric return EC;
4070b57cec5SDimitry Andric
4080b57cec5SDimitry Andric BytesLeft -= BytesToWriteInChunk;
4090b57cec5SDimitry Andric BytesWritten += BytesToWriteInChunk;
4100b57cec5SDimitry Andric ++BlockNum;
4110b57cec5SDimitry Andric OffsetInBlock = 0;
4120b57cec5SDimitry Andric }
4130b57cec5SDimitry Andric
4140b57cec5SDimitry Andric ReadInterface.fixCacheAfterWrite(Offset, Buffer);
4150b57cec5SDimitry Andric
4160b57cec5SDimitry Andric return Error::success();
4170b57cec5SDimitry Andric }
4180b57cec5SDimitry Andric
commit()4190b57cec5SDimitry Andric Error WritableMappedBlockStream::commit() { return WriteInterface.commit(); }
420