| ... | @@ -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 | } |
| 88 | | 88 | |
| 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_len | 92 | const maybe_unaligned_hint = @atomicLoad(@TypeOf(std.heap.next_mmap_addr_hint), &std.heap.next_mmap_addr_hint, .unordered); |
| 93 | else | 93 | |
| 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 | } |
| 116 | | 118 | |