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

Ron Ofir ron.ofir at gmail.com
Sun Oct 13 13:32:58 PDT 2013


Using GetModuleHandleEx for kernel32 seems unnecessary, as nothing on
Windows can't can't really run without it being loaded. Is there really a
reason to do that?
On Oct 13, 2013 12:39 PM, "David Majnemer" <david.majnemer at gmail.com> wrote:

> 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;
>
>
> _______________________________________________
> llvm-commits mailing list
> llvm-commits at cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20131013/1eaed2b4/attachment.html>


More information about the llvm-commits mailing list