Skip to content

Commit b181835

Browse files
committed
Auto merge of #68529 - TimDiekmann:rename-alloc, r=Amanieu
Rename `Alloc` to `AllocRef` The allocator-wg has decided to merge this change upstream in rust-lang/wg-allocators#8 (comment). This renames `Alloc` to `AllocRef` because types that implement `Alloc` are a reference, smart pointer, or ZSTs. It is not possible to have an allocator like `MyAlloc([u8; N])`, that owns the memory and also implements `Alloc`, since that would mean, that moving a `Vec<T, MyAlloc>` would need to correct the internal pointer, which is not possible as we don't have move constructors. For further explanation please see rust-lang/wg-allocators#8 (comment) and the comments after that one. Additionally it clarifies the semantics of `Clone` on an allocator. In the case of `AllocRef`, it is clear that the cloned handle still points to the same allocator instance, and that you can free data allocated from one handle with another handle. The initial proposal was to rename `Alloc` to `AllocHandle`, but `Ref` expresses the semantics better than `Handle`. Also, the only appearance of `Handle` in `std` are for windows specific resources, which might be confusing. Blocked on rust-lang/miri#1160
2 parents 8201866 + 7ca25db commit b181835

File tree

16 files changed

+60
-45
lines changed

16 files changed

+60
-45
lines changed

