From 7ca25db8162128808714f536fa993aefcf6a2239 Mon Sep 17 00:00:00 2001 From: Tim Diekmann Date: Thu, 23 Jan 2020 01:49:29 +0100 Subject: [PATCH] Rename `Alloc` to `AllocRef` --- .../src/library-features/allocator-api.md | 2 +- src/liballoc/alloc.rs | 22 +++++------ src/liballoc/boxed.rs | 2 +- src/liballoc/collections/btree/node.rs | 2 +- src/liballoc/raw_vec.rs | 16 ++++---- src/liballoc/raw_vec/tests.rs | 2 +- src/liballoc/rc.rs | 2 +- src/liballoc/sync.rs | 2 +- src/liballoc/tests/heap.rs | 4 +- src/libcore/alloc.rs | 37 +++++++++++++------ src/libstd/alloc.rs | 4 +- src/test/ui/allocator-alloc-one.rs | 2 +- src/test/ui/allocator/custom.rs | 2 +- src/test/ui/allocator/xcrate-use.rs | 2 +- src/test/ui/realloc-16687.rs | 2 +- src/test/ui/regions/regions-mock-codegen.rs | 2 +- 16 files changed, 60 insertions(+), 45 deletions(-) diff --git a/src/doc/unstable-book/src/library-features/allocator-api.md b/src/doc/unstable-book/src/library-features/allocator-api.md index e3969ace7e9..9f045ce08a4 100644 --- a/src/doc/unstable-book/src/library-features/allocator-api.md +++ b/src/doc/unstable-book/src/library-features/allocator-api.md @@ -9,7 +9,7 @@ The tracking issue for this feature is [#32838] Sometimes you want the memory for one collection to use a different allocator than the memory for another collection. In this case, replacing the global allocator is not a workable option. Instead, -you need to pass in an instance of an `Alloc` to each collection +you need to pass in an instance of an `AllocRef` to each collection for which you want a custom allocator. TBD diff --git a/src/liballoc/alloc.rs b/src/liballoc/alloc.rs index 0c0dc928b95..9fb0de63e6f 100644 --- a/src/liballoc/alloc.rs +++ b/src/liballoc/alloc.rs @@ -31,14 +31,14 @@ /// The global memory allocator. /// -/// This type implements the [`Alloc`] trait by forwarding calls +/// This type implements the [`AllocRef`] trait by forwarding calls /// to the allocator registered with the `#[global_allocator]` attribute /// if there is one, or the `std` crate’s default. /// /// Note: while this type is unstable, the functionality it provides can be /// accessed through the [free functions in `alloc`](index.html#functions). /// -/// [`Alloc`]: trait.Alloc.html +/// [`AllocRef`]: trait.AllocRef.html #[unstable(feature = "allocator_api", issue = "32838")] #[derive(Copy, Clone, Default, Debug)] pub struct Global; @@ -50,14 +50,14 @@ /// if there is one, or the `std` crate’s default. /// /// This function is expected to be deprecated in favor of the `alloc` method -/// of the [`Global`] type when it and the [`Alloc`] trait become stable. +/// of the [`Global`] type when it and the [`AllocRef`] trait become stable. /// /// # Safety /// /// See [`GlobalAlloc::alloc`]. /// /// [`Global`]: struct.Global.html -/// [`Alloc`]: trait.Alloc.html +/// [`AllocRef`]: trait.AllocRef.html /// [`GlobalAlloc::alloc`]: trait.GlobalAlloc.html#tymethod.alloc /// /// # Examples @@ -88,14 +88,14 @@ pub unsafe fn alloc(layout: Layout) -> *mut u8 { /// if there is one, or the `std` crate’s default. /// /// This function is expected to be deprecated in favor of the `dealloc` method -/// of the [`Global`] type when it and the [`Alloc`] trait become stable. +/// of the [`Global`] type when it and the [`AllocRef`] trait become stable. /// /// # Safety /// /// See [`GlobalAlloc::dealloc`]. /// /// [`Global`]: struct.Global.html -/// [`Alloc`]: trait.Alloc.html +/// [`AllocRef`]: trait.AllocRef.html /// [`GlobalAlloc::dealloc`]: trait.GlobalAlloc.html#tymethod.dealloc #[stable(feature = "global_alloc", since = "1.28.0")] #[inline] @@ -110,14 +110,14 @@ pub unsafe fn dealloc(ptr: *mut u8, layout: Layout) { /// if there is one, or the `std` crate’s default. /// /// This function is expected to be deprecated in favor of the `realloc` method -/// of the [`Global`] type when it and the [`Alloc`] trait become stable. +/// of the [`Global`] type when it and the [`AllocRef`] trait become stable. /// /// # Safety /// /// See [`GlobalAlloc::realloc`]. /// /// [`Global`]: struct.Global.html -/// [`Alloc`]: trait.Alloc.html +/// [`AllocRef`]: trait.AllocRef.html /// [`GlobalAlloc::realloc`]: trait.GlobalAlloc.html#method.realloc #[stable(feature = "global_alloc", since = "1.28.0")] #[inline] @@ -132,14 +132,14 @@ pub unsafe fn realloc(ptr: *mut u8, layout: Layout, new_size: usize) -> *mut u8 /// if there is one, or the `std` crate’s default. /// /// This function is expected to be deprecated in favor of the `alloc_zeroed` method -/// of the [`Global`] type when it and the [`Alloc`] trait become stable. +/// of the [`Global`] type when it and the [`AllocRef`] trait become stable. /// /// # Safety /// /// See [`GlobalAlloc::alloc_zeroed`]. /// /// [`Global`]: struct.Global.html -/// [`Alloc`]: trait.Alloc.html +/// [`AllocRef`]: trait.AllocRef.html /// [`GlobalAlloc::alloc_zeroed`]: trait.GlobalAlloc.html#method.alloc_zeroed /// /// # Examples @@ -163,7 +163,7 @@ pub unsafe fn alloc_zeroed(layout: Layout) -> *mut u8 { } #[unstable(feature = "allocator_api", issue = "32838")] -unsafe impl Alloc for Global { +unsafe impl AllocRef for Global { #[inline] unsafe fn alloc(&mut self, layout: Layout) -> Result, AllocErr> { NonNull::new(alloc(layout)).ok_or(AllocErr) diff --git a/src/liballoc/boxed.rs b/src/liballoc/boxed.rs index 87a4924f9be..8735c2c8f36 100644 --- a/src/liballoc/boxed.rs +++ b/src/liballoc/boxed.rs @@ -145,7 +145,7 @@ use core::slice; use core::task::{Context, Poll}; -use crate::alloc::{self, Alloc, Global}; +use crate::alloc::{self, AllocRef, Global}; use crate::raw_vec::RawVec; use crate::str::from_boxed_utf8_unchecked; use crate::vec::Vec; diff --git a/src/liballoc/collections/btree/node.rs b/src/liballoc/collections/btree/node.rs index d9cdebb4f73..c0a96e89366 100644 --- a/src/liballoc/collections/btree/node.rs +++ b/src/liballoc/collections/btree/node.rs @@ -36,7 +36,7 @@ use core::ptr::{self, NonNull, Unique}; use core::slice; -use crate::alloc::{Alloc, Global, Layout}; +use crate::alloc::{AllocRef, Global, Layout}; use crate::boxed::Box; const B: usize = 6; diff --git a/src/liballoc/raw_vec.rs b/src/liballoc/raw_vec.rs index 86aed612efe..e1b549bed18 100644 --- a/src/liballoc/raw_vec.rs +++ b/src/liballoc/raw_vec.rs @@ -7,7 +7,7 @@ use core::ptr::{self, NonNull, Unique}; use core::slice; -use crate::alloc::{handle_alloc_error, Alloc, AllocErr, Global, Layout}; +use crate::alloc::{handle_alloc_error, AllocErr, AllocRef, Global, Layout}; use crate::boxed::Box; use crate::collections::TryReserveError::{self, *}; @@ -42,13 +42,13 @@ /// field. This allows zero-sized types to not be special-cased by consumers of /// this type. #[allow(missing_debug_implementations)] -pub struct RawVec { +pub struct RawVec { ptr: Unique, cap: usize, a: A, } -impl RawVec { +impl RawVec { /// Like `new`, but parameterized over the choice of allocator for /// the returned `RawVec`. pub const fn new_in(a: A) -> Self { @@ -147,7 +147,7 @@ pub fn with_capacity_zeroed(capacity: usize) -> Self { } } -impl RawVec { +impl RawVec { /// Reconstitutes a `RawVec` from a pointer, capacity, and allocator. /// /// # Undefined Behavior @@ -182,7 +182,7 @@ pub fn from_box(mut slice: Box<[T]>) -> Self { } } -impl RawVec { +impl RawVec { /// Gets a raw pointer to the start of the allocation. Note that this is /// `Unique::empty()` if `capacity == 0` or `T` is zero-sized. In the former case, you must /// be careful. @@ -622,7 +622,7 @@ enum ReserveStrategy { use ReserveStrategy::*; -impl RawVec { +impl RawVec { fn reserve_internal( &mut self, used_capacity: usize, @@ -700,7 +700,7 @@ pub unsafe fn into_box(self) -> Box<[T]> { } } -impl RawVec { +impl RawVec { /// Frees the memory owned by the `RawVec` *without* trying to drop its contents. pub unsafe fn dealloc_buffer(&mut self) { let elem_size = mem::size_of::(); @@ -712,7 +712,7 @@ pub unsafe fn dealloc_buffer(&mut self) { } } -unsafe impl<#[may_dangle] T, A: Alloc> Drop for RawVec { +unsafe impl<#[may_dangle] T, A: AllocRef> Drop for RawVec { /// Frees the memory owned by the `RawVec` *without* trying to drop its contents. fn drop(&mut self) { unsafe { diff --git a/src/liballoc/raw_vec/tests.rs b/src/liballoc/raw_vec/tests.rs index b214cef3011..63087501f0e 100644 --- a/src/liballoc/raw_vec/tests.rs +++ b/src/liballoc/raw_vec/tests.rs @@ -19,7 +19,7 @@ fn allocator_param() { struct BoundedAlloc { fuel: usize, } - unsafe impl Alloc for BoundedAlloc { + unsafe impl AllocRef for BoundedAlloc { unsafe fn alloc(&mut self, layout: Layout) -> Result, AllocErr> { let size = layout.size(); if size > self.fuel { diff --git a/src/liballoc/rc.rs b/src/liballoc/rc.rs index b176e0f6e2a..9dc5447397f 100644 --- a/src/liballoc/rc.rs +++ b/src/liballoc/rc.rs @@ -252,7 +252,7 @@ use core::slice::{self, from_raw_parts_mut}; use core::usize; -use crate::alloc::{box_free, handle_alloc_error, Alloc, Global, Layout}; +use crate::alloc::{box_free, handle_alloc_error, AllocRef, Global, Layout}; use crate::string::String; use crate::vec::Vec; diff --git a/src/liballoc/sync.rs b/src/liballoc/sync.rs index 4aa0190b149..fd285242d5b 100644 --- a/src/liballoc/sync.rs +++ b/src/liballoc/sync.rs @@ -25,7 +25,7 @@ use core::sync::atomic::Ordering::{Acquire, Relaxed, Release, SeqCst}; use core::{isize, usize}; -use crate::alloc::{box_free, handle_alloc_error, Alloc, Global, Layout}; +use crate::alloc::{box_free, handle_alloc_error, AllocRef, Global, Layout}; use crate::boxed::Box; use crate::rc::is_dangling; use crate::string::String; diff --git a/src/liballoc/tests/heap.rs b/src/liballoc/tests/heap.rs index 43cd7187823..7fcfcf9b294 100644 --- a/src/liballoc/tests/heap.rs +++ b/src/liballoc/tests/heap.rs @@ -1,4 +1,4 @@ -use std::alloc::{Alloc, Global, Layout, System}; +use std::alloc::{AllocRef, Global, Layout, System}; /// Issue #45955 and #62251. #[test] @@ -11,7 +11,7 @@ fn std_heap_overaligned_request() { check_overalign_requests(Global) } -fn check_overalign_requests(mut allocator: T) { +fn check_overalign_requests(mut allocator: T) { for &align in &[4, 8, 16, 32] { // less than and bigger than `MIN_ALIGN` for &size in &[align / 2, align - 1] { diff --git a/src/libcore/alloc.rs b/src/libcore/alloc.rs index 09f743fb81e..1b7dfafbd70 100644 --- a/src/libcore/alloc.rs +++ b/src/libcore/alloc.rs @@ -31,7 +31,7 @@ const fn size_align() -> (usize, usize) { /// /// (Note however that layouts are *not* required to have positive /// size, even though many allocators require that all memory -/// requests have positive size. A caller to the `Alloc::alloc` +/// requests have positive size. A caller to the `AllocRef::alloc` /// method must either ensure that conditions like this are met, or /// use specific allocators with looser requirements.) #[stable(feature = "alloc_layout", since = "1.28.0")] @@ -364,8 +364,8 @@ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { /// [`shrink_in_place`] were unable to reuse the given memory block for /// a requested layout. /// -/// [`grow_in_place`]: ./trait.Alloc.html#method.grow_in_place -/// [`shrink_in_place`]: ./trait.Alloc.html#method.shrink_in_place +/// [`grow_in_place`]: ./trait.AllocRef.html#method.grow_in_place +/// [`shrink_in_place`]: ./trait.AllocRef.html#method.shrink_in_place #[unstable(feature = "allocator_api", issue = "32838")] #[derive(Clone, PartialEq, Eq, Debug)] pub struct CannotReallocInPlace; @@ -580,9 +580,14 @@ unsafe fn realloc(&self, ptr: *mut u8, layout: Layout, new_size: usize) -> *mut } } -/// An implementation of `Alloc` can allocate, reallocate, and +/// An implementation of `AllocRef` can allocate, reallocate, and /// deallocate arbitrary blocks of data described via `Layout`. /// +/// `AllocRef` is designed to be implemented on ZSTs, references, or +/// smart pointers because having an allocator like `MyAlloc([u8; N])` +/// cannot be moved, without updating the pointers to the allocated +/// memory. +/// /// Some of the methods require that a memory block be *currently /// allocated* via an allocator. This means that: /// @@ -598,7 +603,7 @@ unsafe fn realloc(&self, ptr: *mut u8, layout: Layout, new_size: usize) -> *mut /// passed to a reallocation method (see above) that returns `Ok`. /// /// A note regarding zero-sized types and zero-sized layouts: many -/// methods in the `Alloc` trait state that allocation requests +/// methods in the `AllocRef` trait state that allocation requests /// must be non-zero size, or else undefined behavior can result. /// /// * However, some higher-level allocation methods (`alloc_one`, @@ -606,7 +611,7 @@ unsafe fn realloc(&self, ptr: *mut u8, layout: Layout, new_size: usize) -> *mut /// optionally support them: it is left up to the implementor /// whether to return `Err`, or to return `Ok` with some pointer. /// -/// * If an `Alloc` implementation chooses to return `Ok` in this +/// * If an `AllocRef` implementation chooses to return `Ok` in this /// case (i.e., the pointer denotes a zero-sized inaccessible block) /// then that returned pointer must be considered "currently /// allocated". On such an allocator, *all* methods that take @@ -646,13 +651,16 @@ unsafe fn realloc(&self, ptr: *mut u8, layout: Layout, new_size: usize) -> *mut /// /// # Safety /// -/// The `Alloc` trait is an `unsafe` trait for a number of reasons, and +/// The `AllocRef` trait is an `unsafe` trait for a number of reasons, and /// implementors must ensure that they adhere to these contracts: /// /// * Pointers returned from allocation functions must point to valid memory and -/// retain their validity until at least the instance of `Alloc` is dropped +/// retain their validity until at least one instance of `AllocRef` is dropped /// itself. /// +/// * Cloning or moving the allocator must not invalidate pointers returned +/// from this allocator. Cloning must return a reference to the same allocator. +/// /// * `Layout` queries and calculations in general must be correct. Callers of /// this trait are allowed to rely on the contracts defined on each method, /// and implementors must ensure such contracts remain true. @@ -660,7 +668,7 @@ unsafe fn realloc(&self, ptr: *mut u8, layout: Layout, new_size: usize) -> *mut /// Note that this list may get tweaked over time as clarifications are made in /// the future. #[unstable(feature = "allocator_api", issue = "32838")] -pub unsafe trait Alloc { +pub unsafe trait AllocRef { // (Note: some existing allocators have unspecified but well-defined // behavior in response to a zero size allocation request ; // e.g., in C, `malloc` of 0 will either return a null pointer or a @@ -1042,7 +1050,7 @@ unsafe fn shrink_in_place( /// must be considered "currently allocated" and must be /// acceptable input to methods such as `realloc` or `dealloc`, /// *even if* `T` is a zero-sized type. In other words, if your - /// `Alloc` implementation overrides this method in a manner + /// `AllocRef` implementation overrides this method in a manner /// that can return a zero-sized `ptr`, then all reallocation and /// deallocation methods need to be similarly overridden to accept /// such values as input. @@ -1106,7 +1114,7 @@ unsafe fn dealloc_one(&mut self, ptr: NonNull) /// must be considered "currently allocated" and must be /// acceptable input to methods such as `realloc` or `dealloc`, /// *even if* `T` is a zero-sized type. In other words, if your - /// `Alloc` implementation overrides this method in a manner + /// `AllocRef` implementation overrides this method in a manner /// that can return a zero-sized `ptr`, then all reallocation and /// deallocation methods need to be similarly overridden to accept /// such values as input. @@ -1219,3 +1227,10 @@ unsafe fn dealloc_array(&mut self, ptr: NonNull, n: usize) -> Result<(), A } } } + +// In order to rename `Alloc` to `AllocRef`, some submoduleshas to be updated as well. The CI fails +// if either of the submodules fails to compile. The submodules have their own CI depending on a +// specific Rust version, which don't have `AllocRef` yet. This alias is used to make the submodules +// compile and pass the CI. +#[unstable(feature = "allocator_api", issue = "32838")] +pub use self::AllocRef as Alloc; diff --git a/src/libstd/alloc.rs b/src/libstd/alloc.rs index 7fd55af1694..8965c6860c4 100644 --- a/src/libstd/alloc.rs +++ b/src/libstd/alloc.rs @@ -133,9 +133,9 @@ #[derive(Debug, Default, Copy, Clone)] pub struct System; -// The Alloc impl just forwards to the GlobalAlloc impl, which is in `std::sys::*::alloc`. +// The AllocRef impl just forwards to the GlobalAlloc impl, which is in `std::sys::*::alloc`. #[unstable(feature = "allocator_api", issue = "32838")] -unsafe impl Alloc for System { +unsafe impl AllocRef for System { #[inline] unsafe fn alloc(&mut self, layout: Layout) -> Result, AllocErr> { NonNull::new(GlobalAlloc::alloc(self, layout)).ok_or(AllocErr) diff --git a/src/test/ui/allocator-alloc-one.rs b/src/test/ui/allocator-alloc-one.rs index 312d5f13b1a..b821a2c5939 100644 --- a/src/test/ui/allocator-alloc-one.rs +++ b/src/test/ui/allocator-alloc-one.rs @@ -4,7 +4,7 @@ #![feature(allocator_api, nonnull)] -use std::alloc::{Alloc, Global, Layout, handle_alloc_error}; +use std::alloc::{AllocRef, Global, Layout, handle_alloc_error}; fn main() { unsafe { diff --git a/src/test/ui/allocator/custom.rs b/src/test/ui/allocator/custom.rs index 71f72ae46c2..0b1f6d5a96e 100644 --- a/src/test/ui/allocator/custom.rs +++ b/src/test/ui/allocator/custom.rs @@ -7,7 +7,7 @@ extern crate helper; -use std::alloc::{self, Global, Alloc, System, Layout}; +use std::alloc::{self, Global, AllocRef, System, Layout}; use std::sync::atomic::{AtomicUsize, Ordering}; static HITS: AtomicUsize = AtomicUsize::new(0); diff --git a/src/test/ui/allocator/xcrate-use.rs b/src/test/ui/allocator/xcrate-use.rs index 039c70e77be..37b28c195df 100644 --- a/src/test/ui/allocator/xcrate-use.rs +++ b/src/test/ui/allocator/xcrate-use.rs @@ -9,7 +9,7 @@ extern crate custom; extern crate helper; -use std::alloc::{Global, Alloc, System, Layout}; +use std::alloc::{Global, AllocRef, System, Layout}; use std::sync::atomic::{Ordering, AtomicUsize}; #[global_allocator] diff --git a/src/test/ui/realloc-16687.rs b/src/test/ui/realloc-16687.rs index 69292d241c3..425aa83e70a 100644 --- a/src/test/ui/realloc-16687.rs +++ b/src/test/ui/realloc-16687.rs @@ -6,7 +6,7 @@ #![feature(allocator_api)] -use std::alloc::{Global, Alloc, Layout, handle_alloc_error}; +use std::alloc::{Global, AllocRef, Layout, handle_alloc_error}; use std::ptr::{self, NonNull}; fn main() { diff --git a/src/test/ui/regions/regions-mock-codegen.rs b/src/test/ui/regions/regions-mock-codegen.rs index 521ef3f6a4b..f50b1c8b17f 100644 --- a/src/test/ui/regions/regions-mock-codegen.rs +++ b/src/test/ui/regions/regions-mock-codegen.rs @@ -6,7 +6,7 @@ #![feature(allocator_api)] -use std::alloc::{Alloc, Global, Layout, handle_alloc_error}; +use std::alloc::{AllocRef, Global, Layout, handle_alloc_error}; use std::ptr::NonNull; struct arena(());