From fcb8ce1ebc6190a11326c0ff3452f43017d4f667 Mon Sep 17 00:00:00 2001 From: Stenzek Date: Sun, 25 Feb 2024 15:47:23 +1000 Subject: [PATCH] Common: Add DynamicLibrary --- src/common/CMakeLists.txt | 2 + src/common/common.vcxproj | 2 + src/common/common.vcxproj.filters | 2 + src/common/dynamic_library.cpp | 132 ++++++++++++++++++++++++++++++ src/common/dynamic_library.h | 73 +++++++++++++++++ 5 files changed, 211 insertions(+) create mode 100644 src/common/dynamic_library.cpp create mode 100644 src/common/dynamic_library.h diff --git a/src/common/CMakeLists.txt b/src/common/CMakeLists.txt index a2143bc23..f69f9e687 100644 --- a/src/common/CMakeLists.txt +++ b/src/common/CMakeLists.txt @@ -10,6 +10,8 @@ add_library(common crash_handler.cpp crash_handler.h dimensional_array.h + dynamic_library.cpp + dynamic_library.h error.cpp error.h fastjmp.cpp diff --git a/src/common/common.vcxproj b/src/common/common.vcxproj index 5603c60c9..a6663a857 100644 --- a/src/common/common.vcxproj +++ b/src/common/common.vcxproj @@ -10,6 +10,7 @@ + @@ -47,6 +48,7 @@ + diff --git a/src/common/common.vcxproj.filters b/src/common/common.vcxproj.filters index 6cc4f98ea..1e972ee0d 100644 --- a/src/common/common.vcxproj.filters +++ b/src/common/common.vcxproj.filters @@ -45,6 +45,7 @@ thirdparty + @@ -73,6 +74,7 @@ thirdparty + diff --git a/src/common/dynamic_library.cpp b/src/common/dynamic_library.cpp new file mode 100644 index 000000000..493faa30c --- /dev/null +++ b/src/common/dynamic_library.cpp @@ -0,0 +1,132 @@ +// SPDX-FileCopyrightText: 2019-2024 Connor McLaughlin +// SPDX-License-Identifier: (GPL-3.0 OR CC-BY-NC-ND-4.0) + +#include "common/dynamic_library.h" +#include "common/assert.h" +#include "common/error.h" +#include "common/log.h" +#include "common/small_string.h" +#include "common/string_util.h" + +#include "fmt/format.h" +#include + +#ifdef _WIN32 +#include "common/windows_headers.h" +#else +#include +#endif + +Log_SetChannel(DynamicLibrary); + +DynamicLibrary::DynamicLibrary() = default; + +DynamicLibrary::DynamicLibrary(const char* filename) +{ + Error error; + if (!Open(filename, &error)) + Log_ErrorPrint(error.GetDescription()); +} + +DynamicLibrary::DynamicLibrary(DynamicLibrary&& move) : m_handle(move.m_handle) +{ + move.m_handle = nullptr; +} + +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, Error* error) +{ +#ifdef _WIN32 + m_handle = reinterpret_cast(LoadLibraryW(StringUtil::UTF8StringToWideString(filename).c_str())); + if (!m_handle) + { + Error::SetWin32(error, TinyString::from_format("Loading {} failed: ", filename), GetLastError()); + return false; + } + + return true; +#else + m_handle = dlopen(filename, RTLD_NOW); + if (!m_handle) + { + const char* err = dlerror(); + Error::SetStringFmt(error, "Loading {} failed: {}", filename, err ? err : ""); + return false; + } + + return true; +#endif +} + +void DynamicLibrary::Close() +{ + if (!IsOpen()) + return; + +#ifdef _WIN32 + FreeLibrary(reinterpret_cast(m_handle)); +#else + dlclose(m_handle); +#endif + m_handle = nullptr; +} + +void* DynamicLibrary::GetSymbolAddress(const char* name) const +{ +#ifdef _WIN32 + return reinterpret_cast(GetProcAddress(reinterpret_cast(m_handle), name)); +#else + return reinterpret_cast(dlsym(m_handle, name)); +#endif +} + +DynamicLibrary& DynamicLibrary::operator=(DynamicLibrary&& move) +{ + Close(); + m_handle = move.m_handle; + move.m_handle = nullptr; + return *this; +} diff --git a/src/common/dynamic_library.h b/src/common/dynamic_library.h new file mode 100644 index 000000000..a9d8fb764 --- /dev/null +++ b/src/common/dynamic_library.h @@ -0,0 +1,73 @@ +// SPDX-FileCopyrightText: 2019-2024 Connor McLaughlin +// SPDX-License-Identifier: (GPL-3.0 OR CC-BY-NC-ND-4.0) + +#pragma once + +#include + +class Error; + +/** + * 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. + DynamicLibrary(); + + /// Automatically loads the specified library. Call IsOpen() to check validity before use. + DynamicLibrary(const char* filename); + + /// Move constructor, transfers ownership. + DynamicLibrary(DynamicLibrary&& move); + + /// 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 m_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, Error* error); + + /// 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 + bool GetSymbol(const char* name, T* ptr) const + { + *ptr = reinterpret_cast(GetSymbolAddress(name)); + return *ptr != nullptr; + } + + /// Move assignment, transfer ownership. + DynamicLibrary& operator=(DynamicLibrary&& move); + +private: + DynamicLibrary(const DynamicLibrary&) = delete; + DynamicLibrary& operator=(const DynamicLibrary&) = delete; + + /// Platform-dependent data type representing a dynamic library handle. + void* m_handle = nullptr; +};