@@ -7,11 +7,17 @@ use crate::sys_common::alloc::{realloc_fallback, MIN_ALIGN};
77#[ repr( C ) ]
88struct Header ( * mut u8 ) ;
99
10+ /// # Safety
11+ ///
12+ /// There must be a `Header` at `ptr.offset(-1)`.
1013unsafe fn get_header < ' a > ( ptr : * mut u8 ) -> & ' a mut Header {
1114 // SAFETY: the safety contract must be upheld by the caller
1215 unsafe { & mut * ( ptr as * mut Header ) . offset ( -1 ) }
1316}
1417
18+ /// # Safety
19+ ///
20+ /// `ptr`, once aligned, must have space for a Header at `ptr.offset(-1)`.
1521unsafe fn align_ptr ( ptr : * mut u8 , align : usize ) -> * mut u8 {
1622 // SAFETY: the safety contract must be upheld by the caller
1723 unsafe {
@@ -30,7 +36,7 @@ unsafe fn allocate_with_flags(layout: Layout, flags: c::DWORD) -> *mut u8 {
3036
3137 let ptr = unsafe {
3238 // SAFETY: The caller must ensure that
33- // `layout.size()` + `layout.size ()` does not overflow.
39+ // `layout.size()` + `layout.align ()` does not overflow.
3440 let size = layout. size ( ) + layout. align ( ) ;
3541 c:: HeapAlloc ( c:: GetProcessHeap ( ) , flags, size)
3642 } ;
@@ -71,17 +77,18 @@ unsafe impl GlobalAlloc for System {
7177 c:: HeapFree ( c:: GetProcessHeap ( ) , 0 , header. 0 as c:: LPVOID )
7278 }
7379 } ;
80+ // SAFETY: `c::GetLastError()` cannot fail
7481 debug_assert ! ( err != 0 , "Failed to free heap memory: {}" , unsafe { c:: GetLastError ( ) } ) ;
7582 }
7683
7784 #[ inline]
7885 unsafe fn realloc ( & self , ptr : * mut u8 , layout : Layout , new_size : usize ) -> * mut u8 {
86+ // SAFETY: HeapReAlloc/realloc_fallback is safe if ptr was allocated by this allocator
87+ // and new_size is not 0.
88+ debug_assert_ne ! ( new_size, 0 ) ;
7989 if layout. align ( ) <= MIN_ALIGN {
80- // SAFETY: HeapReAlloc is safe if ptr was allocated by this allocator
81- // and new_size is not 0.
8290 unsafe { c:: HeapReAlloc ( c:: GetProcessHeap ( ) , 0 , ptr as c:: LPVOID , new_size) as * mut u8 }
8391 } else {
84- // SAFETY: The safety contract for `realloc_fallback` must be upheld by the caller
8592 unsafe { realloc_fallback ( self , ptr, layout, new_size) }
8693 }
8794 }
0 commit comments