2016-07-23 03:56:05 +08:00
|
|
|
//===- MappedBlockStream.cpp - Reads stream data from an MSF file ---------===//
|
2016-04-26 01:38:08 +08:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2016-07-30 04:56:36 +08:00
|
|
|
#include "llvm/DebugInfo/MSF/MappedBlockStream.h"
|
2016-07-29 03:11:09 +08:00
|
|
|
|
2016-07-30 04:56:36 +08:00
|
|
|
#include "llvm/DebugInfo/MSF/IMSFFile.h"
|
|
|
|
#include "llvm/DebugInfo/MSF/MSFCommon.h"
|
|
|
|
#include "llvm/DebugInfo/MSF/MSFStreamLayout.h"
|
2017-03-03 04:52:51 +08:00
|
|
|
#include "llvm/Support/BinaryStreamError.h"
|
2016-04-26 01:38:08 +08:00
|
|
|
|
|
|
|
using namespace llvm;
|
2016-07-23 03:56:05 +08:00
|
|
|
using namespace llvm::msf;
|
2016-04-26 01:38:08 +08:00
|
|
|
|
2016-06-09 01:26:39 +08:00
|
|
|
namespace {
|
2016-07-29 03:12:28 +08:00
|
|
|
template <typename Base> class MappedBlockStreamImpl : public Base {
|
2016-06-09 01:26:39 +08:00
|
|
|
public:
|
2016-07-29 03:12:28 +08:00
|
|
|
template <typename... Args>
|
|
|
|
MappedBlockStreamImpl(Args &&... Params)
|
|
|
|
: Base(std::forward<Args>(Params)...) {}
|
2016-06-09 01:26:39 +08:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2016-08-04 00:53:21 +08:00
|
|
|
static void initializeFpmStreamLayout(const MSFLayout &Layout,
|
|
|
|
MSFStreamLayout &FpmLayout) {
|
|
|
|
uint32_t NumFpmIntervals = msf::getNumFpmIntervals(Layout);
|
|
|
|
support::ulittle32_t FpmBlock = Layout.SB->FreeBlockMapBlock;
|
|
|
|
assert(FpmBlock == 1 || FpmBlock == 2);
|
|
|
|
while (NumFpmIntervals > 0) {
|
|
|
|
FpmLayout.Blocks.push_back(FpmBlock);
|
|
|
|
FpmBlock += msf::getFpmIntervalLength(Layout);
|
|
|
|
--NumFpmIntervals;
|
|
|
|
}
|
|
|
|
FpmLayout.Length = msf::getFullFpmByteSize(Layout);
|
|
|
|
}
|
|
|
|
|
2016-06-10 13:09:12 +08:00
|
|
|
typedef std::pair<uint32_t, uint32_t> Interval;
|
|
|
|
static Interval intersect(const Interval &I1, const Interval &I2) {
|
|
|
|
return std::make_pair(std::max(I1.first, I2.first),
|
|
|
|
std::min(I1.second, I2.second));
|
|
|
|
}
|
|
|
|
|
2016-07-29 03:12:28 +08:00
|
|
|
MappedBlockStream::MappedBlockStream(uint32_t BlockSize, uint32_t NumBlocks,
|
2016-07-30 04:56:36 +08:00
|
|
|
const MSFStreamLayout &Layout,
|
2017-02-28 06:11:43 +08:00
|
|
|
BinaryStreamRef MsfData)
|
2016-07-29 03:12:28 +08:00
|
|
|
: BlockSize(BlockSize), NumBlocks(NumBlocks), StreamLayout(Layout),
|
|
|
|
MsfData(MsfData) {}
|
|
|
|
|
|
|
|
std::unique_ptr<MappedBlockStream>
|
|
|
|
MappedBlockStream::createStream(uint32_t BlockSize, uint32_t NumBlocks,
|
2016-07-30 04:56:36 +08:00
|
|
|
const MSFStreamLayout &Layout,
|
2017-02-28 06:11:43 +08:00
|
|
|
BinaryStreamRef MsfData) {
|
2016-07-29 03:12:28 +08:00
|
|
|
return llvm::make_unique<MappedBlockStreamImpl<MappedBlockStream>>(
|
|
|
|
BlockSize, NumBlocks, Layout, MsfData);
|
|
|
|
}
|
|
|
|
|
2017-02-28 06:11:43 +08:00
|
|
|
std::unique_ptr<MappedBlockStream> MappedBlockStream::createIndexedStream(
|
|
|
|
const MSFLayout &Layout, BinaryStreamRef MsfData, uint32_t StreamIndex) {
|
2016-12-06 06:44:00 +08:00
|
|
|
assert(StreamIndex < Layout.StreamMap.size() && "Invalid stream index");
|
2016-07-30 04:56:36 +08:00
|
|
|
MSFStreamLayout SL;
|
2016-07-29 03:12:28 +08:00
|
|
|
SL.Blocks = Layout.StreamMap[StreamIndex];
|
|
|
|
SL.Length = Layout.StreamSizes[StreamIndex];
|
|
|
|
return llvm::make_unique<MappedBlockStreamImpl<MappedBlockStream>>(
|
|
|
|
Layout.SB->BlockSize, Layout.SB->NumBlocks, SL, MsfData);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::unique_ptr<MappedBlockStream>
|
2016-07-30 04:56:36 +08:00
|
|
|
MappedBlockStream::createDirectoryStream(const MSFLayout &Layout,
|
2017-02-28 06:11:43 +08:00
|
|
|
BinaryStreamRef MsfData) {
|
2016-07-30 04:56:36 +08:00
|
|
|
MSFStreamLayout SL;
|
2016-07-29 03:12:28 +08:00
|
|
|
SL.Blocks = Layout.DirectoryBlocks;
|
|
|
|
SL.Length = Layout.SB->NumDirectoryBytes;
|
|
|
|
return createStream(Layout.SB->BlockSize, Layout.SB->NumBlocks, SL, MsfData);
|
|
|
|
}
|
2016-04-26 01:38:08 +08:00
|
|
|
|
2016-08-04 00:53:21 +08:00
|
|
|
std::unique_ptr<MappedBlockStream>
|
|
|
|
MappedBlockStream::createFpmStream(const MSFLayout &Layout,
|
2017-02-28 06:11:43 +08:00
|
|
|
BinaryStreamRef MsfData) {
|
2016-08-04 00:53:21 +08:00
|
|
|
MSFStreamLayout SL;
|
|
|
|
initializeFpmStreamLayout(Layout, SL);
|
|
|
|
return createStream(Layout.SB->BlockSize, Layout.SB->NumBlocks, SL, MsfData);
|
|
|
|
}
|
|
|
|
|
2016-05-27 09:54:44 +08:00
|
|
|
Error MappedBlockStream::readBytes(uint32_t Offset, uint32_t Size,
|
2017-02-28 06:11:43 +08:00
|
|
|
ArrayRef<uint8_t> &Buffer) {
|
2016-05-27 09:54:44 +08:00
|
|
|
// Make sure we aren't trying to read beyond the end of the stream.
|
2017-03-01 01:49:34 +08:00
|
|
|
if (auto EC = checkOffset(Offset, Size))
|
|
|
|
return EC;
|
2016-05-27 09:54:44 +08:00
|
|
|
|
|
|
|
if (tryReadContiguously(Offset, Size, Buffer))
|
|
|
|
return Error::success();
|
|
|
|
|
|
|
|
auto CacheIter = CacheMap.find(Offset);
|
|
|
|
if (CacheIter != CacheMap.end()) {
|
2016-06-10 13:09:12 +08:00
|
|
|
// Try to find an alloc that was large enough for this request.
|
|
|
|
for (auto &Entry : CacheIter->second) {
|
|
|
|
if (Entry.size() >= Size) {
|
|
|
|
Buffer = Entry.slice(0, Size);
|
|
|
|
return Error::success();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// We couldn't find a buffer that started at the correct offset (the most
|
|
|
|
// common scenario). Try to see if there is a buffer that starts at some
|
|
|
|
// other offset but overlaps the desired range.
|
|
|
|
for (auto &CacheItem : CacheMap) {
|
|
|
|
Interval RequestExtent = std::make_pair(Offset, Offset + Size);
|
|
|
|
|
|
|
|
// We already checked this one on the fast path above.
|
|
|
|
if (CacheItem.first == Offset)
|
|
|
|
continue;
|
|
|
|
// If the initial extent of the cached item is beyond the ending extent
|
|
|
|
// of the request, there is no overlap.
|
|
|
|
if (CacheItem.first >= Offset + Size)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// We really only have to check the last item in the list, since we append
|
|
|
|
// in order of increasing length.
|
|
|
|
if (CacheItem.second.empty())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
auto CachedAlloc = CacheItem.second.back();
|
|
|
|
// If the initial extent of the request is beyond the ending extent of
|
|
|
|
// the cached item, there is no overlap.
|
|
|
|
Interval CachedExtent =
|
|
|
|
std::make_pair(CacheItem.first, CacheItem.first + CachedAlloc.size());
|
|
|
|
if (RequestExtent.first >= CachedExtent.first + CachedExtent.second)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
Interval Intersection = intersect(CachedExtent, RequestExtent);
|
|
|
|
// Only use this if the entire request extent is contained in the cached
|
|
|
|
// extent.
|
|
|
|
if (Intersection != RequestExtent)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
uint32_t CacheRangeOffset =
|
|
|
|
AbsoluteDifference(CachedExtent.first, Intersection.first);
|
|
|
|
Buffer = CachedAlloc.slice(CacheRangeOffset, Size);
|
2016-05-27 09:54:44 +08:00
|
|
|
return Error::success();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise allocate a large enough buffer in the pool, memcpy the data
|
2016-06-10 13:09:12 +08:00
|
|
|
// into it, and return an ArrayRef to that. Do not touch existing pool
|
|
|
|
// allocations, as existing clients may be holding a pointer which must
|
|
|
|
// not be invalidated.
|
2016-06-11 05:47:26 +08:00
|
|
|
uint8_t *WriteBuffer = static_cast<uint8_t *>(Pool.Allocate(Size, 8));
|
2016-05-27 09:54:44 +08:00
|
|
|
if (auto EC = readBytes(Offset, MutableArrayRef<uint8_t>(WriteBuffer, Size)))
|
|
|
|
return EC;
|
2016-06-10 13:09:12 +08:00
|
|
|
|
|
|
|
if (CacheIter != CacheMap.end()) {
|
|
|
|
CacheIter->second.emplace_back(WriteBuffer, Size);
|
|
|
|
} else {
|
|
|
|
std::vector<CacheEntry> List;
|
|
|
|
List.emplace_back(WriteBuffer, Size);
|
|
|
|
CacheMap.insert(std::make_pair(Offset, List));
|
|
|
|
}
|
2016-05-27 09:54:44 +08:00
|
|
|
Buffer = ArrayRef<uint8_t>(WriteBuffer, Size);
|
|
|
|
return Error::success();
|
|
|
|
}
|
|
|
|
|
2017-02-28 06:11:43 +08:00
|
|
|
Error MappedBlockStream::readLongestContiguousChunk(uint32_t Offset,
|
|
|
|
ArrayRef<uint8_t> &Buffer) {
|
2016-06-10 13:09:12 +08:00
|
|
|
// Make sure we aren't trying to read beyond the end of the stream.
|
2017-03-01 01:49:34 +08:00
|
|
|
if (auto EC = checkOffset(Offset, 1))
|
|
|
|
return EC;
|
|
|
|
|
2016-07-29 03:12:28 +08:00
|
|
|
uint32_t First = Offset / BlockSize;
|
2016-06-10 13:09:12 +08:00
|
|
|
uint32_t Last = First;
|
|
|
|
|
2016-07-29 03:12:28 +08:00
|
|
|
while (Last < NumBlocks - 1) {
|
|
|
|
if (StreamLayout.Blocks[Last] != StreamLayout.Blocks[Last + 1] - 1)
|
2016-06-10 13:09:12 +08:00
|
|
|
break;
|
|
|
|
++Last;
|
|
|
|
}
|
|
|
|
|
2016-07-29 03:12:28 +08:00
|
|
|
uint32_t OffsetInFirstBlock = Offset % BlockSize;
|
|
|
|
uint32_t BytesFromFirstBlock = BlockSize - OffsetInFirstBlock;
|
2016-06-10 13:09:12 +08:00
|
|
|
uint32_t BlockSpan = Last - First + 1;
|
2016-07-29 03:12:28 +08:00
|
|
|
uint32_t ByteSpan = BytesFromFirstBlock + (BlockSpan - 1) * BlockSize;
|
|
|
|
|
|
|
|
ArrayRef<uint8_t> BlockData;
|
|
|
|
uint32_t MsfOffset = blockToOffset(StreamLayout.Blocks[First], BlockSize);
|
|
|
|
if (auto EC = MsfData.readBytes(MsfOffset, BlockSize, BlockData))
|
|
|
|
return EC;
|
|
|
|
|
|
|
|
BlockData = BlockData.drop_front(OffsetInFirstBlock);
|
|
|
|
Buffer = ArrayRef<uint8_t>(BlockData.data(), ByteSpan);
|
2016-06-10 13:09:12 +08:00
|
|
|
return Error::success();
|
|
|
|
}
|
|
|
|
|
2017-02-28 06:11:43 +08:00
|
|
|
uint32_t MappedBlockStream::getLength() { return StreamLayout.Length; }
|
2016-07-01 01:43:00 +08:00
|
|
|
|
2016-05-27 09:54:44 +08:00
|
|
|
bool MappedBlockStream::tryReadContiguously(uint32_t Offset, uint32_t Size,
|
2017-02-28 06:11:43 +08:00
|
|
|
ArrayRef<uint8_t> &Buffer) {
|
2016-09-10 03:00:49 +08:00
|
|
|
if (Size == 0) {
|
|
|
|
Buffer = ArrayRef<uint8_t>();
|
|
|
|
return true;
|
|
|
|
}
|
2016-05-27 09:54:44 +08:00
|
|
|
// Attempt to fulfill the request with a reference directly into the stream.
|
|
|
|
// This can work even if the request crosses a block boundary, provided that
|
|
|
|
// all subsequent blocks are contiguous. For example, a 10k read with a 4k
|
|
|
|
// block size can be filled with a reference if, from the starting offset,
|
|
|
|
// 3 blocks in a row are contiguous.
|
2016-07-29 03:12:28 +08:00
|
|
|
uint32_t BlockNum = Offset / BlockSize;
|
|
|
|
uint32_t OffsetInBlock = Offset % BlockSize;
|
|
|
|
uint32_t BytesFromFirstBlock = std::min(Size, BlockSize - OffsetInBlock);
|
2016-05-27 09:54:44 +08:00
|
|
|
uint32_t NumAdditionalBlocks =
|
2016-07-29 03:12:28 +08:00
|
|
|
llvm::alignTo(Size - BytesFromFirstBlock, BlockSize) / BlockSize;
|
2016-05-27 09:54:44 +08:00
|
|
|
|
|
|
|
uint32_t RequiredContiguousBlocks = NumAdditionalBlocks + 1;
|
2016-07-29 03:12:28 +08:00
|
|
|
uint32_t E = StreamLayout.Blocks[BlockNum];
|
2016-05-27 09:54:44 +08:00
|
|
|
for (uint32_t I = 0; I < RequiredContiguousBlocks; ++I, ++E) {
|
2016-07-29 03:12:28 +08:00
|
|
|
if (StreamLayout.Blocks[I + BlockNum] != E)
|
2016-05-27 09:54:44 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-07-29 03:12:28 +08:00
|
|
|
// Read out the entire block where the requested offset starts. Then drop
|
|
|
|
// bytes from the beginning so that the actual starting byte lines up with
|
|
|
|
// the requested starting byte. Then, since we know this is a contiguous
|
|
|
|
// cross-block span, explicitly resize the ArrayRef to cover the entire
|
|
|
|
// request length.
|
|
|
|
ArrayRef<uint8_t> BlockData;
|
|
|
|
uint32_t FirstBlockAddr = StreamLayout.Blocks[BlockNum];
|
|
|
|
uint32_t MsfOffset = blockToOffset(FirstBlockAddr, BlockSize);
|
|
|
|
if (auto EC = MsfData.readBytes(MsfOffset, BlockSize, BlockData)) {
|
|
|
|
consumeError(std::move(EC));
|
2016-07-10 11:34:47 +08:00
|
|
|
return false;
|
|
|
|
}
|
2016-07-29 03:12:28 +08:00
|
|
|
BlockData = BlockData.drop_front(OffsetInBlock);
|
|
|
|
Buffer = ArrayRef<uint8_t>(BlockData.data(), Size);
|
2016-05-27 09:54:44 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-05-07 04:51:57 +08:00
|
|
|
Error MappedBlockStream::readBytes(uint32_t Offset,
|
2017-02-28 06:11:43 +08:00
|
|
|
MutableArrayRef<uint8_t> Buffer) {
|
2016-07-29 03:12:28 +08:00
|
|
|
uint32_t BlockNum = Offset / BlockSize;
|
|
|
|
uint32_t OffsetInBlock = Offset % BlockSize;
|
2016-04-26 01:38:08 +08:00
|
|
|
|
|
|
|
// Make sure we aren't trying to read beyond the end of the stream.
|
2017-03-01 01:49:34 +08:00
|
|
|
if (auto EC = checkOffset(Offset, Buffer.size()))
|
|
|
|
return EC;
|
2016-04-26 01:38:08 +08:00
|
|
|
|
2016-04-30 01:22:58 +08:00
|
|
|
uint32_t BytesLeft = Buffer.size();
|
2016-04-26 01:38:08 +08:00
|
|
|
uint32_t BytesWritten = 0;
|
2016-04-30 01:22:58 +08:00
|
|
|
uint8_t *WriteBuffer = Buffer.data();
|
2016-04-26 01:38:08 +08:00
|
|
|
while (BytesLeft > 0) {
|
2016-07-29 03:12:28 +08:00
|
|
|
uint32_t StreamBlockAddr = StreamLayout.Blocks[BlockNum];
|
2016-04-26 01:38:08 +08:00
|
|
|
|
2016-07-29 03:12:28 +08:00
|
|
|
ArrayRef<uint8_t> BlockData;
|
|
|
|
uint32_t Offset = blockToOffset(StreamBlockAddr, BlockSize);
|
|
|
|
if (auto EC = MsfData.readBytes(Offset, BlockSize, BlockData))
|
|
|
|
return EC;
|
2016-04-26 01:38:08 +08:00
|
|
|
|
2016-07-29 03:12:28 +08:00
|
|
|
const uint8_t *ChunkStart = BlockData.data() + OffsetInBlock;
|
|
|
|
uint32_t BytesInChunk = std::min(BytesLeft, BlockSize - OffsetInBlock);
|
2016-04-26 01:38:08 +08:00
|
|
|
::memcpy(WriteBuffer + BytesWritten, ChunkStart, BytesInChunk);
|
|
|
|
|
|
|
|
BytesWritten += BytesInChunk;
|
|
|
|
BytesLeft -= BytesInChunk;
|
|
|
|
++BlockNum;
|
|
|
|
OffsetInBlock = 0;
|
|
|
|
}
|
2016-04-29 07:24:23 +08:00
|
|
|
|
2016-05-07 04:51:57 +08:00
|
|
|
return Error::success();
|
2016-06-10 13:09:12 +08:00
|
|
|
}
|
2016-05-03 08:28:21 +08:00
|
|
|
|
2016-07-29 03:12:28 +08:00
|
|
|
uint32_t MappedBlockStream::getNumBytesCopied() const {
|
|
|
|
return static_cast<uint32_t>(Pool.getBytesAllocated());
|
|
|
|
}
|
2016-06-10 13:09:12 +08:00
|
|
|
|
2016-07-29 03:12:28 +08:00
|
|
|
void MappedBlockStream::invalidateCache() { CacheMap.shrink_and_clear(); }
|
2016-06-10 13:09:12 +08:00
|
|
|
|
2016-07-29 03:12:28 +08:00
|
|
|
void MappedBlockStream::fixCacheAfterWrite(uint32_t Offset,
|
|
|
|
ArrayRef<uint8_t> Data) const {
|
2016-06-10 13:09:12 +08:00
|
|
|
// If this write overlapped a read which previously came from the pool,
|
|
|
|
// someone may still be holding a pointer to that alloc which is now invalid.
|
|
|
|
// Compute the overlapping range and update the cache entry, so any
|
|
|
|
// outstanding buffers are automatically updated.
|
|
|
|
for (const auto &MapEntry : CacheMap) {
|
|
|
|
// If the end of the written extent precedes the beginning of the cached
|
|
|
|
// extent, ignore this map entry.
|
2016-07-29 03:12:28 +08:00
|
|
|
if (Offset + Data.size() < MapEntry.first)
|
2016-06-10 13:09:12 +08:00
|
|
|
continue;
|
|
|
|
for (const auto &Alloc : MapEntry.second) {
|
|
|
|
// If the end of the cached extent precedes the beginning of the written
|
|
|
|
// extent, ignore this alloc.
|
|
|
|
if (MapEntry.first + Alloc.size() < Offset)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// If we get here, they are guaranteed to overlap.
|
2016-07-29 03:12:28 +08:00
|
|
|
Interval WriteInterval = std::make_pair(Offset, Offset + Data.size());
|
2016-06-10 13:09:12 +08:00
|
|
|
Interval CachedInterval =
|
|
|
|
std::make_pair(MapEntry.first, MapEntry.first + Alloc.size());
|
|
|
|
// If they overlap, we need to write the new data into the overlapping
|
|
|
|
// range.
|
|
|
|
auto Intersection = intersect(WriteInterval, CachedInterval);
|
|
|
|
assert(Intersection.first <= Intersection.second);
|
|
|
|
|
|
|
|
uint32_t Length = Intersection.second - Intersection.first;
|
|
|
|
uint32_t SrcOffset =
|
|
|
|
AbsoluteDifference(WriteInterval.first, Intersection.first);
|
|
|
|
uint32_t DestOffset =
|
|
|
|
AbsoluteDifference(CachedInterval.first, Intersection.first);
|
2016-07-29 03:12:28 +08:00
|
|
|
::memcpy(Alloc.data() + DestOffset, Data.data() + SrcOffset, Length);
|
2016-06-10 13:09:12 +08:00
|
|
|
}
|
|
|
|
}
|
2016-07-29 03:12:28 +08:00
|
|
|
}
|
2016-06-10 13:09:12 +08:00
|
|
|
|
2016-07-29 03:12:28 +08:00
|
|
|
WritableMappedBlockStream::WritableMappedBlockStream(
|
2016-07-30 04:56:36 +08:00
|
|
|
uint32_t BlockSize, uint32_t NumBlocks, const MSFStreamLayout &Layout,
|
2017-02-28 06:11:43 +08:00
|
|
|
WritableBinaryStreamRef MsfData)
|
2016-07-29 03:12:28 +08:00
|
|
|
: ReadInterface(BlockSize, NumBlocks, Layout, MsfData),
|
|
|
|
WriteInterface(MsfData) {}
|
|
|
|
|
|
|
|
std::unique_ptr<WritableMappedBlockStream>
|
|
|
|
WritableMappedBlockStream::createStream(uint32_t BlockSize, uint32_t NumBlocks,
|
2016-07-30 04:56:36 +08:00
|
|
|
const MSFStreamLayout &Layout,
|
2017-02-28 06:11:43 +08:00
|
|
|
WritableBinaryStreamRef MsfData) {
|
2016-07-29 03:12:28 +08:00
|
|
|
return llvm::make_unique<MappedBlockStreamImpl<WritableMappedBlockStream>>(
|
|
|
|
BlockSize, NumBlocks, Layout, MsfData);
|
2016-05-03 08:28:21 +08:00
|
|
|
}
|
2016-06-01 06:41:52 +08:00
|
|
|
|
2016-07-29 03:12:28 +08:00
|
|
|
std::unique_ptr<WritableMappedBlockStream>
|
2016-07-30 04:56:36 +08:00
|
|
|
WritableMappedBlockStream::createIndexedStream(const MSFLayout &Layout,
|
2017-02-28 06:11:43 +08:00
|
|
|
WritableBinaryStreamRef MsfData,
|
2016-07-29 03:12:28 +08:00
|
|
|
uint32_t StreamIndex) {
|
2016-12-06 06:44:00 +08:00
|
|
|
assert(StreamIndex < Layout.StreamMap.size() && "Invalid stream index");
|
2016-07-30 04:56:36 +08:00
|
|
|
MSFStreamLayout SL;
|
2016-07-29 03:12:28 +08:00
|
|
|
SL.Blocks = Layout.StreamMap[StreamIndex];
|
|
|
|
SL.Length = Layout.StreamSizes[StreamIndex];
|
|
|
|
return createStream(Layout.SB->BlockSize, Layout.SB->NumBlocks, SL, MsfData);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::unique_ptr<WritableMappedBlockStream>
|
|
|
|
WritableMappedBlockStream::createDirectoryStream(
|
2017-02-28 06:11:43 +08:00
|
|
|
const MSFLayout &Layout, WritableBinaryStreamRef MsfData) {
|
2016-07-30 04:56:36 +08:00
|
|
|
MSFStreamLayout SL;
|
2016-07-29 03:12:28 +08:00
|
|
|
SL.Blocks = Layout.DirectoryBlocks;
|
|
|
|
SL.Length = Layout.SB->NumDirectoryBytes;
|
|
|
|
return createStream(Layout.SB->BlockSize, Layout.SB->NumBlocks, SL, MsfData);
|
|
|
|
}
|
|
|
|
|
2016-08-04 00:53:21 +08:00
|
|
|
std::unique_ptr<WritableMappedBlockStream>
|
|
|
|
WritableMappedBlockStream::createFpmStream(const MSFLayout &Layout,
|
2017-02-28 06:11:43 +08:00
|
|
|
WritableBinaryStreamRef MsfData) {
|
2016-08-04 00:53:21 +08:00
|
|
|
MSFStreamLayout SL;
|
|
|
|
initializeFpmStreamLayout(Layout, SL);
|
|
|
|
return createStream(Layout.SB->BlockSize, Layout.SB->NumBlocks, SL, MsfData);
|
|
|
|
}
|
|
|
|
|
2016-07-29 03:12:28 +08:00
|
|
|
Error WritableMappedBlockStream::readBytes(uint32_t Offset, uint32_t Size,
|
2017-02-28 06:11:43 +08:00
|
|
|
ArrayRef<uint8_t> &Buffer) {
|
2016-07-29 03:12:28 +08:00
|
|
|
return ReadInterface.readBytes(Offset, Size, Buffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
Error WritableMappedBlockStream::readLongestContiguousChunk(
|
2017-02-28 06:11:43 +08:00
|
|
|
uint32_t Offset, ArrayRef<uint8_t> &Buffer) {
|
2016-07-29 03:12:28 +08:00
|
|
|
return ReadInterface.readLongestContiguousChunk(Offset, Buffer);
|
2016-06-01 06:41:52 +08:00
|
|
|
}
|
2016-06-09 01:26:39 +08:00
|
|
|
|
2017-02-28 06:11:43 +08:00
|
|
|
uint32_t WritableMappedBlockStream::getLength() {
|
2016-07-29 03:12:28 +08:00
|
|
|
return ReadInterface.getLength();
|
2016-06-09 01:26:39 +08:00
|
|
|
}
|
|
|
|
|
2016-07-29 03:12:28 +08:00
|
|
|
Error WritableMappedBlockStream::writeBytes(uint32_t Offset,
|
2017-02-28 06:11:43 +08:00
|
|
|
ArrayRef<uint8_t> Buffer) {
|
2016-07-29 03:12:28 +08:00
|
|
|
// Make sure we aren't trying to write beyond the end of the stream.
|
2017-03-01 01:49:34 +08:00
|
|
|
if (auto EC = checkOffset(Offset, Buffer.size()))
|
|
|
|
return EC;
|
2016-07-29 03:12:28 +08:00
|
|
|
|
|
|
|
uint32_t BlockNum = Offset / getBlockSize();
|
|
|
|
uint32_t OffsetInBlock = Offset % getBlockSize();
|
|
|
|
|
|
|
|
uint32_t BytesLeft = Buffer.size();
|
|
|
|
uint32_t BytesWritten = 0;
|
|
|
|
while (BytesLeft > 0) {
|
|
|
|
uint32_t StreamBlockAddr = getStreamLayout().Blocks[BlockNum];
|
|
|
|
uint32_t BytesToWriteInChunk =
|
|
|
|
std::min(BytesLeft, getBlockSize() - OffsetInBlock);
|
|
|
|
|
|
|
|
const uint8_t *Chunk = Buffer.data() + BytesWritten;
|
|
|
|
ArrayRef<uint8_t> ChunkData(Chunk, BytesToWriteInChunk);
|
|
|
|
uint32_t MsfOffset = blockToOffset(StreamBlockAddr, getBlockSize());
|
|
|
|
MsfOffset += OffsetInBlock;
|
|
|
|
if (auto EC = WriteInterface.writeBytes(MsfOffset, ChunkData))
|
|
|
|
return EC;
|
|
|
|
|
|
|
|
BytesLeft -= BytesToWriteInChunk;
|
|
|
|
BytesWritten += BytesToWriteInChunk;
|
|
|
|
++BlockNum;
|
|
|
|
OffsetInBlock = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
ReadInterface.fixCacheAfterWrite(Offset, Buffer);
|
|
|
|
|
|
|
|
return Error::success();
|
|
|
|
}
|
2016-07-29 03:11:09 +08:00
|
|
|
|
2017-02-28 06:11:43 +08:00
|
|
|
Error WritableMappedBlockStream::commit() { return WriteInterface.commit(); }
|