diff --git a/crossbeam-channel/src/flavors/list.rs b/crossbeam-channel/src/flavors/list.rs index ac43c066a..8ffe58beb 100644 --- a/crossbeam-channel/src/flavors/list.rs +++ b/crossbeam-channel/src/flavors/list.rs @@ -72,18 +72,22 @@ struct Block { } impl Block { - /// Creates an empty block. - fn new() -> Box { + const LAYOUT: Layout = { let layout = Layout::new::(); assert!( layout.size() != 0, "Block should never be zero-sized, as it has an AtomicPtr field" ); + layout + }; + + /// Creates an empty block. + fn new() -> Box { // SAFETY: layout is not zero-sized - let ptr = unsafe { alloc_zeroed(layout) }; + let ptr = unsafe { alloc_zeroed(Self::LAYOUT) }; // Handle allocation failure if ptr.is_null() { - handle_alloc_error(layout) + handle_alloc_error(Self::LAYOUT) } // SAFETY: This is safe because: // [1] `Block::next` (AtomicPtr) may be safely zero initialized. diff --git a/crossbeam-deque/src/deque.rs b/crossbeam-deque/src/deque.rs index e207db641..4aa69a68f 100644 --- a/crossbeam-deque/src/deque.rs +++ b/crossbeam-deque/src/deque.rs @@ -1227,18 +1227,22 @@ struct Block { } impl Block { - /// Creates an empty block. - fn new() -> Box { + const LAYOUT: Layout = { let layout = Layout::new::(); assert!( layout.size() != 0, "Block should never be zero-sized, as it has an AtomicPtr field" ); + layout + }; + + /// Creates an empty block. + fn new() -> Box { // SAFETY: layout is not zero-sized - let ptr = unsafe { alloc_zeroed(layout) }; + let ptr = unsafe { alloc_zeroed(Self::LAYOUT) }; // Handle allocation failure if ptr.is_null() { - handle_alloc_error(layout) + handle_alloc_error(Self::LAYOUT) } // SAFETY: This is safe because: // [1] `Block::next` (AtomicPtr) may be safely zero initialized. diff --git a/crossbeam-queue/src/seg_queue.rs b/crossbeam-queue/src/seg_queue.rs index 5e23a8133..c9344fe42 100644 --- a/crossbeam-queue/src/seg_queue.rs +++ b/crossbeam-queue/src/seg_queue.rs @@ -58,18 +58,22 @@ struct Block { } impl Block { - /// Creates an empty block. - fn new() -> Box { + const LAYOUT: Layout = { let layout = Layout::new::(); assert!( layout.size() != 0, "Block should never be zero-sized, as it has an AtomicPtr field" ); + layout + }; + + /// Creates an empty block. + fn new() -> Box { // SAFETY: layout is not zero-sized - let ptr = unsafe { alloc_zeroed(layout) }; + let ptr = unsafe { alloc_zeroed(Self::LAYOUT) }; // Handle allocation failure if ptr.is_null() { - handle_alloc_error(layout) + handle_alloc_error(Self::LAYOUT) } // SAFETY: This is safe because: // [1] `Block::next` (AtomicPtr) may be safely zero initialized.