Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

changed Memory::init to utilize Vec::resize #170

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions src/memory.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::convert::TryInto;

/// Emulates main memory.
pub struct Memory {
/// Memory content
Expand All @@ -18,11 +20,15 @@ impl Memory {
/// # Arguments
/// * `capacity`
pub fn init(&mut self, capacity: u64) {
for _i in 0..((capacity + 7) / 8) {
self.data.push(0);
}
// casting may fail on 32-bit targets, and an error here is more useful than one in a VM
// expecting more memory than it has
self.data.resize(
((capacity + 7) / 8).try_into()
.expect("unable to allocate, usize cannot handle the required capacity"),
0
);
}

/// Reads a byte from memory.
///
/// # Arguments
Expand Down