authorgravatar for rpkak@noreply.codeberg.orgrpkak <rpkak@noreply.codeberg.org> 2026-02-01 11:21:16+01:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2026-02-03 20:27:28+01:00
log184c8f9545944333bf122203c8d813785b43df8a
tree9a4987c077dbab71ba061834d0f953c57c008243
parente9b442db5a2b03c09f8286cab94d9ce4976bc077

std.heap.PageAllocator: align hint


1 files changed, 9 insertions(+), 7 deletions(-)

lib/std/heap/PageAllocator.zig+9-7
...@@ -87,12 +87,14 @@ pub fn map(n: usize, alignment: mem.Alignment) ?[*]u8 {...@@ -87,12 +87,14 @@ pub fn map(n: usize, alignment: mem.Alignment) ?[*]u8 {
87 }87 }
8888
89 const aligned_len = mem.alignForward(usize, n, page_size);89 const aligned_len = mem.alignForward(usize, n, page_size);
90 const max_drop_len = alignment_bytes - @min(alignment_bytes, page_size);90 const max_drop_len = alignment_bytes -| page_size;
91 const overalloc_len = if (max_drop_len <= aligned_len - n)91 const overalloc_len = aligned_len + max_drop_len;
92 aligned_len92 const maybe_unaligned_hint = @atomicLoad(@TypeOf(std.heap.next_mmap_addr_hint), &std.heap.next_mmap_addr_hint, .unordered);
93 else93
94 mem.alignForward(usize, aligned_len + max_drop_len, page_size);94 // Aligning hint does not use mem.alignPointer, because it is slow.
95 const hint = @atomicLoad(@TypeOf(std.heap.next_mmap_addr_hint), &std.heap.next_mmap_addr_hint, .unordered);95 // Aligning hint does not use mem.alignForward, because it asserts that there will be no overflow.
96 const hint: ?[*]align(page_size_min) u8 = @ptrFromInt(((@intFromPtr(maybe_unaligned_hint)) +% (alignment_bytes - 1)) & ~(alignment_bytes - 1));
97
96 const slice = posix.mmap(98 const slice = posix.mmap(
97 hint,99 hint,
98 overalloc_len,100 overalloc_len,
...@@ -110,7 +112,7 @@ pub fn map(n: usize, alignment: mem.Alignment) ?[*]u8 {...@@ -110,7 +112,7 @@ pub fn map(n: usize, alignment: mem.Alignment) ?[*]u8 {
110 const remaining_len = overalloc_len - drop_len;112 const remaining_len = overalloc_len - drop_len;
111 if (remaining_len > aligned_len) posix.munmap(@alignCast(result_ptr[aligned_len..remaining_len]));113 if (remaining_len > aligned_len) posix.munmap(@alignCast(result_ptr[aligned_len..remaining_len]));
112 const new_hint: [*]align(page_size_min) u8 = @alignCast(result_ptr + aligned_len);114 const new_hint: [*]align(page_size_min) u8 = @alignCast(result_ptr + aligned_len);
113 _ = @cmpxchgStrong(@TypeOf(std.heap.next_mmap_addr_hint), &std.heap.next_mmap_addr_hint, hint, new_hint, .monotonic, .monotonic);115 _ = @cmpxchgStrong(@TypeOf(std.heap.next_mmap_addr_hint), &std.heap.next_mmap_addr_hint, maybe_unaligned_hint, new_hint, .monotonic, .monotonic);
114 return result_ptr;116 return result_ptr;
115}117}
116118