Skip to content

Commit b0cf826

Browse files
committed
dynapi: Minor cleanups to library loading code.
1 parent 6b06043 commit b0cf826

File tree

1 file changed

+34
-27
lines changed

1 file changed

+34
-27
lines changed

src/dynapi/SDL_dynapi.c

Lines changed: 34 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -448,47 +448,60 @@ Sint32 SDL_DYNAPI_entry(Uint32 apiver, void *table, Uint32 tablesize)
448448
}
449449
#endif
450450

451-
// The handle to the other SDL library, loaded with SDL_DYNAMIC_API_ENVVAR
452-
#if defined(WIN32) || defined(_WIN32) || defined(SDL_PLATFORM_CYGWIN)
453-
static HMODULE lib = NULL;
454-
#elif defined(SDL_PLATFORM_UNIX) || defined(SDL_PLATFORM_APPLE) || defined(SDL_PLATFORM_HAIKU)
455-
static void *lib = NULL;
456-
#else
457-
#error Please define your platform.
458-
#endif
459451

460452
// Obviously we can't use SDL_LoadObject() to load SDL. :)
461-
// Also obviously, we never close the loaded library.
453+
// Also obviously, we never close the loaded library, once we accept it.
462454
#if defined(WIN32) || defined(_WIN32) || defined(SDL_PLATFORM_CYGWIN)
455+
static HMODULE sdlapi_lib = NULL; // The handle to the other SDL library, loaded with SDL_DYNAMIC_API_ENVVAR
456+
457+
static SDL_INLINE void unload_sdlapi_library(void)
458+
{
459+
if (sdlapi_lib) {
460+
FreeLibrary(sdlapi_lib);
461+
sdlapi_lib = NULL;
462+
}
463+
}
464+
463465
static SDL_INLINE void *get_sdlapi_entry(const char *fname, const char *sym)
464466
{
465-
lib = LoadLibraryA(fname);
467+
sdlapi_lib = LoadLibraryA(fname);
466468
void *result = NULL;
467-
if (lib) {
468-
result = (void *) GetProcAddress(lib, sym);
469+
if (sdlapi_lib) {
470+
result = (void *) GetProcAddress(sdlapi_lib, sym);
469471
if (!result) {
470-
FreeLibrary(lib);
471-
lib = NULL;
472+
unload_sdlapi_library();
472473
}
473474
}
474475
return result;
475476
}
476477

477478
#elif defined(SDL_PLATFORM_UNIX) || defined(SDL_PLATFORM_APPLE) || defined(SDL_PLATFORM_HAIKU)
478479
#include <dlfcn.h>
480+
static void *sdlapi_lib = NULL; // The handle to the other SDL library, loaded with SDL_DYNAMIC_API_ENVVAR
481+
482+
static SDL_INLINE void unload_sdlapi_library(void)
483+
{
484+
if (sdlapi_lib) {
485+
dlclose(sdlapi_lib);
486+
sdlapi_lib = NULL;
487+
}
488+
}
489+
479490
static SDL_INLINE void *get_sdlapi_entry(const char *fname, const char *sym)
480491
{
481-
lib = dlopen(fname, RTLD_NOW | RTLD_LOCAL);
492+
sdlapi_lib = dlopen(fname, RTLD_NOW | RTLD_LOCAL);
482493
void *result = NULL;
483-
if (lib) {
484-
result = dlsym(lib, sym);
494+
if (sdlapi_lib) {
495+
result = dlsym(sdlapi_lib, sym);
485496
if (!result) {
486-
dlclose(lib);
487-
lib = NULL;
497+
unload_sdlapi_library();
488498
}
489499
}
490500
return result;
491501
}
502+
503+
#else
504+
#error Please define your platform.
492505
#endif
493506

494507
static void dynapi_warn(const char *msg)
@@ -557,14 +570,8 @@ static void SDL_InitDynamicAPILocked(void)
557570
if (entry(SDL_DYNAPI_VERSION, &jump_table, sizeof(jump_table)) < 0) {
558571
dynapi_warn("Couldn't override SDL library. Using a newer SDL build might help. Please fix or remove the " SDL_DYNAMIC_API_ENVVAR " environment variable. Using the default SDL.");
559572

560-
// unload the other SDL library
561-
#if defined(WIN32) || defined(_WIN32) || defined(SDL_PLATFORM_CYGWIN)
562-
FreeLibrary(lib);
563-
lib = NULL;
564-
#elif defined(SDL_PLATFORM_UNIX) || defined(SDL_PLATFORM_APPLE) || defined(SDL_PLATFORM_HAIKU)
565-
dlclose(lib);
566-
lib = NULL;
567-
#endif
573+
// unload the failed SDL library
574+
unload_sdlapi_library();
568575

569576
// Just fill in the function pointers from this library, later.
570577
} else {

0 commit comments

Comments
 (0)