Skip to content

Commit 1869826

Browse files
authored
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
1 parent 0bcd706 commit 1869826

File tree

3 files changed

+23
-35
lines changed

3 files changed

+23
-35
lines changed

lib/pure/concurrency/cpuinfo.nim

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ include "system/inclrtl"
1818
when defined(posix) and not (defined(macosx) or defined(bsd)):
1919
import posix
2020

21+
when defined(windows):
22+
import std/private/win_getsysteminfo
23+
2124
when defined(freebsd) or defined(macosx):
2225
{.emit: "#include <sys/types.h>".}
2326

@@ -54,25 +57,10 @@ proc countProcessors*(): int {.rtl, extern: "ncpi$1".} =
5457
## Returns the number of the processors/cores the machine has.
5558
## Returns 0 if it cannot be detected.
5659
when defined(windows):
57-
type
58-
SYSTEM_INFO {.final, pure.} = object
59-
u1: int32
60-
dwPageSize: int32
61-
lpMinimumApplicationAddress: pointer
62-
lpMaximumApplicationAddress: pointer
63-
dwActiveProcessorMask: ptr int32
64-
dwNumberOfProcessors: int32
65-
dwProcessorType: int32
66-
dwAllocationGranularity: int32
67-
wProcessorLevel: int16
68-
wProcessorRevision: int16
69-
70-
proc GetSystemInfo(lpSystemInfo: var SYSTEM_INFO) {.stdcall, dynlib: "kernel32", importc: "GetSystemInfo".}
71-
7260
var
73-
si: SYSTEM_INFO
74-
GetSystemInfo(si)
75-
result = si.dwNumberOfProcessors
61+
si: SystemInfo
62+
getSystemInfo(addr si)
63+
result = int(si.dwNumberOfProcessors)
7664
elif defined(macosx) or defined(bsd):
7765
var
7866
mib: array[0..3, cint]

lib/pure/reservedmem.nim

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -42,25 +42,10 @@ type
4242

4343
when defined(windows):
4444
import winlean
45-
46-
type
47-
SYSTEM_INFO {.final, pure.} = object
48-
u1: uint32
49-
dwPageSize: uint32
50-
lpMinimumApplicationAddress: pointer
51-
lpMaximumApplicationAddress: pointer
52-
dwActiveProcessorMask: ptr uint32
53-
dwNumberOfProcessors: uint32
54-
dwProcessorType: uint32
55-
dwAllocationGranularity: uint32
56-
wProcessorLevel: uint16
57-
wProcessorRevision: uint16
58-
59-
proc getSystemInfo(lpSystemInfo: ptr SYSTEM_INFO) {.stdcall,
60-
dynlib: "kernel32", importc: "GetSystemInfo".}
45+
import std/private/win_getsysteminfo
6146

6247
proc getAllocationGranularity: uint =
63-
var sysInfo: SYSTEM_INFO
48+
var sysInfo: SystemInfo
6449
getSystemInfo(addr sysInfo)
6550
return uint(sysInfo.dwAllocationGranularity)
6651

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
type
2+
SystemInfo* = object
3+
u1: uint32
4+
dwPageSize: uint32
5+
lpMinimumApplicationAddress: pointer
6+
lpMaximumApplicationAddress: pointer
7+
dwActiveProcessorMask: ptr uint32
8+
dwNumberOfProcessors*: uint32
9+
dwProcessorType: uint32
10+
dwAllocationGranularity*: uint32
11+
wProcessorLevel: uint16
12+
wProcessorRevision: uint16
13+
14+
proc getSystemInfo*(lpSystemInfo: ptr SystemInfo) {.stdcall,
15+
dynlib: "kernel32", importc: "GetSystemInfo".}

0 commit comments

Comments
 (0)