@@ -68,6 +68,13 @@ impl Layout {
68
68
return Err ( LayoutError ) ;
69
69
}
70
70
71
+ // SAFETY: just checked that align is a power of two.
72
+ Layout :: from_size_valid_align ( size, unsafe { ValidAlign :: new_unchecked ( align) } )
73
+ }
74
+
75
+ /// Internal helper constructor to skip revalidating alignment validity.
76
+ #[ inline]
77
+ const fn from_size_valid_align ( size : usize , align : ValidAlign ) -> Result < Self , LayoutError > {
71
78
// (power-of-two implies align != 0.)
72
79
73
80
// Rounded up size is:
@@ -82,23 +89,11 @@ impl Layout {
82
89
//
83
90
// Above implies that checking for summation overflow is both
84
91
// necessary and sufficient.
85
- if size > isize:: MAX as usize - ( align - 1 ) {
86
- return Err ( LayoutError ) ;
87
- }
88
-
89
- // SAFETY: the conditions for `from_size_align_unchecked` have been
90
- // checked above.
91
- unsafe { Ok ( Layout :: from_size_align_unchecked ( size, align) ) }
92
- }
93
-
94
- /// Internal helper constructor to skip revalidating alignment validity.
95
- #[ inline]
96
- const fn from_size_valid_align ( size : usize , align : ValidAlign ) -> Result < Self , LayoutError > {
97
- // See above for the correctness of this check.
98
92
if size > isize:: MAX as usize - ( align. as_nonzero ( ) . get ( ) - 1 ) {
99
93
return Err ( LayoutError ) ;
100
94
}
101
- // SAFTEY: as above, this check is sufficient.
95
+
96
+ // SAFETY: Layout::size invariants checked above.
102
97
Ok ( Layout { size, align } )
103
98
}
104
99
@@ -113,8 +108,8 @@ impl Layout {
113
108
#[ must_use]
114
109
#[ inline]
115
110
pub const unsafe fn from_size_align_unchecked ( size : usize , align : usize ) -> Self {
116
- // SAFETY: the caller must ensure that `align` is a power of two .
117
- Layout { size, align : unsafe { ValidAlign :: new_unchecked ( align) } }
111
+ // SAFETY: the caller is required to uphold the preconditions .
112
+ unsafe { Layout { size, align : ValidAlign :: new_unchecked ( align) } }
118
113
}
119
114
120
115
/// The minimum size in bytes for a memory block of this layout.
0 commit comments