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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
mod abort;
mod layout;
pub use self::{
abort::AbortAlloc,
layout::{LayoutErr, NonZeroLayout},
};
pub use core::alloc::{GlobalAlloc, Layout};
use core::{
fmt,
ptr::{self, NonNull},
};
pub use liballoc::alloc::{alloc, alloc_zeroed, dealloc, realloc};
#[cfg(feature = "std")]
use std::alloc::System;
pub trait BuildAlloc: Sized {
type AllocRef: AllocRef<BuildAlloc = Self>;
unsafe fn build_alloc_ref(&mut self, ptr: NonNull<u8>, layout: Layout) -> Self::AllocRef;
}
pub trait BuildDealloc: Sized {
type DeallocRef: DeallocRef<BuildDealloc = Self>;
unsafe fn build_dealloc_ref(&mut self, ptr: NonNull<u8>, layout: Layout) -> Self::DeallocRef;
}
pub trait BuildRealloc: Sized {
type ReallocRef: ReallocRef<BuildRealloc = Self>;
unsafe fn build_realloc_ref(&mut self, ptr: NonNull<u8>, layout: Layout) -> Self::ReallocRef;
}
pub trait AllocRef: Sized {
type BuildAlloc: BuildAlloc<AllocRef = Self>;
type Error;
fn get_build_alloc(&mut self) -> Self::BuildAlloc;
fn alloc(&mut self, layout: NonZeroLayout) -> Result<NonNull<u8>, Self::Error>;
fn alloc_zeroed(&mut self, layout: NonZeroLayout) -> Result<NonNull<u8>, Self::Error> {
let size = layout.size();
let p = self.alloc(layout)?;
unsafe {
ptr::write_bytes(p.as_ptr(), 0, size);
}
Ok(p)
}
}
pub trait DeallocRef: Sized {
type BuildDealloc: BuildDealloc<DeallocRef = Self>;
fn get_build_dealloc(&mut self) -> Self::BuildDealloc;
unsafe fn dealloc(&mut self, ptr: NonNull<u8>, layout: NonZeroLayout);
}
pub trait ReallocRef: Sized {
type BuildRealloc: BuildRealloc<ReallocRef = Self>;
type Error;
fn get_build_realloc(&mut self) -> Self::BuildRealloc;
unsafe fn realloc(
&mut self,
ptr: NonNull<u8>,
layout: NonZeroLayout,
new_size: usize,
) -> Result<NonNull<u8>, Self::Error>;
}
#[derive(Clone, PartialEq, Eq, Debug)]
pub struct AllocErr;
impl fmt::Display for AllocErr {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str("memory allocation failed")
}
}
#[derive(Copy, Clone, Default, Debug)]
pub struct Global;
macro_rules! impl_common_alloc_zst {
($ty:tt) => {
impl BuildAlloc for $ty {
type AllocRef = Self;
unsafe fn build_alloc_ref(
&mut self,
_ptr: NonNull<u8>,
_layout: Layout,
) -> Self::AllocRef {
Self
}
}
impl BuildDealloc for $ty {
type DeallocRef = Self;
unsafe fn build_dealloc_ref(
&mut self,
_ptr: NonNull<u8>,
_layout: Layout,
) -> Self::DeallocRef {
Self
}
}
impl BuildRealloc for $ty {
type ReallocRef = Self;
unsafe fn build_realloc_ref(
&mut self,
_ptr: NonNull<u8>,
_layout: Layout,
) -> Self::ReallocRef {
Self
}
}
};
}
impl_common_alloc_zst!(Global);
#[cfg(feature = "std")]
impl_common_alloc_zst!(System);
impl AllocRef for Global {
type BuildAlloc = Self;
type Error = AllocErr;
fn get_build_alloc(&mut self) -> Self::BuildAlloc {
Self
}
fn alloc(&mut self, layout: NonZeroLayout) -> Result<NonNull<u8>, Self::Error> {
unsafe { NonNull::new(alloc(layout.into())).ok_or(AllocErr) }
}
fn alloc_zeroed(&mut self, layout: NonZeroLayout) -> Result<NonNull<u8>, Self::Error> {
unsafe { NonNull::new(alloc_zeroed(layout.into())).ok_or(AllocErr) }
}
}
impl DeallocRef for Global {
type BuildDealloc = Self;
fn get_build_dealloc(&mut self) -> Self::BuildDealloc {
Self
}
unsafe fn dealloc(&mut self, ptr: NonNull<u8>, layout: NonZeroLayout) {
dealloc(ptr.as_ptr(), layout.into())
}
}
impl ReallocRef for Global {
type BuildRealloc = Self;
type Error = AllocErr;
fn get_build_realloc(&mut self) -> Self::BuildRealloc {
Self
}
unsafe fn realloc(
&mut self,
ptr: NonNull<u8>,
layout: NonZeroLayout,
new_size: usize,
) -> Result<NonNull<u8>, Self::Error> {
NonNull::new(realloc(ptr.as_ptr(), layout.into(), new_size)).ok_or(AllocErr)
}
}
#[cfg(feature = "std")]
impl AllocRef for System {
type BuildAlloc = Self;
type Error = AllocErr;
fn get_build_alloc(&mut self) -> Self::BuildAlloc {
Self
}
fn alloc(&mut self, layout: NonZeroLayout) -> Result<NonNull<u8>, Self::Error> {
unsafe { NonNull::new(GlobalAlloc::alloc(self, layout.into())).ok_or(AllocErr) }
}
fn alloc_zeroed(&mut self, layout: NonZeroLayout) -> Result<NonNull<u8>, Self::Error> {
unsafe { NonNull::new(GlobalAlloc::alloc_zeroed(self, layout.into())).ok_or(AllocErr) }
}
}
#[cfg(feature = "std")]
impl DeallocRef for System {
type BuildDealloc = Self;
fn get_build_dealloc(&mut self) -> Self::BuildDealloc {
Self
}
unsafe fn dealloc(&mut self, ptr: NonNull<u8>, layout: NonZeroLayout) {
GlobalAlloc::dealloc(self, ptr.as_ptr(), layout.into())
}
}
#[cfg(feature = "std")]
impl ReallocRef for System {
type BuildRealloc = Self;
type Error = AllocErr;
fn get_build_realloc(&mut self) -> Self::BuildRealloc {
Self
}
unsafe fn realloc(
&mut self,
ptr: NonNull<u8>,
layout: NonZeroLayout,
new_size: usize,
) -> Result<NonNull<u8>, Self::Error> {
NonNull::new(GlobalAlloc::realloc(
self,
ptr.as_ptr(),
layout.into(),
new_size,
))
.ok_or(AllocErr)
}
}