Skip to main content

VirtualAddress

Struct VirtualAddress 

Source
#[repr(transparent)]
pub struct VirtualAddress { pub addr: usize, }
Expand description

A virtual memory address.

Fields§

§addr: usize

The underlying address

Implementations§

Source§

impl VirtualAddress

Source

pub const fn new(addr: usize) -> Self

Creates a new VirtualAddress with the given address.

§Examples
use kernel_api::memory::VirtualAddress;

let addr = VirtualAddress::new(0);
Source

pub const fn as_ptr(self) -> *mut u8

Converts a VirtualAddress into a raw pointer.

§Examples
use kernel_api::memory::VirtualAddress;

let addr = VirtualAddress::new(0x1000);
let ptr = addr.as_ptr();
// if `addr` came from allocated memory, it can be accessed through `ptr`
// unsafe { *ptr = 5 };
Source

pub const fn is_higher_half(self) -> bool

Returns true if the address is in the upper half of the address space, i.e. kernelspace.

§Examples
use kernel_api::memory::VirtualAddress;

let addr = VirtualAddress::new(usize::MAX);
assert_eq!(addr.is_higher_half(), true);
Source

pub const fn align_down_to_page(self) -> RawPage

Returns the closest RawPage at or below the current address.

§Examples
use kernel_api::memory::{VirtualAddress, PAGE_SIZE};

let address = VirtualAddress::new(0x1);
let page = address.align_down_to_page();
assert!(address >= *page);
assert!((*page).is_aligned_to(PAGE_SIZE));

// If the address is already aligned, then the value will remain unchanged.
let address = VirtualAddress::new(PAGE_SIZE);
let page = address.align_down_to_page();
assert!(address == *page);
Source

pub const fn align_up_to_page(self) -> RawPage

Returns the closest RawPage at or above the current address.

§Examples
use kernel_api::memory::{VirtualAddress, PAGE_SIZE};

let address = VirtualAddress::new(0x1);
let page = address.align_up_to_page();
assert!(address <= *page);
assert!((*page).is_aligned_to(PAGE_SIZE));

// If the address is already aligned, then the value will remain unchanged.
let address = VirtualAddress::new(PAGE_SIZE);
let page = address.align_up_to_page();
assert!(address == *page);
Source

pub const fn is_aligned_to(self, align: usize) -> bool

Returns true if the address is aligned to align.

§Panics

If align is not a power of two.

§Examples
use kernel_api::memory::VirtualAddress;

let addr = VirtualAddress::new(64);
assert_eq!(addr.is_aligned_to(32), true);
assert_eq!(addr.is_aligned_to(128), false);
Source

pub const fn saturating_add(self, rhs: usize) -> Self

Computes self + rhs, saturating when the address reaches [usize::MAX].

§Examples
use kernel_api::memory::VirtualAddress;

let a = VirtualAddress::new(0x1000);
assert_eq!(a.saturating_add(1), Some(VirtualAddress::new(0x1001)));
assert_eq!(a.saturating_add(usize::MAX), None);
Source

pub const fn saturating_sub(self, rhs: usize) -> Self

Computes self - rhs saturating when the address reaches 0.

§Examples
use kernel_api::memory::VirtualAddress;

let a = VirtualAddress::new(0x1001);
assert_eq!(a.saturating_sub(1), Some(VirtualAddress::new(0x1000)));
assert_eq!(a.saturating_sub(usize::MAX), None);
Source

pub const fn to_physical(self) -> PhysicalAddress

Converts an address in the physical page map region into an PhysicalAddress.

This is the virtual-to-physical counterpart to to_virtual().

§Panics

Panics if the address is not in the physical page map region on a best effort basis.

§Examples
ⓘ
use kernel_api::memory::VirtualAddress;

let virt = VirtualAddress::new(0);

// converting non physical page map addresses may panic
let phys = virt.to_physical();

Trait Implementations§

Source§

impl Add<isize> for VirtualAddress

Source§

type Output = VirtualAddress

The resulting type after applying the + operator.
Source§

fn add(self, rhs: isize) -> Self::Output

Performs the + operation. Read more
Source§

impl Add<usize> for VirtualAddress

Source§

type Output = VirtualAddress

The resulting type after applying the + operator.
Source§

fn add(self, rhs: usize) -> Self::Output

Performs the + operation. Read more
Source§

impl Clone for VirtualAddress

Source§

fn clone(&self) -> VirtualAddress

Returns a duplicate of the value. Read more
1.0.0 (const: unstable)§

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

Performs copy-assignment from source. Read more
Source§

impl Copy for VirtualAddress

Source§

impl Debug for VirtualAddress

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Eq for VirtualAddress

Source§

impl<T: ?Sized> From<*const T> for VirtualAddress

Source§

fn from(value: *const T) -> Self

Converts to this type from the input type.
Source§

