Share some more code in runtime-core::sys

This commit is contained in:
Mark McCaskey 2020-01-22 13:34:50 -08:00
parent 5dfa61e30a
commit 2c45106ae0
3 changed files with 14 additions and 22 deletions

View File

@ -80,3 +80,15 @@ impl<'de> Deserialize<'de> for Memory {
deserializer.deserialize_struct("Memory", &["protection", "data"], MemoryVisitor)
}
}
/// Round `size` up to the nearest multiple of `page_size`.
fn round_up_to_page_size(size: usize, page_size: usize) -> usize {
assert!(page_size.is_power_of_two());
(size + (page_size - 1)) & !(page_size - 1)
}
/// Round `size` down to the nearest multiple of `page_size`.
fn round_down_to_page_size(size: usize, page_size: usize) -> usize {
assert!(page_size.is_power_of_two());
size & !(page_size - 1)
}

View File

@ -1,5 +1,6 @@
use crate::error::MemoryCreationError;
use crate::error::MemoryProtectionError;
use crate::sys::{round_down_to_page_size, round_up_to_page_size};
use errno;
use nix::libc;
use page_size;
@ -313,15 +314,3 @@ impl Drop for RawFd {
);
}
}
/// Round `size` up to the nearest multiple of `page_size`.
fn round_up_to_page_size(size: usize, page_size: usize) -> usize {
assert!(page_size.is_power_of_two());
(size + (page_size - 1)) & !(page_size - 1)
}
/// Round `size` down to the nearest multiple of `page_size`.
fn round_down_to_page_size(size: usize, page_size: usize) -> usize {
assert!(page_size.is_power_of_two());
size & !(page_size - 1)
}

View File

@ -1,5 +1,6 @@
use crate::error::MemoryCreationError;
use crate::error::MemoryProtectionError;
use crate::sys::{round_down_to_page_size, round_up_to_page_size};
use page_size;
use std::ops::{Bound, RangeBounds};
use std::{ptr, slice};
@ -243,16 +244,6 @@ impl Protect {
}
}
/// Round `size` up to the nearest multiple of `page_size`.
fn round_up_to_page_size(size: usize, page_size: usize) -> usize {
(size + (page_size - 1)) & !(page_size - 1)
}
/// Round `size` down to the nearest multiple of `page_size`.
fn round_down_to_page_size(size: usize, page_size: usize) -> usize {
size & !(page_size - 1)
}
#[cfg(test)]
mod tests {
use super::*;