Skip to content

Commit a804158

Browse files
committed
Update for new Shared and NonZero API.
Changed in rust-lang/rust#41064 Fixes Manishearth#55
1 parent d8252ae commit a804158

File tree

5 files changed

+27
-33
lines changed

5 files changed

+27
-33
lines changed

gc/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "gc"
3-
version = "0.2.1"
3+
version = "0.3.0"
44
authors = ["Manish Goregaokar <[email protected]>", "Michael Layzell <[email protected]>"]
55
description = "Tracing garbage collector plugin for Rust. Not ready for use yet, please see README"
66
repository = "https://github.com/Manishearth/rust-gc"
@@ -12,4 +12,4 @@ keywords = ["garbage", "plugin", "memory"]
1212
nightly = []
1313

1414
[dev-dependencies]
15-
gc_derive = { path = "../gc_derive", version = "0.2.1" }
15+
gc_derive = { path = "../gc_derive", version = "0.3" }

gc/src/gc.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,14 @@ impl Drop for GcState {
2727
{
2828
let mut p = &self.boxes_start;
2929
while let Some(node) = *p {
30-
Finalize::finalize(&(**node).data);
31-
p = &(**node).header.next;
30+
Finalize::finalize(&(*node.as_ptr()).data);
31+
p = &(*node.as_ptr()).header.next;
3232
}
3333
}
3434

3535
let _guard = DropGuard::new();
3636
while let Some(node) = self.boxes_start {
37-
let node = Box::from_raw(*node);
37+
let node = Box::from_raw(node.as_ptr());
3838
self.boxes_start = node.header.next;
3939
}
4040
}
@@ -162,39 +162,39 @@ fn collect_garbage(st: &mut GcState) {
162162
// Walk the tree, tracing and marking the nodes
163163
let mut mark_head = *head;
164164
while let Some(node) = mark_head {
165-
if (**node).header.roots.get() > 0 {
166-
(**node).trace_inner();
165+
if (*node.as_ptr()).header.roots.get() > 0 {
166+
(*node.as_ptr()).trace_inner();
167167
}
168168

169-
mark_head = (**node).header.next;
169+
mark_head = (*node.as_ptr()).header.next;
170170
}
171171

172172
// Collect a vector of all of the nodes which were not marked,
173173
// and unmark the ones which were.
174174
let mut unmarked = Vec::new();
175175
let mut unmark_head = head;
176176
while let Some(node) = *unmark_head {
177-
if (**node).header.marked.get() {
178-
(**node).header.marked.set(false);
177+
if (*node.as_ptr()).header.marked.get() {
178+
(*node.as_ptr()).header.marked.set(false);
179179
} else {
180180
unmarked.push(Unmarked {
181181
incoming: unmark_head,
182182
this: node,
183183
});
184184
}
185-
unmark_head = &mut (**node).header.next;
185+
unmark_head = &mut (*node.as_ptr()).header.next;
186186
}
187187
unmarked
188188
}
189189

190190
unsafe fn sweep(finalized: Vec<Unmarked>, bytes_allocated: &mut usize) {
191191
let _guard = DropGuard::new();
192192
for node in finalized.into_iter().rev() {
193-
if (**node.this).header.marked.get() {
193+
if (*node.this.as_ptr()).header.marked.get() {
194194
continue;
195195
}
196196
let incoming = node.incoming;
197-
let mut node = Box::from_raw(*node.this);
197+
let mut node = Box::from_raw(node.this.as_ptr());
198198
*bytes_allocated -= mem::size_of_val::<GcBox<_>>(&*node);
199199
*incoming = node.header.next.take();
200200
}
@@ -206,7 +206,7 @@ fn collect_garbage(st: &mut GcState) {
206206
return;
207207
}
208208
for node in &unmarked {
209-
Trace::finalize_glue(&(**node.this).data);
209+
Trace::finalize_glue(&(*node.this.as_ptr()).data);
210210
}
211211
mark(&mut st.boxes_start);
212212
sweep(unmarked, &mut st.bytes_allocated);

gc/src/lib.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,9 @@ impl<T: Trace> Gc<T> {
8383

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

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

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

130-
unsafe { &**clear_root_bit(self.ptr_root.get()) }
130+
unsafe { &*clear_root_bit(self.ptr_root.get()).get() }
131131
}
132132
}
133133

gc/src/stable.rs

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,14 @@ pub struct NonZero<T> {
1111
p: T,
1212
}
1313

14-
impl<T> Deref for NonZero<T> {
15-
type Target = T;
16-
fn deref(&self) -> &T {
17-
&self.p
18-
}
19-
}
20-
2114
impl<T> NonZero<T> {
2215
pub unsafe fn new(p: T) -> NonZero<T> {
2316
NonZero { p: p }
2417
}
18+
19+
pub fn get(self) -> T {
20+
self.p
21+
}
2522
}
2623

2724
/// See `::std::prt::Shared`
@@ -37,12 +34,9 @@ impl<T: ?Sized> Shared<T> {
3734
_pd: PhantomData,
3835
}
3936
}
40-
}
4137

42-
impl<T: ?Sized> Deref for Shared<T> {
43-
type Target = *mut T;
44-
fn deref(&self) -> &*mut T {
45-
&self.p
38+
pub fn as_ptr(&self) -> *mut T {
39+
self.p.get()
4640
}
4741
}
4842

gc_derive/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "gc_derive"
3-
version = "0.2.1"
3+
version = "0.3.0"
44
authors = ["Manish Goregaokar <[email protected]>", "Michael Layzell <[email protected]>"]
55

66
description = "Garbage collector derive plugin for rust-gc"

0 commit comments

Comments
 (0)