diff options
author | David Robillard <d@drobilla.net> | 2020-09-27 16:16:15 +0200 |
---|---|---|
committer | David Robillard <d@drobilla.net> | 2020-09-27 16:16:15 +0200 |
commit | e5345ab3e4bad3ee929c0f08a9161b46fa6e2071 (patch) | |
tree | a2b5d9b32f3643e2a301680a1ef6f750d0993c0a /src/dylib.h | |
parent | 0ab07d65fe9261ecfa495090691db324eff7aefc (diff) | |
download | suil-e5345ab3e4bad3ee929c0f08a9161b46fa6e2071.tar.gz suil-e5345ab3e4bad3ee929c0f08a9161b46fa6e2071.tar.bz2 suil-e5345ab3e4bad3ee929c0f08a9161b46fa6e2071.zip |
Add a less janky portability wrapper for dlopen() and friends
Diffstat (limited to 'src/dylib.h')
-rw-r--r-- | src/dylib.h | 78 |
1 files changed, 78 insertions, 0 deletions
diff --git a/src/dylib.h b/src/dylib.h new file mode 100644 index 0000000..246693c --- /dev/null +++ b/src/dylib.h @@ -0,0 +1,78 @@ +/* + Copyright 2020 David Robillard <http://drobilla.net> + + Permission to use, copy, modify, and/or distribute this software for any + purpose with or without fee is hereby granted, provided that the above + copyright notice and this permission notice appear in all copies. + + THIS SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +*/ + +#ifndef SUIL_DYLIB_H +#define SUIL_DYLIB_H + +#ifdef _WIN32 + +#include <windows.h> + +enum DylibFlags { + DYLIB_GLOBAL = 0, + DYLIB_LAZY = 1, + DYLIB_NOW = 2, +}; + +static inline void* +dylib_open(const char* const filename, const int flags) +{ + return LoadLibrary(filename); +} + +static inline int +dylib_close(void* const handle) +{ + return !FreeLibrary((HMODULE)handle); +} + +static inline const char* +dylib_error(void) +{ + return "Unknown error"; +} + +#else + +#include <dlfcn.h> + +enum DylibFlags { + DYLIB_GLOBAL = RTLD_GLOBAL, + DYLIB_LAZY = RTLD_LAZY, + DYLIB_NOW = RTLD_NOW, +}; + +static inline void* +dylib_open(const char* const filename, const int flags) +{ + return dlopen(filename, flags); +} + +static inline int +dylib_close(void* const handle) +{ + return dlclose(handle); +} + +static inline const char* +dylib_error(void) +{ + return dlerror(); +} + +#endif + +#endif // SUIL_DYLIB_H |