Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow DlLoader to have multiple fallback names when searching for a library #1635

Merged
merged 3 commits into from
Feb 22, 2018
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion core/cc/armlinux/get_vulkan_proc_address.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,6 @@ GetVulkanInstanceProcAddressFunc* GetVulkanInstanceProcAddress = getVulkanInstan
GetVulkanDeviceProcAddressFunc* GetVulkanDeviceProcAddress = getVulkanDeviceProcAddress;
GetVulkanProcAddressFunc* GetVulkanProcAddress = getVulkanProcAddress;
bool HasVulkanLoader() {
return DlLoader::can_load("libvulkan.so");
return DlLoader::can_load("libvulkan.so") || DlLoader::can_load("libvulkan.so.1");
}
} // namespace core
38 changes: 30 additions & 8 deletions core/cc/dl_loader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,15 @@
namespace {

// load defs
void* load(const char* name) {
void* load() {
return nullptr;
}

template<typename... ConstCharPtrs>
void* load(const char* name, ConstCharPtrs... fallback_names) {
void* ret = nullptr;
#if TARGET_OS == GAPID_OS_WINDOWS
return reinterpret_cast<void*>(LoadLibraryExA(name, NULL, 0));
ret = reinterpret_cast<void*>(LoadLibraryExA(name, NULL, 0));
#elif TARGET_OS == GAPID_OS_OSX
if (name == nullptr) {
return nullptr;
Expand All @@ -50,14 +56,19 @@ void* load(const char* name) {
remove(tmp);
}
}
return res;
ret = res;
#else
return dlopen(name, RTLD_NOW | RTLD_LOCAL);
ret = dlopen(name, RTLD_NOW | RTLD_LOCAL);
#endif // TARGET_OS
if (ret == nullptr) {
return load(fallback_names...);
}
return ret;
}

void* must_load(const char* name) {
void* res = load(name);
template<typename... ConstCharPtrs>
void* must_load(const char* name, ConstCharPtrs... fallback_names) {
void* res = load(name, fallback_names...);
if (res == nullptr) {
#if TARGET_OS == GAPID_OS_WINDOWS
GAPID_FATAL("Can't load library %s: %d", name, GetLastError());
Expand Down Expand Up @@ -100,8 +111,8 @@ void close(void* lib) {
} // anonymous namespace

namespace core {

DlLoader::DlLoader(const char* name) : mLibrary(must_load(name)) {}
template<typename... ConstCharPtrs>
DlLoader::DlLoader(const char* name, ConstCharPtrs... fallback_names) : mLibrary(must_load(name, fallback_names...)) {}

DlLoader::~DlLoader() {
close(mLibrary);
Expand All @@ -125,4 +136,15 @@ bool DlLoader::can_load(const char* lib_name) {
return false;
}

#define CC const char*
#define DL(...) \
template DlLoader::DlLoader(__VA_ARGS__)
DL(CC _1);
DL(CC _1, CC _2);
DL(CC _1, CC _2, CC _3);
DL(CC _1, CC _2, CC _3, CC _4);
DL(CC _1, CC _2, CC _3, CC _4, CC _5);
#undef DLLOADER
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#undef DL ?

#undef CC

} // namespace core
10 changes: 6 additions & 4 deletions core/cc/dl_loader.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,12 @@ namespace core {
// Utility class for retrieving function pointers from dynamic libraries.
class DlLoader {
public:
// Loads the specified dynamic library.
// If the library cannot be loaded then this is a fatal error.
// For *nix systems, a nullptr can be used to search the application's functions.
DlLoader(const char* name);
// Loads the dynamic library specified by the given name and fallback names
// (if any). Names will be used to try to find the library in order. If the
// library cannot be loaded then this is a fatal error. For *nix systems,
// a nullptr can be used to search the application's functions.
template<typename... ConstCharPtrs>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a particular reason we are doing this with template magic, rather than something like std::initializer_list?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm, I just feel DlLoader("libvulkan.so") looks a little bit better than DlLoader({"libvulkan.so"}). But yes, initializer list can do the same and does not need explicit instantiation...Don't have other particular reasons.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, as much as I personally love templates, reading

void* load() {
    return nullptr;
}

template<typename... ConstCharPtrs>
void* load(const char* name, ConstCharPtrs... fallback_names) {
....
...
    return load(fallback_names...);
...
...
Bunch of macros that will cause a link error if we hit 6 arguments.
...

is a lot harder to parse than

void* load(std::initializer_list<const char* name> names) {
   for (auto& nm: names) {
   }

DlLoader(const char* name, ConstCharPtrs... fallback_names);

// Unloads the library loaded in the constructor.
~DlLoader();
Expand Down
4 changes: 2 additions & 2 deletions core/cc/linux/get_vulkan_proc_address.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ typedef size_t VkInstance;
void* getVulkanInstanceProcAddress(size_t instance, const char *name, bool bypassLocal) {
typedef PFN_vkVoidFunction (*VPAPROC)(VkInstance instance, const char *name);

static DlLoader dylib("libvulkan.so");
static DlLoader dylib("libvulkan.so", "libvulkan.so.1");

if (VPAPROC vpa = reinterpret_cast<VPAPROC>(dylib.lookup("vkGetInstanceProcAddr"))) {
if (void* proc = vpa(instance, name)) {
Expand Down Expand Up @@ -72,6 +72,6 @@ GetVulkanInstanceProcAddressFunc* GetVulkanInstanceProcAddress = getVulkanInstan
GetVulkanDeviceProcAddressFunc* GetVulkanDeviceProcAddress = getVulkanDeviceProcAddress;
GetVulkanProcAddressFunc* GetVulkanProcAddress = getVulkanProcAddress;
bool HasVulkanLoader() {
return DlLoader::can_load("libvulkan.so");
return DlLoader::can_load("libvulkan.so") || DlLoader::can_load("libvulkan.so.1");
}
} // namespace core