From d9d02fa168016b5b5b2033a2964a723f447f94b0 Mon Sep 17 00:00:00 2001 From: blitzerr Date: Sun, 20 Sep 2020 20:14:44 -0700 Subject: [PATCH 1/7] Changing the alloc() to accept &self instead of &mut self --- library/alloc/src/alloc.rs | 4 ++-- library/alloc/src/raw_vec/tests.rs | 15 ++++++++------- library/core/src/alloc/mod.rs | 4 ++-- library/std/src/alloc.rs | 4 ++-- src/test/ui/allocator/custom.rs | 2 +- 5 files changed, 15 insertions(+), 14 deletions(-) diff --git a/library/alloc/src/alloc.rs b/library/alloc/src/alloc.rs index 341c6816197c7..7d872b1a66bb9 100644 --- a/library/alloc/src/alloc.rs +++ b/library/alloc/src/alloc.rs @@ -145,7 +145,7 @@ pub unsafe fn alloc_zeroed(layout: Layout) -> *mut u8 { impl Global { #[inline] - fn alloc_impl(&mut self, layout: Layout, zeroed: bool) -> Result, AllocErr> { + fn alloc_impl(&self, layout: Layout, zeroed: bool) -> Result, AllocErr> { match layout.size() { 0 => Ok(NonNull::slice_from_raw_parts(layout.dangling(), 0)), // SAFETY: `layout` is non-zero in size, @@ -208,7 +208,7 @@ impl Global { #[unstable(feature = "allocator_api", issue = "32838")] unsafe impl AllocRef for Global { #[inline] - fn alloc(&mut self, layout: Layout) -> Result, AllocErr> { + fn alloc(&self, layout: Layout) -> Result, AllocErr> { self.alloc_impl(layout, false) } diff --git a/library/alloc/src/raw_vec/tests.rs b/library/alloc/src/raw_vec/tests.rs index cadd913aa6bf2..f348710d61a77 100644 --- a/library/alloc/src/raw_vec/tests.rs +++ b/library/alloc/src/raw_vec/tests.rs @@ -1,4 +1,5 @@ use super::*; +use std::cell::Cell; #[test] fn allocator_param() { @@ -17,17 +18,17 @@ fn allocator_param() { // A dumb allocator that consumes a fixed amount of fuel // before allocation attempts start failing. struct BoundedAlloc { - fuel: usize, + fuel: Cell, } unsafe impl AllocRef for BoundedAlloc { - fn alloc(&mut self, layout: Layout) -> Result, AllocErr> { + fn alloc(&self, layout: Layout) -> Result, AllocErr> { let size = layout.size(); - if size > self.fuel { + if size > self.fuel.get() { return Err(AllocErr); } match Global.alloc(layout) { ok @ Ok(_) => { - self.fuel -= size; + self.fuel.update(|old| old - size); ok } err @ Err(_) => err, @@ -38,11 +39,11 @@ fn allocator_param() { } } - let a = BoundedAlloc { fuel: 500 }; + let a = BoundedAlloc { fuel: Cell::new(500) }; let mut v: RawVec = RawVec::with_capacity_in(50, a); - assert_eq!(v.alloc.fuel, 450); + assert_eq!(v.alloc.fuel.get(), 450); v.reserve(50, 150); // (causes a realloc, thus using 50 + 150 = 200 units of fuel) - assert_eq!(v.alloc.fuel, 250); + assert_eq!(v.alloc.fuel.get(), 250); } #[test] diff --git a/library/core/src/alloc/mod.rs b/library/core/src/alloc/mod.rs index c1fda2fce641f..b7dd249d09361 100644 --- a/library/core/src/alloc/mod.rs +++ b/library/core/src/alloc/mod.rs @@ -109,7 +109,7 @@ pub unsafe trait AllocRef { /// call the [`handle_alloc_error`] function, rather than directly invoking `panic!` or similar. /// /// [`handle_alloc_error`]: ../../alloc/alloc/fn.handle_alloc_error.html - fn alloc(&mut self, layout: Layout) -> Result, AllocErr>; + fn alloc(&self, layout: Layout) -> Result, AllocErr>; /// Behaves like `alloc`, but also ensures that the returned memory is zero-initialized. /// @@ -348,7 +348,7 @@ where A: AllocRef + ?Sized, { #[inline] - fn alloc(&mut self, layout: Layout) -> Result, AllocErr> { + fn alloc(&self, layout: Layout) -> Result, AllocErr> { (**self).alloc(layout) } diff --git a/library/std/src/alloc.rs b/library/std/src/alloc.rs index 770c97899f002..86ae4cf4dd213 100644 --- a/library/std/src/alloc.rs +++ b/library/std/src/alloc.rs @@ -133,7 +133,7 @@ pub struct System; impl System { #[inline] - fn alloc_impl(&mut self, layout: Layout, zeroed: bool) -> Result, AllocErr> { + fn alloc_impl(&self, layout: Layout, zeroed: bool) -> Result, AllocErr> { match layout.size() { 0 => Ok(NonNull::slice_from_raw_parts(layout.dangling(), 0)), // SAFETY: `layout` is non-zero in size, @@ -202,7 +202,7 @@ impl System { #[unstable(feature = "allocator_api", issue = "32838")] unsafe impl AllocRef for System { #[inline] - fn alloc(&mut self, layout: Layout) -> Result, AllocErr> { + fn alloc(&self, layout: Layout) -> Result, AllocErr> { self.alloc_impl(layout, false) } diff --git a/src/test/ui/allocator/custom.rs b/src/test/ui/allocator/custom.rs index a6c2317c73669..603f59ab06927 100644 --- a/src/test/ui/allocator/custom.rs +++ b/src/test/ui/allocator/custom.rs @@ -18,7 +18,7 @@ struct A; unsafe impl alloc::GlobalAlloc for A { unsafe fn alloc(&self, layout: Layout) -> *mut u8 { HITS.fetch_add(1, Ordering::SeqCst); - System.alloc(layout) + AllocRef::alloc(&System, layout).unwrap().as_mut_ptr() } unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) { From 7e443a1ffc587858c16aeac1dda3d2dffce0ff5f Mon Sep 17 00:00:00 2001 From: blitzerr Date: Mon, 21 Sep 2020 15:52:35 -0700 Subject: [PATCH 2/7] Added feature flag to use cell_update --- library/alloc/src/raw_vec/tests.rs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/library/alloc/src/raw_vec/tests.rs b/library/alloc/src/raw_vec/tests.rs index f348710d61a77..41d6c98b48196 100644 --- a/library/alloc/src/raw_vec/tests.rs +++ b/library/alloc/src/raw_vec/tests.rs @@ -1,6 +1,10 @@ +#![feature(cell_update)] + use super::*; use std::cell::Cell; + + #[test] fn allocator_param() { use crate::alloc::AllocErr; From 219003bd2ee6778333cf92405c6ea8591ecc8816 Mon Sep 17 00:00:00 2001 From: blitzerr Date: Mon, 21 Sep 2020 16:55:07 -0700 Subject: [PATCH 3/7] replaced cell::update with cell::[g|s]et --- library/alloc/src/raw_vec/tests.rs | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/library/alloc/src/raw_vec/tests.rs b/library/alloc/src/raw_vec/tests.rs index 41d6c98b48196..2ed9d3685c69f 100644 --- a/library/alloc/src/raw_vec/tests.rs +++ b/library/alloc/src/raw_vec/tests.rs @@ -1,10 +1,6 @@ -#![feature(cell_update)] - use super::*; use std::cell::Cell; - - #[test] fn allocator_param() { use crate::alloc::AllocErr; @@ -32,7 +28,7 @@ fn allocator_param() { } match Global.alloc(layout) { ok @ Ok(_) => { - self.fuel.update(|old| old - size); + self.fuel.set(self.fuel.get() - size); ok } err @ Err(_) => err, From 3ffd403c6b97f181c189a8eb5fbd30e3e7a95b43 Mon Sep 17 00:00:00 2001 From: blitzerr Date: Tue, 22 Sep 2020 06:22:02 -0700 Subject: [PATCH 4/7] removing &mut self for other methods of AllocRef --- library/alloc/src/alloc.rs | 4 ++-- library/alloc/src/raw_vec.rs | 2 +- library/alloc/src/raw_vec/tests.rs | 2 +- library/alloc/tests/heap.rs | 2 +- library/core/src/alloc/mod.rs | 8 ++++---- library/std/src/alloc.rs | 4 ++-- 6 files changed, 11 insertions(+), 11 deletions(-) diff --git a/library/alloc/src/alloc.rs b/library/alloc/src/alloc.rs index 7d872b1a66bb9..462bcd15fa941 100644 --- a/library/alloc/src/alloc.rs +++ b/library/alloc/src/alloc.rs @@ -213,12 +213,12 @@ unsafe impl AllocRef for Global { } #[inline] - fn alloc_zeroed(&mut self, layout: Layout) -> Result, AllocErr> { + fn alloc_zeroed(&self, layout: Layout) -> Result, AllocErr> { self.alloc_impl(layout, true) } #[inline] - unsafe fn dealloc(&mut self, ptr: NonNull, layout: Layout) { + unsafe fn dealloc(&self, ptr: NonNull, layout: Layout) { if layout.size() != 0 { // SAFETY: `layout` is non-zero in size, // other conditions must be upheld by the caller diff --git a/library/alloc/src/raw_vec.rs b/library/alloc/src/raw_vec.rs index 62675665f037f..5b4a4957f6cb1 100644 --- a/library/alloc/src/raw_vec.rs +++ b/library/alloc/src/raw_vec.rs @@ -169,7 +169,7 @@ impl RawVec { Self::allocate_in(capacity, AllocInit::Zeroed, alloc) } - fn allocate_in(capacity: usize, init: AllocInit, mut alloc: A) -> Self { + fn allocate_in(capacity: usize, init: AllocInit, alloc: A) -> Self { if mem::size_of::() == 0 { Self::new_in(alloc) } else { diff --git a/library/alloc/src/raw_vec/tests.rs b/library/alloc/src/raw_vec/tests.rs index 2ed9d3685c69f..e4c8b3709dfee 100644 --- a/library/alloc/src/raw_vec/tests.rs +++ b/library/alloc/src/raw_vec/tests.rs @@ -34,7 +34,7 @@ fn allocator_param() { err @ Err(_) => err, } } - unsafe fn dealloc(&mut self, ptr: NonNull, layout: Layout) { + unsafe fn dealloc(&self, ptr: NonNull, layout: Layout) { unsafe { Global.dealloc(ptr, layout) } } } diff --git a/library/alloc/tests/heap.rs b/library/alloc/tests/heap.rs index cbde2a7e28e8f..a7239a4b14fae 100644 --- a/library/alloc/tests/heap.rs +++ b/library/alloc/tests/heap.rs @@ -11,7 +11,7 @@ fn std_heap_overaligned_request() { check_overalign_requests(Global) } -fn check_overalign_requests(mut allocator: T) { +fn check_overalign_requests(allocator: T) { for &align in &[4, 8, 16, 32] { // less than and bigger than `MIN_ALIGN` for &size in &[align / 2, align - 1] { diff --git a/library/core/src/alloc/mod.rs b/library/core/src/alloc/mod.rs index b7dd249d09361..5f9092fe7037e 100644 --- a/library/core/src/alloc/mod.rs +++ b/library/core/src/alloc/mod.rs @@ -126,7 +126,7 @@ pub unsafe trait AllocRef { /// call the [`handle_alloc_error`] function, rather than directly invoking `panic!` or similar. /// /// [`handle_alloc_error`]: ../../alloc/alloc/fn.handle_alloc_error.html - fn alloc_zeroed(&mut self, layout: Layout) -> Result, AllocErr> { + fn alloc_zeroed(&self, layout: Layout) -> Result, AllocErr> { let ptr = self.alloc(layout)?; // SAFETY: `alloc` returns a valid memory block unsafe { ptr.as_non_null_ptr().as_ptr().write_bytes(0, ptr.len()) } @@ -142,7 +142,7 @@ pub unsafe trait AllocRef { /// /// [*currently allocated*]: #currently-allocated-memory /// [*fit*]: #memory-fitting - unsafe fn dealloc(&mut self, ptr: NonNull, layout: Layout); + unsafe fn dealloc(&self, ptr: NonNull, layout: Layout); /// Attempts to extend the memory block. /// @@ -353,12 +353,12 @@ where } #[inline] - fn alloc_zeroed(&mut self, layout: Layout) -> Result, AllocErr> { + fn alloc_zeroed(&self, layout: Layout) -> Result, AllocErr> { (**self).alloc_zeroed(layout) } #[inline] - unsafe fn dealloc(&mut self, ptr: NonNull, layout: Layout) { + unsafe fn dealloc(&self, ptr: NonNull, layout: Layout) { // SAFETY: the safety contract must be upheld by the caller unsafe { (**self).dealloc(ptr, layout) } } diff --git a/library/std/src/alloc.rs b/library/std/src/alloc.rs index 86ae4cf4dd213..f41aa28b5ecb5 100644 --- a/library/std/src/alloc.rs +++ b/library/std/src/alloc.rs @@ -207,12 +207,12 @@ unsafe impl AllocRef for System { } #[inline] - fn alloc_zeroed(&mut self, layout: Layout) -> Result, AllocErr> { + fn alloc_zeroed(&self, layout: Layout) -> Result, AllocErr> { self.alloc_impl(layout, true) } #[inline] - unsafe fn dealloc(&mut self, ptr: NonNull, layout: Layout) { + unsafe fn dealloc(&self, ptr: NonNull, layout: Layout) { if layout.size() != 0 { // SAFETY: `layout` is non-zero in size, // other conditions must be upheld by the caller From 14736ca20b2e5f60981743c6ee2dfa62db818767 Mon Sep 17 00:00:00 2001 From: blitzerr Date: Tue, 22 Sep 2020 08:51:20 -0700 Subject: [PATCH 5/7] fixing the custom.rs --- src/test/ui/allocator/custom.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/ui/allocator/custom.rs b/src/test/ui/allocator/custom.rs index 603f59ab06927..f54ef1f0bc152 100644 --- a/src/test/ui/allocator/custom.rs +++ b/src/test/ui/allocator/custom.rs @@ -23,7 +23,7 @@ unsafe impl alloc::GlobalAlloc for A { unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) { HITS.fetch_add(1, Ordering::SeqCst); - System.dealloc(ptr, layout) + AllocRef::dealloc(&System, ptr, layout) } } From 985dff9e7ed1fd7896b071eaf637fd531a690e91 Mon Sep 17 00:00:00 2001 From: blitzerr Date: Tue, 22 Sep 2020 20:16:13 -0700 Subject: [PATCH 6/7] fixing the test failure --- src/test/ui/allocator/custom.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/test/ui/allocator/custom.rs b/src/test/ui/allocator/custom.rs index f54ef1f0bc152..73750bf139a7d 100644 --- a/src/test/ui/allocator/custom.rs +++ b/src/test/ui/allocator/custom.rs @@ -10,6 +10,7 @@ extern crate helper; use std::alloc::{self, AllocRef, Global, Layout, System}; use std::sync::atomic::{AtomicUsize, Ordering}; +use std::ptr::NonNull; static HITS: AtomicUsize = AtomicUsize::new(0); @@ -23,7 +24,7 @@ unsafe impl alloc::GlobalAlloc for A { unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) { HITS.fetch_add(1, Ordering::SeqCst); - AllocRef::dealloc(&System, ptr, layout) + AllocRef::dealloc(&System, NonNull::new(ptr).unwrap(), layout) } } From 2b19b14cecbcdd173e29a801baff71e31cae7331 Mon Sep 17 00:00:00 2001 From: blitzerr Date: Tue, 22 Sep 2020 21:04:31 -0700 Subject: [PATCH 7/7] a few more &mut self -> self changes --- library/alloc/src/alloc.rs | 8 ++++---- library/core/src/alloc/mod.rs | 16 ++++++++-------- library/std/src/alloc.rs | 16 ++++++++-------- src/test/ui/allocator/custom.rs | 2 +- 4 files changed, 21 insertions(+), 21 deletions(-) diff --git a/library/alloc/src/alloc.rs b/library/alloc/src/alloc.rs index 462bcd15fa941..8b8cdbf252555 100644 --- a/library/alloc/src/alloc.rs +++ b/library/alloc/src/alloc.rs @@ -160,7 +160,7 @@ impl Global { // SAFETY: Same as `AllocRef::grow` #[inline] unsafe fn grow_impl( - &mut self, + &self, ptr: NonNull, old_layout: Layout, new_layout: Layout, @@ -228,7 +228,7 @@ unsafe impl AllocRef for Global { #[inline] unsafe fn grow( - &mut self, + &self, ptr: NonNull, old_layout: Layout, new_layout: Layout, @@ -239,7 +239,7 @@ unsafe impl AllocRef for Global { #[inline] unsafe fn grow_zeroed( - &mut self, + &self, ptr: NonNull, old_layout: Layout, new_layout: Layout, @@ -250,7 +250,7 @@ unsafe impl AllocRef for Global { #[inline] unsafe fn shrink( - &mut self, + &self, ptr: NonNull, old_layout: Layout, new_layout: Layout, diff --git a/library/core/src/alloc/mod.rs b/library/core/src/alloc/mod.rs index 5f9092fe7037e..f9eb8981bbfc2 100644 --- a/library/core/src/alloc/mod.rs +++ b/library/core/src/alloc/mod.rs @@ -183,7 +183,7 @@ pub unsafe trait AllocRef { /// /// [`handle_alloc_error`]: ../../alloc/alloc/fn.handle_alloc_error.html unsafe fn grow( - &mut self, + &self, ptr: NonNull, old_layout: Layout, new_layout: Layout, @@ -244,7 +244,7 @@ pub unsafe trait AllocRef { /// /// [`handle_alloc_error`]: ../../alloc/alloc/fn.handle_alloc_error.html unsafe fn grow_zeroed( - &mut self, + &self, ptr: NonNull, old_layout: Layout, new_layout: Layout, @@ -308,7 +308,7 @@ pub unsafe trait AllocRef { /// /// [`handle_alloc_error`]: ../../alloc/alloc/fn.handle_alloc_error.html unsafe fn shrink( - &mut self, + &self, ptr: NonNull, old_layout: Layout, new_layout: Layout, @@ -337,13 +337,13 @@ pub unsafe trait AllocRef { /// /// The returned adaptor also implements `AllocRef` and will simply borrow this. #[inline(always)] - fn by_ref(&mut self) -> &mut Self { + fn by_ref(&mut self) -> &Self { self } } #[unstable(feature = "allocator_api", issue = "32838")] -unsafe impl AllocRef for &mut A +unsafe impl AllocRef for &A where A: AllocRef + ?Sized, { @@ -365,7 +365,7 @@ where #[inline] unsafe fn grow( - &mut self, + &self, ptr: NonNull, old_layout: Layout, new_layout: Layout, @@ -376,7 +376,7 @@ where #[inline] unsafe fn grow_zeroed( - &mut self, + &self, ptr: NonNull, old_layout: Layout, new_layout: Layout, @@ -387,7 +387,7 @@ where #[inline] unsafe fn shrink( - &mut self, + &self, ptr: NonNull, old_layout: Layout, new_layout: Layout, diff --git a/library/std/src/alloc.rs b/library/std/src/alloc.rs index f41aa28b5ecb5..ba158511f64c0 100644 --- a/library/std/src/alloc.rs +++ b/library/std/src/alloc.rs @@ -152,7 +152,7 @@ impl System { // SAFETY: Same as `AllocRef::grow` #[inline] unsafe fn grow_impl( - &mut self, + &self, ptr: NonNull, 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) }, } @@ -222,7 +222,7 @@ unsafe impl AllocRef for System { #[inline] unsafe fn grow( - &mut self, + &self, ptr: NonNull, 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, old_layout: Layout, new_layout: Layout, @@ -244,7 +244,7 @@ unsafe impl AllocRef for System { #[inline] unsafe fn shrink( - &mut self, + &self, ptr: NonNull, 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) }, } diff --git a/src/test/ui/allocator/custom.rs b/src/test/ui/allocator/custom.rs index 73750bf139a7d..dfb5d3e9e38d0 100644 --- a/src/test/ui/allocator/custom.rs +++ b/src/test/ui/allocator/custom.rs @@ -19,7 +19,7 @@ struct A; unsafe impl alloc::GlobalAlloc for A { unsafe fn alloc(&self, layout: Layout) -> *mut u8 { HITS.fetch_add(1, Ordering::SeqCst); - AllocRef::alloc(&System, layout).unwrap().as_mut_ptr() + alloc::GlobalAlloc::alloc(&System, layout) } unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) {