Skip to content

Commit 8f09e89

Browse files
committed
std.os.windows: move kernel32 api to ntdll.
kernel32.AddVectoredExceptionHandler -> ntdll.RtlAddVectoredExceptionHandler kernel32.RemoveVectoredExceptionHandler -> ntdll.RtlRemoveVectoredExceptionHandler kernel32.ExitProcess -> ntdll.RtlExitUserProcess kernel32.InitializeCriticalSection -> ntdll.RtlInitializeCriticalSection kernel32.EnterCriticalSection -> ntdll.RtlEnterCriticalSection kernel32.LeaveCriticalSection -> ntdll.RtlLeaveCriticalSection kernel32.DeleteCriticalSection -> ntdll.RtlDeleteCriticalSection kernel32.TryAcquireSRWLockExclusive -> ntdll.RtlTryAcquireSRWLockExclusive kernel32.AcquireSRWLockExclusive -> ntdll.RtlAcquireSRWLockExclusive kernel32.ReleaseSRWLockExclusive -> ntdll.RtlReleaseSRWLockExclusive kernel32.WakeConditionVariable -> ntdll.RtlWakeConditionVariable kernel32.WakeAllConditionVariable -> ntdll.RtlWakeAllConditionVariable kernel32.HeapReAlloc -> ntdll.RtlReAllocateHeap kernel32.HeapAlloc -> ntdll.RtlAllocateHeap kernel32.HeapFree -> ntdll.RtlFreeHeap
1 parent 51a229c commit 8f09e89

File tree

7 files changed

+16
-36
lines changed

7 files changed

+16
-36
lines changed

