File tree 1 file changed +25
-0
lines changed 1 file changed +25
-0
lines changed Original file line number Diff line number Diff line change 1
1
//ignore-windows: Inspects allocation base address on Windows
2
2
#![ feature( allocator_api) ]
3
3
4
+ use std:: ptr:: NonNull ;
4
5
use std:: alloc:: { Global , Alloc , Layout , System } ;
5
6
6
7
fn check_overalign_requests < T : Alloc > ( mut allocator : T ) {
@@ -23,7 +24,31 @@ fn check_overalign_requests<T: Alloc>(mut allocator: T) {
23
24
}
24
25
}
25
26
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
+
26
49
fn main ( ) {
27
50
check_overalign_requests ( System ) ;
28
51
check_overalign_requests ( Global ) ;
52
+ global_to_box ( ) ;
53
+ box_to_global ( ) ;
29
54
}
You can’t perform that action at this time.
0 commit comments