Skip to content

Commit c8dccf9

Browse files
committed
Move sweep_fn from a field in each page to the PageSet. #15.
1 parent d51554d commit c8dccf9

File tree

2 files changed

+12
-17
lines changed

2 files changed

+12
-17
lines changed

src/heap.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -263,12 +263,11 @@ impl<'h> HeapSession<'h> {
263263
fn get_page_set<'a, T: IntoHeapAllocation<'h> + 'a>(&'a mut self) -> PageSetRef<'a, 'h, T> {
264264
let key = heap_type_id::<T>();
265265
let heap: *mut Heap = self.heap;
266-
let page_ref =
267-
self.heap
266+
self.heap
268267
.page_sets
269268
.entry(key)
270-
.or_insert_with(|| unsafe { PageSet::new(heap) });
271-
unsafe { page_ref.unchecked_downcast_mut() }
269+
.or_insert_with(|| unsafe { PageSet::new::<T>(heap) })
270+
.downcast_mut()
272271
}
273272

274273
pub fn set_page_limit<T: IntoHeapAllocation<'h>>(&mut self, limit: Option<usize>) {

src/pages.rs

+9-13
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@ pub struct PageHeader {
5050
mark_bits: BitVec,
5151
allocated_bits: BitVec,
5252
mark_fn: unsafe fn(UntypedPointer, &mut MarkingTracer),
53-
sweep_fn: unsafe fn(&mut PageHeader),
5453
freelist: *mut (),
5554
}
5655

@@ -73,10 +72,6 @@ impl PageHeader {
7372
(self.mark_fn)(ptr, tracer);
7473
}
7574

76-
pub unsafe fn sweep(&mut self) {
77-
(self.sweep_fn)(self);
78-
}
79-
8075
pub fn type_id(&self) -> usize {
8176
self.mark_fn as usize
8277
}
@@ -268,7 +263,6 @@ impl PageBox {
268263
mark_bits: BitVec::from_elem(capacity, false),
269264
allocated_bits: BitVec::from_elem(capacity, false),
270265
mark_fn: mark_entry_point::<T>,
271-
sweep_fn: sweep_entry_point::<T>,
272266
freelist: ptr::null_mut(),
273267
},
274268
allocations: PhantomData,
@@ -310,6 +304,8 @@ impl Drop for PageBox {
310304
pub struct PageSet {
311305
heap: *mut Heap,
312306

307+
sweep_fn: unsafe fn(&mut PageHeader),
308+
313309
/// The collection of pages.
314310
///
315311
/// All pages in this collection have matching `.heap`, `.mark_fn`, and
@@ -326,25 +322,25 @@ impl PageSet {
326322
/// # Safety
327323
///
328324
/// Safe as long as `heap` is a valid pointer.
329-
pub unsafe fn new(heap: *mut Heap) -> PageSet {
325+
pub unsafe fn new<'h, T: IntoHeapAllocation<'h>>(heap: *mut Heap) -> PageSet {
330326
PageSet {
331327
heap,
328+
sweep_fn: sweep_entry_point::<T>,
332329
pages: vec![],
333330
limit: None
334331
}
335332
}
336333

337334
/// Downcast to a typed PageSetRef.
338335
///
339-
/// # Safety
336+
/// # Panics
340337
///
341-
/// The actual allocation type of this page set must be T.
342-
pub unsafe fn unchecked_downcast_mut<'a, 'h, T>(&'a mut self) -> PageSetRef<'a, 'h, T>
338+
/// If T is not the actual allocation type for this page set.
339+
pub fn downcast_mut<'a, 'h, T>(&'a mut self) -> PageSetRef<'a, 'h, T>
343340
where
344341
T: IntoHeapAllocation<'h> + 'a
345342
{
346-
// The assertion only covers the case where we have at least one page.
347-
assert!(self.pages.is_empty() || self.pages[0].downcast_mut::<T>().is_some());
343+
assert_eq!(self.sweep_fn as *const (), sweep_entry_point::<T> as *const ());
348344

349345
PageSetRef {
350346
pages: self,
@@ -371,7 +367,7 @@ impl PageSet {
371367
/// Safe to call only as the final part of GC.
372368
pub unsafe fn sweep(&mut self) {
373369
for page in &mut self.pages {
374-
page.sweep();
370+
(self.sweep_fn)(page);
375371
}
376372
}
377373

0 commit comments

Comments
 (0)