friend class WritableMappedBlockStream;
public:
static std::unique_ptr<MappedBlockStream>
- createStream(uint32_t BlockSize, uint32_t NumBlocks,
- const MSFStreamLayout &Layout, BinaryStreamRef MsfData);
+ createStream(uint32_t BlockSize, const MSFStreamLayout &Layout,
+ BinaryStreamRef MsfData);
static std::unique_ptr<MappedBlockStream>
createIndexedStream(const MSFLayout &Layout, BinaryStreamRef MsfData,
void invalidateCache();
uint32_t getBlockSize() const { return BlockSize; }
- uint32_t getNumBlocks() const { return NumBlocks; }
+ uint32_t getNumBlocks() const { return StreamLayout.Blocks.size(); }
uint32_t getStreamLength() const { return StreamLayout.Length; }
protected:
- MappedBlockStream(uint32_t BlockSize, uint32_t NumBlocks,
- const MSFStreamLayout &StreamLayout,
+ MappedBlockStream(uint32_t BlockSize, const MSFStreamLayout &StreamLayout,
BinaryStreamRef MsfData);
private:
ArrayRef<uint8_t> &Buffer);
const uint32_t BlockSize;
- const uint32_t NumBlocks;
const MSFStreamLayout StreamLayout;
BinaryStreamRef MsfData;
class WritableMappedBlockStream : public WritableBinaryStream {
public:
static std::unique_ptr<WritableMappedBlockStream>
- createStream(uint32_t BlockSize, uint32_t NumBlocks,
- const MSFStreamLayout &Layout, WritableBinaryStreamRef MsfData);
+ createStream(uint32_t BlockSize, const MSFStreamLayout &Layout,
+ WritableBinaryStreamRef MsfData);
static std::unique_ptr<WritableMappedBlockStream>
createIndexedStream(const MSFLayout &Layout, WritableBinaryStreamRef MsfData,
uint32_t getStreamLength() const { return ReadInterface.getStreamLength(); }
protected:
- WritableMappedBlockStream(uint32_t BlockSize, uint32_t NumBlocks,
+ WritableMappedBlockStream(uint32_t BlockSize,
const MSFStreamLayout &StreamLayout,
WritableBinaryStreamRef MsfData);
std::min(I1.second, I2.second));
}
-MappedBlockStream::MappedBlockStream(uint32_t BlockSize, uint32_t NumBlocks,
+MappedBlockStream::MappedBlockStream(uint32_t BlockSize,
const MSFStreamLayout &Layout,
BinaryStreamRef MsfData)
- : BlockSize(BlockSize), NumBlocks(NumBlocks), StreamLayout(Layout),
- MsfData(MsfData) {}
+ : BlockSize(BlockSize), StreamLayout(Layout), MsfData(MsfData) {}
std::unique_ptr<MappedBlockStream>
-MappedBlockStream::createStream(uint32_t BlockSize, uint32_t NumBlocks,
+MappedBlockStream::createStream(uint32_t BlockSize,
const MSFStreamLayout &Layout,
BinaryStreamRef MsfData) {
return llvm::make_unique<MappedBlockStreamImpl<MappedBlockStream>>(
- BlockSize, NumBlocks, Layout, MsfData);
+ BlockSize, Layout, MsfData);
}
std::unique_ptr<MappedBlockStream> MappedBlockStream::createIndexedStream(
SL.Blocks = Layout.StreamMap[StreamIndex];
SL.Length = Layout.StreamSizes[StreamIndex];
return llvm::make_unique<MappedBlockStreamImpl<MappedBlockStream>>(
- Layout.SB->BlockSize, Layout.SB->NumBlocks, SL, MsfData);
+ Layout.SB->BlockSize, SL, MsfData);
}
std::unique_ptr<MappedBlockStream>
MSFStreamLayout SL;
SL.Blocks = Layout.DirectoryBlocks;
SL.Length = Layout.SB->NumDirectoryBytes;
- return createStream(Layout.SB->BlockSize, Layout.SB->NumBlocks, SL, MsfData);
+ return createStream(Layout.SB->BlockSize, SL, MsfData);
}
std::unique_ptr<MappedBlockStream>
BinaryStreamRef MsfData) {
MSFStreamLayout SL;
initializeFpmStreamLayout(Layout, SL);
- return createStream(Layout.SB->BlockSize, Layout.SB->NumBlocks, SL, MsfData);
+ return createStream(Layout.SB->BlockSize, SL, MsfData);
}
Error MappedBlockStream::readBytes(uint32_t Offset, uint32_t Size,
uint32_t First = Offset / BlockSize;
uint32_t Last = First;
- while (Last < NumBlocks - 1) {
+ while (Last < getNumBlocks() - 1) {
if (StreamLayout.Blocks[Last] != StreamLayout.Blocks[Last + 1] - 1)
break;
++Last;
}
WritableMappedBlockStream::WritableMappedBlockStream(
- uint32_t BlockSize, uint32_t NumBlocks, const MSFStreamLayout &Layout,
+ uint32_t BlockSize, const MSFStreamLayout &Layout,
WritableBinaryStreamRef MsfData)
- : ReadInterface(BlockSize, NumBlocks, Layout, MsfData),
- WriteInterface(MsfData) {}
+ : ReadInterface(BlockSize, Layout, MsfData), WriteInterface(MsfData) {}
std::unique_ptr<WritableMappedBlockStream>
-WritableMappedBlockStream::createStream(uint32_t BlockSize, uint32_t NumBlocks,
+WritableMappedBlockStream::createStream(uint32_t BlockSize,
const MSFStreamLayout &Layout,
WritableBinaryStreamRef MsfData) {
return llvm::make_unique<MappedBlockStreamImpl<WritableMappedBlockStream>>(
- BlockSize, NumBlocks, Layout, MsfData);
+ BlockSize, Layout, MsfData);
}
std::unique_ptr<WritableMappedBlockStream>
MSFStreamLayout SL;
SL.Blocks = Layout.StreamMap[StreamIndex];
SL.Length = Layout.StreamSizes[StreamIndex];
- return createStream(Layout.SB->BlockSize, Layout.SB->NumBlocks, SL, MsfData);
+ return createStream(Layout.SB->BlockSize, SL, MsfData);
}
std::unique_ptr<WritableMappedBlockStream>
MSFStreamLayout SL;
SL.Blocks = Layout.DirectoryBlocks;
SL.Length = Layout.SB->NumDirectoryBytes;
- return createStream(Layout.SB->BlockSize, Layout.SB->NumBlocks, SL, MsfData);
+ return createStream(Layout.SB->BlockSize, SL, MsfData);
}
std::unique_ptr<WritableMappedBlockStream>
WritableBinaryStreamRef MsfData) {
MSFStreamLayout SL;
initializeFpmStreamLayout(Layout, SL);
- return createStream(Layout.SB->BlockSize, Layout.SB->NumBlocks, SL, MsfData);
+ return createStream(Layout.SB->BlockSize, SL, MsfData);
}
Error WritableMappedBlockStream::readBytes(uint32_t Offset, uint32_t Size,
MutableArrayRef<uint8_t> Data;
};
+TEST(MappedBlockStreamTest, NumBlocks) {
+ DiscontiguousStream F(BlocksAry, DataAry);
+ auto S = MappedBlockStream::createStream(F.block_size(), F.layout(), F);
+ EXPECT_EQ(F.block_size(), S->getBlockSize());
+ EXPECT_EQ(F.layout().Blocks.size(), S->getNumBlocks());
+
+}
+
// Tests that a read which is entirely contained within a single block works
// and does not allocate.
TEST(MappedBlockStreamTest, ReadBeyondEndOfStreamRef) {
DiscontiguousStream F(BlocksAry, DataAry);
- auto S = MappedBlockStream::createStream(F.block_size(), F.block_count(),
- F.layout(), F);
+ auto S = MappedBlockStream::createStream(F.block_size(), F.layout(), F);
BinaryStreamReader R(*S);
BinaryStreamRef SR;
// does not fail due to the length of the output buffer.
TEST(MappedBlockStreamTest, ReadOntoNonEmptyBuffer) {
DiscontiguousStream F(BlocksAry, DataAry);
- auto S = MappedBlockStream::createStream(F.block_size(), F.block_count(),
- F.layout(), F);
+ auto S = MappedBlockStream::createStream(F.block_size(), F.layout(), F);
BinaryStreamReader R(*S);
StringRef Str = "ZYXWVUTSRQPONMLKJIHGFEDCBA";
// not allocate memory.
TEST(MappedBlockStreamTest, ZeroCopyReadContiguousBreak) {
DiscontiguousStream F(BlocksAry, DataAry);
- auto S = MappedBlockStream::createStream(F.block_size(), F.block_count(),
+ auto S = MappedBlockStream::createStream(F.block_size(),
F.layout(), F);
BinaryStreamReader R(*S);
StringRef Str;
// requested.
TEST(MappedBlockStreamTest, CopyReadNonContiguousBreak) {
DiscontiguousStream F(BlocksAry, DataAry);
- auto S = MappedBlockStream::createStream(F.block_size(), F.block_count(),
- F.layout(), F);
+ auto S = MappedBlockStream::createStream(F.block_size(), F.layout(), F);
BinaryStreamReader R(*S);
StringRef Str;
EXPECT_NO_ERROR(R.readFixedString(Str, 10));
// fails and allocates no memory.
TEST(MappedBlockStreamTest, InvalidReadSizeNoBreak) {
DiscontiguousStream F(BlocksAry, DataAry);
- auto S = MappedBlockStream::createStream(F.block_size(), F.block_count(),
- F.layout(), F);
+ auto S = MappedBlockStream::createStream(F.block_size(), F.layout(), F);
BinaryStreamReader R(*S);
StringRef Str;
// fails and allocates no memory.
TEST(MappedBlockStreamTest, InvalidReadSizeContiguousBreak) {
DiscontiguousStream F(BlocksAry, DataAry);
- auto S = MappedBlockStream::createStream(F.block_size(), F.block_count(),
- F.layout(), F);
+ auto S = MappedBlockStream::createStream(F.block_size(), F.layout(), F);
BinaryStreamReader R(*S);
StringRef Str;
// boundary fails and allocates no memory.
TEST(MappedBlockStreamTest, InvalidReadSizeNonContiguousBreak) {
DiscontiguousStream F(BlocksAry, DataAry);
- auto S = MappedBlockStream::createStream(F.block_size(), F.block_count(),
- F.layout(), F);
+ auto S = MappedBlockStream::createStream(F.block_size(), F.layout(), F);
BinaryStreamReader R(*S);
StringRef Str;
// beyond the end of a StreamRef fails.
TEST(MappedBlockStreamTest, ZeroCopyReadNoBreak) {
DiscontiguousStream F(BlocksAry, DataAry);
- auto S = MappedBlockStream::createStream(F.block_size(), F.block_count(),
- F.layout(), F);
+ auto S = MappedBlockStream::createStream(F.block_size(), F.layout(), F);
BinaryStreamReader R(*S);
StringRef Str;
EXPECT_NO_ERROR(R.readFixedString(Str, 1));
// previous allocation.
TEST(MappedBlockStreamTest, UnalignedOverlappingRead) {
DiscontiguousStream F(BlocksAry, DataAry);
- auto S = MappedBlockStream::createStream(F.block_size(), F.block_count(),
- F.layout(), F);
+ auto S = MappedBlockStream::createStream(F.block_size(), F.layout(), F);
BinaryStreamReader R(*S);
StringRef Str1;
StringRef Str2;
// still works correctly and allocates again from the shared pool.
TEST(MappedBlockStreamTest, UnalignedOverlappingReadFail) {
DiscontiguousStream F(BlocksAry, DataAry);
- auto S = MappedBlockStream::createStream(F.block_size(), F.block_count(),
- F.layout(), F);
+ auto S = MappedBlockStream::createStream(F.block_size(), F.layout(), F);
BinaryStreamReader R(*S);
StringRef Str1;
StringRef Str2;
DiscontiguousStream F(BlocksAry, Data);
auto S = WritableMappedBlockStream::createStream(
- F.block_size(), F.block_count(), F.layout(), F);
+ F.block_size(), F.layout(), F);
ArrayRef<uint8_t> Buffer;
EXPECT_ERROR(S->writeBytes(0, ArrayRef<uint8_t>(LargeBuffer)));
static uint8_t Data[] = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J'};
DiscontiguousStream F(BlocksAry, Data);
auto S = WritableMappedBlockStream::createStream(
- F.block_size(), F.block_count(), F.layout(), F);
+ F.block_size(), F.layout(), F);
ArrayRef<uint8_t> Buffer;
EXPECT_NO_ERROR(S->readBytes(0, 1, Buffer));
DiscontiguousStream F(BlocksAry, Data);
auto S = WritableMappedBlockStream::createStream(
- F.block_size(), F.block_count(), F.layout(), F);
+ F.block_size(), F.layout(), F);
ArrayRef<uint8_t> Buffer;
EXPECT_NO_ERROR(S->writeBytes(0, TestData));
DiscontiguousStream F(Blocks, Data);
auto S = WritableMappedBlockStream::createStream(
- F.block_size(), F.block_count(), F.layout(), F);
+ F.block_size(), F.layout(), F);
enum class MyEnum : uint32_t { Val1 = 2908234, Val2 = 120891234 };
using support::ulittle32_t;
DiscontiguousStream F(DestBlocks, DestData);
auto DestStream = WritableMappedBlockStream::createStream(
- F.block_size(), F.block_count(), F.layout(), F);
+ F.block_size(), F.layout(), F);
// First write "Test Str" into the source stream.
MutableBinaryByteStream SourceStream(SrcData, little);
DiscontiguousStream SrcF(SrcBlocks, SrcData);
auto Dest = WritableMappedBlockStream::createStream(
- DestF.block_size(), DestF.block_count(), DestF.layout(), DestF);
+ DestF.block_size(), DestF.layout(), DestF);
auto Src = WritableMappedBlockStream::createStream(
- SrcF.block_size(), SrcF.block_count(), SrcF.layout(), SrcF);
+ SrcF.block_size(), SrcF.layout(), SrcF);
// First write "Test Str" into the source stream.
BinaryStreamWriter SourceWriter(*Src);