host_memory.h 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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 <memory>
  6. #include "common/common_types.h"
  7. #include "common/virtual_buffer.h"
  8. namespace Common {
  9. /**
  10. * A low level linear memory buffer, which supports multiple mappings
  11. * Its purpose is to rebuild a given sparse memory layout, including mirrors.
  12. */
  13. class HostMemory {
  14. public:
  15. explicit HostMemory(size_t backing_size_, size_t virtual_size_);
  16. ~HostMemory();
  17. /**
  18. * Copy constructors. They shall return a copy of the buffer without the mappings.
  19. * TODO: Implement them with COW if needed.
  20. */
  21. HostMemory(const HostMemory& other) = delete;
  22. HostMemory& operator=(const HostMemory& other) = delete;
  23. /**
  24. * Move constructors. They will move the buffer and the mappings to the new object.
  25. */
  26. HostMemory(HostMemory&& other) noexcept;
  27. HostMemory& operator=(HostMemory&& other) noexcept;
  28. void Map(size_t virtual_offset, size_t host_offset, size_t length);
  29. void Unmap(size_t virtual_offset, size_t length);
  30. void Protect(size_t virtual_offset, size_t length, bool read, bool write);
  31. [[nodiscard]] u8* BackingBasePointer() noexcept {
  32. return backing_base;
  33. }
  34. [[nodiscard]] const u8* BackingBasePointer() const noexcept {
  35. return backing_base;
  36. }
  37. [[nodiscard]] u8* VirtualBasePointer() noexcept {
  38. return virtual_base;
  39. }
  40. [[nodiscard]] const u8* VirtualBasePointer() const noexcept {
  41. return virtual_base;
  42. }
  43. private:
  44. size_t backing_size{};
  45. size_t virtual_size{};
  46. // Low level handler for the platform dependent memory routines
  47. class Impl;
  48. std::unique_ptr<Impl> impl;
  49. u8* backing_base{};
  50. u8* virtual_base{};
  51. size_t virtual_base_offset{};
  52. // Fallback if fastmem is not supported on this platform
  53. std::unique_ptr<Common::VirtualBuffer<u8>> fallback_buffer;
  54. };
  55. } // namespace Common