frame-alloc 0.1.0

A no_std, dependency-free, const-constructible physical frame allocator for kernels
Documentation
//! Cache-line padding to prevent false sharing.
//!
//! [`CachePadded<T>`] aligns its contents to the start of a cache line and pads
//! the tail so that no two values placed back-to-back (e.g. adjacent elements of
//! an array) ever land on the same line. Threads touching distinct values then
//! avoid the coherence-traffic penalty of invalidating each other's caches.
//!
//! The alignment table mirrors `crossbeam_utils::CachePadded`; see the module docs:
//! <https://docs.rs/crossbeam-utils/latest/src/crossbeam_utils/cache_padded.rs.html>

use core::ops::{Deref, DerefMut};

/// A value aligned and padded to occupy its own cache line(s).
///
/// Wrap a type in `CachePadded` wherever independent values would otherwise
/// share a cache line — typically the elements of a per-CPU or per-order array.
/// Access is transparent through [`Deref`]/[`DerefMut`].
#[cfg_attr(
    any(
        target_arch = "x86_64",
        target_arch = "aarch64",
        target_arch = "arm64ec",
        target_arch = "powerpc64",
    ),
    repr(align(128))
)]
#[cfg_attr(
    any(
        target_arch = "arm",
        target_arch = "mips",
        target_arch = "mips32r6",
        target_arch = "mips64",
        target_arch = "mips64r6",
        target_arch = "sparc",
        target_arch = "hexagon",
    ),
    repr(align(32))
)]
#[cfg_attr(target_arch = "m68k", repr(align(16)))]
#[cfg_attr(target_arch = "s390x", repr(align(256)))]
#[cfg_attr(
    not(any(
        target_arch = "x86_64",
        target_arch = "aarch64",
        target_arch = "arm64ec",
        target_arch = "powerpc64",
        target_arch = "arm",
        target_arch = "mips",
        target_arch = "mips32r6",
        target_arch = "mips64",
        target_arch = "mips64r6",
        target_arch = "sparc",
        target_arch = "hexagon",
        target_arch = "m68k",
        target_arch = "s390x",
    )),
    repr(align(64))
)]
pub(crate) struct CachePadded<T> {
    value: T,
}

impl<T> CachePadded<T> {
    /// Wrap `value` on its own cache line.
    pub(crate) const fn new(t: T) -> CachePadded<T> {
        CachePadded::<T> { value: t }
    }
}

impl<T> Deref for CachePadded<T> {
    type Target = T;

    #[inline]
    fn deref(&self) -> &T {
        &self.value
    }
}

impl<T> DerefMut for CachePadded<T> {
    #[inline]
    fn deref_mut(&mut self) -> &mut T {
        &mut self.value
    }
}