From a7837a3791562899bf5e0e98aef851a2f4aaf376 Mon Sep 17 00:00:00 2001 From: ReinUsesLisp Date: Sat, 5 Jun 2021 06:23:25 -0300 Subject: common/host_memory: Add interface and Windows implementation --- src/common/host_memory.h | 62 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 src/common/host_memory.h (limited to 'src/common/host_memory.h') diff --git a/src/common/host_memory.h b/src/common/host_memory.h new file mode 100644 index 000000000..98005df7a --- /dev/null +++ b/src/common/host_memory.h @@ -0,0 +1,62 @@ +// Copyright 2019 yuzu Emulator Project +// Licensed under GPLv2 or any later version +// Refer to the license.txt file included. + +#pragma once + +#include +#include "common/common_types.h" + +namespace Common { + +/** + * A low level linear memory buffer, which supports multiple mappings + * Its purpose is to rebuild a given sparse memory layout, including mirrors. + */ +class HostMemory { +public: + explicit HostMemory(size_t backing_size, size_t virtual_size); + ~HostMemory(); + + /** + * Copy constructors. They shall return a copy of the buffer without the mappings. + * TODO: Implement them with COW if needed. + */ + HostMemory(const HostMemory& other) = delete; + HostMemory& operator=(const HostMemory& other) = delete; + + /** + * Move constructors. They will move the buffer and the mappings to the new object. + */ + HostMemory(HostMemory&& other) noexcept; + HostMemory& operator=(HostMemory&& other) noexcept; + + void Map(size_t virtual_offset, size_t host_offset, size_t length); + + void Unmap(size_t virtual_offset, size_t length); + + void Protect(size_t virtual_offset, size_t length, bool read, bool write); + + [[nodiscard]] u8* BackingBasePointer() noexcept { + return backing_base; + } + [[nodiscard]] const u8* BackingBasePointer() const noexcept { + return backing_base; + } + + [[nodiscard]] u8* VirtualBasePointer() noexcept { + return virtual_base; + } + [[nodiscard]] const u8* VirtualBasePointer() const noexcept { + return virtual_base; + } + +private: + // Low level handler for the platform dependent memory routines + class Impl; + std::unique_ptr impl; + u8* backing_base{}; + u8* virtual_base{}; +}; + +} // namespace Common -- cgit v1.2.3 From c4609c92eea30558473f02082733c7e59c2d2013 Mon Sep 17 00:00:00 2001 From: Markus Wick Date: Sat, 5 Jun 2021 11:47:08 +0200 Subject: common/host_memory: Optimize for huge tables. In theory, if we have 2 MB continously mapped, this should save one layer of TLB. Let's make it at least more likely by aligning the memory. --- src/common/host_memory.h | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) (limited to 'src/common/host_memory.h') diff --git a/src/common/host_memory.h b/src/common/host_memory.h index 98005df7a..eaa7d18ab 100644 --- a/src/common/host_memory.h +++ b/src/common/host_memory.h @@ -15,7 +15,7 @@ namespace Common { */ class HostMemory { public: - explicit HostMemory(size_t backing_size, size_t virtual_size); + explicit HostMemory(size_t backing_size_, size_t virtual_size_); ~HostMemory(); /** @@ -52,11 +52,15 @@ public: } private: + size_t backing_size{}; + size_t virtual_size{}; + // Low level handler for the platform dependent memory routines class Impl; std::unique_ptr impl; u8* backing_base{}; u8* virtual_base{}; + size_t virtual_base_offset{}; }; } // namespace Common -- cgit v1.2.3 From 7f85abb28120fbb57bb813b828ee42f2a2031990 Mon Sep 17 00:00:00 2001 From: Markus Wick Date: Fri, 11 Jun 2021 11:47:23 +0200 Subject: common/host_memory: Implement a fallback if fastmem fails. This falls back to the old approach of using a virtual buffer. Windows is untested, but this build should fix support for Windows < 10 v1803. However without fastmem support at all. --- src/common/host_memory.h | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'src/common/host_memory.h') diff --git a/src/common/host_memory.h b/src/common/host_memory.h index eaa7d18ab..9b8326d0f 100644 --- a/src/common/host_memory.h +++ b/src/common/host_memory.h @@ -6,6 +6,7 @@ #include #include "common/common_types.h" +#include "common/virtual_buffer.h" namespace Common { @@ -61,6 +62,9 @@ private: u8* backing_base{}; u8* virtual_base{}; size_t virtual_base_offset{}; + + // Fallback if fastmem is not supported on this platform + std::unique_ptr> fallback_buffer; }; } // namespace Common -- cgit v1.2.3