summaryrefslogtreecommitdiff
path: root/src/common
diff options
context:
space:
mode:
Diffstat (limited to 'src/common')
-rw-r--r--src/common/CMakeLists.txt4
-rw-r--r--src/common/alignment.h7
-rw-r--r--src/common/common_funcs.h32
-rw-r--r--src/common/dynamic_library.cpp106
-rw-r--r--src/common/dynamic_library.h75
-rw-r--r--src/common/file_util.cpp28
-rw-r--r--src/common/file_util.h14
-rw-r--r--src/common/page_table.cpp34
-rw-r--r--src/common/page_table.h37
-rw-r--r--src/common/scope_exit.h9
-rw-r--r--src/common/virtual_buffer.cpp52
-rw-r--r--src/common/virtual_buffer.h58
12 files changed, 393 insertions, 63 deletions
diff --git a/src/common/CMakeLists.txt b/src/common/CMakeLists.txt
index fbebed715..6ffc612e7 100644
--- a/src/common/CMakeLists.txt
+++ b/src/common/CMakeLists.txt
@@ -106,6 +106,8 @@ add_library(common STATIC
common_funcs.h
common_paths.h
common_types.h
+ dynamic_library.cpp
+ dynamic_library.h
file_util.cpp
file_util.h
hash.h
@@ -153,6 +155,8 @@ add_library(common STATIC
uuid.cpp
uuid.h
vector_math.h
+ virtual_buffer.cpp
+ virtual_buffer.h
web_result.h
zstd_compression.cpp
zstd_compression.h
diff --git a/src/common/alignment.h b/src/common/alignment.h
index cdd4833f8..f8c49e079 100644
--- a/src/common/alignment.h
+++ b/src/common/alignment.h
@@ -38,6 +38,13 @@ constexpr bool IsWordAligned(T value) {
return (value & 0b11) == 0;
}
+template <typename T>
+constexpr bool IsAligned(T value, std::size_t alignment) {
+ using U = typename std::make_unsigned<T>::type;
+ const U mask = static_cast<U>(alignment - 1);
+ return (value & mask) == 0;
+}
+
template <typename T, std::size_t Align = 16>
class AlignmentAllocator {
public:
diff --git a/src/common/common_funcs.h b/src/common/common_funcs.h
index 052254678..88cf5250a 100644
--- a/src/common/common_funcs.h
+++ b/src/common/common_funcs.h
@@ -55,6 +55,38 @@ __declspec(dllimport) void __stdcall DebugBreak(void);
// Defined in Misc.cpp.
std::string GetLastErrorMsg();
+#define DECLARE_ENUM_FLAG_OPERATORS(type) \
+ constexpr type operator|(type a, type b) noexcept { \
+ using T = std::underlying_type_t<type>; \
+ return static_cast<type>(static_cast<T>(a) | static_cast<T>(b)); \
+ } \
+ constexpr type operator&(type a, type b) noexcept { \
+ using T = std::underlying_type_t<type>; \
+ return static_cast<type>(static_cast<T>(a) & static_cast<T>(b)); \
+ } \
+ constexpr type& operator|=(type& a, type b) noexcept { \
+ using T = std::underlying_type_t<type>; \
+ a = static_cast<type>(static_cast<T>(a) | static_cast<T>(b)); \
+ return a; \
+ } \
+ constexpr type& operator&=(type& a, type b) noexcept { \
+ using T = std::underlying_type_t<type>; \
+ a = static_cast<type>(static_cast<T>(a) & static_cast<T>(b)); \
+ return a; \
+ } \
+ constexpr type operator~(type key) noexcept { \
+ using T = std::underlying_type_t<type>; \
+ return static_cast<type>(~static_cast<T>(key)); \
+ } \
+ constexpr bool True(type key) noexcept { \
+ using T = std::underlying_type_t<type>; \
+ return static_cast<T>(key) != 0; \
+ } \
+ constexpr bool False(type key) noexcept { \
+ using T = std::underlying_type_t<type>; \
+ return static_cast<T>(key) == 0; \
+ }
+
namespace Common {
constexpr u32 MakeMagic(char a, char b, char c, char d) {
diff --git a/src/common/dynamic_library.cpp b/src/common/dynamic_library.cpp
new file mode 100644
index 000000000..7ab54e9e4
--- /dev/null
+++ b/src/common/dynamic_library.cpp
@@ -0,0 +1,106 @@
+// Copyright 2019 Dolphin Emulator Project
+// Licensed under GPLv2+
+// Refer to the license.txt file included.
+
+#include <cstring>
+#include <string>
+#include <utility>
+
+#include <fmt/format.h>
+
+#include "common/dynamic_library.h"
+
+#ifdef _WIN32
+#include <windows.h>
+#else
+#include <dlfcn.h>
+#endif
+
+namespace Common {
+
+DynamicLibrary::DynamicLibrary() = default;
+
+DynamicLibrary::DynamicLibrary(const char* filename) {
+ Open(filename);
+}
+
+DynamicLibrary::DynamicLibrary(DynamicLibrary&& rhs) noexcept
+ : handle{std::exchange(rhs.handle, nullptr)} {}
+
+DynamicLibrary& DynamicLibrary::operator=(DynamicLibrary&& rhs) noexcept {
+ Close();
+ handle = std::exchange(rhs.handle, nullptr);
+ return *this;
+}
+
+DynamicLibrary::~DynamicLibrary() {
+ Close();
+}
+
+std::string DynamicLibrary::GetUnprefixedFilename(const char* filename) {
+#if defined(_WIN32)
+ return std::string(filename) + ".dll";
+#elif defined(__APPLE__)
+ return std::string(filename) + ".dylib";
+#else
+ return std::string(filename) + ".so";
+#endif
+}
+
+std::string DynamicLibrary::GetVersionedFilename(const char* libname, int major, int minor) {
+#if defined(_WIN32)
+ if (major >= 0 && minor >= 0)
+ return fmt::format("{}-{}-{}.dll", libname, major, minor);
+ else if (major >= 0)
+ return fmt::format("{}-{}.dll", libname, major);
+ else
+ return fmt::format("{}.dll", libname);
+#elif defined(__APPLE__)
+ const char* prefix = std::strncmp(libname, "lib", 3) ? "lib" : "";
+ if (major >= 0 && minor >= 0)
+ return fmt::format("{}{}.{}.{}.dylib", prefix, libname, major, minor);
+ else if (major >= 0)
+ return fmt::format("{}{}.{}.dylib", prefix, libname, major);
+ else
+ return fmt::format("{}{}.dylib", prefix, libname);
+#else
+ const char* prefix = std::strncmp(libname, "lib", 3) ? "lib" : "";
+ if (major >= 0 && minor >= 0)
+ return fmt::format("{}{}.so.{}.{}", prefix, libname, major, minor);
+ else if (major >= 0)
+ return fmt::format("{}{}.so.{}", prefix, libname, major);
+ else
+ return fmt::format("{}{}.so", prefix, libname);
+#endif
+}
+
+bool DynamicLibrary::Open(const char* filename) {
+#ifdef _WIN32
+ handle = reinterpret_cast<void*>(LoadLibraryA(filename));
+#else
+ handle = dlopen(filename, RTLD_NOW);
+#endif
+ return handle != nullptr;
+}
+
+void DynamicLibrary::Close() {
+ if (!IsOpen())
+ return;
+
+#ifdef _WIN32
+ FreeLibrary(reinterpret_cast<HMODULE>(handle));
+#else
+ dlclose(handle);
+#endif
+ handle = nullptr;
+}
+
+void* DynamicLibrary::GetSymbolAddress(const char* name) const {
+#ifdef _WIN32
+ return reinterpret_cast<void*>(GetProcAddress(reinterpret_cast<HMODULE>(handle), name));
+#else
+ return reinterpret_cast<void*>(dlsym(handle, name));
+#endif
+}
+
+} // namespace Common
diff --git a/src/common/dynamic_library.h b/src/common/dynamic_library.h
new file mode 100644
index 000000000..2a06372fd
--- /dev/null
+++ b/src/common/dynamic_library.h
@@ -0,0 +1,75 @@
+// Copyright 2019 Dolphin Emulator Project
+// Licensed under GPLv2+
+// Refer to the license.txt file included.
+
+#pragma once
+
+#include <string>
+
+namespace Common {
+
+/**
+ * Provides a platform-independent interface for loading a dynamic library and retrieving symbols.
+ * The interface maintains an internal reference count to allow one handle to be shared between
+ * multiple users.
+ */
+class DynamicLibrary final {
+public:
+ /// Default constructor, does not load a library.
+ explicit DynamicLibrary();
+
+ /// Automatically loads the specified library. Call IsOpen() to check validity before use.
+ explicit DynamicLibrary(const char* filename);
+
+ /// Moves the library.
+ DynamicLibrary(DynamicLibrary&&) noexcept;
+ DynamicLibrary& operator=(DynamicLibrary&&) noexcept;
+
+ /// Delete copies, we can't copy a dynamic library.
+ DynamicLibrary(const DynamicLibrary&) = delete;
+ DynamicLibrary& operator=(const DynamicLibrary&) = delete;
+
+ /// Closes the library.
+ ~DynamicLibrary();
+
+ /// Returns the specified library name with the platform-specific suffix added.
+ static std::string GetUnprefixedFilename(const char* filename);
+
+ /// Returns the specified library name in platform-specific format.
+ /// Major/minor versions will not be included if set to -1.
+ /// If libname already contains the "lib" prefix, it will not be added again.
+ /// Windows: LIBNAME-MAJOR-MINOR.dll
+ /// Linux: libLIBNAME.so.MAJOR.MINOR
+ /// Mac: libLIBNAME.MAJOR.MINOR.dylib
+ static std::string GetVersionedFilename(const char* libname, int major = -1, int minor = -1);
+
+ /// Returns true if a module is loaded, otherwise false.
+ bool IsOpen() const {
+ return handle != nullptr;
+ }
+
+ /// Loads (or replaces) the handle with the specified library file name.
+ /// Returns true if the library was loaded and can be used.
+ bool Open(const char* filename);
+
+ /// Unloads the library, any function pointers from this library are no longer valid.
+ void Close();
+
+ /// Returns the address of the specified symbol (function or variable) as an untyped pointer.
+ /// If the specified symbol does not exist in this library, nullptr is returned.
+ void* GetSymbolAddress(const char* name) const;
+
+ /// Obtains the address of the specified symbol, automatically casting to the correct type.
+ /// Returns true if the symbol was found and assigned, otherwise false.
+ template <typename T>
+ bool GetSymbol(const char* name, T* ptr) const {
+ *ptr = reinterpret_cast<T>(GetSymbolAddress(name));
+ return *ptr != nullptr;
+ }
+
+private:
+ /// Platform-dependent data type representing a dynamic library handle.
+ void* handle = nullptr;
+};
+
+} // namespace Common
diff --git a/src/common/file_util.cpp b/src/common/file_util.cpp
index 28a16077d..45b750e1e 100644
--- a/src/common/file_util.cpp
+++ b/src/common/file_util.cpp
@@ -974,6 +974,34 @@ bool IOFile::Flush() {
return IsOpen() && 0 == std::fflush(m_file);
}
+std::size_t IOFile::ReadImpl(void* data, std::size_t length, std::size_t data_size) const {
+ if (!IsOpen()) {
+ return std::numeric_limits<std::size_t>::max();
+ }
+
+ if (length == 0) {
+ return 0;
+ }
+
+ DEBUG_ASSERT(data != nullptr);
+
+ return std::fread(data, data_size, length, m_file);
+}
+
+std::size_t IOFile::WriteImpl(const void* data, std::size_t length, std::size_t data_size) {
+ if (!IsOpen()) {
+ return std::numeric_limits<std::size_t>::max();
+ }
+
+ if (length == 0) {
+ return 0;
+ }
+
+ DEBUG_ASSERT(data != nullptr);
+
+ return std::fwrite(data, data_size, length, m_file);
+}
+
bool IOFile::Resize(u64 size) {
return IsOpen() && 0 ==
#ifdef _WIN32
diff --git a/src/common/file_util.h b/src/common/file_util.h
index cde7ddf2d..f7a0c33fa 100644
--- a/src/common/file_util.h
+++ b/src/common/file_util.h
@@ -222,22 +222,15 @@ public:
static_assert(std::is_trivially_copyable_v<T>,
"Given array does not consist of trivially copyable objects");
- if (!IsOpen()) {
- return std::numeric_limits<std::size_t>::max();
- }
-
- return std::fread(data, sizeof(T), length, m_file);
+ return ReadImpl(data, length, sizeof(T));
}
template <typename T>
std::size_t WriteArray(const T* data, std::size_t length) {
static_assert(std::is_trivially_copyable_v<T>,
"Given array does not consist of trivially copyable objects");
- if (!IsOpen()) {
- return std::numeric_limits<std::size_t>::max();
- }
- return std::fwrite(data, sizeof(T), length, m_file);
+ return WriteImpl(data, length, sizeof(T));
}
template <typename T>
@@ -278,6 +271,9 @@ public:
}
private:
+ std::size_t ReadImpl(void* data, std::size_t length, std::size_t data_size) const;
+ std::size_t WriteImpl(const void* data, std::size_t length, std::size_t data_size);
+
std::FILE* m_file = nullptr;
};
diff --git a/src/common/page_table.cpp b/src/common/page_table.cpp
index 566b57b62..e5d3090d5 100644
--- a/src/common/page_table.cpp
+++ b/src/common/page_table.cpp
@@ -6,36 +6,20 @@
namespace Common {
-PageTable::PageTable(std::size_t page_size_in_bits) : page_size_in_bits{page_size_in_bits} {}
+PageTable::PageTable() = default;
PageTable::~PageTable() = default;
-void PageTable::Resize(std::size_t address_space_width_in_bits) {
- const std::size_t num_page_table_entries = 1ULL
- << (address_space_width_in_bits - page_size_in_bits);
-
+void PageTable::Resize(std::size_t address_space_width_in_bits, std::size_t page_size_in_bits,
+ bool has_attribute) {
+ const std::size_t num_page_table_entries{1ULL
+ << (address_space_width_in_bits - page_size_in_bits)};
pointers.resize(num_page_table_entries);
- attributes.resize(num_page_table_entries);
-
- // The default is a 39-bit address space, which causes an initial 1GB allocation size. If the
- // vector size is subsequently decreased (via resize), the vector might not automatically
- // actually reallocate/resize its underlying allocation, which wastes up to ~800 MB for
- // 36-bit titles. Call shrink_to_fit to reduce capacity to what's actually in use.
-
- pointers.shrink_to_fit();
- attributes.shrink_to_fit();
-}
-
-BackingPageTable::BackingPageTable(std::size_t page_size_in_bits) : PageTable{page_size_in_bits} {}
-
-BackingPageTable::~BackingPageTable() = default;
-
-void BackingPageTable::Resize(std::size_t address_space_width_in_bits) {
- PageTable::Resize(address_space_width_in_bits);
- const std::size_t num_page_table_entries = 1ULL
- << (address_space_width_in_bits - page_size_in_bits);
backing_addr.resize(num_page_table_entries);
- backing_addr.shrink_to_fit();
+
+ if (has_attribute) {
+ attributes.resize(num_page_table_entries);
+ }
}
} // namespace Common
diff --git a/src/common/page_table.h b/src/common/page_table.h
index dbc272ab7..1e8bd3187 100644
--- a/src/common/page_table.h
+++ b/src/common/page_table.h
@@ -5,9 +5,12 @@
#pragma once
#include <vector>
+
#include <boost/icl/interval_map.hpp>
+
#include "common/common_types.h"
#include "common/memory_hook.h"
+#include "common/virtual_buffer.h"
namespace Common {
@@ -47,7 +50,7 @@ struct SpecialRegion {
* mimics the way a real CPU page table works.
*/
struct PageTable {
- explicit PageTable(std::size_t page_size_in_bits);
+ PageTable();
~PageTable();
/**
@@ -56,40 +59,18 @@ struct PageTable {
*
* @param address_space_width_in_bits The address size width in bits.
*/
- void Resize(std::size_t address_space_width_in_bits);
+ void Resize(std::size_t address_space_width_in_bits, std::size_t page_size_in_bits,
+ bool has_attribute);
/**
* Vector of memory pointers backing each page. An entry can only be non-null if the
* corresponding entry in the `attributes` vector is of type `Memory`.
*/
- std::vector<u8*> pointers;
-
- /**
- * Contains MMIO handlers that back memory regions whose entries in the `attribute` vector is
- * of type `Special`.
- */
- boost::icl::interval_map<u64, std::set<SpecialRegion>> special_regions;
-
- /**
- * Vector of fine grained page attributes. If it is set to any value other than `Memory`, then
- * the corresponding entry in `pointers` MUST be set to null.
- */
- std::vector<PageType> attributes;
-
- const std::size_t page_size_in_bits{};
-};
-
-/**
- * A more advanced Page Table with the ability to save a backing address when using it
- * depends on another MMU.
- */
-struct BackingPageTable : PageTable {
- explicit BackingPageTable(std::size_t page_size_in_bits);
- ~BackingPageTable();
+ VirtualBuffer<u8*> pointers;
- void Resize(std::size_t address_space_width_in_bits);
+ VirtualBuffer<u64> backing_addr;
- std::vector<u64> backing_addr;
+ VirtualBuffer<PageType> attributes;
};
} // namespace Common
diff --git a/src/common/scope_exit.h b/src/common/scope_exit.h
index 1176a72b1..68ef5f197 100644
--- a/src/common/scope_exit.h
+++ b/src/common/scope_exit.h
@@ -12,10 +12,17 @@ template <typename Func>
struct ScopeExitHelper {
explicit ScopeExitHelper(Func&& func) : func(std::move(func)) {}
~ScopeExitHelper() {
- func();
+ if (active) {
+ func();
+ }
+ }
+
+ void Cancel() {
+ active = false;
}
Func func;
+ bool active{true};
};
template <typename Func>
diff --git a/src/common/virtual_buffer.cpp b/src/common/virtual_buffer.cpp
new file mode 100644
index 000000000..b426f4747
--- /dev/null
+++ b/src/common/virtual_buffer.cpp
@@ -0,0 +1,52 @@
+// Copyright 2020 yuzu Emulator Project
+// Licensed under GPLv2 or any later version
+// Refer to the license.txt file included.
+
+#ifdef _WIN32
+#include <windows.h>
+#else
+#include <stdio.h>
+#include <sys/mman.h>
+#include <sys/types.h>
+#if defined __APPLE__ || defined __FreeBSD__ || defined __OpenBSD__
+#include <sys/sysctl.h>
+#elif defined __HAIKU__
+#include <OS.h>
+#else
+#include <sys/sysinfo.h>
+#endif
+#endif
+
+#include "common/assert.h"
+#include "common/virtual_buffer.h"
+
+namespace Common {
+
+void* AllocateMemoryPages(std::size_t size) {
+#ifdef _WIN32
+ void* base{VirtualAlloc(nullptr, size, MEM_COMMIT, PAGE_READWRITE)};
+#else
+ void* base{mmap(nullptr, size, PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE, -1, 0)};
+
+ if (base == MAP_FAILED) {
+ base = nullptr;
+ }
+#endif
+
+ ASSERT(base);
+
+ return base;
+}
+
+void FreeMemoryPages(void* base, std::size_t size) {
+ if (!base) {
+ return;
+ }
+#ifdef _WIN32
+ ASSERT(VirtualFree(base, 0, MEM_RELEASE));
+#else
+ ASSERT(munmap(base, size) == 0);
+#endif
+}
+
+} // namespace Common
diff --git a/src/common/virtual_buffer.h b/src/common/virtual_buffer.h
new file mode 100644
index 000000000..da064e59e
--- /dev/null
+++ b/src/common/virtual_buffer.h
@@ -0,0 +1,58 @@
+// Copyright 2020 yuzu Emulator Project
+// Licensed under GPLv2 or any later version
+// Refer to the license.txt file included.
+
+#pragma once
+
+#include "common/common_funcs.h"
+
+namespace Common {
+
+void* AllocateMemoryPages(std::size_t size);
+void FreeMemoryPages(void* base, std::size_t size);
+
+template <typename T>
+class VirtualBuffer final : NonCopyable {
+public:
+ constexpr VirtualBuffer() = default;
+ explicit VirtualBuffer(std::size_t count) : alloc_size{count * sizeof(T)} {
+ base_ptr = reinterpret_cast<T*>(AllocateMemoryPages(alloc_size));
+ }
+
+ ~VirtualBuffer() {
+ FreeMemoryPages(base_ptr, alloc_size);
+ }
+
+ void resize(std::size_t count) {
+ FreeMemoryPages(base_ptr, alloc_size);
+
+ alloc_size = count * sizeof(T);
+ base_ptr = reinterpret_cast<T*>(AllocateMemoryPages(alloc_size));
+ }
+
+ constexpr const T& operator[](std::size_t index) const {
+ return base_ptr[index];
+ }
+
+ constexpr T& operator[](std::size_t index) {
+ return base_ptr[index];
+ }
+
+ constexpr T* data() {
+ return base_ptr;
+ }
+
+ constexpr const T* data() const {
+ return base_ptr;
+ }
+
+ constexpr std::size_t size() const {
+ return alloc_size / sizeof(T);
+ }
+
+private:
+ std::size_t alloc_size{};
+ T* base_ptr{};
+};
+
+} // namespace Common