Improve DYNAMIC_IMPORT

Don't call get_proc_address every time if if failed.
Also rename Utilities/dynamic_library.h to util/dyn_lib.hpp
This commit is contained in:
Nekotekina 2020-11-10 01:11:30 +03:00
parent cdaa8cb5c4
commit bd5253047b
7 changed files with 27 additions and 23 deletions

View File

@ -4,7 +4,7 @@
#include "types.h"
#include "util/atomic.hpp"
#include "dynamic_library.h"
#include "util/dyn_lib.hpp"
#ifdef _WIN32
#define NOMINMAX

View File

@ -40,11 +40,11 @@ target_sources(rpcs3_emu PRIVATE
../util/yaml.cpp
../util/cereal.cpp
../util/vm_native.cpp
../util/dyn_lib.cpp
../../Utilities/bin_patch.cpp
../../Utilities/cheat_info.cpp
../../Utilities/cond.cpp
../../Utilities/Config.cpp
../../Utilities/dynamic_library.cpp
../../Utilities/File.cpp
../../Utilities/JIT.cpp
../../Utilities/LUrlParser.cpp

View File

@ -141,7 +141,9 @@
<ClCompile Include="..\Utilities\cond.cpp">
<PrecompiledHeader>NotUsing</PrecompiledHeader>
</ClCompile>
<ClCompile Include="..\Utilities\dynamic_library.cpp" />
<ClCompile Include="util\dyn_lib.cpp">
<PrecompiledHeader>NotUsing</PrecompiledHeader>
</ClCompile>
<ClCompile Include="..\Utilities\JIT.cpp">
<PrecompiledHeader>NotUsing</PrecompiledHeader>
</ClCompile>
@ -527,7 +529,6 @@
<ClInclude Include="..\Utilities\cond.h" />
<ClInclude Include="..\Utilities\CRC.h" />
<ClInclude Include="..\Utilities\date_time.h" />
<ClInclude Include="..\Utilities\dynamic_library.h" />
<ClInclude Include="..\Utilities\geometry.h" />
<ClInclude Include="..\Utilities\hash.h" />
<ClInclude Include="..\Utilities\JIT.h" />
@ -540,6 +541,7 @@
<ClInclude Include="util\fixed_typemap.hpp" />
<ClInclude Include="util\init_mutex.hpp" />
<ClInclude Include="util\logs.hpp" />
<ClInclude Include="..\Utilities\dyn_lib.hpp" />
<ClInclude Include="..\Utilities\File.h" />
<ClInclude Include="..\Utilities\Config.h" />
<ClInclude Include="..\Utilities\rXml.h" />

View File

@ -734,7 +734,7 @@
<ClCompile Include="Emu\IdManager.cpp">
<Filter>Emu</Filter>
</ClCompile>
<ClCompile Include="..\Utilities\dynamic_library.cpp">
<ClCompile Include="util\dyn_lib.cpp">
<Filter>Utilities</Filter>
</ClCompile>
<ClCompile Include="..\Utilities\version.cpp">
@ -1561,7 +1561,7 @@
<ClInclude Include="Emu\RSX\rsx_cache.h">
<Filter>Emu\GPU\RSX</Filter>
</ClInclude>
<ClInclude Include="..\Utilities\dynamic_library.h">
<ClInclude Include="util\dyn_lib.hpp">
<Filter>Utilities</Filter>
</ClInclude>
<ClInclude Include="..\Utilities\version.h">

View File

@ -17,7 +17,7 @@
#include "Utilities/sema.h"
#ifdef _WIN32
#include <windows.h>
#include "Utilities/dynamic_library.h"
#include "util/dyn_lib.hpp"
DYNAMIC_IMPORT("ntdll.dll", NtQueryTimerResolution, NTSTATUS(PULONG MinimumResolution, PULONG MaximumResolution, PULONG CurrentResolution));
DYNAMIC_IMPORT("ntdll.dll", NtSetTimerResolution, NTSTATUS(ULONG DesiredResolution, BOOLEAN SetResolution, PULONG CurrentResolution));
#else

View File

@ -1,5 +1,5 @@
#include "stdafx.h"
#include "dynamic_library.h"
#include "dyn_lib.hpp"
#ifdef _WIN32
#include <Windows.h>

View File

@ -53,40 +53,42 @@ namespace utils
template <typename R, typename... Args>
struct dynamic_import<R(Args...)>
{
R (*ptr)(Args...);
atomic_t<std::uintptr_t> ptr;
const char* const lib;
const char* const name;
// Constant initialization
constexpr dynamic_import(const char* lib, const char* name)
: ptr(nullptr)
constexpr dynamic_import(const char* lib, const char* name) noexcept
: ptr(-1)
, lib(lib)
, name(name)
{
}
void init()
void init() noexcept
{
if (!ptr)
{
// TODO: atomic
ptr = reinterpret_cast<R (*)(Args...)>(get_proc_address(lib, name));
}
ptr.release(reinterpret_cast<std::uintptr_t>(get_proc_address(lib, name)));
}
operator bool()
operator bool() noexcept
{
init();
if (ptr == umax) [[unlikely]]
{
init();
}
return ptr;
return ptr != 0;
}
// Caller
R operator()(Args... args)
R operator()(Args... args) noexcept
{
init();
if (ptr == umax) [[unlikely]]
{
init();
}
return ptr(args...);
return reinterpret_cast<R (*)(Args...)>(ptr.load())(args...);
}
};
}