blob: e52c88a5bfb821f85c166e20f714b0833c43bd1e [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,
NAKAMURA Takumi05a75e42017-02-25 17:04:23 +000050 const ReadableStream &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,
NAKAMURA Takumi05a75e42017-02-25 17:04:23 +000057 const ReadableStream &MsfData) {
Zachary Turnerd66889c2016-07-28 19:12:28 +000058 return llvm::make_unique<MappedBlockStreamImpl<MappedBlockStream>>(
59 BlockSize, NumBlocks, Layout, MsfData);
60}
61
NAKAMURA Takumi05a75e42017-02-25 17:04:23 +000062std::unique_ptr<MappedBlockStream>
63MappedBlockStream::createIndexedStream(const MSFLayout &Layout,
64 const ReadableStream &MsfData,
65 uint32_t StreamIndex) {
Bob Haarmana5b43582016-12-05 22:44:00 +000066 assert(StreamIndex < Layout.StreamMap.size() && "Invalid stream index");
Zachary Turnera3225b02016-07-29 20:56:36 +000067 MSFStreamLayout SL;
Zachary Turnerd66889c2016-07-28 19:12:28 +000068 SL.Blocks = Layout.StreamMap[StreamIndex];
69 SL.Length = Layout.StreamSizes[StreamIndex];
70 return llvm::make_unique<MappedBlockStreamImpl<MappedBlockStream>>(
71 Layout.SB->BlockSize, Layout.SB->NumBlocks, SL, MsfData);
72}
73
74std::unique_ptr<MappedBlockStream>
Zachary Turnera3225b02016-07-29 20:56:36 +000075MappedBlockStream::createDirectoryStream(const MSFLayout &Layout,
NAKAMURA Takumi05a75e42017-02-25 17:04:23 +000076 const ReadableStream &MsfData) {
Zachary Turnera3225b02016-07-29 20:56:36 +000077 MSFStreamLayout SL;
Zachary Turnerd66889c2016-07-28 19:12:28 +000078 SL.Blocks = Layout.DirectoryBlocks;
79 SL.Length = Layout.SB->NumDirectoryBytes;
80 return createStream(Layout.SB->BlockSize, Layout.SB->NumBlocks, SL, MsfData);
81}
Zachary Turner6ba65de2016-04-29 17:22:58 +000082
Zachary Turner8cf51c32016-08-03 16:53:21 +000083std::unique_ptr<MappedBlockStream>
84MappedBlockStream::createFpmStream(const MSFLayout &Layout,
NAKAMURA Takumi05a75e42017-02-25 17:04:23 +000085 const ReadableStream &MsfData) {
Zachary Turner8cf51c32016-08-03 16:53:21 +000086 MSFStreamLayout SL;
87 initializeFpmStreamLayout(Layout, SL);
88 return createStream(Layout.SB->BlockSize, Layout.SB->NumBlocks, SL, MsfData);
89}
90
Zachary Turner8dbe3622016-05-27 01:54:44 +000091Error MappedBlockStream::readBytes(uint32_t Offset, uint32_t Size,
NAKAMURA Takumi05a75e42017-02-25 17:04:23 +000092 ArrayRef<uint8_t> &Buffer) const {
Zachary Turner8dbe3622016-05-27 01:54:44 +000093 // Make sure we aren't trying to read beyond the end of the stream.
Zachary Turnerd66889c2016-07-28 19:12:28 +000094 if (Size > StreamLayout.Length)
Zachary Turnera3225b02016-07-29 20:56:36 +000095 return make_error<MSFError>(msf_error_code::insufficient_buffer);
Zachary Turnerd66889c2016-07-28 19:12:28 +000096 if (Offset > StreamLayout.Length - Size)
Zachary Turnera3225b02016-07-29 20:56:36 +000097 return make_error<MSFError>(msf_error_code::insufficient_buffer);
Zachary Turner8dbe3622016-05-27 01:54:44 +000098
99 if (tryReadContiguously(Offset, Size, Buffer))
100 return Error::success();
101
102 auto CacheIter = CacheMap.find(Offset);
103 if (CacheIter != CacheMap.end()) {
Zachary Turner5acb4ac2016-06-10 05:09:12 +0000104 // Try to find an alloc that was large enough for this request.
105 for (auto &Entry : CacheIter->second) {
106 if (Entry.size() >= Size) {
107 Buffer = Entry.slice(0, Size);
108 return Error::success();
109 }
110 }
111 }
112
113 // We couldn't find a buffer that started at the correct offset (the most
114 // common scenario). Try to see if there is a buffer that starts at some
115 // other offset but overlaps the desired range.
116 for (auto &CacheItem : CacheMap) {
117 Interval RequestExtent = std::make_pair(Offset, Offset + Size);
118
119 // We already checked this one on the fast path above.
120 if (CacheItem.first == Offset)
121 continue;
122 // If the initial extent of the cached item is beyond the ending extent
123 // of the request, there is no overlap.
124 if (CacheItem.first >= Offset + Size)
125 continue;
126
127 // We really only have to check the last item in the list, since we append
128 // in order of increasing length.
129 if (CacheItem.second.empty())
130 continue;
131
132 auto CachedAlloc = CacheItem.second.back();
133 // If the initial extent of the request is beyond the ending extent of
134 // the cached item, there is no overlap.
135 Interval CachedExtent =
136 std::make_pair(CacheItem.first, CacheItem.first + CachedAlloc.size());
137 if (RequestExtent.first >= CachedExtent.first + CachedExtent.second)
138 continue;
139
140 Interval Intersection = intersect(CachedExtent, RequestExtent);
141 // Only use this if the entire request extent is contained in the cached
142 // extent.
143 if (Intersection != RequestExtent)
144 continue;
145
146 uint32_t CacheRangeOffset =
147 AbsoluteDifference(CachedExtent.first, Intersection.first);
148 Buffer = CachedAlloc.slice(CacheRangeOffset, Size);
Zachary Turner8dbe3622016-05-27 01:54:44 +0000149 return Error::success();
150 }
151
152 // Otherwise allocate a large enough buffer in the pool, memcpy the data
Zachary Turner5acb4ac2016-06-10 05:09:12 +0000153 // into it, and return an ArrayRef to that. Do not touch existing pool
154 // allocations, as existing clients may be holding a pointer which must
155 // not be invalidated.
Zachary Turner97609bb2016-06-10 21:47:26 +0000156 uint8_t *WriteBuffer = static_cast<uint8_t *>(Pool.Allocate(Size, 8));
Zachary Turner8dbe3622016-05-27 01:54:44 +0000157 if (auto EC = readBytes(Offset, MutableArrayRef<uint8_t>(WriteBuffer, Size)))
158 return EC;
Zachary Turner5acb4ac2016-06-10 05:09:12 +0000159
160 if (CacheIter != CacheMap.end()) {
161 CacheIter->second.emplace_back(WriteBuffer, Size);
162 } else {
163 std::vector<CacheEntry> List;
164 List.emplace_back(WriteBuffer, Size);
165 CacheMap.insert(std::make_pair(Offset, List));
166 }
Zachary Turner8dbe3622016-05-27 01:54:44 +0000167 Buffer = ArrayRef<uint8_t>(WriteBuffer, Size);
168 return Error::success();
169}
170
NAKAMURA Takumi05a75e42017-02-25 17:04:23 +0000171Error MappedBlockStream::readLongestContiguousChunk(
172 uint32_t Offset, ArrayRef<uint8_t> &Buffer) const {
Zachary Turner5acb4ac2016-06-10 05:09:12 +0000173 // Make sure we aren't trying to read beyond the end of the stream.
Zachary Turnerd66889c2016-07-28 19:12:28 +0000174 if (Offset >= StreamLayout.Length)
Zachary Turnera3225b02016-07-29 20:56:36 +0000175 return make_error<MSFError>(msf_error_code::insufficient_buffer);
Zachary Turnerd66889c2016-07-28 19:12:28 +0000176 uint32_t First = Offset / BlockSize;
Zachary Turner5acb4ac2016-06-10 05:09:12 +0000177 uint32_t Last = First;
178
Zachary Turnerd66889c2016-07-28 19:12:28 +0000179 while (Last < NumBlocks - 1) {
180 if (StreamLayout.Blocks[Last] != StreamLayout.Blocks[Last + 1] - 1)
Zachary Turner5acb4ac2016-06-10 05:09:12 +0000181 break;
182 ++Last;
183 }
184
Zachary Turnerd66889c2016-07-28 19:12:28 +0000185 uint32_t OffsetInFirstBlock = Offset % BlockSize;
186 uint32_t BytesFromFirstBlock = BlockSize - OffsetInFirstBlock;
Zachary Turner5acb4ac2016-06-10 05:09:12 +0000187 uint32_t BlockSpan = Last - First + 1;
Zachary Turnerd66889c2016-07-28 19:12:28 +0000188 uint32_t ByteSpan = BytesFromFirstBlock + (BlockSpan - 1) * BlockSize;
189
190 ArrayRef<uint8_t> BlockData;
191 uint32_t MsfOffset = blockToOffset(StreamLayout.Blocks[First], BlockSize);
192 if (auto EC = MsfData.readBytes(MsfOffset, BlockSize, BlockData))
193 return EC;
194
195 BlockData = BlockData.drop_front(OffsetInFirstBlock);
196 Buffer = ArrayRef<uint8_t>(BlockData.data(), ByteSpan);
Zachary Turner5acb4ac2016-06-10 05:09:12 +0000197 return Error::success();
198}
199
NAKAMURA Takumi05a75e42017-02-25 17:04:23 +0000200uint32_t MappedBlockStream::getLength() const { return StreamLayout.Length; }
Zachary Turnerab58ae82016-06-30 17:43:00 +0000201
Zachary Turner8dbe3622016-05-27 01:54:44 +0000202bool MappedBlockStream::tryReadContiguously(uint32_t Offset, uint32_t Size,
NAKAMURA Takumi05a75e42017-02-25 17:04:23 +0000203 ArrayRef<uint8_t> &Buffer) const {
Zachary Turner36efbfa2016-09-09 19:00:49 +0000204 if (Size == 0) {
205 Buffer = ArrayRef<uint8_t>();
206 return true;
207 }
Zachary Turner8dbe3622016-05-27 01:54:44 +0000208 // Attempt to fulfill the request with a reference directly into the stream.
209 // This can work even if the request crosses a block boundary, provided that
210 // all subsequent blocks are contiguous. For example, a 10k read with a 4k
211 // block size can be filled with a reference if, from the starting offset,
212 // 3 blocks in a row are contiguous.
Zachary Turnerd66889c2016-07-28 19:12:28 +0000213 uint32_t BlockNum = Offset / BlockSize;
214 uint32_t OffsetInBlock = Offset % BlockSize;
215 uint32_t BytesFromFirstBlock = std::min(Size, BlockSize - OffsetInBlock);
Zachary Turner8dbe3622016-05-27 01:54:44 +0000216 uint32_t NumAdditionalBlocks =
Zachary Turnerd66889c2016-07-28 19:12:28 +0000217 llvm::alignTo(Size - BytesFromFirstBlock, BlockSize) / BlockSize;
Zachary Turner8dbe3622016-05-27 01:54:44 +0000218
219 uint32_t RequiredContiguousBlocks = NumAdditionalBlocks + 1;
Zachary Turnerd66889c2016-07-28 19:12:28 +0000220 uint32_t E = StreamLayout.Blocks[BlockNum];
Zachary Turner8dbe3622016-05-27 01:54:44 +0000221 for (uint32_t I = 0; I < RequiredContiguousBlocks; ++I, ++E) {
Zachary Turnerd66889c2016-07-28 19:12:28 +0000222 if (StreamLayout.Blocks[I + BlockNum] != E)
Zachary Turner8dbe3622016-05-27 01:54:44 +0000223 return false;
224 }
225
Zachary Turnerd66889c2016-07-28 19:12:28 +0000226 // Read out the entire block where the requested offset starts. Then drop
227 // bytes from the beginning so that the actual starting byte lines up with
228 // the requested starting byte. Then, since we know this is a contiguous
229 // cross-block span, explicitly resize the ArrayRef to cover the entire
230 // request length.
231 ArrayRef<uint8_t> BlockData;
232 uint32_t FirstBlockAddr = StreamLayout.Blocks[BlockNum];
233 uint32_t MsfOffset = blockToOffset(FirstBlockAddr, BlockSize);
234 if (auto EC = MsfData.readBytes(MsfOffset, BlockSize, BlockData)) {
235 consumeError(std::move(EC));
David Majnemer6211b1f2016-07-10 03:34:47 +0000236 return false;
237 }
Zachary Turnerd66889c2016-07-28 19:12:28 +0000238 BlockData = BlockData.drop_front(OffsetInBlock);
239 Buffer = ArrayRef<uint8_t>(BlockData.data(), Size);
Zachary Turner8dbe3622016-05-27 01:54:44 +0000240 return true;
241}
242
Zachary Turner819e77d2016-05-06 20:51:57 +0000243Error MappedBlockStream::readBytes(uint32_t Offset,
NAKAMURA Takumi05a75e42017-02-25 17:04:23 +0000244 MutableArrayRef<uint8_t> Buffer) const {
Zachary Turnerd66889c2016-07-28 19:12:28 +0000245 uint32_t BlockNum = Offset / BlockSize;
246 uint32_t OffsetInBlock = Offset % BlockSize;
Zachary Turner6ba65de2016-04-29 17:22:58 +0000247
248 // Make sure we aren't trying to read beyond the end of the stream.
Zachary Turnerd66889c2016-07-28 19:12:28 +0000249 if (Buffer.size() > StreamLayout.Length)
Zachary Turnera3225b02016-07-29 20:56:36 +0000250 return make_error<MSFError>(msf_error_code::insufficient_buffer);
Zachary Turnerd66889c2016-07-28 19:12:28 +0000251 if (Offset > StreamLayout.Length - Buffer.size())
Zachary Turnera3225b02016-07-29 20:56:36 +0000252 return make_error<MSFError>(msf_error_code::insufficient_buffer);
Zachary Turner6ba65de2016-04-29 17:22:58 +0000253
254 uint32_t BytesLeft = Buffer.size();
255 uint32_t BytesWritten = 0;
256 uint8_t *WriteBuffer = Buffer.data();
257 while (BytesLeft > 0) {
Zachary Turnerd66889c2016-07-28 19:12:28 +0000258 uint32_t StreamBlockAddr = StreamLayout.Blocks[BlockNum];
Zachary Turner6ba65de2016-04-29 17:22:58 +0000259
Zachary Turnerd66889c2016-07-28 19:12:28 +0000260 ArrayRef<uint8_t> BlockData;
261 uint32_t Offset = blockToOffset(StreamBlockAddr, BlockSize);
262 if (auto EC = MsfData.readBytes(Offset, BlockSize, BlockData))
263 return EC;
Zachary Turner6ba65de2016-04-29 17:22:58 +0000264
Zachary Turnerd66889c2016-07-28 19:12:28 +0000265 const uint8_t *ChunkStart = BlockData.data() + OffsetInBlock;
266 uint32_t BytesInChunk = std::min(BytesLeft, BlockSize - OffsetInBlock);
Zachary Turner6ba65de2016-04-29 17:22:58 +0000267 ::memcpy(WriteBuffer + BytesWritten, ChunkStart, BytesInChunk);
268
269 BytesWritten += BytesInChunk;
270 BytesLeft -= BytesInChunk;
271 ++BlockNum;
272 OffsetInBlock = 0;
273 }
274
Zachary Turner819e77d2016-05-06 20:51:57 +0000275 return Error::success();
Zachary Turner5acb4ac2016-06-10 05:09:12 +0000276}
Zachary Turnerf5c59652016-05-03 00:28:21 +0000277
Zachary Turnerd66889c2016-07-28 19:12:28 +0000278uint32_t MappedBlockStream::getNumBytesCopied() const {
279 return static_cast<uint32_t>(Pool.getBytesAllocated());
280}
Zachary Turner5acb4ac2016-06-10 05:09:12 +0000281
Zachary Turnerd66889c2016-07-28 19:12:28 +0000282void MappedBlockStream::invalidateCache() { CacheMap.shrink_and_clear(); }
Zachary Turner5acb4ac2016-06-10 05:09:12 +0000283
Zachary Turnerd66889c2016-07-28 19:12:28 +0000284void MappedBlockStream::fixCacheAfterWrite(uint32_t Offset,
285 ArrayRef<uint8_t> Data) const {
Zachary Turner5acb4ac2016-06-10 05:09:12 +0000286 // If this write overlapped a read which previously came from the pool,
287 // someone may still be holding a pointer to that alloc which is now invalid.
288 // Compute the overlapping range and update the cache entry, so any
289 // outstanding buffers are automatically updated.
290 for (const auto &MapEntry : CacheMap) {
291 // If the end of the written extent precedes the beginning of the cached
292 // extent, ignore this map entry.
Zachary Turnerd66889c2016-07-28 19:12:28 +0000293 if (Offset + Data.size() < MapEntry.first)
Zachary Turner5acb4ac2016-06-10 05:09:12 +0000294 continue;
295 for (const auto &Alloc : MapEntry.second) {
296 // If the end of the cached extent precedes the beginning of the written
297 // extent, ignore this alloc.
298 if (MapEntry.first + Alloc.size() < Offset)
299 continue;
300
301 // If we get here, they are guaranteed to overlap.
Zachary Turnerd66889c2016-07-28 19:12:28 +0000302 Interval WriteInterval = std::make_pair(Offset, Offset + Data.size());
Zachary Turner5acb4ac2016-06-10 05:09:12 +0000303 Interval CachedInterval =
304 std::make_pair(MapEntry.first, MapEntry.first + Alloc.size());
305 // If they overlap, we need to write the new data into the overlapping
306 // range.
307 auto Intersection = intersect(WriteInterval, CachedInterval);
308 assert(Intersection.first <= Intersection.second);
309
310 uint32_t Length = Intersection.second - Intersection.first;
311 uint32_t SrcOffset =
312 AbsoluteDifference(WriteInterval.first, Intersection.first);
313 uint32_t DestOffset =
314 AbsoluteDifference(CachedInterval.first, Intersection.first);
Zachary Turnerd66889c2016-07-28 19:12:28 +0000315 ::memcpy(Alloc.data() + DestOffset, Data.data() + SrcOffset, Length);
Zachary Turner5acb4ac2016-06-10 05:09:12 +0000316 }
317 }
Zachary Turnerd66889c2016-07-28 19:12:28 +0000318}
319
320WritableMappedBlockStream::WritableMappedBlockStream(
Zachary Turnera3225b02016-07-29 20:56:36 +0000321 uint32_t BlockSize, uint32_t NumBlocks, const MSFStreamLayout &Layout,
NAKAMURA Takumi05a75e42017-02-25 17:04:23 +0000322 const WritableStream &MsfData)
Zachary Turnerd66889c2016-07-28 19:12:28 +0000323 : ReadInterface(BlockSize, NumBlocks, Layout, MsfData),
324 WriteInterface(MsfData) {}
325
326std::unique_ptr<WritableMappedBlockStream>
327WritableMappedBlockStream::createStream(uint32_t BlockSize, uint32_t NumBlocks,
Zachary Turnera3225b02016-07-29 20:56:36 +0000328 const MSFStreamLayout &Layout,
NAKAMURA Takumi05a75e42017-02-25 17:04:23 +0000329 const WritableStream &MsfData) {
Zachary Turnerd66889c2016-07-28 19:12:28 +0000330 return llvm::make_unique<MappedBlockStreamImpl<WritableMappedBlockStream>>(
331 BlockSize, NumBlocks, Layout, MsfData);
332}
333
334std::unique_ptr<WritableMappedBlockStream>
Zachary Turnera3225b02016-07-29 20:56:36 +0000335WritableMappedBlockStream::createIndexedStream(const MSFLayout &Layout,
NAKAMURA Takumi05a75e42017-02-25 17:04:23 +0000336 const WritableStream &MsfData,
Zachary Turnerd66889c2016-07-28 19:12:28 +0000337 uint32_t StreamIndex) {
Bob Haarmana5b43582016-12-05 22:44:00 +0000338 assert(StreamIndex < Layout.StreamMap.size() && "Invalid stream index");
Zachary Turnera3225b02016-07-29 20:56:36 +0000339 MSFStreamLayout SL;
Zachary Turnerd66889c2016-07-28 19:12:28 +0000340 SL.Blocks = Layout.StreamMap[StreamIndex];
341 SL.Length = Layout.StreamSizes[StreamIndex];
342 return createStream(Layout.SB->BlockSize, Layout.SB->NumBlocks, SL, MsfData);
343}
344
345std::unique_ptr<WritableMappedBlockStream>
346WritableMappedBlockStream::createDirectoryStream(
NAKAMURA Takumi05a75e42017-02-25 17:04:23 +0000347 const MSFLayout &Layout, const WritableStream &MsfData) {
Zachary Turnera3225b02016-07-29 20:56:36 +0000348 MSFStreamLayout SL;
Zachary Turnerd66889c2016-07-28 19:12:28 +0000349 SL.Blocks = Layout.DirectoryBlocks;
350 SL.Length = Layout.SB->NumDirectoryBytes;
351 return createStream(Layout.SB->BlockSize, Layout.SB->NumBlocks, SL, MsfData);
352}
353
Zachary Turner8cf51c32016-08-03 16:53:21 +0000354std::unique_ptr<WritableMappedBlockStream>
355WritableMappedBlockStream::createFpmStream(const MSFLayout &Layout,
NAKAMURA Takumi05a75e42017-02-25 17:04:23 +0000356 const WritableStream &MsfData) {
Zachary Turner8cf51c32016-08-03 16:53:21 +0000357 MSFStreamLayout SL;
358 initializeFpmStreamLayout(Layout, SL);
359 return createStream(Layout.SB->BlockSize, Layout.SB->NumBlocks, SL, MsfData);
360}
361
Zachary Turnerd66889c2016-07-28 19:12:28 +0000362Error WritableMappedBlockStream::readBytes(uint32_t Offset, uint32_t Size,
NAKAMURA Takumi05a75e42017-02-25 17:04:23 +0000363 ArrayRef<uint8_t> &Buffer) const {
Zachary Turnerd66889c2016-07-28 19:12:28 +0000364 return ReadInterface.readBytes(Offset, Size, Buffer);
365}
366
367Error WritableMappedBlockStream::readLongestContiguousChunk(
NAKAMURA Takumi05a75e42017-02-25 17:04:23 +0000368 uint32_t Offset, ArrayRef<uint8_t> &Buffer) const {
Zachary Turnerd66889c2016-07-28 19:12:28 +0000369 return ReadInterface.readLongestContiguousChunk(Offset, Buffer);
370}
371
NAKAMURA Takumi05a75e42017-02-25 17:04:23 +0000372uint32_t WritableMappedBlockStream::getLength() const {
Zachary Turnerd66889c2016-07-28 19:12:28 +0000373 return ReadInterface.getLength();
374}
375
376Error WritableMappedBlockStream::writeBytes(uint32_t Offset,
NAKAMURA Takumi05a75e42017-02-25 17:04:23 +0000377 ArrayRef<uint8_t> Buffer) const {
Zachary Turnerd66889c2016-07-28 19:12:28 +0000378 // Make sure we aren't trying to write beyond the end of the stream.
379 if (Buffer.size() > getStreamLength())
Zachary Turnera3225b02016-07-29 20:56:36 +0000380 return make_error<MSFError>(msf_error_code::insufficient_buffer);
Zachary Turnerd66889c2016-07-28 19:12:28 +0000381
382 if (Offset > getStreamLayout().Length - Buffer.size())
Zachary Turnera3225b02016-07-29 20:56:36 +0000383 return make_error<MSFError>(msf_error_code::insufficient_buffer);
Zachary Turnerd66889c2016-07-28 19:12:28 +0000384
385 uint32_t BlockNum = Offset / getBlockSize();
386 uint32_t OffsetInBlock = Offset % getBlockSize();
387
388 uint32_t BytesLeft = Buffer.size();
389 uint32_t BytesWritten = 0;
390 while (BytesLeft > 0) {
391 uint32_t StreamBlockAddr = getStreamLayout().Blocks[BlockNum];
392 uint32_t BytesToWriteInChunk =
393 std::min(BytesLeft, getBlockSize() - OffsetInBlock);
394
395 const uint8_t *Chunk = Buffer.data() + BytesWritten;
396 ArrayRef<uint8_t> ChunkData(Chunk, BytesToWriteInChunk);
397 uint32_t MsfOffset = blockToOffset(StreamBlockAddr, getBlockSize());
398 MsfOffset += OffsetInBlock;
399 if (auto EC = WriteInterface.writeBytes(MsfOffset, ChunkData))
400 return EC;
401
402 BytesLeft -= BytesToWriteInChunk;
403 BytesWritten += BytesToWriteInChunk;
404 ++BlockNum;
405 OffsetInBlock = 0;
406 }
407
408 ReadInterface.fixCacheAfterWrite(Offset, Buffer);
Zachary Turner5acb4ac2016-06-10 05:09:12 +0000409
410 return Error::success();
Zachary Turnerf5c59652016-05-03 00:28:21 +0000411}
Zachary Turner90b8b8d2016-05-31 22:41:52 +0000412
NAKAMURA Takumi05a75e42017-02-25 17:04:23 +0000413Error WritableMappedBlockStream::commit() const {
414 return WriteInterface.commit();
415}