summaryrefslogtreecommitdiffstats
path: root/src/hid_core/resources/ring_lifo.h
diff options
context:
space:
mode:
authorNarr the Reg <juangerman-13@hotmail.com>2024-01-06 22:38:59 +0100
committerGitHub <noreply@github.com>2024-01-06 22:38:59 +0100
commit12fd2ae86d78c69d5bce6ab5b5ba26a4b265ac92 (patch)
tree3b95cbb74be05f0ce7a007353f1f9f95e1ed3901 /src/hid_core/resources/ring_lifo.h
parentMerge pull request #12437 from ameerj/gl-amd-fixes (diff)
parenthid_core: Move hid to it's own subproject (diff)
downloadyuzu-12fd2ae86d78c69d5bce6ab5b5ba26a4b265ac92.tar
yuzu-12fd2ae86d78c69d5bce6ab5b5ba26a4b265ac92.tar.gz
yuzu-12fd2ae86d78c69d5bce6ab5b5ba26a4b265ac92.tar.bz2
yuzu-12fd2ae86d78c69d5bce6ab5b5ba26a4b265ac92.tar.lz
yuzu-12fd2ae86d78c69d5bce6ab5b5ba26a4b265ac92.tar.xz
yuzu-12fd2ae86d78c69d5bce6ab5b5ba26a4b265ac92.tar.zst
yuzu-12fd2ae86d78c69d5bce6ab5b5ba26a4b265ac92.zip
Diffstat (limited to 'src/hid_core/resources/ring_lifo.h')
-rw-r--r--src/hid_core/resources/ring_lifo.h53
1 files changed, 53 insertions, 0 deletions
diff --git a/src/hid_core/resources/ring_lifo.h b/src/hid_core/resources/ring_lifo.h
new file mode 100644
index 000000000..0816784e0
--- /dev/null
+++ b/src/hid_core/resources/ring_lifo.h
@@ -0,0 +1,53 @@
+// SPDX-FileCopyrightText: Copyright 2021 yuzu Emulator Project
+// SPDX-License-Identifier: GPL-2.0-or-later
+
+#pragma once
+
+#include <array>
+
+#include "common/common_types.h"
+
+namespace Service::HID {
+
+template <typename State>
+struct AtomicStorage {
+ s64 sampling_number;
+ State state;
+};
+
+template <typename State, std::size_t max_buffer_size>
+struct Lifo {
+ s64 timestamp{};
+ s64 total_buffer_count = static_cast<s64>(max_buffer_size);
+ s64 buffer_tail{};
+ s64 buffer_count{};
+ std::array<AtomicStorage<State>, max_buffer_size> entries{};
+
+ const AtomicStorage<State>& ReadCurrentEntry() const {
+ return entries[buffer_tail];
+ }
+
+ const AtomicStorage<State>& ReadPreviousEntry() const {
+ return entries[GetPreviousEntryIndex()];
+ }
+
+ std::size_t GetPreviousEntryIndex() const {
+ return static_cast<size_t>((buffer_tail + max_buffer_size - 1) % max_buffer_size);
+ }
+
+ std::size_t GetNextEntryIndex() const {
+ return static_cast<size_t>((buffer_tail + 1) % max_buffer_size);
+ }
+
+ void WriteNextEntry(const State& new_state) {
+ if (buffer_count < static_cast<s64>(max_buffer_size) - 1) {
+ buffer_count++;
+ }
+ buffer_tail = GetNextEntryIndex();
+ const auto& previous_entry = ReadPreviousEntry();
+ entries[buffer_tail].sampling_number = previous_entry.sampling_number + 1;
+ entries[buffer_tail].state = new_state;
+ }
+};
+
+} // namespace Service::HID