host_memory.h 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. [[nodiscard]] u8* BackingBasePointer() noexcept {
  40. return backing_base;
  41. }
  42. [[nodiscard]] const u8* BackingBasePointer() const noexcept {
  43. return backing_base;
  44. }
  45. [[nodiscard]] u8* VirtualBasePointer() noexcept {
  46. return virtual_base;
  47. }
  48. [[nodiscard]] const u8* VirtualBasePointer() const noexcept {
  49. return virtual_base;
  50. }
  51. private:
  52. size_t backing_size{};
  53. size_t virtual_size{};
  54. // Low level handler for the platform dependent memory routines
  55. class Impl;
  56. std::unique_ptr<Impl> impl;
  57. u8* backing_base{};
  58. u8* virtual_base{};
  59. size_t virtual_base_offset{};
  60. // Fallback if fastmem is not supported on this platform
  61. std::unique_ptr<Common::VirtualBuffer<u8>> fallback_buffer;
  62. };
  63. } // namespace Common