From 88ba5a7f22b5783e6e19059e49b632d0bd9c8da0 Mon Sep 17 00:00:00 2001 From: ameerj <52414509+ameerj@users.noreply.github.com> Date: Sun, 18 Dec 2022 17:44:34 -0500 Subject: common: add make_unique_for_overwrite --- src/common/CMakeLists.txt | 1 + src/common/make_unique_for_overwrite.h | 25 +++++++++++++++++++++++++ 2 files changed, 26 insertions(+) create mode 100644 src/common/make_unique_for_overwrite.h (limited to 'src/common') diff --git a/src/common/CMakeLists.txt b/src/common/CMakeLists.txt index 25b22a281..f558f5a58 100644 --- a/src/common/CMakeLists.txt +++ b/src/common/CMakeLists.txt @@ -78,6 +78,7 @@ add_library(common STATIC logging/types.h lz4_compression.cpp lz4_compression.h + make_unique_for_overwrite.h math_util.h memory_detect.cpp memory_detect.h diff --git a/src/common/make_unique_for_overwrite.h b/src/common/make_unique_for_overwrite.h new file mode 100644 index 000000000..c7413cf51 --- /dev/null +++ b/src/common/make_unique_for_overwrite.h @@ -0,0 +1,25 @@ +// SPDX-FileCopyrightText: Copyright 2022 yuzu Emulator Project +// SPDX-License-Identifier: GPL-2.0-or-later + +#pragma once + +#include +#include + +namespace Common { + +template +requires(!std::is_array_v) std::unique_ptr make_unique_for_overwrite() { + return std::unique_ptr(new T); +} + +template +requires std::is_unbounded_array_v std::unique_ptr make_unique_for_overwrite(std::size_t n) { + return std::unique_ptr(new std::remove_extent_t[n]); +} + +template +requires std::is_bounded_array_v +void make_unique_for_overwrite(Args&&...) = delete; + +} // namespace Common -- cgit v1.2.3 From cfc34dd41d63f45b0587d089b8ec7fc2ed27c04e Mon Sep 17 00:00:00 2001 From: ameerj <52414509+ameerj@users.noreply.github.com> Date: Sun, 18 Dec 2022 18:08:20 -0500 Subject: common: Add ScratchBuffer class This class creates a default initialized heap allocated buffer for cases where value initializing members during allocation or resize is redundant. --- src/common/CMakeLists.txt | 1 + src/common/scratch_buffer.h | 74 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 75 insertions(+) create mode 100644 src/common/scratch_buffer.h (limited to 'src/common') diff --git a/src/common/CMakeLists.txt b/src/common/CMakeLists.txt index f558f5a58..eb05e46a8 100644 --- a/src/common/CMakeLists.txt +++ b/src/common/CMakeLists.txt @@ -102,6 +102,7 @@ add_library(common STATIC ${CMAKE_CURRENT_BINARY_DIR}/scm_rev.cpp scm_rev.h scope_exit.h + scratch_buffer.h settings.cpp settings.h settings_input.cpp diff --git a/src/common/scratch_buffer.h b/src/common/scratch_buffer.h new file mode 100644 index 000000000..afbe2eee1 --- /dev/null +++ b/src/common/scratch_buffer.h @@ -0,0 +1,74 @@ +// SPDX-FileCopyrightText: Copyright 2022 yuzu Emulator Project +// SPDX-License-Identifier: GPL-2.0-or-later + +#pragma once + +#include "common/make_unique_for_overwrite.h" + +namespace Common { + +/** + * ScratchBuffer class + * This class creates a default initialized heap allocated buffer for cases such as intermediate + * buffers being copied into entirely, where value initializing members during allocation or resize + * is redundant. + */ +template +class ScratchBuffer { +public: + ScratchBuffer() = default; + + explicit ScratchBuffer(size_t initial_capacity) + : last_requested_size{initial_capacity}, capacity{initial_capacity}, + buffer{Common::make_unique_for_overwrite(initial_capacity)} {} + + ~ScratchBuffer() = default; + + /// This will only grow the buffer's capacity if size is greater than the current capacity. + void resize(size_t size) { + if (size > capacity) { + capacity = size; + buffer = Common::make_unique_for_overwrite(capacity); + } + last_requested_size = size; + } + + [[nodiscard]] T* data() noexcept { + return buffer.get(); + } + + [[nodiscard]] const T* data() const noexcept { + return buffer.get(); + } + + [[nodiscard]] T* begin() noexcept { + return data(); + } + + [[nodiscard]] const T* begin() const noexcept { + return data(); + } + + [[nodiscard]] T* end() noexcept { + return data() + last_requested_size; + } + + [[nodiscard]] const T* end() const noexcept { + return data() + last_requested_size; + } + + [[nodiscard]] T& operator[](size_t i) { + return buffer[i]; + } + + [[nodiscard]] size_t size() const noexcept { + return last_requested_size; + } + +private: + size_t last_requested_size{}; + size_t capacity{}; + std::unique_ptr buffer{}; +}; + +} // namespace Common -- cgit v1.2.3 From 64869807e2e4604f3d6334feeaf890515e9edb81 Mon Sep 17 00:00:00 2001 From: ameerj <52414509+ameerj@users.noreply.github.com> Date: Mon, 19 Dec 2022 12:30:52 -0500 Subject: tests: Add ScratchBuffer tests --- src/common/scratch_buffer.h | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) (limited to 'src/common') diff --git a/src/common/scratch_buffer.h b/src/common/scratch_buffer.h index afbe2eee1..59bb8a9ea 100644 --- a/src/common/scratch_buffer.h +++ b/src/common/scratch_buffer.h @@ -19,16 +19,16 @@ public: ScratchBuffer() = default; explicit ScratchBuffer(size_t initial_capacity) - : last_requested_size{initial_capacity}, capacity{initial_capacity}, + : last_requested_size{initial_capacity}, buffer_capacity{initial_capacity}, buffer{Common::make_unique_for_overwrite(initial_capacity)} {} ~ScratchBuffer() = default; /// This will only grow the buffer's capacity if size is greater than the current capacity. void resize(size_t size) { - if (size > capacity) { - capacity = size; - buffer = Common::make_unique_for_overwrite(capacity); + if (size > buffer_capacity) { + buffer_capacity = size; + buffer = Common::make_unique_for_overwrite(buffer_capacity); } last_requested_size = size; } @@ -65,9 +65,13 @@ public: return last_requested_size; } + [[nodiscard]] size_t capacity() const noexcept { + return buffer_capacity; + } + private: size_t last_requested_size{}; - size_t capacity{}; + size_t buffer_capacity{}; std::unique_ptr buffer{}; }; -- cgit v1.2.3 From c6590ad07b384762fd90ee8852796ec681a69286 Mon Sep 17 00:00:00 2001 From: ameerj <52414509+ameerj@users.noreply.github.com> Date: Mon, 19 Dec 2022 22:40:50 -0500 Subject: scratch_buffer: Explicitly defing resize and resize_destructive functions resize keeps previous data intact when the buffer grows resize_destructive destroys the previous data when the buffer grows --- src/common/scratch_buffer.h | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) (limited to 'src/common') diff --git a/src/common/scratch_buffer.h b/src/common/scratch_buffer.h index 59bb8a9ea..1245a5086 100644 --- a/src/common/scratch_buffer.h +++ b/src/common/scratch_buffer.h @@ -25,7 +25,20 @@ public: ~ScratchBuffer() = default; /// This will only grow the buffer's capacity if size is greater than the current capacity. + /// The previously held data will remain intact. void resize(size_t size) { + if (size > buffer_capacity) { + auto new_buffer = Common::make_unique_for_overwrite(size); + std::move(buffer.get(), buffer.get() + buffer_capacity, new_buffer.get()); + buffer = std::move(new_buffer); + buffer_capacity = size; + } + last_requested_size = size; + } + + /// This will only grow the buffer's capacity if size is greater than the current capacity. + /// The previously held data will be destroyed if a reallocation occurs. + void resize_destructive(size_t size) { if (size > buffer_capacity) { buffer_capacity = size; buffer = Common::make_unique_for_overwrite(buffer_capacity); @@ -61,6 +74,10 @@ public: return buffer[i]; } + [[nodiscard]] const T& operator[](size_t i) const { + return buffer[i]; + } + [[nodiscard]] size_t size() const noexcept { return last_requested_size; } -- cgit v1.2.3