From 344786f3c3f29d1c0b95f6bb7ba4cdebbf472d8a Mon Sep 17 00:00:00 2001 From: molivier Date: Wed, 24 Mar 2004 13:40:43 +0000 Subject: [PATCH] Factorized some code in the shared library loaders git-svn-id: svn://svn.icculus.org/twilight/trunk/darkplaces@4056 d7cf8633-e32d-0410-b094-e92efae38249 --- fs.c | 26 ++++-------------------- jpeg.c | 26 ++++-------------------- snd_ogg.c | 26 ++++-------------------- sys.h | 4 ++-- sys_shared.c | 57 ++++++++++++++++++++++++++++++++++++++++------------ 5 files changed, 58 insertions(+), 81 deletions(-) diff --git a/fs.c b/fs.c index 093d0ac9..90ba0702 100644 --- a/fs.c +++ b/fs.c @@ -317,11 +317,7 @@ Unload the Zlib DLL */ void PK3_CloseLibrary (void) { - if (!zlib_dll) - return; - - Sys_UnloadLibrary (zlib_dll); - zlib_dll = NULL; + Sys_UnloadLibrary (&zlib_dll); } @@ -335,7 +331,6 @@ Try to load the Zlib DLL qboolean PK3_OpenLibrary (void) { const char* dllname; - const dllfunction_t *func; // Already loaded? if (zlib_dll) @@ -347,27 +342,14 @@ qboolean PK3_OpenLibrary (void) dllname = "libz.so"; #endif - // Initializations - for (func = zlibfuncs; func && func->name != NULL; func++) - *func->funcvariable = NULL; - // Load the DLL - if (! (zlib_dll = Sys_LoadLibrary (dllname))) + if (! Sys_LoadLibrary (dllname, &zlib_dll, zlibfuncs)) { - Con_Printf("Can't find %s. Compressed files support disabled\n", dllname); + Con_Printf ("Compressed files support disabled\n"); return false; } - // Get the function adresses - for (func = zlibfuncs; func && func->name != NULL; func++) - if (!(*func->funcvariable = (void *) Sys_GetProcAddress (zlib_dll, func->name))) - { - Con_Printf("missing function \"%s\" - broken Zlib library!\n", func->name); - PK3_CloseLibrary (); - return false; - } - - Con_Printf("%s loaded. Compressed files support enabled\n", dllname); + Con_Printf ("Compressed files support enabled\n"); return true; } diff --git a/jpeg.c b/jpeg.c index c979fb14..2637a030 100644 --- a/jpeg.c +++ b/jpeg.c @@ -395,7 +395,6 @@ Try to load the JPEG DLL qboolean JPEG_OpenLibrary (void) { const char* dllname; - const dllfunction_t *func; // Already loaded? if (jpeg_dll) @@ -407,27 +406,14 @@ qboolean JPEG_OpenLibrary (void) dllname = "libjpeg.so.62"; #endif - // Initializations - for (func = jpegfuncs; func && func->name != NULL; func++) - *func->funcvariable = NULL; - // Load the DLL - if (! (jpeg_dll = Sys_LoadLibrary (dllname))) + if (! Sys_LoadLibrary (dllname, &jpeg_dll, jpegfuncs)) { - Con_DPrintf("Can't find %s. JPEG support disabled\n", dllname); + Con_Printf ("JPEG support disabled\n"); return false; } - // Get the function adresses - for (func = jpegfuncs; func && func->name != NULL; func++) - if (!(*func->funcvariable = (void *) Sys_GetProcAddress (jpeg_dll, func->name))) - { - Con_Printf("missing function \"%s\" - broken JPEG library!\n", func->name); - JPEG_CloseLibrary (); - return false; - } - - Con_DPrintf("%s loaded. JPEG support enabled\n", dllname); + Con_Printf ("JPEG support enabled\n"); return true; } @@ -441,11 +427,7 @@ Unload the JPEG DLL */ void JPEG_CloseLibrary (void) { - if (!jpeg_dll) - return; - - Sys_UnloadLibrary (jpeg_dll); - jpeg_dll = NULL; + Sys_UnloadLibrary (&jpeg_dll); } diff --git a/snd_ogg.c b/snd_ogg.c index d1a44f26..c8eae942 100644 --- a/snd_ogg.c +++ b/snd_ogg.c @@ -292,7 +292,6 @@ Try to load the VorbisFile DLL qboolean OGG_OpenLibrary (void) { const char* dllname; - const dllfunction_t *func; // Already loaded? if (vf_dll) @@ -304,27 +303,14 @@ qboolean OGG_OpenLibrary (void) dllname = "libvorbisfile.so"; #endif - // Initializations - for (func = oggvorbisfuncs; func && func->name != NULL; func++) - *func->funcvariable = NULL; - // Load the DLL - if (! (vf_dll = Sys_LoadLibrary (dllname))) + if (! Sys_LoadLibrary (dllname, &vf_dll, oggvorbisfuncs)) { - Con_DPrintf("Can't find %s. Ogg Vorbis support disabled\n", dllname); + Con_Printf ("Ogg Vorbis support disabled\n"); return false; } - // Get the function adresses - for (func = oggvorbisfuncs; func && func->name != NULL; func++) - if (!(*func->funcvariable = (void *) Sys_GetProcAddress (vf_dll, func->name))) - { - Con_Printf("missing function \"%s\" - broken Ogg Vorbis library!\n", func->name); - OGG_CloseLibrary (); - return false; - } - - Con_DPrintf("%s loaded. Ogg Vorbis support enabled\n", dllname); + Con_Printf ("Ogg Vorbis support enabled\n"); return true; } @@ -338,11 +324,7 @@ Unload the VorbisFile DLL */ void OGG_CloseLibrary (void) { - if (!vf_dll) - return; - - Sys_UnloadLibrary (vf_dll); - vf_dll = NULL; + Sys_UnloadLibrary (&vf_dll); } diff --git a/sys.h b/sys.h index 689f2b93..f0e2ad59 100644 --- a/sys.h +++ b/sys.h @@ -44,8 +44,8 @@ typedef struct } dllfunction_t; -dllhandle_t Sys_LoadLibrary (const char* name); -void Sys_UnloadLibrary (dllhandle_t handle); +qboolean Sys_LoadLibrary (const char* dllname, dllhandle_t* handle, const dllfunction_t *fcts); +void Sys_UnloadLibrary (dllhandle_t* handle); void* Sys_GetProcAddress (dllhandle_t handle, const char* name); diff --git a/sys_shared.c b/sys_shared.c index a8016fa7..258e6cf4 100644 --- a/sys_shared.c +++ b/sys_shared.c @@ -1,9 +1,10 @@ #include "quakedef.h" -#include +# include #ifndef WIN32 -#include -#include +# include +# include +# include #endif extern cvar_t timestamps; @@ -135,26 +136,56 @@ DLL MANAGEMENT =============================================================================== */ -#ifndef WIN32 -#include -#endif - -dllhandle_t Sys_LoadLibrary (const char* name) +qboolean Sys_LoadLibrary (const char* dllname, dllhandle_t* handle, const dllfunction_t *fcts) { + const dllfunction_t *func; + dllhandle_t dllhandle; + + if (handle == NULL) + return false; + + // Initializations + for (func = fcts; func && func->name != NULL; func++) + *func->funcvariable = NULL; + + // Load the DLL #ifdef WIN32 - return LoadLibrary (name); + dllhandle = LoadLibrary (dllname); #else - return dlopen (name, RTLD_LAZY); + dllhandle = dlopen (dllname, RTLD_LAZY); #endif + if (! dllhandle) + { + Con_Printf ("Can't load \"%s\".\n", dllname); + return false; + } + + // Get the function adresses + for (func = fcts; func && func->name != NULL; func++) + if (!(*func->funcvariable = (void *) Sys_GetProcAddress (dllhandle, func->name))) + { + Con_Printf ("Missing function \"%s\" - broken library!\n", func->name); + Sys_UnloadLibrary (&dllhandle); + return false; + } + + *handle = dllhandle; + Con_DPrintf("\"%s\" loaded.\n", dllname); + return true; } -void Sys_UnloadLibrary (dllhandle_t handle) +void Sys_UnloadLibrary (dllhandle_t* handle) { + if (handle == NULL || *handle == NULL) + return; + #ifdef WIN32 - FreeLibrary (handle); + FreeLibrary (*handle); #else - dlclose (handle); + dlclose (*handle); #endif + + *handle = NULL; } void* Sys_GetProcAddress (dllhandle_t handle, const char* name) -- 2.39.2