1*0b57cec5SDimitry Andric //===- MappedBlockStream.cpp - Reads stream data from an MSF file ---------===// 2*0b57cec5SDimitry Andric // 3*0b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4*0b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 5*0b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6*0b57cec5SDimitry Andric // 7*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 8*0b57cec5SDimitry Andric 9*0b57cec5SDimitry Andric #include "llvm/DebugInfo/MSF/MappedBlockStream.h" 10*0b57cec5SDimitry Andric #include "llvm/ADT/ArrayRef.h" 11*0b57cec5SDimitry Andric #include "llvm/ADT/STLExtras.h" 12*0b57cec5SDimitry Andric #include "llvm/DebugInfo/MSF/MSFCommon.h" 13*0b57cec5SDimitry Andric #include "llvm/Support/BinaryStreamWriter.h" 14*0b57cec5SDimitry Andric #include "llvm/Support/Endian.h" 15*0b57cec5SDimitry Andric #include "llvm/Support/Error.h" 16*0b57cec5SDimitry Andric #include "llvm/Support/MathExtras.h" 17*0b57cec5SDimitry Andric #include <algorithm> 18*0b57cec5SDimitry Andric #include <cassert> 19*0b57cec5SDimitry Andric #include <cstdint> 20*0b57cec5SDimitry Andric #include <cstring> 21*0b57cec5SDimitry Andric #include <utility> 22*0b57cec5SDimitry Andric #include <vector> 23*0b57cec5SDimitry Andric 24*0b57cec5SDimitry Andric using namespace llvm; 25*0b57cec5SDimitry Andric using namespace llvm::msf; 26*0b57cec5SDimitry Andric 27*0b57cec5SDimitry Andric namespace { 28*0b57cec5SDimitry Andric 29*0b57cec5SDimitry Andric template <typename Base> class MappedBlockStreamImpl : public Base { 30*0b57cec5SDimitry Andric public: 31*0b57cec5SDimitry Andric template <typename... Args> 32*0b57cec5SDimitry Andric MappedBlockStreamImpl(Args &&... Params) 33*0b57cec5SDimitry Andric : Base(std::forward<Args>(Params)...) {} 34*0b57cec5SDimitry Andric }; 35*0b57cec5SDimitry Andric 36*0b57cec5SDimitry Andric } // end anonymous namespace 37*0b57cec5SDimitry Andric 38*0b57cec5SDimitry Andric using Interval = std::pair<uint32_t, uint32_t>; 39*0b57cec5SDimitry Andric 40*0b57cec5SDimitry Andric static Interval intersect(const Interval &I1, const Interval &I2) { 41*0b57cec5SDimitry Andric return std::make_pair(std::max(I1.first, I2.first), 42*0b57cec5SDimitry Andric std::min(I1.second, I2.second)); 43*0b57cec5SDimitry Andric } 44*0b57cec5SDimitry Andric 45*0b57cec5SDimitry Andric MappedBlockStream::MappedBlockStream(uint32_t BlockSize, 46*0b57cec5SDimitry Andric const MSFStreamLayout &Layout, 47*0b57cec5SDimitry Andric BinaryStreamRef MsfData, 48*0b57cec5SDimitry Andric BumpPtrAllocator &Allocator) 49*0b57cec5SDimitry Andric : BlockSize(BlockSize), StreamLayout(Layout), MsfData(MsfData), 50*0b57cec5SDimitry Andric Allocator(Allocator) {} 51*0b57cec5SDimitry Andric 52*0b57cec5SDimitry Andric std::unique_ptr<MappedBlockStream> MappedBlockStream::createStream( 53*0b57cec5SDimitry Andric uint32_t BlockSize, const MSFStreamLayout &Layout, BinaryStreamRef MsfData, 54*0b57cec5SDimitry Andric BumpPtrAllocator &Allocator) { 55*0b57cec5SDimitry Andric return llvm::make_unique<MappedBlockStreamImpl<MappedBlockStream>>( 56*0b57cec5SDimitry Andric BlockSize, Layout, MsfData, Allocator); 57*0b57cec5SDimitry Andric } 58*0b57cec5SDimitry Andric 59*0b57cec5SDimitry Andric std::unique_ptr<MappedBlockStream> MappedBlockStream::createIndexedStream( 60*0b57cec5SDimitry Andric const MSFLayout &Layout, BinaryStreamRef MsfData, uint32_t StreamIndex, 61*0b57cec5SDimitry Andric BumpPtrAllocator &Allocator) { 62*0b57cec5SDimitry Andric assert(StreamIndex < Layout.StreamMap.size() && "Invalid stream index"); 63*0b57cec5SDimitry Andric MSFStreamLayout SL; 64*0b57cec5SDimitry Andric SL.Blocks = Layout.StreamMap[StreamIndex]; 65*0b57cec5SDimitry Andric SL.Length = Layout.StreamSizes[StreamIndex]; 66*0b57cec5SDimitry Andric return llvm::make_unique<MappedBlockStreamImpl<MappedBlockStream>>( 67*0b57cec5SDimitry Andric Layout.SB->BlockSize, SL, MsfData, Allocator); 68*0b57cec5SDimitry Andric } 69*0b57cec5SDimitry Andric 70*0b57cec5SDimitry Andric std::unique_ptr<MappedBlockStream> 71*0b57cec5SDimitry Andric MappedBlockStream::createDirectoryStream(const MSFLayout &Layout, 72*0b57cec5SDimitry Andric BinaryStreamRef MsfData, 73*0b57cec5SDimitry Andric BumpPtrAllocator &Allocator) { 74*0b57cec5SDimitry Andric MSFStreamLayout SL; 75*0b57cec5SDimitry Andric SL.Blocks = Layout.DirectoryBlocks; 76*0b57cec5SDimitry Andric SL.Length = Layout.SB->NumDirectoryBytes; 77*0b57cec5SDimitry Andric return createStream(Layout.SB->BlockSize, SL, MsfData, Allocator); 78*0b57cec5SDimitry Andric } 79*0b57cec5SDimitry Andric 80*0b57cec5SDimitry Andric std::unique_ptr<MappedBlockStream> 81*0b57cec5SDimitry Andric MappedBlockStream::createFpmStream(const MSFLayout &Layout, 82*0b57cec5SDimitry Andric BinaryStreamRef MsfData, 83*0b57cec5SDimitry Andric BumpPtrAllocator &Allocator) { 84*0b57cec5SDimitry Andric MSFStreamLayout SL(getFpmStreamLayout(Layout)); 85*0b57cec5SDimitry Andric return createStream(Layout.SB->BlockSize, SL, MsfData, Allocator); 86*0b57cec5SDimitry Andric } 87*0b57cec5SDimitry Andric 88*0b57cec5SDimitry Andric Error MappedBlockStream::readBytes(uint32_t Offset, uint32_t Size, 89*0b57cec5SDimitry Andric ArrayRef<uint8_t> &Buffer) { 90*0b57cec5SDimitry Andric // Make sure we aren't trying to read beyond the end of the stream. 91*0b57cec5SDimitry Andric if (auto EC = checkOffsetForRead(Offset, Size)) 92*0b57cec5SDimitry Andric return EC; 93*0b57cec5SDimitry Andric 94*0b57cec5SDimitry Andric if (tryReadContiguously(Offset, Size, Buffer)) 95*0b57cec5SDimitry Andric return Error::success(); 96*0b57cec5SDimitry Andric 97*0b57cec5SDimitry Andric auto CacheIter = CacheMap.find(Offset); 98*0b57cec5SDimitry Andric if (CacheIter != CacheMap.end()) { 99*0b57cec5SDimitry Andric // Try to find an alloc that was large enough for this request. 100*0b57cec5SDimitry Andric for (auto &Entry : CacheIter->second) { 101*0b57cec5SDimitry Andric if (Entry.size() >= Size) { 102*0b57cec5SDimitry Andric Buffer = Entry.slice(0, Size); 103*0b57cec5SDimitry Andric return Error::success(); 104*0b57cec5SDimitry Andric } 105*0b57cec5SDimitry Andric } 106*0b57cec5SDimitry Andric } 107*0b57cec5SDimitry Andric 108*0b57cec5SDimitry Andric // We couldn't find a buffer that started at the correct offset (the most 109*0b57cec5SDimitry Andric // common scenario). Try to see if there is a buffer that starts at some 110*0b57cec5SDimitry Andric // other offset but overlaps the desired range. 111*0b57cec5SDimitry Andric for (auto &CacheItem : CacheMap) { 112*0b57cec5SDimitry Andric Interval RequestExtent = std::make_pair(Offset, Offset + Size); 113*0b57cec5SDimitry Andric 114*0b57cec5SDimitry Andric // We already checked this one on the fast path above. 115*0b57cec5SDimitry Andric if (CacheItem.first == Offset) 116*0b57cec5SDimitry Andric continue; 117*0b57cec5SDimitry Andric // If the initial extent of the cached item is beyond the ending extent 118*0b57cec5SDimitry Andric // of the request, there is no overlap. 119*0b57cec5SDimitry Andric if (CacheItem.first >= Offset + Size) 120*0b57cec5SDimitry Andric continue; 121*0b57cec5SDimitry Andric 122*0b57cec5SDimitry Andric // We really only have to check the last item in the list, since we append 123*0b57cec5SDimitry Andric // in order of increasing length. 124*0b57cec5SDimitry Andric if (CacheItem.second.empty()) 125*0b57cec5SDimitry Andric continue; 126*0b57cec5SDimitry Andric 127*0b57cec5SDimitry Andric auto CachedAlloc = CacheItem.second.back(); 128*0b57cec5SDimitry Andric // If the initial extent of the request is beyond the ending extent of 129*0b57cec5SDimitry Andric // the cached item, there is no overlap. 130*0b57cec5SDimitry Andric Interval CachedExtent = 131*0b57cec5SDimitry Andric std::make_pair(CacheItem.first, CacheItem.first + CachedAlloc.size()); 132*0b57cec5SDimitry Andric if (RequestExtent.first >= CachedExtent.first + CachedExtent.second) 133*0b57cec5SDimitry Andric continue; 134*0b57cec5SDimitry Andric 135*0b57cec5SDimitry Andric Interval Intersection = intersect(CachedExtent, RequestExtent); 136*0b57cec5SDimitry Andric // Only use this if the entire request extent is contained in the cached 137*0b57cec5SDimitry Andric // extent. 138*0b57cec5SDimitry Andric if (Intersection != RequestExtent) 139*0b57cec5SDimitry Andric continue; 140*0b57cec5SDimitry Andric 141*0b57cec5SDimitry Andric uint32_t CacheRangeOffset = 142*0b57cec5SDimitry Andric AbsoluteDifference(CachedExtent.first, Intersection.first); 143*0b57cec5SDimitry Andric Buffer = CachedAlloc.slice(CacheRangeOffset, Size); 144*0b57cec5SDimitry Andric return Error::success(); 145*0b57cec5SDimitry Andric } 146*0b57cec5SDimitry Andric 147*0b57cec5SDimitry Andric // Otherwise allocate a large enough buffer in the pool, memcpy the data 148*0b57cec5SDimitry Andric // into it, and return an ArrayRef to that. Do not touch existing pool 149*0b57cec5SDimitry Andric // allocations, as existing clients may be holding a pointer which must 150*0b57cec5SDimitry Andric // not be invalidated. 151*0b57cec5SDimitry Andric uint8_t *WriteBuffer = static_cast<uint8_t *>(Allocator.Allocate(Size, 8)); 152*0b57cec5SDimitry Andric if (auto EC = readBytes(Offset, MutableArrayRef<uint8_t>(WriteBuffer, Size))) 153*0b57cec5SDimitry Andric return EC; 154*0b57cec5SDimitry Andric 155*0b57cec5SDimitry Andric if (CacheIter != CacheMap.end()) { 156*0b57cec5SDimitry Andric CacheIter->second.emplace_back(WriteBuffer, Size); 157*0b57cec5SDimitry Andric } else { 158*0b57cec5SDimitry Andric std::vector<CacheEntry> List; 159*0b57cec5SDimitry Andric List.emplace_back(WriteBuffer, Size); 160*0b57cec5SDimitry Andric CacheMap.insert(std::make_pair(Offset, List)); 161*0b57cec5SDimitry Andric } 162*0b57cec5SDimitry Andric Buffer = ArrayRef<uint8_t>(WriteBuffer, Size); 163*0b57cec5SDimitry Andric return Error::success(); 164*0b57cec5SDimitry Andric } 165*0b57cec5SDimitry Andric 166*0b57cec5SDimitry Andric Error MappedBlockStream::readLongestContiguousChunk(uint32_t Offset, 167*0b57cec5SDimitry Andric ArrayRef<uint8_t> &Buffer) { 168*0b57cec5SDimitry Andric // Make sure we aren't trying to read beyond the end of the stream. 169*0b57cec5SDimitry Andric if (auto EC = checkOffsetForRead(Offset, 1)) 170*0b57cec5SDimitry Andric return EC; 171*0b57cec5SDimitry Andric 172*0b57cec5SDimitry Andric uint32_t First = Offset / BlockSize; 173*0b57cec5SDimitry Andric uint32_t Last = First; 174*0b57cec5SDimitry Andric 175*0b57cec5SDimitry Andric while (Last < getNumBlocks() - 1) { 176*0b57cec5SDimitry Andric if (StreamLayout.Blocks[Last] != StreamLayout.Blocks[Last + 1] - 1) 177*0b57cec5SDimitry Andric break; 178*0b57cec5SDimitry Andric ++Last; 179*0b57cec5SDimitry Andric } 180*0b57cec5SDimitry Andric 181*0b57cec5SDimitry Andric uint32_t OffsetInFirstBlock = Offset % BlockSize; 182*0b57cec5SDimitry Andric uint32_t BytesFromFirstBlock = BlockSize - OffsetInFirstBlock; 183*0b57cec5SDimitry Andric uint32_t BlockSpan = Last - First + 1; 184*0b57cec5SDimitry Andric uint32_t ByteSpan = BytesFromFirstBlock + (BlockSpan - 1) * BlockSize; 185*0b57cec5SDimitry Andric 186*0b57cec5SDimitry Andric ArrayRef<uint8_t> BlockData; 187*0b57cec5SDimitry Andric uint32_t MsfOffset = blockToOffset(StreamLayout.Blocks[First], BlockSize); 188*0b57cec5SDimitry Andric if (auto EC = MsfData.readBytes(MsfOffset, BlockSize, BlockData)) 189*0b57cec5SDimitry Andric return EC; 190*0b57cec5SDimitry Andric 191*0b57cec5SDimitry Andric BlockData = BlockData.drop_front(OffsetInFirstBlock); 192*0b57cec5SDimitry Andric Buffer = ArrayRef<uint8_t>(BlockData.data(), ByteSpan); 193*0b57cec5SDimitry Andric return Error::success(); 194*0b57cec5SDimitry Andric } 195*0b57cec5SDimitry Andric 196*0b57cec5SDimitry Andric uint32_t MappedBlockStream::getLength() { return StreamLayout.Length; } 197*0b57cec5SDimitry Andric 198*0b57cec5SDimitry Andric bool MappedBlockStream::tryReadContiguously(uint32_t Offset, uint32_t Size, 199*0b57cec5SDimitry Andric ArrayRef<uint8_t> &Buffer) { 200*0b57cec5SDimitry Andric if (Size == 0) { 201*0b57cec5SDimitry Andric Buffer = ArrayRef<uint8_t>(); 202*0b57cec5SDimitry Andric return true; 203*0b57cec5SDimitry Andric } 204*0b57cec5SDimitry Andric // Attempt to fulfill the request with a reference directly into the stream. 205*0b57cec5SDimitry Andric // This can work even if the request crosses a block boundary, provided that 206*0b57cec5SDimitry Andric // all subsequent blocks are contiguous. For example, a 10k read with a 4k 207*0b57cec5SDimitry Andric // block size can be filled with a reference if, from the starting offset, 208*0b57cec5SDimitry Andric // 3 blocks in a row are contiguous. 209*0b57cec5SDimitry Andric uint32_t BlockNum = Offset / BlockSize; 210*0b57cec5SDimitry Andric uint32_t OffsetInBlock = Offset % BlockSize; 211*0b57cec5SDimitry Andric uint32_t BytesFromFirstBlock = std::min(Size, BlockSize - OffsetInBlock); 212*0b57cec5SDimitry Andric uint32_t NumAdditionalBlocks = 213*0b57cec5SDimitry Andric alignTo(Size - BytesFromFirstBlock, BlockSize) / BlockSize; 214*0b57cec5SDimitry Andric 215*0b57cec5SDimitry Andric uint32_t RequiredContiguousBlocks = NumAdditionalBlocks + 1; 216*0b57cec5SDimitry Andric uint32_t E = StreamLayout.Blocks[BlockNum]; 217*0b57cec5SDimitry Andric for (uint32_t I = 0; I < RequiredContiguousBlocks; ++I, ++E) { 218*0b57cec5SDimitry Andric if (StreamLayout.Blocks[I + BlockNum] != E) 219*0b57cec5SDimitry Andric return false; 220*0b57cec5SDimitry Andric } 221*0b57cec5SDimitry Andric 222*0b57cec5SDimitry Andric // Read out the entire block where the requested offset starts. Then drop 223*0b57cec5SDimitry Andric // bytes from the beginning so that the actual starting byte lines up with 224*0b57cec5SDimitry Andric // the requested starting byte. Then, since we know this is a contiguous 225*0b57cec5SDimitry Andric // cross-block span, explicitly resize the ArrayRef to cover the entire 226*0b57cec5SDimitry Andric // request length. 227*0b57cec5SDimitry Andric ArrayRef<uint8_t> BlockData; 228*0b57cec5SDimitry Andric uint32_t FirstBlockAddr = StreamLayout.Blocks[BlockNum]; 229*0b57cec5SDimitry Andric uint32_t MsfOffset = blockToOffset(FirstBlockAddr, BlockSize); 230*0b57cec5SDimitry Andric if (auto EC = MsfData.readBytes(MsfOffset, BlockSize, BlockData)) { 231*0b57cec5SDimitry Andric consumeError(std::move(EC)); 232*0b57cec5SDimitry Andric return false; 233*0b57cec5SDimitry Andric } 234*0b57cec5SDimitry Andric BlockData = BlockData.drop_front(OffsetInBlock); 235*0b57cec5SDimitry Andric Buffer = ArrayRef<uint8_t>(BlockData.data(), Size); 236*0b57cec5SDimitry Andric return true; 237*0b57cec5SDimitry Andric } 238*0b57cec5SDimitry Andric 239*0b57cec5SDimitry Andric Error MappedBlockStream::readBytes(uint32_t Offset, 240*0b57cec5SDimitry Andric MutableArrayRef<uint8_t> Buffer) { 241*0b57cec5SDimitry Andric uint32_t BlockNum = Offset / BlockSize; 242*0b57cec5SDimitry Andric uint32_t OffsetInBlock = Offset % BlockSize; 243*0b57cec5SDimitry Andric 244*0b57cec5SDimitry Andric // Make sure we aren't trying to read beyond the end of the stream. 245*0b57cec5SDimitry Andric if (auto EC = checkOffsetForRead(Offset, Buffer.size())) 246*0b57cec5SDimitry Andric return EC; 247*0b57cec5SDimitry Andric 248*0b57cec5SDimitry Andric uint32_t BytesLeft = Buffer.size(); 249*0b57cec5SDimitry Andric uint32_t BytesWritten = 0; 250*0b57cec5SDimitry Andric uint8_t *WriteBuffer = Buffer.data(); 251*0b57cec5SDimitry Andric while (BytesLeft > 0) { 252*0b57cec5SDimitry Andric uint32_t StreamBlockAddr = StreamLayout.Blocks[BlockNum]; 253*0b57cec5SDimitry Andric 254*0b57cec5SDimitry Andric ArrayRef<uint8_t> BlockData; 255*0b57cec5SDimitry Andric uint32_t Offset = blockToOffset(StreamBlockAddr, BlockSize); 256*0b57cec5SDimitry Andric if (auto EC = MsfData.readBytes(Offset, BlockSize, BlockData)) 257*0b57cec5SDimitry Andric return EC; 258*0b57cec5SDimitry Andric 259*0b57cec5SDimitry Andric const uint8_t *ChunkStart = BlockData.data() + OffsetInBlock; 260*0b57cec5SDimitry Andric uint32_t BytesInChunk = std::min(BytesLeft, BlockSize - OffsetInBlock); 261*0b57cec5SDimitry Andric ::memcpy(WriteBuffer + BytesWritten, ChunkStart, BytesInChunk); 262*0b57cec5SDimitry Andric 263*0b57cec5SDimitry Andric BytesWritten += BytesInChunk; 264*0b57cec5SDimitry Andric BytesLeft -= BytesInChunk; 265*0b57cec5SDimitry Andric ++BlockNum; 266*0b57cec5SDimitry Andric OffsetInBlock = 0; 267*0b57cec5SDimitry Andric } 268*0b57cec5SDimitry Andric 269*0b57cec5SDimitry Andric return Error::success(); 270*0b57cec5SDimitry Andric } 271*0b57cec5SDimitry Andric 272*0b57cec5SDimitry Andric void MappedBlockStream::invalidateCache() { CacheMap.shrink_and_clear(); } 273*0b57cec5SDimitry Andric 274*0b57cec5SDimitry Andric void MappedBlockStream::fixCacheAfterWrite(uint32_t Offset, 275*0b57cec5SDimitry Andric ArrayRef<uint8_t> Data) const { 276*0b57cec5SDimitry Andric // If this write overlapped a read which previously came from the pool, 277*0b57cec5SDimitry Andric // someone may still be holding a pointer to that alloc which is now invalid. 278*0b57cec5SDimitry Andric // Compute the overlapping range and update the cache entry, so any 279*0b57cec5SDimitry Andric // outstanding buffers are automatically updated. 280*0b57cec5SDimitry Andric for (const auto &MapEntry : CacheMap) { 281*0b57cec5SDimitry Andric // If the end of the written extent precedes the beginning of the cached 282*0b57cec5SDimitry Andric // extent, ignore this map entry. 283*0b57cec5SDimitry Andric if (Offset + Data.size() < MapEntry.first) 284*0b57cec5SDimitry Andric continue; 285*0b57cec5SDimitry Andric for (const auto &Alloc : MapEntry.second) { 286*0b57cec5SDimitry Andric // If the end of the cached extent precedes the beginning of the written 287*0b57cec5SDimitry Andric // extent, ignore this alloc. 288*0b57cec5SDimitry Andric if (MapEntry.first + Alloc.size() < Offset) 289*0b57cec5SDimitry Andric continue; 290*0b57cec5SDimitry Andric 291*0b57cec5SDimitry Andric // If we get here, they are guaranteed to overlap. 292*0b57cec5SDimitry Andric Interval WriteInterval = std::make_pair(Offset, Offset + Data.size()); 293*0b57cec5SDimitry Andric Interval CachedInterval = 294*0b57cec5SDimitry Andric std::make_pair(MapEntry.first, MapEntry.first + Alloc.size()); 295*0b57cec5SDimitry Andric // If they overlap, we need to write the new data into the overlapping 296*0b57cec5SDimitry Andric // range. 297*0b57cec5SDimitry Andric auto Intersection = intersect(WriteInterval, CachedInterval); 298*0b57cec5SDimitry Andric assert(Intersection.first <= Intersection.second); 299*0b57cec5SDimitry Andric 300*0b57cec5SDimitry Andric uint32_t Length = Intersection.second - Intersection.first; 301*0b57cec5SDimitry Andric uint32_t SrcOffset = 302*0b57cec5SDimitry Andric AbsoluteDifference(WriteInterval.first, Intersection.first); 303*0b57cec5SDimitry Andric uint32_t DestOffset = 304*0b57cec5SDimitry Andric AbsoluteDifference(CachedInterval.first, Intersection.first); 305*0b57cec5SDimitry Andric ::memcpy(Alloc.data() + DestOffset, Data.data() + SrcOffset, Length); 306*0b57cec5SDimitry Andric } 307*0b57cec5SDimitry Andric } 308*0b57cec5SDimitry Andric } 309*0b57cec5SDimitry Andric 310*0b57cec5SDimitry Andric WritableMappedBlockStream::WritableMappedBlockStream( 311*0b57cec5SDimitry Andric uint32_t BlockSize, const MSFStreamLayout &Layout, 312*0b57cec5SDimitry Andric WritableBinaryStreamRef MsfData, BumpPtrAllocator &Allocator) 313*0b57cec5SDimitry Andric : ReadInterface(BlockSize, Layout, MsfData, Allocator), 314*0b57cec5SDimitry Andric WriteInterface(MsfData) {} 315*0b57cec5SDimitry Andric 316*0b57cec5SDimitry Andric std::unique_ptr<WritableMappedBlockStream> 317*0b57cec5SDimitry Andric WritableMappedBlockStream::createStream(uint32_t BlockSize, 318*0b57cec5SDimitry Andric const MSFStreamLayout &Layout, 319*0b57cec5SDimitry Andric WritableBinaryStreamRef MsfData, 320*0b57cec5SDimitry Andric BumpPtrAllocator &Allocator) { 321*0b57cec5SDimitry Andric return llvm::make_unique<MappedBlockStreamImpl<WritableMappedBlockStream>>( 322*0b57cec5SDimitry Andric BlockSize, Layout, MsfData, Allocator); 323*0b57cec5SDimitry Andric } 324*0b57cec5SDimitry Andric 325*0b57cec5SDimitry Andric std::unique_ptr<WritableMappedBlockStream> 326*0b57cec5SDimitry Andric WritableMappedBlockStream::createIndexedStream(const MSFLayout &Layout, 327*0b57cec5SDimitry Andric WritableBinaryStreamRef MsfData, 328*0b57cec5SDimitry Andric uint32_t StreamIndex, 329*0b57cec5SDimitry Andric BumpPtrAllocator &Allocator) { 330*0b57cec5SDimitry Andric assert(StreamIndex < Layout.StreamMap.size() && "Invalid stream index"); 331*0b57cec5SDimitry Andric MSFStreamLayout SL; 332*0b57cec5SDimitry Andric SL.Blocks = Layout.StreamMap[StreamIndex]; 333*0b57cec5SDimitry Andric SL.Length = Layout.StreamSizes[StreamIndex]; 334*0b57cec5SDimitry Andric return createStream(Layout.SB->BlockSize, SL, MsfData, Allocator); 335*0b57cec5SDimitry Andric } 336*0b57cec5SDimitry Andric 337*0b57cec5SDimitry Andric std::unique_ptr<WritableMappedBlockStream> 338*0b57cec5SDimitry Andric WritableMappedBlockStream::createDirectoryStream( 339*0b57cec5SDimitry Andric const MSFLayout &Layout, WritableBinaryStreamRef MsfData, 340*0b57cec5SDimitry Andric BumpPtrAllocator &Allocator) { 341*0b57cec5SDimitry Andric MSFStreamLayout SL; 342*0b57cec5SDimitry Andric SL.Blocks = Layout.DirectoryBlocks; 343*0b57cec5SDimitry Andric SL.Length = Layout.SB->NumDirectoryBytes; 344*0b57cec5SDimitry Andric return createStream(Layout.SB->BlockSize, SL, MsfData, Allocator); 345*0b57cec5SDimitry Andric } 346*0b57cec5SDimitry Andric 347*0b57cec5SDimitry Andric std::unique_ptr<WritableMappedBlockStream> 348*0b57cec5SDimitry Andric WritableMappedBlockStream::createFpmStream(const MSFLayout &Layout, 349*0b57cec5SDimitry Andric WritableBinaryStreamRef MsfData, 350*0b57cec5SDimitry Andric BumpPtrAllocator &Allocator, 351*0b57cec5SDimitry Andric bool AltFpm) { 352*0b57cec5SDimitry Andric // We only want to give the user a stream containing the bytes of the FPM that 353*0b57cec5SDimitry Andric // are actually valid, but we want to initialize all of the bytes, even those 354*0b57cec5SDimitry Andric // that come from reserved FPM blocks where the entire block is unused. To do 355*0b57cec5SDimitry Andric // this, we first create the full layout, which gives us a stream with all 356*0b57cec5SDimitry Andric // bytes and all blocks, and initialize everything to 0xFF (all blocks in the 357*0b57cec5SDimitry Andric // file are unused). Then we create the minimal layout (which contains only a 358*0b57cec5SDimitry Andric // subset of the bytes previously initialized), and return that to the user. 359*0b57cec5SDimitry Andric MSFStreamLayout MinLayout(getFpmStreamLayout(Layout, false, AltFpm)); 360*0b57cec5SDimitry Andric 361*0b57cec5SDimitry Andric MSFStreamLayout FullLayout(getFpmStreamLayout(Layout, true, AltFpm)); 362*0b57cec5SDimitry Andric auto Result = 363*0b57cec5SDimitry Andric createStream(Layout.SB->BlockSize, FullLayout, MsfData, Allocator); 364*0b57cec5SDimitry Andric if (!Result) 365*0b57cec5SDimitry Andric return Result; 366*0b57cec5SDimitry Andric std::vector<uint8_t> InitData(Layout.SB->BlockSize, 0xFF); 367*0b57cec5SDimitry Andric BinaryStreamWriter Initializer(*Result); 368*0b57cec5SDimitry Andric while (Initializer.bytesRemaining() > 0) 369*0b57cec5SDimitry Andric cantFail(Initializer.writeBytes(InitData)); 370*0b57cec5SDimitry Andric return createStream(Layout.SB->BlockSize, MinLayout, MsfData, Allocator); 371*0b57cec5SDimitry Andric } 372*0b57cec5SDimitry Andric 373*0b57cec5SDimitry Andric Error WritableMappedBlockStream::readBytes(uint32_t Offset, uint32_t Size, 374*0b57cec5SDimitry Andric ArrayRef<uint8_t> &Buffer) { 375*0b57cec5SDimitry Andric return ReadInterface.readBytes(Offset, Size, Buffer); 376*0b57cec5SDimitry Andric } 377*0b57cec5SDimitry Andric 378*0b57cec5SDimitry Andric Error WritableMappedBlockStream::readLongestContiguousChunk( 379*0b57cec5SDimitry Andric uint32_t Offset, ArrayRef<uint8_t> &Buffer) { 380*0b57cec5SDimitry Andric return ReadInterface.readLongestContiguousChunk(Offset, Buffer); 381*0b57cec5SDimitry Andric } 382*0b57cec5SDimitry Andric 383*0b57cec5SDimitry Andric uint32_t WritableMappedBlockStream::getLength() { 384*0b57cec5SDimitry Andric return ReadInterface.getLength(); 385*0b57cec5SDimitry Andric } 386*0b57cec5SDimitry Andric 387*0b57cec5SDimitry Andric Error WritableMappedBlockStream::writeBytes(uint32_t Offset, 388*0b57cec5SDimitry Andric ArrayRef<uint8_t> Buffer) { 389*0b57cec5SDimitry Andric // Make sure we aren't trying to write beyond the end of the stream. 390*0b57cec5SDimitry Andric if (auto EC = checkOffsetForWrite(Offset, Buffer.size())) 391*0b57cec5SDimitry Andric return EC; 392*0b57cec5SDimitry Andric 393*0b57cec5SDimitry Andric uint32_t BlockNum = Offset / getBlockSize(); 394*0b57cec5SDimitry Andric uint32_t OffsetInBlock = Offset % getBlockSize(); 395*0b57cec5SDimitry Andric 396*0b57cec5SDimitry Andric uint32_t BytesLeft = Buffer.size(); 397*0b57cec5SDimitry Andric uint32_t BytesWritten = 0; 398*0b57cec5SDimitry Andric while (BytesLeft > 0) { 399*0b57cec5SDimitry Andric uint32_t StreamBlockAddr = getStreamLayout().Blocks[BlockNum]; 400*0b57cec5SDimitry Andric uint32_t BytesToWriteInChunk = 401*0b57cec5SDimitry Andric std::min(BytesLeft, getBlockSize() - OffsetInBlock); 402*0b57cec5SDimitry Andric 403*0b57cec5SDimitry Andric const uint8_t *Chunk = Buffer.data() + BytesWritten; 404*0b57cec5SDimitry Andric ArrayRef<uint8_t> ChunkData(Chunk, BytesToWriteInChunk); 405*0b57cec5SDimitry Andric uint32_t MsfOffset = blockToOffset(StreamBlockAddr, getBlockSize()); 406*0b57cec5SDimitry Andric MsfOffset += OffsetInBlock; 407*0b57cec5SDimitry Andric if (auto EC = WriteInterface.writeBytes(MsfOffset, ChunkData)) 408*0b57cec5SDimitry Andric return EC; 409*0b57cec5SDimitry Andric 410*0b57cec5SDimitry Andric BytesLeft -= BytesToWriteInChunk; 411*0b57cec5SDimitry Andric BytesWritten += BytesToWriteInChunk; 412*0b57cec5SDimitry Andric ++BlockNum; 413*0b57cec5SDimitry Andric OffsetInBlock = 0; 414*0b57cec5SDimitry Andric } 415*0b57cec5SDimitry Andric 416*0b57cec5SDimitry Andric ReadInterface.fixCacheAfterWrite(Offset, Buffer); 417*0b57cec5SDimitry Andric 418*0b57cec5SDimitry Andric return Error::success(); 419*0b57cec5SDimitry Andric } 420*0b57cec5SDimitry Andric 421*0b57cec5SDimitry Andric Error WritableMappedBlockStream::commit() { return WriteInterface.commit(); } 422