From e57c9c0931cba3cdec6fba562aeb5c1910d8f2c4 Mon Sep 17 00:00:00 2001 From: Lucas Santos <117400842+LucasSantos91@users.noreply.github.com> Date: Thu, 9 May 2024 21:39:11 -0300 Subject: [PATCH] Avoid unnecessary operation in PageAllocator. There's no need to call `alignForward` before `VirtualAlloc`. From [MSDN](https://learn.microsoft.com/en-us/windows/win32/api/memoryapi/nf-memoryapi-virtualalloc): ``` If the lpAddress parameter is NULL, this value is rounded up to the next page boundary ``` --- lib/std/heap/PageAllocator.zig | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/lib/std/heap/PageAllocator.zig b/lib/std/heap/PageAllocator.zig index 67cafe48d9..4188c25528 100644 --- a/lib/std/heap/PageAllocator.zig +++ b/lib/std/heap/PageAllocator.zig @@ -19,18 +19,22 @@ fn alloc(_: *anyopaque, n: usize, log2_align: u8, ra: usize) ?[*]u8 { _ = log2_align; assert(n > 0); if (n > maxInt(usize) - (mem.page_size - 1)) return null; - const aligned_len = mem.alignForward(usize, n, mem.page_size); if (native_os == .windows) { const addr = windows.VirtualAlloc( null, - aligned_len, + + // VirtualAlloc will round the length to a multiple of page size. + // VirtualAlloc docs: If the lpAddress parameter is NULL, this value is rounded up to the next page boundary + n, + windows.MEM_COMMIT | windows.MEM_RESERVE, windows.PAGE_READWRITE, ) catch return null; return @ptrCast(addr); } + const aligned_len = mem.alignForward(usize, n, mem.page_size); const hint = @atomicLoad(@TypeOf(std.heap.next_mmap_addr_hint), &std.heap.next_mmap_addr_hint, .unordered); const slice = posix.mmap( hint,