8
8
// option. This file may not be copied, modified, or distributed
9
9
// except according to those terms.
10
10
11
+ use core:: { isize, usize} ;
12
+
13
+ #[ inline( always) ]
14
+ fn check_size_and_alignment ( size : usize , align : usize ) {
15
+ debug_assert ! ( size != 0 ) ;
16
+ debug_assert ! ( size <= isize :: MAX as usize , "Tried to allocate too much: {} bytes" , size) ;
17
+ debug_assert ! ( usize :: is_power_of_two( align) , "Invalid alignment of allocation: {}" , align) ;
18
+ }
19
+
11
20
// FIXME: #13996: mark the `allocate` and `reallocate` return value as `noalias`
12
21
13
22
/// Return a pointer to `size` bytes of memory aligned to `align`.
19
28
/// size on the platform.
20
29
#[ inline]
21
30
pub unsafe fn allocate ( size : usize , align : usize ) -> * mut u8 {
31
+ check_size_and_alignment ( size, align) ;
22
32
imp:: allocate ( size, align)
23
33
}
24
34
@@ -38,6 +48,7 @@ pub unsafe fn allocate(size: usize, align: usize) -> *mut u8 {
38
48
/// any value in range_inclusive(requested_size, usable_size).
39
49
#[ inline]
40
50
pub unsafe fn reallocate ( ptr : * mut u8 , old_size : usize , size : usize , align : usize ) -> * mut u8 {
51
+ check_size_and_alignment ( size, align) ;
41
52
imp:: reallocate ( ptr, old_size, size, align)
42
53
}
43
54
@@ -56,6 +67,7 @@ pub unsafe fn reallocate(ptr: *mut u8, old_size: usize, size: usize, align: usiz
56
67
#[ inline]
57
68
pub unsafe fn reallocate_inplace ( ptr : * mut u8 , old_size : usize , size : usize ,
58
69
align : usize ) -> usize {
70
+ check_size_and_alignment ( size, align) ;
59
71
imp:: reallocate_inplace ( ptr, old_size, size, align)
60
72
}
61
73
0 commit comments