88// option. This file may not be copied, modified, or distributed
99// except according to those terms.
1010
11- pub use alloc:: { Excess , Layout , AllocErr , CannotReallocInPlace } ;
11+ #![ allow( deprecated) ]
12+
13+ pub use alloc:: { Layout , AllocErr , CannotReallocInPlace , Void } ;
1214use core:: alloc:: Alloc as CoreAlloc ;
15+ use core:: ptr:: NonNull ;
1316
1417#[ doc( hidden) ]
1518pub mod __core {
1619 pub use core:: * ;
1720}
1821
22+ #[ derive( Debug ) ]
23+ pub struct Excess ( pub * mut u8 , pub usize ) ;
24+
1925/// Compatibility with older versions of #[global_allocator] during bootstrap
2026pub unsafe trait Alloc {
2127 unsafe fn alloc ( & mut self , layout : Layout ) -> Result < * mut u8 , AllocErr > ;
@@ -42,13 +48,13 @@ pub unsafe trait Alloc {
4248 new_layout : Layout ) -> Result < ( ) , CannotReallocInPlace > ;
4349}
4450
45- #[ allow( deprecated) ]
4651unsafe impl < T > Alloc for T where T : CoreAlloc {
4752 unsafe fn alloc ( & mut self , layout : Layout ) -> Result < * mut u8 , AllocErr > {
48- CoreAlloc :: alloc ( self , layout)
53+ CoreAlloc :: alloc ( self , layout) . map ( |ptr| ptr . cast ( ) . as_ptr ( ) )
4954 }
5055
5156 unsafe fn dealloc ( & mut self , ptr : * mut u8 , layout : Layout ) {
57+ let ptr = NonNull :: new_unchecked ( ptr as * mut Void ) ;
5258 CoreAlloc :: dealloc ( self , ptr, layout)
5359 }
5460
@@ -64,35 +70,41 @@ unsafe impl<T> Alloc for T where T: CoreAlloc {
6470 ptr : * mut u8 ,
6571 layout : Layout ,
6672 new_layout : Layout ) -> Result < * mut u8 , AllocErr > {
67- CoreAlloc :: realloc ( self , ptr, layout, new_layout. size ( ) )
73+ let ptr = NonNull :: new_unchecked ( ptr as * mut Void ) ;
74+ CoreAlloc :: realloc ( self , ptr, layout, new_layout. size ( ) ) . map ( |ptr| ptr. cast ( ) . as_ptr ( ) )
6875 }
6976
7077 unsafe fn alloc_zeroed ( & mut self , layout : Layout ) -> Result < * mut u8 , AllocErr > {
71- CoreAlloc :: alloc_zeroed ( self , layout)
78+ CoreAlloc :: alloc_zeroed ( self , layout) . map ( |ptr| ptr . cast ( ) . as_ptr ( ) )
7279 }
7380
7481 unsafe fn alloc_excess ( & mut self , layout : Layout ) -> Result < Excess , AllocErr > {
7582 CoreAlloc :: alloc_excess ( self , layout)
83+ . map ( |e| Excess ( e. 0 . cast ( ) . as_ptr ( ) , e. 1 ) )
7684 }
7785
7886 unsafe fn realloc_excess ( & mut self ,
7987 ptr : * mut u8 ,
8088 layout : Layout ,
8189 new_layout : Layout ) -> Result < Excess , AllocErr > {
90+ let ptr = NonNull :: new_unchecked ( ptr as * mut Void ) ;
8291 CoreAlloc :: realloc_excess ( self , ptr, layout, new_layout. size ( ) )
92+ . map ( |e| Excess ( e. 0 . cast ( ) . as_ptr ( ) , e. 1 ) )
8393 }
8494
8595 unsafe fn grow_in_place ( & mut self ,
8696 ptr : * mut u8 ,
8797 layout : Layout ,
8898 new_layout : Layout ) -> Result < ( ) , CannotReallocInPlace > {
99+ let ptr = NonNull :: new_unchecked ( ptr as * mut Void ) ;
89100 CoreAlloc :: grow_in_place ( self , ptr, layout, new_layout. size ( ) )
90101 }
91102
92103 unsafe fn shrink_in_place ( & mut self ,
93104 ptr : * mut u8 ,
94105 layout : Layout ,
95106 new_layout : Layout ) -> Result < ( ) , CannotReallocInPlace > {
107+ let ptr = NonNull :: new_unchecked ( ptr as * mut Void ) ;
96108 CoreAlloc :: shrink_in_place ( self , ptr, layout, new_layout. size ( ) )
97109 }
98110}
0 commit comments