Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions tests/run-pass/heap_system.rs → tests/run-pass/heap_allocator.rs
Original file line number Diff line number Diff line change
@@ -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) {
Expand All @@ -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();
}