[−][src]Trait vm_memory::address::Address
Trait to represent an address within an address space.
To simplify the design and implementation, assume the same raw data type (AddressValue::V) could be used to store address, size and offset for the address space. Thus the Address trait could be used to manage address, size and offset. On the other hand, type aliases may be defined to improve code readability.
One design rule is applied to the Address trait, namely that operators (+, -, &, | etc) are not supported and it forces clients to explicitly invoke corresponding methods. But there are always exceptions: Address (BitAnd|BitOr) AddressValue are supported.
Required methods
fn new(addr: Self::V) -> Self
Creates an address from a raw address value.
fn raw_value(&self) -> Self::V
Returns the raw value of the address.
fn checked_offset_from(&self, base: Self) -> Option<Self::V>
Computes the offset from this address to the given base address.
Returns None
if there is underflow.
fn checked_add(&self, other: Self::V) -> Option<Self>
Computes self + other
, returning None
if overflow occurred.
fn overflowing_add(&self, other: Self::V) -> (Self, bool)
Computes self + other
.
Returns a tuple of the addition result along with a boolean indicating whether an arithmetic overflow would occur. If an overflow would have occurred then the wrapped address is returned.
fn unchecked_add(&self, offset: Self::V) -> Self
Computes self + offset
.
Results in undefined behavior when an overflow occurs.
fn checked_sub(&self, other: Self::V) -> Option<Self>
Subtracts two addresses, checking for underflow. If underflow happens, None
is returned.
fn overflowing_sub(&self, other: Self::V) -> (Self, bool)
Computes self - other
.
Returns a tuple of the subtraction result along with a boolean indicating whether an arithmetic overflow would occur. If an overflow would have occurred then the wrapped address is returned.
fn unchecked_sub(&self, other: Self::V) -> Self
Computes self - other
.
Results in undefined behavior when an underflow occurs.
Provided methods
fn mask(&self, mask: Self::V) -> Self::V
Returns the bitwise and of the address with the given mask.
fn unchecked_offset_from(&self, base: Self) -> Self::V
Computes the offset from this address to the given base address.
Results in undefined behavior when an underflow occurs.
Examples
let base = GuestAddress(0x100); let addr = GuestAddress(0x150); assert_eq!(addr.unchecked_offset_from(base), 0x50);
fn checked_align_up(&self, power_of_two: Self::V) -> Option<Self>
Returns self, aligned to the given power of two.
fn unchecked_align_up(&self, power_of_two: Self::V) -> Self
Returns self, aligned to the given power of two. Only use this when the result is guaranteed not to overflow.