Move buffer param into function
Needed for thread-safe mode, otherwise there will be borrow problems with trying to borrow immutably, while already borrowed mutably.
This commit is contained in:
parent
7fe6ef4e35
commit
7546d3026d
|
@ -204,9 +204,10 @@ impl MemoryInstance {
|
||||||
|
|
||||||
/// Get value from memory at given offset.
|
/// Get value from memory at given offset.
|
||||||
pub fn get_value<T: LittleEndianConvert>(&self, offset: u32) -> Result<T, Error> {
|
pub fn get_value<T: LittleEndianConvert>(&self, offset: u32) -> Result<T, Error> {
|
||||||
let mut buffer = self.buffer.borrow_mut();
|
|
||||||
let region =
|
let region =
|
||||||
self.checked_region(&mut buffer, offset as usize, ::core::mem::size_of::<T>())?;
|
self.checked_region(offset as usize, ::core::mem::size_of::<T>())?;
|
||||||
|
|
||||||
|
let buffer = self.buffer.borrow();
|
||||||
Ok(T::from_little_endian(&buffer[region.range()]).expect("Slice size is checked"))
|
Ok(T::from_little_endian(&buffer[region.range()]).expect("Slice size is checked"))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -217,9 +218,9 @@ impl MemoryInstance {
|
||||||
///
|
///
|
||||||
/// [`get_into`]: #method.get_into
|
/// [`get_into`]: #method.get_into
|
||||||
pub fn get(&self, offset: u32, size: usize) -> Result<Vec<u8>, Error> {
|
pub fn get(&self, offset: u32, size: usize) -> Result<Vec<u8>, Error> {
|
||||||
let mut buffer = self.buffer.borrow_mut();
|
let region = self.checked_region(offset as usize, size)?;
|
||||||
let region = self.checked_region(&mut buffer, offset as usize, size)?;
|
|
||||||
|
|
||||||
|
let buffer = self.buffer.borrow();
|
||||||
Ok(buffer[region.range()].to_vec())
|
Ok(buffer[region.range()].to_vec())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -229,9 +230,9 @@ impl MemoryInstance {
|
||||||
///
|
///
|
||||||
/// Returns `Err` if the specified region is out of bounds.
|
/// Returns `Err` if the specified region is out of bounds.
|
||||||
pub fn get_into(&self, offset: u32, target: &mut [u8]) -> Result<(), Error> {
|
pub fn get_into(&self, offset: u32, target: &mut [u8]) -> Result<(), Error> {
|
||||||
let mut buffer = self.buffer.borrow_mut();
|
let region = self.checked_region(offset as usize, target.len())?;
|
||||||
let region = self.checked_region(&mut buffer, offset as usize, target.len())?;
|
|
||||||
|
|
||||||
|
let buffer = self.buffer.borrow();
|
||||||
target.copy_from_slice(&buffer[region.range()]);
|
target.copy_from_slice(&buffer[region.range()]);
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
|
@ -239,14 +240,14 @@ impl MemoryInstance {
|
||||||
|
|
||||||
/// Copy data in the memory at given offset.
|
/// Copy data in the memory at given offset.
|
||||||
pub fn set(&self, offset: u32, value: &[u8]) -> Result<(), Error> {
|
pub fn set(&self, offset: u32, value: &[u8]) -> Result<(), Error> {
|
||||||
let mut buffer = self.buffer.borrow_mut();
|
|
||||||
let range = self
|
let range = self
|
||||||
.checked_region(&mut buffer, offset as usize, value.len())?
|
.checked_region(offset as usize, value.len())?
|
||||||
.range();
|
.range();
|
||||||
|
|
||||||
if offset < self.lowest_used.get() {
|
if offset < self.lowest_used.get() {
|
||||||
self.lowest_used.set(offset);
|
self.lowest_used.set(offset);
|
||||||
}
|
}
|
||||||
|
let mut buffer = self.buffer.borrow_mut();
|
||||||
buffer[range].copy_from_slice(value);
|
buffer[range].copy_from_slice(value);
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
|
@ -254,13 +255,13 @@ impl MemoryInstance {
|
||||||
|
|
||||||
/// Copy value in the memory at given offset.
|
/// Copy value in the memory at given offset.
|
||||||
pub fn set_value<T: LittleEndianConvert>(&self, offset: u32, value: T) -> Result<(), Error> {
|
pub fn set_value<T: LittleEndianConvert>(&self, offset: u32, value: T) -> Result<(), Error> {
|
||||||
let mut buffer = self.buffer.borrow_mut();
|
|
||||||
let range = self
|
let range = self
|
||||||
.checked_region(&mut buffer, offset as usize, ::core::mem::size_of::<T>())?
|
.checked_region(offset as usize, ::core::mem::size_of::<T>())?
|
||||||
.range();
|
.range();
|
||||||
if offset < self.lowest_used.get() {
|
if offset < self.lowest_used.get() {
|
||||||
self.lowest_used.set(offset);
|
self.lowest_used.set(offset);
|
||||||
}
|
}
|
||||||
|
let mut buffer = self.buffer.borrow_mut();
|
||||||
value.into_little_endian(&mut buffer[range]);
|
value.into_little_endian(&mut buffer[range]);
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
@ -299,15 +300,12 @@ impl MemoryInstance {
|
||||||
Ok(size_before_grow)
|
Ok(size_before_grow)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn checked_region<B>(
|
fn checked_region(
|
||||||
&self,
|
&self,
|
||||||
buffer: &mut B,
|
|
||||||
offset: usize,
|
offset: usize,
|
||||||
size: usize,
|
size: usize,
|
||||||
) -> Result<CheckedRegion, Error>
|
) -> Result<CheckedRegion, Error> {
|
||||||
where
|
let mut buffer = self.buffer.borrow_mut();
|
||||||
B: ::core::ops::DerefMut<Target = Vec<u8>>,
|
|
||||||
{
|
|
||||||
let end = offset.checked_add(size).ok_or_else(|| {
|
let end = offset.checked_add(size).ok_or_else(|| {
|
||||||
Error::Memory(format!(
|
Error::Memory(format!(
|
||||||
"trying to access memory block of size {} from offset {}",
|
"trying to access memory block of size {} from offset {}",
|
||||||
|
@ -334,17 +332,13 @@ impl MemoryInstance {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
fn checked_region_pair<B>(
|
fn checked_region_pair(
|
||||||
&self,
|
&self,
|
||||||
buffer: &mut B,
|
|
||||||
offset1: usize,
|
offset1: usize,
|
||||||
size1: usize,
|
size1: usize,
|
||||||
offset2: usize,
|
offset2: usize,
|
||||||
size2: usize,
|
size2: usize,
|
||||||
) -> Result<(CheckedRegion, CheckedRegion), Error>
|
) -> Result<(CheckedRegion, CheckedRegion), Error> {
|
||||||
where
|
|
||||||
B: ::core::ops::DerefMut<Target = Vec<u8>>,
|
|
||||||
{
|
|
||||||
let end1 = offset1.checked_add(size1).ok_or_else(|| {
|
let end1 = offset1.checked_add(size1).ok_or_else(|| {
|
||||||
Error::Memory(format!(
|
Error::Memory(format!(
|
||||||
"trying to access memory block of size {} from offset {}",
|
"trying to access memory block of size {} from offset {}",
|
||||||
|
@ -359,6 +353,7 @@ impl MemoryInstance {
|
||||||
))
|
))
|
||||||
})?;
|
})?;
|
||||||
|
|
||||||
|
let mut buffer = self.buffer.borrow_mut();
|
||||||
let max = cmp::max(end1, end2);
|
let max = cmp::max(end1, end2);
|
||||||
if max <= self.current_size.get() && buffer.len() < max {
|
if max <= self.current_size.get() && buffer.len() < max {
|
||||||
buffer.resize(max, 0);
|
buffer.resize(max, 0);
|
||||||
|
@ -402,15 +397,14 @@ impl MemoryInstance {
|
||||||
///
|
///
|
||||||
/// Returns `Err` if either of specified regions is out of bounds.
|
/// Returns `Err` if either of specified regions is out of bounds.
|
||||||
pub fn copy(&self, src_offset: usize, dst_offset: usize, len: usize) -> Result<(), Error> {
|
pub fn copy(&self, src_offset: usize, dst_offset: usize, len: usize) -> Result<(), Error> {
|
||||||
let mut buffer = self.buffer.borrow_mut();
|
|
||||||
|
|
||||||
let (read_region, write_region) =
|
let (read_region, write_region) =
|
||||||
self.checked_region_pair(&mut buffer, src_offset, len, dst_offset, len)?;
|
self.checked_region_pair(src_offset, len, dst_offset, len)?;
|
||||||
|
|
||||||
if dst_offset < self.lowest_used.get() as usize {
|
if dst_offset < self.lowest_used.get() as usize {
|
||||||
self.lowest_used.set(dst_offset as u32);
|
self.lowest_used.set(dst_offset as u32);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let mut buffer = self.buffer.borrow_mut();
|
||||||
unsafe {
|
unsafe {
|
||||||
::core::ptr::copy(
|
::core::ptr::copy(
|
||||||
buffer[read_region.range()].as_ptr(),
|
buffer[read_region.range()].as_ptr(),
|
||||||
|
@ -439,10 +433,8 @@ impl MemoryInstance {
|
||||||
dst_offset: usize,
|
dst_offset: usize,
|
||||||
len: usize,
|
len: usize,
|
||||||
) -> Result<(), Error> {
|
) -> Result<(), Error> {
|
||||||
let mut buffer = self.buffer.borrow_mut();
|
|
||||||
|
|
||||||
let (read_region, write_region) =
|
let (read_region, write_region) =
|
||||||
self.checked_region_pair(&mut buffer, src_offset, len, dst_offset, len)?;
|
self.checked_region_pair(src_offset, len, dst_offset, len)?;
|
||||||
|
|
||||||
if read_region.intersects(&write_region) {
|
if read_region.intersects(&write_region) {
|
||||||
return Err(Error::Memory(format!(
|
return Err(Error::Memory(format!(
|
||||||
|
@ -454,6 +446,7 @@ impl MemoryInstance {
|
||||||
self.lowest_used.set(dst_offset as u32);
|
self.lowest_used.set(dst_offset as u32);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let mut buffer = self.buffer.borrow_mut();
|
||||||
unsafe {
|
unsafe {
|
||||||
::core::ptr::copy_nonoverlapping(
|
::core::ptr::copy_nonoverlapping(
|
||||||
buffer[read_region.range()].as_ptr(),
|
buffer[read_region.range()].as_ptr(),
|
||||||
|
@ -475,28 +468,25 @@ impl MemoryInstance {
|
||||||
dst_offset: usize,
|
dst_offset: usize,
|
||||||
len: usize,
|
len: usize,
|
||||||
) -> Result<(), Error> {
|
) -> Result<(), Error> {
|
||||||
if Rc::ptr_eq(&src.0, &dst.0) {
|
if ::MyRc::ptr_eq(&src.0, &dst.0) {
|
||||||
// `transfer` is invoked with with same source and destination. Let's assume that regions may
|
// `transfer` is invoked with with same source and destination. Let's assume that regions may
|
||||||
// overlap and use `copy`.
|
// overlap and use `copy`.
|
||||||
return src.copy(src_offset, dst_offset, len);
|
return src.copy(src_offset, dst_offset, len);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Because memory references point to different memory instances, it is safe to `borrow_mut`
|
|
||||||
// both buffers at once (modulo `with_direct_access_mut`).
|
|
||||||
let mut src_buffer = src.buffer.borrow_mut();
|
|
||||||
let mut dst_buffer = dst.buffer.borrow_mut();
|
|
||||||
|
|
||||||
let src_range = src
|
let src_range = src
|
||||||
.checked_region(&mut src_buffer, src_offset, len)?
|
.checked_region(src_offset, len)?
|
||||||
.range();
|
.range();
|
||||||
let dst_range = dst
|
let dst_range = dst
|
||||||
.checked_region(&mut dst_buffer, dst_offset, len)?
|
.checked_region(dst_offset, len)?
|
||||||
.range();
|
.range();
|
||||||
|
|
||||||
if dst_offset < dst.lowest_used.get() as usize {
|
if dst_offset < dst.lowest_used.get() as usize {
|
||||||
dst.lowest_used.set(dst_offset as u32);
|
dst.lowest_used.set(dst_offset as u32);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let mut dst_buffer = dst.buffer.borrow_mut();
|
||||||
|
let src_buffer = src.buffer.borrow();
|
||||||
dst_buffer[dst_range].copy_from_slice(&src_buffer[src_range]);
|
dst_buffer[dst_range].copy_from_slice(&src_buffer[src_range]);
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
|
@ -510,14 +500,13 @@ impl MemoryInstance {
|
||||||
///
|
///
|
||||||
/// Returns `Err` if the specified region is out of bounds.
|
/// Returns `Err` if the specified region is out of bounds.
|
||||||
pub fn clear(&self, offset: usize, new_val: u8, len: usize) -> Result<(), Error> {
|
pub fn clear(&self, offset: usize, new_val: u8, len: usize) -> Result<(), Error> {
|
||||||
let mut buffer = self.buffer.borrow_mut();
|
let range = self.checked_region(offset, len)?.range();
|
||||||
|
|
||||||
let range = self.checked_region(&mut buffer, offset, len)?.range();
|
|
||||||
|
|
||||||
if offset < self.lowest_used.get() as usize {
|
if offset < self.lowest_used.get() as usize {
|
||||||
self.lowest_used.set(offset as u32);
|
self.lowest_used.set(offset as u32);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let mut buffer = self.buffer.borrow_mut();
|
||||||
for val in &mut buffer[range] {
|
for val in &mut buffer[range] {
|
||||||
*val = new_val
|
*val = new_val
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue