diff options
Diffstat (limited to 'src/common')
-rw-r--r-- | src/common/CMakeLists.txt | 2 | ||||
-rw-r--r-- | src/common/assert.h | 4 | ||||
-rw-r--r-- | src/common/make_unique_for_overwrite.h | 25 | ||||
-rw-r--r-- | src/common/scratch_buffer.h | 95 | ||||
-rw-r--r-- | src/common/settings.cpp | 1 | ||||
-rw-r--r-- | src/common/settings.h | 1 | ||||
-rw-r--r-- | src/common/thread.h | 11 |
7 files changed, 133 insertions, 6 deletions
diff --git a/src/common/CMakeLists.txt b/src/common/CMakeLists.txt index 25b22a281..eb05e46a8 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 @@ -101,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/assert.h b/src/common/assert.h index 8c927fcc0..67e7e9375 100644 --- a/src/common/assert.h +++ b/src/common/assert.h @@ -69,7 +69,7 @@ void assert_fail_impl(); #define ASSERT_OR_EXECUTE(_a_, _b_) \ do { \ ASSERT(_a_); \ - if (!(_a_)) { \ + if (!(_a_)) [[unlikely]] { \ _b_ \ } \ } while (0) @@ -78,7 +78,7 @@ void assert_fail_impl(); #define ASSERT_OR_EXECUTE_MSG(_a_, _b_, ...) \ do { \ ASSERT_MSG(_a_, __VA_ARGS__); \ - if (!(_a_)) { \ + if (!(_a_)) [[unlikely]] { \ _b_ \ } \ } while (0) 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 <memory> +#include <type_traits> + +namespace Common { + +template <class T> +requires(!std::is_array_v<T>) std::unique_ptr<T> make_unique_for_overwrite() { + return std::unique_ptr<T>(new T); +} + +template <class T> +requires std::is_unbounded_array_v<T> std::unique_ptr<T> make_unique_for_overwrite(std::size_t n) { + return std::unique_ptr<T>(new std::remove_extent_t<T>[n]); +} + +template <class T, class... Args> +requires std::is_bounded_array_v<T> +void make_unique_for_overwrite(Args&&...) = delete; + +} // namespace Common diff --git a/src/common/scratch_buffer.h b/src/common/scratch_buffer.h new file mode 100644 index 000000000..1245a5086 --- /dev/null +++ b/src/common/scratch_buffer.h @@ -0,0 +1,95 @@ +// 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 <typename T> +class ScratchBuffer { +public: + ScratchBuffer() = default; + + explicit ScratchBuffer(size_t initial_capacity) + : last_requested_size{initial_capacity}, buffer_capacity{initial_capacity}, + buffer{Common::make_unique_for_overwrite<T[]>(initial_capacity)} {} + + ~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<T[]>(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<T[]>(buffer_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]] const T& operator[](size_t i) const { + return buffer[i]; + } + + [[nodiscard]] size_t size() const noexcept { + return last_requested_size; + } + + [[nodiscard]] size_t capacity() const noexcept { + return buffer_capacity; + } + +private: + size_t last_requested_size{}; + size_t buffer_capacity{}; + std::unique_ptr<T[]> buffer{}; +}; + +} // namespace Common diff --git a/src/common/settings.cpp b/src/common/settings.cpp index d8ffe34c3..149e621f9 100644 --- a/src/common/settings.cpp +++ b/src/common/settings.cpp @@ -40,6 +40,7 @@ void LogSettings() { LOG_INFO(Config, "yuzu Configuration:"); log_setting("Controls_UseDockedMode", values.use_docked_mode.GetValue()); log_setting("System_RngSeed", values.rng_seed.GetValue().value_or(0)); + log_setting("System_DeviceName", values.device_name.GetValue()); log_setting("System_CurrentUser", values.current_user.GetValue()); log_setting("System_LanguageIndex", values.language_index.GetValue()); log_setting("System_RegionIndex", values.region_index.GetValue()); diff --git a/src/common/settings.h b/src/common/settings.h index 7ce9ea23c..6b199af93 100644 --- a/src/common/settings.h +++ b/src/common/settings.h @@ -458,6 +458,7 @@ struct Values { // System SwitchableSetting<std::optional<u32>> rng_seed{std::optional<u32>(), "rng_seed"}; + Setting<std::string> device_name{"Yuzu", "device_name"}; // Measured in seconds since epoch std::optional<s64> custom_rtc; // Set on game boot, reset on stop. Seconds difference between current time and `custom_rtc` diff --git a/src/common/thread.h b/src/common/thread.h index e17a7850f..8ae169b4e 100644 --- a/src/common/thread.h +++ b/src/common/thread.h @@ -11,6 +11,7 @@ #include <mutex> #include <thread> #include "common/common_types.h" +#include "common/polyfill_thread.h" namespace Common { @@ -69,7 +70,7 @@ public: explicit Barrier(std::size_t count_) : count(count_) {} /// Blocks until all "count" threads have called Sync() - void Sync() { + bool Sync(std::stop_token token = {}) { std::unique_lock lk{mutex}; const std::size_t current_generation = generation; @@ -77,14 +78,16 @@ public: generation++; waiting = 0; condvar.notify_all(); + return true; } else { - condvar.wait(lk, - [this, current_generation] { return current_generation != generation; }); + CondvarWait(condvar, lk, token, + [this, current_generation] { return current_generation != generation; }); + return !token.stop_requested(); } } private: - std::condition_variable condvar; + std::condition_variable_any condvar; std::mutex mutex; std::size_t count; std::size_t waiting = 0; |