Skip to content

Commit 1926679

Browse files
RalfJungnia-e
authored andcommitted
avoid some clones
1 parent 9bba6e8 commit 1926679

File tree

1 file changed

+6
-10
lines changed

1 file changed

+6
-10
lines changed

src/alloc/alloc_bytes.rs

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -90,15 +90,15 @@ impl MiriAllocBytes {
9090
size: u64,
9191
align: u64,
9292
params: MiriAllocParams,
93-
alloc_fn: impl FnOnce(Layout) -> *mut u8,
93+
alloc_fn: impl FnOnce(Layout, &MiriAllocParams) -> *mut u8,
9494
) -> Result<MiriAllocBytes, ()> {
9595
let size = usize::try_from(size).map_err(|_| ())?;
9696
let align = usize::try_from(align).map_err(|_| ())?;
9797
let layout = Layout::from_size_align(size, align).map_err(|_| ())?;
9898
// When size is 0 we allocate 1 byte anyway, to ensure each allocation has a unique address.
9999
let alloc_layout =
100100
if size == 0 { Layout::from_size_align(1, align).unwrap() } else { layout };
101-
let ptr = alloc_fn(alloc_layout);
101+
let ptr = alloc_fn(alloc_layout, &params);
102102
if ptr.is_null() {
103103
Err(())
104104
} else {
@@ -119,12 +119,9 @@ impl AllocBytes for MiriAllocBytes {
119119
let slice = slice.into();
120120
let size = slice.len();
121121
let align = align.bytes();
122-
// We need to clone here, since one copy goes into the alloc_fn and
123-
// another is stored in the actual MiriAllocBytes
124-
let p_clone = params.clone();
125122
// SAFETY: `alloc_fn` will only be used with `size != 0`.
126-
let alloc_fn = |layout| unsafe {
127-
match p_clone {
123+
let alloc_fn = |layout, params: &MiriAllocParams| unsafe {
124+
match params {
128125
MiriAllocParams::Global => alloc::alloc(layout),
129126
#[cfg(target_os = "linux")]
130127
MiriAllocParams::Isolated(alloc) => alloc.borrow_mut().alloc(layout),
@@ -143,10 +140,9 @@ impl AllocBytes for MiriAllocBytes {
143140
fn zeroed(size: Size, align: Align, params: MiriAllocParams) -> Option<Self> {
144141
let size = size.bytes();
145142
let align = align.bytes();
146-
let p_clone = params.clone();
147143
// SAFETY: `alloc_fn` will only be used with `size != 0`.
148-
let alloc_fn = |layout| unsafe {
149-
match p_clone {
144+
let alloc_fn = |layout, params: &MiriAllocParams| unsafe {
145+
match params {
150146
MiriAllocParams::Global => alloc::alloc_zeroed(layout),
151147
#[cfg(target_os = "linux")]
152148
MiriAllocParams::Isolated(alloc) => alloc.borrow_mut().alloc_zeroed(layout),

0 commit comments

Comments
 (0)