1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
use alloc::alloc::Layout;
use core::fmt::{self, Display, Formatter};
use core::mem::MaybeUninit;
use core::ptr::{self, NonNull};
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct AllocError;
impl Display for AllocError {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
write!(f, "memory allocation failed")
}
}
#[cfg(feature = "std")]
#[cfg_attr(feature = "doc_cfg", doc(cfg(feature = "std")))]
impl std::error::Error for AllocError {}
pub unsafe trait Allocator {
fn allocate(&self, layout: Layout) -> Result<NonNull<[u8]>, AllocError>;
unsafe fn deallocate(&self, ptr: NonNull<u8>, layout: Layout);
fn allocate_zeroed(
&self,
layout: Layout,
) -> Result<NonNull<[u8]>, AllocError> {
let ptr = self.allocate(layout)?;
unsafe {
let len = (*(ptr.as_ptr() as *mut [MaybeUninit<u8>])).len();
(ptr.as_ptr() as *mut u8).write_bytes(0_u8, len);
}
Ok(ptr)
}
unsafe fn grow(
&self,
ptr: NonNull<u8>,
old_layout: Layout,
new_layout: Layout,
) -> Result<NonNull<[u8]>, AllocError> {
let new = self.allocate(new_layout)?;
unsafe {
(new.as_ptr() as *mut u8)
.copy_from_nonoverlapping(ptr.as_ptr(), old_layout.size());
self.deallocate(ptr, old_layout);
}
Ok(new)
}
unsafe fn grow_zeroed(
&self,
ptr: NonNull<u8>,
old_layout: Layout,
new_layout: Layout,
) -> Result<NonNull<[u8]>, AllocError> {
let new = self.allocate(new_layout)?;
unsafe {
let len = (*(new.as_ptr() as *mut [MaybeUninit<u8>])).len();
(new.as_ptr() as *mut u8)
.copy_from_nonoverlapping(ptr.as_ptr(), old_layout.size());
(new.as_ptr() as *mut u8)
.add(old_layout.size())
.write_bytes(0_u8, len - old_layout.size());
self.deallocate(ptr, old_layout);
}
Ok(new)
}
unsafe fn shrink(
&self,
ptr: NonNull<u8>,
old_layout: Layout,
new_layout: Layout,
) -> Result<NonNull<[u8]>, AllocError> {
let new = self.allocate(new_layout)?;
unsafe {
let len = (*(new.as_ptr() as *mut [MaybeUninit<u8>])).len();
(new.as_ptr() as *mut u8)
.copy_from_nonoverlapping(ptr.as_ptr(), len);
self.deallocate(ptr, old_layout);
}
Ok(new)
}
fn by_ref(&self) -> &Self
where
Self: Sized,
{
self
}
}
unsafe impl<A: Allocator> Allocator for &A {
fn allocate(&self, layout: Layout) -> Result<NonNull<[u8]>, AllocError> {
A::allocate(*self, layout)
}
unsafe fn deallocate(&self, ptr: NonNull<u8>, layout: Layout) {
unsafe {
A::deallocate(*self, ptr, layout);
}
}
}
#[derive(Clone, Copy, Debug, Default)]
pub struct Global;
unsafe impl Allocator for Global {
fn allocate(&self, layout: Layout) -> Result<NonNull<[u8]>, AllocError> {
assert!(layout.size() != 0);
NonNull::new(ptr::slice_from_raw_parts_mut(
unsafe { alloc::alloc::alloc(layout) },
layout.size(),
))
.ok_or(AllocError)
}
unsafe fn deallocate(&self, ptr: NonNull<u8>, layout: Layout) {
unsafe { alloc::alloc::dealloc(ptr.as_ptr(), layout) };
}
}