Simplify implementations of AllocRef for Global and System

This commit is contained in:
Tim Diekmann 2020-07-29 11:41:36 +02:00
parent 076ef66ba2
commit b01fbc437e
2 changed files with 143 additions and 195 deletions

View file

@ -165,34 +165,33 @@ pub unsafe fn alloc_zeroed(layout: Layout) -> *mut u8 {
unsafe impl AllocRef for Global { unsafe impl AllocRef for Global {
#[inline] #[inline]
fn alloc(&mut self, layout: Layout) -> Result<MemoryBlock, AllocErr> { fn alloc(&mut self, layout: Layout) -> Result<MemoryBlock, AllocErr> {
unsafe {
let size = layout.size(); let size = layout.size();
if size == 0 { let ptr = if size == 0 {
Ok(MemoryBlock { ptr: layout.dangling(), size: 0 }) layout.dangling()
} else { } else {
let raw_ptr = alloc(layout); // SAFETY: `layout` is non-zero in size,
let ptr = NonNull::new(raw_ptr).ok_or(AllocErr)?; unsafe { NonNull::new(alloc(layout)).ok_or(AllocErr)? }
};
Ok(MemoryBlock { ptr, size }) Ok(MemoryBlock { ptr, size })
} }
}
}
#[inline]
fn alloc_zeroed(&mut self, layout: Layout) -> Result<MemoryBlock, AllocErr> { fn alloc_zeroed(&mut self, layout: Layout) -> Result<MemoryBlock, AllocErr> {
unsafe {
let size = layout.size(); let size = layout.size();
if size == 0 { let ptr = if size == 0 {
Ok(MemoryBlock { ptr: layout.dangling(), size: 0 }) layout.dangling()
} else { } else {
let raw_ptr = alloc_zeroed(layout); // SAFETY: `layout` is non-zero in size,
let ptr = NonNull::new(raw_ptr).ok_or(AllocErr)?; unsafe { NonNull::new(alloc_zeroed(layout)).ok_or(AllocErr)? }
};
Ok(MemoryBlock { ptr, size }) Ok(MemoryBlock { ptr, size })
} }
}
}
#[inline] #[inline]
unsafe fn dealloc(&mut self, ptr: NonNull<u8>, layout: Layout) { unsafe fn dealloc(&mut self, ptr: NonNull<u8>, layout: Layout) {
if layout.size() != 0 { if layout.size() != 0 {
// SAFETY: `layout` is non-zero in size,
// other conditions must be upheld by the caller
unsafe { dealloc(ptr.as_ptr(), layout) } unsafe { dealloc(ptr.as_ptr(), layout) }
} }
} }
@ -204,59 +203,55 @@ unsafe impl AllocRef for Global {
layout: Layout, layout: Layout,
new_size: usize, new_size: usize,
) -> Result<MemoryBlock, AllocErr> { ) -> Result<MemoryBlock, AllocErr> {
let size = layout.size();
debug_assert!( debug_assert!(
new_size >= size, new_size >= layout.size(),
"`new_size` must be greater than or equal to `memory.size()`" "`new_size` must be greater than or equal to `layout.size()`"
); );
if size == new_size { // SAFETY: `new_size` must be non-zero, which is checked in the match expression.
return Ok(MemoryBlock { ptr, size }); // Other conditions must be upheld by the caller
} unsafe {
match layout.size() {
if layout.size() == 0 { old_size if old_size == new_size => Ok(MemoryBlock { ptr, size: new_size }),
let new_layout = unsafe { Layout::from_size_align_unchecked(new_size, layout.align()) }; 0 => self.alloc(Layout::from_size_align_unchecked(new_size, layout.align())),
self.alloc(new_layout) old_size => {
} else {
// `realloc` probably checks for `new_size > size` or something similar. // `realloc` probably checks for `new_size > size` or something similar.
let ptr = unsafe { intrinsics::assume(new_size > old_size);
intrinsics::assume(new_size > size); let raw_ptr = realloc(ptr.as_ptr(), layout, new_size);
realloc(ptr.as_ptr(), layout, new_size) let ptr = NonNull::new(raw_ptr).ok_or(AllocErr)?;
}; Ok(MemoryBlock { ptr, size: new_size })
Ok(MemoryBlock { ptr: NonNull::new(ptr).ok_or(AllocErr)?, size: new_size }) }
}
} }
} }
#[inline]
unsafe fn grow_zeroed( unsafe fn grow_zeroed(
&mut self, &mut self,
ptr: NonNull<u8>, ptr: NonNull<u8>,
layout: Layout, layout: Layout,
new_size: usize, new_size: usize,
) -> Result<MemoryBlock, AllocErr> { ) -> Result<MemoryBlock, AllocErr> {
let size = layout.size();
debug_assert!( debug_assert!(
new_size >= size, new_size >= layout.size(),
"`new_size` must be greater than or equal to `memory.size()`" "`new_size` must be greater than or equal to `layout.size()`"
); );
if size == new_size { // SAFETY: `new_size` must be non-zero, which is checked in the match expression.
return Ok(MemoryBlock { ptr, size }); // Other conditions must be upheld by the caller
}
if layout.size() == 0 {
let new_layout = unsafe { Layout::from_size_align_unchecked(new_size, layout.align()) };
self.alloc(new_layout)
} else {
// `realloc` probably checks for `new_size > size` or something similar.
let ptr = unsafe {
intrinsics::assume(new_size > size);
realloc(ptr.as_ptr(), layout, new_size)
};
let memory = MemoryBlock { ptr: NonNull::new(ptr).ok_or(AllocErr)?, size: new_size };
unsafe { unsafe {
memory.ptr.as_ptr().add(size).write_bytes(0, memory.size - size); match layout.size() {
old_size if old_size == new_size => Ok(MemoryBlock { ptr, size: new_size }),
0 => self.alloc_zeroed(Layout::from_size_align_unchecked(new_size, layout.align())),
old_size => {
// `realloc` probably checks for `new_size > size` or something similar.
intrinsics::assume(new_size > old_size);
let raw_ptr = realloc(ptr.as_ptr(), layout, new_size);
raw_ptr.add(old_size).write_bytes(0, new_size - old_size);
let ptr = NonNull::new(raw_ptr).ok_or(AllocErr)?;
Ok(MemoryBlock { ptr, size: new_size })
}
} }
Ok(memory)
} }
} }
@ -267,29 +262,33 @@ unsafe impl AllocRef for Global {
layout: Layout, layout: Layout,
new_size: usize, new_size: usize,
) -> Result<MemoryBlock, AllocErr> { ) -> Result<MemoryBlock, AllocErr> {
let size = layout.size(); let old_size = layout.size();
debug_assert!( debug_assert!(
new_size <= size, new_size <= old_size,
"`new_size` must be smaller than or equal to `memory.size()`" "`new_size` must be smaller than or equal to `layout.size()`"
); );
if size == new_size { let ptr = if new_size == old_size {
return Ok(MemoryBlock { ptr, size }); ptr
} } else if new_size == 0 {
// SAFETY: `layout` is non-zero in size as `old_size` != `new_size`
if new_size == 0 { // Other conditions must be upheld by the caller
unsafe { unsafe {
self.dealloc(ptr, layout); self.dealloc(ptr, layout);
} }
Ok(MemoryBlock { ptr: layout.dangling(), size: 0 }) layout.dangling()
} else { } else {
// `realloc` probably checks for `new_size < size` or something similar. // SAFETY: new_size is not zero,
let ptr = unsafe { // Other conditions must be upheld by the caller
intrinsics::assume(new_size < size); let raw_ptr = unsafe {
// `realloc` probably checks for `new_size < old_size` or something similar.
intrinsics::assume(new_size < old_size);
realloc(ptr.as_ptr(), layout, new_size) realloc(ptr.as_ptr(), layout, new_size)
}; };
Ok(MemoryBlock { ptr: NonNull::new(ptr).ok_or(AllocErr)?, size: new_size }) NonNull::new(raw_ptr).ok_or(AllocErr)?
} };
Ok(MemoryBlock { ptr, size: new_size })
} }
} }

