diff options
| author | Dan Engelbrecht <[email protected]> | 2022-03-23 11:14:12 +0100 |
|---|---|---|
| committer | Dan Engelbrecht <[email protected]> | 2022-03-31 11:29:27 +0200 |
| commit | 600bb21d3328ba4958c55c38459845c7a11359cc (patch) | |
| tree | 68268fa4d6aed399e83e1679258e4d4e0bd40811 /zenstore/include | |
| parent | Remove bad kIsOwnedByThis (diff) | |
| download | zen-600bb21d3328ba4958c55c38459845c7a11359cc.tar.xz zen-600bb21d3328ba4958c55c38459845c7a11359cc.zip | |
Add separate blockstore.h/.cpp
Diffstat (limited to 'zenstore/include')
| -rw-r--r-- | zenstore/include/zenstore/blockstore.h | 103 |
1 files changed, 103 insertions, 0 deletions
diff --git a/zenstore/include/zenstore/blockstore.h b/zenstore/include/zenstore/blockstore.h new file mode 100644 index 000000000..29f520722 --- /dev/null +++ b/zenstore/include/zenstore/blockstore.h @@ -0,0 +1,103 @@ +// Copyright Epic Games, Inc. All Rights Reserved. + +#pragma once + +#include <zencore/zencore.h> + +namespace zen { + +////////////////////////////////////////////////////////////////////////// + +struct BlockStoreLocation +{ + uint32_t BlockIndex; + uint64_t Offset; + uint64_t Size; +}; + +#pragma pack(push) +#pragma pack(1) + +struct BlockStoreDiskLocation +{ + constexpr static uint32_t MaxBlockIndexBits = 20; + constexpr static uint32_t MaxOffsetBits = 28; + constexpr static uint32_t MaxBlockIndex = (1ul << BlockStoreDiskLocation::MaxBlockIndexBits) - 1ul; + constexpr static uint32_t MaxOffset = (1ul << BlockStoreDiskLocation::MaxOffsetBits) - 1ul; + + BlockStoreDiskLocation(const BlockStoreLocation& Location, uint64_t OffsetAlignement) + { + Init(Location.BlockIndex, Location.Offset / OffsetAlignement, Location.Size); + } + + BlockStoreDiskLocation() = default; + + inline BlockStoreLocation Get(uint64_t OffsetAlignement) const + { + uint64_t PackedOffset = 0; + memcpy(&PackedOffset, &m_Offset, sizeof m_Offset); + return {.BlockIndex = static_cast<std::uint32_t>(PackedOffset >> MaxOffsetBits), + .Offset = (PackedOffset & MaxOffset) * OffsetAlignement, + .Size = GetSize()}; + } + + inline uint32_t GetBlockIndex() const + { + uint64_t PackedOffset = 0; + memcpy(&PackedOffset, &m_Offset, sizeof m_Offset); + return static_cast<std::uint32_t>(PackedOffset >> MaxOffsetBits); + } + + inline uint64_t GetOffset() const + { + uint64_t PackedOffset = 0; + memcpy(&PackedOffset, &m_Offset, sizeof m_Offset); + return PackedOffset & MaxOffset; + } + + inline uint64_t GetSize() const { return m_Size; } + +private: + inline void Init(uint32_t BlockIndex, uint64_t Offset, uint64_t Size) + { + ZEN_ASSERT(BlockIndex <= MaxBlockIndex); + ZEN_ASSERT(Offset <= MaxOffset); + ZEN_ASSERT(Size <= std::numeric_limits<std::uint32_t>::max()); + + m_Size = static_cast<uint32_t>(Size); + uint64_t PackedOffset = (static_cast<uint64_t>(BlockIndex) << MaxOffsetBits) + Offset; + memcpy(&m_Offset[0], &PackedOffset, sizeof m_Offset); + } + + uint32_t m_Size; + uint8_t m_Offset[6]; +}; + +#pragma pack(pop) + +struct BlockStoreFile +{ + explicit BlockStoreFile(const std::filesystem::path& BlockPath); + ~BlockStoreFile(); + const std::filesystem::path& GetPath() const; + void Open(); + void Create(uint64_t InitialSize); + void MarkAsDeleteOnClose(std::error_code& Ec); + uint64_t FileSize(); + IoBuffer GetChunk(uint64_t Offset, uint64_t Size); + void Read(void* Data, uint64_t Size, uint64_t FileOffset); + void Write(const void* Data, uint64_t Size, uint64_t FileOffset); + void Flush(); + void StreamByteRange(uint64_t FileOffset, uint64_t Size, std::function<void(const void* Data, uint64_t Size)>&& ChunkFun); + +private: + void InternalOpen(); + const std::filesystem::path m_Path; + RwLock m_OpenLock; + BasicFile m_File; + IoBuffer m_IoBuffer; +}; + +void blockstore_forcelink(); + +} // namespace zen |