From 83adde623f5ba4c7fb1aee5200658a677e8a7046 Mon Sep 17 00:00:00 2001 From: Scott Olson Date: Mon, 7 Mar 2016 07:19:43 -0600 Subject: [PATCH] Simplfy memory allocation. --- src/interpreter.rs | 12 +++++++----- src/memory.rs | 8 ++------ 2 files changed, 9 insertions(+), 11 deletions(-) diff --git a/src/interpreter.rs b/src/interpreter.rs index c08ef00612b2..b5023b5b9fd2 100644 --- a/src/interpreter.rs +++ b/src/interpreter.rs @@ -118,7 +118,7 @@ impl<'a, 'tcx: 'a> Interpreter<'a, 'tcx> { for (arg_decl, arg_operand) in mir.arg_decls.iter().zip(args) { let repr = Repr::from_ty(arg_decl.ty); - let dest = self.memory.allocate(&repr); + let dest = self.memory.allocate(repr.size()); let src = try!(self.operand_to_ptr(arg_operand)); try!(self.memory.copy(src, dest, repr.size())); locals.push(dest); @@ -126,7 +126,9 @@ impl<'a, 'tcx: 'a> Interpreter<'a, 'tcx> { let var_tys = mir.var_decls.iter().map(|v| v.ty); let temp_tys = mir.temp_decls.iter().map(|t| t.ty); - locals.extend(var_tys.chain(temp_tys).map(|ty| self.memory.allocate(&Repr::from_ty(ty)))); + locals.extend(var_tys.chain(temp_tys).map(|ty| { + self.memory.allocate(Repr::from_ty(ty).size()) + })); self.stack.push(Frame { mir: mir, @@ -411,7 +413,7 @@ impl<'a, 'tcx: 'a> Interpreter<'a, 'tcx> { match *const_val { Float(_f) => unimplemented!(), Int(n) => { - let ptr = self.memory.allocate(&Repr::Int); + let ptr = self.memory.allocate(Repr::Int.size()); try!(self.memory.write_int(ptr, n)); Ok(ptr) } @@ -419,7 +421,7 @@ impl<'a, 'tcx: 'a> Interpreter<'a, 'tcx> { Str(ref _s) => unimplemented!(), ByteStr(ref _bs) => unimplemented!(), Bool(b) => { - let ptr = self.memory.allocate(&Repr::Bool); + let ptr = self.memory.allocate(Repr::Bool.size()); try!(self.memory.write_bool(ptr, b)); Ok(ptr) }, @@ -446,7 +448,7 @@ pub fn interpret_start_points<'tcx>(tcx: &TyCtxt<'tcx>, mir_map: &MirMap<'tcx>) let mut miri = Interpreter::new(tcx, mir_map); let return_ptr = match mir.return_ty { - ty::FnConverging(ty) => Some(miri.memory.allocate(&Repr::from_ty(ty))), + ty::FnConverging(ty) => Some(miri.memory.allocate(Repr::from_ty(ty).size())), ty::FnDiverging => None, }; miri.call(mir, &[], return_ptr).unwrap(); diff --git a/src/memory.rs b/src/memory.rs index e6d3f5096fad..6ebdf17f82ca 100644 --- a/src/memory.rs +++ b/src/memory.rs @@ -48,17 +48,13 @@ impl Memory { Memory { next_id: 0, alloc_map: HashMap::new() } } - pub fn allocate_raw(&mut self, size: usize) -> AllocId { + pub fn allocate(&mut self, size: usize) -> Pointer { let id = AllocId(self.next_id); let alloc = Allocation { bytes: vec![0; size] }; self.alloc_map.insert(self.next_id, alloc); self.next_id += 1; - id - } - - pub fn allocate(&mut self, repr: &Repr) -> Pointer { Pointer { - alloc_id: self.allocate_raw(repr.size()), + alloc_id: id, offset: 0, } }