Skip to content

Commit

Permalink
Fix memory being reclaimed too early
Browse files Browse the repository at this point in the history
  • Loading branch information
rozukke committed Sep 20, 2024
1 parent e598bdf commit 2846092
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 9 deletions.
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

0 comments on commit 2846092

Please sign in to comment.