1
Fork 0

Rollup merge of #76993 - blitzerr:alloc-ref, r=Amanieu

Changing the alloc() to accept &self instead of &mut self

Fixes: [#55](https://github.com/rust-lang/wg-allocators/issues/55)

This is the first cut. It only makes the change for `alloc` method.
This commit is contained in:
Dylan DPC 2020-09-23 14:54:06 +02:00 committed by GitHub
commit a40d79c9fb
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 48 additions and 46 deletions

View file

@ -133,7 +133,7 @@ pub struct System;
impl System {
#[inline]
fn alloc_impl(&mut self, layout: Layout, zeroed: bool) -> Result<NonNull<[u8]>, AllocErr> {
fn alloc_impl(&self, layout: Layout, zeroed: bool) -> Result<NonNull<[u8]>, AllocErr> {
match layout.size() {
0 => Ok(NonNull::slice_from_raw_parts(layout.dangling(), 0)),
// SAFETY: `layout` is non-zero in size,
@ -152,7 +152,7 @@ impl System {
// SAFETY: Same as `AllocRef::grow`
#[inline]
unsafe fn grow_impl(
&mut self,
&self,
ptr: NonNull<u8>,
old_layout: Layout,
new_layout: Layout,
@ -190,7 +190,7 @@ impl System {
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);
AllocRef::dealloc(&self, ptr, old_layout);
Ok(new_ptr)
},
}
@ -202,17 +202,17 @@ impl System {
#[unstable(feature = "allocator_api", issue = "32838")]
unsafe impl AllocRef for System {
#[inline]
fn alloc(&mut self, layout: Layout) -> Result<NonNull<[u8]>, AllocErr> {
fn alloc(&self, layout: Layout) -> Result<NonNull<[u8]>, AllocErr> {
self.alloc_impl(layout, false)
}
#[inline]
fn alloc_zeroed(&mut self, layout: Layout) -> Result<NonNull<[u8]>, AllocErr> {
fn alloc_zeroed(&self, layout: Layout) -> Result<NonNull<[u8]>, AllocErr> {
self.alloc_impl(layout, true)
}
#[inline]
unsafe fn dealloc(&mut self, ptr: NonNull<u8>, layout: Layout) {
unsafe fn dealloc(&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
@ -222,7 +222,7 @@ unsafe impl AllocRef for System {
#[inline]
unsafe fn grow(
&mut self,
&self,
ptr: NonNull<u8>,
old_layout: Layout,
new_layout: Layout,
@ -233,7 +233,7 @@ unsafe impl AllocRef for System {
#[inline]
unsafe fn grow_zeroed(
&mut self,
&self,
ptr: NonNull<u8>,
old_layout: Layout,
new_layout: Layout,
@ -244,7 +244,7 @@ unsafe impl AllocRef for System {
#[inline]
unsafe fn shrink(
&mut self,
&self,
ptr: NonNull<u8>,
old_layout: Layout,
new_layout: Layout,
@ -257,7 +257,7 @@ unsafe impl AllocRef for System {
match new_layout.size() {
// SAFETY: conditions must be upheld by the caller
0 => unsafe {
self.dealloc(ptr, old_layout);
AllocRef::dealloc(&self, ptr, old_layout);
Ok(NonNull::slice_from_raw_parts(new_layout.dangling(), 0))
},
@ -277,9 +277,9 @@ unsafe impl AllocRef for System {
// `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 = AllocRef::alloc(&self, new_layout)?;
ptr::copy_nonoverlapping(ptr.as_ptr(), new_ptr.as_mut_ptr(), new_size);
self.dealloc(ptr, old_layout);
AllocRef::dealloc(&self, ptr, old_layout);
Ok(new_ptr)
},
}