use std::alloc::LayoutError;
use thiserror::Error;
#[derive(Debug, Error, PartialEq, Eq, Clone)]
pub enum AllocationError {
#[error("an arithmetic error occurred")]
ArithmeticError,
#[error(transparent)]
LayoutError(#[from] LayoutError),
#[error("process ran out of memory")]
OutOfMemory,
}
#[derive(Debug, Error, PartialEq, Eq, Clone)]
pub enum DeallocationError {
#[error("this allocation's marker is corrupted: either memory corruption occurred, or a pointer not allocated by this crate's alloc was passed to realloc or free")]
CorruptedMarker,
#[error("this memory was already freed")]
DoubleFree,
#[error("the pointer provided was not properly aligned")]
ImproperAlignment,
#[error(transparent)]
LayoutError(#[from] LayoutError),
#[error("tried to free a null pointer")]
NullPtr,
}
pub(crate) enum ValidationError {
ImproperAlignment,
MarkerFree,
MarkerCorrupted,
}
impl From<ValidationError> for DeallocationError {
fn from(error: ValidationError) -> Self {
match error {
ValidationError::ImproperAlignment => Self::ImproperAlignment,
ValidationError::MarkerFree => Self::DoubleFree,
ValidationError::MarkerCorrupted => Self::CorruptedMarker,
}
}
}
impl From<ValidationError> for ReallocationError {
fn from(error: ValidationError) -> Self {
match error {
ValidationError::ImproperAlignment => Self::ImproperAlignment,
ValidationError::MarkerFree => Self::MarkerFree,
ValidationError::MarkerCorrupted => Self::MarkerCorrupted,
}
}
}
#[derive(Debug, Error, PartialEq, Eq, Clone)]
pub enum ReallocationError {
#[error(transparent)]
AllocationError(#[from] AllocationError),
#[error(transparent)]
DeallocationError(#[from] DeallocationError),
#[error("the pointer provided was not properly aligned")]
ImproperAlignment,
#[error("the pointer provided points to already freed memory")]
MarkerFree,
#[error("the pointer provided has a corrupted marker; was it allocated by this crate?")]
MarkerCorrupted,
#[error("the new allocation failed, but your old pointer remains valid")]
NewAllocationFailed,
}