diff options
author | Liam <byteslice@airmail.cc> | 2023-08-11 03:34:43 +0200 |
---|---|---|
committer | Liam <byteslice@airmail.cc> | 2023-08-15 23:47:25 +0200 |
commit | 86f6b6b7b2d930e8203114332b04a5c49a780b06 (patch) | |
tree | bf7ff58b0a36051d3c3489a40999d80357c570d0 /src/core/file_sys/fssystem/fssystem_pooled_buffer.h | |
parent | Merge pull request #11287 from liamwhite/replaced-bytes (diff) | |
download | yuzu-86f6b6b7b2d930e8203114332b04a5c49a780b06.tar yuzu-86f6b6b7b2d930e8203114332b04a5c49a780b06.tar.gz yuzu-86f6b6b7b2d930e8203114332b04a5c49a780b06.tar.bz2 yuzu-86f6b6b7b2d930e8203114332b04a5c49a780b06.tar.lz yuzu-86f6b6b7b2d930e8203114332b04a5c49a780b06.tar.xz yuzu-86f6b6b7b2d930e8203114332b04a5c49a780b06.tar.zst yuzu-86f6b6b7b2d930e8203114332b04a5c49a780b06.zip |
Diffstat (limited to '')
-rw-r--r-- | src/core/file_sys/fssystem/fssystem_pooled_buffer.h | 96 |
1 files changed, 96 insertions, 0 deletions
diff --git a/src/core/file_sys/fssystem/fssystem_pooled_buffer.h b/src/core/file_sys/fssystem/fssystem_pooled_buffer.h new file mode 100644 index 000000000..1df3153a1 --- /dev/null +++ b/src/core/file_sys/fssystem/fssystem_pooled_buffer.h @@ -0,0 +1,96 @@ +// SPDX-FileCopyrightText: Copyright 2023 yuzu Emulator Project +// SPDX-License-Identifier: GPL-2.0-or-later + +#pragma once + +#include "common/common_funcs.h" +#include "common/common_types.h" +#include "common/literals.h" +#include "core/hle/result.h" + +namespace FileSys { + +using namespace Common::Literals; + +constexpr inline size_t BufferPoolAlignment = 4_KiB; +constexpr inline size_t BufferPoolWorkSize = 320; + +class PooledBuffer { + YUZU_NON_COPYABLE(PooledBuffer); + +private: + char* m_buffer; + size_t m_size; + +private: + static size_t GetAllocatableSizeMaxCore(bool large); + +public: + static size_t GetAllocatableSizeMax() { + return GetAllocatableSizeMaxCore(false); + } + static size_t GetAllocatableParticularlyLargeSizeMax() { + return GetAllocatableSizeMaxCore(true); + } + +private: + void Swap(PooledBuffer& rhs) { + std::swap(m_buffer, rhs.m_buffer); + std::swap(m_size, rhs.m_size); + } + +public: + // Constructor/Destructor. + constexpr PooledBuffer() : m_buffer(), m_size() {} + + PooledBuffer(size_t ideal_size, size_t required_size) : m_buffer(), m_size() { + this->Allocate(ideal_size, required_size); + } + + ~PooledBuffer() { + this->Deallocate(); + } + + // Move and assignment. + explicit PooledBuffer(PooledBuffer&& rhs) : m_buffer(rhs.m_buffer), m_size(rhs.m_size) { + rhs.m_buffer = nullptr; + rhs.m_size = 0; + } + + PooledBuffer& operator=(PooledBuffer&& rhs) { + PooledBuffer(std::move(rhs)).Swap(*this); + return *this; + } + + // Allocation API. + void Allocate(size_t ideal_size, size_t required_size) { + return this->AllocateCore(ideal_size, required_size, false); + } + + void AllocateParticularlyLarge(size_t ideal_size, size_t required_size) { + return this->AllocateCore(ideal_size, required_size, true); + } + + void Shrink(size_t ideal_size); + + void Deallocate() { + // Shrink the buffer to empty. + this->Shrink(0); + ASSERT(m_buffer == nullptr); + } + + char* GetBuffer() const { + ASSERT(m_buffer != nullptr); + return m_buffer; + } + + size_t GetSize() const { + ASSERT(m_buffer != nullptr); + return m_size; + } + +private: + void AllocateCore(size_t ideal_size, size_t required_size, bool large); +}; + +} // namespace FileSys |