host_memory.h 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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_types.h"
  6. #include "common/virtual_buffer.h"
  7. namespace Common {
  8. /**
  9. * A low level linear memory buffer, which supports multiple mappings
  10. * Its purpose is to rebuild a given sparse memory layout, including mirrors.
  11. */
  12. class HostMemory {
  13. public:
  14. explicit HostMemory(size_t backing_size_, size_t virtual_size_);
  15. ~HostMemory();
  16. /**
  17. * Copy constructors. They shall return a copy of the buffer without the mappings.
  18. * TODO: Implement them with COW if needed.
  19. */
  20. HostMemory(const HostMemory& other) = delete;
  21. HostMemory& operator=(const HostMemory& other) = delete;
  22. /**
  23. * Move constructors. They will move the buffer and the mappings to the new object.
  24. */
  25. HostMemory(HostMemory&& other) noexcept;
  26. HostMemory& operator=(HostMemory&& other) noexcept;
  27. void Map(size_t virtual_offset, size_t host_offset, size_t length);
  28. void Unmap(size_t virtual_offset, size_t length);
  29. void Protect(size_t virtual_offset, size_t length, bool read, bool write);
  30. void EnableDirectMappedAddress();
  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