Skip to content

std.heap.PageAllocator updates to fix race condition and utilize NtAllocateVirtualMemory / NtFreeVirtualMemory instead of VirtualAlloc / VirtualFree #23097

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

Merged
merged 2 commits into from
Mar 6, 2025

Conversation

ziggoon
Copy link
Contributor

@ziggoon ziggoon commented Mar 5, 2025

This PR attempts to address some of the concerns outlined in #22846

Two new functions (NtAllocateVirtualMemory and NtFreeVirtualMemory) were added to ntdll.zig and windows.zig as an FFI signature and Zig wrapper, respectively.

Changes made to std.heap.PageAllocator include:
Usage of VirtualAlloc and VirtualFree has been replaced with their respective NT counterpart and memory allocations are now using placeholder constants to prevent the race condition previously present. Lastly, instead of freeing the entire region - the unaligned portions at the head and tail of the region are freed which should improve performance.

ziggoon added 2 commits March 4, 2025 22:01
…ondition and utilize NtAllocateVirtualMemory / NtFreeVirtualMemory instead of VirtualAlloc and VirtualFree
…+ add missing alloction constants MEM_RESERVE_PLACEHOLDER / MEM_PRESERVE_PLACEHOLDER
@der-teufel-programming
Copy link
Contributor

Looks good to me and it agrees with what I've learnt about those Nt*VirtualMemory functions

@andrewrk andrewrk merged commit 4cefd1b into ziglang:master Mar 6, 2025
9 checks passed
@andrewrk
Copy link
Member

andrewrk commented Mar 6, 2025

Good work, thank you. A possible improvement could be to only try the first code path when the requested alignment is below page_size_min, otherwise jump straight to the second strategy.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants