talc 5.0.3

A fast and flexible allocator for no_std and WebAssembly
Documentation
use core::ptr::NonNull;

#[inline]
pub fn is_aligned_to(ptr: *mut u8, align: usize) -> bool {
    debug_assert!(align.is_power_of_two());

    ptr as usize & (align - 1) == 0
}

#[inline]
pub fn nonnull_slice_from_raw_parts(nn: NonNull<u8>, len: usize) -> NonNull<[u8]> {
    // SAFETY: if `nn` is non-null, then the resulting slice is non-null
    unsafe { NonNull::new_unchecked(core::ptr::slice_from_raw_parts_mut(nn.as_ptr(), len)) }
}

#[inline]
pub fn align_down_by(ptr: *mut u8, align: usize) -> *mut u8 {
    debug_assert!(align.is_power_of_two());

    // this incantation maintains provenance of ptr for MIRI
    // while allowing the compiler to see through the wrapping_add and optimize it
    ptr.wrapping_sub(ptr as usize & (align - 1))
}

#[inline]
pub fn align_up_by(ptr: *mut u8, align: usize) -> *mut u8 {
    align_up_by_mask(ptr, align - 1)
}

#[inline]
pub fn align_up_by_mask(ptr: *mut u8, align_mask: usize) -> *mut u8 {
    debug_assert!((align_mask + 1).is_power_of_two());

    // this incantation maintains provenance of ptr for MIRI
    // while allowing the compiler to see through the wrapping_add and optimize it
    ptr.wrapping_add(
        ((ptr as usize).wrapping_add(align_mask) & !align_mask).wrapping_sub(ptr as usize),
    )
    // equivalent to the following:
    // ((ptr as usize + align_mask) & !align_mask) as *mut u8
    // i.e. just align up to the next align_mask + 1
}

#[inline]
pub fn saturating_ptr_add(ptr: *mut u8, bytes: usize) -> *mut u8 {
    // done this way to maintain the provenance of `base` for MIRI

    // if you add to ptr and the result is less than ptr, it wrapped
    if ptr.wrapping_add(bytes) < ptr {
        // this gets to NULL-1, the compiler will see through it
        ptr.wrapping_add((ptr as usize).wrapping_neg() - 1)
    } else {
        // normal result
        ptr.wrapping_add(bytes)
    }
}