authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2026-01-15 13:48:27-08:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2026-01-15 14:18:21-08:00
logd2585e68a79b8e43166e797d6adc33282acac046
tree9f5f630e1db7f3fb8a800874b0ecb9e97e20c43b
parentde347dd6a981151609f1b9e79183d3f44046abc0

std.Io.Threaded: use NtExtendSection in fileMemoryMapSetLength

is this a good idea? not sure yet. reverting in the next commit

2 files changed, 42 insertions(+), 20 deletions(-)

lib/std/Io/Threaded.zig+37-20
......@@ -16279,6 +16279,8 @@ fn createFileMap(
1627916279 .SUCCESS => {},
1628016280 .CONFLICTING_ADDRESSES => return error.MappingAlreadyExists,
1628116281 .SECTION_PROTECTION => return error.PermissionDenied,
16282 .ACCESS_DENIED => return error.AccessDenied,
16283 .INVALID_VIEW_SIZE => |status| return windows.statusBug(status),
1628216284 else => |status| return windows.unexpectedStatus(status),
1628316285 }
1628416286 if (builtin.mode == .Debug) {
......@@ -16392,32 +16394,47 @@ fn fileMemoryMapSetLength(
1639216394 const new_len = options.len;
1639316395
1639416396 if (mm.section) |section| {
16395 if (alignment.forward(new_len) == alignment.forward(old_memory.len)) {
16397 const aligned_old_len = alignment.forward(old_memory.len);
16398 const aligned_new_len = alignment.forward(new_len);
16399 if (aligned_new_len == aligned_old_len) {
1639616400 mm.memory.len = new_len;
1639716401 return;
1639816402 }
1639916403 switch (native_os) {
1640016404 .windows => {
16401 var contents_ptr: ?[*]align(page_align) u8 = null;
16402 var contents_len = new_len;
16403 switch (windows.ntdll.NtMapViewOfSection(
16404 section,
16405 windows.current_process,
16406 @ptrCast(&contents_ptr),
16407 null,
16408 0,
16409 null,
16410 &contents_len,
16411 .Unmap,
16412 .{},
16413 .{ .READWRITE = true },
16414 )) {
16415 .SUCCESS => {},
16416 .SECTION_PROTECTION => return error.PermissionDenied,
16417 else => |status| return windows.unexpectedStatus(status),
16405 if (aligned_new_len > aligned_old_len) {
16406 var new_section_size: windows.LARGE_INTEGER = @intCast(aligned_new_len);
16407 switch (windows.ntdll.NtExtendSection(section, &new_section_size)) {
16408 .SUCCESS => {},
16409 else => |status| return windows.unexpectedStatus(status),
16410 }
16411 assert(new_section_size == aligned_new_len);
16412 mm.memory.len = new_len;
16413 } else {
16414 _ = windows.ntdll.NtUnmapViewOfSection(windows.current_process, old_memory.ptr);
16415 windows.CloseHandle(section);
16416 var contents_len = aligned_new_len;
16417 var contents_ptr: ?[*]align(std.heap.page_size_min) u8 = null;
16418 switch (windows.ntdll.NtMapViewOfSection(
16419 section,
16420 windows.current_process,
16421 @ptrCast(&contents_ptr),
16422 null,
16423 0,
16424 null,
16425 &contents_len,
16426 .Unmap,
16427 .{},
16428 .{ .READWRITE = true },
16429 )) {
16430 .SUCCESS => {},
16431 .SECTION_PROTECTION => return error.PermissionDenied,
16432 .INVALID_VIEW_SIZE => |status| return windows.statusBug(status),
16433 else => |status| return windows.unexpectedStatus(status),
16434 }
16435 assert(contents_len == aligned_new_len);
16436 mm.memory = contents_ptr.?[0..new_len];
1641816437 }
16419 assert(contents_len == alignment.forward(new_len));
16420 mm.memory = contents_ptr.?[0..new_len];
1642116438 },
1642216439 .wasi => unreachable,
1642316440 .linux => {
lib/std/os/windows/ntdll.zig+5
......@@ -253,6 +253,11 @@ pub extern "ntdll" fn NtCreateSection(
253253 FileHandle: ?HANDLE,
254254) callconv(.winapi) NTSTATUS;
255255
256pub extern "ntdll" fn NtExtendSection(
257 SectionHandle: HANDLE,
258 NewSectionSize: *LARGE_INTEGER,
259) callconv(.winapi) NTSTATUS;
260
256261pub extern "ntdll" fn NtAllocateVirtualMemory(
257262 ProcessHandle: HANDLE,
258263 BaseAddress: *PVOID,