From 43ce33b6cced1d049f1cef3a9b1fddcfad8aef7c Mon Sep 17 00:00:00 2001 From: M&M Date: Wed, 29 Jul 2020 10:25:37 -0700 Subject: logging/settings: Increase maximum log size to 100 MB and add extended logging option The extended logging option is automatically disabled on boot but can be enabled afterwards, allowing the log file to go up to 1 GB during that session. This commit also fixes a few errors that are present in the general debug menu. --- src/common/logging/backend.cpp | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) (limited to 'src/common') diff --git a/src/common/logging/backend.cpp b/src/common/logging/backend.cpp index 62cfde397..fdb2e52fa 100644 --- a/src/common/logging/backend.cpp +++ b/src/common/logging/backend.cpp @@ -23,6 +23,7 @@ #include "common/logging/text_formatter.h" #include "common/string_util.h" #include "common/threadsafe_queue.h" +#include "core/settings.h" namespace Log { @@ -152,10 +153,19 @@ FileBackend::FileBackend(const std::string& filename) void FileBackend::Write(const Entry& entry) { // prevent logs from going over the maximum size (in case its spamming and the user doesn't // know) - constexpr std::size_t MAX_BYTES_WRITTEN = 50 * 1024L * 1024L; - if (!file.IsOpen() || bytes_written > MAX_BYTES_WRITTEN) { + constexpr std::size_t MAX_BYTES_WRITTEN = 100 * 1024 * 1024; + constexpr std::size_t MAX_BYTES_WRITTEN_EXTENDED = 1024 * 1024 * 1024; + + if (!file.IsOpen()) { + return; + } + + if (Settings::values.extended_logging && bytes_written > MAX_BYTES_WRITTEN_EXTENDED) { + return; + } else if (!Settings::values.extended_logging && bytes_written > MAX_BYTES_WRITTEN) { return; } + bytes_written += file.WriteString(FormatLogMessage(entry).append(1, '\n')); if (entry.log_level >= Level::Error) { file.Flush(); -- cgit v1.2.3 From 9d665cb8dbc560e5f5492d34aeab4bf41b9353d3 Mon Sep 17 00:00:00 2001 From: lat9nq Date: Mon, 28 Sep 2020 20:28:47 -0400 Subject: CMakeLists: fix for finding zstd on linux-mingw --- src/common/CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/common') diff --git a/src/common/CMakeLists.txt b/src/common/CMakeLists.txt index 5d54516eb..ea0191c4b 100644 --- a/src/common/CMakeLists.txt +++ b/src/common/CMakeLists.txt @@ -192,4 +192,4 @@ create_target_directory_groups(common) find_package(Boost 1.71 COMPONENTS context headers REQUIRED) target_link_libraries(common PUBLIC ${Boost_LIBRARIES} fmt::fmt microprofile) -target_link_libraries(common PRIVATE lz4::lz4 zstd::zstd xbyak) +target_link_libraries(common PRIVATE lz4::lz4 zstd xbyak) -- cgit v1.2.3 From 2cbce77b925c4320d90d5845563928a838c3372c Mon Sep 17 00:00:00 2001 From: lat9nq Date: Mon, 28 Sep 2020 21:11:39 -0400 Subject: CMakeLists: use system zstd on Linux From what I understand, this tells CMake to use the system, not conan, version of zstd. Required to build on the coming MinGW Docker container. --- src/common/CMakeLists.txt | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) (limited to 'src/common') diff --git a/src/common/CMakeLists.txt b/src/common/CMakeLists.txt index ea0191c4b..0fb5d9708 100644 --- a/src/common/CMakeLists.txt +++ b/src/common/CMakeLists.txt @@ -192,4 +192,9 @@ create_target_directory_groups(common) find_package(Boost 1.71 COMPONENTS context headers REQUIRED) target_link_libraries(common PUBLIC ${Boost_LIBRARIES} fmt::fmt microprofile) -target_link_libraries(common PRIVATE lz4::lz4 zstd xbyak) +target_link_libraries(common PRIVATE lz4::lz4 xbyak) +if (MSVC) + target_link_libraries(common PRIVATE zstd::zstd) +else() + target_link_libraries(common PRIVATE zstd) +endif() -- cgit v1.2.3 From 771a9c21cc2f401cb9fd653cefcfe9da78b8f1a7 Mon Sep 17 00:00:00 2001 From: ReinUsesLisp Date: Tue, 29 Sep 2020 16:19:37 -0300 Subject: common/wall_clock: Add virtual destructors From -fsanitize=address, this code wasn't calling the proper destructor. Adding virtual destructors for each inherited class and the base class fixes this bug. While we are at it, mark the functions as final. --- src/common/wall_clock.cpp | 2 +- src/common/wall_clock.h | 2 ++ src/common/x64/native_clock.h | 2 +- 3 files changed, 4 insertions(+), 2 deletions(-) (limited to 'src/common') diff --git a/src/common/wall_clock.cpp b/src/common/wall_clock.cpp index 3afbdb898..7a20e95b7 100644 --- a/src/common/wall_clock.cpp +++ b/src/common/wall_clock.cpp @@ -15,7 +15,7 @@ namespace Common { using base_timer = std::chrono::steady_clock; using base_time_point = std::chrono::time_point; -class StandardWallClock : public WallClock { +class StandardWallClock final : public WallClock { public: StandardWallClock(u64 emulated_cpu_frequency, u64 emulated_clock_frequency) : WallClock(emulated_cpu_frequency, emulated_clock_frequency, false) { diff --git a/src/common/wall_clock.h b/src/common/wall_clock.h index 5db30083d..bc7adfbf8 100644 --- a/src/common/wall_clock.h +++ b/src/common/wall_clock.h @@ -13,6 +13,8 @@ namespace Common { class WallClock { public: + virtual ~WallClock() = default; + /// Returns current wall time in nanoseconds [[nodiscard]] virtual std::chrono::nanoseconds GetTimeNS() = 0; diff --git a/src/common/x64/native_clock.h b/src/common/x64/native_clock.h index 891a3bbfd..7c503df26 100644 --- a/src/common/x64/native_clock.h +++ b/src/common/x64/native_clock.h @@ -12,7 +12,7 @@ namespace Common { namespace X64 { -class NativeClock : public WallClock { +class NativeClock final : public WallClock { public: NativeClock(u64 emulated_cpu_frequency, u64 emulated_clock_frequency, u64 rtsc_frequency); -- cgit v1.2.3 From 39c8d18feba8eafcd43fbb55e73ae150a1947aad Mon Sep 17 00:00:00 2001 From: Lioncash Date: Tue, 13 Oct 2020 08:10:50 -0400 Subject: core/CMakeLists: Make some warnings errors Makes our error coverage a little more consistent across the board by applying it to Linux side of things as well. This also makes it more consistent with the warning settings in other libraries in the project. This also updates httplib to 0.7.9, as there are several warning cleanups made that allow us to enable several warnings as errors. --- src/common/hex_util.h | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'src/common') diff --git a/src/common/hex_util.h b/src/common/hex_util.h index 120f1a5e6..a8d414fb8 100644 --- a/src/common/hex_util.h +++ b/src/common/hex_util.h @@ -16,14 +16,14 @@ namespace Common { [[nodiscard]] constexpr u8 ToHexNibble(char c) { if (c >= 65 && c <= 70) { - return c - 55; + return static_cast(c - 55); } if (c >= 97 && c <= 102) { - return c - 87; + return static_cast(c - 87); } - return c - 48; + return static_cast(c - 48); } [[nodiscard]] std::vector HexStringToVector(std::string_view str, bool little_endian); @@ -33,11 +33,11 @@ template std::array out{}; if constexpr (le) { for (std::size_t i = 2 * Size - 2; i <= 2 * Size; i -= 2) { - out[i / 2] = (ToHexNibble(str[i]) << 4) | ToHexNibble(str[i + 1]); + out[i / 2] = static_cast((ToHexNibble(str[i]) << 4) | ToHexNibble(str[i + 1])); } } else { for (std::size_t i = 0; i < 2 * Size; i += 2) { - out[i / 2] = (ToHexNibble(str[i]) << 4) | ToHexNibble(str[i + 1]); + out[i / 2] = static_cast((ToHexNibble(str[i]) << 4) | ToHexNibble(str[i + 1])); } } return out; -- cgit v1.2.3 From 046c0c91a3ed665531f20955e7cfb86fe5b73213 Mon Sep 17 00:00:00 2001 From: Lioncash Date: Wed, 14 Oct 2020 02:51:14 -0400 Subject: input_common/CMakeLists: Make some warnings errors Makes the input_common code warnings consistent with the rest of the codebase. --- src/common/math_util.h | 4 +-- src/common/vector_math.h | 75 ++++++++++++++++++++++++++++++++++++++++++------ 2 files changed, 68 insertions(+), 11 deletions(-) (limited to 'src/common') diff --git a/src/common/math_util.h b/src/common/math_util.h index b35ad8507..7cec80d57 100644 --- a/src/common/math_util.h +++ b/src/common/math_util.h @@ -20,8 +20,8 @@ struct Rectangle { constexpr Rectangle() = default; - constexpr Rectangle(T left, T top, T right, T bottom) - : left(left), top(top), right(right), bottom(bottom) {} + constexpr Rectangle(T left_, T top_, T right_, T bottom_) + : left(left_), top(top_), right(right_), bottom(bottom_) {} [[nodiscard]] T GetWidth() const { if constexpr (std::is_floating_point_v) { diff --git a/src/common/vector_math.h b/src/common/vector_math.h index 2a0fcf541..22dba3c2d 100644 --- a/src/common/vector_math.h +++ b/src/common/vector_math.h @@ -87,7 +87,13 @@ public: template [[nodiscard]] constexpr Vec2 operator*(const V& f) const { - return {x * f, y * f}; + using TV = decltype(T{} * V{}); + using C = std::common_type_t; + + return { + static_cast(static_cast(x) * static_cast(f)), + static_cast(static_cast(y) * static_cast(f)), + }; } template @@ -98,7 +104,13 @@ public: template [[nodiscard]] constexpr Vec2 operator/(const V& f) const { - return {x / f, y / f}; + using TV = decltype(T{} / V{}); + using C = std::common_type_t; + + return { + static_cast(static_cast(x) / static_cast(f)), + static_cast(static_cast(y) / static_cast(f)), + }; } template @@ -168,7 +180,10 @@ public: template [[nodiscard]] constexpr Vec2 operator*(const V& f, const Vec2& vec) { - return Vec2(f * vec.x, f * vec.y); + using C = std::common_type_t; + + return Vec2(static_cast(static_cast(f) * static_cast(vec.x)), + static_cast(static_cast(f) * static_cast(vec.y))); } using Vec2f = Vec2; @@ -237,7 +252,14 @@ public: template [[nodiscard]] constexpr Vec3 operator*(const V& f) const { - return {x * f, y * f, z * f}; + using TV = decltype(T{} * V{}); + using C = std::common_type_t; + + return { + static_cast(static_cast(x) * static_cast(f)), + static_cast(static_cast(y) * static_cast(f)), + static_cast(static_cast(z) * static_cast(f)), + }; } template @@ -247,7 +269,14 @@ public: } template [[nodiscard]] constexpr Vec3 operator/(const V& f) const { - return {x / f, y / f, z / f}; + using TV = decltype(T{} / V{}); + using C = std::common_type_t; + + return { + static_cast(static_cast(x) / static_cast(f)), + static_cast(static_cast(y) / static_cast(f)), + static_cast(static_cast(z) / static_cast(f)), + }; } template @@ -367,7 +396,11 @@ public: template [[nodiscard]] constexpr Vec3 operator*(const V& f, const Vec3& vec) { - return Vec3(f * vec.x, f * vec.y, f * vec.z); + using C = std::common_type_t; + + return Vec3(static_cast(static_cast(f) * static_cast(vec.x)), + static_cast(static_cast(f) * static_cast(vec.y)), + static_cast(static_cast(f) * static_cast(vec.z))); } template <> @@ -446,7 +479,15 @@ public: template [[nodiscard]] constexpr Vec4 operator*(const V& f) const { - return {x * f, y * f, z * f, w * f}; + using TV = decltype(T{} * V{}); + using C = std::common_type_t; + + return { + static_cast(static_cast(x) * static_cast(f)), + static_cast(static_cast(y) * static_cast(f)), + static_cast(static_cast(z) * static_cast(f)), + static_cast(static_cast(w) * static_cast(f)), + }; } template @@ -457,7 +498,15 @@ public: template [[nodiscard]] constexpr Vec4 operator/(const V& f) const { - return {x / f, y / f, z / f, w / f}; + using TV = decltype(T{} / V{}); + using C = std::common_type_t; + + return { + static_cast(static_cast(x) / static_cast(f)), + static_cast(static_cast(y) / static_cast(f)), + static_cast(static_cast(z) / static_cast(f)), + static_cast(static_cast(w) / static_cast(f)), + }; } template @@ -582,7 +631,15 @@ public: template [[nodiscard]] constexpr Vec4 operator*(const V& f, const Vec4& vec) { - return {f * vec.x, f * vec.y, f * vec.z, f * vec.w}; + using TV = decltype(V{} * T{}); + using C = std::common_type_t; + + return { + static_cast(static_cast(f) * static_cast(vec.x)), + static_cast(static_cast(f) * static_cast(vec.y)), + static_cast(static_cast(f) * static_cast(vec.z)), + static_cast(static_cast(f) * static_cast(vec.w)), + }; } using Vec4f = Vec4; -- cgit v1.2.3 From be1954e04cb5a0c3a526f78ed5490a5e65310280 Mon Sep 17 00:00:00 2001 From: Lioncash Date: Thu, 15 Oct 2020 14:49:45 -0400 Subject: core: Fix clang build Recent changes to the build system that made more warnings be flagged as errors caused building via clang to break. Fixes #4795 --- src/common/fiber.h | 4 ++-- src/common/file_util.h | 3 ++- src/common/math_util.h | 4 ++-- src/common/multi_level_queue.h | 2 +- src/common/spin_lock.h | 8 ++++++++ src/common/swap.h | 10 +++++----- src/common/thread_queue_list.h | 4 ++-- 7 files changed, 22 insertions(+), 13 deletions(-) (limited to 'src/common') diff --git a/src/common/fiber.h b/src/common/fiber.h index 89dde5e36..bc1db1582 100644 --- a/src/common/fiber.h +++ b/src/common/fiber.h @@ -41,8 +41,8 @@ public: Fiber(const Fiber&) = delete; Fiber& operator=(const Fiber&) = delete; - Fiber(Fiber&&) = default; - Fiber& operator=(Fiber&&) = default; + Fiber(Fiber&&) = delete; + Fiber& operator=(Fiber&&) = delete; /// Yields control from Fiber 'from' to Fiber 'to' /// Fiber 'from' must be the currently running fiber. diff --git a/src/common/file_util.h b/src/common/file_util.h index 8b587320f..508b7a10a 100644 --- a/src/common/file_util.h +++ b/src/common/file_util.h @@ -189,7 +189,8 @@ template return {}; } last = std::min(last, vector.size()); - return std::vector(vector.begin() + first, vector.begin() + first + last); + return std::vector(vector.begin() + static_cast(first), + vector.begin() + static_cast(first + last)); } enum class DirectorySeparator { diff --git a/src/common/math_util.h b/src/common/math_util.h index b35ad8507..661c056ca 100644 --- a/src/common/math_util.h +++ b/src/common/math_util.h @@ -27,7 +27,7 @@ struct Rectangle { if constexpr (std::is_floating_point_v) { return std::abs(right - left); } else { - return std::abs(static_cast>(right - left)); + return static_cast(std::abs(static_cast>(right - left))); } } @@ -35,7 +35,7 @@ struct Rectangle { if constexpr (std::is_floating_point_v) { return std::abs(bottom - top); } else { - return std::abs(static_cast>(bottom - top)); + return static_cast(std::abs(static_cast>(bottom - top))); } } diff --git a/src/common/multi_level_queue.h b/src/common/multi_level_queue.h index 4b305bf40..71613f18b 100644 --- a/src/common/multi_level_queue.h +++ b/src/common/multi_level_queue.h @@ -320,7 +320,7 @@ private: } const auto begin_range = list.begin(); - const auto end_range = std::next(begin_range, shift); + const auto end_range = std::next(begin_range, static_cast(shift)); list.splice(list.end(), list, begin_range, end_range); } diff --git a/src/common/spin_lock.h b/src/common/spin_lock.h index 4f946a258..06ac2f5bb 100644 --- a/src/common/spin_lock.h +++ b/src/common/spin_lock.h @@ -15,6 +15,14 @@ namespace Common { */ class SpinLock { public: + SpinLock() = default; + + SpinLock(const SpinLock&) = delete; + SpinLock& operator=(const SpinLock&) = delete; + + SpinLock(SpinLock&&) = delete; + SpinLock& operator=(SpinLock&&) = delete; + void lock(); void unlock(); [[nodiscard]] bool try_lock(); diff --git a/src/common/swap.h b/src/common/swap.h index 7665942a2..8c68c1f26 100644 --- a/src/common/swap.h +++ b/src/common/swap.h @@ -504,35 +504,35 @@ bool operator==(const S& p, const swap_struct_t v) { template struct swap_64_t { static T swap(T x) { - return static_cast(Common::swap64(x)); + return static_cast(Common::swap64(static_cast(x))); } }; template struct swap_32_t { static T swap(T x) { - return static_cast(Common::swap32(x)); + return static_cast(Common::swap32(static_cast(x))); } }; template struct swap_16_t { static T swap(T x) { - return static_cast(Common::swap16(x)); + return static_cast(Common::swap16(static_cast(x))); } }; template struct swap_float_t { static T swap(T x) { - return static_cast(Common::swapf(x)); + return static_cast(Common::swapf(static_cast(x))); } }; template struct swap_double_t { static T swap(T x) { - return static_cast(Common::swapd(x)); + return static_cast(Common::swapd(static_cast(x))); } }; diff --git a/src/common/thread_queue_list.h b/src/common/thread_queue_list.h index def9e5d8d..69c9193da 100644 --- a/src/common/thread_queue_list.h +++ b/src/common/thread_queue_list.h @@ -33,7 +33,7 @@ struct ThreadQueueList { } } - return -1; + return static_cast(-1); } [[nodiscard]] T get_first() const { @@ -156,7 +156,7 @@ private: void link(Priority priority) { Queue* cur = &queues[priority]; - for (int i = priority - 1; i >= 0; --i) { + for (auto i = static_cast(priority - 1); i >= 0; --i) { if (queues[i].next_nonempty != UnlinkedTag()) { cur->next_nonempty = queues[i].next_nonempty; queues[i].next_nonempty = cur; -- cgit v1.2.3 From 3d592972dc3fd61cc88771b889eff237e4e03e0f Mon Sep 17 00:00:00 2001 From: bunnei Date: Tue, 20 Oct 2020 19:07:39 -0700 Subject: Revert "core: Fix clang build" --- src/common/fiber.h | 4 ++-- src/common/file_util.h | 3 +-- src/common/math_util.h | 4 ++-- src/common/multi_level_queue.h | 2 +- src/common/spin_lock.h | 8 -------- src/common/swap.h | 10 +++++----- src/common/thread_queue_list.h | 4 ++-- 7 files changed, 13 insertions(+), 22 deletions(-) (limited to 'src/common') diff --git a/src/common/fiber.h b/src/common/fiber.h index bc1db1582..89dde5e36 100644 --- a/src/common/fiber.h +++ b/src/common/fiber.h @@ -41,8 +41,8 @@ public: Fiber(const Fiber&) = delete; Fiber& operator=(const Fiber&) = delete; - Fiber(Fiber&&) = delete; - Fiber& operator=(Fiber&&) = delete; + Fiber(Fiber&&) = default; + Fiber& operator=(Fiber&&) = default; /// Yields control from Fiber 'from' to Fiber 'to' /// Fiber 'from' must be the currently running fiber. diff --git a/src/common/file_util.h b/src/common/file_util.h index 508b7a10a..8b587320f 100644 --- a/src/common/file_util.h +++ b/src/common/file_util.h @@ -189,8 +189,7 @@ template return {}; } last = std::min(last, vector.size()); - return std::vector(vector.begin() + static_cast(first), - vector.begin() + static_cast(first + last)); + return std::vector(vector.begin() + first, vector.begin() + first + last); } enum class DirectorySeparator { diff --git a/src/common/math_util.h b/src/common/math_util.h index 4c38d8040..7cec80d57 100644 --- a/src/common/math_util.h +++ b/src/common/math_util.h @@ -27,7 +27,7 @@ struct Rectangle { if constexpr (std::is_floating_point_v) { return std::abs(right - left); } else { - return static_cast(std::abs(static_cast>(right - left))); + return std::abs(static_cast>(right - left)); } } @@ -35,7 +35,7 @@ struct Rectangle { if constexpr (std::is_floating_point_v) { return std::abs(bottom - top); } else { - return static_cast(std::abs(static_cast>(bottom - top))); + return std::abs(static_cast>(bottom - top)); } } diff --git a/src/common/multi_level_queue.h b/src/common/multi_level_queue.h index 71613f18b..4b305bf40 100644 --- a/src/common/multi_level_queue.h +++ b/src/common/multi_level_queue.h @@ -320,7 +320,7 @@ private: } const auto begin_range = list.begin(); - const auto end_range = std::next(begin_range, static_cast(shift)); + const auto end_range = std::next(begin_range, shift); list.splice(list.end(), list, begin_range, end_range); } diff --git a/src/common/spin_lock.h b/src/common/spin_lock.h index 06ac2f5bb..4f946a258 100644 --- a/src/common/spin_lock.h +++ b/src/common/spin_lock.h @@ -15,14 +15,6 @@ namespace Common { */ class SpinLock { public: - SpinLock() = default; - - SpinLock(const SpinLock&) = delete; - SpinLock& operator=(const SpinLock&) = delete; - - SpinLock(SpinLock&&) = delete; - SpinLock& operator=(SpinLock&&) = delete; - void lock(); void unlock(); [[nodiscard]] bool try_lock(); diff --git a/src/common/swap.h b/src/common/swap.h index 8c68c1f26..7665942a2 100644 --- a/src/common/swap.h +++ b/src/common/swap.h @@ -504,35 +504,35 @@ bool operator==(const S& p, const swap_struct_t v) { template struct swap_64_t { static T swap(T x) { - return static_cast(Common::swap64(static_cast(x))); + return static_cast(Common::swap64(x)); } }; template struct swap_32_t { static T swap(T x) { - return static_cast(Common::swap32(static_cast(x))); + return static_cast(Common::swap32(x)); } }; template struct swap_16_t { static T swap(T x) { - return static_cast(Common::swap16(static_cast(x))); + return static_cast(Common::swap16(x)); } }; template struct swap_float_t { static T swap(T x) { - return static_cast(Common::swapf(static_cast(x))); + return static_cast(Common::swapf(x)); } }; template struct swap_double_t { static T swap(T x) { - return static_cast(Common::swapd(static_cast(x))); + return static_cast(Common::swapd(x)); } }; diff --git a/src/common/thread_queue_list.h b/src/common/thread_queue_list.h index 69c9193da..def9e5d8d 100644 --- a/src/common/thread_queue_list.h +++ b/src/common/thread_queue_list.h @@ -33,7 +33,7 @@ struct ThreadQueueList { } } - return static_cast(-1); + return -1; } [[nodiscard]] T get_first() const { @@ -156,7 +156,7 @@ private: void link(Priority priority) { Queue* cur = &queues[priority]; - for (auto i = static_cast(priority - 1); i >= 0; --i) { + for (int i = priority - 1; i >= 0; --i) { if (queues[i].next_nonempty != UnlinkedTag()) { cur->next_nonempty = queues[i].next_nonempty; queues[i].next_nonempty = cur; -- cgit v1.2.3 From ea20b5c970cb261df93743803a227fafd5403d02 Mon Sep 17 00:00:00 2001 From: Lioncash Date: Wed, 21 Oct 2020 22:14:21 -0400 Subject: core: Fix clang build pt.3 Should finally resolve building with clang. --- src/common/math_util.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src/common') diff --git a/src/common/math_util.h b/src/common/math_util.h index 7cec80d57..4c38d8040 100644 --- a/src/common/math_util.h +++ b/src/common/math_util.h @@ -27,7 +27,7 @@ struct Rectangle { if constexpr (std::is_floating_point_v) { return std::abs(right - left); } else { - return std::abs(static_cast>(right - left)); + return static_cast(std::abs(static_cast>(right - left))); } } @@ -35,7 +35,7 @@ struct Rectangle { if constexpr (std::is_floating_point_v) { return std::abs(bottom - top); } else { - return std::abs(static_cast>(bottom - top)); + return static_cast(std::abs(static_cast>(bottom - top))); } } -- cgit v1.2.3 From eb67a45ca82bc01ac843c853fd3c17f2a90e0250 Mon Sep 17 00:00:00 2001 From: ameerj Date: Mon, 26 Oct 2020 23:07:36 -0400 Subject: video_core: NVDEC Implementation This commit aims to implement the NVDEC (Nvidia Decoder) functionality, with video frame decoding being handled by the FFmpeg library. The process begins with Ioctl commands being sent to the NVDEC and VIC (Video Image Composer) emulated devices. These allocate the necessary GPU buffers for the frame data, along with providing information on the incoming video data. A Submit command then signals the GPU to process and decode the frame data. To decode the frame, the respective codec's header must be manually composed from the information provided by NVDEC, then sent with the raw frame data to the ffmpeg library. Currently, H264 and VP9 are supported, with VP9 having some minor artifacting issues related mainly to the reference frame composition in its uncompressed header. Async GPU is not properly implemented at the moment. Co-Authored-By: David <25727384+ogniK5377@users.noreply.github.com> --- src/common/CMakeLists.txt | 2 ++ src/common/stream.cpp | 47 ++++++++++++++++++++++++++++++++++++++++++++ src/common/stream.h | 50 +++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 99 insertions(+) create mode 100644 src/common/stream.cpp create mode 100644 src/common/stream.h (limited to 'src/common') diff --git a/src/common/CMakeLists.txt b/src/common/CMakeLists.txt index 0fb5d9708..e50ab2922 100644 --- a/src/common/CMakeLists.txt +++ b/src/common/CMakeLists.txt @@ -150,6 +150,8 @@ add_library(common STATIC scope_exit.h spin_lock.cpp spin_lock.h + stream.cpp + stream.h string_util.cpp string_util.h swap.h diff --git a/src/common/stream.cpp b/src/common/stream.cpp new file mode 100644 index 000000000..bf0496c26 --- /dev/null +++ b/src/common/stream.cpp @@ -0,0 +1,47 @@ +// Copyright 2020 yuzu Emulator Project +// Licensed under GPLv2 or any later version +// Refer to the license.txt file included. + +#include +#include "common/common_types.h" +#include "common/stream.h" + +namespace Common { + +Stream::Stream() = default; +Stream::~Stream() = default; + +void Stream::Seek(s32 offset, SeekOrigin origin) { + if (origin == SeekOrigin::SetOrigin) { + if (offset < 0) { + position = 0; + } else if (position >= buffer.size()) { + position = buffer.size(); + } else { + position = offset; + } + } else if (origin == SeekOrigin::FromCurrentPos) { + Seek(static_cast(position) + offset, SeekOrigin::SetOrigin); + } else if (origin == SeekOrigin::FromEnd) { + Seek(static_cast(buffer.size()) - offset, SeekOrigin::SetOrigin); + } +} + +u8 Stream::ReadByte() { + if (position < buffer.size()) { + return buffer[position++]; + } else { + throw std::out_of_range("Attempting to read a byte not within the buffer range"); + } +} + +void Stream::WriteByte(u8 byte) { + if (position == buffer.size()) { + buffer.push_back(byte); + position++; + } else { + buffer.insert(buffer.begin() + position, byte); + } +} + +} // namespace Common diff --git a/src/common/stream.h b/src/common/stream.h new file mode 100644 index 000000000..2585c16af --- /dev/null +++ b/src/common/stream.h @@ -0,0 +1,50 @@ +// Copyright 2020 yuzu Emulator Project +// Licensed under GPLv2 or any later version +// Refer to the license.txt file included. + +#pragma once + +#include +#include "common/common_types.h" + +namespace Common { + +enum class SeekOrigin { + SetOrigin, + FromCurrentPos, + FromEnd, +}; + +class Stream { +public: + /// Stream creates a bitstream and provides common functionality on the stream. + explicit Stream(); + ~Stream(); + + /// Reposition bitstream "cursor" to the specified offset from origin + void Seek(s32 offset, SeekOrigin origin); + + /// Reads next byte in the stream buffer and increments position + u8 ReadByte(); + + /// Writes byte at current position + void WriteByte(u8 byte); + + std::size_t GetPosition() const { + return position; + } + + std::vector& GetBuffer() { + return buffer; + } + + const std::vector& GetBuffer() const { + return buffer; + } + +private: + std::vector buffer; + std::size_t position{0}; +}; + +} // namespace Common -- cgit v1.2.3 From cdb2480d391b3c57fa014c2ab65824f7c9e378fc Mon Sep 17 00:00:00 2001 From: ReinUsesLisp Date: Wed, 28 Oct 2020 01:42:41 -0300 Subject: common/fiber: Take shared_ptr by copy in YieldTo YieldTo does not intend to modify the passed shared_ptrs. Pass it by copy to keep a reference count while this function executes. --- src/common/fiber.cpp | 4 ++-- src/common/fiber.h | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) (limited to 'src/common') diff --git a/src/common/fiber.cpp b/src/common/fiber.cpp index 1c1d09ccb..e186ed880 100644 --- a/src/common/fiber.cpp +++ b/src/common/fiber.cpp @@ -91,7 +91,7 @@ void Fiber::Rewind() { SwitchToFiber(impl->rewind_handle); } -void Fiber::YieldTo(std::shared_ptr& from, std::shared_ptr& to) { +void Fiber::YieldTo(std::shared_ptr from, std::shared_ptr to) { ASSERT_MSG(from != nullptr, "Yielding fiber is null!"); ASSERT_MSG(to != nullptr, "Next fiber is null!"); to->guard.lock(); @@ -199,7 +199,7 @@ void Fiber::Rewind() { boost::context::detail::jump_fcontext(impl->rewind_context, this); } -void Fiber::YieldTo(std::shared_ptr& from, std::shared_ptr& to) { +void Fiber::YieldTo(std::shared_ptr from, std::shared_ptr to) { ASSERT_MSG(from != nullptr, "Yielding fiber is null!"); ASSERT_MSG(to != nullptr, "Next fiber is null!"); to->guard.lock(); diff --git a/src/common/fiber.h b/src/common/fiber.h index 89dde5e36..cefd61df9 100644 --- a/src/common/fiber.h +++ b/src/common/fiber.h @@ -46,7 +46,7 @@ public: /// Yields control from Fiber 'from' to Fiber 'to' /// Fiber 'from' must be the currently running fiber. - static void YieldTo(std::shared_ptr& from, std::shared_ptr& to); + static void YieldTo(std::shared_ptr from, std::shared_ptr to); [[nodiscard]] static std::shared_ptr ThreadToFiber(); void SetRewindPoint(std::function&& rewind_func, void* start_parameter); -- cgit v1.2.3 From 8049b8beb625686a4edd9fd1bdf133496e6f462c Mon Sep 17 00:00:00 2001 From: Lioncash Date: Thu, 29 Oct 2020 22:55:56 -0400 Subject: common/stream: Be explicit with copy and move operators --- src/common/stream.h | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) (limited to 'src/common') diff --git a/src/common/stream.h b/src/common/stream.h index 2585c16af..0e40692de 100644 --- a/src/common/stream.h +++ b/src/common/stream.h @@ -21,6 +21,12 @@ public: explicit Stream(); ~Stream(); + Stream(const Stream&) = delete; + Stream& operator=(const Stream&) = delete; + + Stream(Stream&&) = default; + Stream& operator=(Stream&&) = default; + /// Reposition bitstream "cursor" to the specified offset from origin void Seek(s32 offset, SeekOrigin origin); @@ -30,15 +36,15 @@ public: /// Writes byte at current position void WriteByte(u8 byte); - std::size_t GetPosition() const { + [[nodiscard]] std::size_t GetPosition() const { return position; } - std::vector& GetBuffer() { + [[nodiscard]] std::vector& GetBuffer() { return buffer; } - const std::vector& GetBuffer() const { + [[nodiscard]] const std::vector& GetBuffer() const { return buffer; } -- cgit v1.2.3 From 26547d3e3be403047445fd39e671d7ef3b88dabb Mon Sep 17 00:00:00 2001 From: Lioncash Date: Thu, 29 Oct 2020 23:30:42 -0400 Subject: General: Make ignoring a discarded return value an error Allows our CI to catch more potential bugs. This also removes the [[nodiscard]] attribute of IOFile's Open member function. There are cases where a file may want to be opened, but have the status of it checked at a later time. --- src/common/file_util.h | 2 +- src/common/misc.cpp | 15 +++++++++++---- 2 files changed, 12 insertions(+), 5 deletions(-) (limited to 'src/common') diff --git a/src/common/file_util.h b/src/common/file_util.h index 8b587320f..840cde2a6 100644 --- a/src/common/file_util.h +++ b/src/common/file_util.h @@ -232,7 +232,7 @@ public: void Swap(IOFile& other) noexcept; - [[nodiscard]] bool Open(const std::string& filename, const char openmode[], int flags = 0); + bool Open(const std::string& filename, const char openmode[], int flags = 0); bool Close(); template diff --git a/src/common/misc.cpp b/src/common/misc.cpp index 68cb86cd1..1d5393597 100644 --- a/src/common/misc.cpp +++ b/src/common/misc.cpp @@ -16,16 +16,23 @@ // Call directly after the command or use the error num. // This function might change the error code. std::string GetLastErrorMsg() { - static const std::size_t buff_size = 255; + static constexpr std::size_t buff_size = 255; char err_str[buff_size]; #ifdef _WIN32 FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM, nullptr, GetLastError(), MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), err_str, buff_size, nullptr); + return std::string(err_str, buff_size); +#elif defined(__GLIBC__) && (_GNU_SOURCE || (_POSIX_C_SOURCE < 200112L && _XOPEN_SOURCE < 600)) + // Thread safe (GNU-specific) + const char* str = strerror_r(errno, err_str, buff_size); + return std::string(str); #else // Thread safe (XSI-compliant) - strerror_r(errno, err_str, buff_size); + const int success = strerror_r(errno, err_str, buff_size); + if (success != 0) { + return {}; + } + return std::string(err_str); #endif - - return std::string(err_str, buff_size); } -- cgit v1.2.3 From 4a4b685a0420b0ac7c026cd2370c23d54f469976 Mon Sep 17 00:00:00 2001 From: Lioncash Date: Fri, 30 Oct 2020 15:02:02 -0400 Subject: common: Enable warnings as errors Cleans up common so that we can enable warnings as errors. --- src/common/CMakeLists.txt | 16 ++++++++++++++++ src/common/fiber.cpp | 8 ++++---- src/common/fiber.h | 2 +- src/common/file_util.cpp | 31 ++++++++++++++++++------------- src/common/logging/backend.cpp | 2 -- src/common/string_util.cpp | 5 +++-- src/common/timer.cpp | 12 +++++------- src/common/wall_clock.cpp | 2 +- src/common/x64/native_clock.h | 2 +- 9 files changed, 49 insertions(+), 31 deletions(-) (limited to 'src/common') diff --git a/src/common/CMakeLists.txt b/src/common/CMakeLists.txt index e50ab2922..207c7a0a6 100644 --- a/src/common/CMakeLists.txt +++ b/src/common/CMakeLists.txt @@ -190,6 +190,22 @@ if(ARCHITECTURE_x86_64) ) endif() +if (MSVC) + target_compile_definitions(common PRIVATE + # The standard library doesn't provide any replacement for codecvt yet + # so we can disable this deprecation warning for the time being. + _SILENCE_CXX17_CODECVT_HEADER_DEPRECATION_WARNING + ) + target_compile_options(common PRIVATE + /W4 + /WX + ) +else() + target_compile_options(common PRIVATE + -Werror + ) +endif() + create_target_directory_groups(common) find_package(Boost 1.71 COMPONENTS context headers REQUIRED) diff --git a/src/common/fiber.cpp b/src/common/fiber.cpp index e186ed880..b209f52fc 100644 --- a/src/common/fiber.cpp +++ b/src/common/fiber.cpp @@ -79,9 +79,9 @@ void Fiber::Exit() { released = true; } -void Fiber::SetRewindPoint(std::function&& rewind_func, void* start_parameter) { +void Fiber::SetRewindPoint(std::function&& rewind_func, void* rewind_param) { rewind_point = std::move(rewind_func); - rewind_parameter = start_parameter; + rewind_parameter = rewind_param; } void Fiber::Rewind() { @@ -161,9 +161,9 @@ Fiber::Fiber(std::function&& entry_point_func, void* start_paramete boost::context::detail::make_fcontext(stack_base, impl->stack.size(), FiberStartFunc); } -void Fiber::SetRewindPoint(std::function&& rewind_func, void* start_parameter) { +void Fiber::SetRewindPoint(std::function&& rewind_func, void* rewind_param) { rewind_point = std::move(rewind_func); - rewind_parameter = start_parameter; + rewind_parameter = rewind_param; } Fiber::Fiber() : impl{std::make_unique()} {} diff --git a/src/common/fiber.h b/src/common/fiber.h index cefd61df9..699286ee2 100644 --- a/src/common/fiber.h +++ b/src/common/fiber.h @@ -49,7 +49,7 @@ public: static void YieldTo(std::shared_ptr from, std::shared_ptr to); [[nodiscard]] static std::shared_ptr ThreadToFiber(); - void SetRewindPoint(std::function&& rewind_func, void* start_parameter); + void SetRewindPoint(std::function&& rewind_func, void* rewind_param); void Rewind(); diff --git a/src/common/file_util.cpp b/src/common/file_util.cpp index 16c3713e0..18fbfa25b 100644 --- a/src/common/file_util.cpp +++ b/src/common/file_util.cpp @@ -472,13 +472,14 @@ u64 ScanDirectoryTree(const std::string& directory, FSTEntry& parent_entry, } bool DeleteDirRecursively(const std::string& directory, unsigned int recursion) { - const auto callback = [recursion](u64* num_entries_out, const std::string& directory, - const std::string& virtual_name) -> bool { - std::string new_path = directory + DIR_SEP_CHR + virtual_name; + const auto callback = [recursion](u64*, const std::string& directory, + const std::string& virtual_name) { + const std::string new_path = directory + DIR_SEP_CHR + virtual_name; if (IsDirectory(new_path)) { - if (recursion == 0) + if (recursion == 0) { return false; + } return DeleteDirRecursively(new_path, recursion - 1); } return Delete(new_path); @@ -492,7 +493,8 @@ bool DeleteDirRecursively(const std::string& directory, unsigned int recursion) return true; } -void CopyDir(const std::string& source_path, const std::string& dest_path) { +void CopyDir([[maybe_unused]] const std::string& source_path, + [[maybe_unused]] const std::string& dest_path) { #ifndef _WIN32 if (source_path == dest_path) { return; @@ -553,7 +555,7 @@ std::optional GetCurrentDir() { std::string strDir = dir; #endif free(dir); - return std::move(strDir); + return strDir; } bool SetCurrentDir(const std::string& directory) { @@ -772,21 +774,23 @@ std::size_t ReadFileToString(bool text_file, const std::string& filename, std::s void SplitFilename83(const std::string& filename, std::array& short_name, std::array& extension) { - const std::string forbidden_characters = ".\"/\\[]:;=, "; + static constexpr std::string_view forbidden_characters = ".\"/\\[]:;=, "; // On a FAT32 partition, 8.3 names are stored as a 11 bytes array, filled with spaces. short_name = {{' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '\0'}}; extension = {{' ', ' ', ' ', '\0'}}; - std::string::size_type point = filename.rfind('.'); - if (point == filename.size() - 1) + auto point = filename.rfind('.'); + if (point == filename.size() - 1) { point = filename.rfind('.', point); + } // Get short name. int j = 0; for (char letter : filename.substr(0, point)) { - if (forbidden_characters.find(letter, 0) != std::string::npos) + if (forbidden_characters.find(letter, 0) != std::string::npos) { continue; + } if (j == 8) { // TODO(Link Mauve): also do that for filenames containing a space. // TODO(Link Mauve): handle multiple files having the same short name. @@ -794,14 +798,15 @@ void SplitFilename83(const std::string& filename, std::array& short_nam short_name[7] = '1'; break; } - short_name[j++] = toupper(letter); + short_name[j++] = static_cast(std::toupper(letter)); } // Get extension. if (point != std::string::npos) { j = 0; - for (char letter : filename.substr(point + 1, 3)) - extension[j++] = toupper(letter); + for (char letter : filename.substr(point + 1, 3)) { + extension[j++] = static_cast(std::toupper(letter)); + } } } diff --git a/src/common/logging/backend.cpp b/src/common/logging/backend.cpp index 62cfde397..90dfa22ca 100644 --- a/src/common/logging/backend.cpp +++ b/src/common/logging/backend.cpp @@ -274,7 +274,6 @@ const char* GetLogClassName(Class log_class) { case Class::Count: break; } - UNREACHABLE(); return "Invalid"; } @@ -293,7 +292,6 @@ const char* GetLevelName(Level log_level) { break; } #undef LVL - UNREACHABLE(); return "Invalid"; } diff --git a/src/common/string_util.cpp b/src/common/string_util.cpp index 84883a1d3..4cba2aaa4 100644 --- a/src/common/string_util.cpp +++ b/src/common/string_util.cpp @@ -8,6 +8,7 @@ #include #include #include + #include "common/common_paths.h" #include "common/logging/log.h" #include "common/string_util.h" @@ -21,14 +22,14 @@ namespace Common { /// Make a string lowercase std::string ToLower(std::string str) { std::transform(str.begin(), str.end(), str.begin(), - [](unsigned char c) { return std::tolower(c); }); + [](unsigned char c) { return static_cast(std::tolower(c)); }); return str; } /// Make a string uppercase std::string ToUpper(std::string str) { std::transform(str.begin(), str.end(), str.begin(), - [](unsigned char c) { return std::toupper(c); }); + [](unsigned char c) { return static_cast(std::toupper(c)); }); return str; } diff --git a/src/common/timer.cpp b/src/common/timer.cpp index 2dc15e434..d17dc2a50 100644 --- a/src/common/timer.cpp +++ b/src/common/timer.cpp @@ -142,20 +142,18 @@ std::string Timer::GetTimeFormatted() { // ---------------- double Timer::GetDoubleTime() { // Get continuous timestamp - u64 TmpSeconds = static_cast(Common::Timer::GetTimeSinceJan1970().count()); - double ms = static_cast(GetTimeMs().count()) % 1000; + auto tmp_seconds = static_cast(GetTimeSinceJan1970().count()); + const auto ms = static_cast(static_cast(GetTimeMs().count()) % 1000); // Remove a few years. We only really want enough seconds to make // sure that we are detecting actual actions, perhaps 60 seconds is // enough really, but I leave a year of seconds anyway, in case the // user's clock is incorrect or something like that. - TmpSeconds = TmpSeconds - (38 * 365 * 24 * 60 * 60); + tmp_seconds = tmp_seconds - (38 * 365 * 24 * 60 * 60); // Make a smaller integer that fits in the double - u32 Seconds = static_cast(TmpSeconds); - double TmpTime = Seconds + ms; - - return TmpTime; + const auto seconds = static_cast(tmp_seconds); + return seconds + ms; } } // Namespace Common diff --git a/src/common/wall_clock.cpp b/src/common/wall_clock.cpp index 7a20e95b7..452a2837e 100644 --- a/src/common/wall_clock.cpp +++ b/src/common/wall_clock.cpp @@ -53,7 +53,7 @@ public: return Common::Divide128On32(temporary, 1000000000).first; } - void Pause(bool is_paused) override { + void Pause([[maybe_unused]] bool is_paused) override { // Do nothing in this clock type. } diff --git a/src/common/x64/native_clock.h b/src/common/x64/native_clock.h index 7c503df26..97aab6ac9 100644 --- a/src/common/x64/native_clock.h +++ b/src/common/x64/native_clock.h @@ -34,7 +34,7 @@ private: /// value used to reduce the native clocks accuracy as some apss rely on /// undefined behavior where the level of accuracy in the clock shouldn't /// be higher. - static constexpr u64 inaccuracy_mask = ~(0x400 - 1); + static constexpr u64 inaccuracy_mask = ~(UINT64_C(0x400) - 1); SpinLock rtsc_serialize{}; u64 last_measure{}; -- cgit v1.2.3 From 6f006d051e1fad075048ea5664e1ef0605e48a46 Mon Sep 17 00:00:00 2001 From: Lioncash Date: Wed, 4 Nov 2020 20:41:16 -0500 Subject: General: Fix clang build Allows building on clang to work again --- src/common/fiber.h | 4 ++-- src/common/spin_lock.h | 8 ++++++++ 2 files changed, 10 insertions(+), 2 deletions(-) (limited to 'src/common') diff --git a/src/common/fiber.h b/src/common/fiber.h index 699286ee2..1a027be96 100644 --- a/src/common/fiber.h +++ b/src/common/fiber.h @@ -41,8 +41,8 @@ public: Fiber(const Fiber&) = delete; Fiber& operator=(const Fiber&) = delete; - Fiber(Fiber&&) = default; - Fiber& operator=(Fiber&&) = default; + Fiber(Fiber&&) = delete; + Fiber& operator=(Fiber&&) = delete; /// Yields control from Fiber 'from' to Fiber 'to' /// Fiber 'from' must be the currently running fiber. diff --git a/src/common/spin_lock.h b/src/common/spin_lock.h index 4f946a258..06ac2f5bb 100644 --- a/src/common/spin_lock.h +++ b/src/common/spin_lock.h @@ -15,6 +15,14 @@ namespace Common { */ class SpinLock { public: + SpinLock() = default; + + SpinLock(const SpinLock&) = delete; + SpinLock& operator=(const SpinLock&) = delete; + + SpinLock(SpinLock&&) = delete; + SpinLock& operator=(SpinLock&&) = delete; + void lock(); void unlock(); [[nodiscard]] bool try_lock(); -- cgit v1.2.3 From 00fb79b2f3f49335543c57f1d27b057320f3ff05 Mon Sep 17 00:00:00 2001 From: Lioncash Date: Fri, 6 Nov 2020 20:29:54 -0500 Subject: common/fiber: Move all member variables into impl class Hides all of the implementation details for users of the class. This has the benefit of reducing includes and also making the fiber classes movable again. --- src/common/fiber.cpp | 155 +++++++++++++++++++++++++++------------------------ src/common/fiber.h | 20 +------ 2 files changed, 86 insertions(+), 89 deletions(-) (limited to 'src/common') diff --git a/src/common/fiber.cpp b/src/common/fiber.cpp index b209f52fc..3e3029cd1 100644 --- a/src/common/fiber.cpp +++ b/src/common/fiber.cpp @@ -4,6 +4,8 @@ #include "common/assert.h" #include "common/fiber.h" +#include "common/spin_lock.h" + #if defined(_WIN32) || defined(WIN32) #include #else @@ -14,18 +16,45 @@ namespace Common { constexpr std::size_t default_stack_size = 256 * 1024; // 256kb -#if defined(_WIN32) || defined(WIN32) - struct Fiber::FiberImpl { + SpinLock guard{}; + std::function entry_point; + std::function rewind_point; + void* rewind_parameter{}; + void* start_parameter{}; + std::shared_ptr previous_fiber; + bool is_thread_fiber{}; + bool released{}; + +#if defined(_WIN32) || defined(WIN32) LPVOID handle = nullptr; LPVOID rewind_handle = nullptr; +#else + alignas(64) std::array stack; + alignas(64) std::array rewind_stack; + u8* stack_limit; + u8* rewind_stack_limit; + boost::context::detail::fcontext_t context; + boost::context::detail::fcontext_t rewind_context; +#endif }; +void Fiber::SetStartParameter(void* new_parameter) { + impl->start_parameter = new_parameter; +} + +void Fiber::SetRewindPoint(std::function&& rewind_func, void* rewind_param) { + impl->rewind_point = std::move(rewind_func); + impl->rewind_parameter = rewind_param; +} + +#if defined(_WIN32) || defined(WIN32) + void Fiber::Start() { - ASSERT(previous_fiber != nullptr); - previous_fiber->guard.unlock(); - previous_fiber.reset(); - entry_point(start_parameter); + ASSERT(impl->previous_fiber != nullptr); + impl->previous_fiber->impl->guard.unlock(); + impl->previous_fiber.reset(); + impl->entry_point(impl->start_parameter); UNREACHABLE(); } @@ -34,58 +63,54 @@ void Fiber::OnRewind() { DeleteFiber(impl->handle); impl->handle = impl->rewind_handle; impl->rewind_handle = nullptr; - rewind_point(rewind_parameter); + impl->rewind_point(impl->rewind_parameter); UNREACHABLE(); } void Fiber::FiberStartFunc(void* fiber_parameter) { - auto fiber = static_cast(fiber_parameter); + auto* fiber = static_cast(fiber_parameter); fiber->Start(); } void Fiber::RewindStartFunc(void* fiber_parameter) { - auto fiber = static_cast(fiber_parameter); + auto* fiber = static_cast(fiber_parameter); fiber->OnRewind(); } Fiber::Fiber(std::function&& entry_point_func, void* start_parameter) - : entry_point{std::move(entry_point_func)}, start_parameter{start_parameter} { - impl = std::make_unique(); + : impl{std::make_unique()} { + impl->entry_point = std::move(entry_point_func); + impl->start_parameter = start_parameter; impl->handle = CreateFiber(default_stack_size, &FiberStartFunc, this); } Fiber::Fiber() : impl{std::make_unique()} {} Fiber::~Fiber() { - if (released) { + if (impl->released) { return; } // Make sure the Fiber is not being used - const bool locked = guard.try_lock(); + const bool locked = impl->guard.try_lock(); ASSERT_MSG(locked, "Destroying a fiber that's still running"); if (locked) { - guard.unlock(); + impl->guard.unlock(); } DeleteFiber(impl->handle); } void Fiber::Exit() { - ASSERT_MSG(is_thread_fiber, "Exitting non main thread fiber"); - if (!is_thread_fiber) { + ASSERT_MSG(impl->is_thread_fiber, "Exitting non main thread fiber"); + if (!impl->is_thread_fiber) { return; } ConvertFiberToThread(); - guard.unlock(); - released = true; -} - -void Fiber::SetRewindPoint(std::function&& rewind_func, void* rewind_param) { - rewind_point = std::move(rewind_func); - rewind_parameter = rewind_param; + impl->guard.unlock(); + impl->released = true; } void Fiber::Rewind() { - ASSERT(rewind_point); + ASSERT(impl->rewind_point); ASSERT(impl->rewind_handle == nullptr); impl->rewind_handle = CreateFiber(default_stack_size, &RewindStartFunc, this); SwitchToFiber(impl->rewind_handle); @@ -94,39 +119,30 @@ void Fiber::Rewind() { void Fiber::YieldTo(std::shared_ptr from, std::shared_ptr to) { ASSERT_MSG(from != nullptr, "Yielding fiber is null!"); ASSERT_MSG(to != nullptr, "Next fiber is null!"); - to->guard.lock(); - to->previous_fiber = from; + to->impl->guard.lock(); + to->impl->previous_fiber = from; SwitchToFiber(to->impl->handle); - ASSERT(from->previous_fiber != nullptr); - from->previous_fiber->guard.unlock(); - from->previous_fiber.reset(); + ASSERT(from->impl->previous_fiber != nullptr); + from->impl->previous_fiber->impl->guard.unlock(); + from->impl->previous_fiber.reset(); } std::shared_ptr Fiber::ThreadToFiber() { std::shared_ptr fiber = std::shared_ptr{new Fiber()}; - fiber->guard.lock(); + fiber->impl->guard.lock(); fiber->impl->handle = ConvertThreadToFiber(nullptr); - fiber->is_thread_fiber = true; + fiber->impl->is_thread_fiber = true; return fiber; } #else -struct Fiber::FiberImpl { - alignas(64) std::array stack; - alignas(64) std::array rewind_stack; - u8* stack_limit; - u8* rewind_stack_limit; - boost::context::detail::fcontext_t context; - boost::context::detail::fcontext_t rewind_context; -}; - void Fiber::Start(boost::context::detail::transfer_t& transfer) { - ASSERT(previous_fiber != nullptr); - previous_fiber->impl->context = transfer.fctx; - previous_fiber->guard.unlock(); - previous_fiber.reset(); - entry_point(start_parameter); + ASSERT(impl->previous_fiber != nullptr); + impl->previous_fiber->impl->context = transfer.fctx; + impl->previous_fiber->impl->guard.unlock(); + impl->previous_fiber.reset(); + impl->entry_point(impl->start_parameter); UNREACHABLE(); } @@ -137,23 +153,24 @@ void Fiber::OnRewind([[maybe_unused]] boost::context::detail::transfer_t& transf u8* tmp = impl->stack_limit; impl->stack_limit = impl->rewind_stack_limit; impl->rewind_stack_limit = tmp; - rewind_point(rewind_parameter); + impl->rewind_point(impl->rewind_parameter); UNREACHABLE(); } void Fiber::FiberStartFunc(boost::context::detail::transfer_t transfer) { - auto fiber = static_cast(transfer.data); + auto* fiber = static_cast(transfer.data); fiber->Start(transfer); } void Fiber::RewindStartFunc(boost::context::detail::transfer_t transfer) { - auto fiber = static_cast(transfer.data); + auto* fiber = static_cast(transfer.data); fiber->OnRewind(transfer); } Fiber::Fiber(std::function&& entry_point_func, void* start_parameter) - : entry_point{std::move(entry_point_func)}, start_parameter{start_parameter} { - impl = std::make_unique(); + : impl{std::make_unique()} { + impl->entry_point = std::move(entry_point_func); + impl->start_parameter = start_parameter; impl->stack_limit = impl->stack.data(); impl->rewind_stack_limit = impl->rewind_stack.data(); u8* stack_base = impl->stack_limit + default_stack_size; @@ -161,37 +178,31 @@ Fiber::Fiber(std::function&& entry_point_func, void* start_paramete boost::context::detail::make_fcontext(stack_base, impl->stack.size(), FiberStartFunc); } -void Fiber::SetRewindPoint(std::function&& rewind_func, void* rewind_param) { - rewind_point = std::move(rewind_func); - rewind_parameter = rewind_param; -} - Fiber::Fiber() : impl{std::make_unique()} {} Fiber::~Fiber() { - if (released) { + if (impl->released) { return; } // Make sure the Fiber is not being used - const bool locked = guard.try_lock(); + const bool locked = impl->guard.try_lock(); ASSERT_MSG(locked, "Destroying a fiber that's still running"); if (locked) { - guard.unlock(); + impl->guard.unlock(); } } void Fiber::Exit() { - - ASSERT_MSG(is_thread_fiber, "Exitting non main thread fiber"); - if (!is_thread_fiber) { + ASSERT_MSG(impl->is_thread_fiber, "Exitting non main thread fiber"); + if (!impl->is_thread_fiber) { return; } - guard.unlock(); - released = true; + impl->guard.unlock(); + impl->released = true; } void Fiber::Rewind() { - ASSERT(rewind_point); + ASSERT(impl->rewind_point); ASSERT(impl->rewind_context == nullptr); u8* stack_base = impl->rewind_stack_limit + default_stack_size; impl->rewind_context = @@ -202,19 +213,19 @@ void Fiber::Rewind() { void Fiber::YieldTo(std::shared_ptr from, std::shared_ptr to) { ASSERT_MSG(from != nullptr, "Yielding fiber is null!"); ASSERT_MSG(to != nullptr, "Next fiber is null!"); - to->guard.lock(); - to->previous_fiber = from; + to->impl->guard.lock(); + to->impl->previous_fiber = from; auto transfer = boost::context::detail::jump_fcontext(to->impl->context, to.get()); - ASSERT(from->previous_fiber != nullptr); - from->previous_fiber->impl->context = transfer.fctx; - from->previous_fiber->guard.unlock(); - from->previous_fiber.reset(); + ASSERT(from->impl->previous_fiber != nullptr); + from->impl->previous_fiber->impl->context = transfer.fctx; + from->impl->previous_fiber->impl->guard.unlock(); + from->impl->previous_fiber.reset(); } std::shared_ptr Fiber::ThreadToFiber() { std::shared_ptr fiber = std::shared_ptr{new Fiber()}; - fiber->guard.lock(); - fiber->is_thread_fiber = true; + fiber->impl->guard.lock(); + fiber->impl->is_thread_fiber = true; return fiber; } diff --git a/src/common/fiber.h b/src/common/fiber.h index 1a027be96..5323e8579 100644 --- a/src/common/fiber.h +++ b/src/common/fiber.h @@ -7,9 +7,6 @@ #include #include -#include "common/common_types.h" -#include "common/spin_lock.h" - #if !defined(_WIN32) && !defined(WIN32) namespace boost::context::detail { struct transfer_t; @@ -41,8 +38,8 @@ public: Fiber(const Fiber&) = delete; Fiber& operator=(const Fiber&) = delete; - Fiber(Fiber&&) = delete; - Fiber& operator=(Fiber&&) = delete; + Fiber(Fiber&&) = default; + Fiber& operator=(Fiber&&) = default; /// Yields control from Fiber 'from' to Fiber 'to' /// Fiber 'from' must be the currently running fiber. @@ -57,9 +54,7 @@ public: void Exit(); /// Changes the start parameter of the fiber. Has no effect if the fiber already started - void SetStartParameter(void* new_parameter) { - start_parameter = new_parameter; - } + void SetStartParameter(void* new_parameter); private: Fiber(); @@ -77,16 +72,7 @@ private: #endif struct FiberImpl; - - SpinLock guard{}; - std::function entry_point; - std::function rewind_point; - void* rewind_parameter{}; - void* start_parameter{}; - std::shared_ptr previous_fiber; std::unique_ptr impl; - bool is_thread_fiber{}; - bool released{}; }; } // namespace Common -- cgit v1.2.3 From 0890451c554973b907dc8febe6e65b499f21ab09 Mon Sep 17 00:00:00 2001 From: Lioncash Date: Tue, 17 Nov 2020 19:43:24 -0500 Subject: page_table: Remove unnecessary header inclusions Prevents indirect inclusions for these headers. --- src/common/page_table.h | 4 ---- 1 file changed, 4 deletions(-) (limited to 'src/common') diff --git a/src/common/page_table.h b/src/common/page_table.h index cf5eed780..9908947ac 100644 --- a/src/common/page_table.h +++ b/src/common/page_table.h @@ -4,10 +4,6 @@ #pragma once -#include - -#include - #include "common/common_types.h" #include "common/memory_hook.h" #include "common/virtual_buffer.h" -- cgit v1.2.3 From 3cfd962ef471fa064f0f72eceff2a592e37b6642 Mon Sep 17 00:00:00 2001 From: Lioncash Date: Tue, 17 Nov 2020 19:45:17 -0500 Subject: page_table: Add missing doxygen parameters to Resize() Resolves two -Wdocumentation warnings. --- src/common/page_table.h | 2 ++ 1 file changed, 2 insertions(+) (limited to 'src/common') diff --git a/src/common/page_table.h b/src/common/page_table.h index 9908947ac..a5be7a668 100644 --- a/src/common/page_table.h +++ b/src/common/page_table.h @@ -54,6 +54,8 @@ struct PageTable { * a given address space. * * @param address_space_width_in_bits The address size width in bits. + * @param page_size_in_bits The page size in bits. + * @param has_attribute Whether or not this page has any backing attributes. */ void Resize(std::size_t address_space_width_in_bits, std::size_t page_size_in_bits, bool has_attribute); -- cgit v1.2.3 From b3c8997829ed986c948d195bc1e7c8f66c42755e Mon Sep 17 00:00:00 2001 From: Lioncash Date: Tue, 17 Nov 2020 19:58:41 -0500 Subject: page_table: Allow page tables to be moved Makes page tables and virtual buffers able to be moved, but not copied, making the interface more flexible. Previously, with the destructor specified, but no move assignment or constructor specified, they wouldn't be implicitly generated. --- src/common/page_table.cpp | 2 +- src/common/page_table.h | 10 +++++++++- src/common/virtual_buffer.cpp | 4 ++-- src/common/virtual_buffer.h | 23 ++++++++++++++++++----- 4 files changed, 30 insertions(+), 9 deletions(-) (limited to 'src/common') diff --git a/src/common/page_table.cpp b/src/common/page_table.cpp index e5d3090d5..bccea0894 100644 --- a/src/common/page_table.cpp +++ b/src/common/page_table.cpp @@ -8,7 +8,7 @@ namespace Common { PageTable::PageTable() = default; -PageTable::~PageTable() = default; +PageTable::~PageTable() noexcept = default; void PageTable::Resize(std::size_t address_space_width_in_bits, std::size_t page_size_in_bits, bool has_attribute) { diff --git a/src/common/page_table.h b/src/common/page_table.h index a5be7a668..9754fabf9 100644 --- a/src/common/page_table.h +++ b/src/common/page_table.h @@ -4,6 +4,8 @@ #pragma once +#include + #include "common/common_types.h" #include "common/memory_hook.h" #include "common/virtual_buffer.h" @@ -47,7 +49,13 @@ struct SpecialRegion { */ struct PageTable { PageTable(); - ~PageTable(); + ~PageTable() noexcept; + + PageTable(const PageTable&) = delete; + PageTable& operator=(const PageTable&) = delete; + + PageTable(PageTable&&) noexcept = default; + PageTable& operator=(PageTable&&) noexcept = default; /** * Resizes the page table to be able to accomodate enough pages within diff --git a/src/common/virtual_buffer.cpp b/src/common/virtual_buffer.cpp index b009cb500..e3ca29258 100644 --- a/src/common/virtual_buffer.cpp +++ b/src/common/virtual_buffer.cpp @@ -13,7 +13,7 @@ namespace Common { -void* AllocateMemoryPages(std::size_t size) { +void* AllocateMemoryPages(std::size_t size) noexcept { #ifdef _WIN32 void* base{VirtualAlloc(nullptr, size, MEM_COMMIT, PAGE_READWRITE)}; #else @@ -29,7 +29,7 @@ void* AllocateMemoryPages(std::size_t size) { return base; } -void FreeMemoryPages(void* base, [[maybe_unused]] std::size_t size) { +void FreeMemoryPages(void* base, [[maybe_unused]] std::size_t size) noexcept { if (!base) { return; } diff --git a/src/common/virtual_buffer.h b/src/common/virtual_buffer.h index 125cb42f0..363913d45 100644 --- a/src/common/virtual_buffer.h +++ b/src/common/virtual_buffer.h @@ -4,25 +4,38 @@ #pragma once -#include "common/common_funcs.h" +#include namespace Common { -void* AllocateMemoryPages(std::size_t size); -void FreeMemoryPages(void* base, std::size_t size); +void* AllocateMemoryPages(std::size_t size) noexcept; +void FreeMemoryPages(void* base, std::size_t size) noexcept; template -class VirtualBuffer final : NonCopyable { +class VirtualBuffer final { public: constexpr VirtualBuffer() = default; explicit VirtualBuffer(std::size_t count) : alloc_size{count * sizeof(T)} { base_ptr = reinterpret_cast(AllocateMemoryPages(alloc_size)); } - ~VirtualBuffer() { + ~VirtualBuffer() noexcept { FreeMemoryPages(base_ptr, alloc_size); } + VirtualBuffer(const VirtualBuffer&) = delete; + VirtualBuffer& operator=(const VirtualBuffer&) = delete; + + VirtualBuffer(VirtualBuffer&& other) noexcept + : alloc_size{std::exchange(other.alloc_size, 0)}, base_ptr{std::exchange(other.base_ptr), + nullptr} {} + + VirtualBuffer& operator=(VirtualBuffer&& other) noexcept { + alloc_size = std::exchange(other.alloc_size, 0); + base_ptr = std::exchange(other.base_ptr, nullptr); + return *this; + } + void resize(std::size_t count) { FreeMemoryPages(base_ptr, alloc_size); -- cgit v1.2.3 From 0ca91ced2dab3654e78e4c465506d7baad3cbeeb Mon Sep 17 00:00:00 2001 From: Lioncash Date: Tue, 17 Nov 2020 20:09:55 -0500 Subject: virtual_buffer: Add compile-time type-safety guarantees with VirtualBuffer VirtualBuffer makes use of VirtualAlloc (on Windows) and mmap() (on other platforms). Neither of these ensure that non-trivial objects are properly constructed in the allocated memory. To prevent potential undefined behavior occurring due to that, we can add a static assert to loudly complain about cases where that is done. --- src/common/virtual_buffer.h | 6 ++++++ 1 file changed, 6 insertions(+) (limited to 'src/common') diff --git a/src/common/virtual_buffer.h b/src/common/virtual_buffer.h index 363913d45..078e61c77 100644 --- a/src/common/virtual_buffer.h +++ b/src/common/virtual_buffer.h @@ -4,6 +4,7 @@ #pragma once +#include #include namespace Common { @@ -14,6 +15,11 @@ void FreeMemoryPages(void* base, std::size_t size) noexcept; template class VirtualBuffer final { public: + static_assert( + std::is_trivially_constructible_v, + "T must be trivially constructible, as non-trivial constructors will not be executed " + "with the current allocator"); + constexpr VirtualBuffer() = default; explicit VirtualBuffer(std::size_t count) : alloc_size{count * sizeof(T)} { base_ptr = reinterpret_cast(AllocateMemoryPages(alloc_size)); -- cgit v1.2.3 From 412044960a39180bc9990f6b35e1872c6a69591b Mon Sep 17 00:00:00 2001 From: Lioncash Date: Thu, 19 Nov 2020 07:54:00 -0500 Subject: virtual_buffer: Do nothing on resize() calls with same sizes Prevents us from churning memory by freeing and reallocating a memory block that would have already been adequate as is. --- src/common/virtual_buffer.h | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) (limited to 'src/common') diff --git a/src/common/virtual_buffer.h b/src/common/virtual_buffer.h index 078e61c77..91d430036 100644 --- a/src/common/virtual_buffer.h +++ b/src/common/virtual_buffer.h @@ -43,9 +43,14 @@ public: } void resize(std::size_t count) { + const auto new_size = count * sizeof(T); + if (new_size == alloc_size) { + return; + } + FreeMemoryPages(base_ptr, alloc_size); - alloc_size = count * sizeof(T); + alloc_size = new_size; base_ptr = reinterpret_cast(AllocateMemoryPages(alloc_size)); } -- cgit v1.2.3 From 6e3767648253b2162b86ecdca6fcbc31e9471595 Mon Sep 17 00:00:00 2001 From: bunnei Date: Thu, 19 Nov 2020 12:35:07 -0800 Subject: hle: service: Stub OLSC Initialize and SetSaveDataBackupSettingEnabled functions. - Used by Animal Cross: New Horizons v1.6.0 update, minimal stub gets this update working. --- src/common/logging/backend.cpp | 1 + src/common/logging/log.h | 1 + 2 files changed, 2 insertions(+) (limited to 'src/common') diff --git a/src/common/logging/backend.cpp b/src/common/logging/backend.cpp index 90dfa22ca..7859344b9 100644 --- a/src/common/logging/backend.cpp +++ b/src/common/logging/backend.cpp @@ -222,6 +222,7 @@ void DebuggerBackend::Write(const Entry& entry) { SUB(Service, NPNS) \ SUB(Service, NS) \ SUB(Service, NVDRV) \ + SUB(Service, OLSC) \ SUB(Service, PCIE) \ SUB(Service, PCTL) \ SUB(Service, PCV) \ diff --git a/src/common/logging/log.h b/src/common/logging/log.h index 13a4f1e30..835894918 100644 --- a/src/common/logging/log.h +++ b/src/common/logging/log.h @@ -95,6 +95,7 @@ enum class Class : ClassType { Service_NPNS, ///< The NPNS service Service_NS, ///< The NS services Service_NVDRV, ///< The NVDRV (Nvidia driver) service + Service_OLSC, ///< The OLSC service Service_PCIE, ///< The PCIe service Service_PCTL, ///< The PCTL (Parental control) service Service_PCV, ///< The PCV service -- cgit v1.2.3 From 3f2e605dd12e91f81dd8debf46e69accab3aa1d0 Mon Sep 17 00:00:00 2001 From: ReinUsesLisp Date: Fri, 20 Nov 2020 01:52:37 -0300 Subject: common/bit_cast: Add function matching std::bit_cast without constexpr Add a std::bit_cast-like function archiving the same runtime results as the standard function, without compile time support. This allows us to use bit_cast while we wait for compiler support, it can be trivially replaced in the future. --- src/common/CMakeLists.txt | 1 + src/common/bit_cast.h | 22 ++++++++++++++++++++++ 2 files changed, 23 insertions(+) create mode 100644 src/common/bit_cast.h (limited to 'src/common') diff --git a/src/common/CMakeLists.txt b/src/common/CMakeLists.txt index 207c7a0a6..d20e6c3b5 100644 --- a/src/common/CMakeLists.txt +++ b/src/common/CMakeLists.txt @@ -102,6 +102,7 @@ add_library(common STATIC atomic_ops.h detached_tasks.cpp detached_tasks.h + bit_cast.h bit_field.h bit_util.h cityhash.cpp diff --git a/src/common/bit_cast.h b/src/common/bit_cast.h new file mode 100644 index 000000000..a32a063d1 --- /dev/null +++ b/src/common/bit_cast.h @@ -0,0 +1,22 @@ +// Copyright 2020 yuzu emulator team +// Licensed under GPLv2 or any later version +// Refer to the license.txt file included. + +#pragma once + +#include +#include + +namespace Common { + +template +[[nodiscard]] std::enable_if_t && + std::is_trivially_copyable_v, + To> +BitCast(const From& src) noexcept { + To dst; + std::memcpy(&dst, &src, sizeof(To)); + return dst; +} + +} // namespace Common -- cgit v1.2.3 From 630823e3630f2bb77a3a5e4aa0f8f87859d05ee1 Mon Sep 17 00:00:00 2001 From: ReinUsesLisp Date: Wed, 25 Nov 2020 23:03:50 -0300 Subject: common: Add Common::DivCeil and Common::DivCeilLog2 Add an equivalent to 'Common::AlignUp(n, d) / d' and a log2 alternative. --- src/common/CMakeLists.txt | 1 + src/common/div_ceil.h | 26 ++++++++++++++++++++++++++ 2 files changed, 27 insertions(+) create mode 100644 src/common/div_ceil.h (limited to 'src/common') diff --git a/src/common/CMakeLists.txt b/src/common/CMakeLists.txt index d20e6c3b5..56c7e21f5 100644 --- a/src/common/CMakeLists.txt +++ b/src/common/CMakeLists.txt @@ -112,6 +112,7 @@ add_library(common STATIC common_paths.h common_types.h concepts.h + div_ceil.h dynamic_library.cpp dynamic_library.h fiber.cpp diff --git a/src/common/div_ceil.h b/src/common/div_ceil.h new file mode 100644 index 000000000..6b2c48f91 --- /dev/null +++ b/src/common/div_ceil.h @@ -0,0 +1,26 @@ +// Copyright 2020 yuzu emulator team +// Licensed under GPLv2 or any later version +// Refer to the license.txt file included. + +#pragma once + +#include +#include + +namespace Common { + +/// Ceiled integer division. +template +requires std::is_integral_v&& std::is_unsigned_v[[nodiscard]] constexpr auto DivCeil( + N number, D divisor) { + return (static_cast(number) + divisor - 1) / divisor; +} + +/// Ceiled integer division with logarithmic divisor in base 2 +template +requires std::is_integral_v&& std::is_unsigned_v[[nodiscard]] constexpr auto DivCeilLog2( + N value, D alignment_log2) { + return (static_cast(value) + (D(1) << alignment_log2) - 1) >> alignment_log2; +} + +} // namespace Common -- cgit v1.2.3 From c042a89113617f75e81163f103ef82d6d714cd87 Mon Sep 17 00:00:00 2001 From: bunnei Date: Fri, 13 Nov 2020 15:17:47 -0800 Subject: common: fiber: Use boost::context instead of native fibers on Windows. --- src/common/fiber.cpp | 114 ++++----------------------------------------------- src/common/fiber.h | 9 ---- 2 files changed, 8 insertions(+), 115 deletions(-) (limited to 'src/common') diff --git a/src/common/fiber.cpp b/src/common/fiber.cpp index 3e3029cd1..3978c8624 100644 --- a/src/common/fiber.cpp +++ b/src/common/fiber.cpp @@ -6,17 +6,16 @@ #include "common/fiber.h" #include "common/spin_lock.h" -#if defined(_WIN32) || defined(WIN32) -#include -#else #include -#endif namespace Common { -constexpr std::size_t default_stack_size = 256 * 1024; // 256kb +constexpr std::size_t default_stack_size = 256 * 1024; struct Fiber::FiberImpl { + alignas(64) std::array stack; + alignas(64) std::array rewind_stack; + SpinLock guard{}; std::function entry_point; std::function rewind_point; @@ -26,17 +25,10 @@ struct Fiber::FiberImpl { bool is_thread_fiber{}; bool released{}; -#if defined(_WIN32) || defined(WIN32) - LPVOID handle = nullptr; - LPVOID rewind_handle = nullptr; -#else - alignas(64) std::array stack; - alignas(64) std::array rewind_stack; - u8* stack_limit; - u8* rewind_stack_limit; - boost::context::detail::fcontext_t context; - boost::context::detail::fcontext_t rewind_context; -#endif + u8* stack_limit{}; + u8* rewind_stack_limit{}; + boost::context::detail::fcontext_t context{}; + boost::context::detail::fcontext_t rewind_context{}; }; void Fiber::SetStartParameter(void* new_parameter) { @@ -48,95 +40,6 @@ void Fiber::SetRewindPoint(std::function&& rewind_func, void* rewin impl->rewind_parameter = rewind_param; } -#if defined(_WIN32) || defined(WIN32) - -void Fiber::Start() { - ASSERT(impl->previous_fiber != nullptr); - impl->previous_fiber->impl->guard.unlock(); - impl->previous_fiber.reset(); - impl->entry_point(impl->start_parameter); - UNREACHABLE(); -} - -void Fiber::OnRewind() { - ASSERT(impl->handle != nullptr); - DeleteFiber(impl->handle); - impl->handle = impl->rewind_handle; - impl->rewind_handle = nullptr; - impl->rewind_point(impl->rewind_parameter); - UNREACHABLE(); -} - -void Fiber::FiberStartFunc(void* fiber_parameter) { - auto* fiber = static_cast(fiber_parameter); - fiber->Start(); -} - -void Fiber::RewindStartFunc(void* fiber_parameter) { - auto* fiber = static_cast(fiber_parameter); - fiber->OnRewind(); -} - -Fiber::Fiber(std::function&& entry_point_func, void* start_parameter) - : impl{std::make_unique()} { - impl->entry_point = std::move(entry_point_func); - impl->start_parameter = start_parameter; - impl->handle = CreateFiber(default_stack_size, &FiberStartFunc, this); -} - -Fiber::Fiber() : impl{std::make_unique()} {} - -Fiber::~Fiber() { - if (impl->released) { - return; - } - // Make sure the Fiber is not being used - const bool locked = impl->guard.try_lock(); - ASSERT_MSG(locked, "Destroying a fiber that's still running"); - if (locked) { - impl->guard.unlock(); - } - DeleteFiber(impl->handle); -} - -void Fiber::Exit() { - ASSERT_MSG(impl->is_thread_fiber, "Exitting non main thread fiber"); - if (!impl->is_thread_fiber) { - return; - } - ConvertFiberToThread(); - impl->guard.unlock(); - impl->released = true; -} - -void Fiber::Rewind() { - ASSERT(impl->rewind_point); - ASSERT(impl->rewind_handle == nullptr); - impl->rewind_handle = CreateFiber(default_stack_size, &RewindStartFunc, this); - SwitchToFiber(impl->rewind_handle); -} - -void Fiber::YieldTo(std::shared_ptr from, std::shared_ptr to) { - ASSERT_MSG(from != nullptr, "Yielding fiber is null!"); - ASSERT_MSG(to != nullptr, "Next fiber is null!"); - to->impl->guard.lock(); - to->impl->previous_fiber = from; - SwitchToFiber(to->impl->handle); - ASSERT(from->impl->previous_fiber != nullptr); - from->impl->previous_fiber->impl->guard.unlock(); - from->impl->previous_fiber.reset(); -} - -std::shared_ptr Fiber::ThreadToFiber() { - std::shared_ptr fiber = std::shared_ptr{new Fiber()}; - fiber->impl->guard.lock(); - fiber->impl->handle = ConvertThreadToFiber(nullptr); - fiber->impl->is_thread_fiber = true; - return fiber; -} - -#else - void Fiber::Start(boost::context::detail::transfer_t& transfer) { ASSERT(impl->previous_fiber != nullptr); impl->previous_fiber->impl->context = transfer.fctx; @@ -229,5 +132,4 @@ std::shared_ptr Fiber::ThreadToFiber() { return fiber; } -#endif } // namespace Common diff --git a/src/common/fiber.h b/src/common/fiber.h index 5323e8579..f7f587f8c 100644 --- a/src/common/fiber.h +++ b/src/common/fiber.h @@ -7,11 +7,9 @@ #include #include -#if !defined(_WIN32) && !defined(WIN32) namespace boost::context::detail { struct transfer_t; } -#endif namespace Common { @@ -59,17 +57,10 @@ public: private: Fiber(); -#if defined(_WIN32) || defined(WIN32) - void OnRewind(); - void Start(); - static void FiberStartFunc(void* fiber_parameter); - static void RewindStartFunc(void* fiber_parameter); -#else void OnRewind(boost::context::detail::transfer_t& transfer); void Start(boost::context::detail::transfer_t& transfer); static void FiberStartFunc(boost::context::detail::transfer_t transfer); static void RewindStartFunc(boost::context::detail::transfer_t transfer); -#endif struct FiberImpl; std::unique_ptr impl; -- cgit v1.2.3 From 24cae76d16d7344154c1a507889e33793b369be7 Mon Sep 17 00:00:00 2001 From: bunnei Date: Sun, 15 Nov 2020 00:36:26 -0800 Subject: common: fiber: Use VirtualBuffer for stack memory. - This will be aligned by default, and helps memory usage. --- src/common/fiber.cpp | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) (limited to 'src/common') diff --git a/src/common/fiber.cpp b/src/common/fiber.cpp index 3978c8624..3c1eefcb7 100644 --- a/src/common/fiber.cpp +++ b/src/common/fiber.cpp @@ -5,6 +5,7 @@ #include "common/assert.h" #include "common/fiber.h" #include "common/spin_lock.h" +#include "common/virtual_buffer.h" #include @@ -13,8 +14,10 @@ namespace Common { constexpr std::size_t default_stack_size = 256 * 1024; struct Fiber::FiberImpl { - alignas(64) std::array stack; - alignas(64) std::array rewind_stack; + FiberImpl() : stack{default_stack_size}, rewind_stack{default_stack_size} {} + + VirtualBuffer stack; + VirtualBuffer rewind_stack; SpinLock guard{}; std::function entry_point; -- cgit v1.2.3 From 1ea6bdef058a789e2771511f741bffcca73c3525 Mon Sep 17 00:00:00 2001 From: Lioncash Date: Wed, 25 Nov 2020 15:21:03 -0500 Subject: audio_core: Make shadowing and unused parameters errors Moves the audio code closer to enabling warnings as errors in general. --- src/common/wall_clock.cpp | 4 ++-- src/common/wall_clock.h | 6 +++--- src/common/x64/native_clock.cpp | 8 ++++---- src/common/x64/native_clock.h | 3 ++- 4 files changed, 11 insertions(+), 10 deletions(-) (limited to 'src/common') diff --git a/src/common/wall_clock.cpp b/src/common/wall_clock.cpp index 452a2837e..a8c143f85 100644 --- a/src/common/wall_clock.cpp +++ b/src/common/wall_clock.cpp @@ -17,8 +17,8 @@ using base_time_point = std::chrono::time_point; class StandardWallClock final : public WallClock { public: - StandardWallClock(u64 emulated_cpu_frequency, u64 emulated_clock_frequency) - : WallClock(emulated_cpu_frequency, emulated_clock_frequency, false) { + explicit StandardWallClock(u64 emulated_cpu_frequency_, u64 emulated_clock_frequency_) + : WallClock(emulated_cpu_frequency_, emulated_clock_frequency_, false) { start_time = base_timer::now(); } diff --git a/src/common/wall_clock.h b/src/common/wall_clock.h index bc7adfbf8..cef3e9499 100644 --- a/src/common/wall_clock.h +++ b/src/common/wall_clock.h @@ -38,9 +38,9 @@ public: } protected: - WallClock(u64 emulated_cpu_frequency, u64 emulated_clock_frequency, bool is_native) - : emulated_cpu_frequency{emulated_cpu_frequency}, - emulated_clock_frequency{emulated_clock_frequency}, is_native{is_native} {} + explicit WallClock(u64 emulated_cpu_frequency_, u64 emulated_clock_frequency_, bool is_native_) + : emulated_cpu_frequency{emulated_cpu_frequency_}, + emulated_clock_frequency{emulated_clock_frequency_}, is_native{is_native_} {} u64 emulated_cpu_frequency; u64 emulated_clock_frequency; diff --git a/src/common/x64/native_clock.cpp b/src/common/x64/native_clock.cpp index 424b39b1f..eb8a7782f 100644 --- a/src/common/x64/native_clock.cpp +++ b/src/common/x64/native_clock.cpp @@ -43,10 +43,10 @@ u64 EstimateRDTSCFrequency() { } namespace X64 { -NativeClock::NativeClock(u64 emulated_cpu_frequency, u64 emulated_clock_frequency, - u64 rtsc_frequency) - : WallClock(emulated_cpu_frequency, emulated_clock_frequency, true), rtsc_frequency{ - rtsc_frequency} { +NativeClock::NativeClock(u64 emulated_cpu_frequency_, u64 emulated_clock_frequency_, + u64 rtsc_frequency_) + : WallClock(emulated_cpu_frequency_, emulated_clock_frequency_, true), rtsc_frequency{ + rtsc_frequency_} { _mm_mfence(); last_measure = __rdtsc(); accumulated_ticks = 0U; diff --git a/src/common/x64/native_clock.h b/src/common/x64/native_clock.h index 97aab6ac9..6d1e32ac8 100644 --- a/src/common/x64/native_clock.h +++ b/src/common/x64/native_clock.h @@ -14,7 +14,8 @@ namespace Common { namespace X64 { class NativeClock final : public WallClock { public: - NativeClock(u64 emulated_cpu_frequency, u64 emulated_clock_frequency, u64 rtsc_frequency); + explicit NativeClock(u64 emulated_cpu_frequency_, u64 emulated_clock_frequency_, + u64 rtsc_frequency_); std::chrono::nanoseconds GetTimeNS() override; -- cgit v1.2.3 From b1262674425318eaafc4f4d159b04a0e0e88a4b5 Mon Sep 17 00:00:00 2001 From: Lioncash Date: Sat, 5 Dec 2020 00:40:42 -0500 Subject: xbyak_abi: Avoid implicit sign conversions --- src/common/x64/xbyak_abi.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src/common') diff --git a/src/common/x64/xbyak_abi.h b/src/common/x64/xbyak_abi.h index 26e4bfda5..f25e968aa 100644 --- a/src/common/x64/xbyak_abi.h +++ b/src/common/x64/xbyak_abi.h @@ -11,12 +11,12 @@ namespace Common::X64 { -constexpr std::size_t RegToIndex(const Xbyak::Reg& reg) { +constexpr size_t RegToIndex(const Xbyak::Reg& reg) { using Kind = Xbyak::Reg::Kind; ASSERT_MSG((reg.getKind() & (Kind::REG | Kind::XMM)) != 0, "RegSet only support GPRs and XMM registers."); ASSERT_MSG(reg.getIdx() < 16, "RegSet only supports XXM0-15."); - return reg.getIdx() + (reg.getKind() == Kind::REG ? 0 : 16); + return static_cast(reg.getIdx()) + (reg.getKind() == Kind::REG ? 0 : 16); } constexpr Xbyak::Reg64 IndexToReg64(std::size_t reg_index) { -- cgit v1.2.3 From 2c375013dd96e2b4e57bf50f3c7a62c08ccf5816 Mon Sep 17 00:00:00 2001 From: Lioncash Date: Sat, 5 Dec 2020 00:42:49 -0500 Subject: xbyak_abi: Shorten std::size_t to size_t Makes for less reading. --- src/common/x64/xbyak_abi.h | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) (limited to 'src/common') diff --git a/src/common/x64/xbyak_abi.h b/src/common/x64/xbyak_abi.h index f25e968aa..c2c9b6134 100644 --- a/src/common/x64/xbyak_abi.h +++ b/src/common/x64/xbyak_abi.h @@ -19,17 +19,17 @@ constexpr size_t RegToIndex(const Xbyak::Reg& reg) { return static_cast(reg.getIdx()) + (reg.getKind() == Kind::REG ? 0 : 16); } -constexpr Xbyak::Reg64 IndexToReg64(std::size_t reg_index) { +constexpr Xbyak::Reg64 IndexToReg64(size_t reg_index) { ASSERT(reg_index < 16); return Xbyak::Reg64(static_cast(reg_index)); } -constexpr Xbyak::Xmm IndexToXmm(std::size_t reg_index) { +constexpr Xbyak::Xmm IndexToXmm(size_t reg_index) { ASSERT(reg_index >= 16 && reg_index < 32); return Xbyak::Xmm(static_cast(reg_index - 16)); } -constexpr Xbyak::Reg IndexToReg(std::size_t reg_index) { +constexpr Xbyak::Reg IndexToReg(size_t reg_index) { if (reg_index < 16) { return IndexToReg64(reg_index); } else { @@ -182,7 +182,7 @@ inline size_t ABI_PushRegistersAndAdjustStack(Xbyak::CodeGenerator& code, std::b size_t rsp_alignment, size_t needed_frame_size = 0) { auto frame_info = ABI_CalculateFrameSize(regs, rsp_alignment, needed_frame_size); - for (std::size_t i = 0; i < regs.size(); ++i) { + for (size_t i = 0; i < regs.size(); ++i) { if (regs[i] && ABI_ALL_GPRS[i]) { code.push(IndexToReg64(i)); } @@ -192,7 +192,7 @@ inline size_t ABI_PushRegistersAndAdjustStack(Xbyak::CodeGenerator& code, std::b code.sub(code.rsp, frame_info.subtraction); } - for (std::size_t i = 0; i < regs.size(); ++i) { + for (size_t i = 0; i < regs.size(); ++i) { if (regs[i] && ABI_ALL_XMMS[i]) { code.movaps(code.xword[code.rsp + frame_info.xmm_offset], IndexToXmm(i)); frame_info.xmm_offset += 0x10; @@ -206,7 +206,7 @@ inline void ABI_PopRegistersAndAdjustStack(Xbyak::CodeGenerator& code, std::bits size_t rsp_alignment, size_t needed_frame_size = 0) { auto frame_info = ABI_CalculateFrameSize(regs, rsp_alignment, needed_frame_size); - for (std::size_t i = 0; i < regs.size(); ++i) { + for (size_t i = 0; i < regs.size(); ++i) { if (regs[i] && ABI_ALL_XMMS[i]) { code.movaps(IndexToXmm(i), code.xword[code.rsp + frame_info.xmm_offset]); frame_info.xmm_offset += 0x10; @@ -218,8 +218,8 @@ inline void ABI_PopRegistersAndAdjustStack(Xbyak::CodeGenerator& code, std::bits } // GPRs need to be popped in reverse order - for (std::size_t j = 0; j < regs.size(); ++j) { - const std::size_t i = regs.size() - j - 1; + for (size_t j = 0; j < regs.size(); ++j) { + const size_t i = regs.size() - j - 1; if (regs[i] && ABI_ALL_GPRS[i]) { code.pop(IndexToReg64(i)); } -- cgit v1.2.3 From f95602f15207851b849c57e2a2dd313a087b2493 Mon Sep 17 00:00:00 2001 From: Lioncash Date: Sat, 5 Dec 2020 11:40:14 -0500 Subject: video_core: Resolve more variable shadowing scenarios pt.3 Cleans out the rest of the occurrences of variable shadowing and makes any further occurrences of shadowing compiler errors. --- src/common/scope_exit.h | 2 +- src/common/telemetry.h | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) (limited to 'src/common') diff --git a/src/common/scope_exit.h b/src/common/scope_exit.h index 68ef5f197..fa46cb394 100644 --- a/src/common/scope_exit.h +++ b/src/common/scope_exit.h @@ -10,7 +10,7 @@ namespace detail { template struct ScopeExitHelper { - explicit ScopeExitHelper(Func&& func) : func(std::move(func)) {} + explicit ScopeExitHelper(Func&& func_) : func(std::move(func_)) {} ~ScopeExitHelper() { if (active) { func(); diff --git a/src/common/telemetry.h b/src/common/telemetry.h index a50c5d1de..49186e848 100644 --- a/src/common/telemetry.h +++ b/src/common/telemetry.h @@ -52,8 +52,8 @@ public: template class Field : public FieldInterface { public: - Field(FieldType type, std::string name, T value) - : name(std::move(name)), type(type), value(std::move(value)) {} + Field(FieldType type_, std::string name_, T value_) + : name(std::move(name_)), type(type_), value(std::move(value_)) {} Field(const Field&) = default; Field& operator=(const Field&) = default; -- cgit v1.2.3 From 8dbfa4e1a4af2cda4500304ba8fe1a1b09bbb814 Mon Sep 17 00:00:00 2001 From: bunnei Date: Fri, 27 Nov 2020 14:13:18 -0800 Subject: common: Port BitSet from Mesosphere. --- src/common/CMakeLists.txt | 1 + src/common/bit_set.h | 100 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 101 insertions(+) create mode 100644 src/common/bit_set.h (limited to 'src/common') diff --git a/src/common/CMakeLists.txt b/src/common/CMakeLists.txt index 56c7e21f5..fc2ed9999 100644 --- a/src/common/CMakeLists.txt +++ b/src/common/CMakeLists.txt @@ -104,6 +104,7 @@ add_library(common STATIC detached_tasks.h bit_cast.h bit_field.h + bit_set.h bit_util.h cityhash.cpp cityhash.h diff --git a/src/common/bit_set.h b/src/common/bit_set.h new file mode 100644 index 000000000..4f966de40 --- /dev/null +++ b/src/common/bit_set.h @@ -0,0 +1,100 @@ +/* + * Copyright (c) 2018-2020 Atmosphère-NX + * + * This program is free software; you can redistribute it and/or modify it + * under the terms and conditions of the GNU General Public License, + * version 2, as published by the Free Software Foundation. + * + * This program is distributed in the hope it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#pragma once + +#include "common/alignment.h" +#include "common/bit_util.h" +#include "common/common_types.h" + +namespace Common { + +namespace impl { + +#define BITSIZEOF(x) (sizeof(x) * CHAR_BIT) + +template +class BitSet { +private: + static_assert(std::is_integral::value); + static_assert(std::is_unsigned::value); + static_assert(sizeof(Storage) <= sizeof(u64)); + + static constexpr size_t FlagsPerWord = BITSIZEOF(Storage); + static constexpr size_t NumWords = AlignUp(N, FlagsPerWord) / FlagsPerWord; + + static constexpr auto CountLeadingZeroImpl(Storage word) { + return CountLeadingZeroes64(static_cast(word)) - + (BITSIZEOF(unsigned long long) - FlagsPerWord); + } + + static constexpr Storage GetBitMask(size_t bit) { + return Storage(1) << (FlagsPerWord - 1 - bit); + } + +private: + Storage words[NumWords]; + +public: + constexpr BitSet() : words() { /* ... */ + } + + constexpr void SetBit(size_t i) { + this->words[i / FlagsPerWord] |= GetBitMask(i % FlagsPerWord); + } + + constexpr void ClearBit(size_t i) { + this->words[i / FlagsPerWord] &= ~GetBitMask(i % FlagsPerWord); + } + + constexpr size_t CountLeadingZero() const { + for (size_t i = 0; i < NumWords; i++) { + if (this->words[i]) { + return FlagsPerWord * i + CountLeadingZeroImpl(this->words[i]); + } + } + return FlagsPerWord * NumWords; + } + + constexpr size_t GetNextSet(size_t n) const { + for (size_t i = (n + 1) / FlagsPerWord; i < NumWords; i++) { + Storage word = this->words[i]; + if (!IsAligned(n + 1, FlagsPerWord)) { + word &= GetBitMask(n % FlagsPerWord) - 1; + } + if (word) { + return FlagsPerWord * i + CountLeadingZeroImpl(word); + } + } + return FlagsPerWord * NumWords; + } +}; + +} // namespace impl + +template +using BitSet8 = impl::BitSet; + +template +using BitSet16 = impl::BitSet; + +template +using BitSet32 = impl::BitSet; + +template +using BitSet64 = impl::BitSet; + +} // namespace Common -- cgit v1.2.3 From 8d3e06349e12e7de17c334619f1f986792d1de4b Mon Sep 17 00:00:00 2001 From: bunnei Date: Thu, 3 Dec 2020 16:43:18 -0800 Subject: hle: kernel: Separate KScheduler from GlobalSchedulerContext class. --- src/common/CMakeLists.txt | 1 - src/common/multi_level_queue.h | 345 ----------------------------------------- 2 files changed, 346 deletions(-) delete mode 100644 src/common/multi_level_queue.h (limited to 'src/common') diff --git a/src/common/CMakeLists.txt b/src/common/CMakeLists.txt index fc2ed9999..8e51104a1 100644 --- a/src/common/CMakeLists.txt +++ b/src/common/CMakeLists.txt @@ -141,7 +141,6 @@ add_library(common STATIC microprofile.h microprofileui.h misc.cpp - multi_level_queue.h page_table.cpp page_table.h param_package.cpp diff --git a/src/common/multi_level_queue.h b/src/common/multi_level_queue.h deleted file mode 100644 index 4b305bf40..000000000 --- a/src/common/multi_level_queue.h +++ /dev/null @@ -1,345 +0,0 @@ -// Copyright 2019 TuxSH -// Licensed under GPLv2 or any later version -// Refer to the license.txt file included. - -#pragma once - -#include -#include -#include -#include - -#include "common/bit_util.h" -#include "common/common_types.h" - -namespace Common { - -/** - * A MultiLevelQueue is a type of priority queue which has the following characteristics: - * - iteratable through each of its elements. - * - back can be obtained. - * - O(1) add, lookup (both front and back) - * - discrete priorities and a max of 64 priorities (limited domain) - * This type of priority queue is normaly used for managing threads within an scheduler - */ -template -class MultiLevelQueue { -public: - using value_type = T; - using reference = value_type&; - using const_reference = const value_type&; - using pointer = value_type*; - using const_pointer = const value_type*; - - using difference_type = typename std::pointer_traits::difference_type; - using size_type = std::size_t; - - template - class iterator_impl { - public: - using iterator_category = std::bidirectional_iterator_tag; - using value_type = T; - using pointer = std::conditional_t; - using reference = std::conditional_t; - using difference_type = typename std::pointer_traits::difference_type; - - friend bool operator==(const iterator_impl& lhs, const iterator_impl& rhs) { - if (lhs.IsEnd() && rhs.IsEnd()) - return true; - return std::tie(lhs.current_priority, lhs.it) == std::tie(rhs.current_priority, rhs.it); - } - - friend bool operator!=(const iterator_impl& lhs, const iterator_impl& rhs) { - return !operator==(lhs, rhs); - } - - reference operator*() const { - return *it; - } - - pointer operator->() const { - return it.operator->(); - } - - iterator_impl& operator++() { - if (IsEnd()) { - return *this; - } - - ++it; - - if (it == GetEndItForPrio()) { - u64 prios = mlq.used_priorities; - prios &= ~((1ULL << (current_priority + 1)) - 1); - if (prios == 0) { - current_priority = static_cast(mlq.depth()); - } else { - current_priority = CountTrailingZeroes64(prios); - it = GetBeginItForPrio(); - } - } - return *this; - } - - iterator_impl& operator--() { - if (IsEnd()) { - if (mlq.used_priorities != 0) { - current_priority = 63 - CountLeadingZeroes64(mlq.used_priorities); - it = GetEndItForPrio(); - --it; - } - } else if (it == GetBeginItForPrio()) { - u64 prios = mlq.used_priorities; - prios &= (1ULL << current_priority) - 1; - if (prios != 0) { - current_priority = CountTrailingZeroes64(prios); - it = GetEndItForPrio(); - --it; - } - } else { - --it; - } - return *this; - } - - iterator_impl operator++(int) { - const iterator_impl v{*this}; - ++(*this); - return v; - } - - iterator_impl operator--(int) { - const iterator_impl v{*this}; - --(*this); - return v; - } - - // allow implicit const->non-const - iterator_impl(const iterator_impl& other) - : mlq(other.mlq), it(other.it), current_priority(other.current_priority) {} - - iterator_impl(const iterator_impl& other) - : mlq(other.mlq), it(other.it), current_priority(other.current_priority) {} - - iterator_impl& operator=(const iterator_impl& other) { - mlq = other.mlq; - it = other.it; - current_priority = other.current_priority; - return *this; - } - - friend class iterator_impl; - iterator_impl() = default; - - private: - friend class MultiLevelQueue; - using container_ref = - std::conditional_t; - using list_iterator = std::conditional_t::const_iterator, - typename std::list::iterator>; - - explicit iterator_impl(container_ref mlq, list_iterator it, u32 current_priority) - : mlq(mlq), it(it), current_priority(current_priority) {} - explicit iterator_impl(container_ref mlq, u32 current_priority) - : mlq(mlq), it(), current_priority(current_priority) {} - - bool IsEnd() const { - return current_priority == mlq.depth(); - } - - list_iterator GetBeginItForPrio() const { - return mlq.levels[current_priority].begin(); - } - - list_iterator GetEndItForPrio() const { - return mlq.levels[current_priority].end(); - } - - container_ref mlq; - list_iterator it; - u32 current_priority; - }; - - using iterator = iterator_impl; - using const_iterator = iterator_impl; - - void add(const T& element, u32 priority, bool send_back = true) { - if (send_back) - levels[priority].push_back(element); - else - levels[priority].push_front(element); - used_priorities |= 1ULL << priority; - } - - void remove(const T& element, u32 priority) { - auto it = ListIterateTo(levels[priority], element); - if (it == levels[priority].end()) - return; - levels[priority].erase(it); - if (levels[priority].empty()) { - used_priorities &= ~(1ULL << priority); - } - } - - void adjust(const T& element, u32 old_priority, u32 new_priority, bool adjust_front = false) { - remove(element, old_priority); - add(element, new_priority, !adjust_front); - } - void adjust(const_iterator it, u32 old_priority, u32 new_priority, bool adjust_front = false) { - adjust(*it, old_priority, new_priority, adjust_front); - } - - void transfer_to_front(const T& element, u32 priority, MultiLevelQueue& other) { - ListSplice(other.levels[priority], other.levels[priority].begin(), levels[priority], - ListIterateTo(levels[priority], element)); - - other.used_priorities |= 1ULL << priority; - - if (levels[priority].empty()) { - used_priorities &= ~(1ULL << priority); - } - } - - void transfer_to_front(const_iterator it, u32 priority, MultiLevelQueue& other) { - transfer_to_front(*it, priority, other); - } - - void transfer_to_back(const T& element, u32 priority, MultiLevelQueue& other) { - ListSplice(other.levels[priority], other.levels[priority].end(), levels[priority], - ListIterateTo(levels[priority], element)); - - other.used_priorities |= 1ULL << priority; - - if (levels[priority].empty()) { - used_priorities &= ~(1ULL << priority); - } - } - - void transfer_to_back(const_iterator it, u32 priority, MultiLevelQueue& other) { - transfer_to_back(*it, priority, other); - } - - void yield(u32 priority, std::size_t n = 1) { - ListShiftForward(levels[priority], n); - } - - [[nodiscard]] std::size_t depth() const { - return Depth; - } - - [[nodiscard]] std::size_t size(u32 priority) const { - return levels[priority].size(); - } - - [[nodiscard]] std::size_t size() const { - u64 priorities = used_priorities; - std::size_t size = 0; - while (priorities != 0) { - const u64 current_priority = CountTrailingZeroes64(priorities); - size += levels[current_priority].size(); - priorities &= ~(1ULL << current_priority); - } - return size; - } - - [[nodiscard]] bool empty() const { - return used_priorities == 0; - } - - [[nodiscard]] bool empty(u32 priority) const { - return (used_priorities & (1ULL << priority)) == 0; - } - - [[nodiscard]] u32 highest_priority_set(u32 max_priority = 0) const { - const u64 priorities = - max_priority == 0 ? used_priorities : (used_priorities & ~((1ULL << max_priority) - 1)); - return priorities == 0 ? Depth : static_cast(CountTrailingZeroes64(priorities)); - } - - [[nodiscard]] u32 lowest_priority_set(u32 min_priority = Depth - 1) const { - const u64 priorities = min_priority >= Depth - 1 - ? used_priorities - : (used_priorities & ((1ULL << (min_priority + 1)) - 1)); - return priorities == 0 ? Depth : 63 - CountLeadingZeroes64(priorities); - } - - [[nodiscard]] const_iterator cbegin(u32 max_prio = 0) const { - const u32 priority = highest_priority_set(max_prio); - return priority == Depth ? cend() - : const_iterator{*this, levels[priority].cbegin(), priority}; - } - [[nodiscard]] const_iterator begin(u32 max_prio = 0) const { - return cbegin(max_prio); - } - [[nodiscard]] iterator begin(u32 max_prio = 0) { - const u32 priority = highest_priority_set(max_prio); - return priority == Depth ? end() : iterator{*this, levels[priority].begin(), priority}; - } - - [[nodiscard]] const_iterator cend(u32 min_prio = Depth - 1) const { - return min_prio == Depth - 1 ? const_iterator{*this, Depth} : cbegin(min_prio + 1); - } - [[nodiscard]] const_iterator end(u32 min_prio = Depth - 1) const { - return cend(min_prio); - } - [[nodiscard]] iterator end(u32 min_prio = Depth - 1) { - return min_prio == Depth - 1 ? iterator{*this, Depth} : begin(min_prio + 1); - } - - [[nodiscard]] T& front(u32 max_priority = 0) { - const u32 priority = highest_priority_set(max_priority); - return levels[priority == Depth ? 0 : priority].front(); - } - [[nodiscard]] const T& front(u32 max_priority = 0) const { - const u32 priority = highest_priority_set(max_priority); - return levels[priority == Depth ? 0 : priority].front(); - } - - [[nodiscard]] T& back(u32 min_priority = Depth - 1) { - const u32 priority = lowest_priority_set(min_priority); // intended - return levels[priority == Depth ? 63 : priority].back(); - } - [[nodiscard]] const T& back(u32 min_priority = Depth - 1) const { - const u32 priority = lowest_priority_set(min_priority); // intended - return levels[priority == Depth ? 63 : priority].back(); - } - - void clear() { - used_priorities = 0; - for (std::size_t i = 0; i < Depth; i++) { - levels[i].clear(); - } - } - -private: - using const_list_iterator = typename std::list::const_iterator; - - static void ListShiftForward(std::list& list, const std::size_t shift = 1) { - if (shift >= list.size()) { - return; - } - - const auto begin_range = list.begin(); - const auto end_range = std::next(begin_range, shift); - list.splice(list.end(), list, begin_range, end_range); - } - - static void ListSplice(std::list& in_list, const_list_iterator position, - std::list& out_list, const_list_iterator element) { - in_list.splice(position, out_list, element); - } - - [[nodiscard]] static const_list_iterator ListIterateTo(const std::list& list, - const T& element) { - auto it = list.cbegin(); - while (it != list.cend() && *it != element) { - ++it; - } - return it; - } - - std::array, Depth> levels; - u64 used_priorities = 0; -}; - -} // namespace Common -- cgit v1.2.3 From d2c0c94f0b76fbae9dd5a7b7cb81b9b53db8be0e Mon Sep 17 00:00:00 2001 From: bunnei Date: Fri, 4 Dec 2020 23:46:37 -0800 Subject: common: BitSet: Various style fixes based on code review feedback. --- src/common/bit_set.h | 45 ++++++++++++++++++++++----------------------- 1 file changed, 22 insertions(+), 23 deletions(-) (limited to 'src/common') diff --git a/src/common/bit_set.h b/src/common/bit_set.h index 4f966de40..9235ad412 100644 --- a/src/common/bit_set.h +++ b/src/common/bit_set.h @@ -16,6 +16,9 @@ #pragma once +#include +#include + #include "common/alignment.h" #include "common/bit_util.h" #include "common/common_types.h" @@ -24,33 +27,11 @@ namespace Common { namespace impl { -#define BITSIZEOF(x) (sizeof(x) * CHAR_BIT) - template class BitSet { -private: - static_assert(std::is_integral::value); - static_assert(std::is_unsigned::value); - static_assert(sizeof(Storage) <= sizeof(u64)); - - static constexpr size_t FlagsPerWord = BITSIZEOF(Storage); - static constexpr size_t NumWords = AlignUp(N, FlagsPerWord) / FlagsPerWord; - - static constexpr auto CountLeadingZeroImpl(Storage word) { - return CountLeadingZeroes64(static_cast(word)) - - (BITSIZEOF(unsigned long long) - FlagsPerWord); - } - - static constexpr Storage GetBitMask(size_t bit) { - return Storage(1) << (FlagsPerWord - 1 - bit); - } - -private: - Storage words[NumWords]; public: - constexpr BitSet() : words() { /* ... */ - } + constexpr BitSet() = default; constexpr void SetBit(size_t i) { this->words[i / FlagsPerWord] |= GetBitMask(i % FlagsPerWord); @@ -81,6 +62,24 @@ public: } return FlagsPerWord * NumWords; } + +private: + static_assert(std::is_unsigned_v); + static_assert(sizeof(Storage) <= sizeof(u64)); + + static constexpr size_t FlagsPerWord = BitSize(); + static constexpr size_t NumWords = AlignUp(N, FlagsPerWord) / FlagsPerWord; + + static constexpr auto CountLeadingZeroImpl(Storage word) { + return std::countl_zero(static_cast(word)) - + (BitSize() - FlagsPerWord); + } + + static constexpr Storage GetBitMask(size_t bit) { + return Storage(1) << (FlagsPerWord - 1 - bit); + } + + std::array words{}; }; } // namespace impl -- cgit v1.2.3 From 0e54aa17e64a5c5b62d60a70414742be29eccff9 Mon Sep 17 00:00:00 2001 From: Lioncash Date: Tue, 8 Dec 2020 18:36:49 -0500 Subject: file_util: Migrate Exists() and IsDirectory() over to std::filesystem Greatly simplifies our file-handling code for these functions. --- src/common/file_util.cpp | 61 +++++++----------------------------------------- src/common/file_util.h | 9 +++---- 2 files changed, 13 insertions(+), 57 deletions(-) (limited to 'src/common') diff --git a/src/common/file_util.cpp b/src/common/file_util.cpp index 18fbfa25b..67fcef3e4 100644 --- a/src/common/file_util.cpp +++ b/src/common/file_util.cpp @@ -3,6 +3,7 @@ // Refer to the license.txt file included. #include +#include #include #include #include @@ -75,62 +76,16 @@ // The code still needs a ton of cleanup. // REMEMBER: strdup considered harmful! namespace Common::FS { +namespace fs = std::filesystem; -// Remove any ending forward slashes from directory paths -// Modifies argument. -static void StripTailDirSlashes(std::string& fname) { - if (fname.length() <= 1) { - return; - } - - std::size_t i = fname.length(); - while (i > 0 && fname[i - 1] == DIR_SEP_CHR) { - --i; - } - fname.resize(i); +bool Exists(const fs::path& path) { + std::error_code ec; + return fs::exists(path, ec); } -bool Exists(const std::string& filename) { - struct stat file_info; - - std::string copy(filename); - StripTailDirSlashes(copy); - -#ifdef _WIN32 - // Windows needs a slash to identify a driver root - if (copy.size() != 0 && copy.back() == ':') - copy += DIR_SEP_CHR; - - int result = _wstat64(Common::UTF8ToUTF16W(copy).c_str(), &file_info); -#else - int result = stat(copy.c_str(), &file_info); -#endif - - return (result == 0); -} - -bool IsDirectory(const std::string& filename) { - struct stat file_info; - - std::string copy(filename); - StripTailDirSlashes(copy); - -#ifdef _WIN32 - // Windows needs a slash to identify a driver root - if (copy.size() != 0 && copy.back() == ':') - copy += DIR_SEP_CHR; - - int result = _wstat64(Common::UTF8ToUTF16W(copy).c_str(), &file_info); -#else - int result = stat(copy.c_str(), &file_info); -#endif - - if (result < 0) { - LOG_DEBUG(Common_Filesystem, "stat failed on {}: {}", filename, GetLastErrorMsg()); - return false; - } - - return S_ISDIR(file_info.st_mode); +bool IsDirectory(const fs::path& path) { + std::error_code ec; + return fs::is_directory(path, ec); } bool Delete(const std::string& filename) { diff --git a/src/common/file_util.h b/src/common/file_util.h index 840cde2a6..be0906434 100644 --- a/src/common/file_util.h +++ b/src/common/file_util.h @@ -6,6 +6,7 @@ #include #include +#include #include #include #include @@ -47,11 +48,11 @@ struct FSTEntry { std::vector children; }; -// Returns true if file filename exists -[[nodiscard]] bool Exists(const std::string& filename); +// Returns true if the exists +[[nodiscard]] bool Exists(const std::filesystem::path& path); -// Returns true if filename is a directory -[[nodiscard]] bool IsDirectory(const std::string& filename); +// Returns true if path is a directory +[[nodiscard]] bool IsDirectory(const std::filesystem::path& path); // Returns the size of filename (64bit) [[nodiscard]] u64 GetSize(const std::string& filename); -- cgit v1.2.3 From 20aad9e01af958b6c8bf7d18faebd498541651f5 Mon Sep 17 00:00:00 2001 From: Lioncash Date: Tue, 8 Dec 2020 18:51:04 -0500 Subject: file_util: Migrate remaining file handling functions over to std::filesystem Converts creation and deletion functions over to std::filesystem, simplifying our file-handling code. Notably with this, CopyDir will now function on Windows. --- src/common/file_util.cpp | 378 ++++++++++------------------------------------- src/common/file_util.h | 62 +++----- 2 files changed, 100 insertions(+), 340 deletions(-) (limited to 'src/common') diff --git a/src/common/file_util.cpp b/src/common/file_util.cpp index 67fcef3e4..d5f6ea2ee 100644 --- a/src/common/file_util.cpp +++ b/src/common/file_util.cpp @@ -68,13 +68,6 @@ #include #include -#ifndef S_ISDIR -#define S_ISDIR(m) (((m)&S_IFMT) == S_IFDIR) -#endif - -// This namespace has various generic functions related to files and paths. -// The code still needs a ton of cleanup. -// REMEMBER: strdup considered harmful! namespace Common::FS { namespace fs = std::filesystem; @@ -88,224 +81,89 @@ bool IsDirectory(const fs::path& path) { return fs::is_directory(path, ec); } -bool Delete(const std::string& filename) { - LOG_TRACE(Common_Filesystem, "file {}", filename); +bool Delete(const fs::path& path) { + LOG_TRACE(Common_Filesystem, "file {}", path.string()); // Return true because we care about the file no // being there, not the actual delete. - if (!Exists(filename)) { - LOG_DEBUG(Common_Filesystem, "{} does not exist", filename); + if (!Exists(path)) { + LOG_DEBUG(Common_Filesystem, "{} does not exist", path.string()); return true; } - // We can't delete a directory - if (IsDirectory(filename)) { - LOG_ERROR(Common_Filesystem, "Failed: {} is a directory", filename); - return false; - } + std::error_code ec; + return fs::remove(path, ec); +} -#ifdef _WIN32 - if (!DeleteFileW(Common::UTF8ToUTF16W(filename).c_str())) { - LOG_ERROR(Common_Filesystem, "DeleteFile failed on {}: {}", filename, GetLastErrorMsg()); - return false; - } -#else - if (unlink(filename.c_str()) == -1) { - LOG_ERROR(Common_Filesystem, "unlink failed on {}: {}", filename, GetLastErrorMsg()); +bool CreateDir(const fs::path& path) { + LOG_TRACE(Common_Filesystem, "directory {}", path.string()); + + std::error_code ec; + const bool success = fs::create_directory(path, ec); + + if (!success) { + LOG_ERROR(Common_Filesystem, "Unable to create directory: {}", ec.message()); return false; } -#endif return true; } -bool CreateDir(const std::string& path) { - LOG_TRACE(Common_Filesystem, "directory {}", path); -#ifdef _WIN32 - if (::CreateDirectoryW(Common::UTF8ToUTF16W(path).c_str(), nullptr)) - return true; - DWORD error = GetLastError(); - if (error == ERROR_ALREADY_EXISTS) { - LOG_DEBUG(Common_Filesystem, "CreateDirectory failed on {}: already exists", path); - return true; - } - LOG_ERROR(Common_Filesystem, "CreateDirectory failed on {}: {}", path, error); - return false; -#else - if (mkdir(path.c_str(), 0755) == 0) - return true; +bool CreateFullPath(const fs::path& path) { + LOG_TRACE(Common_Filesystem, "path {}", path.string()); - int err = errno; + std::error_code ec; + const bool success = fs::create_directories(path, ec); - if (err == EEXIST) { - LOG_DEBUG(Common_Filesystem, "mkdir failed on {}: already exists", path); - return true; + if (!success) { + LOG_ERROR(Common_Filesystem, "Unable to create full path: {}", ec.message()); + return false; } - LOG_ERROR(Common_Filesystem, "mkdir failed on {}: {}", path, strerror(err)); - return false; -#endif + return true; } -bool CreateFullPath(const std::string& fullPath) { - int panicCounter = 100; - LOG_TRACE(Common_Filesystem, "path {}", fullPath); - - if (Exists(fullPath)) { - LOG_DEBUG(Common_Filesystem, "path exists {}", fullPath); - return true; - } +bool Rename(const fs::path& src, const fs::path& dst) { + LOG_TRACE(Common_Filesystem, "{} --> {}", src.string(), dst.string()); - std::size_t position = 0; - while (true) { - // Find next sub path - position = fullPath.find(DIR_SEP_CHR, position); - - // we're done, yay! - if (position == fullPath.npos) - return true; - - // Include the '/' so the first call is CreateDir("/") rather than CreateDir("") - std::string const subPath(fullPath.substr(0, position + 1)); - if (!IsDirectory(subPath) && !CreateDir(subPath)) { - LOG_ERROR(Common, "CreateFullPath: directory creation failed"); - return false; - } - - // A safety check - panicCounter--; - if (panicCounter <= 0) { - LOG_ERROR(Common, "CreateFullPath: directory structure is too deep"); - return false; - } - position++; - } -} - -bool DeleteDir(const std::string& filename) { - LOG_TRACE(Common_Filesystem, "directory {}", filename); + std::error_code ec; + fs::rename(src, dst, ec); - // check if a directory - if (!IsDirectory(filename)) { - LOG_ERROR(Common_Filesystem, "Not a directory {}", filename); + if (ec) { + LOG_ERROR(Common_Filesystem, "Unable to rename file from {} to {}: {}", src.string(), + dst.string(), ec.message()); return false; } -#ifdef _WIN32 - if (::RemoveDirectoryW(Common::UTF8ToUTF16W(filename).c_str())) - return true; -#else - if (rmdir(filename.c_str()) == 0) - return true; -#endif - LOG_ERROR(Common_Filesystem, "failed {}: {}", filename, GetLastErrorMsg()); - - return false; -} - -bool Rename(const std::string& srcFilename, const std::string& destFilename) { - LOG_TRACE(Common_Filesystem, "{} --> {}", srcFilename, destFilename); -#ifdef _WIN32 - if (_wrename(Common::UTF8ToUTF16W(srcFilename).c_str(), - Common::UTF8ToUTF16W(destFilename).c_str()) == 0) - return true; -#else - if (rename(srcFilename.c_str(), destFilename.c_str()) == 0) - return true; -#endif - LOG_ERROR(Common_Filesystem, "failed {} --> {}: {}", srcFilename, destFilename, - GetLastErrorMsg()); - return false; + return true; } -bool Copy(const std::string& srcFilename, const std::string& destFilename) { - LOG_TRACE(Common_Filesystem, "{} --> {}", srcFilename, destFilename); -#ifdef _WIN32 - if (CopyFileW(Common::UTF8ToUTF16W(srcFilename).c_str(), - Common::UTF8ToUTF16W(destFilename).c_str(), FALSE)) - return true; +bool Copy(const fs::path& src, const fs::path& dst) { + LOG_TRACE(Common_Filesystem, "{} --> {}", src.string(), dst.string()); - LOG_ERROR(Common_Filesystem, "failed {} --> {}: {}", srcFilename, destFilename, - GetLastErrorMsg()); - return false; -#else - using CFilePointer = std::unique_ptr; - - // Open input file - CFilePointer input{fopen(srcFilename.c_str(), "rb"), std::fclose}; - if (!input) { - LOG_ERROR(Common_Filesystem, "opening input failed {} --> {}: {}", srcFilename, - destFilename, GetLastErrorMsg()); - return false; - } + std::error_code ec; + const bool success = fs::copy_file(src, dst, fs::copy_options::overwrite_existing, ec); - // open output file - CFilePointer output{fopen(destFilename.c_str(), "wb"), std::fclose}; - if (!output) { - LOG_ERROR(Common_Filesystem, "opening output failed {} --> {}: {}", srcFilename, - destFilename, GetLastErrorMsg()); + if (!success) { + LOG_ERROR(Common_Filesystem, "Unable to copy file {} to {}: {}", src.string(), dst.string(), + ec.message()); return false; } - // copy loop - std::array buffer; - while (!feof(input.get())) { - // read input - std::size_t rnum = fread(buffer.data(), sizeof(char), buffer.size(), input.get()); - if (rnum != buffer.size()) { - if (ferror(input.get()) != 0) { - LOG_ERROR(Common_Filesystem, "failed reading from source, {} --> {}: {}", - srcFilename, destFilename, GetLastErrorMsg()); - return false; - } - } - - // write output - std::size_t wnum = fwrite(buffer.data(), sizeof(char), rnum, output.get()); - if (wnum != rnum) { - LOG_ERROR(Common_Filesystem, "failed writing to output, {} --> {}: {}", srcFilename, - destFilename, GetLastErrorMsg()); - return false; - } - } - return true; -#endif } -u64 GetSize(const std::string& filename) { - if (!Exists(filename)) { - LOG_ERROR(Common_Filesystem, "failed {}: No such file", filename); - return 0; - } +u64 GetSize(const fs::path& path) { + std::error_code ec; + const auto size = fs::file_size(path, ec); - if (IsDirectory(filename)) { - LOG_ERROR(Common_Filesystem, "failed {}: is a directory", filename); + if (ec) { + LOG_ERROR(Common_Filesystem, "Unable to retrieve file size ({}): {}", path.string(), + ec.message()); return 0; } - struct stat buf; -#ifdef _WIN32 - if (_wstat64(Common::UTF8ToUTF16W(filename).c_str(), &buf) == 0) -#else - if (stat(filename.c_str(), &buf) == 0) -#endif - { - LOG_TRACE(Common_Filesystem, "{}: {}", filename, buf.st_size); - return buf.st_size; - } - - LOG_ERROR(Common_Filesystem, "Stat failed {}: {}", filename, GetLastErrorMsg()); - return 0; -} - -u64 GetSize(const int fd) { - struct stat buf; - if (fstat(fd, &buf) != 0) { - LOG_ERROR(Common_Filesystem, "GetSize: stat failed {}: {}", fd, GetLastErrorMsg()); - return 0; - } - return buf.st_size; + return size; } u64 GetSize(FILE* f) { @@ -393,132 +251,58 @@ bool ForeachDirectoryEntry(u64* num_entries_out, const std::string& directory, return true; } -u64 ScanDirectoryTree(const std::string& directory, FSTEntry& parent_entry, - unsigned int recursion) { - const auto callback = [recursion, &parent_entry](u64* num_entries_out, - const std::string& directory, - const std::string& virtual_name) -> bool { - FSTEntry entry; - entry.virtualName = virtual_name; - entry.physicalName = directory + DIR_SEP + virtual_name; - - if (IsDirectory(entry.physicalName)) { - entry.isDirectory = true; - // is a directory, lets go inside if we didn't recurse to often - if (recursion > 0) { - entry.size = ScanDirectoryTree(entry.physicalName, entry, recursion - 1); - *num_entries_out += entry.size; - } else { - entry.size = 0; - } - } else { // is a file - entry.isDirectory = false; - entry.size = GetSize(entry.physicalName); - } - (*num_entries_out)++; - - // Push into the tree - parent_entry.children.push_back(std::move(entry)); - return true; - }; - - u64 num_entries; - return ForeachDirectoryEntry(&num_entries, directory, callback) ? num_entries : 0; -} - -bool DeleteDirRecursively(const std::string& directory, unsigned int recursion) { - const auto callback = [recursion](u64*, const std::string& directory, - const std::string& virtual_name) { - const std::string new_path = directory + DIR_SEP_CHR + virtual_name; - - if (IsDirectory(new_path)) { - if (recursion == 0) { - return false; - } - return DeleteDirRecursively(new_path, recursion - 1); - } - return Delete(new_path); - }; +bool DeleteDirRecursively(const fs::path& path) { + std::error_code ec; + fs::remove_all(path, ec); - if (!ForeachDirectoryEntry(nullptr, directory, callback)) + if (ec) { + LOG_ERROR(Common_Filesystem, "Unable to completely delete directory {}: {}", path.string(), + ec.message()); return false; + } - // Delete the outermost directory - DeleteDir(directory); return true; } -void CopyDir([[maybe_unused]] const std::string& source_path, - [[maybe_unused]] const std::string& dest_path) { -#ifndef _WIN32 - if (source_path == dest_path) { - return; - } - if (!Exists(source_path)) { - return; - } - if (!Exists(dest_path)) { - CreateFullPath(dest_path); - } +void CopyDir(const fs::path& src, const fs::path& dst) { + constexpr auto copy_flags = fs::copy_options::skip_existing | fs::copy_options::recursive; + + std::error_code ec; + fs::copy(src, dst, copy_flags, ec); - DIR* dirp = opendir(source_path.c_str()); - if (!dirp) { + if (ec) { + LOG_ERROR(Common_Filesystem, "Error copying directory {} to {}: {}", src.string(), + dst.string(), ec.message()); return; } - while (struct dirent* result = readdir(dirp)) { - const std::string virtualName(result->d_name); - // check for "." and ".." - if (((virtualName[0] == '.') && (virtualName[1] == '\0')) || - ((virtualName[0] == '.') && (virtualName[1] == '.') && (virtualName[2] == '\0'))) { - continue; - } - - std::string source, dest; - source = source_path + virtualName; - dest = dest_path + virtualName; - if (IsDirectory(source)) { - source += '/'; - dest += '/'; - if (!Exists(dest)) { - CreateFullPath(dest); - } - CopyDir(source, dest); - } else if (!Exists(dest)) { - Copy(source, dest); - } - } - closedir(dirp); -#endif + LOG_TRACE(Common_Filesystem, "Successfully copied directory."); } -std::optional GetCurrentDir() { -// Get the current working directory (getcwd uses malloc) -#ifdef _WIN32 - wchar_t* dir = _wgetcwd(nullptr, 0); - if (!dir) { -#else - char* dir = getcwd(nullptr, 0); - if (!dir) { -#endif - LOG_ERROR(Common_Filesystem, "GetCurrentDirectory failed: {}", GetLastErrorMsg()); +std::optional GetCurrentDir() { + std::error_code ec; + auto path = fs::current_path(ec); + + if (ec) { + LOG_ERROR(Common_Filesystem, "Unable to retrieve current working directory: {}", + ec.message()); return std::nullopt; } -#ifdef _WIN32 - std::string strDir = Common::UTF16ToUTF8(dir); -#else - std::string strDir = dir; -#endif - free(dir); - return strDir; + + return {std::move(path)}; } -bool SetCurrentDir(const std::string& directory) { -#ifdef _WIN32 - return _wchdir(Common::UTF8ToUTF16W(directory).c_str()) == 0; -#else - return chdir(directory.c_str()) == 0; -#endif +bool SetCurrentDir(const fs::path& path) { + std::error_code ec; + fs::current_path(path, ec); + + if (ec) { + LOG_ERROR(Common_Filesystem, "Unable to set {} as working directory: {}", path.string(), + ec.message()); + return false; + } + + return true; } #if defined(__APPLE__) diff --git a/src/common/file_util.h b/src/common/file_util.h index be0906434..c2ee7ca27 100644 --- a/src/common/file_util.h +++ b/src/common/file_util.h @@ -39,48 +39,34 @@ enum class UserPath { UserDir, }; -// FileSystem tree node/ -struct FSTEntry { - bool isDirectory; - u64 size; // file length or number of entries from children - std::string physicalName; // name on disk - std::string virtualName; // name in FST names table - std::vector children; -}; - -// Returns true if the exists +// Returns true if the path exists [[nodiscard]] bool Exists(const std::filesystem::path& path); // Returns true if path is a directory [[nodiscard]] bool IsDirectory(const std::filesystem::path& path); // Returns the size of filename (64bit) -[[nodiscard]] u64 GetSize(const std::string& filename); - -// Overloaded GetSize, accepts file descriptor -[[nodiscard]] u64 GetSize(int fd); +[[nodiscard]] u64 GetSize(const std::filesystem::path& path); // Overloaded GetSize, accepts FILE* [[nodiscard]] u64 GetSize(FILE* f); // Returns true if successful, or path already exists. -bool CreateDir(const std::string& filename); - -// Creates the full path of fullPath returns true on success -bool CreateFullPath(const std::string& fullPath); +bool CreateDir(const std::filesystem::path& path); -// Deletes a given filename, return true on success -// Doesn't supports deleting a directory -bool Delete(const std::string& filename); +// Creates the full path of path. Returns true on success +bool CreateFullPath(const std::filesystem::path& path); -// Deletes a directory filename, returns true on success -bool DeleteDir(const std::string& filename); +// Deletes a given file at the path. +// This will also delete empty directories. +// Return true on success +bool Delete(const std::filesystem::path& path); -// renames file srcFilename to destFilename, returns true on success -bool Rename(const std::string& srcFilename, const std::string& destFilename); +// Renames file src to dst, returns true on success +bool Rename(const std::filesystem::path& src, const std::filesystem::path& dst); -// copies file srcFilename to destFilename, returns true on success -bool Copy(const std::string& srcFilename, const std::string& destFilename); +// copies file src to dst, returns true on success +bool Copy(const std::filesystem::path& src, const std::filesystem::path& dst); // creates an empty file filename, returns true on success bool CreateEmptyFile(const std::string& filename); @@ -107,27 +93,17 @@ using DirectoryEntryCallable = std::function GetCurrentDir(); +[[nodiscard]] std::optional GetCurrentDir(); // Create directory and copy contents (does not overwrite existing files) -void CopyDir(const std::string& source_path, const std::string& dest_path); +void CopyDir(const std::filesystem::path& src, const std::filesystem::path& dst); -// Set the current directory to given directory -bool SetCurrentDir(const std::string& directory); +// Set the current directory to given path +bool SetCurrentDir(const std::filesystem::path& path); // Returns a pointer to a string with a yuzu data dir in the user's home // directory. To be used in "multi-user" mode (that is, installed). -- cgit v1.2.3 From 52f13f2339f8c7259d2aa646346b29997b85c0ec Mon Sep 17 00:00:00 2001 From: ReinUsesLisp Date: Wed, 9 Dec 2020 05:21:08 -0300 Subject: common/file_util: Succeed on CreateDir when the directory exists --- src/common/file_util.cpp | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'src/common') diff --git a/src/common/file_util.cpp b/src/common/file_util.cpp index d5f6ea2ee..c4d738bb6 100644 --- a/src/common/file_util.cpp +++ b/src/common/file_util.cpp @@ -98,6 +98,11 @@ bool Delete(const fs::path& path) { bool CreateDir(const fs::path& path) { LOG_TRACE(Common_Filesystem, "directory {}", path.string()); + if (Exists(path)) { + LOG_DEBUG(Common_Filesystem, "path exists {}", path.string()); + return true; + } + std::error_code ec; const bool success = fs::create_directory(path, ec); -- cgit v1.2.3 From 532983437616e15539b448594a360c138995e282 Mon Sep 17 00:00:00 2001 From: ReinUsesLisp Date: Wed, 9 Dec 2020 05:22:16 -0300 Subject: common/file_util: Fix and deprecate CreateFullPath, add CreateDirs Fix CreateFullPath to have its intended previous behavior (whatever that was), and deprecate it in favor of the new CreateDirs function. Unlike CreateDir, CreateDirs is marked as [[nodiscard]] to avoid new code ignoring its result value. --- src/common/file_util.cpp | 25 +++++++++++++++++++++++-- src/common/file_util.h | 10 ++++++++-- 2 files changed, 31 insertions(+), 4 deletions(-) (limited to 'src/common') diff --git a/src/common/file_util.cpp b/src/common/file_util.cpp index c4d738bb6..7752c0421 100644 --- a/src/common/file_util.cpp +++ b/src/common/file_util.cpp @@ -114,20 +114,41 @@ bool CreateDir(const fs::path& path) { return true; } -bool CreateFullPath(const fs::path& path) { +bool CreateDirs(const fs::path& path) { LOG_TRACE(Common_Filesystem, "path {}", path.string()); + if (Exists(path)) { + LOG_DEBUG(Common_Filesystem, "path exists {}", path.string()); + return true; + } + std::error_code ec; const bool success = fs::create_directories(path, ec); if (!success) { - LOG_ERROR(Common_Filesystem, "Unable to create full path: {}", ec.message()); + LOG_ERROR(Common_Filesystem, "Unable to create directories: {}", ec.message()); return false; } return true; } +bool CreateFullPath(const fs::path& path) { + LOG_TRACE(Common_Filesystem, "path {}", path); + + // Removes trailing slashes and turns any '\' into '/' + const auto new_path = SanitizePath(path.string(), DirectorySeparator::ForwardSlash); + + if (new_path.rfind('.') == std::string::npos) { + // The path is a directory + return CreateDirs(new_path); + } else { + // The path is a file + // Creates directory preceding the last '/' + return CreateDirs(new_path.substr(0, new_path.rfind('/'))); + } +} + bool Rename(const fs::path& src, const fs::path& dst) { LOG_TRACE(Common_Filesystem, "{} --> {}", src.string(), dst.string()); diff --git a/src/common/file_util.h b/src/common/file_util.h index c2ee7ca27..cf006cc9d 100644 --- a/src/common/file_util.h +++ b/src/common/file_util.h @@ -54,8 +54,14 @@ enum class UserPath { // Returns true if successful, or path already exists. bool CreateDir(const std::filesystem::path& path); -// Creates the full path of path. Returns true on success -bool CreateFullPath(const std::filesystem::path& path); +// Create all directories in path +// Returns true if successful, or path already exists. +[[nodiscard("Directory creation can fail and must be tested")]] bool CreateDirs( + const std::filesystem::path& path); + +// Creates directories in path. Returns true on success. +[[deprecated("This function is deprecated, use CreateDirs")]] bool CreateFullPath( + const std::filesystem::path& path); // Deletes a given file at the path. // This will also delete empty directories. -- cgit v1.2.3 From bab9cae71f5cd5a0078853e64bdf1d1f8af1fc30 Mon Sep 17 00:00:00 2001 From: ReinUsesLisp Date: Wed, 9 Dec 2020 18:52:36 -0300 Subject: common/file_util: Let std::filesystem cast from UTF16 to std::string Fix invalid encoding paths when iterating over a directory on Windows. --- src/common/file_util.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/common') diff --git a/src/common/file_util.cpp b/src/common/file_util.cpp index 7752c0421..a286b9341 100644 --- a/src/common/file_util.cpp +++ b/src/common/file_util.cpp @@ -239,7 +239,7 @@ bool ForeachDirectoryEntry(u64* num_entries_out, const std::string& directory, } // windows loop do { - const std::string virtual_name(Common::UTF16ToUTF8(ffd.cFileName)); + const std::string virtual_name = std::filesystem::path(ffd.cFileName).string(); #else DIR* dirp = opendir(directory.c_str()); if (!dirp) -- cgit v1.2.3 From ec8548b414d39bf389ca55154e383ca5574344e7 Mon Sep 17 00:00:00 2001 From: Morph Date: Wed, 9 Dec 2020 19:25:00 -0500 Subject: common/file_util: Simplify the behavior of CreateFullPath --- src/common/file_util.cpp | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) (limited to 'src/common') diff --git a/src/common/file_util.cpp b/src/common/file_util.cpp index a286b9341..8e061ff6c 100644 --- a/src/common/file_util.cpp +++ b/src/common/file_util.cpp @@ -136,16 +136,10 @@ bool CreateDirs(const fs::path& path) { bool CreateFullPath(const fs::path& path) { LOG_TRACE(Common_Filesystem, "path {}", path); - // Removes trailing slashes and turns any '\' into '/' - const auto new_path = SanitizePath(path.string(), DirectorySeparator::ForwardSlash); - - if (new_path.rfind('.') == std::string::npos) { - // The path is a directory - return CreateDirs(new_path); + if (path.has_extension()) { + return CreateDirs(path.parent_path()); } else { - // The path is a file - // Creates directory preceding the last '/' - return CreateDirs(new_path.substr(0, new_path.rfind('/'))); + return CreateDirs(path); } } -- cgit v1.2.3 From ac3ec5ed132a8d8b63c4e95205521addb90fdd0f Mon Sep 17 00:00:00 2001 From: Morph Date: Fri, 11 Dec 2020 20:21:21 -0500 Subject: Revert "Merge pull request #5181 from Morph1984/5174-review" This reverts commit cdb36aef9ec9d30bdef1953f9ed46776ae2f12af, reversing changes made to 5e9b77129f2cf8c039a8d98033cae4ac0f93f515. --- src/common/file_util.cpp | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) (limited to 'src/common') diff --git a/src/common/file_util.cpp b/src/common/file_util.cpp index 8e061ff6c..a286b9341 100644 --- a/src/common/file_util.cpp +++ b/src/common/file_util.cpp @@ -136,10 +136,16 @@ bool CreateDirs(const fs::path& path) { bool CreateFullPath(const fs::path& path) { LOG_TRACE(Common_Filesystem, "path {}", path); - if (path.has_extension()) { - return CreateDirs(path.parent_path()); + // Removes trailing slashes and turns any '\' into '/' + const auto new_path = SanitizePath(path.string(), DirectorySeparator::ForwardSlash); + + if (new_path.rfind('.') == std::string::npos) { + // The path is a directory + return CreateDirs(new_path); } else { - return CreateDirs(path); + // The path is a file + // Creates directory preceding the last '/' + return CreateDirs(new_path.substr(0, new_path.rfind('/'))); } } -- cgit v1.2.3 From 0195038c07f88bb62fd492559bbc264a71c3ac7a Mon Sep 17 00:00:00 2001 From: Morph Date: Fri, 11 Dec 2020 20:21:46 -0500 Subject: Revert "Merge pull request #5179 from ReinUsesLisp/fs-path" This reverts commit 4e94d0d53af2cdb7b03ef9de23cc29f3565df97a, reversing changes made to 6d6115475b4edccdf1bb4e96ecc3d3b1be319e76. --- src/common/file_util.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/common') diff --git a/src/common/file_util.cpp b/src/common/file_util.cpp index a286b9341..7752c0421 100644 --- a/src/common/file_util.cpp +++ b/src/common/file_util.cpp @@ -239,7 +239,7 @@ bool ForeachDirectoryEntry(u64* num_entries_out, const std::string& directory, } // windows loop do { - const std::string virtual_name = std::filesystem::path(ffd.cFileName).string(); + const std::string virtual_name(Common::UTF16ToUTF8(ffd.cFileName)); #else DIR* dirp = opendir(directory.c_str()); if (!dirp) -- cgit v1.2.3 From 8941cdb7d2355ba0f3084b306165923520f9db65 Mon Sep 17 00:00:00 2001 From: Morph Date: Fri, 11 Dec 2020 20:22:01 -0500 Subject: Revert "Merge pull request #5174 from ReinUsesLisp/fs-fix" This reverts commit 5fe55b16a11d9ec607fb8a3fdddc77a4393cd96a, reversing changes made to e94dd7e2c4fc3f7ca2c15c01bdc301be2b8a4c1b. --- src/common/file_util.cpp | 30 ++---------------------------- src/common/file_util.h | 10 ++-------- 2 files changed, 4 insertions(+), 36 deletions(-) (limited to 'src/common') diff --git a/src/common/file_util.cpp b/src/common/file_util.cpp index 7752c0421..d5f6ea2ee 100644 --- a/src/common/file_util.cpp +++ b/src/common/file_util.cpp @@ -98,11 +98,6 @@ bool Delete(const fs::path& path) { bool CreateDir(const fs::path& path) { LOG_TRACE(Common_Filesystem, "directory {}", path.string()); - if (Exists(path)) { - LOG_DEBUG(Common_Filesystem, "path exists {}", path.string()); - return true; - } - std::error_code ec; const bool success = fs::create_directory(path, ec); @@ -114,41 +109,20 @@ bool CreateDir(const fs::path& path) { return true; } -bool CreateDirs(const fs::path& path) { +bool CreateFullPath(const fs::path& path) { LOG_TRACE(Common_Filesystem, "path {}", path.string()); - if (Exists(path)) { - LOG_DEBUG(Common_Filesystem, "path exists {}", path.string()); - return true; - } - std::error_code ec; const bool success = fs::create_directories(path, ec); if (!success) { - LOG_ERROR(Common_Filesystem, "Unable to create directories: {}", ec.message()); + LOG_ERROR(Common_Filesystem, "Unable to create full path: {}", ec.message()); return false; } return true; } -bool CreateFullPath(const fs::path& path) { - LOG_TRACE(Common_Filesystem, "path {}", path); - - // Removes trailing slashes and turns any '\' into '/' - const auto new_path = SanitizePath(path.string(), DirectorySeparator::ForwardSlash); - - if (new_path.rfind('.') == std::string::npos) { - // The path is a directory - return CreateDirs(new_path); - } else { - // The path is a file - // Creates directory preceding the last '/' - return CreateDirs(new_path.substr(0, new_path.rfind('/'))); - } -} - bool Rename(const fs::path& src, const fs::path& dst) { LOG_TRACE(Common_Filesystem, "{} --> {}", src.string(), dst.string()); diff --git a/src/common/file_util.h b/src/common/file_util.h index cf006cc9d..c2ee7ca27 100644 --- a/src/common/file_util.h +++ b/src/common/file_util.h @@ -54,14 +54,8 @@ enum class UserPath { // Returns true if successful, or path already exists. bool CreateDir(const std::filesystem::path& path); -// Create all directories in path -// Returns true if successful, or path already exists. -[[nodiscard("Directory creation can fail and must be tested")]] bool CreateDirs( - const std::filesystem::path& path); - -// Creates directories in path. Returns true on success. -[[deprecated("This function is deprecated, use CreateDirs")]] bool CreateFullPath( - const std::filesystem::path& path); +// Creates the full path of path. Returns true on success +bool CreateFullPath(const std::filesystem::path& path); // Deletes a given file at the path. // This will also delete empty directories. -- cgit v1.2.3 From 4de079b256ce320c7e261a2c35b703e477e46fa5 Mon Sep 17 00:00:00 2001 From: Morph Date: Fri, 11 Dec 2020 20:22:18 -0500 Subject: Revert "Merge pull request #5173 from lioncash/common-fs" This reverts commit ce5fcb6bb2c358b0251a2ce87945bda52789a76d, reversing changes made to 6f41763061082d5fa2ab039c554427152243cb46. --- src/common/file_util.cpp | 439 +++++++++++++++++++++++++++++++++++++---------- src/common/file_util.h | 69 +++++--- 2 files changed, 396 insertions(+), 112 deletions(-) (limited to 'src/common') diff --git a/src/common/file_util.cpp b/src/common/file_util.cpp index d5f6ea2ee..18fbfa25b 100644 --- a/src/common/file_util.cpp +++ b/src/common/file_util.cpp @@ -3,7 +3,6 @@ // Refer to the license.txt file included. #include -#include #include #include #include @@ -68,102 +67,290 @@ #include #include +#ifndef S_ISDIR +#define S_ISDIR(m) (((m)&S_IFMT) == S_IFDIR) +#endif + +// This namespace has various generic functions related to files and paths. +// The code still needs a ton of cleanup. +// REMEMBER: strdup considered harmful! namespace Common::FS { -namespace fs = std::filesystem; -bool Exists(const fs::path& path) { - std::error_code ec; - return fs::exists(path, ec); +// Remove any ending forward slashes from directory paths +// Modifies argument. +static void StripTailDirSlashes(std::string& fname) { + if (fname.length() <= 1) { + return; + } + + std::size_t i = fname.length(); + while (i > 0 && fname[i - 1] == DIR_SEP_CHR) { + --i; + } + fname.resize(i); } -bool IsDirectory(const fs::path& path) { - std::error_code ec; - return fs::is_directory(path, ec); +bool Exists(const std::string& filename) { + struct stat file_info; + + std::string copy(filename); + StripTailDirSlashes(copy); + +#ifdef _WIN32 + // Windows needs a slash to identify a driver root + if (copy.size() != 0 && copy.back() == ':') + copy += DIR_SEP_CHR; + + int result = _wstat64(Common::UTF8ToUTF16W(copy).c_str(), &file_info); +#else + int result = stat(copy.c_str(), &file_info); +#endif + + return (result == 0); } -bool Delete(const fs::path& path) { - LOG_TRACE(Common_Filesystem, "file {}", path.string()); +bool IsDirectory(const std::string& filename) { + struct stat file_info; - // Return true because we care about the file no - // being there, not the actual delete. - if (!Exists(path)) { - LOG_DEBUG(Common_Filesystem, "{} does not exist", path.string()); - return true; + std::string copy(filename); + StripTailDirSlashes(copy); + +#ifdef _WIN32 + // Windows needs a slash to identify a driver root + if (copy.size() != 0 && copy.back() == ':') + copy += DIR_SEP_CHR; + + int result = _wstat64(Common::UTF8ToUTF16W(copy).c_str(), &file_info); +#else + int result = stat(copy.c_str(), &file_info); +#endif + + if (result < 0) { + LOG_DEBUG(Common_Filesystem, "stat failed on {}: {}", filename, GetLastErrorMsg()); + return false; } - std::error_code ec; - return fs::remove(path, ec); + return S_ISDIR(file_info.st_mode); } -bool CreateDir(const fs::path& path) { - LOG_TRACE(Common_Filesystem, "directory {}", path.string()); +bool Delete(const std::string& filename) { + LOG_TRACE(Common_Filesystem, "file {}", filename); - std::error_code ec; - const bool success = fs::create_directory(path, ec); + // Return true because we care about the file no + // being there, not the actual delete. + if (!Exists(filename)) { + LOG_DEBUG(Common_Filesystem, "{} does not exist", filename); + return true; + } + + // We can't delete a directory + if (IsDirectory(filename)) { + LOG_ERROR(Common_Filesystem, "Failed: {} is a directory", filename); + return false; + } - if (!success) { - LOG_ERROR(Common_Filesystem, "Unable to create directory: {}", ec.message()); +#ifdef _WIN32 + if (!DeleteFileW(Common::UTF8ToUTF16W(filename).c_str())) { + LOG_ERROR(Common_Filesystem, "DeleteFile failed on {}: {}", filename, GetLastErrorMsg()); + return false; + } +#else + if (unlink(filename.c_str()) == -1) { + LOG_ERROR(Common_Filesystem, "unlink failed on {}: {}", filename, GetLastErrorMsg()); return false; } +#endif return true; } -bool CreateFullPath(const fs::path& path) { - LOG_TRACE(Common_Filesystem, "path {}", path.string()); +bool CreateDir(const std::string& path) { + LOG_TRACE(Common_Filesystem, "directory {}", path); +#ifdef _WIN32 + if (::CreateDirectoryW(Common::UTF8ToUTF16W(path).c_str(), nullptr)) + return true; + DWORD error = GetLastError(); + if (error == ERROR_ALREADY_EXISTS) { + LOG_DEBUG(Common_Filesystem, "CreateDirectory failed on {}: already exists", path); + return true; + } + LOG_ERROR(Common_Filesystem, "CreateDirectory failed on {}: {}", path, error); + return false; +#else + if (mkdir(path.c_str(), 0755) == 0) + return true; - std::error_code ec; - const bool success = fs::create_directories(path, ec); + int err = errno; - if (!success) { - LOG_ERROR(Common_Filesystem, "Unable to create full path: {}", ec.message()); - return false; + if (err == EEXIST) { + LOG_DEBUG(Common_Filesystem, "mkdir failed on {}: already exists", path); + return true; } - return true; + LOG_ERROR(Common_Filesystem, "mkdir failed on {}: {}", path, strerror(err)); + return false; +#endif } -bool Rename(const fs::path& src, const fs::path& dst) { - LOG_TRACE(Common_Filesystem, "{} --> {}", src.string(), dst.string()); +bool CreateFullPath(const std::string& fullPath) { + int panicCounter = 100; + LOG_TRACE(Common_Filesystem, "path {}", fullPath); + + if (Exists(fullPath)) { + LOG_DEBUG(Common_Filesystem, "path exists {}", fullPath); + return true; + } + + std::size_t position = 0; + while (true) { + // Find next sub path + position = fullPath.find(DIR_SEP_CHR, position); - std::error_code ec; - fs::rename(src, dst, ec); + // we're done, yay! + if (position == fullPath.npos) + return true; - if (ec) { - LOG_ERROR(Common_Filesystem, "Unable to rename file from {} to {}: {}", src.string(), - dst.string(), ec.message()); + // Include the '/' so the first call is CreateDir("/") rather than CreateDir("") + std::string const subPath(fullPath.substr(0, position + 1)); + if (!IsDirectory(subPath) && !CreateDir(subPath)) { + LOG_ERROR(Common, "CreateFullPath: directory creation failed"); + return false; + } + + // A safety check + panicCounter--; + if (panicCounter <= 0) { + LOG_ERROR(Common, "CreateFullPath: directory structure is too deep"); + return false; + } + position++; + } +} + +bool DeleteDir(const std::string& filename) { + LOG_TRACE(Common_Filesystem, "directory {}", filename); + + // check if a directory + if (!IsDirectory(filename)) { + LOG_ERROR(Common_Filesystem, "Not a directory {}", filename); return false; } - return true; +#ifdef _WIN32 + if (::RemoveDirectoryW(Common::UTF8ToUTF16W(filename).c_str())) + return true; +#else + if (rmdir(filename.c_str()) == 0) + return true; +#endif + LOG_ERROR(Common_Filesystem, "failed {}: {}", filename, GetLastErrorMsg()); + + return false; +} + +bool Rename(const std::string& srcFilename, const std::string& destFilename) { + LOG_TRACE(Common_Filesystem, "{} --> {}", srcFilename, destFilename); +#ifdef _WIN32 + if (_wrename(Common::UTF8ToUTF16W(srcFilename).c_str(), + Common::UTF8ToUTF16W(destFilename).c_str()) == 0) + return true; +#else + if (rename(srcFilename.c_str(), destFilename.c_str()) == 0) + return true; +#endif + LOG_ERROR(Common_Filesystem, "failed {} --> {}: {}", srcFilename, destFilename, + GetLastErrorMsg()); + return false; } -bool Copy(const fs::path& src, const fs::path& dst) { - LOG_TRACE(Common_Filesystem, "{} --> {}", src.string(), dst.string()); +bool Copy(const std::string& srcFilename, const std::string& destFilename) { + LOG_TRACE(Common_Filesystem, "{} --> {}", srcFilename, destFilename); +#ifdef _WIN32 + if (CopyFileW(Common::UTF8ToUTF16W(srcFilename).c_str(), + Common::UTF8ToUTF16W(destFilename).c_str(), FALSE)) + return true; - std::error_code ec; - const bool success = fs::copy_file(src, dst, fs::copy_options::overwrite_existing, ec); + LOG_ERROR(Common_Filesystem, "failed {} --> {}: {}", srcFilename, destFilename, + GetLastErrorMsg()); + return false; +#else + using CFilePointer = std::unique_ptr; - if (!success) { - LOG_ERROR(Common_Filesystem, "Unable to copy file {} to {}: {}", src.string(), dst.string(), - ec.message()); + // Open input file + CFilePointer input{fopen(srcFilename.c_str(), "rb"), std::fclose}; + if (!input) { + LOG_ERROR(Common_Filesystem, "opening input failed {} --> {}: {}", srcFilename, + destFilename, GetLastErrorMsg()); return false; } + // open output file + CFilePointer output{fopen(destFilename.c_str(), "wb"), std::fclose}; + if (!output) { + LOG_ERROR(Common_Filesystem, "opening output failed {} --> {}: {}", srcFilename, + destFilename, GetLastErrorMsg()); + return false; + } + + // copy loop + std::array buffer; + while (!feof(input.get())) { + // read input + std::size_t rnum = fread(buffer.data(), sizeof(char), buffer.size(), input.get()); + if (rnum != buffer.size()) { + if (ferror(input.get()) != 0) { + LOG_ERROR(Common_Filesystem, "failed reading from source, {} --> {}: {}", + srcFilename, destFilename, GetLastErrorMsg()); + return false; + } + } + + // write output + std::size_t wnum = fwrite(buffer.data(), sizeof(char), rnum, output.get()); + if (wnum != rnum) { + LOG_ERROR(Common_Filesystem, "failed writing to output, {} --> {}: {}", srcFilename, + destFilename, GetLastErrorMsg()); + return false; + } + } + return true; +#endif } -u64 GetSize(const fs::path& path) { - std::error_code ec; - const auto size = fs::file_size(path, ec); +u64 GetSize(const std::string& filename) { + if (!Exists(filename)) { + LOG_ERROR(Common_Filesystem, "failed {}: No such file", filename); + return 0; + } - if (ec) { - LOG_ERROR(Common_Filesystem, "Unable to retrieve file size ({}): {}", path.string(), - ec.message()); + if (IsDirectory(filename)) { + LOG_ERROR(Common_Filesystem, "failed {}: is a directory", filename); return 0; } - return size; + struct stat buf; +#ifdef _WIN32 + if (_wstat64(Common::UTF8ToUTF16W(filename).c_str(), &buf) == 0) +#else + if (stat(filename.c_str(), &buf) == 0) +#endif + { + LOG_TRACE(Common_Filesystem, "{}: {}", filename, buf.st_size); + return buf.st_size; + } + + LOG_ERROR(Common_Filesystem, "Stat failed {}: {}", filename, GetLastErrorMsg()); + return 0; +} + +u64 GetSize(const int fd) { + struct stat buf; + if (fstat(fd, &buf) != 0) { + LOG_ERROR(Common_Filesystem, "GetSize: stat failed {}: {}", fd, GetLastErrorMsg()); + return 0; + } + return buf.st_size; } u64 GetSize(FILE* f) { @@ -251,58 +438,132 @@ bool ForeachDirectoryEntry(u64* num_entries_out, const std::string& directory, return true; } -bool DeleteDirRecursively(const fs::path& path) { - std::error_code ec; - fs::remove_all(path, ec); +u64 ScanDirectoryTree(const std::string& directory, FSTEntry& parent_entry, + unsigned int recursion) { + const auto callback = [recursion, &parent_entry](u64* num_entries_out, + const std::string& directory, + const std::string& virtual_name) -> bool { + FSTEntry entry; + entry.virtualName = virtual_name; + entry.physicalName = directory + DIR_SEP + virtual_name; + + if (IsDirectory(entry.physicalName)) { + entry.isDirectory = true; + // is a directory, lets go inside if we didn't recurse to often + if (recursion > 0) { + entry.size = ScanDirectoryTree(entry.physicalName, entry, recursion - 1); + *num_entries_out += entry.size; + } else { + entry.size = 0; + } + } else { // is a file + entry.isDirectory = false; + entry.size = GetSize(entry.physicalName); + } + (*num_entries_out)++; - if (ec) { - LOG_ERROR(Common_Filesystem, "Unable to completely delete directory {}: {}", path.string(), - ec.message()); - return false; - } + // Push into the tree + parent_entry.children.push_back(std::move(entry)); + return true; + }; - return true; + u64 num_entries; + return ForeachDirectoryEntry(&num_entries, directory, callback) ? num_entries : 0; } -void CopyDir(const fs::path& src, const fs::path& dst) { - constexpr auto copy_flags = fs::copy_options::skip_existing | fs::copy_options::recursive; +bool DeleteDirRecursively(const std::string& directory, unsigned int recursion) { + const auto callback = [recursion](u64*, const std::string& directory, + const std::string& virtual_name) { + const std::string new_path = directory + DIR_SEP_CHR + virtual_name; - std::error_code ec; - fs::copy(src, dst, copy_flags, ec); + if (IsDirectory(new_path)) { + if (recursion == 0) { + return false; + } + return DeleteDirRecursively(new_path, recursion - 1); + } + return Delete(new_path); + }; - if (ec) { - LOG_ERROR(Common_Filesystem, "Error copying directory {} to {}: {}", src.string(), - dst.string(), ec.message()); - return; - } + if (!ForeachDirectoryEntry(nullptr, directory, callback)) + return false; - LOG_TRACE(Common_Filesystem, "Successfully copied directory."); + // Delete the outermost directory + DeleteDir(directory); + return true; } -std::optional GetCurrentDir() { - std::error_code ec; - auto path = fs::current_path(ec); +void CopyDir([[maybe_unused]] const std::string& source_path, + [[maybe_unused]] const std::string& dest_path) { +#ifndef _WIN32 + if (source_path == dest_path) { + return; + } + if (!Exists(source_path)) { + return; + } + if (!Exists(dest_path)) { + CreateFullPath(dest_path); + } - if (ec) { - LOG_ERROR(Common_Filesystem, "Unable to retrieve current working directory: {}", - ec.message()); - return std::nullopt; + DIR* dirp = opendir(source_path.c_str()); + if (!dirp) { + return; } - return {std::move(path)}; -} + while (struct dirent* result = readdir(dirp)) { + const std::string virtualName(result->d_name); + // check for "." and ".." + if (((virtualName[0] == '.') && (virtualName[1] == '\0')) || + ((virtualName[0] == '.') && (virtualName[1] == '.') && (virtualName[2] == '\0'))) { + continue; + } -bool SetCurrentDir(const fs::path& path) { - std::error_code ec; - fs::current_path(path, ec); + std::string source, dest; + source = source_path + virtualName; + dest = dest_path + virtualName; + if (IsDirectory(source)) { + source += '/'; + dest += '/'; + if (!Exists(dest)) { + CreateFullPath(dest); + } + CopyDir(source, dest); + } else if (!Exists(dest)) { + Copy(source, dest); + } + } + closedir(dirp); +#endif +} - if (ec) { - LOG_ERROR(Common_Filesystem, "Unable to set {} as working directory: {}", path.string(), - ec.message()); - return false; +std::optional GetCurrentDir() { +// Get the current working directory (getcwd uses malloc) +#ifdef _WIN32 + wchar_t* dir = _wgetcwd(nullptr, 0); + if (!dir) { +#else + char* dir = getcwd(nullptr, 0); + if (!dir) { +#endif + LOG_ERROR(Common_Filesystem, "GetCurrentDirectory failed: {}", GetLastErrorMsg()); + return std::nullopt; } +#ifdef _WIN32 + std::string strDir = Common::UTF16ToUTF8(dir); +#else + std::string strDir = dir; +#endif + free(dir); + return strDir; +} - return true; +bool SetCurrentDir(const std::string& directory) { +#ifdef _WIN32 + return _wchdir(Common::UTF8ToUTF16W(directory).c_str()) == 0; +#else + return chdir(directory.c_str()) == 0; +#endif } #if defined(__APPLE__) diff --git a/src/common/file_util.h b/src/common/file_util.h index c2ee7ca27..840cde2a6 100644 --- a/src/common/file_util.h +++ b/src/common/file_util.h @@ -6,7 +6,6 @@ #include #include -#include #include #include #include @@ -39,34 +38,48 @@ enum class UserPath { UserDir, }; -// Returns true if the path exists -[[nodiscard]] bool Exists(const std::filesystem::path& path); +// FileSystem tree node/ +struct FSTEntry { + bool isDirectory; + u64 size; // file length or number of entries from children + std::string physicalName; // name on disk + std::string virtualName; // name in FST names table + std::vector children; +}; + +// Returns true if file filename exists +[[nodiscard]] bool Exists(const std::string& filename); -// Returns true if path is a directory -[[nodiscard]] bool IsDirectory(const std::filesystem::path& path); +// Returns true if filename is a directory +[[nodiscard]] bool IsDirectory(const std::string& filename); // Returns the size of filename (64bit) -[[nodiscard]] u64 GetSize(const std::filesystem::path& path); +[[nodiscard]] u64 GetSize(const std::string& filename); + +// Overloaded GetSize, accepts file descriptor +[[nodiscard]] u64 GetSize(int fd); // Overloaded GetSize, accepts FILE* [[nodiscard]] u64 GetSize(FILE* f); // Returns true if successful, or path already exists. -bool CreateDir(const std::filesystem::path& path); +bool CreateDir(const std::string& filename); + +// Creates the full path of fullPath returns true on success +bool CreateFullPath(const std::string& fullPath); -// Creates the full path of path. Returns true on success -bool CreateFullPath(const std::filesystem::path& path); +// Deletes a given filename, return true on success +// Doesn't supports deleting a directory +bool Delete(const std::string& filename); -// Deletes a given file at the path. -// This will also delete empty directories. -// Return true on success -bool Delete(const std::filesystem::path& path); +// Deletes a directory filename, returns true on success +bool DeleteDir(const std::string& filename); -// Renames file src to dst, returns true on success -bool Rename(const std::filesystem::path& src, const std::filesystem::path& dst); +// renames file srcFilename to destFilename, returns true on success +bool Rename(const std::string& srcFilename, const std::string& destFilename); -// copies file src to dst, returns true on success -bool Copy(const std::filesystem::path& src, const std::filesystem::path& dst); +// copies file srcFilename to destFilename, returns true on success +bool Copy(const std::string& srcFilename, const std::string& destFilename); // creates an empty file filename, returns true on success bool CreateEmptyFile(const std::string& filename); @@ -93,17 +106,27 @@ using DirectoryEntryCallable = std::function GetCurrentDir(); +[[nodiscard]] std::optional GetCurrentDir(); // Create directory and copy contents (does not overwrite existing files) -void CopyDir(const std::filesystem::path& src, const std::filesystem::path& dst); +void CopyDir(const std::string& source_path, const std::string& dest_path); -// Set the current directory to given path -bool SetCurrentDir(const std::filesystem::path& path); +// Set the current directory to given directory +bool SetCurrentDir(const std::string& directory); // Returns a pointer to a string with a yuzu data dir in the user's home // directory. To be used in "multi-user" mode (that is, installed). -- cgit v1.2.3 From 761206cf81b271f7f4dd6a167a120325b760dbf3 Mon Sep 17 00:00:00 2001 From: bunnei Date: Sat, 12 Dec 2020 11:50:07 -0800 Subject: common: Update CMakeList to fix build issue with Boost. --- src/common/CMakeLists.txt | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'src/common') diff --git a/src/common/CMakeLists.txt b/src/common/CMakeLists.txt index 56c7e21f5..0981fe90b 100644 --- a/src/common/CMakeLists.txt +++ b/src/common/CMakeLists.txt @@ -209,9 +209,8 @@ else() endif() create_target_directory_groups(common) -find_package(Boost 1.71 COMPONENTS context headers REQUIRED) -target_link_libraries(common PUBLIC ${Boost_LIBRARIES} fmt::fmt microprofile) +target_link_libraries(common PUBLIC Boost::boost fmt::fmt microprofile) target_link_libraries(common PRIVATE lz4::lz4 xbyak) if (MSVC) target_link_libraries(common PRIVATE zstd::zstd) -- cgit v1.2.3 From 292dd642cefb3717363e96974f055da9dc1cbf23 Mon Sep 17 00:00:00 2001 From: lat9nq Date: Sat, 12 Dec 2020 22:20:24 -0500 Subject: cmake: Fix generating CMake configs and linking with Boost Fixes regression by 761206cf81b271f7f4dd6a167a120325b760dbf3, causing yuzu to not build on Linux with any version of Boost except a cached 1.73 Conan version from before about a day ago. Moves the Boost requirement out of the `REQUIRED_LIBS` psuedo-2D-array for Conan to instead be manually configured, using Conan as a fallback solution if the system does not meet our requirements. Requires any update from the linux-fresh container in order to build. **DO NOT MERGE** until someone with the MSVC toolchain can verify this works there, too. --- src/common/CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/common') diff --git a/src/common/CMakeLists.txt b/src/common/CMakeLists.txt index 0981fe90b..0acf70a0a 100644 --- a/src/common/CMakeLists.txt +++ b/src/common/CMakeLists.txt @@ -210,7 +210,7 @@ endif() create_target_directory_groups(common) -target_link_libraries(common PUBLIC Boost::boost fmt::fmt microprofile) +target_link_libraries(common PUBLIC ${Boost_LIBRARIES} fmt::fmt microprofile) target_link_libraries(common PRIVATE lz4::lz4 xbyak) if (MSVC) target_link_libraries(common PRIVATE zstd::zstd) -- cgit v1.2.3 From 388cf58b31d6480007901785851e6369b0efe64b Mon Sep 17 00:00:00 2001 From: comex Date: Tue, 29 Dec 2020 14:26:16 -0500 Subject: k_priority_queue: Fix concepts use - For `std::same_as`, add missing include of ``. - For `std::convertible_to`, create a replacement in `common/concepts.h` and use that instead. This would also be found in ``, but unlike `std::same_as`, `std::convertible_to` is not yet implemented in libc++, LLVM's STL implementation - not even in master. (In fact, `std::same_as` is the *only* concept currently implemented. For some reason.) --- src/common/concepts.h | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'src/common') diff --git a/src/common/concepts.h b/src/common/concepts.h index 5bef3ad67..aa08065a7 100644 --- a/src/common/concepts.h +++ b/src/common/concepts.h @@ -31,4 +31,8 @@ concept DerivedFrom = requires { std::is_convertible_v; }; +// TODO: Replace with std::convertible_to when libc++ implements it. +template +concept ConvertibleTo = std::is_convertible_v; + } // namespace Common -- cgit v1.2.3 From 69e82d01d5012c0078f65a294ecbb7118707f73c Mon Sep 17 00:00:00 2001 From: bunnei Date: Tue, 29 Dec 2020 16:36:35 -0800 Subject: common: ThreadWorker: Add class to help do asynchronous work. --- src/common/CMakeLists.txt | 2 ++ src/common/thread_worker.cpp | 58 ++++++++++++++++++++++++++++++++++++++++++++ src/common/thread_worker.h | 30 +++++++++++++++++++++++ 3 files changed, 90 insertions(+) create mode 100644 src/common/thread_worker.cpp create mode 100644 src/common/thread_worker.h (limited to 'src/common') diff --git a/src/common/CMakeLists.txt b/src/common/CMakeLists.txt index 943ff996e..5c8003eb1 100644 --- a/src/common/CMakeLists.txt +++ b/src/common/CMakeLists.txt @@ -162,6 +162,8 @@ add_library(common STATIC thread.cpp thread.h thread_queue_list.h + thread_worker.cpp + thread_worker.h threadsafe_queue.h time_zone.cpp time_zone.h diff --git a/src/common/thread_worker.cpp b/src/common/thread_worker.cpp new file mode 100644 index 000000000..8f9bf447a --- /dev/null +++ b/src/common/thread_worker.cpp @@ -0,0 +1,58 @@ +// Copyright 2020 yuzu emulator team +// Licensed under GPLv2 or any later version +// Refer to the license.txt file included. + +#include "common/thread.h" +#include "common/thread_worker.h" + +namespace Common { + +ThreadWorker::ThreadWorker(std::size_t num_workers, const std::string& name) { + for (std::size_t i = 0; i < num_workers; ++i) + threads.emplace_back([this, thread_name{std::string{name}}] { + Common::SetCurrentThreadName(thread_name.c_str()); + + // Wait for first request + { + std::unique_lock lock{queue_mutex}; + condition.wait(lock, [this] { return stop || !requests.empty(); }); + } + + while (true) { + std::function task; + + { + std::unique_lock lock{queue_mutex}; + condition.wait(lock, [this] { return stop || !requests.empty(); }); + if (stop || requests.empty()) { + return; + } + task = std::move(requests.front()); + requests.pop(); + } + + task(); + } + }); +} + +ThreadWorker::~ThreadWorker() { + { + std::unique_lock lock{queue_mutex}; + stop = true; + } + condition.notify_all(); + for (std::thread& thread : threads) { + thread.join(); + } +} + +void ThreadWorker::QueueWork(std::function&& work) { + { + std::unique_lock lock{queue_mutex}; + requests.emplace(work); + } + condition.notify_one(); +} + +} // namespace Common diff --git a/src/common/thread_worker.h b/src/common/thread_worker.h new file mode 100644 index 000000000..f1859971f --- /dev/null +++ b/src/common/thread_worker.h @@ -0,0 +1,30 @@ +// Copyright 2020 yuzu emulator team +// Licensed under GPLv2 or any later version +// Refer to the license.txt file included. + +#pragma once + +#include +#include +#include +#include +#include +#include + +namespace Common { + +class ThreadWorker final { +public: + explicit ThreadWorker(std::size_t num_workers, const std::string& name); + ~ThreadWorker(); + void QueueWork(std::function&& work); + +private: + std::vector threads; + std::queue> requests; + std::mutex queue_mutex; + std::condition_variable condition; + std::atomic_bool stop{}; +}; + +} // namespace Common -- cgit v1.2.3 From b3587102d160fb74a12935a79f06ee8a12712f12 Mon Sep 17 00:00:00 2001 From: ReinUsesLisp Date: Tue, 29 Dec 2020 21:16:57 -0300 Subject: core/memory: Read and write page table atomically Squash attributes into the pointer's integer, making them an uintptr_t pair containing 2 bits at the bottom and then the pointer. These bits are currently unused thanks to alignment requirements. Configure Dynarmic to mask out these bits on pointer reads. While we are at it, remove some unused attributes carried over from Citra. Read/Write and other hot functions use a two step unpacking process that is less readable to stop MSVC from emitting an extra AND instruction in the hot path: mov rdi,rcx shr rdx,0Ch mov r8,qword ptr [rax+8] mov rax,qword ptr [r8+rdx*8] mov rdx,rax -and al,3 and rdx,0FFFFFFFFFFFFFFFCh je Core::Memory::Memory::Impl::Read mov rax,qword ptr [vaddr] movzx eax,byte ptr [rdx+rax] --- src/common/page_table.cpp | 10 ++----- src/common/page_table.h | 68 +++++++++++++++++++++++++++++++++++++-------- src/common/virtual_buffer.h | 10 ++++--- 3 files changed, 65 insertions(+), 23 deletions(-) (limited to 'src/common') diff --git a/src/common/page_table.cpp b/src/common/page_table.cpp index bccea0894..8fd8620fd 100644 --- a/src/common/page_table.cpp +++ b/src/common/page_table.cpp @@ -10,16 +10,10 @@ PageTable::PageTable() = default; PageTable::~PageTable() noexcept = default; -void PageTable::Resize(std::size_t address_space_width_in_bits, std::size_t page_size_in_bits, - bool has_attribute) { - const std::size_t num_page_table_entries{1ULL - << (address_space_width_in_bits - page_size_in_bits)}; +void PageTable::Resize(size_t address_space_width_in_bits, size_t page_size_in_bits) { + const size_t num_page_table_entries{1ULL << (address_space_width_in_bits - page_size_in_bits)}; pointers.resize(num_page_table_entries); backing_addr.resize(num_page_table_entries); - - if (has_attribute) { - attributes.resize(num_page_table_entries); - } } } // namespace Common diff --git a/src/common/page_table.h b/src/common/page_table.h index 9754fabf9..8d4ee9249 100644 --- a/src/common/page_table.h +++ b/src/common/page_table.h @@ -4,6 +4,7 @@ #pragma once +#include #include #include "common/common_types.h" @@ -20,10 +21,6 @@ enum class PageType : u8 { /// Page is mapped to regular memory, but also needs to check for rasterizer cache flushing and /// invalidation RasterizerCachedMemory, - /// Page is mapped to a I/O region. Writing and reading to this page is handled by functions. - Special, - /// Page is allocated for use. - Allocated, }; struct SpecialRegion { @@ -48,6 +45,59 @@ struct SpecialRegion { * mimics the way a real CPU page table works. */ struct PageTable { + /// Number of bits reserved for attribute tagging. + /// This can be at most the guaranteed alignment of the pointers in the page table. + static constexpr int ATTRIBUTE_BITS = 2; + + /** + * Pair of host pointer and page type attribute. + * This uses the lower bits of a given pointer to store the attribute tag. + * Writing and reading the pointer attribute pair is guaranteed to be atomic for the same method + * call. In other words, they are guaranteed to be synchronized at all times. + */ + class PageInfo { + public: + /// Returns the page pointer + [[nodiscard]] u8* Pointer() const noexcept { + return ExtractPointer(raw.load(std::memory_order_relaxed)); + } + + /// Returns the page type attribute + [[nodiscard]] PageType Type() const noexcept { + return ExtractType(raw.load(std::memory_order_relaxed)); + } + + /// Returns the page pointer and attribute pair, extracted from the same atomic read + [[nodiscard]] std::pair PointerType() const noexcept { + const uintptr_t non_atomic_raw = raw.load(std::memory_order_relaxed); + return {ExtractPointer(non_atomic_raw), ExtractType(non_atomic_raw)}; + } + + /// Returns the raw representation of the page information. + /// Use ExtractPointer and ExtractType to unpack the value. + [[nodiscard]] uintptr_t Raw() const noexcept { + return raw.load(std::memory_order_relaxed); + } + + /// Write a page pointer and type pair atomically + void Store(u8* pointer, PageType type) noexcept { + raw.store(reinterpret_cast(pointer) | static_cast(type)); + } + + /// Unpack a pointer from a page info raw representation + [[nodiscard]] static u8* ExtractPointer(uintptr_t raw) noexcept { + return reinterpret_cast(raw & (~uintptr_t{0} << ATTRIBUTE_BITS)); + } + + /// Unpack a page type from a page info raw representation + [[nodiscard]] static PageType ExtractType(uintptr_t raw) noexcept { + return static_cast(raw & ((uintptr_t{1} << ATTRIBUTE_BITS) - 1)); + } + + private: + std::atomic raw; + }; + PageTable(); ~PageTable() noexcept; @@ -63,20 +113,16 @@ struct PageTable { * * @param address_space_width_in_bits The address size width in bits. * @param page_size_in_bits The page size in bits. - * @param has_attribute Whether or not this page has any backing attributes. */ - void Resize(std::size_t address_space_width_in_bits, std::size_t page_size_in_bits, - bool has_attribute); + void Resize(size_t address_space_width_in_bits, size_t page_size_in_bits); /** * Vector of memory pointers backing each page. An entry can only be non-null if the - * corresponding entry in the `attributes` vector is of type `Memory`. + * corresponding attribute element is of type `Memory`. */ - VirtualBuffer pointers; + VirtualBuffer pointers; VirtualBuffer backing_addr; - - VirtualBuffer attributes; }; } // namespace Common diff --git a/src/common/virtual_buffer.h b/src/common/virtual_buffer.h index 91d430036..fb1a6f81f 100644 --- a/src/common/virtual_buffer.h +++ b/src/common/virtual_buffer.h @@ -15,10 +15,12 @@ void FreeMemoryPages(void* base, std::size_t size) noexcept; template class VirtualBuffer final { public: - static_assert( - std::is_trivially_constructible_v, - "T must be trivially constructible, as non-trivial constructors will not be executed " - "with the current allocator"); + // TODO: Uncomment this and change Common::PageTable::PageInfo to be trivially constructible + // using std::atomic_ref once libc++ has support for it + // static_assert( + // std::is_trivially_constructible_v, + // "T must be trivially constructible, as non-trivial constructors will not be executed " + // "with the current allocator"); constexpr VirtualBuffer() = default; explicit VirtualBuffer(std::size_t count) : alloc_size{count * sizeof(T)} { -- cgit v1.2.3 From 6d30745d772c7e332bbea1462a92033386b85b08 Mon Sep 17 00:00:00 2001 From: MerryMage Date: Fri, 1 Jan 2021 11:30:30 +0000 Subject: memory: Remove MemoryHook --- src/common/CMakeLists.txt | 2 -- src/common/memory_hook.cpp | 11 ----------- src/common/memory_hook.h | 47 ---------------------------------------------- src/common/page_table.h | 18 ------------------ 4 files changed, 78 deletions(-) delete mode 100644 src/common/memory_hook.cpp delete mode 100644 src/common/memory_hook.h (limited to 'src/common') diff --git a/src/common/CMakeLists.txt b/src/common/CMakeLists.txt index 5c8003eb1..2c2bd2ee8 100644 --- a/src/common/CMakeLists.txt +++ b/src/common/CMakeLists.txt @@ -135,8 +135,6 @@ add_library(common STATIC math_util.h memory_detect.cpp memory_detect.h - memory_hook.cpp - memory_hook.h microprofile.cpp microprofile.h microprofileui.h diff --git a/src/common/memory_hook.cpp b/src/common/memory_hook.cpp deleted file mode 100644 index 3986986d6..000000000 --- a/src/common/memory_hook.cpp +++ /dev/null @@ -1,11 +0,0 @@ -// Copyright 2018 Citra Emulator Project -// Licensed under GPLv2 or any later version -// Refer to the license.txt file included. - -#include "common/memory_hook.h" - -namespace Common { - -MemoryHook::~MemoryHook() = default; - -} // namespace Common diff --git a/src/common/memory_hook.h b/src/common/memory_hook.h deleted file mode 100644 index adaa4c2c5..000000000 --- a/src/common/memory_hook.h +++ /dev/null @@ -1,47 +0,0 @@ -// Copyright 2016 Citra Emulator Project -// Licensed under GPLv2 or any later version -// Refer to the license.txt file included. - -#pragma once - -#include -#include - -#include "common/common_types.h" - -namespace Common { - -/** - * Memory hooks have two purposes: - * 1. To allow reads and writes to a region of memory to be intercepted. This is used to implement - * texture forwarding and memory breakpoints for debugging. - * 2. To allow for the implementation of MMIO devices. - * - * A hook may be mapped to multiple regions of memory. - * - * If a std::nullopt or false is returned from a function, the read/write request is passed through - * to the underlying memory region. - */ -class MemoryHook { -public: - virtual ~MemoryHook(); - - virtual std::optional IsValidAddress(VAddr addr) = 0; - - virtual std::optional Read8(VAddr addr) = 0; - virtual std::optional Read16(VAddr addr) = 0; - virtual std::optional Read32(VAddr addr) = 0; - virtual std::optional Read64(VAddr addr) = 0; - - virtual bool ReadBlock(VAddr src_addr, void* dest_buffer, std::size_t size) = 0; - - virtual bool Write8(VAddr addr, u8 data) = 0; - virtual bool Write16(VAddr addr, u16 data) = 0; - virtual bool Write32(VAddr addr, u32 data) = 0; - virtual bool Write64(VAddr addr, u64 data) = 0; - - virtual bool WriteBlock(VAddr dest_addr, const void* src_buffer, std::size_t size) = 0; -}; - -using MemoryHookPointer = std::shared_ptr; -} // namespace Common diff --git a/src/common/page_table.h b/src/common/page_table.h index 8d4ee9249..0c14e6433 100644 --- a/src/common/page_table.h +++ b/src/common/page_table.h @@ -8,7 +8,6 @@ #include #include "common/common_types.h" -#include "common/memory_hook.h" #include "common/virtual_buffer.h" namespace Common { @@ -23,23 +22,6 @@ enum class PageType : u8 { RasterizerCachedMemory, }; -struct SpecialRegion { - enum class Type { - DebugHook, - IODevice, - } type; - - MemoryHookPointer handler; - - [[nodiscard]] bool operator<(const SpecialRegion& other) const { - return std::tie(type, handler) < std::tie(other.type, other.handler); - } - - [[nodiscard]] bool operator==(const SpecialRegion& other) const { - return std::tie(type, handler) == std::tie(other.type, other.handler); - } -}; - /** * A (reasonably) fast way of allowing switchable and remappable process address spaces. It loosely * mimics the way a real CPU page table works. -- cgit v1.2.3 From a745d87971b2c9795e1b2c587bfe30b849b522fa Mon Sep 17 00:00:00 2001 From: Morph Date: Sat, 2 Jan 2021 09:00:05 -0500 Subject: general: Fix various spelling errors --- src/common/page_table.h | 2 +- src/common/swap.h | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) (limited to 'src/common') diff --git a/src/common/page_table.h b/src/common/page_table.h index 0c14e6433..61c5552e0 100644 --- a/src/common/page_table.h +++ b/src/common/page_table.h @@ -90,7 +90,7 @@ struct PageTable { PageTable& operator=(PageTable&&) noexcept = default; /** - * Resizes the page table to be able to accomodate enough pages within + * Resizes the page table to be able to accommodate enough pages within * a given address space. * * @param address_space_width_in_bits The address size width in bits. diff --git a/src/common/swap.h b/src/common/swap.h index 7665942a2..a80e191dc 100644 --- a/src/common/swap.h +++ b/src/common/swap.h @@ -394,7 +394,7 @@ public: template friend S operator%(const S& p, const swapped_t v); - // Arithmetics + assignements + // Arithmetics + assignments template friend S operator+=(const S& p, const swapped_t v); @@ -451,7 +451,7 @@ S operator%(const S& i, const swap_struct_t v) { return i % v.swap(); } -// Arithmetics + assignements +// Arithmetics + assignments template S& operator+=(S& i, const swap_struct_t v) { i += v.swap(); -- cgit v1.2.3 From c19058659770fe4b235859aa4c617ae56947051d Mon Sep 17 00:00:00 2001 From: ReinUsesLisp Date: Sat, 9 Jan 2021 03:14:18 -0300 Subject: common/div_ceil: Return numerator type Fixes instances where DivCeil(u32, u64) would surprisingly return u64, instead of the more natural u32. --- src/common/div_ceil.h | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'src/common') diff --git a/src/common/div_ceil.h b/src/common/div_ceil.h index 6b2c48f91..95e1489a9 100644 --- a/src/common/div_ceil.h +++ b/src/common/div_ceil.h @@ -11,16 +11,16 @@ namespace Common { /// Ceiled integer division. template -requires std::is_integral_v&& std::is_unsigned_v[[nodiscard]] constexpr auto DivCeil( - N number, D divisor) { - return (static_cast(number) + divisor - 1) / divisor; +requires std::is_integral_v&& std::is_unsigned_v[[nodiscard]] constexpr N DivCeil(N number, + D divisor) { + return static_cast((static_cast(number) + divisor - 1) / divisor); } /// Ceiled integer division with logarithmic divisor in base 2 template -requires std::is_integral_v&& std::is_unsigned_v[[nodiscard]] constexpr auto DivCeilLog2( +requires std::is_integral_v&& std::is_unsigned_v[[nodiscard]] constexpr N DivCeilLog2( N value, D alignment_log2) { - return (static_cast(value) + (D(1) << alignment_log2) - 1) >> alignment_log2; + return static_cast((static_cast(value) + (D(1) << alignment_log2) - 1) >> alignment_log2); } } // namespace Common -- cgit v1.2.3