-
Notifications
You must be signed in to change notification settings - Fork 50
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add raw pointer interface #71
Conversation
Currently, I am unsure of how to implement
pub struct Rc<T: ?Sized> {
ptr: NonNull<RcBox<T>>,
phantom: PhantomData<T>,
}
pub struct Gc<T: Trace + ?Sized + 'static> {
ptr_root: Cell<NonNull<GcBox<T>>>,
marker: PhantomData<Rc<T>>,
} There is an additional Also, when attempting to copy over the code (with the appropriate changes) into |
Nope,
The standard library can use nightly-only features in stable code due to being part of the distribution. There are actually quite a few features which the stdlib uses which aren't available to the wider ecosystem. The reason for that logic is to determine how to offset the pointer back from a fn padding_needed_for_gc(align: usize) -> usize {
assert!(align.count_ones() == 1, "align must be a power-of-two!");
// Determine how much our header is misaligned for `align`.
// `align - 1` is a mask of all bits less than the alignment,
// as `align` is a power-of-two.
let misaligned = mem::size_of::<GcBox<()>>() & (align - 1);
if misaligned > 0 { align - misaligned } else { 0 }
} |
Oops, made a careless mistake with the doctest there 😅(821c1e2). I have implemented |
gc/src/lib.rs
Outdated
/// [`Gc::into_raw`][into_raw]. | ||
/// | ||
/// This function is unsafe because improper use may lead to memory | ||
/// problems. For example, a double-free may occur if the function is called |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't know if a double-free would necessarily occur. The more likely problem would be a use-after-free.
gc/src/lib.rs
Outdated
/// // Further calls to `Gc::from_raw(x_ptr)` would be memory unsafe. | ||
/// } | ||
/// | ||
/// // The memory was freed when `x` went out of scope above, so `x_ptr` is now dangling! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Given that this is a Gc<T>
, that is not true :-). It won't be freed until after the collector is run. It should instead be said that the memory may be freed any time after x
went out of scope above, so x_ptr
may now be dangling.
Have corrected the documentation. |
Thanks! |
Gc { | ||
ptr_root: Cell::new(NonNull::new_unchecked(rc_ptr)), | ||
marker: PhantomData, | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This Gc
needs to be set as a root (#122).
Closes #62. Aims to implement these methods:
which are analogous to
Rc::into_raw
,Rc::from_raw
.Mostly just following the code from
alloc/rc.rs
.