Make fields in `MemoryBlock` public

This commit is contained in:
Tim Diekmann 2020-03-28 20:21:26 +01:00
parent db15fe6b38
commit bf6a46db31
13 changed files with 79 additions and 100 deletions

View File

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

View File

@ -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::<u8>().as_ptr();
let mut i = memory.ptr.cast::<u8>().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);
}
}

View File

@ -198,7 +198,7 @@ impl<T> Box<T> {
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<T> Box<T> {
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()) }
}

View File

@ -152,8 +152,8 @@ impl<T, A: AllocRef> RawVec<T, A> {
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<T, A: AllocRef> RawVec<T, A> {
}
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.

View File

@ -941,7 +941,7 @@ impl<T: ?Sized> Rc<T> {
.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));

View File

@ -819,7 +819,7 @@ impl<T: ?Sized> Arc<T> {
.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));

View File

@ -26,7 +26,7 @@ fn check_overalign_requests<T: AllocRef>(mut allocator: T) {
AllocInit::Uninitialized,
)
.unwrap()
.ptr()
.ptr
})
.collect();
for &ptr in &pointers {

View File

@ -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<u8>,
size: usize,
pub ptr: NonNull<u8>,
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<u8>, size: usize) -> Self {
Self { ptr, size }
}
/// Acquires the underlying `NonNull<u8>` pointer.
#[inline]
#[unstable(feature = "allocator_api", issue = "32838")]
pub const fn ptr(&self) -> NonNull<u8> {
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)
}

View File

@ -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<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 {
@ -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<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);
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 })
}
}
}

View File

@ -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);
}
}

View File

@ -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);
}
}

View File

@ -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 {

View File

@ -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 _)
}
}