diff --git a/src/liballoc/alloc.rs b/src/liballoc/alloc.rs index b0442026866..67927629ed3 100644 --- a/src/liballoc/alloc.rs +++ b/src/liballoc/alloc.rs @@ -169,14 +169,14 @@ unsafe impl AllocRef for Global { unsafe { let size = layout.size(); if size == 0 { - Ok(MemoryBlock::new(layout.dangling(), 0)) + Ok(MemoryBlock { ptr: layout.dangling(), size: 0 }) } else { let raw_ptr = match init { AllocInit::Uninitialized => alloc(layout), AllocInit::Zeroed => alloc_zeroed(layout), }; let ptr = NonNull::new(raw_ptr).ok_or(AllocErr)?; - Ok(MemoryBlock::new(ptr, size)) + Ok(MemoryBlock { ptr, size }) } } } @@ -197,14 +197,14 @@ unsafe impl AllocRef for Global { placement: ReallocPlacement, init: AllocInit, ) -> Result { - let old_size = layout.size(); + let size = layout.size(); debug_assert!( - new_size >= old_size, + new_size >= size, "`new_size` must be greater than or equal to `memory.size()`" ); - if old_size == new_size { - return Ok(MemoryBlock::new(ptr, old_size)); + if size == new_size { + return Ok(MemoryBlock { ptr, size }); } match placement { @@ -215,10 +215,11 @@ unsafe impl AllocRef for Global { } ReallocPlacement::MayMove => { // `realloc` probably checks for `new_size > old_size` or something similar. - intrinsics::assume(new_size > old_size); + intrinsics::assume(new_size > size); let ptr = realloc(ptr.as_ptr(), layout, new_size); - let mut memory = MemoryBlock::new(NonNull::new(ptr).ok_or(AllocErr)?, new_size); - memory.init_offset(init, old_size); + let mut memory = + MemoryBlock { ptr: NonNull::new(ptr).ok_or(AllocErr)?, size: new_size }; + memory.init_offset(init, size); Ok(memory) } } @@ -232,27 +233,27 @@ unsafe impl AllocRef for Global { new_size: usize, placement: ReallocPlacement, ) -> Result { - let old_size = layout.size(); + let size = layout.size(); debug_assert!( - new_size <= old_size, + new_size <= size, "`new_size` must be smaller than or equal to `memory.size()`" ); - if old_size == new_size { - return Ok(MemoryBlock::new(ptr, old_size)); + if size == new_size { + return Ok(MemoryBlock { ptr, size }); } match placement { ReallocPlacement::InPlace => Err(AllocErr), ReallocPlacement::MayMove if new_size == 0 => { self.dealloc(ptr, layout); - Ok(MemoryBlock::new(layout.dangling(), 0)) + Ok(MemoryBlock { ptr: layout.dangling(), size: 0 }) } ReallocPlacement::MayMove => { // `realloc` probably checks for `new_size < old_size` or something similar. - intrinsics::assume(new_size < old_size); + intrinsics::assume(new_size < size); let ptr = realloc(ptr.as_ptr(), layout, new_size); - Ok(MemoryBlock::new(NonNull::new(ptr).ok_or(AllocErr)?, new_size)) + Ok(MemoryBlock { ptr: NonNull::new(ptr).ok_or(AllocErr)?, size: new_size }) } } } @@ -266,7 +267,7 @@ unsafe impl AllocRef for Global { unsafe fn exchange_malloc(size: usize, align: usize) -> *mut u8 { let layout = Layout::from_size_align_unchecked(size, align); match Global.alloc(layout, AllocInit::Uninitialized) { - Ok(memory) => memory.ptr().as_ptr(), + Ok(memory) => memory.ptr.as_ptr(), Err(_) => handle_alloc_error(layout), } } diff --git a/src/liballoc/alloc/tests.rs b/src/liballoc/alloc/tests.rs index 7fa71f72ee7..1ad40eca93b 100644 --- a/src/liballoc/alloc/tests.rs +++ b/src/liballoc/alloc/tests.rs @@ -12,13 +12,13 @@ fn allocate_zeroed() { .alloc(layout.clone(), AllocInit::Zeroed) .unwrap_or_else(|_| handle_alloc_error(layout)); - let mut i = memory.ptr().cast::().as_ptr(); + let mut i = memory.ptr.cast::().as_ptr(); let end = i.add(layout.size()); while i < end { assert_eq!(*i, 0); i = i.offset(1); } - Global.dealloc(memory.ptr(), layout); + Global.dealloc(memory.ptr, layout); } } diff --git a/src/liballoc/boxed.rs b/src/liballoc/boxed.rs index 03d759e4a9a..5406956a528 100644 --- a/src/liballoc/boxed.rs +++ b/src/liballoc/boxed.rs @@ -198,7 +198,7 @@ impl Box { let ptr = Global .alloc(layout, AllocInit::Uninitialized) .unwrap_or_else(|_| alloc::handle_alloc_error(layout)) - .ptr() + .ptr .cast(); unsafe { Box::from_raw(ptr.as_ptr()) } } @@ -227,7 +227,7 @@ impl Box { let ptr = Global .alloc(layout, AllocInit::Zeroed) .unwrap_or_else(|_| alloc::handle_alloc_error(layout)) - .ptr() + .ptr .cast(); unsafe { Box::from_raw(ptr.as_ptr()) } } diff --git a/src/liballoc/raw_vec.rs b/src/liballoc/raw_vec.rs index a1f9a9291af..590e82357fb 100644 --- a/src/liballoc/raw_vec.rs +++ b/src/liballoc/raw_vec.rs @@ -152,8 +152,8 @@ impl RawVec { let memory = alloc.alloc(layout, init).unwrap_or_else(|_| handle_alloc_error(layout)); Self { - ptr: memory.ptr().cast().into(), - cap: Self::capacity_from_bytes(memory.size()), + ptr: memory.ptr.cast().into(), + cap: Self::capacity_from_bytes(memory.size), alloc, } } @@ -470,8 +470,8 @@ impl RawVec { } fn set_memory(&mut self, memory: MemoryBlock) { - self.ptr = memory.ptr().cast().into(); - self.cap = Self::capacity_from_bytes(memory.size()); + self.ptr = memory.ptr.cast().into(); + self.cap = Self::capacity_from_bytes(memory.size); } /// Single method to handle all possibilities of growing the buffer. diff --git a/src/liballoc/rc.rs b/src/liballoc/rc.rs index ab344be12de..6a78a7398a6 100644 --- a/src/liballoc/rc.rs +++ b/src/liballoc/rc.rs @@ -941,7 +941,7 @@ impl Rc { .unwrap_or_else(|_| handle_alloc_error(layout)); // Initialize the RcBox - let inner = mem_to_rcbox(mem.ptr().as_ptr()); + let inner = mem_to_rcbox(mem.ptr.as_ptr()); debug_assert_eq!(Layout::for_value(&*inner), layout); ptr::write(&mut (*inner).strong, Cell::new(1)); diff --git a/src/liballoc/sync.rs b/src/liballoc/sync.rs index 1adc7fa3040..111a7651b5e 100644 --- a/src/liballoc/sync.rs +++ b/src/liballoc/sync.rs @@ -819,7 +819,7 @@ impl Arc { .unwrap_or_else(|_| handle_alloc_error(layout)); // Initialize the ArcInner - let inner = mem_to_arcinner(mem.ptr().as_ptr()); + let inner = mem_to_arcinner(mem.ptr.as_ptr()); debug_assert_eq!(Layout::for_value(&*inner), layout); ptr::write(&mut (*inner).strong, atomic::AtomicUsize::new(1)); diff --git a/src/liballoc/tests/heap.rs b/src/liballoc/tests/heap.rs index 709e8c148d5..62f062b83d7 100644 --- a/src/liballoc/tests/heap.rs +++ b/src/liballoc/tests/heap.rs @@ -26,7 +26,7 @@ fn check_overalign_requests(mut allocator: T) { AllocInit::Uninitialized, ) .unwrap() - .ptr() + .ptr }) .collect(); for &ptr in &pointers { diff --git a/src/libcore/alloc/mod.rs b/src/libcore/alloc/mod.rs index d5e89f333f1..f2f12a98fa6 100644 --- a/src/libcore/alloc/mod.rs +++ b/src/libcore/alloc/mod.rs @@ -42,35 +42,14 @@ pub enum AllocInit { } /// Represents a block of allocated memory returned by an allocator. -#[derive(Debug)] +#[derive(Debug, Copy, Clone)] #[unstable(feature = "allocator_api", issue = "32838")] pub struct MemoryBlock { - ptr: NonNull, - size: usize, + pub ptr: NonNull, + pub size: usize, } impl MemoryBlock { - /// Creates a new `MemoryBlock` from the specified `ptr` and `size`. - #[inline] - #[unstable(feature = "allocator_api", issue = "32838")] - pub const fn new(ptr: NonNull, size: usize) -> Self { - Self { ptr, size } - } - - /// Acquires the underlying `NonNull` pointer. - #[inline] - #[unstable(feature = "allocator_api", issue = "32838")] - pub const fn ptr(&self) -> NonNull { - self.ptr - } - - /// Returns the size of the memory block. - #[inline] - #[unstable(feature = "allocator_api", issue = "32838")] - pub const fn size(&self) -> usize { - self.size - } - /// Initialize the memory block like specified by `init`. /// /// This behaves like calling [`MemoryBlock::initialize_offset(ptr, layout, 0)`][off]. @@ -98,12 +77,10 @@ impl MemoryBlock { #[inline] #[unstable(feature = "allocator_api", issue = "32838")] pub unsafe fn init_offset(&mut self, init: AllocInit, offset: usize) { - debug_assert!(offset <= self.size(), "`offset` must be smaller than or equal to `size()`"); + debug_assert!(offset <= self.size, "`offset` must be smaller than or equal to `size()`"); match init { AllocInit::Uninitialized => (), - AllocInit::Zeroed => { - self.ptr().as_ptr().add(offset).write_bytes(0, self.size() - offset) - } + AllocInit::Zeroed => self.ptr.as_ptr().add(offset).write_bytes(0, self.size - offset), } } } @@ -246,9 +223,9 @@ pub unsafe trait AllocRef { /// /// * `ptr` must be [*currently allocated*] via this allocator, /// * `layout` must [*fit*] the `ptr`. (The `new_size` argument need not fit it.) - // We can't require that `new_size` is strictly greater than `memory.size()` because of ZSTs. + // 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 strictly greater than `memory.size` or both are zero /// * `new_size` must be greater than or equal to `layout.size()` /// * `new_size`, when rounded up to the nearest multiple of `layout.align()`, must not overflow /// (i.e., the rounded value must be less than `usize::MAX`). @@ -280,19 +257,19 @@ pub unsafe trait AllocRef { match placement { ReallocPlacement::InPlace => Err(AllocErr), ReallocPlacement::MayMove => { - let old_size = layout.size(); + let size = layout.size(); debug_assert!( - new_size >= old_size, + new_size >= size, "`new_size` must be greater than or equal to `layout.size()`" ); - if new_size == old_size { - return Ok(MemoryBlock::new(ptr, old_size)); + if new_size == size { + return Ok(MemoryBlock { ptr, size }); } let new_layout = Layout::from_size_align_unchecked(new_size, layout.align()); let new_memory = self.alloc(new_layout, init)?; - ptr::copy_nonoverlapping(ptr.as_ptr(), new_memory.ptr().as_ptr(), old_size); + ptr::copy_nonoverlapping(ptr.as_ptr(), new_memory.ptr.as_ptr(), size); self.dealloc(ptr, layout); Ok(new_memory) } @@ -324,10 +301,10 @@ pub unsafe trait AllocRef { /// /// * `ptr` must be [*currently allocated*] via this allocator, /// * `layout` must [*fit*] the `ptr`. (The `new_size` argument need not fit it.) - // We can't require that `new_size` is strictly smaller than `memory.size()` because of ZSTs. + // 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 `memory.size()` + // * `new_size must be strictly smaller than `memory.size` or both are zero + /// * `new_size` must be smaller than or equal to `layout.size()` /// /// [*currently allocated*]: #currently-allocated-memory /// [*fit*]: #memory-fitting @@ -355,19 +332,19 @@ pub unsafe trait AllocRef { match placement { ReallocPlacement::InPlace => Err(AllocErr), ReallocPlacement::MayMove => { - let old_size = layout.size(); + let size = layout.size(); debug_assert!( - new_size <= old_size, + new_size <= size, "`new_size` must be smaller than or equal to `layout.size()`" ); - if new_size == old_size { - return Ok(MemoryBlock::new(ptr, old_size)); + if new_size == size { + return Ok(MemoryBlock { ptr, size }); } let new_layout = Layout::from_size_align_unchecked(new_size, layout.align()); let new_memory = self.alloc(new_layout, AllocInit::Uninitialized)?; - ptr::copy_nonoverlapping(ptr.as_ptr(), new_memory.ptr().as_ptr(), new_size); + ptr::copy_nonoverlapping(ptr.as_ptr(), new_memory.ptr.as_ptr(), new_size); self.dealloc(ptr, layout); Ok(new_memory) } diff --git a/src/libstd/alloc.rs b/src/libstd/alloc.rs index 7f3a5d2849b..843c46775af 100644 --- a/src/libstd/alloc.rs +++ b/src/libstd/alloc.rs @@ -143,14 +143,14 @@ unsafe impl AllocRef for System { unsafe { let size = layout.size(); if size == 0 { - Ok(MemoryBlock::new(layout.dangling(), 0)) + Ok(MemoryBlock { ptr: layout.dangling(), size: 0 }) } else { let raw_ptr = match init { AllocInit::Uninitialized => GlobalAlloc::alloc(self, layout), AllocInit::Zeroed => GlobalAlloc::alloc_zeroed(self, layout), }; let ptr = NonNull::new(raw_ptr).ok_or(AllocErr)?; - Ok(MemoryBlock::new(ptr, size)) + Ok(MemoryBlock { ptr, size }) } } } @@ -171,14 +171,14 @@ unsafe impl AllocRef for System { placement: ReallocPlacement, init: AllocInit, ) -> Result { - let old_size = layout.size(); + let size = layout.size(); debug_assert!( - new_size >= old_size, + new_size >= size, "`new_size` must be greater than or equal to `memory.size()`" ); - if old_size == new_size { - return Ok(MemoryBlock::new(ptr, old_size)); + if size == new_size { + return Ok(MemoryBlock { ptr, size }); } match placement { @@ -189,10 +189,11 @@ unsafe impl AllocRef for System { } ReallocPlacement::MayMove => { // `realloc` probably checks for `new_size > old_size` or something similar. - intrinsics::assume(new_size > old_size); + intrinsics::assume(new_size > size); let ptr = GlobalAlloc::realloc(self, ptr.as_ptr(), layout, new_size); - let mut memory = MemoryBlock::new(NonNull::new(ptr).ok_or(AllocErr)?, new_size); - memory.init_offset(init, old_size); + let mut memory = + MemoryBlock { ptr: NonNull::new(ptr).ok_or(AllocErr)?, size: new_size }; + memory.init_offset(init, size); Ok(memory) } } @@ -206,27 +207,27 @@ unsafe impl AllocRef for System { new_size: usize, placement: ReallocPlacement, ) -> Result { - let old_size = layout.size(); + let size = layout.size(); debug_assert!( - new_size <= old_size, + new_size <= size, "`new_size` must be smaller than or equal to `memory.size()`" ); - if old_size == new_size { - return Ok(MemoryBlock::new(ptr, old_size)); + if size == new_size { + return Ok(MemoryBlock { ptr, size }); } match placement { ReallocPlacement::InPlace => Err(AllocErr), ReallocPlacement::MayMove if new_size == 0 => { self.dealloc(ptr, layout); - Ok(MemoryBlock::new(layout.dangling(), 0)) + Ok(MemoryBlock { ptr: layout.dangling(), size: 0 }) } ReallocPlacement::MayMove => { // `realloc` probably checks for `new_size < old_size` or something similar. - intrinsics::assume(new_size < old_size); + intrinsics::assume(new_size < size); let ptr = GlobalAlloc::realloc(self, ptr.as_ptr(), layout, new_size); - Ok(MemoryBlock::new(NonNull::new(ptr).ok_or(AllocErr)?, new_size)) + Ok(MemoryBlock { ptr: NonNull::new(ptr).ok_or(AllocErr)?, size: new_size }) } } } diff --git a/src/test/ui/allocator/custom.rs b/src/test/ui/allocator/custom.rs index 8f894c5db5d..184e4706a4c 100644 --- a/src/test/ui/allocator/custom.rs +++ b/src/test/ui/allocator/custom.rs @@ -38,9 +38,9 @@ fn main() { let layout = Layout::from_size_align(4, 2).unwrap(); let memory = Global.alloc(layout.clone(), AllocInit::Uninitialized).unwrap(); - helper::work_with(&memory.ptr()); + helper::work_with(&memory.ptr); assert_eq!(HITS.load(Ordering::SeqCst), n + 1); - Global.dealloc(memory.ptr(), layout); + Global.dealloc(memory.ptr, layout); assert_eq!(HITS.load(Ordering::SeqCst), n + 2); let s = String::with_capacity(10); @@ -51,8 +51,8 @@ fn main() { let memory = System.alloc(layout.clone(), AllocInit::Uninitialized).unwrap(); assert_eq!(HITS.load(Ordering::SeqCst), n + 4); - helper::work_with(&memory.ptr()); - System.dealloc(memory.ptr(), layout); + helper::work_with(&memory.ptr); + System.dealloc(memory.ptr, layout); assert_eq!(HITS.load(Ordering::SeqCst), n + 4); } } diff --git a/src/test/ui/allocator/xcrate-use.rs b/src/test/ui/allocator/xcrate-use.rs index 689804bde86..7de1ab7a553 100644 --- a/src/test/ui/allocator/xcrate-use.rs +++ b/src/test/ui/allocator/xcrate-use.rs @@ -21,15 +21,15 @@ fn main() { let layout = Layout::from_size_align(4, 2).unwrap(); let memory = Global.alloc(layout.clone(), AllocInit::Uninitialized).unwrap(); - helper::work_with(&memory.ptr()); + helper::work_with(&memory.ptr); assert_eq!(GLOBAL.0.load(Ordering::SeqCst), n + 1); - Global.dealloc(memory.ptr(), layout); + Global.dealloc(memory.ptr, layout); assert_eq!(GLOBAL.0.load(Ordering::SeqCst), n + 2); let memory = System.alloc(layout.clone(), AllocInit::Uninitialized).unwrap(); assert_eq!(GLOBAL.0.load(Ordering::SeqCst), n + 2); - helper::work_with(&memory.ptr()); - System.dealloc(memory.ptr(), layout); + helper::work_with(&memory.ptr); + System.dealloc(memory.ptr, layout); assert_eq!(GLOBAL.0.load(Ordering::SeqCst), n + 2); } } diff --git a/src/test/ui/realloc-16687.rs b/src/test/ui/realloc-16687.rs index 8c419185f51..0687a9ce454 100644 --- a/src/test/ui/realloc-16687.rs +++ b/src/test/ui/realloc-16687.rs @@ -46,10 +46,10 @@ unsafe fn test_triangle() -> bool { .unwrap_or_else(|_| handle_alloc_error(layout)); if PRINT { - println!("allocate({:?}) = {:?}", layout, memory.ptr()); + println!("allocate({:?}) = {:?}", layout, memory.ptr); } - memory.ptr().cast().as_ptr() + memory.ptr.cast().as_ptr() } unsafe fn deallocate(ptr: *mut u8, layout: Layout) { @@ -82,9 +82,9 @@ unsafe fn test_triangle() -> bool { }); if PRINT { - println!("reallocate({:?}, old={:?}, new={:?}) = {:?}", ptr, old, new, memory.ptr()); + println!("reallocate({:?}, old={:?}, new={:?}) = {:?}", ptr, old, new, memory.ptr); } - memory.ptr().cast().as_ptr() + memory.ptr.cast().as_ptr() } fn idx_to_size(i: usize) -> usize { diff --git a/src/test/ui/regions/regions-mock-codegen.rs b/src/test/ui/regions/regions-mock-codegen.rs index 148b0a86a05..380310190be 100644 --- a/src/test/ui/regions/regions-mock-codegen.rs +++ b/src/test/ui/regions/regions-mock-codegen.rs @@ -28,7 +28,7 @@ fn alloc(_bcx: &arena) -> &Bcx<'_> { let memory = Global .alloc(layout, AllocInit::Uninitialized) .unwrap_or_else(|_| handle_alloc_error(layout)); - &*(memory.ptr().as_ptr() as *const _) + &*(memory.ptr.as_ptr() as *const _) } }