host_memory.h 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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. [[nodiscard]] u8* BackingBasePointer() noexcept {
  31. return backing_base;
  32. }
  33. [[nodiscard]] const u8* BackingBasePointer() const noexcept {
  34. return backing_base;
  35. }
  36. [[nodiscard]] u8* VirtualBasePointer() noexcept {
  37. return virtual_base;
  38. }
  39. [[nodiscard]] const u8* VirtualBasePointer() const noexcept {
  40. return virtual_base;
  41. }
  42. private:
  43. size_t backing_size{};
  44. size_t virtual_size{};
  45. // Low level handler for the platform dependent memory routines
  46. class Impl;
  47. std::unique_ptr<Impl> impl;
  48. u8* backing_base{};
  49. u8* virtual_base{};
  50. size_t virtual_base_offset{};
  51. // Fallback if fastmem is not supported on this platform
  52. std::unique_ptr<Common::VirtualBuffer<u8>> fallback_buffer;
  53. };
  54. } // namespace Common