From cde14930d745cec9f809a1daddcbfa4575d0b96d Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Fri, 12 Jun 2020 22:34:01 +1000 Subject: [PATCH] Don't repeatedly dlopen OpenGL library when retrieving address for OpenGL functions --- src/Platform.c | 11 ----------- src/Platform.h | 1 - src/Window.c | 14 ++++++++++++-- 3 files changed, 12 insertions(+), 14 deletions(-) diff --git a/src/Platform.c b/src/Platform.c index a24633c3a..be9882ed0 100644 --- a/src/Platform.c +++ b/src/Platform.c @@ -1477,17 +1477,6 @@ cc_result DynamicLib_Get(void* lib, const char* name, void** symbol) { return *symbol == NULL; } -void* DynamicLib_GetFrom(const char* filename, const char* name) { - void* lib; - String path; - - path = String_FromReadonly(filename); - lib = DynamicLib_Load2(&path); - if (!lib) return NULL; - - return DynamicLib_Get2(lib, name); -} - /*########################################################################################################################* *--------------------------------------------------------Platform---------------------------------------------------------* diff --git a/src/Platform.h b/src/Platform.h index 401f12a4d..6fd9ac632 100644 --- a/src/Platform.h +++ b/src/Platform.h @@ -86,7 +86,6 @@ CC_API void* DynamicLib_Load2(const String* path); /* Attempts to get the address of the symbol in the given dynamic library. */ /* NOTE: Do NOT use this to load OpenGL functions, use GLContext_GetAddress. */ CC_API void* DynamicLib_Get2(void* lib, const char* name); -void* DynamicLib_GetFrom(const char* filename, const char* name); /* OBSOLETE */ /* Outputs more detailed information about errors with the DynamicLib functions. */ /* NOTE: You MUST call this immediately after DynamicLib_Load2/DynamicLib_Get2 returns NULL. */ CC_API cc_bool DynamicLib_DescribeError(String* dst); diff --git a/src/Window.c b/src/Window.c index 36053203c..983590e16 100644 --- a/src/Window.c +++ b/src/Window.c @@ -4356,7 +4356,12 @@ void GLContext_Free(void) { } void* GLContext_GetAddress(const char* function) { - void* addr = DynamicLib_GetFrom("/System/Library/Frameworks/OpenGL.framework/Versions/Current/OpenGL", function); + static const String glPath = String_FromConst("/System/Library/Frameworks/OpenGL.framework/Versions/Current/OpenGL"); + static void* lib; + void* addr; + + if (!lib) lib = DynamicLib_Load2(&glPath); + addr = DynamicLib_Get2(lib, function); return GLContext_IsInvalidAddress(addr) ? NULL : addr; } @@ -4433,7 +4438,12 @@ void GLContext_Free(void) { } void* GLContext_GetAddress(const char* function) { - void* addr = DynamicLib_GetFrom("/System/Library/Frameworks/OpenGL.framework/Versions/Current/OpenGL", function); + static const String glPath = String_FromConst("/System/Library/Frameworks/OpenGL.framework/Versions/Current/OpenGL"); + static void* lib; + void* addr; + + if (!lib) lib = DynamicLib_Load2(&glPath); + addr = DynamicLib_Get2(lib, function); return GLContext_IsInvalidAddress(addr) ? NULL : addr; }