From 545ef9d83ab383bdc99376a2fe4164e46f4a3f6e Mon Sep 17 00:00:00 2001 From: Tim Diekmann Date: Sat, 7 Mar 2020 02:40:54 +0100 Subject: [PATCH 1/2] Add `Layout::dangling()` to return a well-aligned `NonNull` --- src/libcore/alloc.rs | 12 ++++++++++++ src/libcore/tests/alloc.rs | 3 +++ src/libcore/tests/lib.rs | 1 + 3 files changed, 16 insertions(+) diff --git a/src/libcore/alloc.rs b/src/libcore/alloc.rs index f3a2b73f2b8de..4f401c75104de 100644 --- a/src/libcore/alloc.rs +++ b/src/libcore/alloc.rs @@ -140,6 +140,18 @@ impl Layout { unsafe { Layout::from_size_align_unchecked(size, align) } } + /// Creates a `NonNull` that is dangling, but well-aligned for this Layout. + /// + /// Note that the pointer value may potentially represent a valid pointer to + /// a `T`, which means this must not be used as a "not yet initialized" + /// sentinel value. Types that lazily allocate must track initialization by + /// some other means. + #[unstable(feature = "alloc_layout_extra", issue = "55724")] + pub const fn dangling(&self) -> NonNull { + // align is non-zero and a power of two + unsafe { NonNull::new_unchecked(self.align() as *mut u8) } + } + /// Creates a layout describing the record that can hold a value /// of the same layout as `self`, but that also is aligned to /// alignment `align` (measured in bytes). diff --git a/src/libcore/tests/alloc.rs b/src/libcore/tests/alloc.rs index 63537ba23d84d..c8592e40a69a0 100644 --- a/src/libcore/tests/alloc.rs +++ b/src/libcore/tests/alloc.rs @@ -1,10 +1,13 @@ use core::alloc::Layout; +use core::ptr::NonNull; #[test] fn const_unchecked_layout() { const SIZE: usize = 0x2000; const ALIGN: usize = 0x1000; const LAYOUT: Layout = unsafe { Layout::from_size_align_unchecked(SIZE, ALIGN) }; + const DANGLING: NonNull = LAYOUT.dangling(); assert_eq!(LAYOUT.size(), SIZE); assert_eq!(LAYOUT.align(), ALIGN); + assert_eq!(Some(DANGLING), NonNull::new(ALIGN as *mut u8)); } diff --git a/src/libcore/tests/lib.rs b/src/libcore/tests/lib.rs index 991458db5b72b..71a061af28920 100644 --- a/src/libcore/tests/lib.rs +++ b/src/libcore/tests/lib.rs @@ -1,3 +1,4 @@ +#![feature(alloc_layout_extra)] #![feature(bool_to_option)] #![feature(bound_cloned)] #![feature(box_syntax)] From 09d3ba13afb8fdf449928b160431787e2d259db1 Mon Sep 17 00:00:00 2001 From: Tim Diekmann <21277928+TimDiekmann@users.noreply.github.com> Date: Sat, 7 Mar 2020 02:45:55 +0100 Subject: [PATCH 2/2] Update alloc.rs --- src/libcore/alloc.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/libcore/alloc.rs b/src/libcore/alloc.rs index 4f401c75104de..0a7a8ab266aee 100644 --- a/src/libcore/alloc.rs +++ b/src/libcore/alloc.rs @@ -142,8 +142,8 @@ impl Layout { /// Creates a `NonNull` that is dangling, but well-aligned for this Layout. /// - /// Note that the pointer value may potentially represent a valid pointer to - /// a `T`, which means this must not be used as a "not yet initialized" + /// Note that the pointer value may potentially represent a valid pointer, + /// which means this must not be used as a "not yet initialized" /// sentinel value. Types that lazily allocate must track initialization by /// some other means. #[unstable(feature = "alloc_layout_extra", issue = "55724")]