@@ -31,7 +31,7 @@ const fn size_align<T>() -> (usize, usize) {
31
31
///
32
32
/// (Note however that layouts are *not* required to have positive
33
33
/// size, even though many allocators require that all memory
34
- /// requests have positive size. A caller to the `Alloc ::alloc`
34
+ /// requests have positive size. A caller to the `AllocRef ::alloc`
35
35
/// method must either ensure that conditions like this are met, or
36
36
/// use specific allocators with looser requirements.)
37
37
#[ stable( feature = "alloc_layout" , since = "1.28.0" ) ]
@@ -364,8 +364,8 @@ impl fmt::Display for AllocErr {
364
364
/// [`shrink_in_place`] were unable to reuse the given memory block for
365
365
/// a requested layout.
366
366
///
367
- /// [`grow_in_place`]: ./trait.Alloc .html#method.grow_in_place
368
- /// [`shrink_in_place`]: ./trait.Alloc .html#method.shrink_in_place
367
+ /// [`grow_in_place`]: ./trait.AllocRef .html#method.grow_in_place
368
+ /// [`shrink_in_place`]: ./trait.AllocRef .html#method.shrink_in_place
369
369
#[ unstable( feature = "allocator_api" , issue = "32838" ) ]
370
370
#[ derive( Clone , PartialEq , Eq , Debug ) ]
371
371
pub struct CannotReallocInPlace ;
@@ -580,9 +580,14 @@ pub unsafe trait GlobalAlloc {
580
580
}
581
581
}
582
582
583
- /// An implementation of `Alloc ` can allocate, reallocate, and
583
+ /// An implementation of `AllocRef ` can allocate, reallocate, and
584
584
/// deallocate arbitrary blocks of data described via `Layout`.
585
585
///
586
+ /// `AllocRef` is designed to be implemented on ZSTs, references, or
587
+ /// smart pointers because having an allocator like `MyAlloc([u8; N])`
588
+ /// cannot be moved, without updating the pointers to the allocated
589
+ /// memory.
590
+ ///
586
591
/// Some of the methods require that a memory block be *currently
587
592
/// allocated* via an allocator. This means that:
588
593
///
@@ -598,15 +603,15 @@ pub unsafe trait GlobalAlloc {
598
603
/// passed to a reallocation method (see above) that returns `Ok`.
599
604
///
600
605
/// A note regarding zero-sized types and zero-sized layouts: many
601
- /// methods in the `Alloc ` trait state that allocation requests
606
+ /// methods in the `AllocRef ` trait state that allocation requests
602
607
/// must be non-zero size, or else undefined behavior can result.
603
608
///
604
609
/// * However, some higher-level allocation methods (`alloc_one`,
605
610
/// `alloc_array`) are well-defined on zero-sized types and can
606
611
/// optionally support them: it is left up to the implementor
607
612
/// whether to return `Err`, or to return `Ok` with some pointer.
608
613
///
609
- /// * If an `Alloc ` implementation chooses to return `Ok` in this
614
+ /// * If an `AllocRef ` implementation chooses to return `Ok` in this
610
615
/// case (i.e., the pointer denotes a zero-sized inaccessible block)
611
616
/// then that returned pointer must be considered "currently
612
617
/// allocated". On such an allocator, *all* methods that take
@@ -646,21 +651,24 @@ pub unsafe trait GlobalAlloc {
646
651
///
647
652
/// # Safety
648
653
///
649
- /// The `Alloc ` trait is an `unsafe` trait for a number of reasons, and
654
+ /// The `AllocRef ` trait is an `unsafe` trait for a number of reasons, and
650
655
/// implementors must ensure that they adhere to these contracts:
651
656
///
652
657
/// * Pointers returned from allocation functions must point to valid memory and
653
- /// retain their validity until at least the instance of `Alloc ` is dropped
658
+ /// retain their validity until at least one instance of `AllocRef ` is dropped
654
659
/// itself.
655
660
///
661
+ /// * Cloning or moving the allocator must not invalidate pointers returned
662
+ /// from this allocator. Cloning must return a reference to the same allocator.
663
+ ///
656
664
/// * `Layout` queries and calculations in general must be correct. Callers of
657
665
/// this trait are allowed to rely on the contracts defined on each method,
658
666
/// and implementors must ensure such contracts remain true.
659
667
///
660
668
/// Note that this list may get tweaked over time as clarifications are made in
661
669
/// the future.
662
670
#[ unstable( feature = "allocator_api" , issue = "32838" ) ]
663
- pub unsafe trait Alloc {
671
+ pub unsafe trait AllocRef {
664
672
// (Note: some existing allocators have unspecified but well-defined
665
673
// behavior in response to a zero size allocation request ;
666
674
// e.g., in C, `malloc` of 0 will either return a null pointer or a
@@ -1042,7 +1050,7 @@ pub unsafe trait Alloc {
1042
1050
/// must be considered "currently allocated" and must be
1043
1051
/// acceptable input to methods such as `realloc` or `dealloc`,
1044
1052
/// *even if* `T` is a zero-sized type. In other words, if your
1045
- /// `Alloc ` implementation overrides this method in a manner
1053
+ /// `AllocRef ` implementation overrides this method in a manner
1046
1054
/// that can return a zero-sized `ptr`, then all reallocation and
1047
1055
/// deallocation methods need to be similarly overridden to accept
1048
1056
/// such values as input.
@@ -1106,7 +1114,7 @@ pub unsafe trait Alloc {
1106
1114
/// must be considered "currently allocated" and must be
1107
1115
/// acceptable input to methods such as `realloc` or `dealloc`,
1108
1116
/// *even if* `T` is a zero-sized type. In other words, if your
1109
- /// `Alloc ` implementation overrides this method in a manner
1117
+ /// `AllocRef ` implementation overrides this method in a manner
1110
1118
/// that can return a zero-sized `ptr`, then all reallocation and
1111
1119
/// deallocation methods need to be similarly overridden to accept
1112
1120
/// such values as input.
@@ -1219,3 +1227,10 @@ pub unsafe trait Alloc {
1219
1227
}
1220
1228
}
1221
1229
}
1230
+
1231
+ // In order to rename `Alloc` to `AllocRef`, some submoduleshas to be updated as well. The CI fails
1232
+ // if either of the submodules fails to compile. The submodules have their own CI depending on a
1233
+ // specific Rust version, which don't have `AllocRef` yet. This alias is used to make the submodules
1234
+ // compile and pass the CI.
1235
+ #[ unstable( feature = "allocator_api" , issue = "32838" ) ]
1236
+ pub use self :: AllocRef as Alloc ;
0 commit comments