diff options
author | David Robillard <d@drobilla.net> | 2024-12-11 14:30:13 -0500 |
---|---|---|
committer | David Robillard <d@drobilla.net> | 2024-12-11 14:57:06 -0500 |
commit | 8ce82a873ac1667ee4cbdc83b812a91e4ada0edf (patch) | |
tree | 9b9ca40afd075df80d5addc7a1408a70a0755687 /src/dylib.c | |
parent | b1b2a693ba14627a2170cecfd7ece313224888cd (diff) | |
download | lilv-8ce82a873ac1667ee4cbdc83b812a91e4ada0edf.tar.gz lilv-8ce82a873ac1667ee4cbdc83b812a91e4ada0edf.tar.bz2 lilv-8ce82a873ac1667ee4cbdc83b812a91e4ada0edf.zip |
Add dylib abstraction to isolate platform-specific code
Diffstat (limited to 'src/dylib.c')
-rw-r--r-- | src/dylib.c | 67 |
1 files changed, 67 insertions, 0 deletions
diff --git a/src/dylib.c b/src/dylib.c new file mode 100644 index 0000000..4c6c219 --- /dev/null +++ b/src/dylib.c @@ -0,0 +1,67 @@ +// Copyright 2020-2024 David Robillard <d@drobilla.net> +// SPDX-License-Identifier: ISC + +#include "dylib.h" + +#ifdef _WIN32 + +# include <windows.h> + +void* +dylib_open(const char* const filename, const unsigned flags) +{ + (void)flags; + return LoadLibrary(filename); +} + +int +dylib_close(DylibLib* const handle) +{ + return !FreeLibrary((HMODULE)handle); +} + +const char* +dylib_error(void) +{ + return "Unknown error"; +} + +DylibFunc +dylib_func(DylibLib* handle, const char* symbol) +{ + return (DylibFunc)GetProcAddress((HMODULE)handle, symbol); +} + +#else + +# include <dlfcn.h> + +void* +dylib_open(const char* const filename, const unsigned flags) +{ + return dlopen(filename, flags == DYLIB_LAZY ? RTLD_LAZY : RTLD_NOW); +} + +int +dylib_close(DylibLib* const handle) +{ + return dlclose(handle); +} + +const char* +dylib_error(void) +{ + return dlerror(); +} + +DylibFunc +dylib_func(DylibLib* handle, const char* symbol) +{ + typedef DylibFunc (*VoidFuncGetter)(void*, const char*); + + VoidFuncGetter dlfunc = (VoidFuncGetter)dlsym; + + return dlfunc(handle, symbol); +} + +#endif |