From 253a17451b45613b167c95ecd20d7250c8fec55d Mon Sep 17 00:00:00 2001 From: Lioncash Date: Thu, 6 Aug 2020 03:03:02 -0400 Subject: freezer: Make use of std::erase_if With C++20 we can simplify the erasing idiom. --- src/core/tools/freezer.cpp | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/core/tools/freezer.cpp b/src/core/tools/freezer.cpp index 2003e096f..c0d9004dd 100644 --- a/src/core/tools/freezer.cpp +++ b/src/core/tools/freezer.cpp @@ -107,10 +107,7 @@ void Freezer::Unfreeze(VAddr address) { LOG_DEBUG(Common_Memory, "Unfreezing memory for address={:016X}", address); - entries.erase( - std::remove_if(entries.begin(), entries.end(), - [&address](const Entry& entry) { return entry.address == address; }), - entries.end()); + std::erase_if(entries, [address](const Entry& entry) { return entry.address == address; }); } bool Freezer::IsFrozen(VAddr address) const { -- cgit v1.2.3 From 06ab28263b783e6ae07e51cdc43c0c381c5ae583 Mon Sep 17 00:00:00 2001 From: Lioncash Date: Thu, 6 Aug 2020 03:04:52 -0400 Subject: freezer: Take address values by value VAddr will always be 64-bit, so there's no need to take a trivial primitive alias by reference. --- src/core/tools/freezer.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/core/tools/freezer.cpp b/src/core/tools/freezer.cpp index c0d9004dd..4ca1870f2 100644 --- a/src/core/tools/freezer.cpp +++ b/src/core/tools/freezer.cpp @@ -113,7 +113,7 @@ void Freezer::Unfreeze(VAddr address) { bool Freezer::IsFrozen(VAddr address) const { std::lock_guard lock{entries_mutex}; - return std::find_if(entries.begin(), entries.end(), [&address](const Entry& entry) { + return std::find_if(entries.begin(), entries.end(), [address](const Entry& entry) { return entry.address == address; }) != entries.end(); } @@ -121,7 +121,7 @@ bool Freezer::IsFrozen(VAddr address) const { void Freezer::SetFrozenValue(VAddr address, u64 value) { std::lock_guard lock{entries_mutex}; - const auto iter = std::find_if(entries.begin(), entries.end(), [&address](const Entry& entry) { + const auto iter = std::find_if(entries.begin(), entries.end(), [address](const Entry& entry) { return entry.address == address; }); @@ -140,7 +140,7 @@ void Freezer::SetFrozenValue(VAddr address, u64 value) { std::optional Freezer::GetEntry(VAddr address) const { std::lock_guard lock{entries_mutex}; - const auto iter = std::find_if(entries.begin(), entries.end(), [&address](const Entry& entry) { + const auto iter = std::find_if(entries.begin(), entries.end(), [address](const Entry& entry) { return entry.address == address; }); -- cgit v1.2.3 From 61cd7eb47d947bd332ea306e74a1491c42854a61 Mon Sep 17 00:00:00 2001 From: Lioncash Date: Thu, 6 Aug 2020 03:11:19 -0400 Subject: freezer: Move entry finding to its own function Cleans up the callsites in other functions. --- src/core/tools/freezer.cpp | 26 +++++++++++++++----------- src/core/tools/freezer.h | 7 ++++++- 2 files changed, 21 insertions(+), 12 deletions(-) diff --git a/src/core/tools/freezer.cpp b/src/core/tools/freezer.cpp index 4ca1870f2..5c674a099 100644 --- a/src/core/tools/freezer.cpp +++ b/src/core/tools/freezer.cpp @@ -113,19 +113,15 @@ void Freezer::Unfreeze(VAddr address) { bool Freezer::IsFrozen(VAddr address) const { std::lock_guard lock{entries_mutex}; - return std::find_if(entries.begin(), entries.end(), [address](const Entry& entry) { - return entry.address == address; - }) != entries.end(); + return FindEntry(address) != entries.cend(); } void Freezer::SetFrozenValue(VAddr address, u64 value) { std::lock_guard lock{entries_mutex}; - const auto iter = std::find_if(entries.begin(), entries.end(), [address](const Entry& entry) { - return entry.address == address; - }); + const auto iter = FindEntry(address); - if (iter == entries.end()) { + if (iter == entries.cend()) { LOG_ERROR(Common_Memory, "Tried to set freeze value for address={:016X} that is not frozen!", address); return; @@ -140,11 +136,9 @@ void Freezer::SetFrozenValue(VAddr address, u64 value) { std::optional Freezer::GetEntry(VAddr address) const { std::lock_guard lock{entries_mutex}; - const auto iter = std::find_if(entries.begin(), entries.end(), [address](const Entry& entry) { - return entry.address == address; - }); + const auto iter = FindEntry(address); - if (iter == entries.end()) { + if (iter == entries.cend()) { return std::nullopt; } @@ -157,6 +151,16 @@ std::vector Freezer::GetEntries() const { return entries; } +Freezer::Entries::iterator Freezer::FindEntry(VAddr address) { + return std::find_if(entries.begin(), entries.end(), + [address](const Entry& entry) { return entry.address == address; }); +} + +Freezer::Entries::const_iterator Freezer::FindEntry(VAddr address) const { + return std::find_if(entries.begin(), entries.end(), + [address](const Entry& entry) { return entry.address == address; }); +} + void Freezer::FrameCallback(std::uintptr_t, std::chrono::nanoseconds ns_late) { if (!IsActive()) { LOG_DEBUG(Common_Memory, "Memory freezer has been deactivated, ending callback events."); diff --git a/src/core/tools/freezer.h b/src/core/tools/freezer.h index 2b2326bc4..0fdb701a7 100644 --- a/src/core/tools/freezer.h +++ b/src/core/tools/freezer.h @@ -73,13 +73,18 @@ public: std::vector GetEntries() const; private: + using Entries = std::vector; + + Entries::iterator FindEntry(VAddr address); + Entries::const_iterator FindEntry(VAddr address) const; + void FrameCallback(std::uintptr_t user_data, std::chrono::nanoseconds ns_late); void FillEntryReads(); std::atomic_bool active{false}; mutable std::mutex entries_mutex; - std::vector entries; + Entries entries; std::shared_ptr event; Core::Timing::CoreTiming& core_timing; -- cgit v1.2.3