lib/std/Thread.zig

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -650,7 +650,7 @@ const WindowsThreadImpl = struct {
650650
thread_handle: windows.HANDLE = undefined,
651651

652652
fn free(self: ThreadCompletion) void {
653-
const status = windows.kernel32.HeapFree(self.heap_handle, 0, self.heap_ptr);
653+
const status = windows.ntdll.RtlFreeHeap(self.heap_handle, 0, self.heap_ptr);
654654
assert(status != 0);
655655
}
656656
};
@@ -674,8 +674,8 @@ const WindowsThreadImpl = struct {
674674

675675
const heap_handle = windows.kernel32.GetProcessHeap() orelse return error.OutOfMemory;
676676
const alloc_bytes = @alignOf(Instance) + @sizeOf(Instance);
677-
const alloc_ptr = windows.kernel32.HeapAlloc(heap_handle, 0, alloc_bytes) orelse return error.OutOfMemory;
678-
errdefer assert(windows.kernel32.HeapFree(heap_handle, 0, alloc_ptr) != 0);
677+
const alloc_ptr = windows.ntdll.RtlAllocateHeap(heap_handle, 0, alloc_bytes) orelse return error.OutOfMemory;
678+
errdefer assert(windows.ntdll.RtlFreeHeap(heap_handle, 0, alloc_ptr) != 0);
679679

680680
const instance_bytes = @as([*]u8, @ptrCast(alloc_ptr))[0..alloc_bytes];
681681
var fba = std.heap.FixedBufferAllocator.init(instance_bytes);

lib/std/Thread/Condition.zig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -180,8 +180,8 @@ const WindowsImpl = struct {
180180

181181
fn wake(self: *Impl, comptime notify: Notify) void {
182182
switch (notify) {
183-
.one => os.windows.kernel32.WakeConditionVariable(&self.condition),
184-
.all => os.windows.kernel32.WakeAllConditionVariable(&self.condition),
183+
.one => os.windows.ntdll.RtlWakeConditionVariable(&self.condition),
184+
.all => os.windows.ntdll.RtlWakeAllConditionVariable(&self.condition),
185185
}
186186
}
187187
};

lib/std/Thread/Mutex.zig

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,15 +109,15 @@ const WindowsImpl = struct {
109109
srwlock: windows.SRWLOCK = .{},
110110

111111
fn tryLock(self: *@This()) bool {
112-
return windows.kernel32.TryAcquireSRWLockExclusive(&self.srwlock) != windows.FALSE;
112+
return windows.ntdll.RtlTryAcquireSRWLockExclusive(&self.srwlock) != windows.FALSE;
113113
}
114114

115115
fn lock(self: *@This()) void {
116-
windows.kernel32.AcquireSRWLockExclusive(&self.srwlock);
116+
windows.ntdll.RtlAcquireSRWLockExclusive(&self.srwlock);
117117
}
118118

119119
fn unlock(self: *@This()) void {
120-
windows.kernel32.ReleaseSRWLockExclusive(&self.srwlock);
120+
windows.ntdll.RtlReleaseSRWLockExclusive(&self.srwlock);
121121
}
122122

123123
const windows = std.os.windows;

lib/std/debug.zig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1430,7 +1430,7 @@ pub fn attachSegfaultHandler() void {
14301430
@compileError("segfault handler not supported for this target");
14311431
}
14321432
if (native_os == .windows) {
1433-
windows_segfault_handle = windows.kernel32.AddVectoredExceptionHandler(0, handleSegfaultWindows);
1433+
windows_segfault_handle = windows.ntdll.RtlAddVectoredExceptionHandler(0, handleSegfaultWindows);
14341434
return;
14351435
}
14361436
const act = posix.Sigaction{
@@ -1444,7 +1444,7 @@ pub fn attachSegfaultHandler() void {
14441444
fn resetSegfaultHandler() void {
14451445
if (native_os == .windows) {
14461446
if (windows_segfault_handle) |handle| {
1447-
assert(windows.kernel32.RemoveVectoredExceptionHandler(handle) != 0);
1447+
assert(windows.ntdll.RtlRemoveVectoredExceptionHandler(handle) != 0);
14481448
windows_segfault_handle = null;
14491449
}
14501450
return;

lib/std/os/windows/advapi32.zig

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,10 @@ pub extern "advapi32" fn RegQueryValueExW(
2828

2929
pub extern "advapi32" fn RegCloseKey(hKey: HKEY) callconv(.winapi) LSTATUS;
3030

31-
// RtlGenRandom is known as SystemFunction036 under advapi32
32-
// http://msdn.microsoft.com/en-us/library/windows/desktop/aa387694.aspx */
33-
pub extern "advapi32" fn SystemFunction036(output: [*]u8, length: ULONG) callconv(.winapi) BOOL;
31+
// RtlGenRandom is known as SystemFunction036 under advapi32( -> cryptbase)
32+
// http://msdn.microsoft.com/en-us/library/windows/desktop/aa387694.aspx
33+
// https://github.com/microsoft/terminal/blob/b7bd4f7dcfa94554dc3006049bcc8c526e5207d6/src/inc/til/rand.h#L20-L24
34+
pub extern "cryptbase" fn SystemFunction036(output: [*]u8, length: ULONG) callconv(.winapi) BOOL;
3435
pub const RtlGenRandom = SystemFunction036;
3536

3637
pub const RRF = struct {

lib/std/os/windows/kernel32.zig

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -255,9 +255,6 @@ pub extern "kernel32" fn CreateIoCompletionPort(
255255
NumberOfConcurrentThreads: DWORD,
256256
) callconv(.winapi) ?HANDLE;
257257

258-
pub const AddVectoredExceptionHandler = ntdll.RtlAddVectoredExceptionHandler;
259-
pub const RemoveVectoredExceptionHandler = ntdll.RtlRemoveVectoredExceptionHandler;
260-
261258
// TODO: Wrapper around RtlReportSilentProcessExit + NtTerminateProcess.
262259
pub extern "kernel32" fn TerminateProcess(
263260
hProcess: HANDLE,
@@ -310,8 +307,6 @@ pub extern "kernel32" fn CreateProcessW(
310307
lpProcessInformation: *PROCESS_INFORMATION,
311308
) callconv(.winapi) BOOL;
312309

313-
pub const ExitProcess = ntdll.RtlExitUserProcess;
314-
315310
// TODO: implement via ntdll instead
316311
pub extern "kernel32" fn SleepEx(
317312
dwMilliseconds: DWORD,
@@ -358,25 +353,13 @@ pub extern "kernel32" fn SwitchToThread() callconv(.winapi) BOOL;
358353

359354
// Locks, critical sections, initializers
360355

361-
pub const InitializeCriticalSection = ntdll.RtlInitializeCriticalSection;
362-
pub const EnterCriticalSection = ntdll.RtlEnterCriticalSection;
363-
pub const LeaveCriticalSection = ntdll.RtlLeaveCriticalSection;
364-
pub const DeleteCriticalSection = ntdll.RtlDeleteCriticalSection;
365-
366-
pub const TryAcquireSRWLockExclusive = ntdll.RtlTryAcquireSRWLockExclusive;
367-
pub const AcquireSRWLockExclusive = ntdll.RtlAcquireSRWLockExclusive;
368-
pub const ReleaseSRWLockExclusive = ntdll.RtlReleaseSRWLockExclusive;
369-
370356
pub extern "kernel32" fn InitOnceExecuteOnce(
371357
InitOnce: *INIT_ONCE,
372358
InitFn: INIT_ONCE_FN,
373359
Parameter: ?*anyopaque,
374360
Context: ?*anyopaque,
375361
) callconv(.winapi) BOOL;
376362

377-
pub const WakeConditionVariable = ntdll.RtlWakeConditionVariable;
378-
pub const WakeAllConditionVariable = ntdll.RtlWakeAllConditionVariable;
379-
380363
// TODO:
381364
// - dwMilliseconds -> LARGE_INTEGER.
382365
// - RtlSleepConditionVariableSRW
@@ -467,10 +450,6 @@ pub extern "kernel32" fn HeapCreate(
467450
dwMaximumSize: SIZE_T,
468451
) callconv(.winapi) ?HANDLE;
469452

470-
pub const HeapReAlloc = ntdll.RtlReAllocateHeap;
471-
pub const HeapAlloc = ntdll.RtlAllocateHeap;
472-
pub const HeapFree = ntdll.RtlFreeHeap;
473-
474453
// TODO: Wrapper around RtlValidateHeap (BOOLEAN -> BOOL)
475454
pub extern "kernel32" fn HeapValidate(
476455
hHeap: HANDLE,

lib/std/posix.zig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -701,7 +701,7 @@ pub fn abort() noreturn {
701701
if (builtin.mode == .Debug and windows.peb().BeingDebugged != 0) {
702702
@breakpoint();
703703
}
704-
windows.kernel32.ExitProcess(3);
704+
windows.ntdll.RtlExitUserProcess(3);
705705
}
706706
if (!builtin.link_libc and native_os == .linux) {
707707
// The Linux man page says that the libc abort() function
@@ -794,7 +794,7 @@ pub fn exit(status: u8) noreturn {
794794
std.c.exit(status);
795795
}
796796
if (native_os == .windows) {
797-
windows.kernel32.ExitProcess(status);
797+
windows.ntdll.RtlExitUserProcess(status);
798798
}
799799
if (native_os == .wasi) {
800800
wasi.proc_exit(status);

0 commit comments

Comments
 (0)