Skip to main content

PhysAddr

Struct PhysAddr 

Source
pub struct PhysAddr(/* private fields */);
Expand description

A physical memory address.

This is a newtype wrapper around u64 that provides type safety and utility methods for working with physical addresses.

§Invariants

Physical addresses should typically be page-aligned when used for allocation. The allocator enforces this constraint.

§Examples

use ruvix_physmem::PhysAddr;

let addr = PhysAddr::new(0x1000_0000);
assert_eq!(addr.as_u64(), 0x1000_0000);
assert!(addr.is_page_aligned());

let next_page = addr.add_pages(1);
assert_eq!(next_page.as_u64(), 0x1000_1000);

Implementations§

Source§

impl PhysAddr

Source

pub const NULL: Self

The null physical address (0x0).

Source

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

Creates a new physical address.

§Arguments
  • addr - The raw physical address value.
§Examples
use ruvix_physmem::PhysAddr;

let addr = PhysAddr::new(0x1000);
assert_eq!(addr.as_u64(), 0x1000);
Source

pub const fn from_pfn(pfn: u64) -> Self

Creates a new physical address from a page frame number.

The page frame number is the physical address divided by PAGE_SIZE.

§Arguments
  • pfn - The page frame number.
§Examples
use ruvix_physmem::PhysAddr;

let addr = PhysAddr::from_pfn(1);
assert_eq!(addr.as_u64(), 0x1000); // Page 1 = 4096

let addr = PhysAddr::from_pfn(256);
assert_eq!(addr.as_u64(), 0x10_0000); // 256 * 4096 = 1MB
Source

pub const fn as_u64(self) -> u64

Returns the raw physical address value.

§Examples
use ruvix_physmem::PhysAddr;

let addr = PhysAddr::new(0x1234_5000);
assert_eq!(addr.as_u64(), 0x1234_5000);
Source

pub const fn pfn(self) -> u64

Returns the page frame number for this address.

This is the physical address divided by PAGE_SIZE, truncating any offset within the page.

§Examples
use ruvix_physmem::PhysAddr;

let addr = PhysAddr::new(0x1000);
assert_eq!(addr.pfn(), 1);

let addr = PhysAddr::new(0x1234);
assert_eq!(addr.pfn(), 1); // Offset 0x234 is truncated
Source

pub const fn page_offset(self) -> u64

Returns the offset within the page.

§Examples
use ruvix_physmem::PhysAddr;

let addr = PhysAddr::new(0x1234);
assert_eq!(addr.page_offset(), 0x234);

let addr = PhysAddr::new(0x1000);
assert_eq!(addr.page_offset(), 0);
Source

pub const fn is_page_aligned(self) -> bool

Checks if the address is page-aligned.

§Examples
use ruvix_physmem::PhysAddr;

assert!(PhysAddr::new(0).is_page_aligned());
assert!(PhysAddr::new(0x1000).is_page_aligned());
assert!(!PhysAddr::new(0x1001).is_page_aligned());
Source

pub const fn is_null(self) -> bool

Checks if the address is null (zero).

§Examples
use ruvix_physmem::PhysAddr;

assert!(PhysAddr::NULL.is_null());
assert!(PhysAddr::new(0).is_null());
assert!(!PhysAddr::new(0x1000).is_null());
Source

pub const fn align_down(self) -> Self

Aligns the address down to the nearest page boundary.

§Examples
use ruvix_physmem::PhysAddr;

let addr = PhysAddr::new(0x1234);
assert_eq!(addr.align_down().as_u64(), 0x1000);

let addr = PhysAddr::new(0x2000);
assert_eq!(addr.align_down().as_u64(), 0x2000);
Source

pub const fn align_up(self) -> Self

Aligns the address up to the nearest page boundary.

§Examples
use ruvix_physmem::PhysAddr;

let addr = PhysAddr::new(0x1001);
assert_eq!(addr.align_up().as_u64(), 0x2000);

let addr = PhysAddr::new(0x2000);
assert_eq!(addr.align_up().as_u64(), 0x2000);
Source

pub const fn add_pages(self, pages: usize) -> Self

Adds a number of pages to the address.

§Arguments
  • pages - The number of pages to add.
§Examples
use ruvix_physmem::PhysAddr;

let addr = PhysAddr::new(0x1000);
assert_eq!(addr.add_pages(1).as_u64(), 0x2000);
assert_eq!(addr.add_pages(4).as_u64(), 0x5000);
Source

pub const fn sub_pages(self, pages: usize) -> Self

Subtracts a number of pages from the address.

§Arguments
  • pages - The number of pages to subtract.
§Examples
use ruvix_physmem::PhysAddr;

