Move the dynload decls and defs to common

master
Chris Robinson 2019-08-10 21:54:30 -07:00
parent dca2365051
commit 70058a8a84
10 changed files with 84 additions and 65 deletions

View File

@ -598,6 +598,8 @@ SET(COMMON_OBJS
common/aloptional.h
common/alspan.h
common/atomic.h
common/dynload.cpp
common/dynload.h
common/endiantest.h
common/intrusive_ptr.h
common/math_defs.h

View File

@ -44,7 +44,7 @@
#include "alnumeric.h"
#include "aloptional.h"
#include "alu.h"
#include "compat.h"
#include "dynload.h"
#include "logging.h"
#include "ringbuffer.h"
#include "threads.h"

View File

@ -48,6 +48,7 @@
#include "alu.h"
#include "ringbuffer.h"
#include "compat.h"
#include "dynload.h"
#include "threads.h"
/* MinGW-w64 needs this for some unknown reason now. */

View File

@ -32,9 +32,9 @@
#include "alcmain.h"
#include "alu.h"
#include "alconfig.h"
#include "dynload.h"
#include "ringbuffer.h"
#include "threads.h"
#include "compat.h"
#include <jack/jack.h>
#include <jack/ringbuffer.h>

View File

@ -29,8 +29,8 @@
#include "alcmain.h"
#include "alu.h"
#include "alconfig.h"
#include "dynload.h"
#include "ringbuffer.h"
#include "compat.h"
#include <portaudio.h>

View File

@ -37,8 +37,9 @@
#include "alcmain.h"
#include "alu.h"
#include "alconfig.h"
#include "compat.h"
#include "alexcpt.h"
#include "compat.h"
#include "dynload.h"
#include <pulse/pulseaudio.h>

View File

@ -1,8 +1,6 @@
#ifndef AL_COMPAT_H
#define AL_COMPAT_H
#ifdef __cplusplus
#ifdef _WIN32
#define WIN32_LEAN_AND_MEAN
@ -86,8 +84,6 @@ public:
} // namespace al
#define HAVE_DYNLOAD 1
#else /* _WIN32 */
#include <fstream>
@ -99,10 +95,6 @@ using ifstream = std::ifstream;
} // namespace al
#if defined(HAVE_DLFCN_H)
#define HAVE_DYNLOAD 1
#endif
#endif /* _WIN32 */
#include <string>
@ -110,12 +102,4 @@ using ifstream = std::ifstream;
struct PathNamePair { std::string path, fname; };
const PathNamePair &GetProcBinary(void);
#ifdef HAVE_DYNLOAD
void *LoadLib(const char *name);
void CloseLib(void *handle);
void *GetSymbol(void *handle, const char *name);
#endif
#endif /* __cplusplus */
#endif /* AL_COMPAT_H */

View File

@ -81,9 +81,6 @@ DEFINE_PROPERTYKEY(PKEY_AudioEndpoint_GUID, 0x1da5d803, 0xd492, 0x4edd, 0x8c, 0x
#endif
#endif /* AL_NO_UID_DEFS */
#ifdef HAVE_DLFCN_H
#include <dlfcn.h>
#endif
#ifdef HAVE_INTRIN_H
#include <intrin.h>
#endif
@ -454,21 +451,6 @@ const PathNamePair &GetProcBinary()
}
void *LoadLib(const char *name)
{
std::wstring wname{utf8_to_wstr(name)};
return LoadLibraryW(wname.c_str());
}
void CloseLib(void *handle)
{ FreeLibrary(static_cast<HMODULE>(handle)); }
void *GetSymbol(void *handle, const char *name)
{
void *ret{reinterpret_cast<void*>(GetProcAddress(static_cast<HMODULE>(handle), name))};
if(!ret) ERR("Failed to load %s\n", name);
return ret;
}
void al_print(FILE *logfile, const char *fmt, ...)
{
al::vector<char> dynmsg;
@ -683,33 +665,6 @@ const PathNamePair &GetProcBinary()
}
#ifdef HAVE_DLFCN_H
void *LoadLib(const char *name)
{
dlerror();
void *handle{dlopen(name, RTLD_NOW)};
const char *err{dlerror()};
if(err) handle = nullptr;
return handle;
}
void CloseLib(void *handle)
{ dlclose(handle); }
void *GetSymbol(void *handle, const char *name)
{
dlerror();
void *sym{dlsym(handle, name)};
const char *err{dlerror()};
if(err)
{
WARN("Failed to load %s: %s\n", name, err);
sym = nullptr;
}
return sym;
}
#endif /* HAVE_DLFCN_H */
void al_print(FILE *logfile, const char *fmt, ...)
{
va_list ap;

62
common/dynload.cpp Normal file
View File

@ -0,0 +1,62 @@
#include "config.h"
#include "dynload.h"
#ifdef _WIN32
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <string>
inline std::wstring utf8_to_wstr(const char *str)
{
std::wstring ret;
int len = MultiByteToWideChar(CP_UTF8, 0, str, -1, NULL, 0);
if(len > 0)
{
ret.resize(len);
MultiByteToWideChar(CP_UTF8, 0, str, -1, &ret[0], len);
ret.pop_back();
}
return ret;
}
void *LoadLib(const char *name)
{
std::wstring wname{utf8_to_wstr(name)};
return LoadLibraryW(wname.c_str());
}
void CloseLib(void *handle)
{ FreeLibrary(static_cast<HMODULE>(handle)); }
void *GetSymbol(void *handle, const char *name)
{ return reinterpret_cast<void*>(GetProcAddress(static_cast<HMODULE>(handle), name)); }
#elif defined(HAVE_DLFCN_H)
#include <dlfcn.h>
void *LoadLib(const char *name)
{
dlerror();
void *handle{dlopen(name, RTLD_NOW)};
const char *err{dlerror()};
if(err) handle = nullptr;
return handle;
}
void CloseLib(void *handle)
{ dlclose(handle); }
void *GetSymbol(void *handle, const char *name)
{
dlerror();
void *sym{dlsym(handle, name)};
const char *err{dlerror()};
if(err) sym = nullptr;
return sym;
}
#endif

14
common/dynload.h Normal file
View File

@ -0,0 +1,14 @@
#ifndef AL_DYNLOAD_H
#define AL_DYNLOAD_H
#if defined(_WIN32) || defined(HAVE_DLFCN_H)
#define HAVE_DYNLOAD
void *LoadLib(const char *name);
void CloseLib(void *handle);
void *GetSymbol(void *handle, const char *name);
#endif
#endif /* AL_DYNLOAD_H */