host_memory.h 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. void Unmap(size_t virtual_offset, size_t length);
  37. void Protect(size_t virtual_offset, size_t length, bool read, bool write, bool execute = false);
  38. void EnableDirectMappedAddress();
  39. void ClearBackingRegion(size_t physical_offset, size_t length, u32 fill_value);
  40. [[nodiscard]] u8* BackingBasePointer() noexcept {
  41. return backing_base;
  42. }
  43. [[nodiscard]] const u8* BackingBasePointer() const noexcept {
  44. return backing_base;
  45. }
  46. [[nodiscard]] u8* VirtualBasePointer() noexcept {
  47. return virtual_base;
  48. }
  49. [[nodiscard]] const u8* VirtualBasePointer() const noexcept {
  50. return virtual_base;
  51. }
  52. private:
  53. size_t backing_size{};
  54. size_t virtual_size{};
  55. // Low level handler for the platform dependent memory routines
  56. class Impl;
  57. std::unique_ptr<Impl> impl;
  58. u8* backing_base{};
  59. u8* virtual_base{};
  60. size_t virtual_base_offset{};
  61. // Fallback if fastmem is not supported on this platform
  62. std::unique_ptr<Common::VirtualBuffer<u8>> fallback_buffer;
  63. };
  64. } // namespace Common