View file

@ -141,38 +141,34 @@ pub struct System;
unsafe impl AllocRef for System { unsafe impl AllocRef for System {
#[inline] #[inline]
fn alloc(&mut self, layout: Layout) -> Result<MemoryBlock, AllocErr> { fn alloc(&mut self, layout: Layout) -> Result<MemoryBlock, AllocErr> {
unsafe {
let size = layout.size(); let size = layout.size();
if size == 0 { let ptr = if size == 0 {
Ok(MemoryBlock { ptr: layout.dangling(), size: 0 }) layout.dangling()
} else { } else {
let raw_ptr = GlobalAlloc::alloc(self, layout); // SAFETY: `layout` is non-zero in size,
let ptr = NonNull::new(raw_ptr).ok_or(AllocErr)?; unsafe { NonNull::new(GlobalAlloc::alloc(&System, layout)).ok_or(AllocErr)? }
};
Ok(MemoryBlock { ptr, size }) Ok(MemoryBlock { ptr, size })
} }
}
}
#[inline] #[inline]
fn alloc_zeroed(&mut self, layout: Layout) -> Result<MemoryBlock, AllocErr> { fn alloc_zeroed(&mut self, layout: Layout) -> Result<MemoryBlock, AllocErr> {
unsafe {
let size = layout.size(); let size = layout.size();
if size == 0 { let ptr = if size == 0 {
Ok(MemoryBlock { ptr: layout.dangling(), size: 0 }) layout.dangling()
} else { } else {
let raw_ptr = GlobalAlloc::alloc_zeroed(self, layout); // SAFETY: `layout` is non-zero in size,
let ptr = NonNull::new(raw_ptr).ok_or(AllocErr)?; unsafe { NonNull::new(GlobalAlloc::alloc_zeroed(&System, layout)).ok_or(AllocErr)? }
};
Ok(MemoryBlock { ptr, size }) Ok(MemoryBlock { ptr, size })
} }
}
}
#[inline] #[inline]
unsafe fn dealloc(&mut self, ptr: NonNull<u8>, layout: Layout) { unsafe fn dealloc(&mut self, ptr: NonNull<u8>, layout: Layout) {
if layout.size() != 0 { if layout.size() != 0 {
// SAFETY: The safety guarantees are explained in the documentation // SAFETY: `layout` is non-zero in size,
// for the `GlobalAlloc` trait and its `dealloc` method. // other conditions must be upheld by the caller
unsafe { GlobalAlloc::dealloc(self, ptr.as_ptr(), layout) } unsafe { GlobalAlloc::dealloc(&System, ptr.as_ptr(), layout) }
} }
} }
@ -183,44 +179,24 @@ unsafe impl AllocRef for System {
layout: Layout, layout: Layout,
new_size: usize, new_size: usize,
) -> Result<MemoryBlock, AllocErr> { ) -> Result<MemoryBlock, AllocErr> {
let size = layout.size();
debug_assert!( debug_assert!(
new_size >= size, new_size >= layout.size(),
"`new_size` must be greater than or equal to `memory.size()`" "`new_size` must be greater than or equal to `layout.size()`"
); );
if size == new_size { // SAFETY: `new_size` must be non-zero, which is checked in the match expression.
return Ok(MemoryBlock { ptr, size }); // Other conditions must be upheld by the caller
}
if layout.size() == 0 {
let new_layout =
// SAFETY: The new size and layout alignement guarantees
// are transfered to the caller (they come from parameters).
//
// See the preconditions for `Layout::from_size_align` to
// see what must be checked.
unsafe { Layout::from_size_align_unchecked(new_size, layout.align()) };
self.alloc(new_layout)
} else {
// SAFETY:
//
// The safety guarantees are explained in the documentation
// for the `GlobalAlloc` trait and its `dealloc` method.
//
// `realloc` probably checks for `new_size > size` or something
// similar.
//
// For the guarantees about `init_offset`, see its documentation:
// `ptr` is assumed valid (and checked for non-NUL) and
// `memory.size` is set to `new_size` so the offset being `size`
// is valid.
unsafe { unsafe {
intrinsics::assume(new_size > size); match layout.size() {
let ptr = GlobalAlloc::realloc(self, ptr.as_ptr(), layout, new_size); old_size if old_size == new_size => Ok(MemoryBlock { ptr, size: new_size }),
let memory = 0 => self.alloc(Layout::from_size_align_unchecked(new_size, layout.align())),
MemoryBlock { ptr: NonNull::new(ptr).ok_or(AllocErr)?, size: new_size }; old_size => {
Ok(memory) // `realloc` probably checks for `new_size > size` or something similar.
intrinsics::assume(new_size > old_size);
let raw_ptr = GlobalAlloc::realloc(&System, ptr.as_ptr(), layout, new_size);
let ptr = NonNull::new(raw_ptr).ok_or(AllocErr)?;
Ok(MemoryBlock { ptr, size: new_size })
}
} }
} }
} }
@ -232,47 +208,26 @@ unsafe impl AllocRef for System {
layout: Layout, layout: Layout,
new_size: usize, new_size: usize,
) -> Result<MemoryBlock, AllocErr> { ) -> Result<MemoryBlock, AllocErr> {
let size = layout.size();
debug_assert!( debug_assert!(
new_size >= size, new_size >= layout.size(),
"`new_size` must be greater than or equal to `memory.size()`" "`new_size` must be greater than or equal to `layout.size()`"
); );
if size == new_size { // SAFETY: `new_size` must be non-zero, which is checked in the match expression.
return Ok(MemoryBlock { ptr, size }); // Other conditions must be upheld by the caller
unsafe {
match layout.size() {
old_size if old_size == new_size => Ok(MemoryBlock { ptr, size: new_size }),
0 => self.alloc_zeroed(Layout::from_size_align_unchecked(new_size, layout.align())),
old_size => {
// `realloc` probably checks for `new_size > size` or something similar.
intrinsics::assume(new_size > old_size);
let raw_ptr = GlobalAlloc::realloc(&System, ptr.as_ptr(), layout, new_size);
raw_ptr.add(old_size).write_bytes(0, new_size - old_size);
let ptr = NonNull::new(raw_ptr).ok_or(AllocErr)?;
Ok(MemoryBlock { ptr, size: new_size })
}
} }
if layout.size() == 0 {
let new_layout =
// SAFETY: The new size and layout alignement guarantees
// are transfered to the caller (they come from parameters).
//
// See the preconditions for `Layout::from_size_align` to
// see what must be checked.
unsafe { Layout::from_size_align_unchecked(new_size, layout.align()) };
self.alloc_zeroed(new_layout)
} else {
// SAFETY:
//
// The safety guarantees are explained in the documentation
// for the `GlobalAlloc` trait and its `dealloc` method.
//
// `realloc` probably checks for `new_size > size` or something
// similar.
//
// For the guarantees about `init_offset`, see its documentation:
// `ptr` is assumed valid (and checked for non-NUL) and
// `memory.size` is set to `new_size` so the offset being `size`
// is valid.
let memory = unsafe {
intrinsics::assume(new_size > size);
let ptr = GlobalAlloc::realloc(self, ptr.as_ptr(), layout, new_size);
let memory =
MemoryBlock { ptr: NonNull::new(ptr).ok_or(AllocErr)?, size: new_size };
memory.ptr.as_ptr().add(size).write_bytes(0, memory.size - size);
memory
};
Ok(memory)
} }
} }
@ -283,39 +238,33 @@ unsafe impl AllocRef for System {
layout: Layout, layout: Layout,
new_size: usize, new_size: usize,
) -> Result<MemoryBlock, AllocErr> { ) -> Result<MemoryBlock, AllocErr> {
let size = layout.size(); let old_size = layout.size();
debug_assert!( debug_assert!(
new_size <= size, new_size <= old_size,
"`new_size` must be smaller than or equal to `memory.size()`" "`new_size` must be smaller than or equal to `layout.size()`"
); );
if size == new_size { let ptr = if new_size == old_size {
return Ok(MemoryBlock { ptr, size }); ptr
} else if new_size == 0 {
// SAFETY: `layout` is non-zero in size as `old_size` != `new_size`
// Other conditions must be upheld by the caller
unsafe {
self.dealloc(ptr, layout);
} }
layout.dangling()
if new_size == 0 {
// SAFETY: see `GlobalAlloc::dealloc` for the guarantees that
// must be respected. `ptr` and `layout` are parameters and so
// those guarantees must be checked by the caller.
unsafe { self.dealloc(ptr, layout) };
Ok(MemoryBlock { ptr: layout.dangling(), size: 0 })
} else { } else {
// SAFETY: // SAFETY: new_size is not zero,
// // Other conditions must be upheld by the caller
// See `GlobalAlloc::realloc` for more informations about the let raw_ptr = unsafe {
// guarantees expected by this method. `ptr`, `layout` and // `realloc` probably checks for `new_size < old_size` or something similar.
// `new_size` are parameters and the responsability for their intrinsics::assume(new_size < old_size);
// correctness is left to the caller. GlobalAlloc::realloc(&System, ptr.as_ptr(), layout, new_size)
//
// `realloc` probably checks for `new_size < size` or something
// similar.
let memory = unsafe {
intrinsics::assume(new_size < size);
let ptr = GlobalAlloc::realloc(self, ptr.as_ptr(), layout, new_size);
MemoryBlock { ptr: NonNull::new(ptr).ok_or(AllocErr)?, size: new_size }
}; };
Ok(memory) NonNull::new(raw_ptr).ok_or(AllocErr)?
} };
Ok(MemoryBlock { ptr, size: new_size })
} }
} }
static HOOK: AtomicPtr<()> = AtomicPtr::new(ptr::null_mut()); static HOOK: AtomicPtr<()> = AtomicPtr::new(ptr::null_mut());