1use std::alloc::LayoutError;
2
3use thiserror::Error;
4
5#[derive(Debug, Error, PartialEq, Eq, Clone)]
7pub enum AllocationError {
8 #[error("an arithmetic error occurred")]
10 ArithmeticError,
11 #[error(transparent)]
13 LayoutError(#[from] LayoutError),
14 #[error("process ran out of memory")]
16 OutOfMemory,
17}
18
19#[derive(Debug, Error, PartialEq, Eq, Clone)]
21pub enum DeallocationError {
22 #[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")]
25 CorruptedMarker,
26 #[error("this memory was already freed")]
28 DoubleFree,
29 #[error("the pointer provided was not properly aligned")]
31 ImproperAlignment,
32 #[error(transparent)]
35 LayoutError(#[from] LayoutError),
36 #[error("tried to free a null pointer")]
38 NullPtr,
39}
40
41pub(crate) enum ValidationError {
43 ImproperAlignment,
44 MarkerFree,
45 MarkerCorrupted,
46}
47
48impl From<ValidationError> for DeallocationError {
49 fn from(error: ValidationError) -> Self {
50 match error {
51 ValidationError::ImproperAlignment => Self::ImproperAlignment,
52 ValidationError::MarkerFree => Self::DoubleFree,
53 ValidationError::MarkerCorrupted => Self::CorruptedMarker,
54 }
55 }
56}
57
58impl From<ValidationError> for ReallocationError {
59 fn from(error: ValidationError) -> Self {
60 match error {
61 ValidationError::ImproperAlignment => Self::ImproperAlignment,
62 ValidationError::MarkerFree => Self::MarkerFree,
63 ValidationError::MarkerCorrupted => Self::MarkerCorrupted,
64 }
65 }
66}
67
68#[derive(Debug, Error, PartialEq, Eq, Clone)]
70pub enum ReallocationError {
71 #[error(transparent)]
73 AllocationError(#[from] AllocationError),
74 #[error(transparent)]
76 DeallocationError(#[from] DeallocationError),
77 #[error("the pointer provided was not properly aligned")]
79 ImproperAlignment,
80 #[error("the pointer provided points to already freed memory")]
82 MarkerFree,
83 #[error("the pointer provided has a corrupted marker; was it allocated by this crate?")]
86 MarkerCorrupted,
87 #[error("the new allocation failed, but your old pointer remains valid")]
89 NewAllocationFailed,
90}