Skip to content

Commit e666972

Browse files
committed
runtime: deduplicate Windows stdcall
There is no need to have a dedicated stdcall variant for each number of arguments. Instead, we can use a variadic function that accepts any number of arguments and handles them uniformly. While here, improve documentation of syscall_syscalln to make it clear that it should not be used within the runtime package. Change-Id: I022afc7f28d969fd7307bb2b1f4594246ac38d18 Reviewed-on: https://go-review.googlesource.com/c/go/+/691215 LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Michael Pratt <mpratt@google.com> Reviewed-by: Mark Freeman <mark@golang.org>
1 parent ef40549 commit e666972

File tree

8 files changed

+120
-186
lines changed

8 files changed

+120
-186
lines changed

src/runtime/export_windows_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ var (
1818

1919
func NumberOfProcessors() int32 {
2020
var info systeminfo
21-
stdcall1(_GetSystemInfo, uintptr(unsafe.Pointer(&info)))
21+
stdcall(_GetSystemInfo, uintptr(unsafe.Pointer(&info)))
2222
return int32(info.dwnumberofprocessors)
2323
}
2424

src/runtime/mem_windows.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,11 @@ const (
2626
//
2727
//go:nosplit
2828
func sysAllocOS(n uintptr, _ string) unsafe.Pointer {
29-
return unsafe.Pointer(stdcall4(_VirtualAlloc, 0, n, _MEM_COMMIT|_MEM_RESERVE, _PAGE_READWRITE))
29+
return unsafe.Pointer(stdcall(_VirtualAlloc, 0, n, _MEM_COMMIT|_MEM_RESERVE, _PAGE_READWRITE))
3030
}
3131

3232
func sysUnusedOS(v unsafe.Pointer, n uintptr) {
33-
r := stdcall3(_VirtualFree, uintptr(v), n, _MEM_DECOMMIT)
33+
r := stdcall(_VirtualFree, uintptr(v), n, _MEM_DECOMMIT)
3434
if r != 0 {
3535
return
3636
}
@@ -46,7 +46,7 @@ func sysUnusedOS(v unsafe.Pointer, n uintptr) {
4646
// in the worst case, but that's fast enough.
4747
for n > 0 {
4848
small := n
49-
for small >= 4096 && stdcall3(_VirtualFree, uintptr(v), small, _MEM_DECOMMIT) == 0 {
49+
for small >= 4096 && stdcall(_VirtualFree, uintptr(v), small, _MEM_DECOMMIT) == 0 {
5050
small /= 2
5151
small &^= 4096 - 1
5252
}
@@ -60,7 +60,7 @@ func sysUnusedOS(v unsafe.Pointer, n uintptr) {
6060
}
6161

6262
func sysUsedOS(v unsafe.Pointer, n uintptr) {
63-
p := stdcall4(_VirtualAlloc, uintptr(v), n, _MEM_COMMIT, _PAGE_READWRITE)
63+
p := stdcall(_VirtualAlloc, uintptr(v), n, _MEM_COMMIT, _PAGE_READWRITE)
6464
if p == uintptr(v) {
6565
return
6666
}
@@ -71,7 +71,7 @@ func sysUsedOS(v unsafe.Pointer, n uintptr) {
7171
k := n
7272
for k > 0 {
7373
small := k
74-
for small >= 4096 && stdcall4(_VirtualAlloc, uintptr(v), small, _MEM_COMMIT, _PAGE_READWRITE) == 0 {
74+
for small >= 4096 && stdcall(_VirtualAlloc, uintptr(v), small, _MEM_COMMIT, _PAGE_READWRITE) == 0 {
7575
small /= 2
7676
small &^= 4096 - 1
7777
}
@@ -105,7 +105,7 @@ func sysHugePageCollapseOS(v unsafe.Pointer, n uintptr) {
105105
//
106106
//go:nosplit
107107
func sysFreeOS(v unsafe.Pointer, n uintptr) {
108-
r := stdcall3(_VirtualFree, uintptr(v), 0, _MEM_RELEASE)
108+
r := stdcall(_VirtualFree, uintptr(v), 0, _MEM_RELEASE)
109109
if r == 0 {
110110
print("runtime: VirtualFree of ", n, " bytes failed with errno=", getlasterror(), "\n")
111111
throw("runtime: failed to release pages")
@@ -121,13 +121,13 @@ func sysReserveOS(v unsafe.Pointer, n uintptr, _ string) unsafe.Pointer {
121121
// v is just a hint.
122122
// First try at v.
123123
// This will fail if any of [v, v+n) is already reserved.
124-
v = unsafe.Pointer(stdcall4(_VirtualAlloc, uintptr(v), n, _MEM_RESERVE, _PAGE_READWRITE))
124+
v = unsafe.Pointer(stdcall(_VirtualAlloc, uintptr(v), n, _MEM_RESERVE, _PAGE_READWRITE))
125125
if v != nil {
126126
return v
127127
}
128128

129129
// Next let the kernel choose the address.
130-
return unsafe.Pointer(stdcall4(_VirtualAlloc, 0, n, _MEM_RESERVE, _PAGE_READWRITE))
130+
return unsafe.Pointer(stdcall(_VirtualAlloc, 0, n, _MEM_RESERVE, _PAGE_READWRITE))
131131
}
132132

133133
func sysMapOS(v unsafe.Pointer, n uintptr, _ string) {

src/runtime/netpoll_windows.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ var (
102102
)
103103

104104
func netpollinit() {
105-
iocphandle = stdcall4(_CreateIoCompletionPort, _INVALID_HANDLE_VALUE, 0, 0, _DWORD_MAX)
105+
iocphandle = stdcall(_CreateIoCompletionPort, _INVALID_HANDLE_VALUE, 0, 0, _DWORD_MAX)
106106
if iocphandle == 0 {
107107
println("runtime: CreateIoCompletionPort failed (errno=", getlasterror(), ")")
108108
throw("runtime: netpollinit failed")
@@ -115,7 +115,7 @@ func netpollIsPollDescriptor(fd uintptr) bool {
115115

116116
func netpollopen(fd uintptr, pd *pollDesc) int32 {
117117
key := packNetpollKey(netpollSourceReady, pd)
118-
if stdcall4(_CreateIoCompletionPort, fd, iocphandle, key, 0) == 0 {
118+
if stdcall(_CreateIoCompletionPort, fd, iocphandle, key, 0) == 0 {
119119
return int32(getlasterror())
120120
}
121121
return 0
@@ -137,7 +137,7 @@ func netpollBreak() {
137137
}
138138

139139
key := packNetpollKey(netpollSourceBreak, nil)
140-
if stdcall4(_PostQueuedCompletionStatus, iocphandle, 0, key, 0) == 0 {
140+
if stdcall(_PostQueuedCompletionStatus, iocphandle, 0, key, 0) == 0 {
141141
println("runtime: netpoll: PostQueuedCompletionStatus failed (errno=", getlasterror(), ")")
142142
throw("runtime: netpoll: PostQueuedCompletionStatus failed")
143143
}
@@ -197,7 +197,7 @@ func netpoll(delay int64) (gList, int32) {
197197
if delay != 0 {
198198
mp.blocked = true
199199
}
200-
if stdcall6(_GetQueuedCompletionStatusEx, iocphandle, uintptr(unsafe.Pointer(&entries[0])), uintptr(n), uintptr(unsafe.Pointer(&n)), uintptr(wait), 0) == 0 {
200+
if stdcall(_GetQueuedCompletionStatusEx, iocphandle, uintptr(unsafe.Pointer(&entries[0])), uintptr(n), uintptr(unsafe.Pointer(&n)), uintptr(wait), 0) == 0 {
201201
mp.blocked = false
202202
errno := getlasterror()
203203
if errno == _WAIT_TIMEOUT {
@@ -256,20 +256,20 @@ func netpollQueueTimer(delay int64) (signaled bool) {
256256
// such as a netpollBreak, so we can get to this point with a timer that hasn't
257257
// expired yet. In this case, the completion packet can still be picked up by
258258
// another thread, so defer the cancellation until it is really necessary.
259-
errno := stdcall2(_NtCancelWaitCompletionPacket, mp.waitIocpHandle, 1)
259+
errno := stdcall(_NtCancelWaitCompletionPacket, mp.waitIocpHandle, 1)
260260
switch errno {
261261
case STATUS_CANCELLED:
262262
// STATUS_CANCELLED is returned when the associated timer has already expired,
263263
// in which automatically cancels the wait completion packet.
264264
fallthrough
265265
case STATUS_SUCCESS:
266266
dt := -delay / 100 // relative sleep (negative), 100ns units
267-
if stdcall6(_SetWaitableTimer, mp.waitIocpTimer, uintptr(unsafe.Pointer(&dt)), 0, 0, 0, 0) == 0 {
267+
if stdcall(_SetWaitableTimer, mp.waitIocpTimer, uintptr(unsafe.Pointer(&dt)), 0, 0, 0, 0) == 0 {
268268
println("runtime: SetWaitableTimer failed; errno=", getlasterror())
269269
throw("runtime: netpoll failed")
270270
}
271271
key := packNetpollKey(netpollSourceTimer, nil)
272-
if errno := stdcall8(_NtAssociateWaitCompletionPacket, mp.waitIocpHandle, iocphandle, mp.waitIocpTimer, key, 0, 0, 0, uintptr(unsafe.Pointer(&signaled))); errno != 0 {
272+
if errno := stdcall(_NtAssociateWaitCompletionPacket, mp.waitIocpHandle, iocphandle, mp.waitIocpTimer, key, 0, 0, 0, uintptr(unsafe.Pointer(&signaled))); errno != 0 {
273273
println("runtime: NtAssociateWaitCompletionPacket failed; errno=", errno)
274274
throw("runtime: netpoll failed")
275275
}

0 commit comments

Comments
 (0)