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
impl PhysAddr
Sourcepub const fn from_pfn(pfn: u64) -> Self
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 = 1MBSourcepub const fn as_u64(self) -> u64
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);Sourcepub const fn pfn(self) -> u64
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 truncatedSourcepub const fn page_offset(self) -> u64
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);Sourcepub const fn is_page_aligned(self) -> bool
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());Sourcepub const fn is_null(self) -> bool
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());Sourcepub const fn align_down(self) -> Self
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);Sourcepub const fn align_up(self) -> Self
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);Sourcepub const fn pages_to(self, other: Self) -> usize
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);Sourcepub const fn is_in_range(self, start: Self, end: Self) -> bool
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));Sourcepub const fn is_order_aligned(self, order: usize) -> bool
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 32KBTrait Implementations§
Source§impl AddAssign<u64> for PhysAddr
impl AddAssign<u64> for PhysAddr
Source§fn add_assign(&mut self, rhs: u64)
fn add_assign(&mut self, rhs: u64)
+= operation. Read moreSource§impl Ord for PhysAddr
impl Ord for PhysAddr
Source§impl PartialOrd for PhysAddr
impl PartialOrd for PhysAddr
Source§impl SubAssign<u64> for PhysAddr
impl SubAssign<u64> for PhysAddr
Source§fn sub_assign(&mut self, rhs: u64)
fn sub_assign(&mut self, rhs: u64)
-= operation. Read more