File tree Expand file tree Collapse file tree 1 file changed +25
-0
lines changed Expand file tree Collapse file tree 1 file changed +25
-0
lines changed Original file line number Diff line number Diff line change 11//ignore-windows: Inspects allocation base address on Windows
22#![ feature( allocator_api) ]
33
4+ use std:: ptr:: NonNull ;
45use std:: alloc:: { Global , Alloc , Layout , System } ;
56
67fn check_overalign_requests < T : Alloc > ( mut allocator : T ) {
@@ -23,7 +24,31 @@ fn check_overalign_requests<T: Alloc>(mut allocator: T) {
2324 }
2425}
2526
27+ fn global_to_box ( ) {
28+ type T = [ i32 ; 4 ] ;
29+ let l = Layout :: new :: < T > ( ) ;
30+ // allocate manually with global allocator, then turn into Box and free there
31+ unsafe {
32+ let ptr = Global . alloc ( l) . unwrap ( ) . as_ptr ( ) as * mut T ;
33+ let b = Box :: from_raw ( ptr) ;
34+ drop ( b) ;
35+ }
36+ }
37+
38+ fn box_to_global ( ) {
39+ type T = [ i32 ; 4 ] ;
40+ let l = Layout :: new :: < T > ( ) ;
41+ // allocate with the Box, then deallocate manually with global allocator
42+ unsafe {
43+ let b = Box :: new ( T :: default ( ) ) ;
44+ let ptr = Box :: into_raw ( b) ;
45+ Global . dealloc ( NonNull :: new ( ptr as * mut u8 ) . unwrap ( ) , l) ;
46+ }
47+ }
48+
2649fn main ( ) {
2750 check_overalign_requests ( System ) ;
2851 check_overalign_requests ( Global ) ;
52+ global_to_box ( ) ;
53+ box_to_global ( ) ;
2954}
You can’t perform that action at this time.
0 commit comments