Struct vmap::Size

source ·
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

source

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.

source

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.

source

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
source

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
source

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
source

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);
source

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
source

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);
source

pub unsafe fn bounds(&self, ptr: *mut u8, len: usize) -> (*mut u8, usize)

Calculates the unit bounds for a pointer and length.

§Safety

There is no verification that the pointer is a mapped page nor that the calculated offset may be dereferenced.

Trait Implementations§

source§

impl Clone for Size

source§

fn clone(&self) -> Size

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Default for Size

source§

fn default() -> Self

Returns the “default value” for a type. Read more
source§

impl Copy for Size

Auto Trait Implementations§

§

impl Freeze for Size

§

impl RefUnwindSafe for Size

§

impl Send for Size

§

impl Sync for Size

§

impl Unpin for Size

§

impl UnwindSafe for Size

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.