Struct Allocation

Source
pub struct Allocation { /* private fields */ }
Expand description

An allocation backed by raw virtual memory, giving you the power to directly manipulate the pages within it.

See also the crate-level documentation for more information about virtual memory.

Implementations§

Source§

impl Allocation

Source

pub fn new(size: usize) -> Result<Self>

Allocates a new region in the process’s virtual address space.

size is the size to reserve in bytes. This number can be excessively huge, as none of the memory is committed until you call commit. The memory is unreserved when the Allocation is dropped.

§Errors

Returns an error if the operating system returns an error.

§Panics
  • Panics if size is not aligned to the page size.
  • Panics if size is zero.
Source

pub const fn dangling(alignment: usize) -> Allocation

Creates a dangling Allocation, that is, an allocation with a dangling pointer and zero size.

This is useful as a placeholder value to defer allocation until later or if no allocation is needed.

alignment is the alignment of the allocation’s pointer, and must be a power of two.

§Panics

Panics if alignment is not a power of two.

Source

pub const fn ptr(&self) -> *mut u8

Returns the pointer to the beginning of the allocation.

The returned pointer is always valid, including dangling allocations, for reads and writes of size() bytes in the sense that it can never lead to undefined behavior. However, doing a read or write access to pages that have not been committed will result in the process receiving SIGSEGV / STATUS_ACCESS_VIOLATION.

The pointer must not be accessed after self has been dropped.

Source

pub const fn size(&self) -> usize

Returns the size that was used to allocate self.

Source

pub fn commit(&self, ptr: *mut u8, size: usize) -> Result<()>

Commits the given region of memory.

§Errors

Returns an error if the operating system returns an error.

§Panics
  • Panics if the allocation is dangling.
  • Panics if ptr and size denote a region that is out of bounds of the allocation.
  • Panics if ptr and/or size is not aligned to the page size.
  • Panics if size is zero.
Source

pub fn decommit(&self, ptr: *mut u8, size: usize) -> Result<()>

Decommits the given region of memory.

§Errors

Returns an error if the operating system returns an error.

§Panics
  • Panics if the allocation is dangling.
  • Panics if ptr and size denote a region that is out of bounds of the allocation.
  • Panics if ptr and/or size is not aligned to the page size.
  • Panics if size is zero.
Source

pub fn prefault(&self, ptr: *mut u8, size: usize) -> Result<()>

Prefaults the given region of memory.

§Errors

Returns an error if the operating system returns an error.

§Panics
  • Panics if the allocation is dangling.
  • Panics if ptr and size denote a region that is out of bounds of the allocation.
  • Panics if ptr and/or size is not aligned to the page size.
  • Panics if size is zero.

Trait Implementations§

Source§

impl Debug for Allocation

Source§

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

Formats the value using the given formatter. Read more

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