Replace `Memoryblock` with `NonNull<[u8]>`

This commit is contained in:
Tim Diekmann 2020-08-04 18:03:34 +02:00
parent 5f6bd6ec0a
commit ab9362ad9a
17 changed files with 158 additions and 143 deletions

View File

@ -164,7 +164,7 @@ pub unsafe fn alloc_zeroed(layout: Layout) -> *mut u8 {
#[unstable(feature = "allocator_api", issue = "32838")] #[unstable(feature = "allocator_api", issue = "32838")]
unsafe impl AllocRef for Global { unsafe impl AllocRef for Global {
#[inline] #[inline]
fn alloc(&mut self, layout: Layout) -> Result<MemoryBlock, AllocErr> { fn alloc(&mut self, layout: Layout) -> Result<NonNull<[u8]>, AllocErr> {
let size = layout.size(); let size = layout.size();
let ptr = if size == 0 { let ptr = if size == 0 {
layout.dangling() layout.dangling()
@ -172,11 +172,11 @@ unsafe impl AllocRef for Global {
// SAFETY: `layout` is non-zero in size, // SAFETY: `layout` is non-zero in size,
unsafe { NonNull::new(alloc(layout)).ok_or(AllocErr)? } unsafe { NonNull::new(alloc(layout)).ok_or(AllocErr)? }
}; };
Ok(MemoryBlock { ptr, size }) Ok(NonNull::slice_from_raw_parts(ptr, size))
} }
#[inline] #[inline]
fn alloc_zeroed(&mut self, layout: Layout) -> Result<MemoryBlock, AllocErr> { fn alloc_zeroed(&mut self, layout: Layout) -> Result<NonNull<[u8]>, AllocErr> {
let size = layout.size(); let size = layout.size();
let ptr = if size == 0 { let ptr = if size == 0 {
layout.dangling() layout.dangling()
@ -184,7 +184,7 @@ unsafe impl AllocRef for Global {
// SAFETY: `layout` is non-zero in size, // SAFETY: `layout` is non-zero in size,
unsafe { NonNull::new(alloc_zeroed(layout)).ok_or(AllocErr)? } unsafe { NonNull::new(alloc_zeroed(layout)).ok_or(AllocErr)? }
}; };
Ok(MemoryBlock { ptr, size }) Ok(NonNull::slice_from_raw_parts(ptr, size))
} }
#[inline] #[inline]
@ -202,7 +202,7 @@ unsafe impl AllocRef for Global {
ptr: NonNull<u8>, ptr: NonNull<u8>,
layout: Layout, layout: Layout,
new_size: usize, new_size: usize,
) -> Result<MemoryBlock, AllocErr> { ) -> Result<NonNull<[u8]>, AllocErr> {
debug_assert!( debug_assert!(
new_size >= layout.size(), new_size >= layout.size(),
"`new_size` must be greater than or equal to `layout.size()`" "`new_size` must be greater than or equal to `layout.size()`"
@ -212,14 +212,16 @@ unsafe impl AllocRef for Global {
// Other conditions must be upheld by the caller // Other conditions must be upheld by the caller
unsafe { unsafe {
match layout.size() { match layout.size() {
old_size if old_size == new_size => Ok(MemoryBlock { ptr, size: new_size }), old_size if old_size == new_size => {
Ok(NonNull::slice_from_raw_parts(ptr, new_size))
}
0 => self.alloc(Layout::from_size_align_unchecked(new_size, layout.align())), 0 => self.alloc(Layout::from_size_align_unchecked(new_size, layout.align())),
old_size => { old_size => {
// `realloc` probably checks for `new_size > size` or something similar. // `realloc` probably checks for `new_size > size` or something similar.
intrinsics::assume(new_size > old_size); intrinsics::assume(new_size > old_size);
let raw_ptr = realloc(ptr.as_ptr(), layout, new_size); let raw_ptr = realloc(ptr.as_ptr(), layout, new_size);
let ptr = NonNull::new(raw_ptr).ok_or(AllocErr)?; let ptr = NonNull::new(raw_ptr).ok_or(AllocErr)?;
Ok(MemoryBlock { ptr, size: new_size }) Ok(NonNull::slice_from_raw_parts(ptr, new_size))
} }
} }
} }
@ -231,7 +233,7 @@ unsafe impl AllocRef for Global {
ptr: NonNull<u8>, ptr: NonNull<u8>,
layout: Layout, layout: Layout,
new_size: usize, new_size: usize,
) -> Result<MemoryBlock, AllocErr> { ) -> Result<NonNull<[u8]>, AllocErr> {
debug_assert!( debug_assert!(
new_size >= layout.size(), new_size >= layout.size(),
"`new_size` must be greater than or equal to `layout.size()`" "`new_size` must be greater than or equal to `layout.size()`"
@ -241,7 +243,9 @@ unsafe impl AllocRef for Global {
// Other conditions must be upheld by the caller // Other conditions must be upheld by the caller
unsafe { unsafe {
match layout.size() { match layout.size() {
old_size if old_size == new_size => Ok(MemoryBlock { ptr, size: new_size }), old_size if old_size == new_size => {
Ok(NonNull::slice_from_raw_parts(ptr, new_size))
}
0 => self.alloc_zeroed(Layout::from_size_align_unchecked(new_size, layout.align())), 0 => self.alloc_zeroed(Layout::from_size_align_unchecked(new_size, layout.align())),
old_size => { old_size => {
// `realloc` probably checks for `new_size > size` or something similar. // `realloc` probably checks for `new_size > size` or something similar.
@ -249,7 +253,7 @@ unsafe impl AllocRef for Global {
let raw_ptr = realloc(ptr.as_ptr(), layout, new_size); let raw_ptr = realloc(ptr.as_ptr(), layout, new_size);
raw_ptr.add(old_size).write_bytes(0, new_size - old_size); raw_ptr.add(old_size).write_bytes(0, new_size - old_size);
let ptr = NonNull::new(raw_ptr).ok_or(AllocErr)?; let ptr = NonNull::new(raw_ptr).ok_or(AllocErr)?;
Ok(MemoryBlock { ptr, size: new_size }) Ok(NonNull::slice_from_raw_parts(ptr, new_size))
} }
} }
} }
@ -261,7 +265,7 @@ unsafe impl AllocRef for Global {
ptr: NonNull<u8>, ptr: NonNull<u8>,
layout: Layout, layout: Layout,
new_size: usize, new_size: usize,
) -> Result<MemoryBlock, AllocErr> { ) -> Result<NonNull<[u8]>, AllocErr> {
let old_size = layout.size(); let old_size = layout.size();
debug_assert!( debug_assert!(
new_size <= old_size, new_size <= old_size,
@ -288,7 +292,7 @@ unsafe impl AllocRef for Global {
NonNull::new(raw_ptr).ok_or(AllocErr)? NonNull::new(raw_ptr).ok_or(AllocErr)?
}; };
Ok(MemoryBlock { ptr, size: new_size }) Ok(NonNull::slice_from_raw_parts(ptr, new_size))
} }
} }
@ -300,7 +304,7 @@ unsafe impl AllocRef for Global {
unsafe fn exchange_malloc(size: usize, align: usize) -> *mut u8 { unsafe fn exchange_malloc(size: usize, align: usize) -> *mut u8 {
let layout = unsafe { Layout::from_size_align_unchecked(size, align) }; let layout = unsafe { Layout::from_size_align_unchecked(size, align) };
match Global.alloc(layout) { match Global.alloc(layout) {
Ok(memory) => memory.ptr.as_ptr(), Ok(ptr) => ptr.as_non_null_ptr().as_ptr(),
Err(_) => handle_alloc_error(layout), Err(_) => handle_alloc_error(layout),
} }
} }

View File

@ -8,16 +8,16 @@ use test::Bencher;
fn allocate_zeroed() { fn allocate_zeroed() {
unsafe { unsafe {
let layout = Layout::from_size_align(1024, 1).unwrap(); let layout = Layout::from_size_align(1024, 1).unwrap();
let memory = let ptr =
Global.alloc_zeroed(layout.clone()).unwrap_or_else(|_| handle_alloc_error(layout)); Global.alloc_zeroed(layout.clone()).unwrap_or_else(|_| handle_alloc_error(layout));
let mut i = memory.ptr.cast::<u8>().as_ptr(); let mut i = ptr.as_non_null_ptr().as_ptr();
let end = i.add(layout.size()); let end = i.add(layout.size());
while i < end { while i < end {
assert_eq!(*i, 0); assert_eq!(*i, 0);
i = i.offset(1); i = i.offset(1);
} }
Global.dealloc(memory.ptr, layout); Global.dealloc(ptr.as_non_null_ptr(), layout);
} }
} }

View File

@ -197,8 +197,7 @@ impl<T> Box<T> {
#[unstable(feature = "new_uninit", issue = "63291")] #[unstable(feature = "new_uninit", issue = "63291")]
pub fn new_uninit() -> Box<mem::MaybeUninit<T>> { pub fn new_uninit() -> Box<mem::MaybeUninit<T>> {
let layout = alloc::Layout::new::<mem::MaybeUninit<T>>(); let layout = alloc::Layout::new::<mem::MaybeUninit<T>>();
let ptr = let ptr = Global.alloc(layout).unwrap_or_else(|_| alloc::handle_alloc_error(layout)).cast();
Global.alloc(layout).unwrap_or_else(|_| alloc::handle_alloc_error(layout)).ptr.cast();
unsafe { Box::from_raw(ptr.as_ptr()) } unsafe { Box::from_raw(ptr.as_ptr()) }
} }
@ -226,7 +225,6 @@ impl<T> Box<T> {
let ptr = Global let ptr = Global
.alloc_zeroed(layout) .alloc_zeroed(layout)
.unwrap_or_else(|_| alloc::handle_alloc_error(layout)) .unwrap_or_else(|_| alloc::handle_alloc_error(layout))
.ptr
.cast(); .cast();
unsafe { Box::from_raw(ptr.as_ptr()) } unsafe { Box::from_raw(ptr.as_ptr()) }
} }

View File

@ -104,6 +104,7 @@
#![feature(negative_impls)] #![feature(negative_impls)]
#![feature(new_uninit)] #![feature(new_uninit)]
#![feature(nll)] #![feature(nll)]
#![feature(nonnull_slice_from_raw_parts)]
#![feature(optin_builtin_traits)] #![feature(optin_builtin_traits)]
#![feature(or_patterns)] #![feature(or_patterns)]
#![feature(pattern)] #![feature(pattern)]
@ -113,6 +114,8 @@
#![feature(rustc_attrs)] #![feature(rustc_attrs)]
#![feature(receiver_trait)] #![feature(receiver_trait)]
#![feature(min_specialization)] #![feature(min_specialization)]
#![feature(slice_ptr_get)]
#![feature(slice_ptr_len)]
#![feature(staged_api)] #![feature(staged_api)]
#![feature(std_internals)] #![feature(std_internals)]
#![feature(str_internals)] #![feature(str_internals)]

View File

@ -1,7 +1,7 @@
#![unstable(feature = "raw_vec_internals", reason = "implementation detail", issue = "none")] #![unstable(feature = "raw_vec_internals", reason = "implementation detail", issue = "none")]
#![doc(hidden)] #![doc(hidden)]
use core::alloc::{LayoutErr, MemoryBlock}; use core::alloc::LayoutErr;
use core::cmp; use core::cmp;
use core::mem::{self, ManuallyDrop, MaybeUninit}; use core::mem::{self, ManuallyDrop, MaybeUninit};
use core::ops::Drop; use core::ops::Drop;
@ -186,14 +186,14 @@ impl<T, A: AllocRef> RawVec<T, A> {
AllocInit::Uninitialized => alloc.alloc(layout), AllocInit::Uninitialized => alloc.alloc(layout),
AllocInit::Zeroed => alloc.alloc_zeroed(layout), AllocInit::Zeroed => alloc.alloc_zeroed(layout),
}; };
let memory = match result { let ptr = match result {
Ok(memory) => memory, Ok(ptr) => ptr,
Err(_) => handle_alloc_error(layout), Err(_) => handle_alloc_error(layout),
}; };
Self { Self {
ptr: unsafe { Unique::new_unchecked(memory.ptr.cast().as_ptr()) }, ptr: unsafe { Unique::new_unchecked(ptr.cast().as_ptr()) },
cap: Self::capacity_from_bytes(memory.size), cap: Self::capacity_from_bytes(ptr.len()),
alloc, alloc,
} }
} }
@ -384,9 +384,9 @@ impl<T, A: AllocRef> RawVec<T, A> {
excess / mem::size_of::<T>() excess / mem::size_of::<T>()
} }
fn set_memory(&mut self, memory: MemoryBlock) { fn set_ptr(&mut self, ptr: NonNull<[u8]>) {
self.ptr = unsafe { Unique::new_unchecked(memory.ptr.cast().as_ptr()) }; self.ptr = unsafe { Unique::new_unchecked(ptr.cast().as_ptr()) };
self.cap = Self::capacity_from_bytes(memory.size); self.cap = Self::capacity_from_bytes(ptr.len());
} }
// This method is usually instantiated many times. So we want it to be as // This method is usually instantiated many times. So we want it to be as
@ -432,8 +432,8 @@ impl<T, A: AllocRef> RawVec<T, A> {
let new_layout = Layout::array::<T>(cap); let new_layout = Layout::array::<T>(cap);
// `finish_grow` is non-generic over `T`. // `finish_grow` is non-generic over `T`.
let memory = finish_grow(new_layout, self.current_memory(), &mut self.alloc)?; let ptr = finish_grow(new_layout, self.current_memory(), &mut self.alloc)?;
self.set_memory(memory); self.set_ptr(ptr);
Ok(()) Ok(())
} }
@ -451,8 +451,8 @@ impl<T, A: AllocRef> RawVec<T, A> {
let new_layout = Layout::array::<T>(cap); let new_layout = Layout::array::<T>(cap);
// `finish_grow` is non-generic over `T`. // `finish_grow` is non-generic over `T`.
let memory = finish_grow(new_layout, self.current_memory(), &mut self.alloc)?; let ptr = finish_grow(new_layout, self.current_memory(), &mut self.alloc)?;
self.set_memory(memory); self.set_ptr(ptr);
Ok(()) Ok(())
} }
@ -462,13 +462,13 @@ impl<T, A: AllocRef> RawVec<T, A> {
let (ptr, layout) = if let Some(mem) = self.current_memory() { mem } else { return Ok(()) }; let (ptr, layout) = if let Some(mem) = self.current_memory() { mem } else { return Ok(()) };
let new_size = amount * mem::size_of::<T>(); let new_size = amount * mem::size_of::<T>();
let memory = unsafe { let ptr = unsafe {
self.alloc.shrink(ptr, layout, new_size).map_err(|_| TryReserveError::AllocError { self.alloc.shrink(ptr, layout, new_size).map_err(|_| TryReserveError::AllocError {
layout: Layout::from_size_align_unchecked(new_size, layout.align()), layout: Layout::from_size_align_unchecked(new_size, layout.align()),
non_exhaustive: (), non_exhaustive: (),
})? })?
}; };
self.set_memory(memory); self.set_ptr(ptr);
Ok(()) Ok(())
} }
} }
@ -481,7 +481,7 @@ fn finish_grow<A>(
new_layout: Result<Layout, LayoutErr>, new_layout: Result<Layout, LayoutErr>,
current_memory: Option<(NonNull<u8>, Layout)>, current_memory: Option<(NonNull<u8>, Layout)>,
alloc: &mut A, alloc: &mut A,
) -> Result<MemoryBlock, TryReserveError> ) -> Result<NonNull<[u8]>, TryReserveError>
where where
A: AllocRef, A: AllocRef,
{ {

View File

@ -20,7 +20,7 @@ fn allocator_param() {
fuel: usize, fuel: usize,
} }
unsafe impl AllocRef for BoundedAlloc { unsafe impl AllocRef for BoundedAlloc {
fn alloc(&mut self, layout: Layout) -> Result<MemoryBlock, AllocErr> { fn alloc(&mut self, layout: Layout) -> Result<NonNull<[u8]>, AllocErr> {
let size = layout.size(); let size = layout.size();
if size > self.fuel { if size > self.fuel {
return Err(AllocErr); return Err(AllocErr);

View File

@ -928,10 +928,10 @@ impl<T: ?Sized> Rc<T> {
let layout = Layout::new::<RcBox<()>>().extend(value_layout).unwrap().0.pad_to_align(); let layout = Layout::new::<RcBox<()>>().extend(value_layout).unwrap().0.pad_to_align();
// Allocate for the layout. // Allocate for the layout.
let mem = Global.alloc(layout).unwrap_or_else(|_| handle_alloc_error(layout)); let ptr = Global.alloc(layout).unwrap_or_else(|_| handle_alloc_error(layout));
// Initialize the RcBox // Initialize the RcBox
let inner = mem_to_rcbox(mem.ptr.as_ptr()); let inner = mem_to_rcbox(ptr.as_non_null_ptr().as_ptr());
unsafe { unsafe {
debug_assert_eq!(Layout::for_value(&*inner), layout); debug_assert_eq!(Layout::for_value(&*inner), layout);

View File

@ -883,10 +883,10 @@ impl<T: ?Sized> Arc<T> {
// reference (see #54908). // reference (see #54908).
let layout = Layout::new::<ArcInner<()>>().extend(value_layout).unwrap().0.pad_to_align(); let layout = Layout::new::<ArcInner<()>>().extend(value_layout).unwrap().0.pad_to_align();
let mem = Global.alloc(layout).unwrap_or_else(|_| handle_alloc_error(layout)); let ptr = Global.alloc(layout).unwrap_or_else(|_| handle_alloc_error(layout));
// Initialize the ArcInner // Initialize the ArcInner
let inner = mem_to_arcinner(mem.ptr.as_ptr()); let inner = mem_to_arcinner(ptr.as_non_null_ptr().as_ptr());
debug_assert_eq!(unsafe { Layout::for_value(&*inner) }, layout); debug_assert_eq!(unsafe { Layout::for_value(&*inner) }, layout);
unsafe { unsafe {
@ -986,7 +986,7 @@ impl<T> Arc<[T]> {
let slice = from_raw_parts_mut(self.elems, self.n_elems); let slice = from_raw_parts_mut(self.elems, self.n_elems);
ptr::drop_in_place(slice); ptr::drop_in_place(slice);
Global.dealloc(self.mem.cast(), self.layout); Global.dealloc(self.mem, self.layout);
} }
} }
} }

View File

@ -20,12 +20,12 @@ fn check_overalign_requests<T: AllocRef>(mut allocator: T) {
unsafe { unsafe {
let pointers: Vec<_> = (0..iterations) let pointers: Vec<_> = (0..iterations)
.map(|_| { .map(|_| {
allocator.alloc(Layout::from_size_align(size, align).unwrap()).unwrap().ptr allocator.alloc(Layout::from_size_align(size, align).unwrap()).unwrap()
}) })
.collect(); .collect();
for &ptr in &pointers { for &ptr in &pointers {
assert_eq!( assert_eq!(
(ptr.as_ptr() as usize) % align, (ptr.as_non_null_ptr().as_ptr() as usize) % align,
0, 0,
"Got a pointer less aligned than requested" "Got a pointer less aligned than requested"
) )
@ -33,7 +33,10 @@ fn check_overalign_requests<T: AllocRef>(mut allocator: T) {
// Clean up // Clean up
for &ptr in &pointers { for &ptr in &pointers {
allocator.dealloc(ptr, Layout::from_size_align(size, align).unwrap()) allocator.dealloc(
ptr.as_non_null_ptr(),
Layout::from_size_align(size, align).unwrap(),
)
} }
} }
} }

View File

@ -13,6 +13,7 @@
#![feature(associated_type_bounds)] #![feature(associated_type_bounds)]
#![feature(binary_heap_into_iter_sorted)] #![feature(binary_heap_into_iter_sorted)]
#![feature(binary_heap_drain_sorted)] #![feature(binary_heap_drain_sorted)]
#![feature(slice_ptr_get)]
#![feature(split_inclusive)] #![feature(split_inclusive)]
#![feature(binary_heap_retain)] #![feature(binary_heap_retain)]

View File

@ -29,14 +29,6 @@ impl fmt::Display for AllocErr {
} }
} }
/// Represents a block of allocated memory returned by an allocator.
#[derive(Debug, Copy, Clone)]
#[unstable(feature = "allocator_api", issue = "32838")]
pub struct MemoryBlock {
pub ptr: NonNull<u8>,
pub size: usize,
}
/// An implementation of `AllocRef` can allocate, grow, shrink, and deallocate arbitrary blocks of /// An implementation of `AllocRef` can allocate, grow, shrink, and deallocate arbitrary blocks of
/// data described via [`Layout`][]. /// data described via [`Layout`][].
/// ///
@ -50,13 +42,13 @@ pub struct MemoryBlock {
/// ///
/// ### Currently allocated memory /// ### Currently allocated memory
/// ///
/// Some of the methods require that a memory block be *currently allocated* via an allocator. This /// Some of the methods require that a memory slice be *currently allocated* via an allocator. This
/// means that: /// means that:
/// ///
/// * the starting address for that memory block was previously returned by [`alloc`], [`grow`], or /// * the starting address for that memory slice was previously returned by [`alloc`], [`grow`], or
/// [`shrink`], and /// [`shrink`], and
/// ///
/// * the memory block has not been subsequently deallocated, where blocks are either deallocated /// * the memory slice has not been subsequently deallocated, where slices are either deallocated
/// directly by being passed to [`dealloc`] or were changed by being passed to [`grow`] or /// directly by being passed to [`dealloc`] or were changed by being passed to [`grow`] or
/// [`shrink`] that returns `Ok`. If `grow` or `shrink` have returned `Err`, the passed pointer /// [`shrink`] that returns `Ok`. If `grow` or `shrink` have returned `Err`, the passed pointer
/// remains valid. /// remains valid.
@ -68,14 +60,14 @@ pub struct MemoryBlock {
/// ///
/// ### Memory fitting /// ### Memory fitting
/// ///
/// Some of the methods require that a layout *fit* a memory block. What it means for a layout to /// Some of the methods require that a layout *fit* a memory slice. What it means for a layout to
/// "fit" a memory block means (or equivalently, for a memory block to "fit" a layout) is that the /// "fit" a memory slice means (or equivalently, for a memory slice to "fit" a layout) is that the
/// following conditions must hold: /// following conditions must hold:
/// ///
/// * The block must be allocated with the same alignment as [`layout.align()`], and /// * The slice must be allocated with the same alignment as [`layout.align()`], and
/// ///
/// * The provided [`layout.size()`] must fall in the range `min ..= max`, where: /// * The provided [`layout.size()`] must fall in the range `min ..= max`, where:
/// - `min` is the size of the layout most recently used to allocate the block, and /// - `min` is the size of the layout most recently used to allocate the slice, and
/// - `max` is the latest actual size returned from [`alloc`], [`grow`], or [`shrink`]. /// - `max` is the latest actual size returned from [`alloc`], [`grow`], or [`shrink`].
/// ///
/// [`layout.align()`]: Layout::align /// [`layout.align()`]: Layout::align
@ -83,25 +75,27 @@ pub struct MemoryBlock {
/// ///
/// # Safety /// # Safety
/// ///
/// * Memory blocks returned from an allocator must point to valid memory and retain their validity /// * Memory slices returned from an allocator must point to valid memory and retain their validity
/// until the instance and all of its clones are dropped, /// until the instance and all of its clones are dropped,
/// ///
/// * cloning or moving the allocator must not invalidate memory blocks returned from this /// * cloning or moving the allocator must not invalidate memory slices returned from this
/// allocator. A cloned allocator must behave like the same allocator, and /// allocator. A cloned allocator must behave like the same allocator, and
/// ///
/// * any pointer to a memory block which is [*currently allocated*] may be passed to any other /// * any pointer to a memory slice which is [*currently allocated*] may be passed to any other
/// method of the allocator. /// method of the allocator.
/// ///
/// [*currently allocated*]: #currently-allocated-memory /// [*currently allocated*]: #currently-allocated-memory
#[unstable(feature = "allocator_api", issue = "32838")] #[unstable(feature = "allocator_api", issue = "32838")]
pub unsafe trait AllocRef { pub unsafe trait AllocRef {
/// Attempts to allocate a block of memory. /// Attempts to allocate a slice of memory.
/// ///
/// On success, returns a [`MemoryBlock`][] meeting the size and alignment guarantees of `layout`. /// On success, returns a [`NonNull<[u8]>`] meeting the size and alignment guarantees of `layout`.
/// ///
/// The returned block may have a larger size than specified by `layout.size()`, and may or may /// The returned slice may have a larger size than specified by `layout.size()`, and may or may
/// not have its contents initialized. /// not have its contents initialized.
/// ///
/// [`NonNull<[u8]>`]: NonNull
///
/// # Errors /// # Errors
/// ///
/// Returning `Err` indicates that either memory is exhausted or `layout` does not meet /// Returning `Err` indicates that either memory is exhausted or `layout` does not meet
@ -115,7 +109,7 @@ pub unsafe trait AllocRef {
/// call the [`handle_alloc_error`] function, rather than directly invoking `panic!` or similar. /// call the [`handle_alloc_error`] function, rather than directly invoking `panic!` or similar.
/// ///
/// [`handle_alloc_error`]: ../../alloc/alloc/fn.handle_alloc_error.html /// [`handle_alloc_error`]: ../../alloc/alloc/fn.handle_alloc_error.html
fn alloc(&mut self, layout: Layout) -> Result<MemoryBlock, AllocErr>; fn alloc(&mut self, layout: Layout) -> Result<NonNull<[u8]>, AllocErr>;
/// Behaves like `alloc`, but also ensures that the returned memory is zero-initialized. /// Behaves like `alloc`, but also ensures that the returned memory is zero-initialized.
/// ///
@ -132,27 +126,27 @@ pub unsafe trait AllocRef {
/// call the [`handle_alloc_error`] function, rather than directly invoking `panic!` or similar. /// call the [`handle_alloc_error`] function, rather than directly invoking `panic!` or similar.
/// ///
/// [`handle_alloc_error`]: ../../alloc/alloc/fn.handle_alloc_error.html /// [`handle_alloc_error`]: ../../alloc/alloc/fn.handle_alloc_error.html
fn alloc_zeroed(&mut self, layout: Layout) -> Result<MemoryBlock, AllocErr> { fn alloc_zeroed(&mut self, layout: Layout) -> Result<NonNull<[u8]>, AllocErr> {
let memory = self.alloc(layout)?; let ptr = self.alloc(layout)?;
// SAFETY: `alloc` returns a valid memory block // SAFETY: `alloc` returns a valid memory slice
unsafe { memory.ptr.as_ptr().write_bytes(0, memory.size) } unsafe { ptr.as_non_null_ptr().as_ptr().write_bytes(0, ptr.len()) }
Ok(memory) Ok(ptr)
} }
/// Deallocates the memory referenced by `ptr`. /// Deallocates the memory slice referenced by `ptr`.
/// ///
/// # Safety /// # Safety
/// ///
/// * `ptr` must denote a block of memory [*currently allocated*] via this allocator, and /// * `ptr` must denote a slice of memory [*currently allocated*] via this allocator, and
/// * `layout` must [*fit*] that block of memory. /// * `layout` must [*fit*] that slice of memory.
/// ///
/// [*currently allocated*]: #currently-allocated-memory /// [*currently allocated*]: #currently-allocated-memory
/// [*fit*]: #memory-fitting /// [*fit*]: #memory-fitting
unsafe fn dealloc(&mut self, ptr: NonNull<u8>, layout: Layout); unsafe fn dealloc(&mut self, ptr: NonNull<u8>, layout: Layout);
/// Attempts to extend the memory block. /// Attempts to extend the memory slice.
/// ///
/// Returns a new [`MemoryBlock`][] containing a pointer and the actual size of the allocated /// Returns a new [`NonNull<[u8]>`] containing a pointer and the actual size of the allocated
/// memory. The pointer is suitable for holding data described by a new layout with `layout`s /// memory. The pointer is suitable for holding data described by a new layout with `layout`s
/// alignment and a size given by `new_size`. To accomplish this, the allocator may extend the /// alignment and a size given by `new_size`. To accomplish this, the allocator may extend the
/// allocation referenced by `ptr` to fit the new layout. /// allocation referenced by `ptr` to fit the new layout.
@ -160,6 +154,8 @@ pub unsafe trait AllocRef {
/// If this method returns `Err`, then ownership of the memory block has not been transferred to /// If this method returns `Err`, then ownership of the memory block has not been transferred to
/// this allocator, and the contents of the memory block are unaltered. /// this allocator, and the contents of the memory block are unaltered.
/// ///
/// [`NonNull<[u8]>`]: NonNull
///
/// # Safety /// # Safety
/// ///
/// * `ptr` must denote a block of memory [*currently allocated*] via this allocator, /// * `ptr` must denote a block of memory [*currently allocated*] via this allocator,
@ -168,7 +164,7 @@ pub unsafe trait AllocRef {
/// * `new_size`, when rounded up to the nearest multiple of `layout.align()`, must not overflow /// * `new_size`, when rounded up to the nearest multiple of `layout.align()`, must not overflow
/// (i.e., the rounded value must be less than or equal to `usize::MAX`). /// (i.e., the rounded value must be less than or equal to `usize::MAX`).
// Note: We can't require that `new_size` is strictly greater than `layout.size()` because of ZSTs. // Note: We can't require that `new_size` is strictly greater than `layout.size()` because of ZSTs.
// alternative: `new_size must be strictly greater than `layout.size()` or both are zero // alternative: `new_size` must be strictly greater than `layout.size()` or both are zero
/// ///
/// [*currently allocated*]: #currently-allocated-memory /// [*currently allocated*]: #currently-allocated-memory
/// [*fit*]: #memory-fitting /// [*fit*]: #memory-fitting
@ -191,15 +187,15 @@ pub unsafe trait AllocRef {
ptr: NonNull<u8>, ptr: NonNull<u8>,
layout: Layout, layout: Layout,
new_size: usize, new_size: usize,
) -> Result<MemoryBlock, AllocErr> { ) -> Result<NonNull<[u8]>, AllocErr> {
let size = layout.size(); let size = layout.size();
debug_assert!( debug_assert!(
new_size >= size, new_size >= size,
"`new_size` must be greater than or equal to `layout.size()`" "`new_size` must be greater than or equal to `layout.size()`"
); );
if new_size == size { if size == new_size {
return Ok(MemoryBlock { ptr, size }); return Ok(NonNull::slice_from_raw_parts(ptr, size));
} }
let new_layout = let new_layout =
@ -208,17 +204,17 @@ pub unsafe trait AllocRef {
// The caller must ensure that `new_size` is greater than or equal to zero. If it's equal // The caller must ensure that `new_size` is greater than or equal to zero. If it's equal
// to zero, it's catched beforehand. // to zero, it's catched beforehand.
unsafe { Layout::from_size_align_unchecked(new_size, layout.align()) }; unsafe { Layout::from_size_align_unchecked(new_size, layout.align()) };
let new_memory = self.alloc(new_layout)?; let new_ptr = self.alloc(new_layout)?;
// SAFETY: because `new_size` must be greater than or equal to `size`, both the old and new // SAFETY: because `new_size` must be greater than or equal to `size`, both the old and new
// memory allocation are valid for reads and writes for `size` bytes. Also, because the old // memory allocation are valid for reads and writes for `size` bytes. Also, because the old
// allocation wasn't yet deallocated, it cannot overlap `new_memory`. Thus, the call to // allocation wasn't yet deallocated, it cannot overlap `new_ptr`. Thus, the call to
// `copy_nonoverlapping` is safe. // `copy_nonoverlapping` is safe.
// The safety contract for `dealloc` must be upheld by the caller. // The safety contract for `dealloc` must be upheld by the caller.
unsafe { unsafe {
ptr::copy_nonoverlapping(ptr.as_ptr(), new_memory.ptr.as_ptr(), size); ptr::copy_nonoverlapping(ptr.as_ptr(), new_ptr.as_non_null_ptr().as_ptr(), size);
self.dealloc(ptr, layout); self.dealloc(ptr, layout);
Ok(new_memory) Ok(new_ptr)
} }
} }
@ -239,12 +235,11 @@ pub unsafe trait AllocRef {
/// ///
/// * `ptr` must denote a block of memory [*currently allocated*] via this allocator, /// * `ptr` must denote a block of memory [*currently allocated*] via this allocator,
/// * `layout` must [*fit*] that block of memory (The `new_size` argument need not fit it.), /// * `layout` must [*fit*] that block of memory (The `new_size` argument need not fit it.),
// We can't require that `new_size` is strictly greater than `memory.size` because of ZSTs.
// An alternative would be
// * `new_size must be strictly greater than `memory.size` or both are zero
/// * `new_size` must be greater than or equal to `layout.size()`, and /// * `new_size` must be greater than or equal to `layout.size()`, and
/// * `new_size`, when rounded up to the nearest multiple of `layout.align()`, must not overflow /// * `new_size`, when rounded up to the nearest multiple of `layout.align()`, must not overflow
/// (i.e., the rounded value must be less than or equal to `usize::MAX`). /// (i.e., the rounded value must be less than or equal to `usize::MAX`).
// Note: We can't require that `new_size` is strictly greater than `layout.size()` because of ZSTs.
// alternative: `new_size` must be strictly greater than `layout.size()` or both are zero
/// ///
/// [*currently allocated*]: #currently-allocated-memory /// [*currently allocated*]: #currently-allocated-memory
/// [*fit*]: #memory-fitting /// [*fit*]: #memory-fitting
@ -267,15 +262,15 @@ pub unsafe trait AllocRef {
ptr: NonNull<u8>, ptr: NonNull<u8>,
layout: Layout, layout: Layout,
new_size: usize, new_size: usize,
) -> Result<MemoryBlock, AllocErr> { ) -> Result<NonNull<[u8]>, AllocErr> {
let size = layout.size(); let size = layout.size();
debug_assert!( debug_assert!(
new_size >= size, new_size >= size,
"`new_size` must be greater than or equal to `layout.size()`" "`new_size` must be greater than or equal to `layout.size()`"
); );
if new_size == size { if size == new_size {
return Ok(MemoryBlock { ptr, size }); return Ok(NonNull::slice_from_raw_parts(ptr, size));
} }
let new_layout = let new_layout =
@ -284,43 +279,44 @@ pub unsafe trait AllocRef {
// The caller must ensure that `new_size` is greater than or equal to zero. If it's equal // The caller must ensure that `new_size` is greater than or equal to zero. If it's equal
// to zero, it's caught beforehand. // to zero, it's caught beforehand.
unsafe { Layout::from_size_align_unchecked(new_size, layout.align()) }; unsafe { Layout::from_size_align_unchecked(new_size, layout.align()) };
let new_memory = self.alloc_zeroed(new_layout)?; let new_ptr = self.alloc_zeroed(new_layout)?;
// SAFETY: because `new_size` must be greater than or equal to `size`, both the old and new // SAFETY: because `new_size` must be greater than or equal to `size`, both the old and new
// memory allocation are valid for reads and writes for `size` bytes. Also, because the old // memory allocation are valid for reads and writes for `size` bytes. Also, because the old
// allocation wasn't yet deallocated, it cannot overlap `new_memory`. Thus, the call to // allocation wasn't yet deallocated, it cannot overlap `new_ptr`. Thus, the call to
// `copy_nonoverlapping` is safe. // `copy_nonoverlapping` is safe.
// The safety contract for `dealloc` must be upheld by the caller. // The safety contract for `dealloc` must be upheld by the caller.
unsafe { unsafe {
ptr::copy_nonoverlapping(ptr.as_ptr(), new_memory.ptr.as_ptr(), size); ptr::copy_nonoverlapping(ptr.as_ptr(), new_ptr.as_non_null_ptr().as_ptr(), size);
self.dealloc(ptr, layout); self.dealloc(ptr, layout);
Ok(new_memory) Ok(new_ptr)
} }
} }
/// Attempts to shrink the memory block. /// Attempts to shrink the memory slice.
/// ///
/// Returns a new [`MemoryBlock`][] containing a pointer and the actual size of the allocated /// Returns a new [`NonNull<[u8]>`] containing a pointer and the actual size of the allocated
/// memory. The pointer is suitable for holding data described by a new layout with `layout`s /// memory. The pointer is suitable for holding data described by a new layout with `layout`s
/// alignment and a size given by `new_size`. To accomplish this, the allocator may shrink the /// alignment and a size given by `new_size`. To accomplish this, the allocator may shrink the
/// allocation referenced by `ptr` to fit the new layout. /// allocation referenced by `ptr` to fit the new layout.
/// ///
/// If this returns `Ok`, then ownership of the memory block referenced by `ptr` has been /// If this returns `Ok`, then ownership of the memory slice referenced by `ptr` has been
/// transferred to this allocator. The memory may or may not have been freed, and should be /// transferred to this allocator. The memory may or may not have been freed, and should be
/// considered unusable unless it was transferred back to the caller again via the /// considered unusable unless it was transferred back to the caller again via the
/// return value of this method. /// return value of this method.
/// ///
/// If this method returns `Err`, then ownership of the memory block has not been transferred to /// If this method returns `Err`, then ownership of the memory slice has not been transferred to
/// this allocator, and the contents of the memory block are unaltered. /// this allocator, and the contents of the memory slice are unaltered.
///
/// [`NonNull<[u8]>`]: NonNull
/// ///
/// # Safety /// # Safety
/// ///
/// * `ptr` must denote a block of memory [*currently allocated*] via this allocator, /// * `ptr` must denote a slice of memory [*currently allocated*] via this allocator,
/// * `layout` must [*fit*] that block of memory (The `new_size` argument need not fit it.), and /// * `layout` must [*fit*] that slice of memory (The `new_size` argument need not fit it.), and
// We can't require that `new_size` is strictly smaller than `memory.size` because of ZSTs.
// An alternative would be
// * `new_size must be strictly smaller than `memory.size` or both are zero
/// * `new_size` must be smaller than or equal to `layout.size()`. /// * `new_size` must be smaller than or equal to `layout.size()`.
// Note: We can't require that `new_size` is strictly smaller than `layout.size()` because of ZSTs.
// alternative: `new_size` must be smaller than `layout.size()` or both are zero
/// ///
/// [*currently allocated*]: #currently-allocated-memory /// [*currently allocated*]: #currently-allocated-memory
/// [*fit*]: #memory-fitting /// [*fit*]: #memory-fitting
@ -343,15 +339,15 @@ pub unsafe trait AllocRef {
ptr: NonNull<u8>, ptr: NonNull<u8>,
layout: Layout, layout: Layout,
new_size: usize, new_size: usize,
) -> Result<MemoryBlock, AllocErr> { ) -> Result<NonNull<[u8]>, AllocErr> {
let size = layout.size(); let size = layout.size();
debug_assert!( debug_assert!(
new_size <= size, new_size <= size,
"`new_size` must be smaller than or equal to `layout.size()`" "`new_size` must be smaller than or equal to `layout.size()`"
); );
if new_size == size { if size == new_size {
return Ok(MemoryBlock { ptr, size }); return Ok(NonNull::slice_from_raw_parts(ptr, size));
} }
let new_layout = let new_layout =
@ -359,17 +355,17 @@ pub unsafe trait AllocRef {
// `layout.align()` comes from a `Layout` and is thus guaranteed to be valid for a Layout. // `layout.align()` comes from a `Layout` and is thus guaranteed to be valid for a Layout.
// The caller must ensure that `new_size` is greater than zero. // The caller must ensure that `new_size` is greater than zero.
unsafe { Layout::from_size_align_unchecked(new_size, layout.align()) }; unsafe { Layout::from_size_align_unchecked(new_size, layout.align()) };
let new_memory = self.alloc(new_layout)?; let new_ptr = self.alloc(new_layout)?;
// SAFETY: because `new_size` must be lower than or equal to `size`, both the old and new // SAFETY: because `new_size` must be lower than or equal to `size`, both the old and new
// memory allocation are valid for reads and writes for `new_size` bytes. Also, because the // memory allocation are valid for reads and writes for `new_size` bytes. Also, because the
// old allocation wasn't yet deallocated, it cannot overlap `new_memory`. Thus, the call to // old allocation wasn't yet deallocated, it cannot overlap `new_ptr`. Thus, the call to
// `copy_nonoverlapping` is safe. // `copy_nonoverlapping` is safe.
// The safety contract for `dealloc` must be upheld by the caller. // The safety contract for `dealloc` must be upheld by the caller.
unsafe { unsafe {
ptr::copy_nonoverlapping(ptr.as_ptr(), new_memory.ptr.as_ptr(), new_size); ptr::copy_nonoverlapping(ptr.as_ptr(), new_ptr.as_non_null_ptr().as_ptr(), size);
self.dealloc(ptr, layout); self.dealloc(ptr, layout);
Ok(new_memory) Ok(new_ptr)
} }
} }
@ -388,12 +384,12 @@ where
A: AllocRef + ?Sized, A: AllocRef + ?Sized,
{ {
#[inline] #[inline]
fn alloc(&mut self, layout: Layout) -> Result<MemoryBlock, AllocErr> { fn alloc(&mut self, layout: Layout) -> Result<NonNull<[u8]>, AllocErr> {
(**self).alloc(layout) (**self).alloc(layout)
} }
#[inline] #[inline]
fn alloc_zeroed(&mut self, layout: Layout) -> Result<MemoryBlock, AllocErr> { fn alloc_zeroed(&mut self, layout: Layout) -> Result<NonNull<[u8]>, AllocErr> {
(**self).alloc_zeroed(layout) (**self).alloc_zeroed(layout)
} }
@ -409,7 +405,7 @@ where
ptr: NonNull<u8>, ptr: NonNull<u8>,
layout: Layout, layout: Layout,
new_size: usize, new_size: usize,
) -> Result<MemoryBlock, AllocErr> { ) -> Result<NonNull<[u8]>, AllocErr> {
// SAFETY: the safety contract must be upheld by the caller // SAFETY: the safety contract must be upheld by the caller
unsafe { (**self).grow(ptr, layout, new_size) } unsafe { (**self).grow(ptr, layout, new_size) }
} }
@ -420,7 +416,7 @@ where
ptr: NonNull<u8>, ptr: NonNull<u8>,
layout: Layout, layout: Layout,
new_size: usize, new_size: usize,
) -> Result<MemoryBlock, AllocErr> { ) -> Result<NonNull<[u8]>, AllocErr> {
// SAFETY: the safety contract must be upheld by the caller // SAFETY: the safety contract must be upheld by the caller
unsafe { (**self).grow_zeroed(ptr, layout, new_size) } unsafe { (**self).grow_zeroed(ptr, layout, new_size) }
} }
@ -431,7 +427,7 @@ where
ptr: NonNull<u8>, ptr: NonNull<u8>,
layout: Layout, layout: Layout,
new_size: usize, new_size: usize,
) -> Result<MemoryBlock, AllocErr> { ) -> Result<NonNull<[u8]>, AllocErr> {
// SAFETY: the safety contract must be upheld by the caller // SAFETY: the safety contract must be upheld by the caller
unsafe { (**self).shrink(ptr, layout, new_size) } unsafe { (**self).shrink(ptr, layout, new_size) }
} }

View File

@ -140,7 +140,7 @@ pub struct System;
#[unstable(feature = "allocator_api", issue = "32838")] #[unstable(feature = "allocator_api", issue = "32838")]
unsafe impl AllocRef for System { unsafe impl AllocRef for System {
#[inline] #[inline]
fn alloc(&mut self, layout: Layout) -> Result<MemoryBlock, AllocErr> { fn alloc(&mut self, layout: Layout) -> Result<NonNull<[u8]>, AllocErr> {
let size = layout.size(); let size = layout.size();
let ptr = if size == 0 { let ptr = if size == 0 {
layout.dangling() layout.dangling()
@ -148,11 +148,11 @@ unsafe impl AllocRef for System {
// SAFETY: `layout` is non-zero in size, // SAFETY: `layout` is non-zero in size,
unsafe { NonNull::new(GlobalAlloc::alloc(&System, layout)).ok_or(AllocErr)? } unsafe { NonNull::new(GlobalAlloc::alloc(&System, layout)).ok_or(AllocErr)? }
}; };
Ok(MemoryBlock { ptr, size }) Ok(NonNull::slice_from_raw_parts(ptr, size))
} }
#[inline] #[inline]
fn alloc_zeroed(&mut self, layout: Layout) -> Result<MemoryBlock, AllocErr> { fn alloc_zeroed(&mut self, layout: Layout) -> Result<NonNull<[u8]>, AllocErr> {
let size = layout.size(); let size = layout.size();
let ptr = if size == 0 { let ptr = if size == 0 {
layout.dangling() layout.dangling()
@ -160,7 +160,7 @@ unsafe impl AllocRef for System {
// SAFETY: `layout` is non-zero in size, // SAFETY: `layout` is non-zero in size,
unsafe { NonNull::new(GlobalAlloc::alloc_zeroed(&System, layout)).ok_or(AllocErr)? } unsafe { NonNull::new(GlobalAlloc::alloc_zeroed(&System, layout)).ok_or(AllocErr)? }
}; };
Ok(MemoryBlock { ptr, size }) Ok(NonNull::slice_from_raw_parts(ptr, size))
} }
#[inline] #[inline]
@ -178,7 +178,7 @@ unsafe impl AllocRef for System {
ptr: NonNull<u8>, ptr: NonNull<u8>,
layout: Layout, layout: Layout,
new_size: usize, new_size: usize,
) -> Result<MemoryBlock, AllocErr> { ) -> Result<NonNull<[u8]>, AllocErr> {
debug_assert!( debug_assert!(
new_size >= layout.size(), new_size >= layout.size(),
"`new_size` must be greater than or equal to `layout.size()`" "`new_size` must be greater than or equal to `layout.size()`"
@ -188,14 +188,16 @@ unsafe impl AllocRef for System {
// Other conditions must be upheld by the caller // Other conditions must be upheld by the caller
unsafe { unsafe {
match layout.size() { match layout.size() {
old_size if old_size == new_size => Ok(MemoryBlock { ptr, size: new_size }), old_size if old_size == new_size => {
Ok(NonNull::slice_from_raw_parts(ptr, new_size))
}
0 => self.alloc(Layout::from_size_align_unchecked(new_size, layout.align())), 0 => self.alloc(Layout::from_size_align_unchecked(new_size, layout.align())),
old_size => { old_size => {
// `realloc` probably checks for `new_size > size` or something similar. // `realloc` probably checks for `new_size > size` or something similar.
intrinsics::assume(new_size > old_size); intrinsics::assume(new_size > old_size);
let raw_ptr = GlobalAlloc::realloc(&System, ptr.as_ptr(), layout, new_size); let raw_ptr = GlobalAlloc::realloc(&System, ptr.as_ptr(), layout, new_size);
let ptr = NonNull::new(raw_ptr).ok_or(AllocErr)?; let ptr = NonNull::new(raw_ptr).ok_or(AllocErr)?;
Ok(MemoryBlock { ptr, size: new_size }) Ok(NonNull::slice_from_raw_parts(ptr, new_size))
} }
} }
} }
@ -207,7 +209,7 @@ unsafe impl AllocRef for System {
ptr: NonNull<u8>, ptr: NonNull<u8>,
layout: Layout, layout: Layout,
new_size: usize, new_size: usize,
) -> Result<MemoryBlock, AllocErr> { ) -> Result<NonNull<[u8]>, AllocErr> {
debug_assert!( debug_assert!(
new_size >= layout.size(), new_size >= layout.size(),
"`new_size` must be greater than or equal to `layout.size()`" "`new_size` must be greater than or equal to `layout.size()`"
@ -217,7 +219,9 @@ unsafe impl AllocRef for System {
// Other conditions must be upheld by the caller // Other conditions must be upheld by the caller
unsafe { unsafe {
match layout.size() { match layout.size() {
old_size if old_size == new_size => Ok(MemoryBlock { ptr, size: new_size }), old_size if old_size == new_size => {
Ok(NonNull::slice_from_raw_parts(ptr, new_size))
}
0 => self.alloc_zeroed(Layout::from_size_align_unchecked(new_size, layout.align())), 0 => self.alloc_zeroed(Layout::from_size_align_unchecked(new_size, layout.align())),
old_size => { old_size => {
// `realloc` probably checks for `new_size > size` or something similar. // `realloc` probably checks for `new_size > size` or something similar.
@ -225,7 +229,7 @@ unsafe impl AllocRef for System {
let raw_ptr = GlobalAlloc::realloc(&System, ptr.as_ptr(), layout, new_size); let raw_ptr = GlobalAlloc::realloc(&System, ptr.as_ptr(), layout, new_size);
raw_ptr.add(old_size).write_bytes(0, new_size - old_size); raw_ptr.add(old_size).write_bytes(0, new_size - old_size);
let ptr = NonNull::new(raw_ptr).ok_or(AllocErr)?; let ptr = NonNull::new(raw_ptr).ok_or(AllocErr)?;
Ok(MemoryBlock { ptr, size: new_size }) Ok(NonNull::slice_from_raw_parts(ptr, new_size))
} }
} }
} }
@ -237,7 +241,7 @@ unsafe impl AllocRef for System {
ptr: NonNull<u8>, ptr: NonNull<u8>,
layout: Layout, layout: Layout,
new_size: usize, new_size: usize,
) -> Result<MemoryBlock, AllocErr> { ) -> Result<NonNull<[u8]>, AllocErr> {
let old_size = layout.size(); let old_size = layout.size();
debug_assert!( debug_assert!(
new_size <= old_size, new_size <= old_size,
@ -264,7 +268,7 @@ unsafe impl AllocRef for System {
NonNull::new(raw_ptr).ok_or(AllocErr)? NonNull::new(raw_ptr).ok_or(AllocErr)?
}; };
Ok(MemoryBlock { ptr, size: new_size }) Ok(NonNull::slice_from_raw_parts(ptr, new_size))
} }
} }
static HOOK: AtomicPtr<()> = AtomicPtr::new(ptr::null_mut()); static HOOK: AtomicPtr<()> = AtomicPtr::new(ptr::null_mut());

View File

@ -291,6 +291,7 @@
#![feature(negative_impls)] #![feature(negative_impls)]
#![feature(never_type)] #![feature(never_type)]
#![feature(nll)] #![feature(nll)]
#![feature(nonnull_slice_from_raw_parts)]
#![feature(once_cell)] #![feature(once_cell)]
#![feature(optin_builtin_traits)] #![feature(optin_builtin_traits)]
#![feature(or_patterns)] #![feature(or_patterns)]
@ -308,6 +309,8 @@
#![feature(shrink_to)] #![feature(shrink_to)]
#![feature(slice_concat_ext)] #![feature(slice_concat_ext)]
#![feature(slice_internals)] #![feature(slice_internals)]
#![feature(slice_ptr_get)]
#![feature(slice_ptr_len)]
#![feature(slice_strip)] #![feature(slice_strip)]
#![feature(staged_api)] #![feature(staged_api)]
#![feature(std_internals)] #![feature(std_internals)]

View File

@ -4,6 +4,7 @@
// no-prefer-dynamic // no-prefer-dynamic
#![feature(allocator_api)] #![feature(allocator_api)]
#![feature(slice_ptr_get)]
extern crate helper; extern crate helper;
@ -38,9 +39,9 @@ fn main() {
let layout = Layout::from_size_align(4, 2).unwrap(); let layout = Layout::from_size_align(4, 2).unwrap();
let memory = Global.alloc(layout.clone()).unwrap(); let memory = Global.alloc(layout.clone()).unwrap();
helper::work_with(&memory.ptr); helper::work_with(&memory);
assert_eq!(HITS.load(Ordering::SeqCst), n + 1); assert_eq!(HITS.load(Ordering::SeqCst), n + 1);
Global.dealloc(memory.ptr, layout); Global.dealloc(memory.as_non_null_ptr(), layout);
assert_eq!(HITS.load(Ordering::SeqCst), n + 2); assert_eq!(HITS.load(Ordering::SeqCst), n + 2);
let s = String::with_capacity(10); let s = String::with_capacity(10);
@ -51,8 +52,8 @@ fn main() {
let memory = System.alloc(layout.clone()).unwrap(); let memory = System.alloc(layout.clone()).unwrap();
assert_eq!(HITS.load(Ordering::SeqCst), n + 4); assert_eq!(HITS.load(Ordering::SeqCst), n + 4);
helper::work_with(&memory.ptr); helper::work_with(&memory);
System.dealloc(memory.ptr, layout); System.dealloc(memory.as_non_null_ptr(), layout);
assert_eq!(HITS.load(Ordering::SeqCst), n + 4); assert_eq!(HITS.load(Ordering::SeqCst), n + 4);
} }
} }

View File

@ -5,6 +5,7 @@
// no-prefer-dynamic // no-prefer-dynamic
#![feature(allocator_api)] #![feature(allocator_api)]
#![feature(slice_ptr_get)]
extern crate custom; extern crate custom;
extern crate helper; extern crate helper;
@ -21,15 +22,15 @@ fn main() {
let layout = Layout::from_size_align(4, 2).unwrap(); let layout = Layout::from_size_align(4, 2).unwrap();
let memory = Global.alloc(layout.clone()).unwrap(); let memory = Global.alloc(layout.clone()).unwrap();
helper::work_with(&memory.ptr); helper::work_with(&memory);
assert_eq!(GLOBAL.0.load(Ordering::SeqCst), n + 1); assert_eq!(GLOBAL.0.load(Ordering::SeqCst), n + 1);
Global.dealloc(memory.ptr, layout); Global.dealloc(memory.as_non_null_ptr(), layout);
assert_eq!(GLOBAL.0.load(Ordering::SeqCst), n + 2); assert_eq!(GLOBAL.0.load(Ordering::SeqCst), n + 2);
let memory = System.alloc(layout.clone()).unwrap(); let memory = System.alloc(layout.clone()).unwrap();
assert_eq!(GLOBAL.0.load(Ordering::SeqCst), n + 2); assert_eq!(GLOBAL.0.load(Ordering::SeqCst), n + 2);
helper::work_with(&memory.ptr); helper::work_with(&memory);
System.dealloc(memory.ptr, layout); System.dealloc(memory.as_non_null_ptr(), layout);
assert_eq!(GLOBAL.0.load(Ordering::SeqCst), n + 2); assert_eq!(GLOBAL.0.load(Ordering::SeqCst), n + 2);
} }
} }

View File

@ -5,6 +5,7 @@
// well enough to reproduce (and illustrate) the bug from #16687. // well enough to reproduce (and illustrate) the bug from #16687.
#![feature(allocator_api)] #![feature(allocator_api)]
#![feature(slice_ptr_get)]
use std::alloc::{handle_alloc_error, AllocRef, Global, Layout}; use std::alloc::{handle_alloc_error, AllocRef, Global, Layout};
use std::ptr::{self, NonNull}; use std::ptr::{self, NonNull};
@ -41,13 +42,13 @@ unsafe fn test_triangle() -> bool {
println!("allocate({:?})", layout); println!("allocate({:?})", layout);
} }
let memory = Global.alloc(layout).unwrap_or_else(|_| handle_alloc_error(layout)); let ptr = Global.alloc(layout).unwrap_or_else(|_| handle_alloc_error(layout));
if PRINT { if PRINT {
println!("allocate({:?}) = {:?}", layout, memory.ptr); println!("allocate({:?}) = {:?}", layout, ptr);
} }
memory.ptr.cast().as_ptr() ptr.as_non_null_ptr().as_ptr()
} }
unsafe fn deallocate(ptr: *mut u8, layout: Layout) { unsafe fn deallocate(ptr: *mut u8, layout: Layout) {
@ -73,14 +74,14 @@ unsafe fn test_triangle() -> bool {
Global.shrink(NonNull::new_unchecked(ptr), old, new.size()) Global.shrink(NonNull::new_unchecked(ptr), old, new.size())
}; };
let memory = memory.unwrap_or_else(|_| { let ptr = memory.unwrap_or_else(|_| {
handle_alloc_error(Layout::from_size_align_unchecked(new.size(), old.align())) handle_alloc_error(Layout::from_size_align_unchecked(new.size(), old.align()))
}); });
if PRINT { if PRINT {
println!("reallocate({:?}, old={:?}, new={:?}) = {:?}", ptr, old, new, memory.ptr); println!("reallocate({:?}, old={:?}, new={:?}) = {:?}", ptr, old, new, ptr);
} }
memory.ptr.cast().as_ptr() ptr.as_non_null_ptr().as_ptr()
} }
fn idx_to_size(i: usize) -> usize { fn idx_to_size(i: usize) -> usize {

View File

@ -25,8 +25,8 @@ struct Ccx {
fn alloc(_bcx: &arena) -> &Bcx<'_> { fn alloc(_bcx: &arena) -> &Bcx<'_> {
unsafe { unsafe {
let layout = Layout::new::<Bcx>(); let layout = Layout::new::<Bcx>();
let memory = Global.alloc(layout).unwrap_or_else(|_| handle_alloc_error(layout)); let ptr = Global.alloc(layout).unwrap_or_else(|_| handle_alloc_error(layout));
&*(memory.ptr.as_ptr() as *const _) &*(ptr.as_ptr() as *const _)
} }
} }