std.heap.PageAllocator.addr_hint
变量
向上提供地址提示时,此值指向下一次期望分配的页面; 向下提供提示时,此值指向上一次成功分配的 起始位置。
TODO: 在 Windows 上利用此项。
?[*]align(page_size_min) u8
std.heap.PageAllocator.Alignment
别名
别名目标 std.mem.Alignment
std.heap.PageAllocator.alloc
函数
fn alloc(context: *anyopaque, n: usize, alignment: Alignment, ra: usize) ?[*]u8
参数
std.heap.PageAllocator.Allocator
别名
别名目标 std.mem.Allocator
std.heap.PageAllocator.assert
别名
别名目标 std.debug.assert
std.heap.PageAllocator.builtin
常量
std.heap.PageAllocator.enable_hints
常量
在尽力避免复用映射的操作系统上禁用地址提示。 例如,OpenBSD 会积极随机化未提供提示的映射地址, 这虽出于安全原因, 也满足了这里的需要。
std.heap.PageAllocator.free
函数
fn free(context: *anyopaque, memory: []u8, alignment: Alignment, return_address: usize) void
参数
std.heap.PageAllocator.map
函数
pub fn map(n: usize, alignment: Alignment) ?[*]u8
参数
n: usize
alignment: Alignment
std.heap.PageAllocator.maxInt
别名
别名目标 std.math.maxInt
std.heap.PageAllocator.mem
别名
别名目标 std.mem
std.heap.PageAllocator.native_os
常量
std.heap.PageAllocator.ntdll
别名
别名目标 std.os.windows.ntdll
std.heap.PageAllocator.page_size_min
别名
std.heap.PageAllocator.posix
别名
别名目标 std.posix
std.heap.PageAllocator.realloc
函数
pub fn realloc(uncasted_memory: []u8, alignment: Alignment, new_len: usize, may_move: bool) ?[*]u8
参数
std.heap.PageAllocator.remap
函数
fn remap(context: *anyopaque, memory: []u8, alignment: Alignment, new_len: usize, return_address: usize) ?[*]u8
参数
std.heap.PageAllocator.resize
函数
fn resize(context: *anyopaque, memory: []u8, alignment: Alignment, new_len: usize, return_address: usize) bool
参数
std.heap.PageAllocator.stack_direction
常量
在不立即映射整个栈的操作系统上,必须 谨慎避免将地址提示指向栈保护间隙后、栈将扩展到的 页面。避免此问题最简单的方法是沿 栈增长方向提供地址提示。
std.heap.PageAllocator.std
别名
别名目标 std
std.heap.PageAllocator.unmap
函数
pub fn unmap(memory: []align(page_size_min) u8) void
参数
memory: []align(page_size_min) u8
std.heap.PageAllocator.vtable
常量
Allocator.VTable
std.heap.PageAllocator.windows
别名
别名目标 std.os.windows
源码
std/heap/PageAllocator.zig
const builtin = @import("builtin");
const native_os = builtin.os.tag;
const std = @import("../std.zig");
const Allocator = std.mem.Allocator;
const Alignment = std.mem.Alignment;
const mem = std.mem;
const maxInt = std.math.maxInt;
const assert = std.debug.assert;
const windows = std.os.windows;
const ntdll = std.os.windows.ntdll;
const posix = std.posix;
const page_size_min = std.heap.page_size_min;
pub const vtable: Allocator.VTable = .{
.alloc = alloc,
.resize = resize,
.remap = remap,
.free = free,
};
/// 在尽力避免复用映射的操作系统上禁用地址提示。
/// 例如,OpenBSD 会积极随机化未提供提示的映射地址,
/// 这虽出于安全原因,
/// 也满足了这里的需要。
const enable_hints = switch (builtin.target.os.tag) {
.linux => !builtin.target.cpu.arch.isSPARC(), // https://bugzilla.kernel.org/show_bug.cgi?id=221820
.openbsd => false,
else => true,
};
/// 在不立即映射整个栈的操作系统上,必须
/// 谨慎避免将地址提示指向栈保护间隙后、栈将扩展到的
/// 页面。避免此问题最简单的方法是沿
/// 栈增长方向提供地址提示。
const stack_direction = builtin.target.stackGrowth();
/// 向上提供地址提示时,此值指向下一次期望分配的页面;
/// 向下提供提示时,此值指向上一次成功分配的
/// 起始位置。
///
/// TODO: 在 Windows 上利用此项。
var addr_hint: ?[*]align(page_size_min) u8 = null;
pub fn map(n: usize, alignment: Alignment) ?[*]u8 {
const page_size = std.heap.pageSize();
if (n >= maxInt(usize) - page_size) return null;
const alignment_bytes = alignment.toByteUnits();
if (native_os == .windows) {
var base_addr: ?*anyopaque = null;
var size: windows.SIZE_T = n;
const current_process = windows.GetCurrentProcess();
var status = ntdll.NtAllocateVirtualMemory(current_process, @ptrCast(&base_addr), 0, &size, .{ .COMMIT = true, .RESERVE = true }, .{ .READWRITE = true });
if (status == .SUCCESS and mem.isAligned(@intFromPtr(base_addr), alignment_bytes)) {
return @ptrCast(base_addr);
}
if (status == .SUCCESS) {
var region_size: windows.SIZE_T = 0;
_ = ntdll.NtFreeVirtualMemory(current_process, @ptrCast(&base_addr), ®ion_size, .{ .RELEASE = true });
}
const overalloc_len = n + alignment_bytes - page_size;
const page_aligned_len = mem.alignForward(usize, n, page_size);
base_addr = null;
size = overalloc_len;
status = ntdll.NtAllocateVirtualMemory(current_process, @ptrCast(&base_addr), 0, &size, .{ .RESERVE = true, .RESERVE_PLACEHOLDER = true }, .{ .NOACCESS = true });
if (status != .SUCCESS) return null;
const placeholder_addr = @intFromPtr(base_addr);
const aligned_addr = mem.alignForward(usize, placeholder_addr, alignment_bytes);
const prefix_size = aligned_addr - placeholder_addr;
if (prefix_size > 0) {
var prefix_base = base_addr;
var prefix_size_param: windows.SIZE_T = prefix_size;
_ = ntdll.NtFreeVirtualMemory(current_process, @ptrCast(&prefix_base), &prefix_size_param, .{ .RELEASE = true, .PRESERVE_PLACEHOLDER = true });
}
const suffix_start = aligned_addr + page_aligned_len;
const suffix_size = (placeholder_addr + overalloc_len) - suffix_start;
if (suffix_size > 0) {
var suffix_base = @as(?*anyopaque, @ptrFromInt(suffix_start));
var suffix_size_param: windows.SIZE_T = suffix_size;
_ = ntdll.NtFreeVirtualMemory(current_process, @ptrCast(&suffix_base), &suffix_size_param, .{ .RELEASE = true, .PRESERVE_PLACEHOLDER = true });
}
base_addr = @ptrFromInt(aligned_addr);
size = page_aligned_len;
status = ntdll.NtAllocateVirtualMemory(current_process, @ptrCast(&base_addr), 0, &size, .{ .COMMIT = true }, .{ .READWRITE = true });
if (status == .SUCCESS) {
return @ptrCast(base_addr);
}
base_addr = @as(?*anyopaque, @ptrFromInt(aligned_addr));
size = page_aligned_len;
_ = ntdll.NtFreeVirtualMemory(current_process, @ptrCast(&base_addr), &size, .{ .RELEASE = true });
return null;
}
const page_aligned_len = mem.alignForward(usize, n, page_size);
const max_drop_len = alignment_bytes -| page_size;
const overalloc_len = page_aligned_len + max_drop_len;
const maybe_unaligned_hint, const hint = blk: {
if (!enable_hints) break :blk .{ null, null };
const maybe_unaligned_hint = @atomicLoad(@TypeOf(addr_hint), &addr_hint, .unordered);
// 首次 mmap 时由内核选择合适的起始地址;
// 此后以该地址为起点提供地址提示。
if (maybe_unaligned_hint == null) break :blk .{ null, null };
// 对齐提示不使用 mem.alignPointer,因为它很慢。
// 对齐提示不使用 mem.alignForward,因为它会断言不发生溢出。
const hint: ?[*]align(page_size_min) u8 = @ptrFromInt(switch (stack_direction) {
.down => ((@intFromPtr(maybe_unaligned_hint) -% page_aligned_len) & ~(alignment_bytes - 1)) -% max_drop_len,
.up => (@intFromPtr(maybe_unaligned_hint) +% (alignment_bytes - 1)) & ~(alignment_bytes - 1),
});
break :blk .{ maybe_unaligned_hint, hint };
};
const slice = posix.mmap(
hint,
overalloc_len,
.{ .READ = true, .WRITE = true },
.{ .TYPE = .PRIVATE, .ANONYMOUS = true },
-1,
0,
) catch return null;
const result_ptr = mem.alignPointer(slice.ptr, alignment_bytes).?;
// 取消映射仅为保证所提供内存范围内存在满足对齐要求的位置
// 而额外请求的字节。
// 这些额外字节可能位于开头、末尾或两端。
const drop_len = result_ptr - slice.ptr;
if (drop_len != 0) posix.munmap(slice[0..drop_len]);
const remaining_len = overalloc_len - drop_len;
if (remaining_len > page_aligned_len) posix.munmap(@alignCast(result_ptr[page_aligned_len..remaining_len]));
if (enable_hints) {
const new_hint: [*]align(page_size_min) u8 = @alignCast(result_ptr + switch (stack_direction) {
.up => page_aligned_len,
.down => 0,
});
_ = @cmpxchgStrong(@TypeOf(addr_hint), &addr_hint, maybe_unaligned_hint, new_hint, .monotonic, .monotonic);
}
return result_ptr;
}
fn alloc(context: *anyopaque, n: usize, alignment: Alignment, ra: usize) ?[*]u8 {
_ = context;
_ = ra;
assert(n > 0);
return map(n, alignment);
}
fn resize(context: *anyopaque, memory: []u8, alignment: Alignment, new_len: usize, return_address: usize) bool {
_ = context;
_ = return_address;
return realloc(memory, alignment, new_len, false) != null;
}
fn remap(context: *anyopaque, memory: []u8, alignment: Alignment, new_len: usize, return_address: usize) ?[*]u8 {
_ = context;
_ = return_address;
return realloc(memory, alignment, new_len, true);
}
fn free(context: *anyopaque, memory: []u8, alignment: Alignment, return_address: usize) void {
_ = context;
_ = return_address;
_ = alignment;
return unmap(@alignCast(memory));
}
pub fn unmap(memory: []align(page_size_min) u8) void {
if (native_os == .windows) {
var base_addr: ?*anyopaque = memory.ptr;
var region_size: windows.SIZE_T = 0;
_ = ntdll.NtFreeVirtualMemory(windows.GetCurrentProcess(), @ptrCast(&base_addr), ®ion_size, .{ .RELEASE = true });
} else {
const page_aligned_len = mem.alignForward(usize, memory.len, std.heap.pageSize());
posix.munmap(memory.ptr[0..page_aligned_len]);
}
}
pub fn realloc(uncasted_memory: []u8, alignment: Alignment, new_len: usize, may_move: bool) ?[*]u8 {
const memory: []align(page_size_min) u8 = @alignCast(uncasted_memory);
const page_size = std.heap.pageSize();
if (alignment.toByteUnits() > page_size) return null;
const new_size_aligned = mem.alignForward(usize, new_len, page_size);
if (native_os == .windows) {
if (new_len <= memory.len) {
const base_addr = @intFromPtr(memory.ptr);
const old_addr_end = base_addr + memory.len;
const new_addr_end = mem.alignForward(usize, base_addr + new_len, page_size);
if (old_addr_end > new_addr_end) {
var decommit_addr: ?*anyopaque = @ptrFromInt(new_addr_end);
var decommit_size: windows.SIZE_T = old_addr_end - new_addr_end;
_ = ntdll.NtAllocateVirtualMemory(windows.GetCurrentProcess(), @ptrCast(&decommit_addr), 0, &decommit_size, .{ .RESET = true }, .{ .NOACCESS = true });
}
return memory.ptr;
}
const old_size_aligned = mem.alignForward(usize, memory.len, page_size);
if (new_size_aligned <= old_size_aligned) {
return memory.ptr;
}
return null;
}
const page_aligned_len = mem.alignForward(usize, memory.len, page_size);
if (new_size_aligned == page_aligned_len)
return memory.ptr;
// 栈向下增长时,仅在允许移动分配块的情况下使用 `mremap`。
// 否则,扩展分配块可能侵占
// 原本要留给栈使用的虚拟地址空间。
if (posix.MREMAP != void and (stack_direction == .up or may_move)) {
// TODO: 若 next_mmap_addr_hint 位于重新映射的范围内,则更新它
const new_memory = posix.mremap(memory.ptr, page_aligned_len, new_size_aligned, .{ .MAYMOVE = may_move }, null) catch return null;
return new_memory.ptr;
}
if (new_size_aligned < page_aligned_len) {
const ptr = memory.ptr + new_size_aligned;
// TODO: 若 next_mmap_addr_hint 位于已解除映射的范围内,则更新它
posix.munmap(@alignCast(ptr[0 .. page_aligned_len - new_size_aligned]));
return memory.ptr;
}
return null;
}