ps-alloc 0.1.0-8

a reasonably safe allocator
Documentation
use crate::marker::{MARKER_FREE, MARKER_USED};

/// Metadata header prefixed to every allocation.
///
/// Stores the allocation state via `marker` (`MARKER_USED` or `MARKER_FREE`) and the
/// total allocation size in bytes (including this header and alignment padding) in `size`.
///
/// Aligned to 16 bytes to guarantee all allocations are aligned to 16 bytes.
#[repr(align(16))]
pub struct AllocationHeader {
    marker: [u8; 8],
    size: usize,
}

impl AllocationHeader {
    /// Checks the allocation size stored in this header.
    ///
    /// Marker checks are best-effort debugging aids.
    /// Reading this header is undefined behavior if the pointer is invalid.
    #[inline]
    pub const fn get_size(&self) -> usize {
        self.size
    }

    /// Checks whether this header is marked as free (deallocated).
    ///
    /// Marker checks are best-effort debugging aids.
    /// Reading this header is undefined behavior if the pointer is invalid.
    #[inline]
    pub fn is_marked_as_free(&self) -> bool {
        self.marker == MARKER_FREE
    }

    /// Checks whether this header is marked as used (allocated).
    ///
    /// Marker checks are best-effort debugging aids.
    /// Reading this header is undefined behavior if the pointer is invalid.
    #[inline]
    pub fn is_marked_as_used(&self) -> bool {
        self.marker == MARKER_USED
    }

    #[inline]
    pub const fn mark_as_free(&mut self) {
        self.marker = MARKER_FREE;
    }

    #[inline]
    pub const fn mark_as_used(&mut self) {
        self.marker = MARKER_USED;
    }

    #[inline]
    pub const fn set_size(&mut self, new_size: usize) {
        self.size = new_size;
    }
}

pub const HEADER_SIZE: usize = std::mem::size_of::<AllocationHeader>();