forked from pytorch/pytorch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DynamicLibrary.cpp
108 lines (90 loc) · 2.63 KB
/
DynamicLibrary.cpp
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
#include <c10/util/Exception.h>
#include <c10/util/Unicode.h>
#include <ATen/DynamicLibrary.h>
#include <ATen/Utils.h>
#ifndef _WIN32
#include <dlfcn.h>
#include <libgen.h>
#else
#include <c10/util/win32-headers.h>
#endif
namespace at {
#ifndef C10_MOBILE
#ifndef _WIN32
// Unix
static void* checkDL(void* x) {
if (!x) {
TORCH_CHECK_WITH(DynamicLibraryError, false, "Error in dlopen or dlsym: ", dlerror());
}
return x;
}
DynamicLibrary::DynamicLibrary(const char* name, const char* alt_name, bool leak_handle_): leak_handle(leak_handle_), handle(dlopen(name, RTLD_LOCAL | RTLD_NOW)) {
if (!handle) {
if (alt_name) {
handle = dlopen(alt_name, RTLD_LOCAL | RTLD_NOW);
if (!handle) {
TORCH_CHECK_WITH(DynamicLibraryError, false, "Error in dlopen for library ", name, "and ", alt_name);
}
} else {
TORCH_CHECK_WITH(DynamicLibraryError, false, "Error in dlopen: ", dlerror());
}
}
}
void* DynamicLibrary::sym(const char* name) {
AT_ASSERT(handle);
return checkDL(dlsym(handle, name));
}
DynamicLibrary::~DynamicLibrary() {
if (!handle || leak_handle) {
return;
}
dlclose(handle);
}
#else
// Windows
DynamicLibrary::DynamicLibrary(const char* name, const char* alt_name, bool leak_handle_): leak_handle(leak_handle_) {
// NOLINTNEXTLINE(hicpp-signed-bitwise)
HMODULE theModule;
bool reload = true;
auto wname = c10::u8u16(name);
// Check if LOAD_LIBRARY_SEARCH_DEFAULT_DIRS is supported
if (GetProcAddress(GetModuleHandleW(L"KERNEL32.DLL"), "AddDllDirectory") != NULL) {
theModule = LoadLibraryExW(
wname.c_str(),
NULL,
LOAD_LIBRARY_SEARCH_DEFAULT_DIRS);
if (theModule != NULL || (GetLastError() != ERROR_MOD_NOT_FOUND)) {
reload = false;
}
}
if (reload) {
theModule = LoadLibraryW(wname.c_str());
}
if (theModule) {
handle = theModule;
} else {
char buf[256];
DWORD dw = GetLastError();
FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
NULL, dw, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
buf, (sizeof(buf) / sizeof(char)), NULL);
TORCH_CHECK_WITH(DynamicLibraryError, false, "error in LoadLibrary for ", name, ". WinError ", dw, ": ", buf);
}
}
void* DynamicLibrary::sym(const char* name) {
AT_ASSERT(handle);
FARPROC procAddress = GetProcAddress((HMODULE)handle, name);
if (!procAddress) {
TORCH_CHECK_WITH(DynamicLibraryError, false, "error in GetProcAddress");
}
return (void*)procAddress;
}
DynamicLibrary::~DynamicLibrary() {
if (!handle || leak_handle) {
return;
}
FreeLibrary((HMODULE)handle);
}
#endif
#endif
} // namespace at