let addr = PhysAddr::new(0x5000);
assert_eq!(addr.sub_pages(1).as_u64(), 0x4000);
assert_eq!(addr.sub_pages(4).as_u64(), 0x1000);
Source

pub const fn add_bytes(self, offset: u64) -> Self

Adds a byte offset to the address.

§Arguments
  • offset - The byte offset to add.
§Examples
use ruvix_physmem::PhysAddr;

let addr = PhysAddr::new(0x1000);
assert_eq!(addr.add_bytes(0x100).as_u64(), 0x1100);
Source

pub const fn pages_to(self, other: Self) -> usize

Calculates the number of pages between two addresses.

Returns the number of complete pages between self and other. Both addresses should be page-aligned for meaningful results.

§Arguments
  • other - The other address to compare with.
§Examples
use ruvix_physmem::PhysAddr;

let start = PhysAddr::new(0x1000);
let end = PhysAddr::new(0x5000);
assert_eq!(start.pages_to(end), 4);
Source

pub const fn is_in_range(self, start: Self, end: Self) -> bool

Checks if this address is within a range.

§Arguments
  • start - The start of the range (inclusive).
  • end - The end of the range (exclusive).
§Examples
use ruvix_physmem::PhysAddr;

let start = PhysAddr::new(0x1000);
let end = PhysAddr::new(0x5000);

assert!(PhysAddr::new(0x1000).is_in_range(start, end));
assert!(PhysAddr::new(0x3000).is_in_range(start, end));
assert!(!PhysAddr::new(0x5000).is_in_range(start, end));
assert!(!PhysAddr::new(0x0000).is_in_range(start, end));
Source

pub const fn is_order_aligned(self, order: usize) -> bool

Checks if the address is aligned to a power-of-two order.

An address is order-aligned if it is aligned to 2^order * PAGE_SIZE.

§Arguments
  • order - The order to check alignment for.
§Examples
use ruvix_physmem::PhysAddr;

let addr = PhysAddr::new(0x4000); // 16KB
assert!(addr.is_order_aligned(0)); // Aligned to 4KB
assert!(addr.is_order_aligned(1)); // Aligned to 8KB
assert!(addr.is_order_aligned(2)); // Aligned to 16KB
assert!(!addr.is_order_aligned(3)); // Not aligned to 32KB

Trait Implementations§

Source§

impl Add<u64> for PhysAddr

Source§

type Output = PhysAddr

The resulting type after applying the + operator.
Source§

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

Performs the + operation. Read more
Source§

impl AddAssign<u64> for PhysAddr

Source§

fn add_assign(&mut self, rhs: u64)

Performs the += operation. Read more
Source§

impl Clone for PhysAddr

Source§

fn clone(&self) -> PhysAddr

Returns a duplicate 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 Debug for PhysAddr

Source§

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

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

impl Default for PhysAddr

Source§

fn default() -> PhysAddr

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

impl Display for PhysAddr

Source§

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

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

impl From<PhysAddr> for u64

Source§

fn from(addr: PhysAddr) -> Self

Converts to this type from the input type.
Source§

impl From<u64> for PhysAddr

Source§

fn from(addr: u64) -> Self

Converts to this type from the input type.
Source§

impl Hash for PhysAddr

Source§

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

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

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 PhysAddr

Source§

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

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

impl Ord for PhysAddr

Source§

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

This method returns an Ordering between self and other. Read more
1.21.0 · Source§

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

Compares and returns the maximum of two values. Read more
1.21.0 · Source§

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

Compares and returns the minimum of two values. Read more
1.50.0 · Source§

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

Restrict a value to a certain interval. Read more
Source§

impl PartialEq for PhysAddr

Source§

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

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

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

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl PartialOrd for PhysAddr

Source§

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

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · Source§

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

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

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 · Source§

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

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

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 Sub<u64> for PhysAddr

Source§

type Output = PhysAddr

The resulting type after applying the - operator.
Source§

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

Performs the - operation. Read more
Source§

impl Sub for PhysAddr

Source§

type Output = u64

The resulting type after applying the - operator.
Source§

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

Performs the - operation. Read more
Source§

impl SubAssign<u64> for PhysAddr

Source§

fn sub_assign(&mut self, rhs: u64)

Performs the -= operation. Read more
Source§

impl UpperHex for PhysAddr

Source§

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

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

impl Copy for PhysAddr

Source§

impl Eq for PhysAddr

Source§

impl StructuralPartialEq for PhysAddr

Auto Trait Implementations§

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> CloneToUninit for T
where T: Clone,

Source§

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
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, U> TryFrom<U> for T
where U: Into<T>,

Source§

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>,

Source§

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.