impl<T: ?Sized> From<*mut T> for VirtualAddress

Source§

fn from(value: *mut T) -> Self

Converts to this type from the input type.
Source§

impl From<RawPage> for VirtualAddress

Source§

fn from(value: RawPage) -> Self

Converts to this type from the input type.
Source§

impl Hash for VirtualAddress

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given [Hasher]. Read more
1.3.0§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given [Hasher]. Read more
Source§

impl LowerHex for VirtualAddress

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Ord for VirtualAddress

Source§

fn cmp(&self, other: &VirtualAddress) -> Ordering

This method returns an [Ordering] between self and other. Read more
1.21.0 (const: unstable)§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 (const: unstable)§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 (const: unstable)§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
§

fn clamp_to<R>(self, range: R) -> Self
where Self: Sized, R: ClampBounds<Self>,

🔬This is a nightly-only experimental API. (clamp_to)
Restrict a value to a certain range. Read more
Source§

impl PartialEq for VirtualAddress

Source§

fn eq(&self, other: &VirtualAddress) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable)§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl PartialOrd for VirtualAddress

Source§

fn partial_cmp(&self, other: &VirtualAddress) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 (const: unstable)§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 (const: unstable)§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 (const: unstable)§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 (const: unstable)§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl Step for VirtualAddress

Source§

fn steps_between(start: &Self, end: &Self) -> (usize, Option<usize>)

🔬This is a nightly-only experimental API. (step_trait)
Returns the bounds on the number of successor steps required to get from start to end like [Iterator::size_hint()][Iterator::size_hint()]. Read more
Source§

fn forward_checked(start: Self, count: usize) -> Option<Self>

🔬This is a nightly-only experimental API. (step_trait)
Returns the value that would be obtained by taking the successor of self count times. Read more
Source§

fn forward_overflowing(start: Self, count: usize) -> (Self, bool)

🔬This is a nightly-only experimental API. (step_trait)
Returns the value that would be obtained by taking the successor of self count times along with a boolean tracking whether overflow occurred. Read more
Source§

fn backward_checked(start: Self, count: usize) -> Option<Self>

🔬This is a nightly-only experimental API. (step_trait)
Returns the value that would be obtained by taking the predecessor of self count times. Read more
Source§

fn backward_overflowing(start: Self, count: usize) -> (Self, bool)

🔬This is a nightly-only experimental API. (step_trait)
Returns the value that would be obtained by taking the successor of self count times along with a boolean tracking whether overflow occurred. Read more
§

fn forward(start: Self, count: usize) -> Self

🔬This is a nightly-only experimental API. (step_trait)
Returns the value that would be obtained by taking the successor of self count times. Read more
§

unsafe fn forward_unchecked(start: Self, count: usize) -> Self

🔬This is a nightly-only experimental API. (step_trait)
Returns the value that would be obtained by taking the successor of self count times. Read more
§

fn backward(start: Self, count: usize) -> Self

🔬This is a nightly-only experimental API. (step_trait)
Returns the value that would be obtained by taking the predecessor of self count times. Read more
§

unsafe fn backward_unchecked(start: Self, count: usize) -> Self

🔬This is a nightly-only experimental API. (step_trait)
Returns the value that would be obtained by taking the predecessor of self count times. Read more
Source§

impl StructuralPartialEq for VirtualAddress

Source§

impl Sub for VirtualAddress

Returns the number of bytes between self and rhs.

Source§

type Output = usize

The resulting type after applying the - operator.
Source§

fn sub(self, rhs: Self) -> Self::Output

Performs the - operation. Read more
Source§

impl Sub<isize> for VirtualAddress

Source§

type Output = VirtualAddress

The resulting type after applying the - operator.
Source§

fn sub(self, rhs: isize) -> Self::Output

Performs the - operation. Read more
Source§

impl Sub<usize> for VirtualAddress

Source§

type Output = VirtualAddress

The resulting type after applying the - operator.
Source§

fn sub(self, rhs: usize) -> Self::Output

Performs the - operation. Read more

Auto Trait Implementations§

§

impl Freeze for VirtualAddress

§

impl RefUnwindSafe for VirtualAddress

§

impl Send for VirtualAddress

§

impl Sync for VirtualAddress

§

impl Unpin for VirtualAddress

§

impl UnsafeUnpin for VirtualAddress

§

impl UnwindSafe for VirtualAddress

Blanket Implementations§

§

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

§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
§

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

§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
§

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

§

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

Mutably borrows from an owned value. Read more
§

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

§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
§

impl<T> From<T> for T

§

fn from(t: T) -> T

Returns the argument unchanged.

§

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

§

fn into(self) -> U

Calls U::from(self).

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

§

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

§

type Owned = T

The resulting type after obtaining ownership.
§

fn to_owned(&self) -> T

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

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

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

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

§

type Error = !

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

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
§

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.
§

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

Performs the conversion.