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