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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
use {
crate::{
error::Error,
layout::Layout,
traits::{
AllocError,
alloc_mut::{AllocMut, DeallocMut, GrowMut, ReallocMut, ShrinkMut},
helpers::{Bytes, default_dealloc_panic, grow, ralloc, shrink_unchecked}
}
},
::core::{
cmp::{Ord, Ordering},
convert::From,
marker::Sized,
ptr::{self, NonNull},
result::Result::{self, Err}
}
};
#[allow(unused_imports)] use crate::error::Cause;
/// A memory allocation interface.
pub trait Alloc: AllocError + AllocMut {
/// Attempts to allocate a block of memory fitting the given [`Layout`].
///
/// # Errors
///
/// Errors are implementation-defined, refer to [`AllocError::Error`] and [`Error`].
///
/// The standard implementations may return:
/// - <code>Err([Error::AllocFailed]\(layout, cause\))</code> if allocation fails. `cause` is
/// typically [`Cause::Unknown`]. If the `os_err_reporting` feature is enabled, it will be
/// <code>[Cause::OSErr]\(oserr\)</code>. In this case, `oserr` will be the error from
/// <code>[last_os_error]\(\).[raw_os_error]\(\)</code>.
/// - <code>Err([Error::ZeroSizedLayout])</code> if <code>[layout.size()](Layout::size) ==
/// 0</code>.
///
/// [last_os_error]: ::std::io::Error::last_os_error
/// [raw_os_error]: ::std::io::Error::raw_os_error
fn alloc(&self, layout: Layout) -> Result<NonNull<u8>, <Self as AllocError>::Error>;
/// Attempts to allocate a zeroed block of memory fitting the given [`Layout`].
///
/// # Errors
///
/// Errors are implementation-defined, refer to [`AllocError::Error`] and [`Error`].
///
/// The standard implementations may return:
/// - <code>Err([Error::AllocFailed]\(layout, cause\))</code> if allocation fails. `cause` is
/// typically [`Cause::Unknown`]. If the `os_err_reporting` feature is enabled, it will be
/// <code>[Cause::OSErr]\(oserr\)</code>. In this case, `oserr` will be the error from
/// <code>[last_os_error]\(\).[raw_os_error]\(\)</code>.
/// - <code>Err([Error::ZeroSizedLayout])</code> if <code>[layout.size()](Layout::size) ==
/// 0</code>.
///
/// [last_os_error]: ::std::io::Error::last_os_error
/// [raw_os_error]: ::std::io::Error::raw_os_error
#[cfg_attr(miri, track_caller)]
#[inline]
fn zalloc(&self, layout: Layout) -> Result<NonNull<u8>, <Self as AllocError>::Error> {
zalloc!(self, alloc, layout)
}
}
/// A memory allocation interface which can also deallocate memory.
pub trait Dealloc: Alloc + DeallocMut {
/// Deallocates a previously allocated block.
///
/// This is a noop if <code>[layout.size()](Layout::size) == 0</code> or `ptr` is
/// [`dangling`](ptr::dangling).
///
/// The default implementation simply calls [`try_dealloc`](Dealloc::try_dealloc) and panics if
/// it returns an error.
///
/// # Safety
///
/// The caller must ensure:
/// - `ptr` points to a block of memory allocated using this allocator.
/// - `layout` describes exactly the same block.
///
/// # Panics
///
/// This function may panic if the [`try_dealloc`](Dealloc::try_dealloc) implementation returns
/// an error, or the implementation chooses to panic for any other reason. It will not panic if
/// `ptr` is [`dangling`](ptr::dangling) or <code>[layout.size()](Layout::size) == 0</code>.
#[track_caller]
#[inline]
unsafe fn dealloc(&self, ptr: NonNull<u8>, layout: Layout) {
default_dealloc!(self.try_dealloc, ptr, layout);
}
/// Attempts to deallocate a previously allocated block. If this allocator is backed by an
/// allocation library which does not provide fallible deallocation operations, this may panic,
/// abort, or incorrectly return `Ok(())`.
///
/// # Safety
///
/// The caller must ensure:
/// - `ptr` points to a block of memory allocated using this allocator.
/// - `layout` describes exactly the same block.
///
/// # Errors
///
/// Errors are implementation-defined, refer to [`AllocError::Error`] and [`Error`].
///
/// The standard implementations may return:
/// - <code>Err([Error::ZeroSizedLayout])</code> if <code>[layout.size()](Layout::size) ==
/// 0</code>.
/// - <code>Err([Error::DanglingDeallocation])</code> if <code>ptr ==
/// [layout.dangling](Layout::dangling)</code>.
/// - <code>Err([Error::Unsupported])</code> if deallocation is unsupported. In this case,
/// reallocation via [`Grow`], [`Shrink`], and [`Realloc`] may still be supported.
///
/// However, if the `alloc_mut` feature is enabled, and using this method on a synchronization
/// primitive wrapping a type which implements [`AllocMut`], an
/// [`Error::Other`] wrapping a generic error message will be returned if locking the primitive
/// fails.
unsafe fn try_dealloc(
&self,
ptr: NonNull<u8>,
layout: Layout
) -> Result<(), <Self as AllocError>::Error>;
/// Attempts to deallocate a previously allocated block after performing validity checks.
///
/// This method must return an error rather than silently accepting the deallocation and
/// potentially causing UB.
///
/// # Errors
///
/// Implementations commonly return:
/// - <code>Err([Error::ZeroSizedLayout])</code> if `layout.size() == 0`.
/// - <code>Err([Error::DanglingDeallocation])</code> if `ptr == layout.dangling()`.
/// - <code>Err([Error::Unsupported])</code> if checked deallocation is unsupported.
/// - <code>Err([Error::Other]\(err\))</code> for allocator-specific validation failures. If the
/// `alloc_mut` feature is enabled, and using this method on a synchronization primitive
/// wrapping a type which implements [`AllocMut`], a generic error message will also be
/// returned if locking the primitive fails.
fn checked_dealloc(
&self,
_ptr: NonNull<u8>,
_layout: Layout
) -> Result<(), <Self as AllocError>::Error> {
Err(<Self as AllocError>::Error::from(Error::Unsupported))
}
}
/// A memory allocation interface which can also grow allocations.
pub trait Grow: Alloc + Dealloc + GrowMut {
/// Grow the given block to a new, larger layout.
///
/// On failure, the original memory will not be deallocated.
///
/// Note that the default implementation simply:
/// 1. Checks that the new layout is larger or the same size. If both layouts are the same,
/// `ptr` is returned and no operation is performed.
/// 2. Allocates a new block of memory via [`Alloc::alloc`].
/// 3. Copies [`old_layout.size()`](Layout::size) bytes from the old block to the new block.
/// 4. Deallocates the old block.
/// 5. Returns a pointer to the new block.
///
/// # Safety
///
/// The caller must ensure:
/// - `ptr` points to a block of memory allocated using this allocator.
/// - `old_layout` describes exactly the same block.
///
/// # Errors
///
/// Errors are implementation-defined, refer to [`AllocError::Error`] and [`Error`].
///
/// The standard implementations may return:
/// - <code>Err([Error::AllocFailed]\(layout, cause\))</code> if allocation fails. `cause` is
/// typically [`Cause::Unknown`]. If the `os_err_reporting` feature is enabled, it will be
/// <code>[Cause::OSErr]\(oserr\)</code>. In this case, `oserr` will be the error from
/// <code>[last_os_error]\(\).[raw_os_error]\(\)</code>.
/// - <code>Err([Error::GrowSmallerNewLayout]\([old_layout.size()](Layout::size),
/// [new_layout.size()](Layout::size))\)</code> if <code>[old_layout.size()](Layout::size) >
/// [new_layout.size()](Layout::size)</code>.
/// - <code>Err([Error::ZeroSizedLayout])</code> if <code>[layout.size()](Layout::size) ==
/// 0</code>.
///
/// [last_os_error]: ::std::io::Error::last_os_error
/// [raw_os_error]: ::std::io::Error::raw_os_error
#[cfg_attr(miri, track_caller)]
#[inline]
unsafe fn grow(
&self,
ptr: NonNull<u8>,
old_layout: Layout,
new_layout: Layout
) -> Result<NonNull<u8>, <Self as AllocError>::Error> {
grow(self, ptr, old_layout, new_layout, Bytes::Uninitialized)
}
/// Grows the given block to a new, larger layout, with extra bytes being zeroed.
///
/// On failure, the original memory will not be deallocated.
///
/// Note that the default implementation simply:
/// 1. Checks that the new layout is larger or the same size. If both layouts are the same,
/// `ptr` is returned and no operation is performed.
/// 2. Allocates a new block of memory via [`Alloc::zalloc`].
/// 3. Copies [`old_layout.size()`](Layout::size) bytes from the old block to the new block.
/// 4. Deallocates the old block.
/// 5. Returns a pointer to the new block.
///
/// # Safety
///
/// The caller must ensure:
/// - `ptr` points to a block of memory allocated using this allocator.
/// - `old_layout` describes exactly the same block.
///
/// # Errors
///
/// Errors are implementation-defined, refer to [`AllocError::Error`] and [`Error`].
///
/// The standard implementations may return:
/// - <code>Err([Error::AllocFailed]\(layout, cause\))</code> if allocation fails. `cause` is
/// typically [`Cause::Unknown`]. If the `os_err_reporting` feature is enabled, it will be
/// <code>[Cause::OSErr]\(oserr\)</code>. In this case, `oserr` will be the error from
/// <code>[last_os_error]\(\).[raw_os_error]\(\)</code>.
/// - <code>Err([Error::GrowSmallerNewLayout]\([old_layout.size()](Layout::size),
/// [new_layout.size()](Layout::size))\)</code> if <code>[old_layout.size()](Layout::size) >
/// [new_layout.size()](Layout::size)</code>.
/// - <code>Err([Error::ZeroSizedLayout])</code> if <code>[layout.size()](Layout::size) ==
/// 0</code>.
///
/// [last_os_error]: ::std::io::Error::last_os_error
/// [raw_os_error]: ::std::io::Error::raw_os_error
#[cfg_attr(miri, track_caller)]
#[inline]
unsafe fn zgrow(
&self,
ptr: NonNull<u8>,
old_layout: Layout,
new_layout: Layout
) -> Result<NonNull<u8>, <Self as AllocError>::Error> {
grow(self, ptr, old_layout, new_layout, Bytes::Zeroed)
}
}
/// A memory allocation interface which can also shrink allocations.
pub trait Shrink: Alloc + Dealloc + ShrinkMut {
/// Shrink the given block to a new, smaller layout.
///
/// On failure, the original memory will not be deallocated.
///
/// Note that the default implementation simply:
/// 1. Checks that the new layout is smaller or the same size. If both layouts are the same,
/// `ptr` is returned and no operation is performed.
/// 2. Allocates a new block of memory via [`Alloc::alloc`].
/// 3. Copies [`new_layout.size()`](Layout::size) bytes from the old block to the new block.
/// This will discard any extra bytes from the old block.
/// 4. Deallocates the old block.
/// 5. Returns a pointer to the new block.
///
/// # Safety
///
/// The caller must ensure:
/// - `ptr` points to a block of memory allocated using this allocator.
/// - `old_layout` describes exactly the same block.
///
/// # Errors
///
/// Errors are implementation-defined, refer to [`AllocError::Error`] and [`Error`].
///
/// The standard implementations may return:
/// - <code>Err([Error::AllocFailed]\(layout, cause\))</code> if allocation fails. `cause` is
/// typically [`Cause::Unknown`]. If the `os_err_reporting` feature is enabled, it will be
/// <code>[Cause::OSErr]\(oserr\)</code>. In this case, `oserr` will be the error from
/// <code>[last_os_error]\(\).[raw_os_error]\(\)</code>.
/// - <code>Err([Error::ShrinkLargerNewLayout]\([old_layout.size()](Layout::size),
/// [new_layout.size()](Layout::size))\)</code> if <code>[old_layout.size()](Layout::size) <
/// [new_layout.size()](Layout::size)</code>.
/// - <code>Err([Error::ZeroSizedLayout])</code> if <code>[layout.size()](Layout::size) ==
/// 0</code>.
///
/// [last_os_error]: ::std::io::Error::last_os_error
/// [raw_os_error]: ::std::io::Error::raw_os_error
#[cfg_attr(miri, track_caller)]
unsafe fn shrink(
&self,
ptr: NonNull<u8>,
old_layout: Layout,
new_layout: Layout
) -> Result<NonNull<u8>, <Self as AllocError>::Error> {
default_shrink!(self::shrink_unchecked, ptr, old_layout, new_layout)
}
}
/// A memory allocation interface which can arbitrarily resize allocations.
pub trait Realloc: Grow + Shrink + ReallocMut {
/// Reallocates a block, growing or shrinking as needed.
///
/// On grow, preserves existing contents up to [`old_layout.size()`](Layout::size), and on
/// shrink, truncates to [`new_layout.size()`](Layout::size).
///
/// On failure, the original memory will not be deallocated.
///
/// # Safety
///
/// The caller must ensure:
/// - `ptr` points to a block previously allocated with this allocator.
/// - `old_layout` describes exactly the same block.
///
/// # Errors
///
/// Errors are implementation-defined, refer to [`AllocError::Error`] and [`Error`].
///
/// The standard implementations may return:
/// - <code>Err([Error::AllocFailed]\(layout, cause\))</code> if allocation fails. `cause` is
/// typically [`Cause::Unknown`]. If the `os_err_reporting` feature is enabled, it will be
/// <code>[Cause::OSErr]\(oserr\)</code>. In this case, `oserr` will be the error from
/// <code>[last_os_error]\(\).[raw_os_error]\(\)</code>.
/// - <code>Err([Error::ZeroSizedLayout])</code> if <code>[layout.size()](Layout::size) ==
/// 0</code>.
///
/// [last_os_error]: ::std::io::Error::last_os_error
/// [raw_os_error]: ::std::io::Error::raw_os_error
#[cfg_attr(miri, track_caller)]
#[inline]
unsafe fn realloc(
&self,
ptr: NonNull<u8>,
old_layout: Layout,
new_layout: Layout
) -> Result<NonNull<u8>, <Self as AllocError>::Error> {
ralloc(self, ptr, old_layout, new_layout, Bytes::Uninitialized)
}
/// Reallocates a block, growing or shrinking as needed, with extra bytes being zeroed.
///
/// On grow, preserves existing contents up to [`old_layout.size()`](Layout::size), and on
/// shrink, truncates to [`new_layout.size()`](Layout::size).
///
/// On failure, the original memory will not be deallocated.
///
/// # Safety
///
/// The caller must ensure:
/// - `ptr` points to a block previously allocated with this allocator.
/// - `old_layout` describes exactly the same block.
///
/// # Errors
///
/// Errors are implementation-defined, refer to [`AllocError::Error`] and [`Error`].
///
/// The standard implementations may return:
/// - <code>Err([Error::AllocFailed]\(layout, cause\))</code> if allocation fails. `cause` is
/// typically [`Cause::Unknown`]. If the `os_err_reporting` feature is enabled, it will be
/// <code>[Cause::OSErr]\(oserr\)</code>. In this case, `oserr` will be the error from
/// <code>[last_os_error]\(\).[raw_os_error]\(\)</code>.
/// - <code>Err([Error::ZeroSizedLayout])</code> if <code>[layout.size()](Layout::size) ==
/// 0</code>.
///
/// [last_os_error]: ::std::io::Error::last_os_error
/// [raw_os_error]: ::std::io::Error::raw_os_error
#[cfg_attr(miri, track_caller)]
#[inline]
unsafe fn rezalloc(
&self,
ptr: NonNull<u8>,
old_layout: Layout,
new_layout: Layout
) -> Result<NonNull<u8>, <Self as AllocError>::Error> {
ralloc(self, ptr, old_layout, new_layout, Bytes::Zeroed)
}
}
/// A memory allocation interface which can allocate and deallocate.
///
/// This exists solely because it reads more nicely than <code>A: [Dealloc]</code>; the two are the
/// same.
pub trait BasicAlloc: Alloc + Dealloc {}
/// A memory allocation interface which can allocate, deallocate, and arbitrarily resize
/// allocations.
///
/// This exists solely because it reads more nicely than <code>A: [Realloc]</code>; the two are the
/// same.
pub trait FullAlloc: Realloc + Grow + Shrink + Alloc + Dealloc {}
impl<A: Alloc + Dealloc> BasicAlloc for A {}
impl<A: Realloc + Grow + Shrink + Alloc + Dealloc> FullAlloc for A {}
macro_rules! impl_alloc_ref {
($($t:ty),+) => {
$(
#[allow(unused_qualifications)]
impl<A: AllocError + ?Sized> AllocError for $t {
type Error = A::Error;
}
#[allow(unused_qualifications)]
impl<A: Alloc + ?Sized> Alloc for $t {
#[cfg_attr(miri, track_caller)]
#[inline(always)]
fn alloc(
&self,
layout: Layout
) -> Result<NonNull<u8>, <$t as AllocError>::Error> {
(**self).alloc(layout)
}
#[cfg_attr(miri, track_caller)]
#[inline(always)]
fn zalloc(
&self,
layout: Layout
) -> Result<NonNull<u8>, <$t as AllocError>::Error> {
(**self).zalloc(layout)
}
}
#[allow(unused_qualifications)]
impl<A: Dealloc + ?Sized> Dealloc for $t {
#[cfg_attr(miri, track_caller)]
#[inline(always)]
unsafe fn dealloc(&self, ptr: NonNull<u8>, layout: Layout) {
(**self).dealloc(ptr, layout);
}
unsafe fn try_dealloc(
&self,
ptr: NonNull<u8>,
layout: Layout
) -> Result<(), <$t as AllocError>::Error> {
(**self).try_dealloc(ptr, layout)
}
}
#[allow(unused_qualifications)]
impl<A: Grow + ?Sized> Grow for $t {
#[cfg_attr(miri, track_caller)]
#[inline(always)]
unsafe fn grow(
&self,
ptr: NonNull<u8>,
old_layout: Layout,
new_layout: Layout
) -> Result<NonNull<u8>, <$t as AllocError>::Error> {
(**self).grow(ptr, old_layout, new_layout)
}
#[cfg_attr(miri, track_caller)]
#[inline(always)]
unsafe fn zgrow(
&self,
ptr: NonNull<u8>,
old_layout: Layout,
new_layout: Layout
) -> Result<NonNull<u8>, <$t as AllocError>::Error> {
(**self).zgrow(ptr, old_layout, new_layout)
}
}
#[allow(unused_qualifications)]
impl<A: Shrink + ?Sized> Shrink for $t {
#[cfg_attr(miri, track_caller)]
#[inline(always)]
unsafe fn shrink(
&self,
ptr: NonNull<u8>,
old_layout: Layout,
new_layout: Layout
) -> Result<NonNull<u8>, <$t as AllocError>::Error> {
(**self).shrink(ptr, old_layout, new_layout)
}
}
#[allow(unused_qualifications)]
impl<A: Realloc + ?Sized> Realloc for $t {
#[cfg_attr(miri, track_caller)]
#[inline(always)]
unsafe fn realloc(
&self,
ptr: NonNull<u8>,
old_layout: Layout,
new_layout: Layout
) -> Result<NonNull<u8>, <$t as AllocError>::Error> {
(**self).realloc(ptr, old_layout, new_layout)
}
#[cfg_attr(miri, track_caller)]
#[inline(always)]
unsafe fn rezalloc(
&self,
ptr: NonNull<u8>,
old_layout: Layout,
new_layout: Layout
) -> Result<NonNull<u8>, <$t as AllocError>::Error> {
(**self).rezalloc(ptr, old_layout, new_layout)
}
}
)+
};
}
impl_alloc_ref! { &A, &mut A }
#[cfg(all(feature = "std", not(feature = "no_alloc")))]
impl_alloc_ref! { ::stdalloc::boxed::Box<A>, ::stdalloc::rc::Rc<A>, ::stdalloc::sync::Arc<A> }
#[cfg(all(feature = "std", not(feature = "no_alloc")))]
macro_rules! sysalloc {
($self:ident, $alloc:ident, $layout:ident) => {
crate::helpers::null_q_dyn_zsl_check(
$layout,
// SAFETY: System::$alloc is only called after the layout is verified non-zero-sized.
|layout| unsafe { ::stdalloc::alloc::GlobalAlloc::$alloc($self, layout.to_stdlib()) }
)
};
}
#[cfg(all(feature = "std", not(feature = "no_alloc")))]
impl AllocError for ::std::alloc::System {
type Error = Error;
}
#[cfg(all(feature = "std", not(feature = "no_alloc")))]
impl Alloc for ::std::alloc::System {
#[cfg_attr(miri, track_caller)]
#[inline]
fn alloc(&self, layout: Layout) -> Result<NonNull<u8>, Error> {
sysalloc!(self, alloc, layout)
}
#[cfg_attr(miri, track_caller)]
#[inline]
fn zalloc(&self, layout: Layout) -> Result<NonNull<u8>, Error> {
sysalloc!(self, alloc_zeroed, layout)
}
}
#[cfg(all(feature = "std", not(feature = "no_alloc")))]
impl Dealloc for ::std::alloc::System {
unsafe fn try_dealloc(&self, ptr: NonNull<u8>, layout: Layout) -> Result<(), Error> {
if layout.is_zero_sized() {
Err(Error::ZeroSizedLayout)
} else if ptr == layout.dangling() {
Err(Error::DanglingDeallocation)
} else {
::stdalloc::alloc::GlobalAlloc::dealloc(self, ptr.as_ptr(), layout.to_stdlib());
::core::result::Result::Ok(())
}
}
}
#[cfg(all(feature = "std", not(feature = "no_alloc")))]
impl Grow for ::std::alloc::System {}
#[cfg(all(feature = "std", not(feature = "no_alloc")))]
impl Shrink for ::std::alloc::System {}
#[cfg(all(feature = "std", not(feature = "no_alloc")))]
impl Realloc for ::std::alloc::System {}