Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion src/sys_limits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,18 @@ fn get_rlimit_as() -> io::Result<libc::rlimit> {

#[cfg(any(target_os = "linux", target_os = "macos"))]
pub fn get_available_memory() -> io::Result<usize> {
let pages = unsafe { libc::sysconf(libc::_SC_PHYS_PAGES) };
let mut pages = unsafe { libc::sysconf(libc::_SC_PHYS_PAGES) };
if pages == -1 {
return Err(io::Error::last_os_error());
}

//Fixed:32-bit os,max memory limit upper to 4G,to avoid overflow.
if usize::BITS==32 {
if pages > 1023 * 1024 {
pages = 1023 * 1024;
}
}

let page_size = unsafe { libc::sysconf(libc::_SC_PAGE_SIZE) };
if page_size == -1 {
return Err(io::Error::last_os_error());
Expand Down