pub struct Size(/* private fields */);
Expand description
Type for calculation system page or allocation size information.
§Examples
let size = vmap::Size::alloc();
let pages = size.count(200);
assert_eq!(pages, 1);
let round = size.round(200);
println!("200 bytes requires a {} byte mapping", round);
let count = size.count(10000);
println!("10000 bytes requires {} pages", count);
let size = size.size(3);
println!("3 pages are {} bytes", size);
Implementations§
source§impl Size
impl Size
sourcepub fn page() -> Self
pub fn page() -> Self
Creates a type for calculating page numbers and byte offsets.
The size is determined from the system’s configurated page size. This value is cached making it very cheap to construct.
sourcepub fn alloc() -> Self
pub fn alloc() -> Self
Creates a type for calculating allocation numbers and byte offsets.
The size is determined from the system’s configurated allocation granularity. This value is cached making it very cheap to construct.
sourcepub unsafe fn with_size(size: usize) -> Self
pub unsafe fn with_size(size: usize) -> Self
Creates a type for calculating allocations numbers and byte offsets using a known size.
§Safety
The size must be a power-of-2. To successfully map pages, the size must also be a mutliple of the actual system allocation granularity. Hypothetically this could be used to simulate larger page sizes, but this has no bearing on the TLB cache.
§Examples
use vmap::Size;
let sys = vmap::allocation_size();
let size = unsafe { Size::with_size(sys << 2) };
assert_eq!(size.round(1), sys << 2); // probably 16384
sourcepub const fn round(&self, len: usize) -> usize
pub const fn round(&self, len: usize) -> usize
Round a byte size up to the nearest unit size.
§Examples
use vmap::Size;
let sys = vmap::page_size();
let size = Size::page();
assert_eq!(size.round(0), 0);
assert_eq!(size.round(1), sys); // probably 4096
assert_eq!(size.round(sys-1), sys); // probably 4096
assert_eq!(size.round(sys), sys); // probably 4096
assert_eq!(size.round(sys+1), sys*2); // probably 8192
sourcepub const fn truncate(&self, len: usize) -> usize
pub const fn truncate(&self, len: usize) -> usize
Round a byte size down to the nearest unit size.
§Examples
use vmap::Size;
let sys = vmap::page_size();
let size = Size::page();
assert_eq!(size.truncate(0), 0);
assert_eq!(size.truncate(1), 0);
assert_eq!(size.truncate(sys-1), 0);
assert_eq!(size.truncate(sys), sys); // probably 4096
assert_eq!(size.truncate(sys+1), sys); // probably 4096
sourcepub const fn offset(&self, len: usize) -> usize
pub const fn offset(&self, len: usize) -> usize
Calculate the byte offset from size unit containing the position.
§Examples
use vmap::Size;
let sys = vmap::page_size();
let size = Size::page();
assert_eq!(size.offset(1), 1);
assert_eq!(size.offset(sys-1), sys-1);
assert_eq!(size.offset(sys*2 + 123), 123);
sourcepub const fn size(&self, count: u32) -> usize
pub const fn size(&self, count: u32) -> usize
Convert a unit count into a byte size.
§Examples
use vmap::Size;
let sys = vmap::page_size();
let size = Size::page();
assert_eq!(size.size(0), 0);
assert_eq!(size.size(1), sys); // probably 4096
assert_eq!(size.size(2), sys*2); // probably 8192
sourcepub const fn count(&self, len: usize) -> u32
pub const fn count(&self, len: usize) -> u32
Covert a byte size into the number of units necessary to contain it.
§Examples
use vmap::Size;
let sys = vmap::page_size();
let size = Size::page();
assert_eq!(size.count(0), 0);
assert_eq!(size.count(1), 1);
assert_eq!(size.count(sys-1), 1);
assert_eq!(size.count(sys), 1);
assert_eq!(size.count(sys+1), 2);
assert_eq!(size.count(sys*2), 2);