From 8ce82a873ac1667ee4cbdc83b812a91e4ada0edf Mon Sep 17 00:00:00 2001 From: David Robillard Date: Wed, 11 Dec 2024 14:30:13 -0500 Subject: Add dylib abstraction to isolate platform-specific code --- src/dylib.c | 67 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 src/dylib.c (limited to 'src/dylib.c') 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 +// SPDX-License-Identifier: ISC + +#include "dylib.h" + +#ifdef _WIN32 + +# include + +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 + +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 -- cgit v1.2.1