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
use Mutex;
use ;
use ;
const RELEASE_LOCK_ON_REALLOC_LIMIT: usize = 0x10000;
type TalcInner = ;
/// Talc lock, contains a mutex-locked [`Talc`].
///
/// # Example
/// ```rust
/// # use talc::*;
/// let talc = Talc::new(ErrOnOom);
/// let talck = talc.lock::<spin::Mutex<()>>();
/// ```
unsafe
// Keep this because we need it for custom volatile allocator in the future.
/*
use core::alloc::{AllocError, Allocator};
fn is_aligned_to(ptr: *mut u8, align: usize) -> bool {
(ptr as usize).trailing_zeros() >= align.trailing_zeros()
}
/// Convert a nonnull and length to a nonnull slice.
fn nonnull_slice_from_raw_parts(ptr: NonNull<u8>, len: usize) -> NonNull<[u8]> {
unsafe { NonNull::new_unchecked(core::ptr::slice_from_raw_parts_mut(ptr.as_ptr(), len)) }
}
unsafe impl<O: OomHandler> Allocator for Talck<O> {
fn allocate(&self, layout: Layout) -> Result<NonNull<[u8]>, AllocError> {
if layout.size() == 0 {
return Ok(nonnull_slice_from_raw_parts(NonNull::dangling(), 0));
}
unsafe { self.lock().malloc(layout) }
.map(|nn| nonnull_slice_from_raw_parts(nn, layout.size()))
.map_err(|_| AllocError)
}
unsafe fn deallocate(&self, ptr: NonNull<u8>, layout: Layout) {
if layout.size() != 0 {
self.lock().free(ptr, layout);
}
}
unsafe fn grow(
&self, ptr: NonNull<u8>, old_layout: Layout, new_layout: Layout,
) -> Result<NonNull<[u8]>, AllocError> {
debug_assert!(new_layout.size() >= old_layout.size());
if old_layout.size() == 0 {
return self.allocate(new_layout);
} else if is_aligned_to(ptr.as_ptr(), new_layout.align()) {
// alignment is fine, try to allocate in-place
if let Ok(nn) = self.lock().grow_in_place(ptr, old_layout, new_layout.size()) {
return Ok(nonnull_slice_from_raw_parts(nn, new_layout.size()));
}
}
// can't grow in place, reallocate manually
let mut lock = self.lock();
let allocation = lock.malloc(new_layout).map_err(|_| AllocError)?;
if old_layout.size() > RELEASE_LOCK_ON_REALLOC_LIMIT {
drop(lock);
allocation
.as_ptr()
.copy_from_nonoverlapping(ptr.as_ptr(), old_layout.size());
lock = self.lock();
} else {
allocation
.as_ptr()
.copy_from_nonoverlapping(ptr.as_ptr(), old_layout.size());
}
lock.free(ptr, old_layout);
Ok(nonnull_slice_from_raw_parts(allocation, new_layout.size()))
}
unsafe fn grow_zeroed(
&self, ptr: NonNull<u8>, old_layout: Layout, new_layout: Layout,
) -> Result<NonNull<[u8]>, AllocError> {
let res = self.grow(ptr, old_layout, new_layout);
if let Ok(allocation) = res {
allocation
.as_ptr()
.cast::<u8>()
.add(old_layout.size())
.write_bytes(0, new_layout.size() - old_layout.size());
}
res
}
unsafe fn shrink(
&self, ptr: NonNull<u8>, old_layout: Layout, new_layout: Layout,
) -> Result<NonNull<[u8]>, AllocError> {
debug_assert!(new_layout.size() <= old_layout.size());
if new_layout.size() == 0 {
if old_layout.size() > 0 {
self.lock().free(ptr, old_layout);
}
return Ok(nonnull_slice_from_raw_parts(NonNull::dangling(), 0));
}
if !is_aligned_to(ptr.as_ptr(), new_layout.align()) {
let mut lock = self.lock();
let allocation = lock.malloc(new_layout).map_err(|_| AllocError)?;
if new_layout.size() > RELEASE_LOCK_ON_REALLOC_LIMIT {
drop(lock);
allocation
.as_ptr()
.copy_from_nonoverlapping(ptr.as_ptr(), new_layout.size());
lock = self.lock();
} else {
allocation
.as_ptr()
.copy_from_nonoverlapping(ptr.as_ptr(), new_layout.size());
}
lock.free(ptr, old_layout);
return Ok(nonnull_slice_from_raw_parts(allocation, new_layout.size()));
}
self.lock().shrink(ptr, old_layout, new_layout.size());
Ok(nonnull_slice_from_raw_parts(ptr, new_layout.size()))
}
}
*/