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

Fix memory being reclaimed too early #32

Merged
merged 1 commit into from
Sep 20, 2024
Merged
Show file tree
Hide file tree
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
20 changes: 14 additions & 6 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ fn main() -> miette::Result<()> {
}
Command::Compile { name, dest } => {
file_message(Green, "Assembing", &name);
let air = assemble(&name)?;
let contents = StaticSource::new(fs::read_to_string(&name).into_diagnostic()?);
let air = assemble(&contents)?;

let out_file_name =
dest.unwrap_or(name.with_extension("lc3").file_stem().unwrap().into());
Expand All @@ -87,6 +88,7 @@ fn main() -> miette::Result<()> {
let _ = file.write(&0x3000u16.to_be_bytes());
}

// Write lines
for stmt in air {
let _ = file.write(&stmt.emit()?.to_be_bytes());
}
Expand All @@ -97,7 +99,8 @@ fn main() -> miette::Result<()> {
}
Command::Check { name } => {
file_message(Green, "Checking", &name);
let _ = assemble(&name)?;
let contents = StaticSource::new(fs::read_to_string(&name).into_diagnostic()?);
let _ = assemble(&contents)?;
message(Green, "Success", "no errors found!");
Ok(())
}
Expand All @@ -120,7 +123,10 @@ fn main() -> miette::Result<()> {
message(Green, "Re-checking", "file change detected");
message(Cyan, "Help", "press CTRL+C to exit");

let _ = match assemble(&name) {
let mut contents = StaticSource::new(
fs::read_to_string(&name).into_diagnostic().unwrap(),
);
let _ = match assemble(&contents) {
Ok(_) => {
message(Green, "Success", "no errors found!");
}
Expand All @@ -130,6 +136,8 @@ fn main() -> miette::Result<()> {
};

reset_state();
// To avoid leaking memory
contents.reclaim();
Flow::Continue
}
EventKind::Remove(_) => {
Expand Down Expand Up @@ -182,7 +190,8 @@ where

fn run(name: &PathBuf) -> Result<()> {
file_message(MsgColor::Green, "Assembling", &name);
let air = assemble(&name)?;
let contents = StaticSource::new(fs::read_to_string(&name).into_diagnostic()?);
let air = assemble(&contents)?;

message(MsgColor::Green, "Running", "emitted binary");
let mut program = RunState::try_from(air)?;
Expand All @@ -193,8 +202,7 @@ fn run(name: &PathBuf) -> Result<()> {
}

/// Return assembly intermediate representation of source file for further processing
fn assemble(name: &PathBuf) -> Result<Air> {
let contents = StaticSource::new(fs::read_to_string(&name).into_diagnostic()?);
fn assemble(contents: &StaticSource) -> Result<Air> {
let parser = lace::AsmParser::new(contents.src())?;
let mut air = parser.parse()?;
air.backpatch()?;
Expand Down
4 changes: 1 addition & 3 deletions src/symbol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,8 @@ impl StaticSource {
pub fn src(&self) -> &'static str {
unsafe { &*self.src }
}
}

impl Drop for StaticSource {
fn drop(&mut self) {
pub fn reclaim(&mut self) {
unsafe { drop(Box::from_raw(self.src)) }
}
}
Expand Down
Loading