Skip to main content

PageSize

Struct PageSize 

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

A power-of-two page size, stored as a bit-shift (log2 of bytes).

§Safety invariant

The inner value must be a valid page size for the target architecture.

Implementations§

Source§

impl PageSize

Source

pub const fn from_log2(shift: u8) -> Self

Construct a PageSize from a log2 value. Panics if shift is 0 or >= usize::BITS.

Examples found in repository?
examples/basic.rs (line 78)
78const BASE: PageSize = PageSize::from_log2(12);
Source

pub const fn bytes(&self) -> usize

The size in bytes.

Examples found in repository?
examples/basic.rs (line 81)
81const MAX_BLOCK: usize = BASE.bytes() << (ORDERS - 1);
82
83/// Total span the allocator manages, in base frames. 256 × 4 KiB = 1 MiB.
84const SPAN_FRAMES: usize = 256;
85
86/// A `const`-constructed `static`: no runtime initialiser, nothing on the heap.
87/// `init` happens once at boot; from then on it is shared, lock-free for the
88/// fast path, across every CPU.
89static PHYS: SummaryBuddyAllocator<ORDERS, IdentityProv> = SummaryBuddyAllocator::new(BASE);
90
91fn main() {
92    // A boot memory map with holes:
93    //
94    // The backing pool spans the whole window; we then declare only the *usable*
95    // sub-ranges. Two holes stay reserved and are never handed out:
96    //
97    //   [  0 .. 100)  usable   <- the buddy carves its in-pool bitmap from here
98    //   [100 .. 104)  RESERVED  (kernel image)
99    //   [104 .. 200)  usable
100    //   [200 .. 205)  RESERVED  (MMIO window)
101    //   [205 .. 256)  usable
102    let pool = Region::new(SPAN_FRAMES * BASE.bytes(), MAX_BLOCK);
103    let phys_base = pool.addr(); // the "physical" origin
104
105    let frame = BASE.bytes();
106    let range = |lo: usize, hi: usize| PhysRange {
107        base: phys_base + lo * frame,
108        len: (hi - lo) * frame,
109    };
110    let usable = [range(0, 100), range(104, 200), range(205, 256)];
111
112    // SAFETY: single-threaded, called once before any allocation. `phys_base` is
113    // MAX_BLOCK-aligned (Region honours the requested alignment); the bitmap host
114    // range is exclusively owned and reachable through `IdentityProv::create`; the
115    // usable ranges are sorted, non-overlapping, base-frame-aligned, and within
116    // the span.
117    unsafe { PHYS.init(phys_base, SPAN_FRAMES * frame, &usable) };
118
119    println!("Initialised SummaryBuddy over a 1 MiB span with two reserved holes.");
120    print_stats("after init", &PHYS);
121
122    // A few allocate / deallocate cycles:
123    //
124    // One single base frame, then a 4-frame (order-2) contiguous block. Every
125    // address is physical and aligned to the request size.
126    let single = PHYS
127        .allocate_physical(BASE, n(1))
128        .expect("single-frame alloc");
129    let block = PHYS
130        .allocate_physical(BASE, n(4))
131        .expect("4-frame contiguous alloc");
132    println!("\nallocate_physical(1 frame)  -> {single:#x}");
133    println!("allocate_physical(4 frames) -> {block:#x}");
134    print_stats("with 5 frames out", &PHYS);
135
136    // SAFETY: each address came from `allocate_physical` with the same page size
137    // and count and is not used afterwards.
138    unsafe {
139        PHYS.deallocate_physical(BASE, n(1), single);
140        PHYS.deallocate_physical(BASE, n(4), block);
141    }
142    println!("\nfreed both — buddies merge back.");
143    print_stats("after free", &PHYS);
144
145    // Composition: a per-CPU magazine + shared depot over a fresh SummaryBuddy:
146    compose_with_depot();
147}
148
149/// Wrap a `SummaryBuddyAllocator` in a [`DepotAllocator`].
150fn compose_with_depot() {
151    /// Uniprocessor selector: every CPU maps to slot 0. A real kernel returns an
152    /// APIC id / `TPIDR_EL1` here.
153    struct OneCpu;
154    impl CpuId for OneCpu {
155        fn current_cpu() -> usize {
156            0
157        }
158    }
159    const SLOTS: usize = 8; // magazines (>= CPUs you want disjoint)
160
161    // Same const-new composability: the whole stack is one `static`-able value.
162    let mag: DepotAllocator<SummaryBuddyAllocator<ORDERS, IdentityProv>, OneCpu, SLOTS> =
163        DepotAllocator::new(BASE, SummaryBuddyAllocator::new(BASE));
164
165    let pool = Region::new(SPAN_FRAMES * BASE.bytes(), MAX_BLOCK);
166    // SAFETY: as above; here the whole region is usable, so the single-range
167    // convenience applies.
168    unsafe { mag.init_region(pool.addr(), SPAN_FRAMES * BASE.bytes()) };
169
170    println!("\n── DepotAllocator<SummaryBuddyAllocator> ──");
171    let a = mag.allocate_physical(BASE, n(1)).expect("mag alloc");
172    // SAFETY: `a` came from this allocator; freed once, then not reused by us.
173    unsafe { mag.deallocate_physical(BASE, n(1), a) };
174    let b = mag.allocate_physical(BASE, n(1)).expect("mag re-alloc");
175    println!("alloc {a:#x} -> free -> alloc {b:#x}");
176    assert_eq!(a, b, "the magazine should return the just-freed frame");
177    println!("re-alloc returned the cached frame (no backend round-trip).");
178    // SAFETY: final free of a live frame.
179    unsafe { mag.deallocate_physical(BASE, n(1), b) };
180}
Source

pub const fn log2(self) -> u8

The log2 / bit-shift value.

Source

pub const fn total_bytes(self, count: NonZeroUsize) -> Option<usize>

Total bytes for count frames of this size. Returns None on overflow.

Source

pub const fn is_aligned(self, addr: usize) -> bool

True if addr is naturally aligned to this page size.

Source

pub const fn align_down(self, addr: usize) -> usize

Round addr down to the nearest aligned frame base.

Source

pub const fn align_up(self, addr: usize) -> Option<usize>

Round addr up to the next aligned frame base. Returns None on overflow.

Trait Implementations§

Source§

impl Clone for PageSize

Source§

fn clone(&self) -> PageSize

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Copy for PageSize

Source§

impl Debug for PageSize

Source§

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

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

impl Eq for PageSize

Source§

impl Ord for PageSize

Source§

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

This method returns an Ordering between self and other. Read more
1.21.0 (const: unstable) · Source§

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

Compares and returns the maximum of two values. Read more
1.21.0 (const: unstable) · Source§

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

Compares and returns the minimum of two values. Read more
1.50.0 (const: unstable) · 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 PageSize

Source§

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

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

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

Inequality operator !=. Read more
Source§

impl PartialOrd for PageSize

Source§

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

This method returns an ordering between self and other values if one exists. Read more
1.0.0 (const: unstable) · 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 (const: unstable) · 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 (const: unstable) · 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 (const: unstable) · 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 StructuralPartialEq for PageSize

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.