From 3d414de23eadccdf85fd1455a0dfcbce10e07cdd Mon Sep 17 00:00:00 2001 From: Stefan Boberg Date: Tue, 28 Sep 2021 21:57:23 +0200 Subject: Removed MemoryOutStream, MemoryInStream BinaryWriter/BinaryReader now implements memory buffer functionality which previously needed two chained instances of a Buffer/Reader. This was originally expected to be an abstraction for file and other stream access but this is not going to be useful so may as well collapse the functionality. This also eliminates the need for stack-aware ref-counting which is the real reason for wanting to get rid of this code. This was a very old experimental feature which turned out to be a bad idea. This also removes the /cas/batch endpoint --- zencore/include/zencore/stream.h | 149 ++++++----------------------------- zencore/include/zencore/streamutil.h | 69 ---------------- 2 files changed, 26 insertions(+), 192 deletions(-) delete mode 100644 zencore/include/zencore/streamutil.h (limited to 'zencore/include') diff --git a/zencore/include/zencore/stream.h b/zencore/include/zencore/stream.h index bab349068..9d1a7628c 100644 --- a/zencore/include/zencore/stream.h +++ b/zencore/include/zencore/stream.h @@ -5,41 +5,12 @@ #include "zencore.h" #include -#include #include -#include #include namespace zen { -/** - * Stream which writes into a growing memory buffer - */ -class MemoryOutStream : public RefCounted -{ -public: - MemoryOutStream() = default; - ~MemoryOutStream() = default; - - void Write(const void* DataPtr, size_t ByteCount, uint64_t Offset); - void Flush(); - inline const uint8_t* Data() const { return m_Buffer.data(); } - inline const uint8_t* GetData() const { return m_Buffer.data(); } - inline uint64_t Size() const { return m_Buffer.size(); } - inline uint64_t GetSize() const { return m_Buffer.size(); } - -private: - RwLock m_Lock; - std::vector m_Buffer; -}; - -inline MemoryView -MakeMemoryView(const MemoryOutStream& Stream) -{ - return MemoryView(Stream.Data(), Stream.Size()); -} - /** * Binary stream writer */ @@ -47,43 +18,36 @@ MakeMemoryView(const MemoryOutStream& Stream) class BinaryWriter { public: - inline BinaryWriter(MemoryOutStream& Stream) : m_Stream(&Stream) {} - ~BinaryWriter() = default; + inline BinaryWriter() = default; + ~BinaryWriter() = default; inline void Write(const void* DataPtr, size_t ByteCount) { - m_Stream->Write(DataPtr, ByteCount, m_Offset); + Write(DataPtr, ByteCount, m_Offset); m_Offset += ByteCount; } inline uint64_t CurrentOffset() const { return m_Offset; } -private: - RefPtr m_Stream; - uint64_t m_Offset = 0; -}; - -/** - * Stream which reads from a memory buffer - */ -class MemoryInStream : public RefCounted -{ -public: - MemoryInStream(const void* Buffer, size_t Size); - MemoryInStream(MemoryView View) : MemoryInStream(View.GetData(), View.GetSize()) {} - ~MemoryInStream() = default; - - void Read(void* DataPtr, size_t ByteCount, uint64_t ReadOffset); - uint64_t Size() const { return m_Buffer.size(); } - uint64_t GetSize() const { return Size(); } inline const uint8_t* Data() const { return m_Buffer.data(); } inline const uint8_t* GetData() const { return m_Buffer.data(); } + inline uint64_t Size() const { return m_Buffer.size(); } + inline uint64_t GetSize() const { return m_Buffer.size(); } private: RwLock m_Lock; std::vector m_Buffer; + uint64_t m_Offset = 0; + + void Write(const void* DataPtr, size_t ByteCount, uint64_t Offset); }; +inline MemoryView +MakeMemoryView(const BinaryWriter& Stream) +{ + return MemoryView(Stream.Data(), Stream.Size()); +} + /** * Binary stream reader */ @@ -91,90 +55,29 @@ private: class BinaryReader { public: - inline BinaryReader(MemoryInStream& Stream) : m_Stream(&Stream) {} - ~BinaryReader() = default; - - inline void Read(void* DataPtr, size_t ByteCount) - { - m_Stream->Read(DataPtr, ByteCount, m_Offset); - m_Offset += ByteCount; - } - - void Seek(uint64_t Offset) + inline BinaryReader(const void* Buffer, uint64_t Size) : m_BufferBase(reinterpret_cast(Buffer)), m_BufferSize(Size) {} + inline BinaryReader(MemoryView Buffer) + : m_BufferBase(reinterpret_cast(Buffer.GetData())) + , m_BufferSize(Buffer.GetSize()) { - ZEN_ASSERT(Offset <= m_Stream->Size()); - m_Offset = Offset; } - void Skip(uint64_t SkipOffset) + inline void Read(void* DataPtr, size_t ByteCount) { - ZEN_ASSERT((m_Offset + SkipOffset) <= m_Stream->Size()); - m_Offset += SkipOffset; + memcpy(DataPtr, m_BufferBase + m_Offset, ByteCount); + m_Offset += ByteCount; } + inline uint64_t Size() const { return m_BufferSize; } + inline uint64_t GetSize() const { return Size(); } inline uint64_t CurrentOffset() const { return m_Offset; } - inline uint64_t AvailableBytes() const { return m_Stream->Size() - m_Offset; } private: - RefPtr m_Stream; - uint64_t m_Offset = 0; + const uint8_t* m_BufferBase; + uint64_t m_BufferSize; + uint64_t m_Offset = 0; }; -inline BinaryReader& -operator>>(BinaryReader& Reader, bool& Value) -{ - Reader.Read(&Value, sizeof Value); - return Reader; -} -inline BinaryReader& -operator>>(BinaryReader& Reader, int8_t& Value) -{ - Reader.Read(&Value, sizeof Value); - return Reader; -} -inline BinaryReader& -operator>>(BinaryReader& Reader, int16_t& Value) -{ - Reader.Read(&Value, sizeof Value); - return Reader; -} -inline BinaryReader& -operator>>(BinaryReader& Reader, int32_t& Value) -{ - Reader.Read(&Value, sizeof Value); - return Reader; -} -inline BinaryReader& -operator>>(BinaryReader& Reader, int64_t& Value) -{ - Reader.Read(&Value, sizeof Value); - return Reader; -} -inline BinaryReader& -operator>>(BinaryReader& Reader, uint8_t& Value) -{ - Reader.Read(&Value, sizeof Value); - return Reader; -} -inline BinaryReader& -operator>>(BinaryReader& Reader, uint16_t& Value) -{ - Reader.Read(&Value, sizeof Value); - return Reader; -} -inline BinaryReader& -operator>>(BinaryReader& Reader, uint32_t& Value) -{ - Reader.Read(&Value, sizeof Value); - return Reader; -} -inline BinaryReader& -operator>>(BinaryReader& Reader, uint64_t& Value) -{ - Reader.Read(&Value, sizeof Value); - return Reader; -} - void stream_forcelink(); // internal } // namespace zen diff --git a/zencore/include/zencore/streamutil.h b/zencore/include/zencore/streamutil.h deleted file mode 100644 index d9b6b5167..000000000 --- a/zencore/include/zencore/streamutil.h +++ /dev/null @@ -1,69 +0,0 @@ -// Copyright Epic Games, Inc. All Rights Reserved. - -#pragma once - -#include -#include -#include -#include - -#include "blake3.h" -#include "iohash.h" -#include "sha1.h" -#include "stream.h" - -namespace zen { - -} // namespace zen - -////////////////////////////////////////////////////////////////////////// - -template<> -struct fmt::formatter -{ - constexpr auto parse(format_parse_context& ctx) - { - // Parse the presentation format and store it in the formatter: - auto it = ctx.begin(), end = ctx.end(); - - // Check if reached the end of the range: - if (it != end && *it != '}') - throw format_error("invalid format"); - - // Return an iterator past the end of the parsed range: - return it; - } - - template - auto format(const zen::IoHash& h, FormatContext& ctx) - { - zen::ExtendableStringBuilder<48> String; - h.ToHexString(String); - return format_to(ctx.out(), std::string_view(String)); - } -}; - -template<> -struct fmt::formatter -{ - constexpr auto parse(format_parse_context& ctx) - { - // Parse the presentation format and store it in the formatter: - auto it = ctx.begin(), end = ctx.end(); - - // Check if reached the end of the range: - if (it != end && *it != '}') - throw format_error("invalid format"); - - // Return an iterator past the end of the parsed range: - return it; - } - - template - auto format(const zen::BLAKE3& h, FormatContext& ctx) - { - zen::ExtendableStringBuilder<80> String; - h.ToHexString(String); - return format_to(ctx.out(), std::string_view(String)); - } -}; -- cgit v1.2.3