alpaqa
1.0.0a18
Nonconvex constrained optimization
Loading...
Searching...
No Matches
src
alpaqa
src
util
dl.cpp
Go to the documentation of this file.
1
#include <
alpaqa/util/dl.hpp
>
2
3
#if _WIN32
4
#include <windows.h>
5
#else
6
#include <dlfcn.h>
7
#endif
8
9
#include <cassert>
10
11
namespace
alpaqa::util
{
12
13
#if _WIN32
14
std::shared_ptr<char>
get_last_error_msg
() {
15
char
*
err
=
nullptr
;
16
auto
opt
=
FORMAT_MESSAGE_FROM_SYSTEM
|
FORMAT_MESSAGE_ALLOCATE_BUFFER
|
17
FORMAT_MESSAGE_IGNORE_INSERTS
;
18
auto
lang
=
MAKELANGID
(
LANG_NEUTRAL
,
SUBLANG_DEFAULT
);
19
if
(
FormatMessage
(
opt
,
NULL
,
GetLastError
(),
lang
,
20
reinterpret_cast<
char
*
>
(&
err
), 0,
NULL
) != 0) {
21
return
std::shared_ptr<char>{
err
, [](
char
*e) {
LocalFree
(e); }};
22
}
else
{
23
static
char
msg
[] =
"(failed to get error message)"
;
24
return
std::shared_ptr<char>{
msg
, [](
char
*) {}};
25
}
26
}
27
28
std::shared_ptr<void>
load_lib
(
const
std::filesystem::path &
so_filename
) {
29
assert
(!
so_filename
.empty());
30
void
*h =
LoadLibraryW
(
so_filename
.c_str());
31
if
(!h)
32
throw
dynamic_load_error(
"Unable to load \""
+
so_filename
.string() +
33
"\": "
+
get_last_error_msg
().get());
34
#if ALPAQA_NO_DLCLOSE
35
return
std::shared_ptr<void>{h, +[](
void
*) {}};
36
#else
37
return
std::shared_ptr<void>{
38
h, +[](
void
*h) {
FreeLibrary
(
static_cast<
HMODULE
>
(h)); }};
39
#endif
40
}
41
42
void
*
load_func
(
void
*handle,
const
std::string &name) {
43
assert
(handle);
44
auto
*h =
GetProcAddress
(
static_cast<
HMODULE
>
(handle), name.c_str());
45
if
(!h)
46
throw
dynamic_load_error(
"Unable to load function '"
+ name +
47
"': "
+
get_last_error_msg
().get());
48
return
reinterpret_cast<
void
*
>
(h);
49
}
50
#else
51
std::shared_ptr<void>
load_lib
(
const
std::filesystem::path &
so_filename
) {
52
assert
(!
so_filename
.empty());
53
::dlerror
();
54
void
*h =
::dlopen
(
so_filename
.c_str(),
RTLD_LOCAL
|
RTLD_NOW
);
55
if
(
auto
*
err
=
::dlerror
())
56
throw
dynamic_load_error(
err
);
57
#if ALPAQA_NO_DLCLOSE
58
return
std::shared_ptr<void>{h, +[](
void
*) {}};
59
#else
60
return
std::shared_ptr<void>{h, &
::dlclose
};
61
#endif
62
}
63
64
void
*
load_func
(
void
*handle,
const
std::string &name) {
65
assert
(handle);
66
::dlerror
();
67
auto
*h =
::dlsym
(handle, name.c_str());
68
if
(
auto
*
err
=
::dlerror
())
69
throw
dynamic_load_error(
"Unable to load function '"
+ name +
70
"': "
+
err
);
71
return
h;
72
}
73
#endif
74
75
}
// namespace alpaqa::util
dl.hpp
alpaqa::util
Definition
check-dim.hpp:8
alpaqa::util::load_func
void * load_func(void *handle, const std::string &name)
Definition
dl.cpp:64
alpaqa::util::load_lib
std::shared_ptr< void > load_lib(const std::filesystem::path &so_filename)
Definition
dl.cpp:51
alpaqa::inf
constexpr const auto inf
Definition
config.hpp:112
Generated on Wed Mar 27 2024 for alpaqa by
1.9.8