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 global allocator instance has already been initialized.
9    AlreadyInitialized,
10    /// A region overlaps with an existing managed region.
11    MemoryOverlap,
12    /// Not enough memory is available to satisfy the request.
13    NoMemory,
14    /// Attempted to deallocate memory that was not allocated.
15    NotAllocated,
16    /// The allocator has not been initialized.
17    NotInitialized,
18    /// The requested address or entity was not found in any managed region.
19    NotFound,
20}
21
22impl fmt::Display for AllocError {
23    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
24        match self {
25            Self::InvalidParam => write!(f, "invalid parameter"),
26            Self::AlreadyInitialized => write!(f, "allocator already initialized"),
27            Self::MemoryOverlap => write!(f, "memory regions overlap"),
28            Self::NoMemory => write!(f, "out of memory"),
29            Self::NotAllocated => write!(f, "memory not allocated"),
30            Self::NotInitialized => write!(f, "allocator not initialized"),
31            Self::NotFound => write!(f, "not found"),
32        }
33    }
34}
35
36/// A [`Result`] alias with [`AllocError`] as the error type.
37pub type AllocResult<T = ()> = Result<T, AllocError>;