alpaqa no-casadi-dep
Nonconvex constrained optimization
Loading...
Searching...
No Matches
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
11namespace alpaqa::util {
12
13#if _WIN32
14std::shared_ptr<char> get_last_error_msg() {
15 char *err = nullptr;
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
28std::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
42void *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
51std::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
64void *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
void * load_func(void *handle, const std::string &name)
Definition dl.cpp:64
std::shared_ptr< void > load_lib(const std::filesystem::path &so_filename)
Definition dl.cpp:51
constexpr const auto inf
Definition config.hpp:112