Skip to content

Commit

Permalink
Auto merge of rust-lang#79286 - TimDiekmann:rename-allocref, r=Lokath…
Browse files Browse the repository at this point in the history
…or,Wodann,m-ou-se

Rename `AllocRef` to `Allocator` and `(de)alloc` to `(de)allocate`

Calling `Box::alloc_ref` and `Vec::alloc_ref` sounds like allocating a reference. To solve this ambiguity, this renames `AllocRef` to `Allocator` and `alloc` to `allocate`. For a more detailed explaination see rust-lang/wg-allocators#76.

closes rust-lang/wg-allocators#76

r? `@KodrAus`
`@rustbot` modify labels: +A-allocators +T-libs
`@rustbot` ping wg-allocators
  • Loading branch information
bors committed Dec 4, 2020
2 parents 2218520 + 9274b37 commit 3ff10e7
Show file tree
Hide file tree
Showing 27 changed files with 337 additions and 335 deletions.
40 changes: 20 additions & 20 deletions library/alloc/src/alloc.rs
Expand Up @@ -38,7 +38,7 @@ extern "Rust" {

/// The global memory allocator.
///
/// This type implements the [`AllocRef`] trait by forwarding calls
/// This type implements the [`Allocator`] trait by forwarding calls
/// to the allocator registered with the `#[global_allocator]` attribute
/// if there is one, or the `std` crate’s default.
///
Expand All @@ -59,7 +59,7 @@ pub use std::alloc::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 [`AllocRef`] trait become stable.
/// of the [`Global`] type when it and the [`Allocator`] trait become stable.
///
/// # Safety
///
Expand Down Expand Up @@ -93,7 +93,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 [`AllocRef`] trait become stable.
/// of the [`Global`] type when it and the [`Allocator`] trait become stable.
///
/// # Safety
///
Expand All @@ -111,7 +111,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 [`AllocRef`] trait become stable.
/// of the [`Global`] type when it and the [`Allocator`] trait become stable.
///
/// # Safety
///
Expand All @@ -129,7 +129,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 [`AllocRef`] trait become stable.
/// of the [`Global`] type when it and the [`Allocator`] trait become stable.
///
/// # Safety
///
Expand Down Expand Up @@ -170,7 +170,7 @@ impl Global {
}
}

