Skip to content

Commit

Permalink
resize
Browse files Browse the repository at this point in the history
  • Loading branch information
aapoalas committed Sep 25, 2023
1 parent afd578c commit cad2fc2
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion nova_vm/src/heap/array_buffer.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::{
alloc::{alloc_zeroed, handle_alloc_error, Layout},
ptr::{read_unaligned, write_unaligned, NonNull},
ptr::{read_unaligned, write_bytes, write_unaligned, NonNull},
};

use crate::{
Expand Down Expand Up @@ -101,6 +101,20 @@ impl BackingStore {
self.cap
}

pub fn resize(&mut self, size: u32) {
debug_assert!(size <= self.cap);
let len = self.len;
self.len = size;
if size < len {
// Zero out the "dropped" bytes.
if let Some(data) = self.ptr {
// SAFETY: The data is properly initialized, and the T being written is
// checked to be fully within the length of the data allocation.
unsafe { write_bytes(data.as_ptr().add(size as usize), 0, (len - size) as usize) }
}
}
}

pub fn view_len<T: Viewable>(&self, offset: u32) -> u32 {
let size = std::mem::size_of::<T>() as u32;
(self.len - offset) / size
Expand Down

0 comments on commit cad2fc2

Please sign in to comment.