<p dir="ltr">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? </p>
<div class="gmail_quote">On Oct 13, 2013 12:39 PM, "David Majnemer" <<a href="mailto:david.majnemer@gmail.com">david.majnemer@gmail.com</a>> wrote:<br type="attribution"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
Author: majnemer<br>
Date: Sun Oct 13 05:34:21 2013<br>
New Revision: 192550<br>
<br>
URL: <a href="http://llvm.org/viewvc/llvm-project?rev=192550&view=rev" target="_blank">http://llvm.org/viewvc/llvm-project?rev=192550&view=rev</a><br>
Log:<br>
Windows: Use GetModuleHandleEx instead of LoadLibrary<br>
<br>
We were using an anti-pattern of:<br>
- LoadLibrary<br>
- GetProcAddress<br>
- FreeLibrary<br>
<br>
This is problematic because of several reasons:<br>
- We are holding on to pointers into a library we just unloaded.<br>
- Calling LoadLibrary results in an increase in the reference count of<br>
the library in question and any libraries that it depends on and<br>
so-on and so-forth. This is none too quick.<br>
<br>
Instead, use GetModuleHandleEx with GET_MODULE_HANDLE_EX_FLAG_PIN. This<br>
is done because because we didn't bring the reference for the library<br>
into existence and therefor shouldn't count on it being around later.<br>
<br>
Modified:<br>
llvm/trunk/lib/Support/Windows/RWMutex.inc<br>
<br>
Modified: llvm/trunk/lib/Support/Windows/RWMutex.inc<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Support/Windows/RWMutex.inc?rev=192550&r1=192549&r2=192550&view=diff" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Support/Windows/RWMutex.inc?rev=192550&r1=192549&r2=192550&view=diff</a><br>
==============================================================================<br>
--- llvm/trunk/lib/Support/Windows/RWMutex.inc (original)<br>
+++ llvm/trunk/lib/Support/Windows/RWMutex.inc Sun Oct 13 05:34:21 2013<br>
@@ -48,7 +48,8 @@ static bool loadSRW() {<br>
if (!sChecked) {<br>
sChecked = true;<br>
<br>
- HMODULE hLib = ::LoadLibraryW(L"Kernel32.dll");<br>
+ HMODULE hLib;<br>
+ ::GetModuleHandleExW(GET_MODULE_HANDLE_EX_FLAG_PIN, L"Kernel32.dll", &hLib);<br>
if (hLib) {<br>
fpInitializeSRWLock =<br>
(VOID (WINAPI *)(PSRWLOCK))::GetProcAddress(hLib,<br>
@@ -65,7 +66,6 @@ static bool loadSRW() {<br>
fpReleaseSRWLockShared =<br>
(VOID (WINAPI *)(PSRWLOCK))::GetProcAddress(hLib,<br>
"ReleaseSRWLockShared");<br>
- ::FreeLibrary(hLib);<br>
<br>
if (fpInitializeSRWLock != NULL) {<br>
sHasSRW = true;<br>
<br>
<br>
_______________________________________________<br>
llvm-commits mailing list<br>
<a href="mailto:llvm-commits@cs.uiuc.edu">llvm-commits@cs.uiuc.edu</a><br>
<a href="http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits" target="_blank">http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits</a><br>
</blockquote></div>