@@ -22,11 +22,16 @@ unsafe extern "Rust" {
2222 #[ rustc_deallocator]
2323 #[ rustc_nounwind]
2424 #[ rustc_std_internal_symbol]
25- fn __rust_dealloc ( ptr : * mut u8 , size : usize , align : Alignment ) ;
25+ fn __rust_dealloc ( ptr : NonNull < u8 > , size : usize , align : Alignment ) ;
2626 #[ rustc_reallocator]
2727 #[ rustc_nounwind]
2828 #[ rustc_std_internal_symbol]
29- fn __rust_realloc ( ptr : * mut u8 , old_size : usize , align : Alignment , new_size : usize ) -> * mut u8 ;
29+ fn __rust_realloc (
30+ ptr : NonNull < u8 > ,
31+ old_size : usize ,
32+ align : Alignment ,
33+ new_size : usize ,
34+ ) -> * mut u8 ;
3035 #[ rustc_allocator_zeroed]
3136 #[ rustc_nounwind]
3237 #[ rustc_std_internal_symbol]
@@ -112,6 +117,13 @@ pub unsafe fn alloc(layout: Layout) -> *mut u8 {
112117#[ inline]
113118#[ cfg_attr( miri, track_caller) ] // even without panics, this helps for Miri backtraces
114119pub unsafe fn dealloc ( ptr : * mut u8 , layout : Layout ) {
120+ unsafe { dealloc_nonnull ( NonNull :: new_unchecked ( ptr) , layout) }
121+ }
122+
123+ /// Same as [`dealloc`] but when you already have a non-null pointer
124+ #[ inline]
125+ #[ cfg_attr( miri, track_caller) ] // even without panics, this helps for Miri backtraces
126+ unsafe fn dealloc_nonnull ( ptr : NonNull < u8 > , layout : Layout ) {
115127 unsafe { __rust_dealloc ( ptr, layout. size ( ) , layout. alignment ( ) ) }
116128}
117129
@@ -132,6 +144,13 @@ pub unsafe fn dealloc(ptr: *mut u8, layout: Layout) {
132144#[ inline]
133145#[ cfg_attr( miri, track_caller) ] // even without panics, this helps for Miri backtraces
134146pub unsafe fn realloc ( ptr : * mut u8 , layout : Layout , new_size : usize ) -> * mut u8 {
147+ unsafe { realloc_nonnull ( NonNull :: new_unchecked ( ptr) , layout, new_size) }
148+ }
149+
150+ /// Same as [`realloc`] but when you already have a non-null pointer
151+ #[ inline]
152+ #[ cfg_attr( miri, track_caller) ] // even without panics, this helps for Miri backtraces
153+ unsafe fn realloc_nonnull ( ptr : NonNull < u8 > , layout : Layout , new_size : usize ) -> * mut u8 {
135154 unsafe { __rust_realloc ( ptr, layout. size ( ) , layout. alignment ( ) , new_size) }
136155}
137156
@@ -206,7 +225,7 @@ impl Global {
206225 // allocation than requested.
207226 // * Other conditions must be upheld by the caller, as per `Allocator::deallocate()`'s
208227 // safety documentation.
209- unsafe { dealloc ( ptr. as_ptr ( ) , layout) }
228+ unsafe { dealloc_nonnull ( ptr, layout) }
210229 }
211230 }
212231
@@ -236,7 +255,7 @@ impl Global {
236255 // `realloc` probably checks for `new_size >= old_layout.size()` or something similar.
237256 hint:: assert_unchecked ( new_size >= old_layout. size ( ) ) ;
238257
239- let raw_ptr = realloc ( ptr. as_ptr ( ) , old_layout, new_size) ;
258+ let raw_ptr = realloc_nonnull ( ptr, old_layout, new_size) ;
240259 let ptr = NonNull :: new ( raw_ptr) . ok_or ( AllocError ) ?;
241260 if zeroed {
242261 raw_ptr. add ( old_size) . write_bytes ( 0 , new_size - old_size) ;
@@ -285,7 +304,7 @@ impl Global {
285304 // `realloc` probably checks for `new_size <= old_layout.size()` or something similar.
286305 hint:: assert_unchecked ( new_size <= old_layout. size ( ) ) ;
287306
288- let raw_ptr = realloc ( ptr. as_ptr ( ) , old_layout, new_size) ;
307+ let raw_ptr = realloc_nonnull ( ptr, old_layout, new_size) ;
289308 let ptr = NonNull :: new ( raw_ptr) . ok_or ( AllocError ) ?;
290309 Ok ( NonNull :: slice_from_raw_parts ( ptr, new_size) )
291310 } ,
0 commit comments