diff options
| author | Stefan Boberg <[email protected]> | 2021-05-13 12:01:18 +0200 |
|---|---|---|
| committer | Stefan Boberg <[email protected]> | 2021-05-13 12:01:18 +0200 |
| commit | 0b8280d4fcbf3a65997c3cc86cf14f5f7c44b7fe (patch) | |
| tree | 5ce29b79d0636e8e127c68c9fb9928b67759d1ac /zencore/include | |
| parent | clang-format (diff) | |
| download | zen-0b8280d4fcbf3a65997c3cc86cf14f5f7c44b7fe.tar.xz zen-0b8280d4fcbf3a65997c3cc86cf14f5f7c44b7fe.zip | |
Made SharedBuffer/UniqueBuffer share guts with IoBuffer
This enables way more efficient marshaling of compact binary objects and attachments
Diffstat (limited to 'zencore/include')
| -rw-r--r-- | zencore/include/zencore/iobuffer.h | 45 | ||||
| -rw-r--r-- | zencore/include/zencore/sharedbuffer.h | 101 |
2 files changed, 58 insertions, 88 deletions
diff --git a/zencore/include/zencore/iobuffer.h b/zencore/include/zencore/iobuffer.h index c93d27959..f38af3c27 100644 --- a/zencore/include/zencore/iobuffer.h +++ b/zencore/include/zencore/iobuffer.h @@ -65,13 +65,38 @@ public: Materialize(); } - inline bool IsOwned() const { return !!(m_Flags & kIsOwned); } + inline bool IsOwnedByThis() const { return !!(m_Flags & kIsOwnedByThis); } + + inline void SetIsOwnedByThis(bool NewState) + { + if (NewState) + { + m_Flags |= kIsOwnedByThis; + } + else + { + m_Flags &= ~kIsOwnedByThis; + } + } + + inline bool IsOwned() const + { + bool ThisIsOwned = !!(m_Flags & kIsOwnedByThis); + if (ThisIsOwned) + { + return true; + } + return m_OuterCore && m_OuterCore->IsOwned(); + } + inline bool IsImmutable() const { return !(m_Flags & kIsMutable); } inline bool IsNull() const { return m_DataBytes == 0; } inline IoBufferExtendedCore* ExtendedCore(); inline const IoBufferExtendedCore* ExtendedCore() const; + ZENCORE_API void* MutableDataPointer() const; + inline const void* DataPointer() const { EnsureDataValid(); @@ -86,18 +111,6 @@ public: m_DataBytes = Sz; } - inline void SetIsOwned(bool NewState) - { - if (NewState) - { - m_Flags |= kIsOwned; - } - else - { - m_Flags &= ~kIsOwned; - } - } - inline void SetIsImmutable(bool NewState) { if (!NewState) @@ -121,7 +134,7 @@ protected: enum Flags { - kIsOwned = 1 << 0, + kIsOwnedByThis = 1 << 0, kIsMutable = 1 << 1, kIsExtended = 1 << 2, // Is actually a SharedBufferExtendedCore kIsMaterialized = 1 << 3, // Data pointers are valid @@ -240,7 +253,7 @@ public: inline IoBuffer(EAssumeOwnershipTag, const void* DataPtr, size_t Sz) : m_Core(new IoBufferCore(DataPtr, Sz)) { - m_Core->SetIsOwned(true); + m_Core->SetIsOwnedByThis(true); } ZENCORE_API IoBuffer(EFileTag, void* FileHandle, uint64_t ChunkFileOffset, uint64_t ChunkSize); @@ -255,6 +268,8 @@ public: private: RefPtr<IoBufferCore> m_Core = new IoBufferCore; + + friend class SharedBuffer; }; class IoBufferBuilder diff --git a/zencore/include/zencore/sharedbuffer.h b/zencore/include/zencore/sharedbuffer.h index c6206f780..093f43231 100644 --- a/zencore/include/zencore/sharedbuffer.h +++ b/zencore/include/zencore/sharedbuffer.h @@ -4,6 +4,7 @@ #include "zencore.h" +#include <zencore/iobuffer.h> #include <zencore/memory.h> #include <zencore/refcount.h> @@ -11,49 +12,10 @@ namespace zen { -class BufferOwner : public RefCounted -{ -protected: - inline BufferOwner(void* DataPtr, uint64_t DataSize, bool IsOwned, BufferOwner* OuterBuffer = nullptr) - : m_IsOwned(IsOwned) - , m_Data(DataPtr) - , m_Size(DataSize) - , m_Outer(OuterBuffer) - { - } - - virtual ~BufferOwner(); - - // Ownership is a transitive property, and m_IsOwned currently only flags that this instance is responsible - // for managing the allocated memory, so we need to make recursive calls. Could be optimized slightly by - // adding a dedicated flag - inline bool IsOwned() const - { - if (m_IsOwned) - { - return true; - } - else - { - return m_Outer && m_Outer->IsOwned(); - } - } - - BufferOwner(const BufferOwner&) = delete; - BufferOwner& operator=(const BufferOwner&) = delete; - -private: - bool m_IsOwned; - void* m_Data; - uint64_t m_Size; - RefPtr<BufferOwner> m_Outer; - - friend class UniqueBuffer; - friend class SharedBuffer; -}; - /** - * Reference to a memory buffer with a single owner (see std::unique_ptr) + * Reference to a memory buffer with a single owner + * + * Internally */ class UniqueBuffer { @@ -62,24 +24,25 @@ public: UniqueBuffer& operator=(const UniqueBuffer&) = delete; UniqueBuffer() = default; - ZENCORE_API explicit UniqueBuffer(BufferOwner* Owner); + ZENCORE_API explicit UniqueBuffer(IoBufferCore* Owner); - void* GetData() { return m_buffer->m_Data; } - const void* GetData() const { return m_buffer->m_Data; } - size_t GetSize() const { return m_buffer->m_Size; } - operator MutableMemoryView() { return GetView(); } - operator MemoryView() const { return MemoryView(m_buffer->m_Data, m_buffer->m_Size); } + void* GetData() { return m_Buffer->MutableDataPointer(); } + const void* GetData() const { return m_Buffer->DataPointer(); } + size_t GetSize() const { return m_Buffer->DataBytes(); } + operator MutableMemoryView() { return GetMutableView(); } + operator MemoryView() const { return GetView(); } - MutableMemoryView GetView() { return MutableMemoryView(m_buffer->m_Data, m_buffer->m_Size); } + inline MutableMemoryView GetMutableView() { return MutableMemoryView(m_Buffer->MutableDataPointer(), m_Buffer->DataBytes()); } + inline MemoryView GetView() const { return MemoryView(m_Buffer->MutableDataPointer(), m_Buffer->DataBytes()); } /** Make an uninitialized owned buffer of the specified size. */ ZENCORE_API static UniqueBuffer Alloc(uint64_t Size); /** Make a non-owned view of the input. */ - ZENCORE_API static UniqueBuffer MakeView(void* DataPtr, uint64_t Size); + ZENCORE_API static UniqueBuffer MakeMutableView(void* DataPtr, uint64_t Size); private: - RefPtr<BufferOwner> m_buffer; + RefPtr<IoBufferCore> m_Buffer; friend class SharedBuffer; }; @@ -92,46 +55,38 @@ class SharedBuffer public: SharedBuffer() = default; ZENCORE_API explicit SharedBuffer(UniqueBuffer&&); - inline explicit SharedBuffer(BufferOwner* Owner) : m_buffer(Owner) {} - - void* GetData() - { - if (m_buffer) - { - return m_buffer->m_Data; - } - return nullptr; - } + inline explicit SharedBuffer(IoBufferCore* Owner) : m_Buffer(Owner) {} + ZENCORE_API explicit SharedBuffer(IoBuffer&& Buffer) : m_Buffer(std::move(Buffer.m_Core)) {} const void* GetData() const { - if (m_buffer) + if (m_Buffer) { - return m_buffer->m_Data; + return m_Buffer->DataPointer(); } return nullptr; } size_t GetSize() const { - if (m_buffer) + if (m_Buffer) { - return m_buffer->m_Size; + return m_Buffer->DataBytes(); } return 0; } ZENCORE_API void MakeOwned(); - bool IsOwned() const { return m_buffer && m_buffer->IsOwned(); } - inline explicit operator bool() const { return m_buffer; } - inline bool IsNull() const { return !m_buffer; } - inline void Reset() { m_buffer = nullptr; } + bool IsOwned() const { return m_Buffer && m_Buffer->IsOwned(); } + inline explicit operator bool() const { return m_Buffer; } + inline bool IsNull() const { return !m_Buffer; } + inline void Reset() { m_Buffer = nullptr; } MemoryView GetView() const { - if (m_buffer) + if (m_Buffer) { - return MemoryView(m_buffer->m_Data, m_buffer->m_Size); + return MemoryView(m_Buffer->DataPointer(), m_Buffer->DataBytes()); } else { @@ -143,7 +98,7 @@ public: SharedBuffer& operator=(UniqueBuffer&& Rhs) { - m_buffer = std::move(Rhs.m_buffer); + m_Buffer = std::move(Rhs.m_Buffer); return *this; } @@ -161,7 +116,7 @@ public: ZENCORE_API static SharedBuffer Clone(MemoryView View); private: - RefPtr<BufferOwner> m_buffer; + RefPtr<IoBufferCore> m_Buffer; }; void sharedbuffer_forcelink(); |