src/doc/unstable-book/src/library-features/allocator-api.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ The tracking issue for this feature is [#32838]
99
Sometimes you want the memory for one collection to use a different
1010
allocator than the memory for another collection. In this case,
1111
replacing the global allocator is not a workable option. Instead,
12-
you need to pass in an instance of an `Alloc` to each collection
12+
you need to pass in an instance of an `AllocRef` to each collection
1313
for which you want a custom allocator.
1414

1515
TBD

src/liballoc/alloc.rs

+11-11
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,14 @@ extern "Rust" {
3131

3232
/// The global memory allocator.
3333
///
34-
/// This type implements the [`Alloc`] trait by forwarding calls
34+
/// This type implements the [`AllocRef`] trait by forwarding calls
3535
/// to the allocator registered with the `#[global_allocator]` attribute
3636
/// if there is one, or the `std` crate’s default.
3737
///
3838
/// Note: while this type is unstable, the functionality it provides can be
3939
/// accessed through the [free functions in `alloc`](index.html#functions).
4040
///
41-
/// [`Alloc`]: trait.Alloc.html
41+
/// [`AllocRef`]: trait.AllocRef.html
4242
#[unstable(feature = "allocator_api", issue = "32838")]
4343
#[derive(Copy, Clone, Default, Debug)]
4444
pub struct Global;
@@ -50,14 +50,14 @@ pub struct Global;
5050
/// if there is one, or the `std` crate’s default.
5151
///
5252
/// This function is expected to be deprecated in favor of the `alloc` method
53-
/// of the [`Global`] type when it and the [`Alloc`] trait become stable.
53+
/// of the [`Global`] type when it and the [`AllocRef`] trait become stable.
5454
///
5555
/// # Safety
5656
///
5757
/// See [`GlobalAlloc::alloc`].
5858
///
5959
/// [`Global`]: struct.Global.html
60-
/// [`Alloc`]: trait.Alloc.html
60+
/// [`AllocRef`]: trait.AllocRef.html
6161
/// [`GlobalAlloc::alloc`]: trait.GlobalAlloc.html#tymethod.alloc
6262
///
6363
/// # Examples
@@ -88,14 +88,14 @@ pub unsafe fn alloc(layout: Layout) -> *mut u8 {
8888
/// if there is one, or the `std` crate’s default.
8989
///
9090
/// This function is expected to be deprecated in favor of the `dealloc` method
91-
/// of the [`Global`] type when it and the [`Alloc`] trait become stable.
91+
/// of the [`Global`] type when it and the [`AllocRef`] trait become stable.
9292
///
9393
/// # Safety
9494
///
9595
/// See [`GlobalAlloc::dealloc`].
9696
///
9797
/// [`Global`]: struct.Global.html
98-
/// [`Alloc`]: trait.Alloc.html
98+
/// [`AllocRef`]: trait.AllocRef.html
9999
/// [`GlobalAlloc::dealloc`]: trait.GlobalAlloc.html#tymethod.dealloc
100100
#[stable(feature = "global_alloc", since = "1.28.0")]
101101
#[inline]
@@ -110,14 +110,14 @@ pub unsafe fn dealloc(ptr: *mut u8, layout: Layout) {
110110
/// if there is one, or the `std` crate’s default.
111111
///
112112
/// This function is expected to be deprecated in favor of the `realloc` method
113-
/// of the [`Global`] type when it and the [`Alloc`] trait become stable.
113+
/// of the [`Global`] type when it and the [`AllocRef`] trait become stable.
114114
///
115115
/// # Safety
116116
///
117117
/// See [`GlobalAlloc::realloc`].
118118
///
119119
/// [`Global`]: struct.Global.html
120-
/// [`Alloc`]: trait.Alloc.html
120+
/// [`AllocRef`]: trait.AllocRef.html
121121
/// [`GlobalAlloc::realloc`]: trait.GlobalAlloc.html#method.realloc
122122
#[stable(feature = "global_alloc", since = "1.28.0")]
123123
#[inline]
@@ -132,14 +132,14 @@ pub unsafe fn realloc(ptr: *mut u8, layout: Layout, new_size: usize) -> *mut u8
132132
/// if there is one, or the `std` crate’s default.
133133
///
134134
/// This function is expected to be deprecated in favor of the `alloc_zeroed` method
135-
/// of the [`Global`] type when it and the [`Alloc`] trait become stable.
135+
/// of the [`Global`] type when it and the [`AllocRef`] trait become stable.
136136
///
137137
/// # Safety
138138
///
139139
/// See [`GlobalAlloc::alloc_zeroed`].
140140
///
141141
/// [`Global`]: struct.Global.html
142-
/// [`Alloc`]: trait.Alloc.html
142+
/// [`AllocRef`]: trait.AllocRef.html
143143
/// [`GlobalAlloc::alloc_zeroed`]: trait.GlobalAlloc.html#method.alloc_zeroed
144144
///
145145
/// # Examples
@@ -163,7 +163,7 @@ pub unsafe fn alloc_zeroed(layout: Layout) -> *mut u8 {
163163
}
164164

165165
#[unstable(feature = "allocator_api", issue = "32838")]
166-
unsafe impl Alloc for Global {
166+
unsafe impl AllocRef for Global {
167167
#[inline]
168168
unsafe fn alloc(&mut self, layout: Layout) -> Result<NonNull<u8>, AllocErr> {
169169
NonNull::new(alloc(layout)).ok_or(AllocErr)

src/liballoc/boxed.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ use core::ptr::{self, NonNull, Unique};
145145
use core::slice;
146146
use core::task::{Context, Poll};
147147

148-
use crate::alloc::{self, Alloc, Global};
148+
use crate::alloc::{self, AllocRef, Global};
149149
use crate::raw_vec::RawVec;
150150
use crate::str::from_boxed_utf8_unchecked;
151151
use crate::vec::Vec;

src/liballoc/collections/btree/node.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ use core::mem::{self, MaybeUninit};
3636
use core::ptr::{self, NonNull, Unique};
3737
use core::slice;
3838

39-
use crate::alloc::{Alloc, Global, Layout};
39+
use crate::alloc::{AllocRef, Global, Layout};
4040
use crate::boxed::Box;
4141

4242
const B: usize = 6;

src/liballoc/raw_vec.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use core::ops::Drop;
77
use core::ptr::{self, NonNull, Unique};
88
use core::slice;
99

10-
use crate::alloc::{handle_alloc_error, Alloc, AllocErr, Global, Layout};
10+
use crate::alloc::{handle_alloc_error, AllocErr, AllocRef, Global, Layout};
1111
use crate::boxed::Box;
1212
use crate::collections::TryReserveError::{self, *};
1313

@@ -42,13 +42,13 @@ mod tests;
4242
/// field. This allows zero-sized types to not be special-cased by consumers of
4343
/// this type.
4444
#[allow(missing_debug_implementations)]
45-
pub struct RawVec<T, A: Alloc = Global> {
45+
pub struct RawVec<T, A: AllocRef = Global> {
4646
ptr: Unique<T>,
4747
cap: usize,
4848
a: A,
4949
}
5050

51-
impl<T, A: Alloc> RawVec<T, A> {
51+
impl<T, A: AllocRef> RawVec<T, A> {
5252
/// Like `new`, but parameterized over the choice of allocator for
5353
/// the returned `RawVec`.
5454
pub const fn new_in(a: A) -> Self {
@@ -147,7 +147,7 @@ impl<T> RawVec<T, Global> {
147147
}
148148
}
149149

150-
impl<T, A: Alloc> RawVec<T, A> {
150+
impl<T, A: AllocRef> RawVec<T, A> {
151151
/// Reconstitutes a `RawVec` from a pointer, capacity, and allocator.
152152
///
153153
/// # Undefined Behavior
@@ -182,7 +182,7 @@ impl<T> RawVec<T, Global> {
182182
}
183183
}
184184

185-
impl<T, A: Alloc> RawVec<T, A> {
185+
impl<T, A: AllocRef> RawVec<T, A> {
186186
/// Gets a raw pointer to the start of the allocation. Note that this is
187187
/// `Unique::empty()` if `capacity == 0` or `T` is zero-sized. In the former case, you must
188188
/// be careful.
@@ -622,7 +622,7 @@ enum ReserveStrategy {
622622

623623
use ReserveStrategy::*;
624624

625-
impl<T, A: Alloc> RawVec<T, A> {
625+
impl<T, A: AllocRef> RawVec<T, A> {
626626
fn reserve_internal(
627627
&mut self,
628628
used_capacity: usize,
@@ -700,7 +700,7 @@ impl<T> RawVec<T, Global> {
700700
}
701701
}
702702

703-
impl<T, A: Alloc> RawVec<T, A> {
703+
impl<T, A: AllocRef> RawVec<T, A> {
704704
/// Frees the memory owned by the `RawVec` *without* trying to drop its contents.
705705
pub unsafe fn dealloc_buffer(&mut self) {
706706
let elem_size = mem::size_of::<T>();
@@ -712,7 +712,7 @@ impl<T, A: Alloc> RawVec<T, A> {
712712
}
713713
}
714714

715-
unsafe impl<#[may_dangle] T, A: Alloc> Drop for RawVec<T, A> {
715+
unsafe impl<#[may_dangle] T, A: AllocRef> Drop for RawVec<T, A> {
716716
/// Frees the memory owned by the `RawVec` *without* trying to drop its contents.
717717
fn drop(&mut self) {
718718
unsafe {

src/liballoc/raw_vec/tests.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ fn allocator_param() {
1919
struct BoundedAlloc {
2020
fuel: usize,
2121
}
22-
unsafe impl Alloc for BoundedAlloc {
22+
unsafe impl AllocRef for BoundedAlloc {
2323
unsafe fn alloc(&mut self, layout: Layout) -> Result<NonNull<u8>, AllocErr> {
2424
let size = layout.size();
2525
if size > self.fuel {

src/liballoc/rc.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ use core::ptr::{self, NonNull};
252252
use core::slice::{self, from_raw_parts_mut};
253253
use core::usize;
254254

255-
use crate::alloc::{box_free, handle_alloc_error, Alloc, Global, Layout};
255+
use crate::alloc::{box_free, handle_alloc_error, AllocRef, Global, Layout};
256256
use crate::string::String;
257257
use crate::vec::Vec;
258258

src/liballoc/sync.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ use core::sync::atomic;
2525
use core::sync::atomic::Ordering::{Acquire, Relaxed, Release, SeqCst};
2626
use core::{isize, usize};
2727

28-
use crate::alloc::{box_free, handle_alloc_error, Alloc, Global, Layout};
28+
use crate::alloc::{box_free, handle_alloc_error, AllocRef, Global, Layout};
2929
use crate::boxed::Box;
3030
use crate::rc::is_dangling;
3131
use crate::string::String;

src/liballoc/tests/heap.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::alloc::{Alloc, Global, Layout, System};
1+
use std::alloc::{AllocRef, Global, Layout, System};
22

33
/// Issue #45955 and #62251.
44
#[test]
@@ -11,7 +11,7 @@ fn std_heap_overaligned_request() {
1111
check_overalign_requests(Global)
1212
}
1313

14-
fn check_overalign_requests<T: Alloc>(mut allocator: T) {
14+
fn check_overalign_requests<T: AllocRef>(mut allocator: T) {
1515
for &align in &[4, 8, 16, 32] {
1616
// less than and bigger than `MIN_ALIGN`
1717
for &size in &[align / 2, align - 1] {

src/libcore/alloc.rs

+26-11
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ const fn size_align<T>() -> (usize, usize) {
3131
///
3232
/// (Note however that layouts are *not* required to have positive
3333
/// 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`
3535
/// method must either ensure that conditions like this are met, or
3636
/// use specific allocators with looser requirements.)
3737
#[stable(feature = "alloc_layout", since = "1.28.0")]
@@ -364,8 +364,8 @@ impl fmt::Display for AllocErr {
364364
/// [`shrink_in_place`] were unable to reuse the given memory block for
365365
/// a requested layout.
366366
///
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
369369
#[unstable(feature = "allocator_api", issue = "32838")]
370370
#[derive(Clone, PartialEq, Eq, Debug)]
371371
pub struct CannotReallocInPlace;
@@ -580,9 +580,14 @@ pub unsafe trait GlobalAlloc {
580580
}
581581
}
582582

583-
/// An implementation of `Alloc` can allocate, reallocate, and
583+
/// An implementation of `AllocRef` can allocate, reallocate, and
584584
/// deallocate arbitrary blocks of data described via `Layout`.
585585
///
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+
///
586591
/// Some of the methods require that a memory block be *currently
587592
/// allocated* via an allocator. This means that:
588593
///
@@ -598,15 +603,15 @@ pub unsafe trait GlobalAlloc {
598603
/// passed to a reallocation method (see above) that returns `Ok`.
599604
///
600605
/// 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
602607
/// must be non-zero size, or else undefined behavior can result.
603608
///
604609
/// * However, some higher-level allocation methods (`alloc_one`,
605610
/// `alloc_array`) are well-defined on zero-sized types and can
606611
/// optionally support them: it is left up to the implementor
607612
/// whether to return `Err`, or to return `Ok` with some pointer.
608613
///
609-
/// * If an `Alloc` implementation chooses to return `Ok` in this
614+
/// * If an `AllocRef` implementation chooses to return `Ok` in this
610615
/// case (i.e., the pointer denotes a zero-sized inaccessible block)
611616
/// then that returned pointer must be considered "currently
612617
/// allocated". On such an allocator, *all* methods that take
@@ -646,21 +651,24 @@ pub unsafe trait GlobalAlloc {
646651
///
647652
/// # Safety
648653
///
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
650655
/// implementors must ensure that they adhere to these contracts:
651656
///
652657
/// * 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
654659
/// itself.
655660
///
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+
///
656664
/// * `Layout` queries and calculations in general must be correct. Callers of
657665
/// this trait are allowed to rely on the contracts defined on each method,
658666
/// and implementors must ensure such contracts remain true.
659667
///
660668
/// Note that this list may get tweaked over time as clarifications are made in
661669
/// the future.
662670
#[unstable(feature = "allocator_api", issue = "32838")]
663-
pub unsafe trait Alloc {
671+
pub unsafe trait AllocRef {
664672
// (Note: some existing allocators have unspecified but well-defined
665673
// behavior in response to a zero size allocation request ;
666674
// e.g., in C, `malloc` of 0 will either return a null pointer or a
@@ -1042,7 +1050,7 @@ pub unsafe trait Alloc {
10421050
/// must be considered "currently allocated" and must be
10431051
/// acceptable input to methods such as `realloc` or `dealloc`,
10441052
/// *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
10461054
/// that can return a zero-sized `ptr`, then all reallocation and
10471055
/// deallocation methods need to be similarly overridden to accept
10481056
/// such values as input.
@@ -1106,7 +1114,7 @@ pub unsafe trait Alloc {
11061114
/// must be considered "currently allocated" and must be
11071115
/// acceptable input to methods such as `realloc` or `dealloc`,
11081116
/// *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
11101118
/// that can return a zero-sized `ptr`, then all reallocation and
11111119
/// deallocation methods need to be similarly overridden to accept
11121120
/// such values as input.
@@ -1219,3 +1227,10 @@ pub unsafe trait Alloc {
12191227
}
12201228
}
12211229
}
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;

src/libstd/alloc.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -133,9 +133,9 @@ pub use alloc_crate::alloc::*;
133133
#[derive(Debug, Default, Copy, Clone)]
134134
pub struct System;
135135

136-
// The Alloc impl just forwards to the GlobalAlloc impl, which is in `std::sys::*::alloc`.
136+
// The AllocRef impl just forwards to the GlobalAlloc impl, which is in `std::sys::*::alloc`.
137137
#[unstable(feature = "allocator_api", issue = "32838")]
138-
unsafe impl Alloc for System {
138+
unsafe impl AllocRef for System {
139139
#[inline]
140140
unsafe fn alloc(&mut self, layout: Layout) -> Result<NonNull<u8>, AllocErr> {
141141
NonNull::new(GlobalAlloc::alloc(self, layout)).ok_or(AllocErr)

src/test/ui/allocator-alloc-one.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
#![feature(allocator_api, nonnull)]
66

7-
use std::alloc::{Alloc, Global, Layout, handle_alloc_error};
7+
use std::alloc::{AllocRef, Global, Layout, handle_alloc_error};
88

99
fn main() {
1010
unsafe {

src/test/ui/allocator/custom.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
extern crate helper;
99

10-
use std::alloc::{self, Global, Alloc, System, Layout};
10+
use std::alloc::{self, Global, AllocRef, System, Layout};
1111
use std::sync::atomic::{AtomicUsize, Ordering};
1212

1313
static HITS: AtomicUsize = AtomicUsize::new(0);

src/test/ui/allocator/xcrate-use.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
extern crate custom;
1010
extern crate helper;
1111

12-
use std::alloc::{Global, Alloc, System, Layout};
12+
use std::alloc::{Global, AllocRef, System, Layout};
1313
use std::sync::atomic::{Ordering, AtomicUsize};
1414

1515
#[global_allocator]

src/test/ui/realloc-16687.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
#![feature(allocator_api)]
88

9-
use std::alloc::{Global, Alloc, Layout, handle_alloc_error};
9+
use std::alloc::{Global, AllocRef, Layout, handle_alloc_error};
1010
use std::ptr::{self, NonNull};
1111

1212
fn main() {

0 commit comments

Comments
 (0)