attachable-slab-allocator 0.1.0

A high-performance, $O(1)$, Master-Slave slab allocator designed for `no_std` environments, kernels, and embedded systems. This library provides fixed-size memory management with RAII safety while remaining completely agnostic of the underlying memory provider.
Documentation
//! # Memory Layout Orchestrator
//!
//! This module ensures that all slab allocations follow the strict "Size == Alignment"
//! invariant required for our high-speed deallocation math.
//!
//! ## The Masking Trick
//!
//! In our system, if a Slab has a size of 4096 bytes (a power of two), it is also
//! **aligned** to a 4096-byte boundary. This allows us to perform a "constant-time
//! home lookup":
//!
//! For any pointer `P` belonging to a slab, the header is located at:
//! `header_address = P & !(SLAB_SIZE - 1)`
//!
//! `get_layout` enforces this alignment at the moment of allocation.

use core::alloc::Layout;

/// Generates a [`Layout`] where the alignment is strictly equal to the size.
///
/// This function is intended for use in `SlabCache` to define the raw memory
/// properties of a new Master or Slave segment.
///
/// # Panics
/// Panics if `slab_size` is not a power of two, as `Layout` construction
/// will fail. In practice, this is caught at compile-time by `SlabCache` assertions.
pub const fn get_layout(slab_size: usize) -> Layout {
    let layout_res = Layout::from_size_align(slab_size, slab_size);

    match layout_res {
        Ok(layout) => layout,
        Err(_) => panic!("Invalid Slab Size: Must be a power of two and non-zero"),
    }
}