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 bc3ed10
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 34 deletions.
4 changes: 2 additions & 2 deletions gc/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "gc"
version = "0.2.1"
version = "0.3.0"
authors = ["Manish Goregaokar <[email protected]>", "Michael Layzell <[email protected]>"]
description = "Tracing garbage collector plugin for Rust. Not ready for use yet, please see README"
repository = "https://github.com/Manishearth/rust-gc"
Expand All @@ -12,4 +12,4 @@ keywords = ["garbage", "plugin", "memory"]
nightly = []

[dev-dependencies]
gc_derive = { path = "../gc_derive", version = "0.2.1" }
gc_derive = { path = "../gc_derive", version = "0.3" }
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
19 changes: 6 additions & 13 deletions gc/src/stable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
//! Shared, such that the same code can be used between the nightly and stable
//! versions of rust-gc.

use std::ops::Deref;
use std::marker::PhantomData;

/// See `::core::nonzero::NonZero`
Expand All @@ -11,17 +10,14 @@ pub struct NonZero<T> {
p: T,
}

impl<T> Deref for NonZero<T> {
type Target = T;
fn deref(&self) -> &T {
&self.p
}
}

impl<T> NonZero<T> {
pub unsafe fn new(p: T) -> NonZero<T> {
NonZero { p: p }
}

pub fn get(self) -> T {
self.p
}
}

/// See `::std::prt::Shared`
Expand All @@ -37,12 +33,9 @@ impl<T: ?Sized> Shared<T> {
_pd: PhantomData,
}
}
}

impl<T: ?Sized> Deref for Shared<T> {
type Target = *mut T;
fn deref(&self) -> &*mut T {
&self.p
pub fn as_ptr(&self) -> *mut T {
self.p.get()
}
}

Expand Down
2 changes: 1 addition & 1 deletion gc_derive/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "gc_derive"
version = "0.2.1"
version = "0.3.0"
authors = ["Manish Goregaokar <[email protected]>", "Michael Layzell <[email protected]>"]

description = "Garbage collector derive plugin for rust-gc"
Expand Down

0 comments on commit bc3ed10

Please sign in to comment.