map_interval.h 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. // Copyright 2019 yuzu Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #pragma once
  5. #include <array>
  6. #include <cstddef>
  7. #include <memory>
  8. #include <vector>
  9. #include <boost/intrusive/set_hook.hpp>
  10. #include "common/common_types.h"
  11. #include "video_core/gpu.h"
  12. namespace VideoCommon {
  13. struct MapInterval : public boost::intrusive::set_base_hook<boost::intrusive::optimize_size<true>> {
  14. MapInterval() = default;
  15. /*implicit*/ MapInterval(VAddr start_) noexcept : start{start_} {}
  16. explicit MapInterval(VAddr start_, VAddr end_, GPUVAddr gpu_addr_) noexcept
  17. : start{start_}, end{end_}, gpu_addr{gpu_addr_} {}
  18. bool IsInside(VAddr other_start, VAddr other_end) const noexcept {
  19. return start <= other_start && other_end <= end;
  20. }
  21. bool Overlaps(VAddr other_start, VAddr other_end) const noexcept {
  22. return start < other_end && other_start < end;
  23. }
  24. void MarkAsModified(bool is_modified_, u64 ticks_) noexcept {
  25. is_modified = is_modified_;
  26. ticks = ticks_;
  27. }
  28. boost::intrusive::set_member_hook<> member_hook_;
  29. VAddr start = 0;
  30. VAddr end = 0;
  31. GPUVAddr gpu_addr = 0;
  32. u64 ticks = 0;
  33. bool is_written = false;
  34. bool is_modified = false;
  35. bool is_registered = false;
  36. bool is_memory_marked = false;
  37. bool is_sync_pending = false;
  38. };
  39. struct MapIntervalCompare {
  40. constexpr bool operator()(const MapInterval& lhs, const MapInterval& rhs) const noexcept {
  41. return lhs.start < rhs.start;
  42. }
  43. };
  44. class MapIntervalAllocator {
  45. public:
  46. MapIntervalAllocator();
  47. ~MapIntervalAllocator();
  48. MapInterval* Allocate() {
  49. if (free_list.empty()) {
  50. AllocateNewChunk();
  51. }
  52. MapInterval* const interval = free_list.back();
  53. free_list.pop_back();
  54. return interval;
  55. }
  56. void Release(MapInterval* interval) {
  57. free_list.push_back(interval);
  58. }
  59. private:
  60. struct Chunk {
  61. std::unique_ptr<Chunk> next;
  62. std::array<MapInterval, 0x8000> data;
  63. };
  64. void AllocateNewChunk();
  65. void FillFreeList(Chunk& chunk);
  66. std::vector<MapInterval*> free_list;
  67. std::unique_ptr<Chunk>* new_chunk = &first_chunk.next;
  68. Chunk first_chunk;
  69. };
  70. } // namespace VideoCommon