Skip to content

Commit 54988eb

Browse files
committed
Calculate layout in const context
1 parent 761d0b6 commit 54988eb

File tree

3 files changed

+24
-12
lines changed

3 files changed

+24
-12
lines changed

crossbeam-channel/src/flavors/list.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,18 +72,22 @@ struct Block<T> {
7272
}
7373

7474
impl<T> Block<T> {
75-
/// Creates an empty block.
76-
fn new() -> Box<Self> {
75+
const LAYOUT: Layout = {
7776
let layout = Layout::new::<Self>();
7877
assert!(
7978
layout.size() != 0,
8079
"Block should never be zero-sized, as it has an AtomicPtr field"
8180
);
81+
layout
82+
};
83+
84+
/// Creates an empty block.
85+
fn new() -> Box<Self> {
8286
// SAFETY: layout is not zero-sized
83-
let ptr = unsafe { alloc_zeroed(layout) };
87+
let ptr = unsafe { alloc_zeroed(Self::LAYOUT) };
8488
// Handle allocation failure
8589
if ptr.is_null() {
86-
handle_alloc_error(layout)
90+
handle_alloc_error(Self::LAYOUT)
8791
}
8892
// SAFETY: This is safe because:
8993
// [1] `Block::next` (AtomicPtr) may be safely zero initialized.

crossbeam-deque/src/deque.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1225,18 +1225,22 @@ struct Block<T> {
12251225
}
12261226

12271227
impl<T> Block<T> {
1228-
/// Creates an empty block.
1229-
fn new() -> Box<Self> {
1228+
const LAYOUT: Layout = {
12301229
let layout = Layout::new::<Self>();
12311230
assert!(
12321231
layout.size() != 0,
12331232
"Block should never be zero-sized, as it has an AtomicPtr field"
12341233
);
1234+
layout
1235+
};
1236+
1237+
/// Creates an empty block.
1238+
fn new() -> Box<Self> {
12351239
// SAFETY: layout is not zero-sized
1236-
let ptr = unsafe { alloc_zeroed(layout) };
1240+
let ptr = unsafe { alloc_zeroed(Self::LAYOUT) };
12371241
// Handle allocation failure
12381242
if ptr.is_null() {
1239-
handle_alloc_error(layout)
1243+
handle_alloc_error(Self::LAYOUT)
12401244
}
12411245
// SAFETY: This is safe because:
12421246
// [1] `Block::next` (AtomicPtr) may be safely zero initialized.

crossbeam-queue/src/seg_queue.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,18 +58,22 @@ struct Block<T> {
5858
}
5959

6060
impl<T> Block<T> {
61-
/// Creates an empty block.
62-
fn new() -> Box<Self> {
61+
const LAYOUT: Layout = {
6362
let layout = Layout::new::<Self>();
6463
assert!(
6564
layout.size() != 0,
6665
"Block should never be zero-sized, as it has an AtomicPtr field"
6766
);
67+
layout
68+
};
69+
70+
/// Creates an empty block.
71+
fn new() -> Box<Self> {
6872
// SAFETY: layout is not zero-sized
69-
let ptr = unsafe { alloc_zeroed(layout) };
73+
let ptr = unsafe { alloc_zeroed(Self::LAYOUT) };
7074
// Handle allocation failure
7175
if ptr.is_null() {
72-
handle_alloc_error(layout)
76+
handle_alloc_error(Self::LAYOUT)
7377
}
7478
// SAFETY: This is safe because:
7579
// [1] `Block::next` (AtomicPtr) may be safely zero initialized.

0 commit comments

Comments
 (0)