blob: 2a200edeaf7c4c4c4493a6a5f068141d4b691361 [file] [log] [blame]
Zachary Turnerbac69d32016-07-22 19:56:05 +00001//===- MappedBlockStream.cpp - Reads stream data from an MSF file ---------===//
Zachary Turner6ba65de2016-04-29 17:22:58 +00002//
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 Turnera3225b02016-07-29 20:56:36 +000010#include "llvm/DebugInfo/MSF/MappedBlockStream.h"
Eugene Zelenko2db0cfa2017-06-23 21:57:40 +000011#include "llvm/ADT/ArrayRef.h"
12#include "llvm/ADT/STLExtras.h"
Zachary Turnera3225b02016-07-29 20:56:36 +000013#include "llvm/DebugInfo/MSF/MSFCommon.h"
Eugene Zelenko2db0cfa2017-06-23 21:57:40 +000014#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 Turner6ba65de2016-04-29 17:22:58 +000023
24using namespace llvm;
Zachary Turnerbac69d32016-07-22 19:56:05 +000025using namespace llvm::msf;
Zachary Turner6ba65de2016-04-29 17:22:58 +000026
Zachary Turnera1657a92016-06-08 17:26:39 +000027namespace {
Eugene Zelenko2db0cfa2017-06-23 21:57:40 +000028
Zachary Turnerd66889c2016-07-28 19:12:28 +000029template <typename Base> class MappedBlockStreamImpl : public Base {
Zachary Turnera1657a92016-06-08 17:26:39 +000030public:
Zachary Turnerd66889c2016-07-28 19:12:28 +000031 template <typename... Args>
32 MappedBlockStreamImpl(Args &&... Params)
33 : Base(std::forward<Args>(Params)...) {}
Zachary Turnera1657a92016-06-08 17:26:39 +000034};
Eugene Zelenko2db0cfa2017-06-23 21:57:40 +000035
36} // end anonymous namespace
Zachary Turnera1657a92016-06-08 17:26:39 +000037
Eugene Zelenko2db0cfa2017-06-23 21:57:40 +000038using Interval = std::pair<uint32_t, uint32_t>;
39
Zachary Turner5acb4ac2016-06-10 05:09:12 +000040static 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 Turnerc4e4b7e2017-05-25 21:12:00 +000045MappedBlockStream::MappedBlockStream(uint32_t BlockSize,
Zachary Turnera3225b02016-07-29 20:56:36 +000046 const MSFStreamLayout &Layout,
Zachary Turner5b74ff32017-06-03 00:33:35 +000047 BinaryStreamRef MsfData,
48 BumpPtrAllocator &Allocator)
49 : BlockSize(BlockSize), StreamLayout(Layout), MsfData(MsfData),
50 Allocator(Allocator) {}
Zachary Turnerd66889c2016-07-28 19:12:28 +000051
Zachary Turner5b74ff32017-06-03 00:33:35 +000052std::unique_ptr<MappedBlockStream> MappedBlockStream::createStream(
53 uint32_t BlockSize, const MSFStreamLayout &Layout, BinaryStreamRef MsfData,
54 BumpPtrAllocator &Allocator) {
Zachary Turnerd66889c2016-07-28 19:12:28 +000055 return llvm::make_unique<MappedBlockStreamImpl<MappedBlockStream>>(
Zachary Turner5b74ff32017-06-03 00:33:35 +000056 BlockSize, Layout, MsfData, Allocator);
Zachary Turnerd66889c2016-07-28 19:12:28 +000057}
58
Zachary Turner120faca2017-02-27 22:11:43 +000059std::unique_ptr<MappedBlockStream> MappedBlockStream::createIndexedStream(
Zachary Turner5b74ff32017-06-03 00:33:35 +000060 const MSFLayout &Layout, BinaryStreamRef MsfData, uint32_t StreamIndex,
61 BumpPtrAllocator &Allocator) {
Bob Haarmana5b43582016-12-05 22:44:00 +000062 assert(StreamIndex < Layout.StreamMap.size() && "Invalid stream index");
Zachary Turnera3225b02016-07-29 20:56:36 +000063 MSFStreamLayout SL;
Zachary Turnerd66889c2016-07-28 19:12:28 +000064 SL.Blocks = Layout.StreamMap[StreamIndex];
65 SL.Length = Layout.StreamSizes[StreamIndex];
66 return llvm::make_unique<MappedBlockStreamImpl<MappedBlockStream>>(
Zachary Turner5b74ff32017-06-03 00:33:35 +000067 Layout.SB->BlockSize, SL, MsfData, Allocator);
Zachary Turnerd66889c2016-07-28 19:12:28 +000068}
69
70std::unique_ptr<MappedBlockStream>
Zachary Turnera3225b02016-07-29 20:56:36 +000071MappedBlockStream::createDirectoryStream(const MSFLayout &Layout,
Zachary Turner5b74ff32017-06-03 00:33:35 +000072 BinaryStreamRef MsfData,
73 BumpPtrAllocator &Allocator) {
Zachary Turnera3225b02016-07-29 20:56:36 +000074 MSFStreamLayout SL;
Zachary Turnerd66889c2016-07-28 19:12:28 +000075 SL.Blocks = Layout.DirectoryBlocks;
76 SL.Length = Layout.SB->NumDirectoryBytes;
Zachary Turner5b74ff32017-06-03 00:33:35 +000077 return createStream(Layout.SB->BlockSize, SL, MsfData, Allocator);
Zachary Turnerd66889c2016-07-28 19:12:28 +000078}
Zachary Turner6ba65de2016-04-29 17:22:58 +000079
Zachary Turner8cf51c32016-08-03 16:53:21 +000080std::unique_ptr<MappedBlockStream>
81MappedBlockStream::createFpmStream(const MSFLayout &Layout,
Zachary Turner5b74ff32017-06-03 00:33:35 +000082 BinaryStreamRef MsfData,
83 BumpPtrAllocator &Allocator) {
Zachary Turnerc3d8eec2017-08-02 22:25:52 +000084 MSFStreamLayout SL(getFpmStreamLayout(Layout));
Zachary Turner5b74ff32017-06-03 00:33:35 +000085 return createStream(Layout.SB->BlockSize, SL, MsfData, Allocator);
Zachary Turner8cf51c32016-08-03 16:53:21 +000086}
87
Zachary Turner8dbe3622016-05-27 01:54:44 +000088Error MappedBlockStream::readBytes(uint32_t Offset, uint32_t Size,
Zachary Turner120faca2017-02-27 22:11:43 +000089 ArrayRef<uint8_t> &Buffer) {
Zachary Turner8dbe3622016-05-27 01:54:44 +000090 // Make sure we aren't trying to read beyond the end of the stream.
Zachary Turnerd0b44fa2017-02-28 17:49:34 +000091 if (auto EC = checkOffset(Offset, Size))
92 return EC;
Zachary Turner8dbe3622016-05-27 01:54:44 +000093
94 if (tryReadContiguously(Offset, Size, Buffer))
95 return Error::success();
96
97 auto CacheIter = CacheMap.find(Offset);
98 if (CacheIter != CacheMap.end()) {
Zachary Turner5acb4ac2016-06-10 05:09:12 +000099 // 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 Turner8dbe3622016-05-27 01:54:44 +0000144 return Error::success();
145 }
146
147 // Otherwise allocate a large enough buffer in the pool, memcpy the data
Zachary Turner5acb4ac2016-06-10 05:09:12 +0000148 // 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 Turner5b74ff32017-06-03 00:33:35 +0000151 uint8_t *WriteBuffer = static_cast<uint8_t *>(Allocator.Allocate(Size, 8));
Zachary Turner8dbe3622016-05-27 01:54:44 +0000152 if (auto EC = readBytes(Offset, MutableArrayRef<uint8_t>(WriteBuffer, Size)))
153 return EC;
Zachary Turner5acb4ac2016-06-10 05:09:12 +0000154
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 Turner8dbe3622016-05-27 01:54:44 +0000162 Buffer = ArrayRef<uint8_t>(WriteBuffer, Size);
163 return Error::success();
164}
165
Zachary Turner120faca2017-02-27 22:11:43 +0000166Error MappedBlockStream::readLongestContiguousChunk(uint32_t Offset,
167 ArrayRef<uint8_t> &Buffer) {
Zachary Turner5acb4ac2016-06-10 05:09:12 +0000168 // Make sure we aren't trying to read beyond the end of the stream.
Zachary Turnerd0b44fa2017-02-28 17:49:34 +0000169 if (auto EC = checkOffset(Offset, 1))
170 return EC;
171
Zachary Turnerd66889c2016-07-28 19:12:28 +0000172 uint32_t First = Offset / BlockSize;
Zachary Turner5acb4ac2016-06-10 05:09:12 +0000173 uint32_t Last = First;
174
Zachary Turnerc4e4b7e2017-05-25 21:12:00 +0000175 while (Last < getNumBlocks() - 1) {
Zachary Turnerd66889c2016-07-28 19:12:28 +0000176 if (StreamLayout.Blocks[Last] != StreamLayout.Blocks[Last + 1] - 1)
Zachary Turner5acb4ac2016-06-10 05:09:12 +0000177 break;
178 ++Last;
179 }
180
Zachary Turnerd66889c2016-07-28 19:12:28 +0000181 uint32_t OffsetInFirstBlock = Offset % BlockSize;
182 uint32_t BytesFromFirstBlock = BlockSize - OffsetInFirstBlock;
Zachary Turner5acb4ac2016-06-10 05:09:12 +0000183 uint32_t BlockSpan = Last - First + 1;
Zachary Turnerd66889c2016-07-28 19:12:28 +0000184 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 Turner5acb4ac2016-06-10 05:09:12 +0000193 return Error::success();
194}
195
Zachary Turner120faca2017-02-27 22:11:43 +0000196uint32_t MappedBlockStream::getLength() { return StreamLayout.Length; }
Zachary Turnerab58ae82016-06-30 17:43:00 +0000197
Zachary Turner8dbe3622016-05-27 01:54:44 +0000198bool MappedBlockStream::tryReadContiguously(uint32_t Offset, uint32_t Size,
Zachary Turner120faca2017-02-27 22:11:43 +0000199 ArrayRef<uint8_t> &Buffer) {
Zachary Turner36efbfa2016-09-09 19:00:49 +0000200 if (Size == 0) {
201 Buffer = ArrayRef<uint8_t>();
202 return true;
203 }
Zachary Turner8dbe3622016-05-27 01:54:44 +0000204 // 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 Turnerd66889c2016-07-28 19:12:28 +0000209 uint32_t BlockNum = Offset / BlockSize;
210 uint32_t OffsetInBlock = Offset % BlockSize;
211 uint32_t BytesFromFirstBlock = std::min(Size, BlockSize - OffsetInBlock);
Zachary Turner8dbe3622016-05-27 01:54:44 +0000212 uint32_t NumAdditionalBlocks =
Eugene Zelenko2db0cfa2017-06-23 21:57:40 +0000213 alignTo(Size - BytesFromFirstBlock, BlockSize) / BlockSize;
Zachary Turner8dbe3622016-05-27 01:54:44 +0000214
215 uint32_t RequiredContiguousBlocks = NumAdditionalBlocks + 1;
Zachary Turnerd66889c2016-07-28 19:12:28 +0000216 uint32_t E = StreamLayout.Blocks[BlockNum];
Zachary Turner8dbe3622016-05-27 01:54:44 +0000217 for (uint32_t I = 0; I < RequiredContiguousBlocks; ++I, ++E) {
Zachary Turnerd66889c2016-07-28 19:12:28 +0000218 if (StreamLayout.Blocks[I + BlockNum] != E)
Zachary Turner8dbe3622016-05-27 01:54:44 +0000219 return false;
220 }
221
Zachary Turnerd66889c2016-07-28 19:12:28 +0000222 // 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 Majnemer6211b1f2016-07-10 03:34:47 +0000232 return false;
233 }
Zachary Turnerd66889c2016-07-28 19:12:28 +0000234 BlockData = BlockData.drop_front(OffsetInBlock);
235 Buffer = ArrayRef<uint8_t>(BlockData.data(), Size);
Zachary Turner8dbe3622016-05-27 01:54:44 +0000236 return true;
237}
238
Zachary Turner819e77d2016-05-06 20:51:57 +0000239Error MappedBlockStream::readBytes(uint32_t Offset,
Zachary Turner120faca2017-02-27 22:11:43 +0000240 MutableArrayRef<uint8_t> Buffer) {
Zachary Turnerd66889c2016-07-28 19:12:28 +0000241 uint32_t BlockNum = Offset / BlockSize;
242 uint32_t OffsetInBlock = Offset % BlockSize;
Zachary Turner6ba65de2016-04-29 17:22:58 +0000243
244 // Make sure we aren't trying to read beyond the end of the stream.
Zachary Turnerd0b44fa2017-02-28 17:49:34 +0000245 if (auto EC = checkOffset(Offset, Buffer.size()))
246 return EC;
Zachary Turner6ba65de2016-04-29 17:22:58 +0000247
248 uint32_t BytesLeft = Buffer.size();
249 uint32_t BytesWritten = 0;
250 uint8_t *WriteBuffer = Buffer.data();
251 while (BytesLeft > 0) {
Zachary Turnerd66889c2016-07-28 19:12:28 +0000252 uint32_t StreamBlockAddr = StreamLayout.Blocks[BlockNum];
Zachary Turner6ba65de2016-04-29 17:22:58 +0000253
Zachary Turnerd66889c2016-07-28 19:12:28 +0000254 ArrayRef<uint8_t> BlockData;
255 uint32_t Offset = blockToOffset(StreamBlockAddr, BlockSize);
256 if (auto EC = MsfData.readBytes(Offset, BlockSize, BlockData))
257 return EC;
Zachary Turner6ba65de2016-04-29 17:22:58 +0000258
Zachary Turnerd66889c2016-07-28 19:12:28 +0000259 const uint8_t *ChunkStart = BlockData.data() + OffsetInBlock;
260 uint32_t BytesInChunk = std::min(BytesLeft, BlockSize - OffsetInBlock);
Zachary Turner6ba65de2016-04-29 17:22:58 +0000261 ::memcpy(WriteBuffer + BytesWritten, ChunkStart, BytesInChunk);
262
263 BytesWritten += BytesInChunk;
264 BytesLeft -= BytesInChunk;
265 ++BlockNum;
266 OffsetInBlock = 0;
267 }
268
Zachary Turner819e77d2016-05-06 20:51:57 +0000269 return Error::success();
Zachary Turner5acb4ac2016-06-10 05:09:12 +0000270}
Zachary Turnerf5c59652016-05-03 00:28:21 +0000271
Zachary Turnerd66889c2016-07-28 19:12:28 +0000272void MappedBlockStream::invalidateCache() { CacheMap.shrink_and_clear(); }
Zachary Turner5acb4ac2016-06-10 05:09:12 +0000273
Zachary Turnerd66889c2016-07-28 19:12:28 +0000274void MappedBlockStream::fixCacheAfterWrite(uint32_t Offset,
275 ArrayRef<uint8_t> Data) const {
Zachary Turner5acb4ac2016-06-10 05:09:12 +0000276 // 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 Turnerd66889c2016-07-28 19:12:28 +0000283 if (Offset + Data.size() < MapEntry.first)
Zachary Turner5acb4ac2016-06-10 05:09:12 +0000284 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 Turnerd66889c2016-07-28 19:12:28 +0000292 Interval WriteInterval = std::make_pair(Offset, Offset + Data.size());
Zachary Turner5acb4ac2016-06-10 05:09:12 +0000293 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 Turnerd66889c2016-07-28 19:12:28 +0000305 ::memcpy(Alloc.data() + DestOffset, Data.data() + SrcOffset, Length);
Zachary Turner5acb4ac2016-06-10 05:09:12 +0000306 }
307 }
Zachary Turnerd66889c2016-07-28 19:12:28 +0000308}
309
310WritableMappedBlockStream::WritableMappedBlockStream(
Zachary Turnerc4e4b7e2017-05-25 21:12:00 +0000311 uint32_t BlockSize, const MSFStreamLayout &Layout,
Zachary Turner5b74ff32017-06-03 00:33:35 +0000312 WritableBinaryStreamRef MsfData, BumpPtrAllocator &Allocator)
313 : ReadInterface(BlockSize, Layout, MsfData, Allocator),
314 WriteInterface(MsfData) {}
Zachary Turnerd66889c2016-07-28 19:12:28 +0000315
316std::unique_ptr<WritableMappedBlockStream>
Zachary Turnerc4e4b7e2017-05-25 21:12:00 +0000317WritableMappedBlockStream::createStream(uint32_t BlockSize,
Zachary Turnera3225b02016-07-29 20:56:36 +0000318 const MSFStreamLayout &Layout,
Zachary Turner5b74ff32017-06-03 00:33:35 +0000319 WritableBinaryStreamRef MsfData,
320 BumpPtrAllocator &Allocator) {
Zachary Turnerd66889c2016-07-28 19:12:28 +0000321 return llvm::make_unique<MappedBlockStreamImpl<WritableMappedBlockStream>>(
Zachary Turner5b74ff32017-06-03 00:33:35 +0000322 BlockSize, Layout, MsfData, Allocator);
Zachary Turnerd66889c2016-07-28 19:12:28 +0000323}
324
325std::unique_ptr<WritableMappedBlockStream>
Zachary Turnera3225b02016-07-29 20:56:36 +0000326WritableMappedBlockStream::createIndexedStream(const MSFLayout &Layout,
Zachary Turner120faca2017-02-27 22:11:43 +0000327 WritableBinaryStreamRef MsfData,
Zachary Turner5b74ff32017-06-03 00:33:35 +0000328 uint32_t StreamIndex,
329 BumpPtrAllocator &Allocator) {
Bob Haarmana5b43582016-12-05 22:44:00 +0000330 assert(StreamIndex < Layout.StreamMap.size() && "Invalid stream index");
Zachary Turnera3225b02016-07-29 20:56:36 +0000331 MSFStreamLayout SL;
Zachary Turnerd66889c2016-07-28 19:12:28 +0000332 SL.Blocks = Layout.StreamMap[StreamIndex];
333 SL.Length = Layout.StreamSizes[StreamIndex];
Zachary Turner5b74ff32017-06-03 00:33:35 +0000334 return createStream(Layout.SB->BlockSize, SL, MsfData, Allocator);
Zachary Turnerd66889c2016-07-28 19:12:28 +0000335}
336
337std::unique_ptr<WritableMappedBlockStream>
338WritableMappedBlockStream::createDirectoryStream(
Zachary Turner5b74ff32017-06-03 00:33:35 +0000339 const MSFLayout &Layout, WritableBinaryStreamRef MsfData,
340 BumpPtrAllocator &Allocator) {
Zachary Turnera3225b02016-07-29 20:56:36 +0000341 MSFStreamLayout SL;
Zachary Turnerd66889c2016-07-28 19:12:28 +0000342 SL.Blocks = Layout.DirectoryBlocks;
343 SL.Length = Layout.SB->NumDirectoryBytes;
Zachary Turner5b74ff32017-06-03 00:33:35 +0000344 return createStream(Layout.SB->BlockSize, SL, MsfData, Allocator);
Zachary Turnerd66889c2016-07-28 19:12:28 +0000345}
346
Zachary Turner8cf51c32016-08-03 16:53:21 +0000347std::unique_ptr<WritableMappedBlockStream>
348WritableMappedBlockStream::createFpmStream(const MSFLayout &Layout,
Zachary Turner5b74ff32017-06-03 00:33:35 +0000349 WritableBinaryStreamRef MsfData,
350 BumpPtrAllocator &Allocator) {
Zachary Turnerc3d8eec2017-08-02 22:25:52 +0000351 MSFStreamLayout SL(getFpmStreamLayout(Layout));
Zachary Turner5b74ff32017-06-03 00:33:35 +0000352 return createStream(Layout.SB->BlockSize, SL, MsfData, Allocator);
Zachary Turner8cf51c32016-08-03 16:53:21 +0000353}
354
Zachary Turnerd66889c2016-07-28 19:12:28 +0000355Error WritableMappedBlockStream::readBytes(uint32_t Offset, uint32_t Size,
Zachary Turner120faca2017-02-27 22:11:43 +0000356 ArrayRef<uint8_t> &Buffer) {
Zachary Turnerd66889c2016-07-28 19:12:28 +0000357 return ReadInterface.readBytes(Offset, Size, Buffer);
358}
359
360Error WritableMappedBlockStream::readLongestContiguousChunk(
Zachary Turner120faca2017-02-27 22:11:43 +0000361 uint32_t Offset, ArrayRef<uint8_t> &Buffer) {
Zachary Turnerd66889c2016-07-28 19:12:28 +0000362 return ReadInterface.readLongestContiguousChunk(Offset, Buffer);
363}
364
Zachary Turner120faca2017-02-27 22:11:43 +0000365uint32_t WritableMappedBlockStream::getLength() {
Zachary Turnerd66889c2016-07-28 19:12:28 +0000366 return ReadInterface.getLength();
367}
368
369Error WritableMappedBlockStream::writeBytes(uint32_t Offset,
Zachary Turner120faca2017-02-27 22:11:43 +0000370 ArrayRef<uint8_t> Buffer) {
Zachary Turnerd66889c2016-07-28 19:12:28 +0000371 // Make sure we aren't trying to write beyond the end of the stream.
Zachary Turnerd0b44fa2017-02-28 17:49:34 +0000372 if (auto EC = checkOffset(Offset, Buffer.size()))
373 return EC;
Zachary Turnerd66889c2016-07-28 19:12:28 +0000374
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 Turner5acb4ac2016-06-10 05:09:12 +0000399
400 return Error::success();
Zachary Turnerf5c59652016-05-03 00:28:21 +0000401}
Zachary Turner90b8b8d2016-05-31 22:41:52 +0000402
Zachary Turner120faca2017-02-27 22:11:43 +0000403Error WritableMappedBlockStream::commit() { return WriteInterface.commit(); }