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