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