Merge pull request #645 from RalfJung/box-alloc

test using the Global allocator trait to alloc/free a Box
This commit is contained in:
Oliver Scherer 2019-02-25 10:32:55 +01:00 committed by GitHub
commit 5566aec8c9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -1,6 +1,7 @@
//ignore-windows: Inspects allocation base address on Windows
#![feature(allocator_api)]
use std::ptr::NonNull;
use std::alloc::{Global, Alloc, Layout, System};
fn check_overalign_requests<T: Alloc>(mut allocator: T) {
@ -23,7 +24,31 @@ fn check_overalign_requests<T: Alloc>(mut allocator: T) {
}
}
fn global_to_box() {
type T = [i32; 4];
let l = Layout::new::<T>();
// allocate manually with global allocator, then turn into Box and free there
unsafe {
let ptr = Global.alloc(l).unwrap().as_ptr() as *mut T;
let b = Box::from_raw(ptr);
drop(b);
}
}
fn box_to_global() {
type T = [i32; 4];
let l = Layout::new::<T>();
// allocate with the Box, then deallocate manually with global allocator
unsafe {
let b = Box::new(T::default());
let ptr = Box::into_raw(b);
Global.dealloc(NonNull::new(ptr as *mut u8).unwrap(), l);
}
}
fn main() {
check_overalign_requests(System);
check_overalign_requests(Global);
global_to_box();
box_to_global();
}