Skip to content

Commit

Permalink
Automatically add finalizer.
Browse files Browse the repository at this point in the history
Use needs_drop to see if the type needs to be dropped. Right now this is only .
  • Loading branch information
Darksecond committed May 28, 2024
1 parent b7790ca commit 6561f41
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 13 deletions.
11 changes: 9 additions & 2 deletions lox-gc/src/gc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ impl ManagedHeap {
}
}

pub fn finalize(&self, gc: Gc<()>) {
fn finalize(&self, gc: Gc<()>) {
self.finalizers.borrow_mut().push(gc);
}

Expand Down Expand Up @@ -90,13 +90,20 @@ impl ManagedHeap {
pub fn manage<T>(&self, data: T) -> Gc<T> where T: Trace + 'static {
let layout = std::alloc::Layout::new::<Allocation<T>>();
let ptr = self.heap.alloc(layout) as *mut Allocation<T>;
unsafe {
let gc = unsafe {
ptr.write(Allocation::new(data));

Gc {
ptr: NonNull::new_unchecked(ptr),
}
};

if std::mem::needs_drop::<T>() {
//eprintln!("Type {} needs drop. Adding to finalizers.", std::any::type_name::<T>());
self.finalize(gc.erase());
}

gc
}

pub fn collect(&self, roots: &[&dyn Trace]) {
Expand Down
6 changes: 0 additions & 6 deletions lox-gc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,3 @@ pub fn collect(roots: &[&dyn Trace]) {
heap.collect(roots)
})
}

pub fn finalize<T>(gc: Gc<T>) {
HEAP.with(|heap| {
heap.finalize(gc.erase())
})
}
6 changes: 3 additions & 3 deletions lox-vm/src/memory/closure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ use std::cell::Cell;
use lox_gc::{Gc, Trace, Tracer};
use crate::memory::{Import, Upvalue};
use crate::fiber::Fiber;
use arrayvec::ArrayVec;
use crate::string::LoxString;
use crate::array::Array;

pub struct Closure {
pub function: Function,
pub upvalues: ArrayVec<Gc<Cell<Upvalue>>, 128>,
pub upvalues: Array<Gc<Cell<Upvalue>>>,
}

unsafe impl Trace for Closure {
Expand Down Expand Up @@ -57,7 +57,7 @@ impl Closure {
};

Closure {
upvalues: ArrayVec::new(),
upvalues: Array::new(),
function,
}
}
Expand Down
2 changes: 0 additions & 2 deletions lox-vm/src/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,6 @@ impl Runtime {
fn prepare_interpret(&mut self, module: Module) -> Gc<Closure> {
let import = Import::with_module("_root", module, &mut self.interner);
let import: Gc<Import> = self.manage(import.into());
lox_gc::finalize(import);
self.imports.insert(import.name.clone(), import);
self.globals_import().copy_to(&import);

Expand All @@ -142,7 +141,6 @@ impl Runtime {
if let Some(module) = module {
let import = Import::with_module(path, module, &mut self.interner);
let import = self.manage(import.into());
lox_gc::finalize(import);
self.imports.insert(path.into(), import);
self.globals_import().copy_to(&import);

Expand Down

0 comments on commit 6561f41

Please sign in to comment.