Document why layout.align() + layout.size() doesn't overflow

This commit is contained in:
John Kåre Alsaker
2026-04-12 18:00:56 +02:00
parent a72e2a71d8
commit b9ec55bbcd
+3
View File
@@ -118,6 +118,9 @@ unsafe fn allocate(layout: Layout, zeroed: bool) -> *mut u8 {
process_heap_alloc(MaybeUninit::uninit(), flags, layout.size()) as *mut u8
} else {
// Allocate extra padding in order to be able to satisfy the alignment.
// This addition does not overflow due to `Layout` type invariants,
// `size()` is at most `isize::MAX` while
// `align()` is at most `1 << (bits in usize - 2)` if `size()` is non-zero.
let total = layout.align() + layout.size();
let ptr = process_heap_alloc(MaybeUninit::uninit(), flags, total) as *mut u8;