Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
alexbevi
GitHub Repository: alexbevi/BizHawk
Path: blob/master/libsnes/bsnes/nall/dl.hpp
2 views
1
#ifndef NALL_DL_HPP
2
#define NALL_DL_HPP
3
4
//dynamic linking support
5
6
#include <nall/intrinsics.hpp>
7
#include <nall/stdint.hpp>
8
#include <nall/string.hpp>
9
#include <nall/utility.hpp>
10
11
#if defined(PLATFORM_X) || defined(PLATFORM_OSX)
12
#include <dlfcn.h>
13
#elif defined(PLATFORM_WINDOWS)
14
#include <windows.h>
15
#include <nall/windows/utf8.hpp>
16
#endif
17
18
namespace nall {
19
struct library {
20
bool opened() const { return handle; }
21
bool open(const char*, const char* = "");
22
bool open_absolute(const char*);
23
void* sym(const char*);
24
void close();
25
26
library() : handle(0) {}
27
~library() { close(); }
28
29
library& operator=(const library&) = delete;
30
library(const library&) = delete;
31
32
private:
33
uintptr_t handle;
34
};
35
36
#if defined(PLATFORM_X)
37
inline bool library::open(const char *name, const char *path) {
38
if(handle) close();
39
handle = (uintptr_t)dlopen(string(path, *path && !strend(path, "/") ? "/" : "", "lib", name, ".so"), RTLD_LAZY);
40
if(!handle) handle = (uintptr_t)dlopen(string("/usr/local/lib/lib", name, ".so"), RTLD_LAZY);
41
return handle;
42
}
43
44
inline bool library::open_absolute(const char *name) {
45
if(handle) close();
46
handle = (uintptr_t)dlopen(name, RTLD_LAZY);
47
return handle;
48
}
49
50
inline void* library::sym(const char *name) {
51
if(!handle) return 0;
52
return dlsym((void*)handle, name);
53
}
54
55
inline void library::close() {
56
if(!handle) return;
57
dlclose((void*)handle);
58
handle = 0;
59
}
60
#elif defined(PLATFORM_OSX)
61
inline bool library::open(const char *name, const char *path) {
62
if(handle) close();
63
handle = (uintptr_t)dlopen(string(path, *path && !strend(path, "/") ? "/" : "", "lib", name, ".dylib"), RTLD_LAZY);
64
if(!handle) handle = (uintptr_t)dlopen(string("/usr/local/lib/lib", name, ".dylib"), RTLD_LAZY);
65
return handle;
66
}
67
68
inline bool library::open_absolute(const char *name) {
69
if(handle) close();
70
handle = (uintptr_t)dlopen(name, RTLD_LAZY);
71
return handle;
72
}
73
74
inline void* library::sym(const char *name) {
75
if(!handle) return 0;
76
return dlsym((void*)handle, name);
77
}
78
79
inline void library::close() {
80
if(!handle) return;
81
dlclose((void*)handle);
82
handle = 0;
83
}
84
#elif defined(PLATFORM_WINDOWS)
85
inline bool library::open(const char *name, const char *path) {
86
if(handle) close();
87
string filepath(path, *path && !strend(path, "/") && !strend(path, "\\") ? "\\" : "", name, ".dll");
88
handle = (uintptr_t)LoadLibraryW(utf16_t(filepath));
89
return handle;
90
}
91
92
inline bool library::open_absolute(const char *name) {
93
if(handle) close();
94
handle = (uintptr_t)LoadLibraryW(utf16_t(name));
95
return handle;
96
}
97
98
inline void* library::sym(const char *name) {
99
if(!handle) return 0;
100
return (void*)GetProcAddress((HMODULE)handle, name);
101
}
102
103
inline void library::close() {
104
if(!handle) return;
105
FreeLibrary((HMODULE)handle);
106
handle = 0;
107
}
108
#else
109
inline bool library::open(const char*, const char*) { return false; }
110
inline void* library::sym(const char*) { return 0; }
111
inline void library::close() {}
112
#endif
113
};
114
115
#endif
116
117