Skip to content

Commit adb4c83

Browse files
committed
Mem.[re||de]allocate
1 parent 2e93c0b commit adb4c83

File tree

3 files changed

+21
-24
lines changed

3 files changed

+21
-24
lines changed

src/librustc_mir/interpret/memory.rs

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use std::ptr;
1111
use std::borrow::Cow;
1212

1313
use rustc::ty::{self, Instance, ParamEnv, query::TyCtxtAt};
14-
use rustc::ty::layout::{Align, TargetDataLayout, Size, HasDataLayout};
14+
use rustc::ty::layout::{Align, MemoryPosition, TargetDataLayout, Size, HasDataLayout};
1515
use rustc_data_structures::fx::{FxHashSet, FxHashMap};
1616

1717
use syntax::ast::Mutability;
@@ -165,11 +165,10 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'mir, 'tcx, M> {
165165

166166
pub fn allocate(
167167
&mut self,
168-
size: Size,
169-
align: Align,
168+
mem_pos: MemoryPosition,
170169
kind: MemoryKind<M::MemoryKinds>,
171170
) -> Pointer<M::PointerTag> {
172-
let alloc = Allocation::undef(size, align);
171+
let alloc = Allocation::undef(mem_pos.size, mem_pos.align);
173172
self.allocate_with(alloc, kind)
174173
}
175174

@@ -196,9 +195,8 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'mir, 'tcx, M> {
196195
pub fn reallocate(
197196
&mut self,
198197
ptr: Pointer<M::PointerTag>,
199-
old_size_and_align: Option<(Size, Align)>,
200-
new_size: Size,
201-
new_align: Align,
198+
old_mem_pos: Option<MemoryPosition>,
199+
new_mem_pos: MemoryPosition,
202200
kind: MemoryKind<M::MemoryKinds>,
203201
) -> InterpResult<'tcx, Pointer<M::PointerTag>> {
204202
if ptr.offset.bytes() != 0 {
@@ -207,18 +205,18 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'mir, 'tcx, M> {
207205

208206
// For simplicities' sake, we implement reallocate as "alloc, copy, dealloc".
209207
// This happens so rarely, the perf advantage is outweighed by the maintenance cost.
210-
let new_ptr = self.allocate(new_size, new_align, kind);
211-
let old_size = match old_size_and_align {
212-
Some((size, _align)) => size,
208+
let new_ptr = self.allocate(new_mem_pos, kind);
209+
let old_size = match old_mem_pos {
210+
Some(old_mem_pos) => old_mem_pos.size,
213211
None => self.get_raw(ptr.alloc_id)?.size,
214212
};
215213
self.copy(
216214
ptr,
217215
new_ptr,
218-
old_size.min(new_size),
216+
old_size.min(new_mem_pos.size),
219217
/*nonoverlapping*/ true,
220218
)?;
221-
self.deallocate(ptr, old_size_and_align, kind)?;
219+
self.deallocate(ptr, old_mem_pos, kind)?;
222220

223221
Ok(new_ptr)
224222
}
@@ -237,7 +235,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'mir, 'tcx, M> {
237235
pub fn deallocate(
238236
&mut self,
239237
ptr: Pointer<M::PointerTag>,
240-
old_size_and_align: Option<(Size, Align)>,
238+
old_mem_pos: Option<MemoryPosition>,
241239
kind: MemoryKind<M::MemoryKinds>,
242240
) -> InterpResult<'tcx> {
243241
trace!("deallocating: {}", ptr.alloc_id);
@@ -270,7 +268,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'mir, 'tcx, M> {
270268
format!("{:?}", kind),
271269
))
272270
}
273-
if let Some((size, align)) = old_size_and_align {
271+
if let Some(MemoryPosition {size, align}) = old_mem_pos {
274272
if size != alloc.size || align != alloc.align {
275273
let bytes = alloc.size;
276274
throw_unsup!(IncorrectAllocationInformation(size, bytes, align, alloc.align))

src/librustc_mir/interpret/place.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -993,7 +993,7 @@ where
993993
let mem_pos = self.mem_pos_of(meta, local_layout)?
994994
.expect("Cannot allocate for non-dyn-sized type");
995995
let (size, align) = (mem_pos.size, mem_pos.align);
996-
let ptr = self.memory.allocate(size, align, MemoryKind::Stack);
996+
let ptr = self.memory.allocate(mem_pos, MemoryKind::Stack);
997997
let mplace = MemPlace { ptr: ptr.into(), align, meta };
998998
if let Some(value) = old_val {
999999
// Preserve old value.
@@ -1030,7 +1030,7 @@ where
10301030
layout: TyLayout<'tcx>,
10311031
kind: MemoryKind<M::MemoryKinds>,
10321032
) -> MPlaceTy<'tcx, M::PointerTag> {
1033-
let ptr = self.memory.allocate(layout.pref_pos.size, layout.pref_pos.align.abi, kind);
1033+
let ptr = self.memory.allocate(layout.pref_pos.mem_pos(), kind);
10341034
MPlaceTy::from_aligned_ptr(ptr, layout)
10351035
}
10361036

src/librustc_mir/interpret/traits.rs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -47,15 +47,14 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
4747
let size = layout.pref_pos.size.bytes();
4848
let align = layout.pref_pos.align.abi.bytes();
4949

50-
let ptr_pref_pos = self.pointer_pos();
50+
let ptr_mem_pos = self.tcx.data_layout.pointer_pos.mem_pos();
5151

5252
// /////////////////////////////////////////////////////////////////////////////////////////
5353
// If you touch this code, be sure to also make the corresponding changes to
5454
// `get_vtable` in rust_codegen_llvm/meth.rs
5555
// /////////////////////////////////////////////////////////////////////////////////////////
5656
let vtable = self.memory.allocate(
57-
(ptr_pref_pos * (3 + methods.len() as u64)).size,
58-
ptr_pref_pos.align.abi,
57+
ptr_mem_pos * (3 + methods.len() as u64),
5958
MemoryKind::Vtable,
6059
);
6160
let tcx = &*self.tcx;
@@ -69,12 +68,12 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
6968
let vtable_alloc = self.memory.get_raw_mut(vtable.alloc_id)?;
7069
vtable_alloc.write_ptr_sized(tcx, vtable, Scalar::Ptr(drop).into())?;
7170

72-
let size_ptr = vtable.offset(ptr_pref_pos.size, tcx)?;
71+
let size_ptr = vtable.offset(ptr_mem_pos.size, tcx)?;
7372
vtable_alloc.write_ptr_sized(tcx, size_ptr,
74-
Scalar::from_uint(size, ptr_pref_pos.size).into())?;
75-
let align_ptr = vtable.offset((ptr_pref_pos * 2).size, tcx)?;
73+
Scalar::from_uint(size, ptr_mem_pos.size).into())?;
74+
let align_ptr = vtable.offset((ptr_mem_pos * 2).size, tcx)?;
7675
vtable_alloc.write_ptr_sized(tcx, align_ptr,
77-
Scalar::from_uint(align, ptr_pref_pos.size).into())?;
76+
Scalar::from_uint(align, ptr_mem_pos.size).into())?;
7877

7978
for (i, method) in methods.iter().enumerate() {
8079
if let Some((def_id, substs)) = *method {
@@ -87,7 +86,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
8786
).ok_or_else(|| err_inval!(TooGeneric))?;
8887
let fn_ptr = self.memory.create_fn_alloc(FnVal::Instance(instance));
8988
// We cannot use `vtable_allic` as we are creating fn ptrs in this loop.
90-
let method_ptr = vtable.offset((ptr_pref_pos * (3 + i as u64)).size, tcx)?;
89+
let method_ptr = vtable.offset((ptr_mem_pos * (3 + i as u64)).size, tcx)?;
9190
self.memory.get_raw_mut(vtable.alloc_id)?
9291
.write_ptr_sized(tcx, method_ptr, Scalar::Ptr(fn_ptr).into())?;
9392
}

0 commit comments

Comments
 (0)