Various cleanups: Store properly-typed function pointers in winapisupp.cpp#4123
Merged
StephanTLavavej merged 2 commits intomicrosoft:mainfrom Oct 25, 2023
Conversation
…tead of enumerators into an array of PVOID. This is within an unnamed namespace (after GH 2841 dramatically simplified the code), so we can freely modify this machinery. Originally, the function pointer table's ordering seemed really important to us, and we were scared about adding/removing/reordering entries. GH 2841 showed that those fears were unfounded, but it was changing a ton of code so I didn't eliminate the table entirely at that time. The table wasn't buying us anything - we were able to properly size it via `eMaxKernel32Function`, but we still had to mention every function to manually form the `eMeow` enumerators. By introducing `DEFINEFUNCTIONPOINTER`, we can spend the same number of lines to form separate global variables, which will still consume the same amount of memory as the array (they're just pointers, they pack perfectly either way). I'm naming these function pointers `__KERNEL32Function_##fn_name` with an underscore in the middle for clarity, e.g. `__KERNEL32Function_GetTempPath2W`. I thought about omitting `Function` but it seems reasonable here. The advantage of this technique is that every function pointer can be given the correct type `decltype(&fn_name)`. We need to adjust how we cast the return value of `GetProcAddress()`, but then we don't need to cast when reading the variable later. Note that `IFDYNAMICGETCACHEDFUNCTION` still forms `const auto pf##fn_name` to defend against modifying the cache unintentionally, and to provide a nice name `pfMeow` for the code that follows its usage. Finally, I'm changing all of the macro parameter names to `fn_name` for consistency.
Co-authored-by: A. Jiang <de34@live.cn>
CaseyCarter
approved these changes
Oct 24, 2023
Member
Author
|
I'm mirroring this to the MSVC-internal repo - please notify me if any further changes are pushed. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
winapisupp.cpp: Use separate function pointers with proper types, instead of enumerators into an array ofPVOID.This is within an unnamed namespace (after #2841 dramatically simplified the code), so we can freely modify this machinery.
Originally, the function pointer table's ordering seemed really important to us, and we were scared about adding/removing/reordering entries. #2841 showed that those fears were unfounded, but it was changing a ton of code so I didn't eliminate the table entirely at that time.
The table wasn't buying us anything - we were able to properly size it via
eMaxKernel32Function, but we still had to mention every function to manually form theeMeowenumerators. By introducingDEFINEFUNCTIONPOINTER, we can spend the same number of lines to form separate global variables, which will still consume the same amount of memory as the array (they're just pointers, they pack perfectly either way).I'm naming these function pointers
__KERNEL32Function_##fn_namewith an underscore in the middle for clarity, e.g.__KERNEL32Function_GetTempPath2W. I thought about omittingFunctionbut it seems reasonable here. The advantage of this technique is that every function pointer can be given the correct typedecltype(&fn_name). We need to adjust how we cast the return value ofGetProcAddress(), but then we don't need to cast when reading the variable later.Note that
IFDYNAMICGETCACHEDFUNCTIONstill formsconst auto pf##fn_nameto defend against modifying the cache unintentionally, and to provide a nice namepfMeowfor the code that follows its usage.Finally, I'm changing all of the macro parameter names to
fn_namefor consistency.@frederick-vs-ja noticed an opportunity to simplify
if (T meow = init; meow)toif (T meow = init); I also found a couple of lines instl/src/tzdb.cppthat could benefit.