diff --git a/crossbeam-epoch/src/atomic.rs b/crossbeam-epoch/src/atomic.rs index 0dc61021a..41b4cd910 100644 --- a/crossbeam-epoch/src/atomic.rs +++ b/crossbeam-epoch/src/atomic.rs @@ -1,13 +1,14 @@ +use alloc::boxed::Box; +use core::alloc::Layout; use core::borrow::{Borrow, BorrowMut}; use core::cmp; use core::fmt; use core::marker::PhantomData; use core::mem::{self, MaybeUninit}; use core::ops::{Deref, DerefMut}; +use core::ptr; use core::slice; -use crate::alloc::alloc; -use crate::alloc::boxed::Box; use crate::guard::Guard; use crate::primitive::sync::atomic::{AtomicUsize, Ordering}; use crossbeam_utils::atomic::AtomicConsume; @@ -232,9 +233,6 @@ impl Pointable for T { /// /// Elements are not present in the type, but they will be in the allocation. /// ``` -/// -// TODO(@jeehoonkang): once we bump the minimum required Rust version to 1.44 or newer, use -// [`alloc::alloc::Layout::extend`] instead. #[repr(C)] struct Array { /// The number of elements (not the number of bytes). @@ -242,20 +240,28 @@ struct Array { elements: [MaybeUninit; 0], } +impl Array { + fn layout(len: usize) -> Layout { + Layout::new::() + .extend(Layout::array::>(len).unwrap()) + .unwrap() + .0 + .pad_to_align() + } +} + impl Pointable for [MaybeUninit] { const ALIGN: usize = mem::align_of::>(); type Init = usize; unsafe fn init(len: Self::Init) -> usize { - let size = mem::size_of::>() + mem::size_of::>() * len; - let align = mem::align_of::>(); - let layout = alloc::Layout::from_size_align(size, align).unwrap(); - let ptr = alloc::alloc(layout).cast::>(); + let layout = Array::::layout(len); + let ptr = alloc::alloc::alloc(layout).cast::>(); if ptr.is_null() { - alloc::handle_alloc_error(layout); + alloc::alloc::handle_alloc_error(layout); } - (*ptr).len = len; + ptr::addr_of_mut!((*ptr).len).write(len); ptr as usize } @@ -270,11 +276,9 @@ impl Pointable for [MaybeUninit] { } unsafe fn drop(ptr: usize) { - let array = &*(ptr as *mut Array); - let size = mem::size_of::>() + mem::size_of::>() * array.len; - let align = mem::align_of::>(); - let layout = alloc::Layout::from_size_align(size, align).unwrap(); - alloc::dealloc(ptr as *mut u8, layout); + let len = (*(ptr as *mut Array)).len; + let layout = Array::::layout(len); + alloc::alloc::dealloc(ptr as *mut u8, layout); } } diff --git a/crossbeam-skiplist/src/base.rs b/crossbeam-skiplist/src/base.rs index b63183650..44ee07332 100644 --- a/crossbeam-skiplist/src/base.rs +++ b/crossbeam-skiplist/src/base.rs @@ -104,11 +104,11 @@ impl Node { handle_alloc_error(layout); } - ptr::write( - &mut (*ptr).refs_and_height, - AtomicUsize::new((height - 1) | ref_count << HEIGHT_BITS), - ); - ptr::write_bytes((*ptr).tower.pointers.as_mut_ptr(), 0, height); + ptr::addr_of_mut!((*ptr).refs_and_height) + .write(AtomicUsize::new((height - 1) | ref_count << HEIGHT_BITS)); + ptr::addr_of_mut!((*ptr).tower.pointers) + .cast::>() + .write_bytes(0, height); ptr } @@ -122,14 +122,14 @@ impl Node { } /// Returns the layout of a node with the given `height`. - unsafe fn get_layout(height: usize) -> Layout { + fn get_layout(height: usize) -> Layout { assert!((1..=MAX_HEIGHT).contains(&height)); - let size_self = mem::size_of::(); - let align_self = mem::align_of::(); - let size_pointer = mem::size_of::>(); - - Layout::from_size_align_unchecked(size_self + size_pointer * height, align_self) + Layout::new::() + .extend(Layout::array::>(height).unwrap()) + .unwrap() + .0 + .pad_to_align() } /// Returns the height of this node's tower. @@ -906,8 +906,8 @@ where let n = Node::::alloc(height, 2); // Write the key and the value into the node. - ptr::write(&mut (*n).key, key); - ptr::write(&mut (*n).value, value); + ptr::addr_of_mut!((*n).key).write(key); + ptr::addr_of_mut!((*n).value).write(value); (Shared::>::from(n as *const _), &*n) };