Skip to content

Commit 656afa9

Browse files
authored
runtime: improve free_memory implementation for OpenBSD, by getting the stats from its UVM system (#24431)
1 parent bbee42f commit 656afa9

File tree

1 file changed

+13
-3
lines changed

1 file changed

+13
-3
lines changed

vlib/runtime/free_memory_impl_openbsd.c.v

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,24 @@
11
module runtime
22

3+
#include <sys/sysctl.h>
4+
#include <uvm/uvmexp.h>
5+
6+
struct C.uvmexp {
7+
pagesize int
8+
free int
9+
}
10+
311
fn free_memory_impl() usize {
412
$if cross ? {
513
return 1
614
}
715
$if !cross ? {
816
$if openbsd {
9-
page_size := usize(C.sysconf(C._SC_PAGESIZE))
10-
av_phys_pages := usize(C.sysconf(C._SC_AVPHYS_PAGES))
11-
return page_size * av_phys_pages
17+
mib := [C.CTL_VM, C.VM_UVMEXP]!
18+
mut uvm := C.uvmexp{0, 0}
19+
mut len := sizeof(C.uvmexp)
20+
unsafe { C.sysctl(&mib[0], mib.len, &uvm, &len, C.NULL, 0) }
21+
return usize(uvm.pagesize * uvm.free)
1222
}
1323
}
1424
return 1

0 commit comments

Comments
 (0)