ring_lifo.h 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. // SPDX-FileCopyrightText: Copyright 2021 yuzu Emulator Project
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #pragma once
  4. #include <array>
  5. #include "common/common_types.h"
  6. namespace Service::HID {
  7. template <typename State>
  8. struct AtomicStorage {
  9. s64 sampling_number;
  10. State state;
  11. };
  12. template <typename State, std::size_t max_buffer_size>
  13. struct Lifo {
  14. s64 timestamp{};
  15. s64 total_buffer_count = static_cast<s64>(max_buffer_size);
  16. s64 buffer_tail{};
  17. s64 buffer_count{};
  18. std::array<AtomicStorage<State>, max_buffer_size> entries{};
  19. const AtomicStorage<State>& ReadCurrentEntry() const {
  20. return entries[buffer_tail];
  21. }
  22. const AtomicStorage<State>& ReadPreviousEntry() const {
  23. return entries[GetPreviousEntryIndex()];
  24. }
  25. std::size_t GetPreviousEntryIndex() const {
  26. return static_cast<size_t>((buffer_tail + max_buffer_size - 1) % max_buffer_size);
  27. }
  28. std::size_t GetNextEntryIndex() const {
  29. return static_cast<size_t>((buffer_tail + 1) % max_buffer_size);
  30. }
  31. void WriteNextEntry(const State& new_state) {
  32. if (buffer_count < static_cast<s64>(max_buffer_size) - 1) {
  33. buffer_count++;
  34. }
  35. buffer_tail = GetNextEntryIndex();
  36. const auto& previous_entry = ReadPreviousEntry();
  37. entries[buffer_tail].sampling_number = previous_entry.sampling_number + 1;
  38. entries[buffer_tail].state = new_state;
  39. }
  40. };
  41. } // namespace Service::HID