Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Various cleanups: Store properly-typed function pointers in winapisupp.cpp #4123

Conversation

StephanTLavavej
Copy link
Member

@StephanTLavavej StephanTLavavej commented Oct 23, 2023

winapisupp.cpp: Use separate function pointers with proper types, instead of enumerators into an array of PVOID.

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 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.

@frederick-vs-ja noticed an opportunity to simplify if (T meow = init; meow) to if (T meow = init); I also found a couple of lines in stl/src/tzdb.cpp that could benefit.

…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.
@StephanTLavavej StephanTLavavej added the enhancement Something can be improved label Oct 23, 2023
@StephanTLavavej StephanTLavavej requested a review from a team as a code owner October 23, 2023 16:35
Co-authored-by: A. Jiang <de34@live.cn>
@StephanTLavavej StephanTLavavej self-assigned this Oct 24, 2023
@StephanTLavavej
Copy link
Member Author

I'm mirroring this to the MSVC-internal repo - please notify me if any further changes are pushed.

@StephanTLavavej StephanTLavavej merged commit 2c81218 into microsoft:main Oct 25, 2023
35 checks passed
@StephanTLavavej StephanTLavavej deleted the dry-cleaning-7-winapisupp-function-pointers branch October 25, 2023 23:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement Something can be improved
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants