1 use crate::{ 2 include::bindings::bindings::mm_stat_t, 3 syscall::{Syscall, SystemError}, 4 }; 5 6 extern "C" { 7 fn sys_do_brk(new_addr: usize) -> usize; 8 fn sys_do_sbrk(incr: isize) -> usize; 9 fn sys_do_mstat(dst: *mut mm_stat_t, from_user: bool) -> usize; 10 } 11 impl Syscall { 12 pub fn brk(new_addr: usize) -> Result<usize, SystemError> { 13 let ret = unsafe { sys_do_brk(new_addr) }; 14 if (ret as isize) < 0 { 15 return Err( 16 SystemError::from_posix_errno(-(ret as isize) as i32).expect("brk: Invalid errno") 17 ); 18 } 19 return Ok(ret); 20 } 21 22 pub fn sbrk(incr: isize) -> Result<usize, SystemError> { 23 let ret = unsafe { sys_do_sbrk(incr) }; 24 if (ret as isize) < 0 { 25 return Err( 26 SystemError::from_posix_errno(-(ret as isize) as i32).expect("sbrk: Invalid errno") 27 ); 28 } 29 return Ok(ret); 30 } 31 32 /// 获取内存统计信息 33 /// 34 /// TODO: 该函数不是符合POSIX标准的,在将来需要删除! 35 pub fn mstat(dst: *mut mm_stat_t, from_user: bool) -> Result<usize, SystemError> { 36 let ret = unsafe { sys_do_mstat(dst, from_user) }; 37 if (ret as isize) < 0 { 38 return Err(SystemError::from_posix_errno(-(ret as isize) as i32) 39 .expect("mstat: Invalid errno")); 40 } 41 return Ok(ret); 42 } 43 } 44