Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion library/std/src/sys/alloc/windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,13 @@ unsafe fn allocate(layout: Layout, zeroed: bool) -> *mut u8 {
// Create a correctly aligned pointer offset from the start of the allocated block,
// and write a header before it.

let offset = layout.align() - (ptr.addr() & (layout.align() - 1));
// SAFETY: Guaranteed by Layout::align.
// This hint is technically redundant, but currently LLVM needs this explicit hint to
// optimize out a panic path inside align_offset.
unsafe {
core::hint::assert_unchecked(layout.align().is_power_of_two());
}
let offset = ptr.wrapping_add(1).align_offset(layout.align()) + 1;
Comment on lines +151 to +154

@ChrisDenton ChrisDenton Nov 12, 2026 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What are you suggesting I write here instead?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mean, remove the unsafe and just use alignment. E.g.:

Suggested change
unsafe {
core::hint::assert_unchecked(layout.align().is_power_of_two());
}
let offset = ptr.wrapping_add(1).align_offset(layout.align()) + 1;
let offset = ptr.wrapping_add(1).align_offset(layout.alignment().as_usize()) + 1;

The point of the alignment type is that it keeps around the "is power of two" information.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is exactly the same conversation I had above.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

// SAFETY: `MIN_ALIGN` <= `offset` <= `layout.align()` and the size of the allocated
// block is `layout.align() + layout.size()`. `aligned` will thus be a correctly aligned
// pointer inside the allocated block with at least `layout.size()` bytes after it and at
Expand Down
Loading