1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

runtime: add new functions total_memory/0 and free_memory/0 (#18499)

This commit is contained in:
kbkpbot
2023-06-24 19:15:15 +08:00
committed by GitHub
parent 2abd2e2c2a
commit 5f0ad64155
7 changed files with 97 additions and 0 deletions

View File

@@ -7,7 +7,14 @@ struct C.SYSTEM_INFO {
dwNumberOfProcessors u32
}
[typedef]
struct C.MEMORYSTATUS {
dwTotalPhys usize
dwAvailPhys usize
}
fn C.GetSystemInfo(&C.SYSTEM_INFO)
fn C.GlobalMemoryStatus(&C.MEMORYSTATUS)
// nr_cpus returns the number of virtual CPU cores found on the system.
pub fn nr_cpus() int {
@@ -19,3 +26,17 @@ pub fn nr_cpus() int {
}
return nr
}
// total_memory returns total physical memory found on the system.
pub fn total_memory() usize {
memory_status := C.MEMORYSTATUS{}
C.GlobalMemoryStatus(&memory_status)
return memory_status.dwTotalPhys
}
// free_memory returns free physical memory found on the system.
pub fn free_memory() usize {
memory_status := C.MEMORYSTATUS{}
C.GlobalMemoryStatus(&memory_status)
return memory_status.dwAvailPhys
}