From cad92e5db0e4746e8feade1616d9acd9468d5a64 Mon Sep 17 00:00:00 2001 From: Simon Sapin Date: Mon, 6 May 2019 22:58:57 +0200 Subject: [PATCH] Rename the Alloc trait to AllocHandle This is a breaking change to an unstable API. Fixes https://github.com/rust-lang/wg-allocators/issues/8 This change has some consensus. Landing it now will hopefully help clarify the vocabulary, including for discussion within the Allocators WG itself. --- src/liballoc/alloc.rs | 14 +++++++------- src/liballoc/collections/btree/node.rs | 2 +- src/liballoc/raw_vec.rs | 18 +++++++++--------- src/liballoc/rc.rs | 2 +- src/liballoc/sync.rs | 2 +- src/liballoc/tests/heap.rs | 4 ++-- src/libcore/alloc.rs | 12 ++++++------ src/libstd/alloc.rs | 2 +- src/test/run-pass/allocator-alloc-one.rs | 2 +- src/test/run-pass/allocator/custom.rs | 2 +- src/test/run-pass/allocator/xcrate-use.rs | 2 +- src/test/run-pass/realloc-16687.rs | 2 +- .../run-pass/regions/regions-mock-codegen.rs | 2 +- 13 files changed, 33 insertions(+), 33 deletions(-) diff --git a/src/liballoc/alloc.rs b/src/liballoc/alloc.rs index ddc6481eec78e..cfc27fd6fa293 100644 --- a/src/liballoc/alloc.rs +++ b/src/liballoc/alloc.rs @@ -31,7 +31,7 @@ extern "Rust" { /// The global memory allocator. /// -/// This type implements the [`Alloc`] trait by forwarding calls +/// This type implements the [`AllocHandle`] trait by forwarding calls /// to the allocator registered with the `#[global_allocator]` attribute /// if there is one, or the `std` crate’s default. /// @@ -48,7 +48,7 @@ pub struct Global; /// 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 [`AllocHandle`] trait become stable. /// /// # Safety /// @@ -82,7 +82,7 @@ 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 [`AllocHandle`] trait become stable. /// /// # Safety /// @@ -100,7 +100,7 @@ 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 [`AllocHandle`] trait become stable. /// /// # Safety /// @@ -118,7 +118,7 @@ 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 [`AllocHandle`] trait become stable. /// /// # Safety /// @@ -145,7 +145,7 @@ pub unsafe fn alloc_zeroed(layout: Layout) -> *mut u8 { } #[unstable(feature = "allocator_api", issue = "32838")] -unsafe impl Alloc for Global { +unsafe impl AllocHandle for Global { #[inline] unsafe fn alloc(&mut self, layout: Layout) -> Result, AllocErr> { NonNull::new(alloc(layout)).ok_or(AllocErr) @@ -232,7 +232,7 @@ mod tests { extern crate test; use test::Bencher; use crate::boxed::Box; - use crate::alloc::{Global, Alloc, Layout, handle_alloc_error}; + use crate::alloc::{Global, AllocHandle, Layout, handle_alloc_error}; #[test] fn allocate_zeroed() { diff --git a/src/liballoc/collections/btree/node.rs b/src/liballoc/collections/btree/node.rs index 581c66c7086a5..4be03180ae0ef 100644 --- a/src/liballoc/collections/btree/node.rs +++ b/src/liballoc/collections/btree/node.rs @@ -36,7 +36,7 @@ use core::mem::{self, MaybeUninit}; use core::ptr::{self, Unique, NonNull}; use core::slice; -use crate::alloc::{Global, Alloc, Layout}; +use crate::alloc::{Global, AllocHandle, Layout}; use crate::boxed::Box; const B: usize = 6; diff --git a/src/liballoc/raw_vec.rs b/src/liballoc/raw_vec.rs index d1fc5ac3b30d4..c717060ac5ffd 100644 --- a/src/liballoc/raw_vec.rs +++ b/src/liballoc/raw_vec.rs @@ -7,7 +7,7 @@ use core::ops::Drop; use core::ptr::{self, NonNull, Unique}; use core::slice; -use crate::alloc::{Alloc, Layout, Global, handle_alloc_error}; +use crate::alloc::{AllocHandle, Layout, Global, handle_alloc_error}; use crate::collections::CollectionAllocErr::{self, *}; use crate::boxed::Box; @@ -39,13 +39,13 @@ use crate::boxed::Box; /// 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 { @@ -146,7 +146,7 @@ impl RawVec { } } -impl RawVec { +impl RawVec { /// Reconstitutes a RawVec from a pointer, capacity, and allocator. /// /// # Undefined Behavior @@ -189,7 +189,7 @@ impl RawVec { } } -impl RawVec { +impl RawVec { /// Gets a raw pointer to the start of the allocation. Note that this is /// Unique::empty() if `cap = 0` or T is zero-sized. In the former case, you must /// be careful. @@ -629,7 +629,7 @@ enum ReserveStrategy { use ReserveStrategy::*; -impl RawVec { +impl RawVec { fn reserve_internal( &mut self, used_cap: usize, @@ -700,7 +700,7 @@ impl RawVec { } } -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 @@ impl RawVec { } } -unsafe impl<#[may_dangle] T, A: Alloc> Drop for RawVec { +unsafe impl<#[may_dangle] T, A: AllocHandle> Drop for RawVec { /// Frees the memory owned by the RawVec *without* trying to Drop its contents. fn drop(&mut self) { unsafe { self.dealloc_buffer(); } @@ -767,7 +767,7 @@ mod tests { // A dumb allocator that consumes a fixed amount of fuel // before allocation attempts start failing. struct BoundedAlloc { fuel: usize } - unsafe impl Alloc for BoundedAlloc { + unsafe impl AllocHandle 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 68eecd97ea11a..34b28b9ba440f 100644 --- a/src/liballoc/rc.rs +++ b/src/liballoc/rc.rs @@ -247,7 +247,7 @@ use core::slice::from_raw_parts_mut; use core::convert::From; use core::usize; -use crate::alloc::{Global, Alloc, Layout, box_free, handle_alloc_error}; +use crate::alloc::{Global, AllocHandle, Layout, box_free, handle_alloc_error}; use crate::string::String; use crate::vec::Vec; diff --git a/src/liballoc/sync.rs b/src/liballoc/sync.rs index 466e806663c7f..53b32651924c7 100644 --- a/src/liballoc/sync.rs +++ b/src/liballoc/sync.rs @@ -23,7 +23,7 @@ use core::{isize, usize}; use core::convert::From; use core::slice::from_raw_parts_mut; -use crate::alloc::{Global, Alloc, Layout, box_free, handle_alloc_error}; +use crate::alloc::{Global, AllocHandle, Layout, box_free, handle_alloc_error}; 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 c225ebfa96b91..917813a629b4e 100644 --- a/src/liballoc/tests/heap.rs +++ b/src/liballoc/tests/heap.rs @@ -1,4 +1,4 @@ -use std::alloc::{Global, Alloc, Layout, System}; +use std::alloc::{Global, AllocHandle, Layout, System}; /// Issue #45955. #[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) { let size = 8; let align = 16; // greater than size let iterations = 100; diff --git a/src/libcore/alloc.rs b/src/libcore/alloc.rs index c124457118cb9..51c54405091c0 100644 --- a/src/libcore/alloc.rs +++ b/src/libcore/alloc.rs @@ -579,7 +579,7 @@ pub unsafe trait GlobalAlloc { } } -/// An implementation of `Alloc` can allocate, reallocate, and +/// A handle to a memory allocator that can allocate, reallocate, and /// deallocate arbitrary blocks of data described via `Layout`. /// /// Some of the methods require that a memory block be *currently @@ -645,11 +645,11 @@ pub unsafe trait GlobalAlloc { /// /// # Safety /// -/// The `Alloc` trait is an `unsafe` trait for a number of reasons, and +/// The `AllocHandle` 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 the instance of `AllocHandle` is dropped /// itself. /// /// * `Layout` queries and calculations in general must be correct. Callers of @@ -659,7 +659,7 @@ pub unsafe trait GlobalAlloc { /// 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 AllocHandle { // (Note: some existing allocators have unspecified but well-defined // behavior in response to a zero size allocation request ; @@ -1046,7 +1046,7 @@ pub unsafe trait Alloc { /// 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 + /// `AllocHandle` 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. @@ -1112,7 +1112,7 @@ pub unsafe trait Alloc { /// 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 + /// `AllocHandle` 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. diff --git a/src/libstd/alloc.rs b/src/libstd/alloc.rs index 4241f47b661d7..ce0c708c2446e 100644 --- a/src/libstd/alloc.rs +++ b/src/libstd/alloc.rs @@ -135,7 +135,7 @@ pub struct System; // The Alloc 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 AllocHandle 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/run-pass/allocator-alloc-one.rs b/src/test/run-pass/allocator-alloc-one.rs index 9011426172374..46e318866008a 100644 --- a/src/test/run-pass/allocator-alloc-one.rs +++ b/src/test/run-pass/allocator-alloc-one.rs @@ -2,7 +2,7 @@ #![feature(allocator_api, nonnull)] -use std::alloc::{Alloc, Global, Layout, handle_alloc_error}; +use std::alloc::{AllocHandle, Global, Layout, handle_alloc_error}; fn main() { unsafe { diff --git a/src/test/run-pass/allocator/custom.rs b/src/test/run-pass/allocator/custom.rs index 71f72ae46c23f..d625916fa7e30 100644 --- a/src/test/run-pass/allocator/custom.rs +++ b/src/test/run-pass/allocator/custom.rs @@ -7,7 +7,7 @@ extern crate helper; -use std::alloc::{self, Global, Alloc, System, Layout}; +use std::alloc::{self, Global, AllocHandle, System, Layout}; use std::sync::atomic::{AtomicUsize, Ordering}; static HITS: AtomicUsize = AtomicUsize::new(0); diff --git a/src/test/run-pass/allocator/xcrate-use.rs b/src/test/run-pass/allocator/xcrate-use.rs index 039c70e77bedf..cdeca8247166a 100644 --- a/src/test/run-pass/allocator/xcrate-use.rs +++ b/src/test/run-pass/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, AllocHandle, System, Layout}; use std::sync::atomic::{Ordering, AtomicUsize}; #[global_allocator] diff --git a/src/test/run-pass/realloc-16687.rs b/src/test/run-pass/realloc-16687.rs index e283d5b6de1db..67b9ec5b8f750 100644 --- a/src/test/run-pass/realloc-16687.rs +++ b/src/test/run-pass/realloc-16687.rs @@ -5,7 +5,7 @@ #![feature(allocator_api)] -use std::alloc::{Global, Alloc, Layout, handle_alloc_error}; +use std::alloc::{Global, AllocHandle, Layout, handle_alloc_error}; use std::ptr::{self, NonNull}; fn main() { diff --git a/src/test/run-pass/regions/regions-mock-codegen.rs b/src/test/run-pass/regions/regions-mock-codegen.rs index 521ef3f6a4b52..71c89df236e05 100644 --- a/src/test/run-pass/regions/regions-mock-codegen.rs +++ b/src/test/run-pass/regions/regions-mock-codegen.rs @@ -6,7 +6,7 @@ #![feature(allocator_api)] -use std::alloc::{Alloc, Global, Layout, handle_alloc_error}; +use std::alloc::{AllocHandle, Global, Layout, handle_alloc_error}; use std::ptr::NonNull; struct arena(());