Skip to content

Commit 1ada80e

Browse files
committed
Merge pull request #57 from kevincox/new-shared
Update for new Shared and NonZero API.
2 parents d8252ae + 7bfdcb8 commit 1ada80e

File tree

5 files changed

+34
-41
lines changed

5 files changed

+34
-41
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: 14 additions & 14 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
}
@@ -111,13 +111,13 @@ impl<T: Trace> GcBox<T> {
111111
data: value,
112112
}));
113113

114-
st.boxes_start = Some(unsafe { Shared::new(gcbox) });
114+
st.boxes_start = Some(unsafe { Shared::new_unchecked(gcbox) });
115115

116116
// We allocated some bytes! Let's record it
117117
st.bytes_allocated += mem::size_of::<GcBox<T>>();
118118

119119
// Return the pointer to the newly allocated data
120-
unsafe { Shared::new(gcbox) }
120+
unsafe { Shared::new_unchecked(gcbox) }
121121
})
122122
}
123123
}
@@ -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: 8 additions & 8 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_unchecked(ptr.as_ptr())),
8989
marker: PhantomData,
9090
};
9191
gc.set_root();
@@ -97,20 +97,20 @@ 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;
102-
NonZero::new(ptr)
102+
NonZero::new_unchecked(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;
113-
self.ptr_root.set(NonZero::new(ptr));
113+
self.ptr_root.set(NonZero::new_unchecked(ptr));
114114
}
115115

116116
unsafe fn clear_root(&self) {
@@ -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: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
//! Shared, such that the same code can be used between the nightly and stable
33
//! versions of rust-gc.
44
5-
use std::ops::Deref;
65
use std::marker::PhantomData;
76

87
/// See `::core::nonzero::NonZero`
@@ -11,17 +10,14 @@ pub struct NonZero<T> {
1110
p: T,
1211
}
1312

14-
impl<T> Deref for NonZero<T> {
15-
type Target = T;
16-
fn deref(&self) -> &T {
17-
&self.p
18-
}
19-
}
20-
2113
impl<T> NonZero<T> {
22-
pub unsafe fn new(p: T) -> NonZero<T> {
14+
pub unsafe fn new_unchecked(p: T) -> NonZero<T> {
2315
NonZero { p: p }
2416
}
17+
18+
pub fn get(self) -> T {
19+
self.p
20+
}
2521
}
2622

2723
/// See `::std::prt::Shared`
@@ -31,18 +27,15 @@ pub struct Shared<T: ?Sized> {
3127
}
3228

3329
impl<T: ?Sized> Shared<T> {
34-
pub unsafe fn new(p: *mut T) -> Self {
30+
pub unsafe fn new_unchecked(p: *mut T) -> Self {
3531
Shared {
36-
p: NonZero::new(p),
32+
p: NonZero::new_unchecked(p),
3733
_pd: PhantomData,
3834
}
3935
}
40-
}
4136

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

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)