-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathDynamicLibraryModule.cpp
executable file
·85 lines (74 loc) · 2 KB
/
DynamicLibraryModule.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
//==============================================================================
// Copyright (c) 2011-2016 Advanced Micro Devices, Inc. All rights reserved.
/// \author AMD Developer Tools
/// \file
/// \brief Base class for dynamic loading of .so/.dll.
//==============================================================================
#include "DynamicLibraryModule.h"
DynamicLibraryModule::DynamicLibraryModule(void)
: m_Module(NULL)
{
}
DynamicLibraryModule::DynamicLibraryModule(const std::string& name)
: m_Module(NULL)
{
LoadModule(name);
}
DynamicLibraryModule::~DynamicLibraryModule(void)
{
UnloadModule();
}
bool DynamicLibraryModule::LoadModule(const std::string& name)
{
#ifdef _WIN32
m_Module = LoadLibraryA(name.c_str());
#else
m_Module = dlopen(name.c_str(), RTLD_LAZY);
#endif
return (m_Module != NULL);
}
// start loading based on the vector of library names. First success
// terminates the loading attempts. Needed for CentOS6 and OpenCL.
// The Catalyst installer (at least in Catalyst 12.1) creates libOpenCL.so.1
// but no symbolic link to libOpenCL.so. This symbolic link exists on other
// distributions where multi-step repackaging is required (build a .deb, run it)
bool DynamicLibraryModule::LoadModule(const std::vector<std::string>& names)
{
bool loaded = false;
for (std::vector<std::string>::const_iterator it = names.begin(); it != names.end(); it++)
{
loaded = LoadModule(*it);
if (loaded)
{
return loaded;
}
}
return loaded;
}
void DynamicLibraryModule::UnloadModule()
{
if (m_Module != NULL)
{
#ifdef _WIN32
FreeLibrary(m_Module);
#else
dlclose(m_Module);
#endif
m_Module = NULL;
}
}
void* DynamicLibraryModule::GetProcAddress(const std::string& name)
{
if (m_Module != NULL)
{
#ifdef _WIN32
return ::GetProcAddress(m_Module, name.c_str());
#else
return dlsym(m_Module, name.c_str());
#endif
}
else
{
return NULL;
}
}