Skip to content

Commit 9eb9fef

Browse files
authored
Unrolled build for rust-lang#137641
Rollup merge of rust-lang#137641 - kpreid:dealloc, r=Amanieu More precisely document `Global::deallocate()`'s safety. There is a subtlety which "other conditions must be upheld by the caller" does not capture: `GlobalAlloc`/`alloc::dealloc()` require that the provided layout will be *equal*, not just that it "fits", the layout used to allocate. This is always true here due to how `allocate()`, `grow()`, and `shrink()` are implemented (they never return a larger allocation than requested), but that is a non-local property of the implementation, so it should be documented explicitly. r? libs `@rustbot` label A-allocators
2 parents daf5985 + 33ee398 commit 9eb9fef

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

Diff for: library/alloc/src/alloc.rs

+8-2
Original file line numberDiff line numberDiff line change
@@ -264,8 +264,14 @@ unsafe impl Allocator for Global {
264264
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
265265
unsafe fn deallocate(&self, ptr: NonNull<u8>, layout: Layout) {
266266
if layout.size() != 0 {
267-
// SAFETY: `layout` is non-zero in size,
268-
// other conditions must be upheld by the caller
267+
// SAFETY:
268+
// * We have checked that `layout` is non-zero in size.
269+
// * The caller is obligated to provide a layout that "fits", and in this case,
270+
// "fit" always means a layout that is equal to the original, because our
271+
// `allocate()`, `grow()`, and `shrink()` implementations never returns a larger
272+
// allocation than requested.
273+
// * Other conditions must be upheld by the caller, as per `Allocator::deallocate()`'s
274+
// safety documentation.
269275
unsafe { dealloc(ptr.as_ptr(), layout) }
270276
}
271277
}

0 commit comments

Comments
 (0)