Skip to main content

index_of

Function index_of 

Source
pub fn index_of(size: usize, align: usize) -> Option<usize>
Expand description

The class index serving size at align, or None when the request belongs on the direct-mapping path (too large, or too strictly aligned to serve from a class).

Slots sit at multiples of the class size inside a span, and span bases are 64 KiB-aligned, so a slot’s alignment is exactly the alignment of its class size. Serving a 16-byte alignment therefore means choosing a class that is a multiple of 16 — which, in the 8-stepped region below 128, is always the very next one.

§Examples

use kevy_alloc::class::{index_of, size_of};

// Every served size rounds UP to its class, never down.
let i = index_of(1, 1).unwrap();
assert!(size_of(i) >= 1);
let i = index_of(100, 1).unwrap();
assert!(size_of(i) >= 100);

// A strict alignment picks a class that is a multiple of it.
let i = index_of(24, 16).unwrap();
assert_eq!(size_of(i) % 16, 0);

// Too large, or too strictly aligned, is None — the direct-mapping
// path, not a wrong class.
assert_eq!(index_of(1 << 30, 1), None);