Common: Add DynamicLibrary

This commit is contained in:
Stenzek 2024-02-25 15:47:23 +10:00
parent b5b5f6bc67
commit fcb8ce1ebc
No known key found for this signature in database
5 changed files with 211 additions and 0 deletions

View File

@ -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

View File

@ -10,6 +10,7 @@
<ClInclude Include="byte_stream.h" />
<ClInclude Include="crash_handler.h" />
<ClInclude Include="dimensional_array.h" />
<ClInclude Include="dynamic_library.h" />
<ClInclude Include="easing.h" />
<ClInclude Include="error.h" />
<ClInclude Include="fastjmp.h" />
@ -47,6 +48,7 @@
<ClCompile Include="assert.cpp" />
<ClCompile Include="byte_stream.cpp" />
<ClCompile Include="crash_handler.cpp" />
<ClCompile Include="dynamic_library.cpp" />
<ClCompile Include="error.cpp" />
<ClCompile Include="fastjmp.cpp" />
<ClCompile Include="file_system.cpp" />

View File

@ -45,6 +45,7 @@
<ClInclude Include="thirdparty\SmallVector.h">
<Filter>thirdparty</Filter>
</ClInclude>
<ClInclude Include="dynamic_library.h" />
</ItemGroup>
<ItemGroup>
<ClCompile Include="small_string.cpp" />
@ -73,6 +74,7 @@
<ClCompile Include="thirdparty\SmallVector.cpp">
<Filter>thirdparty</Filter>
</ClCompile>
<ClCompile Include="dynamic_library.cpp" />
</ItemGroup>
<ItemGroup>
<Natvis Include="bitfield.natvis" />

View File

@ -0,0 +1,132 @@
// SPDX-FileCopyrightText: 2019-2024 Connor McLaughlin <stenzek@gmail.com>
// 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 <cstring>
#ifdef _WIN32
#include "common/windows_headers.h"
#else
#include <dlfcn.h>
#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<void*>(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 : "<UNKNOWN>");
return false;
}
return true;
#endif
}
void DynamicLibrary::Close()
{
if (!IsOpen())
return;
#ifdef _WIN32
FreeLibrary(reinterpret_cast<HMODULE>(m_handle));
#else
dlclose(m_handle);
#endif
m_handle = nullptr;
}
void* DynamicLibrary::GetSymbolAddress(const char* name) const
{
#ifdef _WIN32
return reinterpret_cast<void*>(GetProcAddress(reinterpret_cast<HMODULE>(m_handle), name));
#else
return reinterpret_cast<void*>(dlsym(m_handle, name));
#endif
}
DynamicLibrary& DynamicLibrary::operator=(DynamicLibrary&& move)
{
Close();
m_handle = move.m_handle;
move.m_handle = nullptr;
return *this;
}

View File

@ -0,0 +1,73 @@
// SPDX-FileCopyrightText: 2019-2024 Connor McLaughlin <stenzek@gmail.com>
// SPDX-License-Identifier: (GPL-3.0 OR CC-BY-NC-ND-4.0)
#pragma once
#include <string>
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<typename T>
bool GetSymbol(const char* name, T* ptr) const
{
*ptr = reinterpret_cast<T>(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;
};