Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Separate dealloc from AllocRef into DeallocRef #69065

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions src/liballoc/alloc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,15 +163,18 @@ pub unsafe fn alloc_zeroed(layout: Layout) -> *mut u8 {
}

#[unstable(feature = "allocator_api", issue = "32838")]
unsafe impl AllocRef for Global {
unsafe impl DeallocRef for Global {
#[inline]
unsafe fn alloc(&mut self, layout: Layout) -> Result<NonNull<u8>, AllocErr> {
NonNull::new(alloc(layout)).ok_or(AllocErr)
unsafe fn dealloc(&mut self, ptr: NonNull<u8>, layout: Layout) {
dealloc(ptr.as_ptr(), layout)
}
}

#[unstable(feature = "allocator_api", issue = "32838")]
unsafe impl AllocRef for Global {
#[inline]
unsafe fn dealloc(&mut self, ptr: NonNull<u8>, layout: Layout) {
dealloc(ptr.as_ptr(), layout)
unsafe fn alloc(&mut self, layout: Layout) -> Result<NonNull<u8>, AllocErr> {
NonNull::new(alloc(layout)).ok_or(AllocErr)
}

#[inline]
Expand Down
2 changes: 1 addition & 1 deletion src/liballoc/collections/btree/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ use core::mem::{self, MaybeUninit};
use core::ptr::{self, NonNull, Unique};
use core::slice;

use crate::alloc::{AllocRef, Global, Layout};
use crate::alloc::{DeallocRef, Global, Layout};
use crate::boxed::Box;

const B: usize = 6;
Expand Down
18 changes: 11 additions & 7 deletions src/liballoc/raw_vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use core::ops::Drop;
use core::ptr::{self, NonNull, Unique};
use core::slice;

use crate::alloc::{handle_alloc_error, AllocErr, AllocRef, Global, Layout};
use crate::alloc::{handle_alloc_error, AllocErr, AllocRef, DeallocRef, Global, Layout};
use crate::boxed::Box;
use crate::collections::TryReserveError::{self, *};

Expand Down Expand Up @@ -42,13 +42,13 @@ mod tests;
/// field. This allows zero-sized types to not be special-cased by consumers of
/// this type.
#[allow(missing_debug_implementations)]
pub struct RawVec<T, A: AllocRef = Global> {
pub struct RawVec<T, A: DeallocRef = Global> {
ptr: Unique<T>,
cap: usize,
a: A,
}

impl<T, A: AllocRef> RawVec<T, A> {
impl<T, A: DeallocRef> RawVec<T, A> {
/// Like `new`, but parameterized over the choice of allocator for
/// the returned `RawVec`.
pub const fn new_in(a: A) -> Self {
Expand All @@ -57,7 +57,9 @@ impl<T, A: AllocRef> RawVec<T, A> {
// `Unique::empty()` doubles as "unallocated" and "zero-sized allocation".
RawVec { ptr: Unique::empty(), cap, a }
}
}

impl<T, A: AllocRef> RawVec<T, A> {
/// Like `with_capacity`, but parameterized over the choice of
/// allocator for the returned `RawVec`.
#[inline]
Expand Down Expand Up @@ -147,7 +149,7 @@ impl<T> RawVec<T, Global> {
}
}

impl<T, A: AllocRef> RawVec<T, A> {
impl<T, A: DeallocRef> RawVec<T, A> {
/// Reconstitutes a `RawVec` from a pointer, capacity, and allocator.
///
/// # Undefined Behavior
Expand Down Expand Up @@ -182,7 +184,7 @@ impl<T> RawVec<T, Global> {
}
}

impl<T, A: AllocRef> RawVec<T, A> {
impl<T, A: DeallocRef> RawVec<T, A> {
/// 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.
Expand Down Expand Up @@ -221,7 +223,9 @@ impl<T, A: AllocRef> RawVec<T, A> {
}
}
}
}

impl<T, A: AllocRef> RawVec<T, A> {
/// Doubles the size of the type's backing allocation. This is common enough
/// to want to do that it's easiest to just have a dedicated method. Slightly
/// more efficient logic can be provided for this than the general case.
Expand Down Expand Up @@ -700,7 +704,7 @@ impl<T> RawVec<T, Global> {
}
}

impl<T, A: AllocRef> RawVec<T, A> {
impl<T, A: DeallocRef> RawVec<T, A> {
/// 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::<T>();
Expand All @@ -712,7 +716,7 @@ impl<T, A: AllocRef> RawVec<T, A> {
}
}

unsafe impl<#[may_dangle] T, A: AllocRef> Drop for RawVec<T, A> {
unsafe impl<#[may_dangle] T, A: DeallocRef> Drop for RawVec<T, A> {
/// Frees the memory owned by the `RawVec` *without* trying to drop its contents.
fn drop(&mut self) {
unsafe {
Expand Down
8 changes: 5 additions & 3 deletions src/liballoc/raw_vec/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ fn allocator_param() {
struct BoundedAlloc {
fuel: usize,
}
unsafe impl DeallocRef for BoundedAlloc {
unsafe fn dealloc(&mut self, ptr: NonNull<u8>, layout: Layout) {
Global.dealloc(ptr, layout)
}
}
unsafe impl AllocRef for BoundedAlloc {
unsafe fn alloc(&mut self, layout: Layout) -> Result<NonNull<u8>, AllocErr> {
let size = layout.size();
Expand All @@ -33,9 +38,6 @@ fn allocator_param() {
err @ Err(_) => err,
}
}
unsafe fn dealloc(&mut self, ptr: NonNull<u8>, layout: Layout) {
Global.dealloc(ptr, layout)
}
}

let a = BoundedAlloc { fuel: 500 };
Expand Down
2 changes: 1 addition & 1 deletion src/liballoc/rc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ use core::ptr::{self, NonNull};
use core::slice::{self, from_raw_parts_mut};
use core::usize;

use crate::alloc::{box_free, handle_alloc_error, AllocRef, Global, Layout};
use crate::alloc::{box_free, handle_alloc_error, AllocRef, DeallocRef, Global, Layout};
use crate::string::String;
use crate::vec::Vec;

Expand Down
2 changes: 1 addition & 1 deletion src/liballoc/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use core::sync::atomic;
use core::sync::atomic::Ordering::{Acquire, Relaxed, Release, SeqCst};
use core::{isize, usize};

use crate::alloc::{box_free, handle_alloc_error, AllocRef, Global, Layout};
use crate::alloc::{box_free, handle_alloc_error, AllocRef, DeallocRef, Global, Layout};
use crate::boxed::Box;
use crate::rc::is_dangling;
use crate::string::String;
Expand Down
39 changes: 21 additions & 18 deletions src/libcore/alloc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -580,6 +580,26 @@ pub unsafe trait GlobalAlloc {
}
}

/// An implementation of `DeallocRef` can deallocate arbitrary blocks of
/// data described via `Layout`.
///
/// Please see the documentation of [`AllocRef`][] for more information.
#[unstable(feature = "allocator_api", issue = "32838")]
pub unsafe trait DeallocRef {
/// Deallocate the memory referenced by `ptr`.
///
/// # Safety
///
/// This function is unsafe because undefined behavior can result
/// if the caller does not ensure all of the following:
///
/// * `ptr` must denote a block of memory owned by this allocator,
/// * `layout` must *fit* that block of memory,
/// * the alignment of the `layout` must match the alignment used
/// to allocate that block of memory.
unsafe fn dealloc(&mut self, ptr: NonNull<u8>, layout: Layout);
}

/// An implementation of `AllocRef` can allocate, reallocate, and
/// deallocate arbitrary blocks of data described via `Layout`.
///
Expand Down Expand Up @@ -668,7 +688,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 AllocRef {
pub unsafe trait AllocRef: DeallocRef {
// (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
Expand Down Expand Up @@ -717,23 +737,6 @@ pub unsafe trait AllocRef {
/// [`handle_alloc_error`]: ../../alloc/alloc/fn.handle_alloc_error.html
unsafe fn alloc(&mut self, layout: Layout) -> Result<NonNull<u8>, AllocErr>;

/// Deallocate the memory referenced by `ptr`.
///
/// # Safety
///
/// This function is unsafe because undefined behavior can result
/// if the caller does not ensure all of the following:
///
/// * `ptr` must denote a block of memory currently allocated via
/// this allocator,
///
/// * `layout` must *fit* that block of memory,
///
/// * In addition to fitting the block of memory `layout`, the
/// alignment of the `layout` must match the alignment used
/// to allocate that block of memory.
unsafe fn dealloc(&mut self, ptr: NonNull<u8>, layout: Layout);

// == ALLOCATOR-SPECIFIC QUANTITIES AND LIMITS ==
// usable_size

Expand Down
15 changes: 9 additions & 6 deletions src/libstd/alloc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,15 @@ pub use alloc_crate::alloc::*;
#[derive(Debug, Default, Copy, Clone)]
pub struct System;

// The AllocRef impl just forwards to the GlobalAlloc impl, which is in `std::sys::*::alloc`.
// The (De-)allocRef impl just forwards to the GlobalAlloc impl, which is in `std::sys::*::alloc`.
#[unstable(feature = "allocator_api", issue = "32838")]
unsafe impl DeallocRef for System {
#[inline]
unsafe fn dealloc(&mut self, ptr: NonNull<u8>, layout: Layout) {
GlobalAlloc::dealloc(self, ptr.as_ptr(), layout)
}
}

#[unstable(feature = "allocator_api", issue = "32838")]
unsafe impl AllocRef for System {
#[inline]
Expand All @@ -146,11 +154,6 @@ unsafe impl AllocRef for System {
NonNull::new(GlobalAlloc::alloc_zeroed(self, layout)).ok_or(AllocErr)
}

#[inline]
unsafe fn dealloc(&mut self, ptr: NonNull<u8>, layout: Layout) {
GlobalAlloc::dealloc(self, ptr.as_ptr(), layout)
}

#[inline]
unsafe fn realloc(
&mut self,
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/allocator/custom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

extern crate helper;

use std::alloc::{self, Global, AllocRef, System, Layout};
use std::alloc::{self, Global, AllocRef, DeallocRef, System, Layout};
use std::sync::atomic::{AtomicUsize, Ordering};

static HITS: AtomicUsize = AtomicUsize::new(0);
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/allocator/xcrate-use.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
extern crate custom;
extern crate helper;

use std::alloc::{Global, AllocRef, System, Layout};
use std::alloc::{Global, AllocRef, DeallocRef, System, Layout};
use std::sync::atomic::{Ordering, AtomicUsize};

#[global_allocator]
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/realloc-16687.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

#![feature(allocator_api)]

use std::alloc::{Global, AllocRef, Layout, handle_alloc_error};
use std::alloc::{Global, AllocRef, DeallocRef, Layout, handle_alloc_error};
use std::ptr::{self, NonNull};

fn main() {
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/regions/regions-mock-codegen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

#![feature(allocator_api)]

use std::alloc::{AllocRef, Global, Layout, handle_alloc_error};
use std::alloc::{AllocRef, DeallocRef, Global, Layout, handle_alloc_error};
use std::ptr::NonNull;

struct arena(());
Expand Down