Skip to main content

buddy_slab_allocator/
error.rs

1use core::fmt;
2
3/// The error type used for allocation operations.
4#[derive(Debug, Clone, Copy, PartialEq, Eq)]
5pub enum AllocError {
6    /// Invalid size, alignment, or other input parameter.
7    InvalidParam,
8    /// A region overlaps with an existing managed region.
9    MemoryOverlap,
10    /// Not enough memory is available to satisfy the request.
11    NoMemory,
12    /// Attempted to deallocate memory that was not allocated.
13    NotAllocated,
14    /// The allocator has not been initialized.
15    NotInitialized,
16    /// The requested address or entity was not found in any managed region.
17    NotFound,
18}
19
20impl fmt::Display for AllocError {
21    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
22        match self {
23            Self::InvalidParam => write!(f, "invalid parameter"),
24            Self::MemoryOverlap => write!(f, "memory regions overlap"),
25            Self::NoMemory => write!(f, "out of memory"),
26            Self::NotAllocated => write!(f, "memory not allocated"),
27            Self::NotInitialized => write!(f, "allocator not initialized"),
28            Self::NotFound => write!(f, "not found"),
29        }
30    }
31}
32
33/// A [`Result`] alias with [`AllocError`] as the error type.
34pub type AllocResult<T = ()> = Result<T, AllocError>;