diff options
| author | Tim Diekmann <tim.diekmann@3dvision.de> | 2020-03-25 21:12:12 +0100 |
|---|---|---|
| committer | Tim Diekmann <tim.diekmann@3dvision.de> | 2020-03-26 17:14:12 +0100 |
| commit | 03b055b0b4dcf304cd3c5e7a1c6e68fea91584a9 (patch) | |
| tree | 756da5c6b9cc313eae56f2e6c1af896597f7facf /src/liballoc/alloc.rs | |
| parent | bfbdb5f06fcff7939825c33ce573b8f92b362c40 (diff) | |
| download | rust-03b055b0b4dcf304cd3c5e7a1c6e68fea91584a9.tar.gz rust-03b055b0b4dcf304cd3c5e7a1c6e68fea91584a9.zip | |
Remove alignment from `MemoryBlock`
Diffstat (limited to 'src/liballoc/alloc.rs')
| -rw-r--r-- | src/liballoc/alloc.rs | 62 |
1 files changed, 31 insertions, 31 deletions
diff --git a/src/liballoc/alloc.rs b/src/liballoc/alloc.rs index 7eb9e0d5ea3..b0442026866 100644 --- a/src/liballoc/alloc.rs +++ b/src/liballoc/alloc.rs @@ -4,7 +4,7 @@ use core::intrinsics::{self, min_align_of_val, size_of_val}; use core::ptr::{NonNull, Unique}; -use core::{mem, usize}; +use core::usize; #[stable(feature = "alloc_module", since = "1.28.0")] #[doc(inline)] @@ -167,94 +167,94 @@ unsafe impl AllocRef for Global { #[inline] fn alloc(&mut self, layout: Layout, init: AllocInit) -> Result<MemoryBlock, AllocErr> { unsafe { - if layout.size() == 0 { - Ok(MemoryBlock::new(layout.dangling(), layout)) + let size = layout.size(); + if size == 0 { + Ok(MemoryBlock::new(layout.dangling(), 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, layout)) + Ok(MemoryBlock::new(ptr, size)) } } } #[inline] - unsafe fn dealloc(&mut self, memory: MemoryBlock) { - if memory.size() != 0 { - dealloc(memory.ptr().as_ptr(), memory.layout()) + unsafe fn dealloc(&mut self, ptr: NonNull<u8>, layout: Layout) { + if layout.size() != 0 { + dealloc(ptr.as_ptr(), layout) } } #[inline] unsafe fn grow( &mut self, - memory: &mut MemoryBlock, + ptr: NonNull<u8>, + layout: Layout, new_size: usize, placement: ReallocPlacement, init: AllocInit, - ) -> Result<(), AllocErr> { - let old_size = memory.size(); + ) -> Result<MemoryBlock, AllocErr> { + let old_size = layout.size(); debug_assert!( new_size >= old_size, "`new_size` must be greater than or equal to `memory.size()`" ); if old_size == new_size { - return Ok(()); + return Ok(MemoryBlock::new(ptr, old_size)); } - let new_layout = Layout::from_size_align_unchecked(new_size, memory.align()); match placement { - ReallocPlacement::InPlace => return Err(AllocErr), - ReallocPlacement::MayMove if memory.size() == 0 => { - *memory = self.alloc(new_layout, init)? + ReallocPlacement::InPlace => Err(AllocErr), + ReallocPlacement::MayMove if layout.size() == 0 => { + let new_layout = Layout::from_size_align_unchecked(new_size, layout.align()); + self.alloc(new_layout, init) } ReallocPlacement::MayMove => { // `realloc` probably checks for `new_size > old_size` or something similar. intrinsics::assume(new_size > old_size); - let ptr = realloc(memory.ptr().as_ptr(), memory.layout(), new_size); - *memory = MemoryBlock::new(NonNull::new(ptr).ok_or(AllocErr)?, new_layout); + 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); + Ok(memory) } } - Ok(()) } #[inline] unsafe fn shrink( &mut self, - memory: &mut MemoryBlock, + ptr: NonNull<u8>, + layout: Layout, new_size: usize, placement: ReallocPlacement, - ) -> Result<(), AllocErr> { - let old_size = memory.size(); + ) -> Result<MemoryBlock, AllocErr> { + let old_size = layout.size(); debug_assert!( new_size <= old_size, "`new_size` must be smaller than or equal to `memory.size()`" ); if old_size == new_size { - return Ok(()); + return Ok(MemoryBlock::new(ptr, old_size)); } - let new_layout = Layout::from_size_align_unchecked(new_size, memory.align()); match placement { - ReallocPlacement::InPlace => return Err(AllocErr), + ReallocPlacement::InPlace => Err(AllocErr), ReallocPlacement::MayMove if new_size == 0 => { - let new_memory = MemoryBlock::new(new_layout.dangling(), new_layout); - let old_memory = mem::replace(memory, new_memory); - self.dealloc(old_memory) + self.dealloc(ptr, layout); + Ok(MemoryBlock::new(layout.dangling(), 0)) } ReallocPlacement::MayMove => { // `realloc` probably checks for `new_size < old_size` or something similar. intrinsics::assume(new_size < old_size); - let ptr = realloc(memory.ptr().as_ptr(), memory.layout(), new_size); - *memory = MemoryBlock::new(NonNull::new(ptr).ok_or(AllocErr)?, new_layout); + let ptr = realloc(ptr.as_ptr(), layout, new_size); + Ok(MemoryBlock::new(NonNull::new(ptr).ok_or(AllocErr)?, new_size)) } } - Ok(()) } } @@ -282,7 +282,7 @@ pub(crate) unsafe fn box_free<T: ?Sized>(ptr: Unique<T>) { let size = size_of_val(ptr.as_ref()); let align = min_align_of_val(ptr.as_ref()); let layout = Layout::from_size_align_unchecked(size, align); - Global.dealloc(MemoryBlock::new(ptr.cast().into(), layout)) + Global.dealloc(ptr.cast().into(), layout) } /// Abort on memory allocation error or failure. |
