]> git.lizzy.rs Git - rust.git/blobdiff - src/liballoc/alloc.rs
Rollup merge of #71767 - tshepang:stack-stuff, r=jonas-schievink
[rust.git] / src / liballoc / alloc.rs
index b0442026866456a64e88a70d30450a6a3ca25ea4..d31c73cc1bd8d576e3e2492d46bce1e0c3e34f36 100644 (file)
@@ -4,7 +4,6 @@
 
 use core::intrinsics::{self, min_align_of_val, size_of_val};
 use core::ptr::{NonNull, Unique};
-use core::usize;
 
 #[stable(feature = "alloc_module", since = "1.28.0")]
 #[doc(inline)]
@@ -169,14 +168,14 @@ fn alloc(&mut self, layout: Layout, init: AllocInit) -> Result<MemoryBlock, Allo
         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 +196,14 @@ unsafe fn grow(
         placement: ReallocPlacement,
         init: AllocInit,
     ) -> Result<MemoryBlock, AllocErr> {
-        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 {
@@ -214,11 +213,12 @@ unsafe fn grow(
                 self.alloc(new_layout, init)
             }
             ReallocPlacement::MayMove => {
-                // `realloc` probably checks for `new_size > old_size` or something similar.
-                intrinsics::assume(new_size > old_size);
+                // `realloc` probably checks for `new_size > size` or something similar.
+                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 memory =
+                    MemoryBlock { ptr: NonNull::new(ptr).ok_or(AllocErr)?, size: new_size };
+                init.init_offset(memory, size);
                 Ok(memory)
             }
         }
@@ -232,27 +232,27 @@ unsafe fn shrink(
         new_size: usize,
         placement: ReallocPlacement,
     ) -> Result<MemoryBlock, AllocErr> {
-        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);
+                // `realloc` probably checks for `new_size < size` or something similar.
+                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 +266,7 @@ unsafe fn shrink(
 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),
     }
 }