Skip to content

Commit 37287d2

Browse files
committed
use free methods instead of traits
1 parent e23fdd1 commit 37287d2

File tree

1 file changed

+43
-46
lines changed

1 file changed

+43
-46
lines changed

src/memory.rs

Lines changed: 43 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -343,8 +343,9 @@ impl<'a, 'tcx> Memory<'a, 'tcx> {
343343
pub fn read_ptr(&self, ptr: Pointer) -> EvalResult<'tcx, Pointer> {
344344
let size = self.pointer_size();
345345
self.check_defined(ptr, size)?;
346-
let offset = self.get_bytes_unchecked(ptr, size)?
347-
.read_target_uint(size, self.endianess()).unwrap() as usize;
346+
let endianess = self.endianess();
347+
let bytes = self.get_bytes_unchecked(ptr, size)?;
348+
let offset = read_target_uint(endianess, bytes).unwrap() as usize;
348349
let alloc = self.get(ptr.alloc_id)?;
349350
match alloc.relocations.get(&ptr.offset) {
350351
Some(&alloc_id) => Ok(Pointer { alloc_id: alloc_id, offset: offset }),
@@ -391,21 +392,25 @@ impl<'a, 'tcx> Memory<'a, 'tcx> {
391392
}
392393

393394
pub fn read_int(&self, ptr: Pointer, size: usize) -> EvalResult<'tcx, i64> {
394-
self.get_bytes(ptr, size).map(|mut b| b.read_target_int(size, self.endianess()).unwrap())
395+
self.get_bytes(ptr, size).map(|b| read_target_int(self.endianess(), b).unwrap())
395396
}
396397

397398
pub fn write_int(&mut self, ptr: Pointer, n: i64, size: usize) -> EvalResult<'tcx, ()> {
398399
let endianess = self.endianess();
399-
self.get_bytes_mut(ptr, size).map(|mut b| b.write_target_int(n, size, endianess).unwrap())
400+
let b = self.get_bytes_mut(ptr, size)?;
401+
write_target_int(endianess, b, n).unwrap();
402+
Ok(())
400403
}
401404

402405
pub fn read_uint(&self, ptr: Pointer, size: usize) -> EvalResult<'tcx, u64> {
403-
self.get_bytes(ptr, size).map(|mut b| b.read_target_uint(size, self.endianess()).unwrap())
406+
self.get_bytes(ptr, size).map(|b| read_target_uint(self.endianess(), b).unwrap())
404407
}
405408

406409
pub fn write_uint(&mut self, ptr: Pointer, n: u64, size: usize) -> EvalResult<'tcx, ()> {
407410
let endianess = self.endianess();
408-
self.get_bytes_mut(ptr, size).map(|mut b| b.write_target_uint(n, size, endianess).unwrap())
411+
let b = self.get_bytes_mut(ptr, size)?;
412+
write_target_uint(endianess, b, n).unwrap();
413+
Ok(())
409414
}
410415

411416
pub fn read_isize(&self, ptr: Pointer) -> EvalResult<'tcx, i64> {
@@ -515,6 +520,38 @@ impl<'a, 'tcx> Memory<'a, 'tcx> {
515520
}
516521
}
517522

523+
////////////////////////////////////////////////////////////////////////////////
524+
// Methods to access integers in the target endianess
525+
////////////////////////////////////////////////////////////////////////////////
526+
527+
fn write_target_uint(endianess: layout::Endian, mut target: &mut [u8], data: u64) -> Result<(), byteorder::Error> {
528+
let len = target.len();
529+
match endianess {
530+
layout::Endian::Little => target.write_uint::<LittleEndian>(data, len),
531+
layout::Endian::Big => target.write_uint::<BigEndian>(data, len),
532+
}
533+
}
534+
fn write_target_int(endianess: layout::Endian, mut target: &mut [u8], data: i64) -> Result<(), byteorder::Error> {
535+
let len = target.len();
536+
match endianess {
537+
layout::Endian::Little => target.write_int::<LittleEndian>(data, len),
538+
layout::Endian::Big => target.write_int::<BigEndian>(data, len),
539+
}
540+
}
541+
542+
fn read_target_uint(endianess: layout::Endian, mut source: &[u8]) -> Result<u64, byteorder::Error> {
543+
match endianess {
544+
layout::Endian::Little => source.read_uint::<LittleEndian>(source.len()),
545+
layout::Endian::Big => source.read_uint::<BigEndian>(source.len()),
546+
}
547+
}
548+
fn read_target_int(endianess: layout::Endian, mut source: &[u8]) -> Result<i64, byteorder::Error> {
549+
match endianess {
550+
layout::Endian::Little => source.read_int::<LittleEndian>(source.len()),
551+
layout::Endian::Big => source.read_int::<BigEndian>(source.len()),
552+
}
553+
}
554+
518555
////////////////////////////////////////////////////////////////////////////////
519556
// Undefined byte tracking
520557
////////////////////////////////////////////////////////////////////////////////
@@ -591,43 +628,3 @@ impl UndefMask {
591628
fn bit_index(bits: usize) -> (usize, usize) {
592629
(bits / BLOCK_SIZE, bits % BLOCK_SIZE)
593630
}
594-
595-
trait ReadBytesExt2 {
596-
fn read_target_uint(&mut self, nbytes: usize, endian: layout::Endian) -> Result<u64, byteorder::Error>;
597-
fn read_target_int(&mut self, nbytes: usize, endian: layout::Endian) -> Result<i64, byteorder::Error>;
598-
}
599-
600-
impl<T: ReadBytesExt> ReadBytesExt2 for T {
601-
fn read_target_uint(&mut self, nbytes: usize, endian: layout::Endian) -> Result<u64, byteorder::Error> {
602-
match endian {
603-
layout::Endian::Little => ReadBytesExt::read_uint::<LittleEndian>(self, nbytes),
604-
layout::Endian::Big => ReadBytesExt::read_uint::<BigEndian>(self, nbytes),
605-
}
606-
}
607-
fn read_target_int(&mut self, nbytes: usize, endian: layout::Endian) -> Result<i64, byteorder::Error> {
608-
match endian {
609-
layout::Endian::Little => ReadBytesExt::read_int::<LittleEndian>(self, nbytes),
610-
layout::Endian::Big => ReadBytesExt::read_int::<BigEndian>(self, nbytes),
611-
}
612-
}
613-
}
614-
615-
trait WriteBytesExt2 {
616-
fn write_target_uint(&mut self, data: u64, nbytes: usize, endian: layout::Endian) -> Result<(), byteorder::Error>;
617-
fn write_target_int(&mut self, data: i64, nbytes: usize, endian: layout::Endian) -> Result<(), byteorder::Error>;
618-
}
619-
620-
impl<T: WriteBytesExt> WriteBytesExt2 for T {
621-
fn write_target_uint(&mut self, data: u64, nbytes: usize, endian: layout::Endian) -> Result<(), byteorder::Error> {
622-
match endian {
623-
layout::Endian::Little => WriteBytesExt::write_uint::<LittleEndian>(self, data, nbytes),
624-
layout::Endian::Big => WriteBytesExt::write_uint::<BigEndian>(self, data, nbytes),
625-
}
626-
}
627-
fn write_target_int(&mut self, data: i64, nbytes: usize, endian: layout::Endian) -> Result<(), byteorder::Error> {
628-
match endian {
629-
layout::Endian::Little => WriteBytesExt::write_int::<LittleEndian>(self, data, nbytes),
630-
layout::Endian::Big => WriteBytesExt::write_int::<BigEndian>(self, data, nbytes),
631-
}
632-
}
633-
}

0 commit comments

Comments
 (0)