[llvm] r192550 - Windows: Use GetModuleHandleEx instead of LoadLibrary

David Majnemer david.majnemer at gmail.com
Sun Oct 13 03:34:22 PDT 2013


Author: majnemer
Date: Sun Oct 13 05:34:21 2013
New Revision: 192550

URL: http://llvm.org/viewvc/llvm-project?rev=192550&view=rev
Log:
Windows: Use GetModuleHandleEx instead of LoadLibrary

We were using an anti-pattern of:
 - LoadLibrary
 - GetProcAddress
 - FreeLibrary

This is problematic because of several reasons:
 - We are holding on to pointers into a library we just unloaded.
 - Calling LoadLibrary results in an increase in the reference count of
   the library in question and any libraries that it depends on and
   so-on and so-forth.  This is none too quick.

Instead, use GetModuleHandleEx with GET_MODULE_HANDLE_EX_FLAG_PIN.  This
is done because because we didn't bring the reference for the library
into existence and therefor shouldn't count on it being around later.

Modified:
    llvm/trunk/lib/Support/Windows/RWMutex.inc

Modified: llvm/trunk/lib/Support/Windows/RWMutex.inc
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Support/Windows/RWMutex.inc?rev=192550&r1=192549&r2=192550&view=diff
==============================================================================
--- llvm/trunk/lib/Support/Windows/RWMutex.inc (original)
+++ llvm/trunk/lib/Support/Windows/RWMutex.inc Sun Oct 13 05:34:21 2013
@@ -48,7 +48,8 @@ static bool loadSRW() {
   if (!sChecked) {
     sChecked = true;
 
-    HMODULE hLib = ::LoadLibraryW(L"Kernel32.dll");
+    HMODULE hLib;
+    ::GetModuleHandleExW(GET_MODULE_HANDLE_EX_FLAG_PIN, L"Kernel32.dll", &hLib);
     if (hLib) {
       fpInitializeSRWLock =
         (VOID (WINAPI *)(PSRWLOCK))::GetProcAddress(hLib,
@@ -65,7 +66,6 @@ static bool loadSRW() {
       fpReleaseSRWLockShared =
         (VOID (WINAPI *)(PSRWLOCK))::GetProcAddress(hLib,
                                                "ReleaseSRWLockShared");
-      ::FreeLibrary(hLib);
 
       if (fpInitializeSRWLock != NULL) {
         sHasSRW = true;





More information about the llvm-commits mailing list