Skip to main content

attachable_slab_allocator/
defs.rs

1/// Represents the types of errors that can occur during memory allocation operations.
2#[derive(Debug, Clone, PartialEq, Eq, Hash)]
3pub enum SlabError {
4    /// The requested memory block could not be allocated because no suitable
5    /// free space is available.
6    OutOfMemory,
7    /// The provided address or size does not meet the alignment requirements
8    /// for the specific operation (e.g., page alignment).
9    AlignmentMismatch,
10    /// The provided pointer is invalid or null.
11    InvalidPointer,
12    /// A fatal system error occurred.
13    FatalError,
14}
15
16impl core::fmt::Display for SlabError {
17    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
18        match self {
19            Self::OutOfMemory => write!(f, "Memory allocation failed: Out of memory"),
20            Self::AlignmentMismatch => {
21                write!(f, "Memory alignment is incorrect for this operation")
22            }
23            Self::InvalidPointer => write!(f, "Attempted to use an invalid or null pointer"),
24            Self::FatalError => write!(f, "A fatal system error occurred"),
25        }
26    }
27}
28
29impl core::error::Error for SlabError {
30    fn source(&self) -> Option<&(dyn core::error::Error + 'static)> {
31        None
32    }
33}
34
35/// A specialized `Result` type for memory allocation operations.
36///
37/// This alias simplifies function signatures by defaulting the error type
38/// to [`AllocatorError`].
39pub type Result<T> = core::result::Result<T, SlabError>;