mirror of
https://github.com/Lokathor/bytemuck.git
synced 2024-11-21 14:22:26 +00:00
Remove "dangling" terminology for zero-sized pointees. (#262)
* Update safety comment on BoxBytes pointer field. * Update mentions of "dangling" pointers elsewhere in src/allocation.rs.
This commit is contained in:
parent
5eecd33318
commit
bb62be5baa
@ -68,9 +68,10 @@ pub fn try_cast_box<A: NoUninit, B: AnyBitPattern>(
|
|||||||
#[inline]
|
#[inline]
|
||||||
pub fn try_zeroed_box<T: Zeroable>() -> Result<Box<T>, ()> {
|
pub fn try_zeroed_box<T: Zeroable>() -> Result<Box<T>, ()> {
|
||||||
if size_of::<T>() == 0 {
|
if size_of::<T>() == 0 {
|
||||||
// This will not allocate but simply create a dangling pointer.
|
// This will not allocate but simply create an arbitrary non-null
|
||||||
let dangling = core::ptr::NonNull::dangling().as_ptr();
|
// aligned pointer, valid for Box for a zero-sized pointee.
|
||||||
return Ok(unsafe { Box::from_raw(dangling) });
|
let ptr = core::ptr::NonNull::dangling().as_ptr();
|
||||||
|
return Ok(unsafe { Box::from_raw(ptr) });
|
||||||
}
|
}
|
||||||
let layout = Layout::new::<T>();
|
let layout = Layout::new::<T>();
|
||||||
let ptr = unsafe { alloc_zeroed(layout) };
|
let ptr = unsafe { alloc_zeroed(layout) };
|
||||||
@ -125,10 +126,11 @@ pub fn try_zeroed_slice_box<T: Zeroable>(
|
|||||||
length: usize,
|
length: usize,
|
||||||
) -> Result<Box<[T]>, ()> {
|
) -> Result<Box<[T]>, ()> {
|
||||||
if size_of::<T>() == 0 || length == 0 {
|
if size_of::<T>() == 0 || length == 0 {
|
||||||
// This will not allocate but simply create a dangling slice pointer.
|
// This will not allocate but simply create an arbitrary non-null aligned
|
||||||
let dangling = core::ptr::NonNull::dangling().as_ptr();
|
// slice pointer, valid for Box for a zero-sized pointee.
|
||||||
let dangling_slice = core::ptr::slice_from_raw_parts_mut(dangling, length);
|
let ptr = core::ptr::NonNull::dangling().as_ptr();
|
||||||
return Ok(unsafe { Box::from_raw(dangling_slice) });
|
let slice_ptr = core::ptr::slice_from_raw_parts_mut(ptr, length);
|
||||||
|
return Ok(unsafe { Box::from_raw(slice_ptr) });
|
||||||
}
|
}
|
||||||
let layout = core::alloc::Layout::array::<T>(length).map_err(|_| ())?;
|
let layout = core::alloc::Layout::array::<T>(length).map_err(|_| ())?;
|
||||||
let ptr = unsafe { alloc_zeroed(layout) };
|
let ptr = unsafe { alloc_zeroed(layout) };
|
||||||
@ -742,9 +744,9 @@ impl<I: ?Sized, T: ?Sized + TransparentWrapper<I>> TransparentWrapperAlloc<I>
|
|||||||
|
|
||||||
/// As `Box<[u8]>`, but remembers the original alignment.
|
/// As `Box<[u8]>`, but remembers the original alignment.
|
||||||
pub struct BoxBytes {
|
pub struct BoxBytes {
|
||||||
// SAFETY: `ptr` is owned, points to `layout.size()` initialized bytes, and
|
// SAFETY: `ptr` is aligned to `layout.align()`, points to
|
||||||
// was allocated with `layout` (unless `layout.size() == 0` in which case it
|
// `layout.size()` initialized bytes, and, if `layout.size() > 0`,
|
||||||
// is dangling).
|
// is owned and was allocated with the global allocator with `layout`.
|
||||||
ptr: NonNull<u8>,
|
ptr: NonNull<u8>,
|
||||||
layout: Layout,
|
layout: Layout,
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user