From d1b49cf03b43dcd17783df2ff57244a5a22b8844 Mon Sep 17 00:00:00 2001 From: Nemoumbra Date: Fri, 20 Sep 2024 16:19:24 +0300 Subject: [PATCH] Moved the CyclicBuffer to the Collections dir --- CMakeLists.txt | 1 + Common/Common.vcxproj | 1 + Common/Common.vcxproj.filters | 3 + Common/Data/Collections/CyclicBuffer.h | 87 +++++++++++++++++++++++++ Core/MIPS/MIPSTracer.h | 65 +----------------- UWP/CommonUWP/CommonUWP.vcxproj | 1 + UWP/CommonUWP/CommonUWP.vcxproj.filters | 3 + 7 files changed, 97 insertions(+), 64 deletions(-) create mode 100644 Common/Data/Collections/CyclicBuffer.h diff --git a/CMakeLists.txt b/CMakeLists.txt index a7b642a3ec..3f7b1ff615 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -645,6 +645,7 @@ add_library(Common STATIC Common/Data/Collections/Hashmaps.h Common/Data/Collections/TinySet.h Common/Data/Collections/FastVec.h + Common/Data/Collections/CyclicBuffer.h Common/Data/Collections/ThreadSafeList.h Common/Data/Color/RGBAUtil.cpp Common/Data/Color/RGBAUtil.h diff --git a/Common/Common.vcxproj b/Common/Common.vcxproj index 0b65c4e83c..68bd679caf 100644 --- a/Common/Common.vcxproj +++ b/Common/Common.vcxproj @@ -882,6 +882,7 @@ + diff --git a/Common/Common.vcxproj.filters b/Common/Common.vcxproj.filters index 6541f4776e..10010e8c16 100644 --- a/Common/Common.vcxproj.filters +++ b/Common/Common.vcxproj.filters @@ -994,6 +994,9 @@ Data\Collections + + Data\Collections + GPU diff --git a/Common/Data/Collections/CyclicBuffer.h b/Common/Data/Collections/CyclicBuffer.h new file mode 100644 index 0000000000..7eaa042198 --- /dev/null +++ b/Common/Data/Collections/CyclicBuffer.h @@ -0,0 +1,87 @@ +// Copyright (c) 2024- PPSSPP Project. + +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, version 2.0 or later versions. + +// This program is distributed in the hope that 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 2.0 for more details. + +// A copy of the GPL 2.0 should have been included with the program. +// If not, see http://www.gnu.org/licenses/ + +// Official git repository and contact information can be found at +// https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/. + +#pragma once + +#include + +#include "Common/CommonTypes.h" + + +template +struct CyclicBuffer { + std::vector buffer; + u32 current_index; + bool overflow; + + explicit CyclicBuffer(u32 capacity) : buffer(capacity, T()), current_index(0), overflow(false) {} + + CyclicBuffer(): buffer(), current_index(0), overflow(false) {} + + void push_back(const T& value); + void push_back(T&& value); + + void clear(); + void resize(u32 new_capacity); + + std::vector get_content() const; +}; + +template +std::vector CyclicBuffer::get_content() const { + if (!overflow) { + return std::vector(buffer.begin(), buffer.begin() + current_index); + } + + std::vector ans; + ans.reserve(buffer.size()); + std::copy(buffer.begin() + current_index, buffer.end(), std::back_inserter(ans)); + std::copy(buffer.begin(), buffer.begin() + current_index, std::back_inserter(ans)); + return ans; +} + +template +void CyclicBuffer::push_back(const T& value) { + buffer[current_index] = value; + ++current_index; + if (current_index == buffer.size()) { + current_index = 0; + overflow = true; + } +} + +template +void CyclicBuffer::push_back(T&& value) { + buffer[current_index] = std::move(value); + ++current_index; + if (current_index == buffer.size()) { + current_index = 0; + overflow = true; + } +} + +template +void CyclicBuffer::clear() { + buffer.clear(); + current_index = 0; + overflow = false; +} + +template +void CyclicBuffer::resize(u32 new_capacity) { + buffer.resize(new_capacity); +} diff --git a/Core/MIPS/MIPSTracer.h b/Core/MIPS/MIPSTracer.h index 8ba1bef6e8..d9c873323d 100644 --- a/Core/MIPS/MIPSTracer.h +++ b/Core/MIPS/MIPSTracer.h @@ -26,6 +26,7 @@ #include "Core/MIPS/IR/IRJit.h" #include "Common/Log.h" #include "Common/File/Path.h" +#include "Common/Data/Collections/CyclicBuffer.h" struct TraceBlockInfo { @@ -60,70 +61,6 @@ struct TraceBlockStorage { }; -template -struct CyclicBuffer { - std::vector buffer; - u32 current_index; - bool overflow; - - explicit CyclicBuffer(u32 capacity) : buffer(capacity, T()), current_index(0), overflow(false) {} - - CyclicBuffer(): buffer(), current_index(0), overflow(false) {} - - void push_back(const T& value); - void push_back(T&& value); - - void clear(); - void resize(u32 new_capacity); - - std::vector get_content() const; -}; - -template -std::vector CyclicBuffer::get_content() const { - if (!overflow) { - return std::vector(buffer.begin(), buffer.begin() + current_index); - } - - std::vector ans; - ans.reserve(buffer.size()); - std::copy(buffer.begin() + current_index, buffer.end(), std::back_inserter(ans)); - std::copy(buffer.begin(), buffer.begin() + current_index, std::back_inserter(ans)); - return ans; -} - -template -void CyclicBuffer::push_back(const T& value) { - buffer[current_index] = value; - ++current_index; - if (current_index == buffer.size()) { - current_index = 0; - overflow = true; - } -} - -template -void CyclicBuffer::push_back(T&& value) { - buffer[current_index] = std::move(value); - ++current_index; - if (current_index == buffer.size()) { - current_index = 0; - overflow = true; - } -} - -template -void CyclicBuffer::clear() { - buffer.clear(); - current_index = 0; - overflow = false; -} - -template -void CyclicBuffer::resize(u32 new_capacity) { - buffer.resize(new_capacity); -} - // This system is meant for trace recording. // A trace here stands for a sequence of instructions and their respective addresses in RAM. diff --git a/UWP/CommonUWP/CommonUWP.vcxproj b/UWP/CommonUWP/CommonUWP.vcxproj index 195b61c8b4..17feea19a7 100644 --- a/UWP/CommonUWP/CommonUWP.vcxproj +++ b/UWP/CommonUWP/CommonUWP.vcxproj @@ -115,6 +115,7 @@ + diff --git a/UWP/CommonUWP/CommonUWP.vcxproj.filters b/UWP/CommonUWP/CommonUWP.vcxproj.filters index f38724008a..eaf244e0cb 100644 --- a/UWP/CommonUWP/CommonUWP.vcxproj.filters +++ b/UWP/CommonUWP/CommonUWP.vcxproj.filters @@ -597,6 +597,9 @@ Data\Collections + + Data\Collections + Data\Random