File tree 1 file changed +10
-24
lines changed
1 file changed +10
-24
lines changed Original file line number Diff line number Diff line change 1
- use std :: alloc :: { GlobalAlloc , Layout , System } ;
1
+ #! [ feature ( allocator_api , slice_ptr_get ) ]
2
2
3
- #[ global_allocator]
4
- static ALLOCATOR : Allocator = Allocator ;
5
-
6
- struct Allocator ;
7
-
8
- unsafe impl GlobalAlloc for Allocator {
9
- unsafe fn alloc ( & self , layout : Layout ) -> * mut u8 {
10
- // use specific size to avoid getting triggered by rt
11
- if layout. size ( ) == 123 {
12
- println ! ( "Allocated!" )
13
- }
14
-
15
- System . alloc ( layout)
16
- }
3
+ use std:: alloc:: { Allocator , Global , Layout , System } ;
17
4
18
- unsafe fn dealloc ( & self , ptr : * mut u8 , layout : Layout ) {
19
- if layout. size ( ) == 123 {
20
- println ! ( "Dellocated!" )
21
- }
22
-
23
- System . dealloc ( ptr, layout)
24
- }
25
- }
5
+ #[ global_allocator]
6
+ static ALLOCATOR : System = System ;
26
7
27
8
fn main ( ) {
28
- Box :: new ( [ 0_u8 ; 123 ] ) ;
9
+ // Only okay because we explicitly set the system allocator as the global one!
10
+ let l = Layout :: from_size_align ( 1 , 1 ) . unwrap ( ) ;
11
+ let ptr = Global . allocate ( l) . unwrap ( ) . as_non_null_ptr ( ) ; // allocating with Global...
12
+ unsafe {
13
+ System . deallocate ( ptr, l) ;
14
+ } // ... and deallocating with System.
29
15
}
You can’t perform that action at this time.
0 commit comments