-
Notifications
You must be signed in to change notification settings - Fork 8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add memory page size alignment for madvise, mlock and munlock #19
base: master
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,20 +2,50 @@ | |
|
||
#![cfg(feature = "use_os")] | ||
|
||
use libc::_SC_PAGESIZE; | ||
|
||
/// Retrieve page size of the system | ||
/// | ||
/// Used for alignment | ||
unsafe fn page_size() -> usize { | ||
use libc::sysconf; | ||
sysconf(_SC_PAGESIZE) as usize | ||
} | ||
|
||
/// Cross-platform `mlock`. | ||
/// | ||
/// * Unix `mlock`. | ||
/// * Windows `VirtualLock`. | ||
pub unsafe fn mlock(addr: *mut u8, len: usize) -> bool { | ||
#[cfg(unix)] | ||
{ | ||
let page_size = page_size(); | ||
|
||
// start address of the page obtained from masked the value of the page size | ||
// with the memory address | ||
// | ||
let start_addr = (addr as usize) & !(page_size - 1); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it should be put into a separate function and some tests added. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've refactored it out to a separate function, and added a few tests to check whether it returns the right start and end page offsets |
||
|
||
// End address of the page | ||
let end_addr = ((addr as usize) + len + page_size - 1) & !(page_size - 1); | ||
|
||
let aligned_len = end_addr - start_addr; | ||
|
||
#[cfg(target_os = "linux")] | ||
libc::madvise(addr as *mut libc::c_void, len, libc::MADV_DONTDUMP); | ||
libc::madvise( | ||
start_addr as *mut libc::c_void, | ||
aligned_len, | ||
libc::MADV_DONTDUMP, | ||
); | ||
|
||
#[cfg(any(target_os = "freebsd", target_os = "dragonfly"))] | ||
libc::madvise(addr as *mut libc::c_void, len, libc::MADV_NOCORE); | ||
libc::madvise( | ||
start_addr as *mut libc::c_void, | ||
aligned_len, | ||
libc::MADV_NOCORE, | ||
); | ||
|
||
libc::mlock(addr as *mut libc::c_void, len) == 0 | ||
libc::mlock(start_addr as *mut libc::c_void, aligned_len) == 0 | ||
} | ||
|
||
#[cfg(windows)] | ||
|
@@ -33,13 +63,32 @@ pub unsafe fn munlock(addr: *mut u8, len: usize) -> bool { | |
|
||
#[cfg(unix)] | ||
{ | ||
let page_size = page_size(); | ||
|
||
// start address of the page obtained from masked the value of the page size | ||
// with the memory address | ||
// | ||
let start_addr = (addr as usize) & !(page_size - 1); | ||
|
||
// End address of the page | ||
let end_addr = ((addr as usize) + len + page_size - 1) & !(page_size - 1); | ||
|
||
let aligned_len = end_addr - start_addr; | ||
#[cfg(target_os = "linux")] | ||
libc::madvise(addr as *mut libc::c_void, len, libc::MADV_DODUMP); | ||
libc::madvise( | ||
start_addr as *mut libc::c_void, | ||
aligned_len, | ||
libc::MADV_DODUMP, | ||
); | ||
|
||
#[cfg(any(target_os = "freebsd", target_os = "dragonfly"))] | ||
libc::madvise(addr as *mut libc::c_void, len, libc::MADV_CORE); | ||
libc::madvise( | ||
start_addr as *mut libc::c_void, | ||
aligned_len, | ||
libc::MADV_CORE, | ||
); | ||
|
||
libc::munlock(addr as *mut libc::c_void, len) == 0 | ||
libc::munlock(start_addr as *mut libc::c_void, aligned_len) == 0 | ||
} | ||
|
||
#[cfg(windows)] | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can use the page size cache in alloc mod.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You're right, shouldn't be doing syscalls everytime we mlock/munlock. I've initialized the
alloc_init()
function and returned thePAGE_SIZE
from thealloc
module instead