Skip to main content

FrameAllocOptions

Struct FrameAllocOptions 

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

Options controlling how a physical frame is allocated.

The default configuration (FrameAllocOptions::new()) produces a zeroed frame. Callers that need a non-zeroed frame (e.g. DMA buffers that are immediately filled by hardware, or frames that will be fully overwritten before any read) must explicitly call .zeroed(false).

§Example

// Allocate a zeroed page-table frame (the safe default).
let frame = FrameAllocOptions::new()
    .purpose(FramePurpose::PageTable)
    .allocate(token)?;

// Allocate a user-data frame without zeroing (caller guarantees it will
// be fully overwritten, e.g. by an ELF segment load).
let frame = FrameAllocOptions::new()
    .zeroed(false)
    .purpose(FramePurpose::UserData)
    .allocate(token)?;

Implementations§

Source§

impl FrameAllocOptions

Source

pub fn new() -> Self

Creates allocation options with safe defaults:

  • zeroed = true
  • purpose = KernelData
Source

pub fn zeroed(self, zeroed: bool) -> Self

Override the zero-initialisation policy.

§Safety contract (enforced by convention, not the type system)

If zeroed is set to false, the caller MUST fully overwrite every byte of the frame before allowing any other CPU or subsystem to read it. Violating this rule is a memory-safety hazard: stale bytes in an intermediate page-table node cause the CPU to follow arbitrary PTEs.

Source

pub fn purpose(self, p: FramePurpose) -> Self

Set the intended purpose of the frame.

PageTable purpose forces zeroing even if .zeroed(false) was called.

Source

pub fn allocate(self, token: &IrqDisabledToken) -> Result<PhysFrame, AllocError>

Allocate a single 4 KiB frame according to the configured options.

The allocation path is:

  1. Ask the buddy allocator for an order-0 frame (exclusive ownership is guaranteed by the buddy’s own bitmap + free-list discipline).
  2. Optionally zero the 4 KiB frame contents via the HHDM.
  3. Stamp FrameMeta::flags with the purpose flags using Release ordering.
  4. Store refcount = 1 with Release ordering so any later Acquire load of the refcount observes the fully-initialised metadata and (if zeroed) zeroed content.
§Sentinel handoff: CAS(REFCOUNT_UNUSED -> 1)

buddy.rs maintains the invariant that every frame on the free list has refcount == REFCOUNT_UNUSED. mark_block_allocated() leaves this sentinel intact, so the frame arriving here still carries REFCOUNT_UNUSED.

The CAS atomically claims the frame and acts as a fail-fast corruption check: if the same frame appears twice in the buddy free list (double-free or metadata corruption), the second allocation attempt will observe a refcount of 1 (set by the first allocation) and panic immediately rather than silently aliasing memory.

Trait Implementations§

Source§

impl Default for FrameAllocOptions

Source§

fn default() -> Self

Returns the “default value” for a type. 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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
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.