From e6c272954eb81acec3e8f7b50992da9f1a7fe6af Mon Sep 17 00:00:00 2001 From: kbkpbot Date: Mon, 27 Jan 2025 07:49:56 +0800 Subject: [PATCH 1/3] runtime: add freebsd free_memory() support --- vlib/runtime/free_memory_impl_freebsd.c.v | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 vlib/runtime/free_memory_impl_freebsd.c.v diff --git a/vlib/runtime/free_memory_impl_freebsd.c.v b/vlib/runtime/free_memory_impl_freebsd.c.v new file mode 100644 index 00000000000000..1e48ac1f752bf7 --- /dev/null +++ b/vlib/runtime/free_memory_impl_freebsd.c.v @@ -0,0 +1,21 @@ +module runtime + +fn free_memory_impl() usize { + $if cross ? { + return 1 + } + $if !cross ? { + $if freebsd { + page_size := usize(C.sysconf(C._SC_PAGESIZE)) + // sysctlnametomib("vm.stats.vm.v_free_count") => mib : + mib := [C.CTL_VM, 2147481600, 2147481598, 2147481557]! + mut free_pages := int(0) + bufsize := usize(4) + unsafe { + C.sysctl(&mib[0], mib.len, &free_pages, &bufsize, 0, 0) + } + return page_size * usize(free_pages) + } + } + return 1 +} From 5db0123fc173e781e6393f19f15cfb7ba00604b7 Mon Sep 17 00:00:00 2001 From: kbkpbot Date: Wed, 29 Jan 2025 20:21:43 +0800 Subject: [PATCH 2/3] use C.sysctlnametomib --- vlib/builtin/cfns.c.v | 1 + vlib/runtime/free_memory_impl_freebsd.c.v | 9 ++++----- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/vlib/builtin/cfns.c.v b/vlib/builtin/cfns.c.v index b09ad1a1c4f291..fe3c4e5a2cf7a3 100644 --- a/vlib/builtin/cfns.c.v +++ b/vlib/builtin/cfns.c.v @@ -232,6 +232,7 @@ fn C.isatty(fd int) int fn C.syscall(number int, va ...voidptr) int fn C.sysctl(name &int, namelen u32, oldp voidptr, oldlenp voidptr, newp voidptr, newlen usize) int +fn C.sysctlnametomib(name charptr, mib &int, len &usize) int @[trusted] fn C._fileno(int) int diff --git a/vlib/runtime/free_memory_impl_freebsd.c.v b/vlib/runtime/free_memory_impl_freebsd.c.v index 1e48ac1f752bf7..4eda7154648c44 100644 --- a/vlib/runtime/free_memory_impl_freebsd.c.v +++ b/vlib/runtime/free_memory_impl_freebsd.c.v @@ -7,13 +7,12 @@ fn free_memory_impl() usize { $if !cross ? { $if freebsd { page_size := usize(C.sysconf(C._SC_PAGESIZE)) - // sysctlnametomib("vm.stats.vm.v_free_count") => mib : - mib := [C.CTL_VM, 2147481600, 2147481598, 2147481557]! + mut mib := [4]int{} + mut len := usize(4) + unsafe { C.sysctlnametomib(c'vm.stats.vm.v_free_count', &mib[0], &len) } mut free_pages := int(0) bufsize := usize(4) - unsafe { - C.sysctl(&mib[0], mib.len, &free_pages, &bufsize, 0, 0) - } + unsafe { C.sysctl(&mib[0], mib.len, &free_pages, &bufsize, 0, 0) } return page_size * usize(free_pages) } } From de6e7c245084957b86a24191fb8a3c1294d56bd4 Mon Sep 17 00:00:00 2001 From: kbkpbot Date: Thu, 30 Jan 2025 03:20:01 +0800 Subject: [PATCH 3/3] fix --- vlib/builtin/cfns.c.v | 1 - vlib/runtime/free_memory_impl_freebsd.c.v | 2 ++ 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/vlib/builtin/cfns.c.v b/vlib/builtin/cfns.c.v index fe3c4e5a2cf7a3..b09ad1a1c4f291 100644 --- a/vlib/builtin/cfns.c.v +++ b/vlib/builtin/cfns.c.v @@ -232,7 +232,6 @@ fn C.isatty(fd int) int fn C.syscall(number int, va ...voidptr) int fn C.sysctl(name &int, namelen u32, oldp voidptr, oldlenp voidptr, newp voidptr, newlen usize) int -fn C.sysctlnametomib(name charptr, mib &int, len &usize) int @[trusted] fn C._fileno(int) int diff --git a/vlib/runtime/free_memory_impl_freebsd.c.v b/vlib/runtime/free_memory_impl_freebsd.c.v index 4eda7154648c44..0286e87918b9dc 100644 --- a/vlib/runtime/free_memory_impl_freebsd.c.v +++ b/vlib/runtime/free_memory_impl_freebsd.c.v @@ -1,5 +1,7 @@ module runtime +fn C.sysctlnametomib(name charptr, mib &int, len &usize) int + fn free_memory_impl() usize { $if cross ? { return 1