Skip to content

Commit

Permalink
refactor(memory): split mark&sweep alloc function
Browse files Browse the repository at this point in the history
  • Loading branch information
Wodann committed May 8, 2020
1 parent 8faae79 commit cab1dae
Showing 1 changed file with 22 additions and 14 deletions.
36 changes: 22 additions & 14 deletions crates/mun_memory/src/gc/mark_sweep.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,26 +54,39 @@ where
}
}

/// Logs an allocation
fn log_alloc(&self, handle: GcPtr, ty: T) {
{
let mut stats = self.stats.write();
stats.allocated_memory += ty.layout().size();
}

self.observer.event(Event::Allocation(handle));
}

/// Returns the observer
pub fn observer(&self) -> &O {
&self.observer
}
}

fn alloc_obj<T: Clone + TypeLayout + TypeTrace>(ty: T) -> Pin<Box<ObjectInfo<T>>> {
let ptr = unsafe { std::alloc::alloc(ty.layout()) };
Box::pin(ObjectInfo {
ptr,
ty,
roots: 0,
color: Color::White,
})
}

impl<T, O> GcRuntime<T> for MarkSweep<T, O>
where
T: TypeLayout + TypeTrace + Clone,
O: Observer<Event = Event>,
{
fn alloc(&self, ty: T) -> GcPtr {
let layout = ty.layout();
let ptr = unsafe { std::alloc::alloc(layout) };
let object = Box::pin(ObjectInfo {
ptr,
ty,
roots: 0,
color: Color::White,
});
let object = alloc_obj(ty.clone());

// We want to return a pointer to the `ObjectInfo`, to be used as handle.
let handle = (object.as_ref().deref() as *const _ as RawGcPtr).into();
Expand All @@ -83,12 +96,7 @@ where
objects.insert(handle, object);
}

{
let mut stats = self.stats.write();
stats.allocated_memory += layout.size();
}

self.observer.event(Event::Allocation(handle));
self.log_alloc(handle, ty);
handle
}

Expand Down

0 comments on commit cab1dae

Please sign in to comment.