Skip to content

Commit

Permalink
Update for new Shared and NonZero API.
Browse files Browse the repository at this point in the history
  • Loading branch information
kevincox committed Jul 22, 2017
1 parent d8252ae commit f83de62
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 18 deletions.
24 changes: 12 additions & 12 deletions gc/src/gc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,14 @@ impl Drop for GcState {
{
let mut p = &self.boxes_start;
while let Some(node) = *p {
Finalize::finalize(&(**node).data);
p = &(**node).header.next;
Finalize::finalize(&(*node.as_ptr()).data);
p = &(*node.as_ptr()).header.next;
}
}

let _guard = DropGuard::new();
while let Some(node) = self.boxes_start {
let node = Box::from_raw(*node);
let node = Box::from_raw(node.as_ptr());
self.boxes_start = node.header.next;
}
}
Expand Down Expand Up @@ -162,39 +162,39 @@ fn collect_garbage(st: &mut GcState) {
// Walk the tree, tracing and marking the nodes
let mut mark_head = *head;
while let Some(node) = mark_head {
if (**node).header.roots.get() > 0 {
(**node).trace_inner();
if (*node.as_ptr()).header.roots.get() > 0 {
(*node.as_ptr()).trace_inner();
}

mark_head = (**node).header.next;
mark_head = (*node.as_ptr()).header.next;
}

// Collect a vector of all of the nodes which were not marked,
// and unmark the ones which were.
let mut unmarked = Vec::new();
let mut unmark_head = head;
while let Some(node) = *unmark_head {
if (**node).header.marked.get() {
(**node).header.marked.set(false);
if (*node.as_ptr()).header.marked.get() {
(*node.as_ptr()).header.marked.set(false);
} else {
unmarked.push(Unmarked {
incoming: unmark_head,
this: node,
});
}
unmark_head = &mut (**node).header.next;
unmark_head = &mut (*node.as_ptr()).header.next;
}
unmarked
}

unsafe fn sweep(finalized: Vec<Unmarked>, bytes_allocated: &mut usize) {
let _guard = DropGuard::new();
for node in finalized.into_iter().rev() {
if (**node.this).header.marked.get() {
if (*node.this.as_ptr()).header.marked.get() {
continue;
}
let incoming = node.incoming;
let mut node = Box::from_raw(*node.this);
let mut node = Box::from_raw(node.this.as_ptr());
*bytes_allocated -= mem::size_of_val::<GcBox<_>>(&*node);
*incoming = node.header.next.take();
}
Expand All @@ -206,7 +206,7 @@ fn collect_garbage(st: &mut GcState) {
return;
}
for node in &unmarked {
Trace::finalize_glue(&(**node.this).data);
Trace::finalize_glue(&(*node.this.as_ptr()).data);
}
mark(&mut st.boxes_start);
sweep(unmarked, &mut st.bytes_allocated);
Expand Down
12 changes: 6 additions & 6 deletions gc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,9 @@ impl<T: Trace> Gc<T> {

// When we create a Gc<T>, all pointers which have been moved to the
// heap no longer need to be rooted, so we unroot them.
(**ptr).value().unroot();
(*ptr.as_ptr()).value().unroot();
let gc = Gc {
ptr_root: Cell::new(NonZero::new(*ptr)),
ptr_root: Cell::new(NonZero::new(ptr.as_ptr())),
marker: PhantomData,
};
gc.set_root();
Expand All @@ -97,18 +97,18 @@ impl<T: Trace> Gc<T> {
/// Returns the given pointer with its root bit cleared.
unsafe fn clear_root_bit<T: ?Sized + Trace>(ptr: NonZero<*const GcBox<T>>)
-> NonZero<*const GcBox<T>> {
let mut ptr = *ptr;
let mut ptr = ptr.get();
*(&mut ptr as *mut *const GcBox<T> as *mut usize) &= !1;
NonZero::new(ptr)
}

impl<T: Trace + ?Sized> Gc<T> {
fn rooted(&self) -> bool {
*self.ptr_root.get() as *const u8 as usize & 1 != 0
self.ptr_root.get().get() as *const u8 as usize & 1 != 0
}

unsafe fn set_root(&self) {
let mut ptr = *self.ptr_root.get();
let mut ptr = self.ptr_root.get().get();
*(&mut ptr as *mut *const GcBox<T> as *mut usize) |= 1;
self.ptr_root.set(NonZero::new(ptr));
}
Expand All @@ -127,7 +127,7 @@ impl<T: Trace + ?Sized> Gc<T> {
// This assert exists just in case.
assert!(finalizer_safe());

unsafe { &**clear_root_bit(self.ptr_root.get()) }
unsafe { &*clear_root_bit(self.ptr_root.get()).get() }
}
}

Expand Down

0 comments on commit f83de62

Please sign in to comment.