-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathLoadLibrary.cpp
More file actions
168 lines (141 loc) · 4.96 KB
/
LoadLibrary.cpp
File metadata and controls
168 lines (141 loc) · 4.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/*
This file is part of the Util library.
Copyright (C) 2017 Sascha Brandt <myeti@mail.upb.de>
This library is subject to the terms of the Mozilla Public License, v. 2.0.
You should have received a copy of the MPL along with this library; see the
file LICENSE. If not, you can obtain one at http://mozilla.org/MPL/2.0/.
*/
#include "LoadLibrary.h"
#include "Macros.h"
#if defined(UTIL_HAVE_LIB_SDL2)
COMPILER_WARN_PUSH
COMPILER_WARN_OFF_GCC(-Wswitch-default)
#include <SDL_loadso.h>
COMPILER_WARN_POP
#elif defined(_WIN32)
#include <windows.h>
#elif defined(__APPLE__) || defined(__linux__) || defined(__unix__)
#include <stdio.h>
#include <dlfcn.h>
#endif
#include <unordered_map>
#include <memory>
#include <sstream>
using namespace Util;
struct LibraryHandle {
StringIdentifier id;
#if defined(_WIN32)
HINSTANCE handle;
#else
void* handle;
#endif
};
typedef std::unordered_map<StringIdentifier, LibraryHandle> LibraryHandles_t;
static LibraryHandles_t & getLibraryHandles() {
static std::unique_ptr<LibraryHandles_t> libraryHandles(new LibraryHandles_t);
return *libraryHandles.get();
}
#if defined(UTIL_HAVE_LIB_SDL2)
const StringIdentifier Util::loadLibrary(const std::string& filename) {
StringIdentifier libraryId(filename);
auto & libraryHandles = getLibraryHandles();
auto entry = libraryHandles.find(libraryId);
if (entry == libraryHandles.cend()) {
void* handle = SDL_LoadObject(filename.c_str());
if(!handle) {
WARN(std::string("Util::loadLibrary failed: ") + SDL_GetError());
return StringIdentifier(0);
}
libraryHandles[libraryId] = {libraryId, handle};
}
return libraryId;
}
void* Util::loadFunction(const StringIdentifier& libraryId, const std::string& name) {
auto & libraryHandles = getLibraryHandles();
auto entry = libraryHandles.find(libraryId);
if (entry == libraryHandles.cend()) {
WARN(std::string("Util::loadFunction failed: ") + libraryId.toString() + std::string(" not loaded!"));
return nullptr;
}
void* fnHandle = SDL_LoadFunction(entry->second.handle, name.c_str());
if(!fnHandle) {
WARN(std::string("Util::loadFunction failed: ") + SDL_GetError());
}
return fnHandle;
}
void Util::unloadLibrary(const StringIdentifier& libraryId) {
auto & libraryHandles = getLibraryHandles();
auto entry = libraryHandles.find(libraryId);
if (entry != libraryHandles.cend()) {
SDL_UnloadObject(entry->second.handle);
libraryHandles.erase(entry);
}
}
#else
using namespace Util;
const StringIdentifier Util::loadLibrary(const std::string& filename) {
StringIdentifier libraryId(filename);
auto & libraryHandles = getLibraryHandles();
auto entry = libraryHandles.find(libraryId);
std::ostringstream err;
if (entry == libraryHandles.cend()) {
#if defined(_WIN32) || defined(_WIN64)
HINSTANCE handle = LoadLibrary(filename.c_str());
if(!handle) {
DWORD errorMessageID = GetLastError();
LPSTR messageBuffer = nullptr;
size_t size = FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
NULL, errorMessageID, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPSTR)&messageBuffer, 0, NULL);
std::string message(messageBuffer, size);
LocalFree(messageBuffer);
err << message;
}
#elif defined(__APPLE__) || defined(__linux__) || defined(__unix__)
std::string libPath = filename;
if(libPath.find("/") == std::string::npos)
libPath = "./" + libPath; // need to prepend "./" for some distributions
void* handle = dlopen(libPath.c_str(), RTLD_NOW|RTLD_LOCAL);
if(!handle) err << dlerror();
#else
void* handle = nullptr;
#endif
if(!handle) {
WARN(std::string("Util::loadLibrary failed for file '" + filename + "': " + err.str()));
return StringIdentifier(0);
}
libraryHandles[libraryId] = {libraryId, handle};
}
return libraryId;
}
void* Util::loadFunction(const StringIdentifier& libraryId, const std::string& name) {
auto & libraryHandles = getLibraryHandles();
auto entry = libraryHandles.find(libraryId);
std::ostringstream err;
if (entry == libraryHandles.cend()) {
WARN(std::string("Util::loadFunction failed: ") + libraryId.toString() + std::string(" not loaded!"));
return nullptr;
}
#if defined(_WIN32) || defined(_WIN64)
void* fnHandle = reinterpret_cast<void*>(GetProcAddress(entry->second.handle, name.c_str()));
#elif defined(__APPLE__) || defined(__linux__) || defined(__unix__)
void* fnHandle = dlsym(entry->second.handle, name.c_str());
if(!fnHandle) err << dlerror();
#endif
if(!fnHandle) {
WARN(std::string("Util::loadFunction failed: ") + err.str());
}
return fnHandle;
}
void Util::unloadLibrary(const StringIdentifier& libraryId) {
auto & libraryHandles = getLibraryHandles();
auto entry = libraryHandles.find(libraryId);
if (entry != libraryHandles.cend()) {
#if defined(_WIN32) || defined(_WIN64)
FreeLibrary(entry->second.handle);
#elif defined(__APPLE__) || defined(__linux__) || defined(__unix__)
dlclose(entry->second.handle);
#endif
libraryHandles.erase(entry);
}
}
#endif