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)]
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 })
}
}
}
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 {
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)
}
}
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 })
}
}
}
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),
}
}