blob: c9ba25c0d7d4fbf54d04c99027d1a5da36c6490b [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"
Zachary Turner199f48a2016-07-28 19:11:09 +000011
Zachary Turnera3225b02016-07-29 20:56:36 +000012#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 Turner6ba65de2016-04-29 17:22:58 +000016
17using namespace llvm;
Zachary Turnerbac69d32016-07-22 19:56:05 +000018using namespace llvm::msf;
Zachary Turner6ba65de2016-04-29 17:22:58 +000019
Zachary Turnera1657a92016-06-08 17:26:39 +000020namespace {
Zachary Turnerd66889c2016-07-28 19:12:28 +000021template <typename Base> class MappedBlockStreamImpl : public Base {
Zachary Turnera1657a92016-06-08 17:26:39 +000022public:
Zachary Turnerd66889c2016-07-28 19:12:28 +000023 template <typename... Args>
24 MappedBlockStreamImpl(Args &&... Params)
25 : Base(std::forward<Args>(Params)...) {}
Zachary Turnera1657a92016-06-08 17:26:39 +000026};
27}
28
Zachary Turner8cf51c32016-08-03 16:53:21 +000029static 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 Turner5acb4ac2016-06-10 05:09:12 +000042typedef std::pair<uint32_t, uint32_t> Interval;
43static 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 Turnerd66889c2016-07-28 19:12:28 +000048MappedBlockStream::MappedBlockStream(uint32_t BlockSize, uint32_t NumBlocks,
Zachary Turnera3225b02016-07-29 20:56:36 +000049 const MSFStreamLayout &Layout,
Zachary Turner120faca2017-02-27 22:11:43 +000050 BinaryStreamRef MsfData)
Zachary Turnerd66889c2016-07-28 19:12:28 +000051 : BlockSize(BlockSize), NumBlocks(NumBlocks), StreamLayout(Layout),
52 MsfData(MsfData) {}
53
54std::unique_ptr<MappedBlockStream>
55MappedBlockStream::createStream(uint32_t BlockSize, uint32_t NumBlocks,
Zachary Turnera3225b02016-07-29 20:56:36 +000056 const MSFStreamLayout &Layout,
Zachary Turner120faca2017-02-27 22:11:43 +000057 BinaryStreamRef MsfData) {
Zachary Turnerd66889c2016-07-28 19:12:28 +000058 return llvm::make_unique<MappedBlockStreamImpl<MappedBlockStream>>(
59 BlockSize, NumBlocks, Layout, MsfData);
60}
61
Zachary Turner120faca2017-02-27 22:11:43 +000062std::unique_ptr<MappedBlockStream> MappedBlockStream::createIndexedStream(
63 const MSFLayout &Layout, BinaryStreamRef MsfData, uint32_t StreamIndex) {
Bob Haarmana5b43582016-12-05 22:44:00 +000064 assert(StreamIndex < Layout.StreamMap.size() && "Invalid stream index");
Zachary Turnera3225b02016-07-29 20:56:36 +000065 MSFStreamLayout SL;
Zachary Turnerd66889c2016-07-28 19:12:28 +000066 SL.Blocks = Layout.StreamMap[StreamIndex];
67 SL.Length = Layout.StreamSizes[StreamIndex];
68 return llvm::make_unique<MappedBlockStreamImpl<MappedBlockStream>>(
69 Layout.SB->BlockSize, Layout.SB->NumBlocks, SL, MsfData);
70}
71
72std::unique_ptr<MappedBlockStream>
Zachary Turnera3225b02016-07-29 20:56:36 +000073MappedBlockStream::createDirectoryStream(const MSFLayout &Layout,
Zachary Turner120faca2017-02-27 22:11:43 +000074 BinaryStreamRef MsfData) {
Zachary Turnera3225b02016-07-29 20:56:36 +000075 MSFStreamLayout SL;
Zachary Turnerd66889c2016-07-28 19:12:28 +000076 SL.Blocks = Layout.DirectoryBlocks;
77 SL.Length = Layout.SB->NumDirectoryBytes;
78 return createStream(Layout.SB->BlockSize, Layout.SB->NumBlocks, SL, MsfData);
79}
Zachary Turner6ba65de2016-04-29 17:22:58 +000080
Zachary Turner8cf51c32016-08-03 16:53:21 +000081std::unique_ptr<MappedBlockStream>
82MappedBlockStream::createFpmStream(const MSFLayout &Layout,
Zachary Turner120faca2017-02-27 22:11:43 +000083 BinaryStreamRef MsfData) {
Zachary Turner8cf51c32016-08-03 16:53:21 +000084 MSFStreamLayout SL;
85 initializeFpmStreamLayout(Layout, SL);
86 return createStream(Layout.SB->BlockSize, Layout.SB->NumBlocks, SL, MsfData);
87}
88
Zachary Turner8dbe3622016-05-27 01:54:44 +000089Error MappedBlockStream::readBytes(uint32_t Offset, uint32_t Size,
Zachary Turner120faca2017-02-27 22:11:43 +000090 ArrayRef<uint8_t> &Buffer) {
Zachary Turner8dbe3622016-05-27 01:54:44 +000091 // Make sure we aren't trying to read beyond the end of the stream.
Zachary Turnerd66889c2016-07-28 19:12:28 +000092 if (Size > StreamLayout.Length)
Zachary Turnera3225b02016-07-29 20:56:36 +000093 return make_error<MSFError>(msf_error_code::insufficient_buffer);
Zachary Turnerd66889c2016-07-28 19:12:28 +000094 if (Offset > StreamLayout.Length - Size)
Zachary Turnera3225b02016-07-29 20:56:36 +000095 return make_error<MSFError>(msf_error_code::insufficient_buffer);
Zachary Turner8dbe3622016-05-27 01:54:44 +000096
97 if (tryReadContiguously(Offset, Size, Buffer))
98 return Error::success();
99
100 auto CacheIter = CacheMap.find(Offset);
101 if (CacheIter != CacheMap.end()) {
Zachary Turner5acb4ac2016-06-10 05:09:12 +0000102 // Try to find an alloc that was large enough for this request.
103 for (auto &Entry : CacheIter->second) {
104 if (Entry.size() >= Size) {
105 Buffer = Entry.slice(0, Size);
106 return Error::success();
107 }
108 }
109 }
110
111 // We couldn't find a buffer that started at the correct offset (the most
112 // common scenario). Try to see if there is a buffer that starts at some
113 // other offset but overlaps the desired range.
114 for (auto &CacheItem : CacheMap) {
115 Interval RequestExtent = std::make_pair(Offset, Offset + Size);
116
117 // We already checked this one on the fast path above.
118 if (CacheItem.first == Offset)
119 continue;
120 // If the initial extent of the cached item is beyond the ending extent
121 // of the request, there is no overlap.
122 if (CacheItem.first >= Offset + Size)
123 continue;
124
125 // We really only have to check the last item in the list, since we append
126 // in order of increasing length.
127 if (CacheItem.second.empty())
128 continue;
129
130 auto CachedAlloc = CacheItem.second.back();
131 // If the initial extent of the request is beyond the ending extent of
132 // the cached item, there is no overlap.
133 Interval CachedExtent =
134 std::make_pair(CacheItem.first, CacheItem.first + CachedAlloc.size());
135 if (RequestExtent.first >= CachedExtent.first + CachedExtent.second)
136 continue;
137
138 Interval Intersection = intersect(CachedExtent, RequestExtent);
139 // Only use this if the entire request extent is contained in the cached
140 // extent.
141 if (Intersection != RequestExtent)
142 continue;
143
144 uint32_t CacheRangeOffset =
145 AbsoluteDifference(CachedExtent.first, Intersection.first);
146 Buffer = CachedAlloc.slice(CacheRangeOffset, Size);
Zachary Turner8dbe3622016-05-27 01:54:44 +0000147 return Error::success();
148 }
149
150 // Otherwise allocate a large enough buffer in the pool, memcpy the data
Zachary Turner5acb4ac2016-06-10 05:09:12 +0000151 // into it, and return an ArrayRef to that. Do not touch existing pool
152 // allocations, as existing clients may be holding a pointer which must
153 // not be invalidated.
Zachary Turner97609bb2016-06-10 21:47:26 +0000154 uint8_t *WriteBuffer = static_cast<uint8_t *>(Pool.Allocate(Size, 8));
Zachary Turner8dbe3622016-05-27 01:54:44 +0000155 if (auto EC = readBytes(Offset, MutableArrayRef<uint8_t>(WriteBuffer, Size)))
156 return EC;
Zachary Turner5acb4ac2016-06-10 05:09:12 +0000157
158 if (CacheIter != CacheMap.end()) {
159 CacheIter->second.emplace_back(WriteBuffer, Size);
160 } else {
161 std::vector<CacheEntry> List;
162 List.emplace_back(WriteBuffer, Size);
163 CacheMap.insert(std::make_pair(Offset, List));
164 }
Zachary Turner8dbe3622016-05-27 01:54:44 +0000165 Buffer = ArrayRef<uint8_t>(WriteBuffer, Size);
166 return Error::success();
167}
168
Zachary Turner120faca2017-02-27 22:11:43 +0000169Error MappedBlockStream::readLongestContiguousChunk(uint32_t Offset,
170 ArrayRef<uint8_t> &Buffer) {
Zachary Turner5acb4ac2016-06-10 05:09:12 +0000171 // Make sure we aren't trying to read beyond the end of the stream.
Zachary Turnerd66889c2016-07-28 19:12:28 +0000172 if (Offset >= StreamLayout.Length)
Zachary Turnera3225b02016-07-29 20:56:36 +0000173 return make_error<MSFError>(msf_error_code::insufficient_buffer);
Zachary Turnerd66889c2016-07-28 19:12:28 +0000174 uint32_t First = Offset / BlockSize;
Zachary Turner5acb4ac2016-06-10 05:09:12 +0000175 uint32_t Last = First;
176
Zachary Turnerd66889c2016-07-28 19:12:28 +0000177 while (Last < NumBlocks - 1) {
178 if (StreamLayout.Blocks[Last] != StreamLayout.Blocks[Last + 1] - 1)
Zachary Turner5acb4ac2016-06-10 05:09:12 +0000179 break;
180 ++Last;
181 }
182
Zachary Turnerd66889c2016-07-28 19:12:28 +0000183 uint32_t OffsetInFirstBlock = Offset % BlockSize;
184 uint32_t BytesFromFirstBlock = BlockSize - OffsetInFirstBlock;
Zachary Turner5acb4ac2016-06-10 05:09:12 +0000185 uint32_t BlockSpan = Last - First + 1;
Zachary Turnerd66889c2016-07-28 19:12:28 +0000186 uint32_t ByteSpan = BytesFromFirstBlock + (BlockSpan - 1) * BlockSize;
187
188 ArrayRef<uint8_t> BlockData;
189 uint32_t MsfOffset = blockToOffset(StreamLayout.Blocks[First], BlockSize);
190 if (auto EC = MsfData.readBytes(MsfOffset, BlockSize, BlockData))
191 return EC;
192
193 BlockData = BlockData.drop_front(OffsetInFirstBlock);
194 Buffer = ArrayRef<uint8_t>(BlockData.data(), ByteSpan);
Zachary Turner5acb4ac2016-06-10 05:09:12 +0000195 return Error::success();
196}
197
Zachary Turner120faca2017-02-27 22:11:43 +0000198uint32_t MappedBlockStream::getLength() { return StreamLayout.Length; }
Zachary Turnerab58ae82016-06-30 17:43:00 +0000199
Zachary Turner8dbe3622016-05-27 01:54:44 +0000200bool MappedBlockStream::tryReadContiguously(uint32_t Offset, uint32_t Size,
Zachary Turner120faca2017-02-27 22:11:43 +0000201 ArrayRef<uint8_t> &Buffer) {
Zachary Turner36efbfa2016-09-09 19:00:49 +0000202 if (Size == 0) {
203 Buffer = ArrayRef<uint8_t>();
204 return true;
205 }
Zachary Turner8dbe3622016-05-27 01:54:44 +0000206 // Attempt to fulfill the request with a reference directly into the stream.
207 // This can work even if the request crosses a block boundary, provided that
208 // all subsequent blocks are contiguous. For example, a 10k read with a 4k
209 // block size can be filled with a reference if, from the starting offset,
210 // 3 blocks in a row are contiguous.
Zachary Turnerd66889c2016-07-28 19:12:28 +0000211 uint32_t BlockNum = Offset / BlockSize;
212 uint32_t OffsetInBlock = Offset % BlockSize;
213 uint32_t BytesFromFirstBlock = std::min(Size, BlockSize - OffsetInBlock);
Zachary Turner8dbe3622016-05-27 01:54:44 +0000214 uint32_t NumAdditionalBlocks =
Zachary Turnerd66889c2016-07-28 19:12:28 +0000215 llvm::alignTo(Size - BytesFromFirstBlock, BlockSize) / BlockSize;
Zachary Turner8dbe3622016-05-27 01:54:44 +0000216
217 uint32_t RequiredContiguousBlocks = NumAdditionalBlocks + 1;
Zachary Turnerd66889c2016-07-28 19:12:28 +0000218 uint32_t E = StreamLayout.Blocks[BlockNum];
Zachary Turner8dbe3622016-05-27 01:54:44 +0000219 for (uint32_t I = 0; I < RequiredContiguousBlocks; ++I, ++E) {
Zachary Turnerd66889c2016-07-28 19:12:28 +0000220 if (StreamLayout.Blocks[I + BlockNum] != E)
Zachary Turner8dbe3622016-05-27 01:54:44 +0000221 return false;
222 }
223
Zachary Turnerd66889c2016-07-28 19:12:28 +0000224 // Read out the entire block where the requested offset starts. Then drop
225 // bytes from the beginning so that the actual starting byte lines up with
226 // the requested starting byte. Then, since we know this is a contiguous
227 // cross-block span, explicitly resize the ArrayRef to cover the entire
228 // request length.
229 ArrayRef<uint8_t> BlockData;
230 uint32_t FirstBlockAddr = StreamLayout.Blocks[BlockNum];
231 uint32_t MsfOffset = blockToOffset(FirstBlockAddr, BlockSize);
232 if (auto EC = MsfData.readBytes(MsfOffset, BlockSize, BlockData)) {
233 consumeError(std::move(EC));
David Majnemer6211b1f2016-07-10 03:34:47 +0000234 return false;
235 }
Zachary Turnerd66889c2016-07-28 19:12:28 +0000236 BlockData = BlockData.drop_front(OffsetInBlock);
237 Buffer = ArrayRef<uint8_t>(BlockData.data(), Size);
Zachary Turner8dbe3622016-05-27 01:54:44 +0000238 return true;
239}
240
Zachary Turner819e77d2016-05-06 20:51:57 +0000241Error MappedBlockStream::readBytes(uint32_t Offset,
Zachary Turner120faca2017-02-27 22:11:43 +0000242 MutableArrayRef<uint8_t> Buffer) {
Zachary Turnerd66889c2016-07-28 19:12:28 +0000243 uint32_t BlockNum = Offset / BlockSize;
244 uint32_t OffsetInBlock = Offset % BlockSize;
Zachary Turner6ba65de2016-04-29 17:22:58 +0000245
246 // Make sure we aren't trying to read beyond the end of the stream.
Zachary Turnerd66889c2016-07-28 19:12:28 +0000247 if (Buffer.size() > StreamLayout.Length)
Zachary Turnera3225b02016-07-29 20:56:36 +0000248 return make_error<MSFError>(msf_error_code::insufficient_buffer);
Zachary Turnerd66889c2016-07-28 19:12:28 +0000249 if (Offset > StreamLayout.Length - Buffer.size())
Zachary Turnera3225b02016-07-29 20:56:36 +0000250 return make_error<MSFError>(msf_error_code::insufficient_buffer);
Zachary Turner6ba65de2016-04-29 17:22:58 +0000251
252 uint32_t BytesLeft = Buffer.size();
253 uint32_t BytesWritten = 0;
254 uint8_t *WriteBuffer = Buffer.data();
255 while (BytesLeft > 0) {
Zachary Turnerd66889c2016-07-28 19:12:28 +0000256 uint32_t StreamBlockAddr = StreamLayout.Blocks[BlockNum];
Zachary Turner6ba65de2016-04-29 17:22:58 +0000257
Zachary Turnerd66889c2016-07-28 19:12:28 +0000258 ArrayRef<uint8_t> BlockData;
259 uint32_t Offset = blockToOffset(StreamBlockAddr, BlockSize);
260 if (auto EC = MsfData.readBytes(Offset, BlockSize, BlockData))
261 return EC;
Zachary Turner6ba65de2016-04-29 17:22:58 +0000262
Zachary Turnerd66889c2016-07-28 19:12:28 +0000263 const uint8_t *ChunkStart = BlockData.data() + OffsetInBlock;
264 uint32_t BytesInChunk = std::min(BytesLeft, BlockSize - OffsetInBlock);
Zachary Turner6ba65de2016-04-29 17:22:58 +0000265 ::memcpy(WriteBuffer + BytesWritten, ChunkStart, BytesInChunk);
266
267 BytesWritten += BytesInChunk;
268 BytesLeft -= BytesInChunk;
269 ++BlockNum;
270 OffsetInBlock = 0;
271 }
272
Zachary Turner819e77d2016-05-06 20:51:57 +0000273 return Error::success();
Zachary Turner5acb4ac2016-06-10 05:09:12 +0000274}
Zachary Turnerf5c59652016-05-03 00:28:21 +0000275
Zachary Turnerd66889c2016-07-28 19:12:28 +0000276uint32_t MappedBlockStream::getNumBytesCopied() const {
277 return static_cast<uint32_t>(Pool.getBytesAllocated());
278}
Zachary Turner5acb4ac2016-06-10 05:09:12 +0000279
Zachary Turnerd66889c2016-07-28 19:12:28 +0000280void MappedBlockStream::invalidateCache() { CacheMap.shrink_and_clear(); }
Zachary Turner5acb4ac2016-06-10 05:09:12 +0000281
Zachary Turnerd66889c2016-07-28 19:12:28 +0000282void MappedBlockStream::fixCacheAfterWrite(uint32_t Offset,
283 ArrayRef<uint8_t> Data) const {
Zachary Turner5acb4ac2016-06-10 05:09:12 +0000284 // If this write overlapped a read which previously came from the pool,
285 // someone may still be holding a pointer to that alloc which is now invalid.
286 // Compute the overlapping range and update the cache entry, so any
287 // outstanding buffers are automatically updated.
288 for (const auto &MapEntry : CacheMap) {
289 // If the end of the written extent precedes the beginning of the cached
290 // extent, ignore this map entry.
Zachary Turnerd66889c2016-07-28 19:12:28 +0000291 if (Offset + Data.size() < MapEntry.first)
Zachary Turner5acb4ac2016-06-10 05:09:12 +0000292 continue;
293 for (const auto &Alloc : MapEntry.second) {
294 // If the end of the cached extent precedes the beginning of the written
295 // extent, ignore this alloc.
296 if (MapEntry.first + Alloc.size() < Offset)
297 continue;
298
299 // If we get here, they are guaranteed to overlap.
Zachary Turnerd66889c2016-07-28 19:12:28 +0000300 Interval WriteInterval = std::make_pair(Offset, Offset + Data.size());
Zachary Turner5acb4ac2016-06-10 05:09:12 +0000301 Interval CachedInterval =
302 std::make_pair(MapEntry.first, MapEntry.first + Alloc.size());
303 // If they overlap, we need to write the new data into the overlapping
304 // range.
305 auto Intersection = intersect(WriteInterval, CachedInterval);
306 assert(Intersection.first <= Intersection.second);
307
308 uint32_t Length = Intersection.second - Intersection.first;
309 uint32_t SrcOffset =
310 AbsoluteDifference(WriteInterval.first, Intersection.first);
311 uint32_t DestOffset =
312 AbsoluteDifference(CachedInterval.first, Intersection.first);
Zachary Turnerd66889c2016-07-28 19:12:28 +0000313 ::memcpy(Alloc.data() + DestOffset, Data.data() + SrcOffset, Length);
Zachary Turner5acb4ac2016-06-10 05:09:12 +0000314 }
315 }
Zachary Turnerd66889c2016-07-28 19:12:28 +0000316}
317
318WritableMappedBlockStream::WritableMappedBlockStream(
Zachary Turnera3225b02016-07-29 20:56:36 +0000319 uint32_t BlockSize, uint32_t NumBlocks, const MSFStreamLayout &Layout,
Zachary Turner120faca2017-02-27 22:11:43 +0000320 WritableBinaryStreamRef MsfData)
Zachary Turnerd66889c2016-07-28 19:12:28 +0000321 : ReadInterface(BlockSize, NumBlocks, Layout, MsfData),
322 WriteInterface(MsfData) {}
323
324std::unique_ptr<WritableMappedBlockStream>
325WritableMappedBlockStream::createStream(uint32_t BlockSize, uint32_t NumBlocks,
Zachary Turnera3225b02016-07-29 20:56:36 +0000326 const MSFStreamLayout &Layout,
Zachary Turner120faca2017-02-27 22:11:43 +0000327 WritableBinaryStreamRef MsfData) {
Zachary Turnerd66889c2016-07-28 19:12:28 +0000328 return llvm::make_unique<MappedBlockStreamImpl<WritableMappedBlockStream>>(
329 BlockSize, NumBlocks, Layout, MsfData);
330}
331
332std::unique_ptr<WritableMappedBlockStream>
Zachary Turnera3225b02016-07-29 20:56:36 +0000333WritableMappedBlockStream::createIndexedStream(const MSFLayout &Layout,
Zachary Turner120faca2017-02-27 22:11:43 +0000334 WritableBinaryStreamRef MsfData,
Zachary Turnerd66889c2016-07-28 19:12:28 +0000335 uint32_t StreamIndex) {
Bob Haarmana5b43582016-12-05 22:44:00 +0000336 assert(StreamIndex < Layout.StreamMap.size() && "Invalid stream index");
Zachary Turnera3225b02016-07-29 20:56:36 +0000337 MSFStreamLayout SL;
Zachary Turnerd66889c2016-07-28 19:12:28 +0000338 SL.Blocks = Layout.StreamMap[StreamIndex];
339 SL.Length = Layout.StreamSizes[StreamIndex];
340 return createStream(Layout.SB->BlockSize, Layout.SB->NumBlocks, SL, MsfData);
341}
342
343std::unique_ptr<WritableMappedBlockStream>
344WritableMappedBlockStream::createDirectoryStream(
Zachary Turner120faca2017-02-27 22:11:43 +0000345 const MSFLayout &Layout, WritableBinaryStreamRef MsfData) {
Zachary Turnera3225b02016-07-29 20:56:36 +0000346 MSFStreamLayout SL;
Zachary Turnerd66889c2016-07-28 19:12:28 +0000347 SL.Blocks = Layout.DirectoryBlocks;
348 SL.Length = Layout.SB->NumDirectoryBytes;
349 return createStream(Layout.SB->BlockSize, Layout.SB->NumBlocks, SL, MsfData);
350}
351
Zachary Turner8cf51c32016-08-03 16:53:21 +0000352std::unique_ptr<WritableMappedBlockStream>
353WritableMappedBlockStream::createFpmStream(const MSFLayout &Layout,
Zachary Turner120faca2017-02-27 22:11:43 +0000354 WritableBinaryStreamRef MsfData) {
Zachary Turner8cf51c32016-08-03 16:53:21 +0000355 MSFStreamLayout SL;
356 initializeFpmStreamLayout(Layout, SL);
357 return createStream(Layout.SB->BlockSize, Layout.SB->NumBlocks, SL, MsfData);
358}
359
Zachary Turnerd66889c2016-07-28 19:12:28 +0000360Error WritableMappedBlockStream::readBytes(uint32_t Offset, uint32_t Size,
Zachary Turner120faca2017-02-27 22:11:43 +0000361 ArrayRef<uint8_t> &Buffer) {
Zachary Turnerd66889c2016-07-28 19:12:28 +0000362 return ReadInterface.readBytes(Offset, Size, Buffer);
363}
364
365Error WritableMappedBlockStream::readLongestContiguousChunk(
Zachary Turner120faca2017-02-27 22:11:43 +0000366 uint32_t Offset, ArrayRef<uint8_t> &Buffer) {
Zachary Turnerd66889c2016-07-28 19:12:28 +0000367 return ReadInterface.readLongestContiguousChunk(Offset, Buffer);
368}
369
Zachary Turner120faca2017-02-27 22:11:43 +0000370uint32_t WritableMappedBlockStream::getLength() {
Zachary Turnerd66889c2016-07-28 19:12:28 +0000371 return ReadInterface.getLength();
372}
373
374Error WritableMappedBlockStream::writeBytes(uint32_t Offset,
Zachary Turner120faca2017-02-27 22:11:43 +0000375 ArrayRef<uint8_t> Buffer) {
Zachary Turnerd66889c2016-07-28 19:12:28 +0000376 // Make sure we aren't trying to write beyond the end of the stream.
377 if (Buffer.size() > getStreamLength())
Zachary Turnera3225b02016-07-29 20:56:36 +0000378 return make_error<MSFError>(msf_error_code::insufficient_buffer);
Zachary Turnerd66889c2016-07-28 19:12:28 +0000379
380 if (Offset > getStreamLayout().Length - Buffer.size())
Zachary Turnera3225b02016-07-29 20:56:36 +0000381 return make_error<MSFError>(msf_error_code::insufficient_buffer);
Zachary Turnerd66889c2016-07-28 19:12:28 +0000382
383 uint32_t BlockNum = Offset / getBlockSize();
384 uint32_t OffsetInBlock = Offset % getBlockSize();
385
386 uint32_t BytesLeft = Buffer.size();
387 uint32_t BytesWritten = 0;
388 while (BytesLeft > 0) {
389 uint32_t StreamBlockAddr = getStreamLayout().Blocks[BlockNum];
390 uint32_t BytesToWriteInChunk =
391 std::min(BytesLeft, getBlockSize() - OffsetInBlock);
392
393 const uint8_t *Chunk = Buffer.data() + BytesWritten;
394 ArrayRef<uint8_t> ChunkData(Chunk, BytesToWriteInChunk);
395 uint32_t MsfOffset = blockToOffset(StreamBlockAddr, getBlockSize());
396 MsfOffset += OffsetInBlock;
397 if (auto EC = WriteInterface.writeBytes(MsfOffset, ChunkData))
398 return EC;
399
400 BytesLeft -= BytesToWriteInChunk;
401 BytesWritten += BytesToWriteInChunk;
402 ++BlockNum;
403 OffsetInBlock = 0;
404 }
405
406 ReadInterface.fixCacheAfterWrite(Offset, Buffer);
Zachary Turner5acb4ac2016-06-10 05:09:12 +0000407
408 return Error::success();
Zachary Turnerf5c59652016-05-03 00:28:21 +0000409}
Zachary Turner90b8b8d2016-05-31 22:41:52 +0000410
Zachary Turner120faca2017-02-27 22:11:43 +0000411Error WritableMappedBlockStream::commit() { return WriteInterface.commit(); }