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