Fix issues from review and unsoundness of RawVec::into_box

This commit is contained in:
Tim Diekmann 2020-03-26 17:11:47 +01:00
parent 56cbf2f22a
commit 2526accdd3
17 changed files with 436 additions and 474 deletions

View file

@ -37,10 +37,10 @@ fn main() {
unsafe {
let layout = Layout::from_size_align(4, 2).unwrap();
let (ptr, _) = Global.alloc(layout.clone(), AllocInit::Uninitialized).unwrap();
helper::work_with(&ptr);
let memory = Global.alloc(layout.clone(), AllocInit::Uninitialized).unwrap();
helper::work_with(&memory.ptr());
assert_eq!(HITS.load(Ordering::SeqCst), n + 1);
Global.dealloc(ptr, layout.clone());
Global.dealloc(memory);
assert_eq!(HITS.load(Ordering::SeqCst), n + 2);
let s = String::with_capacity(10);
@ -49,10 +49,10 @@ fn main() {
drop(s);
assert_eq!(HITS.load(Ordering::SeqCst), n + 4);
let (ptr, _) = System.alloc(layout.clone(), AllocInit::Uninitialized).unwrap();
let memory = System.alloc(layout.clone(), AllocInit::Uninitialized).unwrap();
assert_eq!(HITS.load(Ordering::SeqCst), n + 4);
helper::work_with(&ptr);
System.dealloc(ptr, layout);
helper::work_with(&memory.ptr());
System.dealloc(memory);
assert_eq!(HITS.load(Ordering::SeqCst), n + 4);
}
}

View file

@ -20,16 +20,16 @@ fn main() {
let n = GLOBAL.0.load(Ordering::SeqCst);
let layout = Layout::from_size_align(4, 2).unwrap();
let (ptr, _) = Global.alloc(layout.clone(), AllocInit::Uninitialized).unwrap();
helper::work_with(&ptr);
let memory = Global.alloc(layout.clone(), AllocInit::Uninitialized).unwrap();
helper::work_with(&memory.ptr());
assert_eq!(GLOBAL.0.load(Ordering::SeqCst), n + 1);
Global.dealloc(ptr, layout.clone());
Global.dealloc(memory);
assert_eq!(GLOBAL.0.load(Ordering::SeqCst), n + 2);
let (ptr, _) = System.alloc(layout.clone(), AllocInit::Uninitialized).unwrap();
let memory = System.alloc(layout.clone(), AllocInit::Uninitialized).unwrap();
assert_eq!(GLOBAL.0.load(Ordering::SeqCst), n + 2);
helper::work_with(&ptr);
System.dealloc(ptr, layout);
helper::work_with(&memory.ptr());
System.dealloc(memory);
assert_eq!(GLOBAL.0.load(Ordering::SeqCst), n + 2);
}
}

View file

@ -6,7 +6,9 @@
#![feature(allocator_api)]
use std::alloc::{handle_alloc_error, AllocInit, AllocRef, Global, Layout, ReallocPlacement};
use std::alloc::{
handle_alloc_error, AllocInit, AllocRef, Global, Layout, MemoryBlock, ReallocPlacement,
};
use std::ptr::{self, NonNull};
fn main() {
@ -41,15 +43,15 @@ unsafe fn test_triangle() -> bool {
println!("allocate({:?})", layout);
}
let (ptr, _) = Global
let memory = Global
.alloc(layout, AllocInit::Uninitialized)
.unwrap_or_else(|_| handle_alloc_error(layout));
if PRINT {
println!("allocate({:?}) = {:?}", layout, ptr);
println!("allocate({:?}) = {:?}", layout, memory.ptr());
}
ptr.cast().as_ptr()
memory.ptr().cast().as_ptr()
}
unsafe fn deallocate(ptr: *mut u8, layout: Layout) {
@ -57,7 +59,7 @@ unsafe fn test_triangle() -> bool {
println!("deallocate({:?}, {:?}", ptr, layout);
}
Global.dealloc(NonNull::new_unchecked(ptr), layout);
Global.dealloc(MemoryBlock::new(NonNull::new_unchecked(ptr), layout));
}
unsafe fn reallocate(ptr: *mut u8, old: Layout, new: Layout) -> *mut u8 {
@ -65,28 +67,28 @@ unsafe fn test_triangle() -> bool {
println!("reallocate({:?}, old={:?}, new={:?})", ptr, old, new);
}
let allocation = if new.size() > old.size() {
let mut memory = MemoryBlock::new(NonNull::new_unchecked(ptr), old);
let result = if new.size() > old.size() {
Global.grow(
NonNull::new_unchecked(ptr),
old,
&mut memory,
new.size(),
ReallocPlacement::MayMove,
AllocInit::Uninitialized,
)
} else if new.size() < old.size() {
Global.shrink(NonNull::new_unchecked(ptr), old, new.size(), ReallocPlacement::MayMove)
Global.shrink(&mut memory, new.size(), ReallocPlacement::MayMove)
} else {
return ptr;
};
let (ptr, _) = allocation.unwrap_or_else(|_| {
result.unwrap_or_else(|_| {
handle_alloc_error(Layout::from_size_align_unchecked(new.size(), old.align()))
});
if PRINT {
println!("reallocate({:?}, old={:?}, new={:?}) = {:?}", ptr, old, new, ptr);
println!("reallocate({:?}, old={:?}, new={:?}) = {:?}", ptr, old, new, memory.ptr());
}
ptr.cast().as_ptr()
memory.ptr().cast().as_ptr()
}
fn idx_to_size(i: usize) -> usize {

View file

@ -4,7 +4,7 @@
// pretty-expanded FIXME #23616
#![feature(allocator_api)]
use std::alloc::{handle_alloc_error, AllocInit, AllocRef, Global, Layout};
use std::alloc::{handle_alloc_error, AllocInit, AllocRef, Global, Layout, MemoryBlock};
use std::ptr::NonNull;
struct arena(());
@ -25,10 +25,10 @@ struct Ccx {
fn alloc(_bcx: &arena) -> &Bcx<'_> {
unsafe {
let layout = Layout::new::<Bcx>();
let (ptr, _) = Global
let memory = Global
.alloc(layout, AllocInit::Uninitialized)
.unwrap_or_else(|_| handle_alloc_error(layout));
&*(ptr.as_ptr() as *const _)
&*(memory.ptr().as_ptr() as *const _)
}
}
@ -40,7 +40,10 @@ fn g(fcx: &Fcx) {
let bcx = Bcx { fcx };
let bcx2 = h(&bcx);
unsafe {
Global.dealloc(NonNull::new_unchecked(bcx2 as *const _ as *mut _), Layout::new::<Bcx>());
Global.dealloc(MemoryBlock::new(
NonNull::new_unchecked(bcx2 as *const _ as *mut _),
Layout::new::<Bcx>(),
));
}
}