From a118d5659badf126129b59b002aaef71693e02f5 Mon Sep 17 00:00:00 2001 From: Wildan M Date: Thu, 9 Oct 2025 13:15:05 +0700 Subject: [PATCH] Implement reading memory --- src/header/unistd/sysconf/redox.rs | 18 +++++++++++++++--- tests/unistd/sysconf.c | 2 ++ 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/src/header/unistd/sysconf/redox.rs b/src/header/unistd/sysconf/redox.rs index bc5f82e14d..c959bd35e3 100644 --- a/src/header/unistd/sysconf/redox.rs +++ b/src/header/unistd/sysconf/redox.rs @@ -5,7 +5,7 @@ use alloc::string::String; use crate::{ error::Errno, fs::File, - header::{errno, fcntl, limits}, + header::{errno, fcntl, limits, sys_statvfs}, io::Read, platform::{self, Pal, Sys, types::*}, }; @@ -65,8 +65,8 @@ pub(super) fn sysconf_impl(name: c_int) -> c_long { _SC_HOST_NAME_MAX => 64, _SC_NPROCESSORS_CONF => get_cpu_count().unwrap_or(None).unwrap_or(1), _SC_NPROCESSORS_ONLN => get_cpu_count().unwrap_or(None).unwrap_or(1), - _SC_PHYS_PAGES => 262144, - _SC_AVPHYS_PAGES => -1, + _SC_PHYS_PAGES => get_mem_stat().map(|s| s.f_bsize).unwrap_or(-1), + _SC_AVPHYS_PAGES => get_mem_stat().map(|s| s.f_bfree).unwrap_or(-1), _SC_SIGQUEUE_MAX => 32, _SC_REALTIME_SIGNALS => 202405, _ => { @@ -88,3 +88,15 @@ pub fn get_cpu_count() -> Result, Errno> { .and_then(|line| line.split(':').nth(1)) .and_then(|num_str| num_str.trim().parse::().ok())) } + +pub fn get_mem_stat() -> Result { + let fd = Sys::open(c"/scheme/memory".into(), fcntl::O_PATH, 0)?; + if fd < 0 { + return -1; + } + let buf = sys_statvfs::statvfs::default(); + let res = Sys::fstatvfs(fd, buf).map(|()| 0); + Sys::close(fd); + let res = res?; + return Ok(buf); +} diff --git a/tests/unistd/sysconf.c b/tests/unistd/sysconf.c index db369eee97..05b1ed1b66 100644 --- a/tests/unistd/sysconf.c +++ b/tests/unistd/sysconf.c @@ -27,4 +27,6 @@ int main(void) { SC(HOST_NAME_MAX); SC(NPROCESSORS_CONF); SC(NPROCESSORS_ONLN); + SC(PHYS_PAGES); + SC(AVPHYS_PAGES); }