@@ -7,11 +7,17 @@ use crate::sys_common::alloc::{realloc_fallback, MIN_ALIGN};
7
7
#[ repr( C ) ]
8
8
struct Header ( * mut u8 ) ;
9
9
10
+ /// # Safety
11
+ ///
12
+ /// There must be a `Header` at `ptr.offset(-1)`.
10
13
unsafe fn get_header < ' a > ( ptr : * mut u8 ) -> & ' a mut Header {
11
14
// SAFETY: the safety contract must be upheld by the caller
12
15
unsafe { & mut * ( ptr as * mut Header ) . offset ( -1 ) }
13
16
}
14
17
18
+ /// # Safety
19
+ ///
20
+ /// `ptr`, once aligned, must have space for a Header at `ptr.offset(-1)`.
15
21
unsafe fn align_ptr ( ptr : * mut u8 , align : usize ) -> * mut u8 {
16
22
// SAFETY: the safety contract must be upheld by the caller
17
23
unsafe {
@@ -30,7 +36,7 @@ unsafe fn allocate_with_flags(layout: Layout, flags: c::DWORD) -> *mut u8 {
30
36
31
37
let ptr = unsafe {
32
38
// SAFETY: The caller must ensure that
33
- // `layout.size()` + `layout.size ()` does not overflow.
39
+ // `layout.size()` + `layout.align ()` does not overflow.
34
40
let size = layout. size ( ) + layout. align ( ) ;
35
41
c:: HeapAlloc ( c:: GetProcessHeap ( ) , flags, size)
36
42
} ;
@@ -71,17 +77,18 @@ unsafe impl GlobalAlloc for System {
71
77
c:: HeapFree ( c:: GetProcessHeap ( ) , 0 , header. 0 as c:: LPVOID )
72
78
}
73
79
} ;
80
+ // SAFETY: `c::GetLastError()` cannot fail
74
81
debug_assert ! ( err != 0 , "Failed to free heap memory: {}" , unsafe { c:: GetLastError ( ) } ) ;
75
82
}
76
83
77
84
#[ inline]
78
85
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 ) ;
79
89
if layout. align ( ) <= MIN_ALIGN {
80
- // SAFETY: HeapReAlloc is safe if ptr was allocated by this allocator
81
- // and new_size is not 0.
82
90
unsafe { c:: HeapReAlloc ( c:: GetProcessHeap ( ) , 0 , ptr as c:: LPVOID , new_size) as * mut u8 }
83
91
} else {
84
- // SAFETY: The safety contract for `realloc_fallback` must be upheld by the caller
85
92
unsafe { realloc_fallback ( self , ptr, layout, new_size) }
86
93
}
87
94
}
0 commit comments