// SAFETY: Same as `AllocRef::grow`
// SAFETY: Same as `Allocator::grow`
#[inline]
unsafe fn grow_impl(
&self,
Expand Down Expand Up @@ -211,7 +211,7 @@ impl Global {
old_size => unsafe {
let new_ptr = self.alloc_impl(new_layout, zeroed)?;
ptr::copy_nonoverlapping(ptr.as_ptr(), new_ptr.as_mut_ptr(), old_size);
self.dealloc(ptr, old_layout);
self.deallocate(ptr, old_layout);
Ok(new_ptr)
},
}
Expand All @@ -220,19 +220,19 @@ impl Global {

#[unstable(feature = "allocator_api", issue = "32838")]
#[cfg(not(test))]
unsafe impl AllocRef for Global {
unsafe impl Allocator for Global {
#[inline]
fn alloc(&self, layout: Layout) -> Result<NonNull<[u8]>, AllocError> {
fn allocate(&self, layout: Layout) -> Result<NonNull<[u8]>, AllocError> {
self.alloc_impl(layout, false)
}

#[inline]
fn alloc_zeroed(&self, layout: Layout) -> Result<NonNull<[u8]>, AllocError> {
fn allocate_zeroed(&self, layout: Layout) -> Result<NonNull<[u8]>, AllocError> {
self.alloc_impl(layout, true)
}

#[inline]
unsafe fn dealloc(&self, ptr: NonNull<u8>, layout: Layout) {
unsafe fn deallocate(&self, ptr: NonNull<u8>, layout: Layout) {
if layout.size() != 0 {
// SAFETY: `layout` is non-zero in size,
// other conditions must be upheld by the caller
Expand Down Expand Up @@ -277,7 +277,7 @@ unsafe impl AllocRef for Global {
match new_layout.size() {
// SAFETY: conditions must be upheld by the caller
0 => unsafe {
self.dealloc(ptr, old_layout);
self.deallocate(ptr, old_layout);
Ok(NonNull::slice_from_raw_parts(new_layout.dangling(), 0))
},

Expand All @@ -297,9 +297,9 @@ unsafe impl AllocRef for Global {
// `new_ptr`. Thus, the call to `copy_nonoverlapping` is safe. The safety contract
// for `dealloc` must be upheld by the caller.
new_size => unsafe {
let new_ptr = self.alloc(new_layout)?;
let new_ptr = self.allocate(new_layout)?;
ptr::copy_nonoverlapping(ptr.as_ptr(), new_ptr.as_mut_ptr(), new_size);
self.dealloc(ptr, old_layout);
self.deallocate(ptr, old_layout);
Ok(new_ptr)
},
}
Expand All @@ -313,7 +313,7 @@ unsafe impl AllocRef for Global {
#[inline]
unsafe fn exchange_malloc(size: usize, align: usize) -> *mut u8 {
let layout = unsafe { Layout::from_size_align_unchecked(size, align) };
match Global.alloc(layout) {
match Global.allocate(layout) {
Ok(ptr) => ptr.as_mut_ptr(),
Err(_) => handle_alloc_error(layout),
}
Expand All @@ -322,16 +322,16 @@ unsafe fn exchange_malloc(size: usize, align: usize) -> *mut u8 {
#[cfg_attr(not(test), lang = "box_free")]
#[inline]
// This signature has to be the same as `Box`, otherwise an ICE will happen.
// When an additional parameter to `Box` is added (like `A: AllocRef`), this has to be added here as
// When an additional parameter to `Box` is added (like `A: Allocator`), this has to be added here as
// well.
// For example if `Box` is changed to `struct Box<T: ?Sized, A: AllocRef>(Unique<T>, A)`,
// this function has to be changed to `fn box_free<T: ?Sized, A: AllocRef>(Unique<T>, A)` as well.
pub(crate) unsafe fn box_free<T: ?Sized, A: AllocRef>(ptr: Unique<T>, alloc: A) {
// For example if `Box` is changed to `struct Box<T: ?Sized, A: Allocator>(Unique<T>, A)`,
// this function has to be changed to `fn box_free<T: ?Sized, A: Allocator>(Unique<T>, A)` as well.
pub(crate) unsafe fn box_free<T: ?Sized, A: Allocator>(ptr: Unique<T>, alloc: A) {
unsafe {
let size = size_of_val(ptr.as_ref());
let align = min_align_of_val(ptr.as_ref());
let layout = Layout::from_size_align_unchecked(size, align);
alloc.dealloc(ptr.cast().into(), layout)
alloc.deallocate(ptr.cast().into(), layout)
}
}

Expand Down
4 changes: 2 additions & 2 deletions library/alloc/src/alloc/tests.rs
Expand Up @@ -9,15 +9,15 @@ fn allocate_zeroed() {
unsafe {
let layout = Layout::from_size_align(1024, 1).unwrap();
let ptr =
Global.alloc_zeroed(layout.clone()).unwrap_or_else(|_| handle_alloc_error(layout));
Global.allocate_zeroed(layout.clone()).unwrap_or_else(|_| handle_alloc_error(layout));

let mut i = ptr.as_non_null_ptr().as_ptr();
let end = i.add(layout.size());
while i < end {
assert_eq!(*i, 0);
i = i.offset(1);
}
Global.dealloc(ptr.as_non_null_ptr(), layout);
Global.deallocate(ptr.as_non_null_ptr(), layout);
}
}

Expand Down

0 comments on commit 3ff10e7

Please sign in to comment.