summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/common/CMakeLists.txt2
-rw-r--r--src/common/dynamic_library.cpp106
-rw-r--r--src/common/dynamic_library.h75
3 files changed, 183 insertions, 0 deletions
diff --git a/src/common/CMakeLists.txt b/src/common/CMakeLists.txt
index fbebed715..eeceaa655 100644
--- a/src/common/CMakeLists.txt
+++ b/src/common/CMakeLists.txt
@@ -106,6 +106,8 @@ add_library(common STATIC
106 common_funcs.h 106 common_funcs.h
107 common_paths.h 107 common_paths.h
108 common_types.h 108 common_types.h
109 dynamic_library.cpp
110 dynamic_library.h
109 file_util.cpp 111 file_util.cpp
110 file_util.h 112 file_util.h
111 hash.h 113 hash.h
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 @@
1// Copyright 2019 Dolphin Emulator Project
2// Licensed under GPLv2+
3// Refer to the license.txt file included.
4
5#include <cstring>
6#include <string>
7#include <utility>
8
9#include <fmt/format.h>
10
11#include "common/dynamic_library.h"
12
13#ifdef _WIN32
14#include <windows.h>
15#else
16#include <dlfcn.h>
17#endif
18
19namespace Common {
20
21DynamicLibrary::DynamicLibrary() = default;
22
23DynamicLibrary::DynamicLibrary(const char* filename) {
24 Open(filename);
25}
26
27DynamicLibrary::DynamicLibrary(DynamicLibrary&& rhs) noexcept
28 : handle{std::exchange(rhs.handle, nullptr)} {}
29
30DynamicLibrary& DynamicLibrary::operator=(DynamicLibrary&& rhs) noexcept {
31 Close();
32 handle = std::exchange(rhs.handle, nullptr);
33 return *this;
34}
35
36DynamicLibrary::~DynamicLibrary() {
37 Close();
38}
39
40std::string DynamicLibrary::GetUnprefixedFilename(const char* filename) {
41#if defined(_WIN32)
42 return std::string(filename) + ".dll";
43#elif defined(__APPLE__)
44 return std::string(filename) + ".dylib";
45#else
46 return std::string(filename) + ".so";
47#endif
48}
49
50std::string DynamicLibrary::GetVersionedFilename(const char* libname, int major, int minor) {
51#if defined(_WIN32)
52 if (major >= 0 && minor >= 0)
53 return fmt::format("{}-{}-{}.dll", libname, major, minor);
54 else if (major >= 0)
55 return fmt::format("{}-{}.dll", libname, major);
56 else
57 return fmt::format("{}.dll", libname);
58#elif defined(__APPLE__)
59 const char* prefix = std::strncmp(libname, "lib", 3) ? "lib" : "";
60 if (major >= 0 && minor >= 0)
61 return fmt::format("{}{}.{}.{}.dylib", prefix, libname, major, minor);
62 else if (major >= 0)
63 return fmt::format("{}{}.{}.dylib", prefix, libname, major);
64 else
65 return fmt::format("{}{}.dylib", prefix, libname);
66#else
67 const char* prefix = std::strncmp(libname, "lib", 3) ? "lib" : "";
68 if (major >= 0 && minor >= 0)
69 return fmt::format("{}{}.so.{}.{}", prefix, libname, major, minor);
70 else if (major >= 0)
71 return fmt::format("{}{}.so.{}", prefix, libname, major);
72 else
73 return fmt::format("{}{}.so", prefix, libname);
74#endif
75}
76
77bool DynamicLibrary::Open(const char* filename) {
78#ifdef _WIN32
79 handle = reinterpret_cast<void*>(LoadLibraryA(filename));
80#else
81 handle = dlopen(filename, RTLD_NOW);
82#endif
83 return handle != nullptr;
84}
85
86void DynamicLibrary::Close() {
87 if (!IsOpen())
88 return;
89
90#ifdef _WIN32
91 FreeLibrary(reinterpret_cast<HMODULE>(handle));
92#else
93 dlclose(handle);
94#endif
95 handle = nullptr;
96}
97
98void* DynamicLibrary::GetSymbolAddress(const char* name) const {
99#ifdef _WIN32
100 return reinterpret_cast<void*>(GetProcAddress(reinterpret_cast<HMODULE>(handle), name));
101#else
102 return reinterpret_cast<void*>(dlsym(handle, name));
103#endif
104}
105
106} // 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 @@
1// Copyright 2019 Dolphin Emulator Project
2// Licensed under GPLv2+
3// Refer to the license.txt file included.
4
5#pragma once
6
7#include <string>
8
9namespace Common {
10
11/**
12 * Provides a platform-independent interface for loading a dynamic library and retrieving symbols.
13 * The interface maintains an internal reference count to allow one handle to be shared between
14 * multiple users.
15 */
16class DynamicLibrary final {
17public:
18 /// Default constructor, does not load a library.
19 explicit DynamicLibrary();
20
21 /// Automatically loads the specified library. Call IsOpen() to check validity before use.
22 explicit DynamicLibrary(const char* filename);
23
24 /// Moves the library.
25 DynamicLibrary(DynamicLibrary&&) noexcept;
26 DynamicLibrary& operator=(DynamicLibrary&&) noexcept;
27
28 /// Delete copies, we can't copy a dynamic library.
29 DynamicLibrary(const DynamicLibrary&) = delete;
30 DynamicLibrary& operator=(const DynamicLibrary&) = delete;
31
32 /// Closes the library.
33 ~DynamicLibrary();
34
35 /// Returns the specified library name with the platform-specific suffix added.
36 static std::string GetUnprefixedFilename(const char* filename);
37
38 /// Returns the specified library name in platform-specific format.
39 /// Major/minor versions will not be included if set to -1.
40 /// If libname already contains the "lib" prefix, it will not be added again.
41 /// Windows: LIBNAME-MAJOR-MINOR.dll
42 /// Linux: libLIBNAME.so.MAJOR.MINOR
43 /// Mac: libLIBNAME.MAJOR.MINOR.dylib
44 static std::string GetVersionedFilename(const char* libname, int major = -1, int minor = -1);
45
46 /// Returns true if a module is loaded, otherwise false.
47 bool IsOpen() const {
48 return handle != nullptr;
49 }
50
51 /// Loads (or replaces) the handle with the specified library file name.
52 /// Returns true if the library was loaded and can be used.
53 bool Open(const char* filename);
54
55 /// Unloads the library, any function pointers from this library are no longer valid.
56 void Close();
57
58 /// Returns the address of the specified symbol (function or variable) as an untyped pointer.
59 /// If the specified symbol does not exist in this library, nullptr is returned.
60 void* GetSymbolAddress(const char* name) const;
61
62 /// Obtains the address of the specified symbol, automatically casting to the correct type.
63 /// Returns true if the symbol was found and assigned, otherwise false.
64 template <typename T>
65 bool GetSymbol(const char* name, T* ptr) const {
66 *ptr = reinterpret_cast<T>(GetSymbolAddress(name));
67 return *ptr != nullptr;
68 }
69
70private:
71 /// Platform-dependent data type representing a dynamic library handle.
72 void* handle = nullptr;
73};
74
75} // namespace Common