add std/private/win_getsysteminfo; refactor the usage of GetSystemInfo (#19310)

* add std/private/win_getsysteminfo

* import at the top level

* wrappers follow nep1 too

* follow review comment
This commit is contained in:
flywind 2022-01-04 18:49:54 +08:00 • committed by GitHub
commit 1869826668
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 23 additions and 35 deletions

View file

@ -18,6 +18,9 @@ include "system/inclrtl"
when defined(posix) and not (defined(macosx) or defined(bsd)): when defined(posix) and not (defined(macosx) or defined(bsd)):
import posix import posix
when defined(windows):
import std/private/win_getsysteminfo
when defined(freebsd) or defined(macosx): when defined(freebsd) or defined(macosx):
{.emit: "#include <sys/types.h>".} {.emit: "#include <sys/types.h>".}
@ -54,25 +57,10 @@ proc countProcessors*(): int {.rtl, extern: "ncpi$1".} =
## Returns the number of the processors/cores the machine has. ## Returns the number of the processors/cores the machine has.
## Returns 0 if it cannot be detected. ## Returns 0 if it cannot be detected.
when defined(windows): when defined(windows):
type
SYSTEM_INFO {.final, pure.} = object
u1: int32
dwPageSize: int32
lpMinimumApplicationAddress: pointer
lpMaximumApplicationAddress: pointer
dwActiveProcessorMask: ptr int32
dwNumberOfProcessors: int32
dwProcessorType: int32
dwAllocationGranularity: int32
wProcessorLevel: int16
wProcessorRevision: int16
proc GetSystemInfo(lpSystemInfo: var SYSTEM_INFO) {.stdcall, dynlib: "kernel32", importc: "GetSystemInfo".}
var var
si: SYSTEM_INFO si: SystemInfo
GetSystemInfo(si) getSystemInfo(addr si)
result = si.dwNumberOfProcessors result = int(si.dwNumberOfProcessors)
elif defined(macosx) or defined(bsd): elif defined(macosx) or defined(bsd):
var var
mib: array[0..3, cint] mib: array[0..3, cint]

View file

@ -42,25 +42,10 @@ type
when defined(windows): when defined(windows):
import winlean import winlean
import std/private/win_getsysteminfo
type
SYSTEM_INFO {.final, pure.} = object
u1: uint32
dwPageSize: uint32
lpMinimumApplicationAddress: pointer
lpMaximumApplicationAddress: pointer
dwActiveProcessorMask: ptr uint32
dwNumberOfProcessors: uint32
dwProcessorType: uint32
dwAllocationGranularity: uint32
wProcessorLevel: uint16
wProcessorRevision: uint16
proc getSystemInfo(lpSystemInfo: ptr SYSTEM_INFO) {.stdcall,
dynlib: "kernel32", importc: "GetSystemInfo".}
proc getAllocationGranularity: uint = proc getAllocationGranularity: uint =
var sysInfo: SYSTEM_INFO var sysInfo: SystemInfo
getSystemInfo(addr sysInfo) getSystemInfo(addr sysInfo)
return uint(sysInfo.dwAllocationGranularity) return uint(sysInfo.dwAllocationGranularity)

View file

@ -0,0 +1,15 @@
type
SystemInfo* = object
u1: uint32
dwPageSize: uint32
lpMinimumApplicationAddress: pointer
lpMaximumApplicationAddress: pointer
dwActiveProcessorMask: ptr uint32
dwNumberOfProcessors*: uint32
dwProcessorType: uint32
dwAllocationGranularity*: uint32
wProcessorLevel: uint16
wProcessorRevision: uint16
proc getSystemInfo*(lpSystemInfo: ptr SystemInfo) {.stdcall,
dynlib: "kernel32", importc: "GetSystemInfo".}