host_memory.h 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. // SPDX-FileCopyrightText: Copyright 2019 yuzu Emulator Project
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #pragma once
  4. #include <memory>
  5. #include "common/common_funcs.h"
  6. #include "common/common_types.h"
  7. #include "common/virtual_buffer.h"
  8. namespace Common {
  9. enum class MemoryPermission : u32 {
  10. Read = 1 << 0,
  11. Write = 1 << 1,
  12. ReadWrite = Read | Write,
  13. Execute = 1 << 2,
  14. };
  15. DECLARE_ENUM_FLAG_OPERATORS(MemoryPermission)
  16. /**
  17. * A low level linear memory buffer, which supports multiple mappings
  18. * Its purpose is to rebuild a given sparse memory layout, including mirrors.
  19. */
  20. class HostMemory {
  21. public:
  22. explicit HostMemory(size_t backing_size_, size_t virtual_size_);
  23. ~HostMemory();
  24. /**
  25. * Copy constructors. They shall return a copy of the buffer without the mappings.
  26. * TODO: Implement them with COW if needed.
  27. */
  28. HostMemory(const HostMemory& other) = delete;
  29. HostMemory& operator=(const HostMemory& other) = delete;
  30. /**
  31. * Move constructors. They will move the buffer and the mappings to the new object.
  32. */
  33. HostMemory(HostMemory&& other) noexcept;
  34. HostMemory& operator=(HostMemory&& other) noexcept;
  35. void Map(size_t virtual_offset, size_t host_offset, size_t length, MemoryPermission perms,
  36. bool separate_heap);
  37. void Unmap(size_t virtual_offset, size_t length, bool separate_heap);
  38. void Protect(size_t virtual_offset, size_t length, MemoryPermission perms);
  39. void EnableDirectMappedAddress();
  40. void ClearBackingRegion(size_t physical_offset, size_t length, u32 fill_value);
  41. [[nodiscard]] u8* BackingBasePointer() noexcept {
  42. return backing_base;
  43. }
  44. [[nodiscard]] const u8* BackingBasePointer() const noexcept {
  45. return backing_base;
  46. }
  47. [[nodiscard]] u8* VirtualBasePointer() noexcept {
  48. return virtual_base;
  49. }
  50. [[nodiscard]] const u8* VirtualBasePointer() const noexcept {
  51. return virtual_base;
  52. }
  53. bool IsInVirtualRange(void* address) const noexcept {
  54. return address >= virtual_base && address < virtual_base + virtual_size;
  55. }
  56. private:
  57. size_t backing_size{};
  58. size_t virtual_size{};
  59. // Low level handler for the platform dependent memory routines
  60. class Impl;
  61. std::unique_ptr<Impl> impl;
  62. u8* backing_base{};
  63. u8* virtual_base{};
  64. size_t virtual_base_offset{};
  65. // Fallback if fastmem is not supported on this platform
  66. std::unique_ptr<Common::VirtualBuffer<u8>> fallback_buffer;
  67. };
  68. } // namespace Common