Skip to content

Commit d0686d1

Browse files
committed
reduce dupliaction between mark_sweep and mark_sweep_arena2
1 parent cc7df09 commit d0686d1

File tree

13 files changed

+58
-1351
lines changed

13 files changed

+58
-1351
lines changed

oscars/src/alloc/arena2/alloc.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,15 @@ impl<'arena> ErasedArenaPointer<'arena> {
133133
self.0.as_ptr()
134134
}
135135

136+
/// Extend the lifetime of this erased arena pointer to 'static
137+
///
138+
/// SAFETY:
139+
///
140+
/// safe because the gc collector owns the arena and keeps it alive
141+
pub(crate) unsafe fn extend_lifetime(self) -> ErasedArenaPointer<'static> {
142+
ErasedArenaPointer(self.0, PhantomData)
143+
}
144+
136145
/// Returns an [`ArenaPointer`] for the current [`ErasedArenaPointer`]
137146
///
138147
/// # Safety
@@ -178,6 +187,15 @@ impl<'arena, T> ArenaPointer<'arena, T> {
178187
pub fn to_erased(self) -> ErasedArenaPointer<'arena> {
179188
self.0
180189
}
190+
191+
/// extend the lifetime of this arena pointer to 'static
192+
///
193+
/// SAFETY:
194+
///
195+
/// safe because the gc collector owns the arena and keeps it alive
196+
pub(crate) unsafe fn extend_lifetime(self) -> ArenaPointer<'static, T> {
197+
ArenaPointer(unsafe { self.0.extend_lifetime() }, PhantomData)
198+
}
181199
}
182200

183201
const FULL_MASK: u8 = 0b0100_0000;

oscars/src/collectors/mark_sweep/cell.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,14 @@ impl<T: ?Sized> GcRefCell<T> {
171171
}
172172
}
173173

174+
// returns a raw pointer to the inner value or `None` if currently mutably borrowed
175+
pub(crate) fn get_raw(&self) -> Option<*mut T> {
176+
match self.borrow.get().borrowed() {
177+
BorrowState::Writing => None,
178+
_ => Some(self.cell.get()),
179+
}
180+
}
181+
174182
/// Mutably borrows the wrapped value, returning an error if the value is currently borrowed.
175183
///
176184
/// The borrow lasts until the returned `GcCellRefMut` exits scope.

oscars/src/collectors/mark_sweep/internals/gc_box.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,10 @@ impl<T: Trace + Finalize + ?Sized> WeakGcBox<T> {
4545

4646
pub(crate) fn erased_inner_ptr(&self) -> NonNull<GcBox<NonTraceable>> {
4747
use crate::alloc::arena3::ArenaHeapItem;
48-
// SAFETY: `ArenaHeapItem` is `repr(transparent)`, use addr_of_mut! to avoid
48+
// SAFETY: `ArenaHeapItem` is `repr(transparent)`, use `&raw mut` to avoid
4949
// creating a &mut reference during trace
5050
let raw: *mut ArenaHeapItem<GcBox<NonTraceable>> = self.as_heap_ptr().as_ptr();
51-
unsafe { NonNull::new_unchecked(core::ptr::addr_of_mut!((*raw).0)) }
51+
unsafe { NonNull::new_unchecked(&raw mut (*raw).0) }
5252
}
5353

5454
pub(crate) fn as_heap_ptr(&self) -> NonNull<ArenaHeapItem<GcBox<NonTraceable>>> {

oscars/src/collectors/mark_sweep/internals/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ mod gc_header;
44
mod vtable;
55

66
pub(crate) use ephemeron::Ephemeron;
7+
pub(crate) use gc_header::{GcHeader, HeaderColor, HeaderFlags};
78
pub(crate) use vtable::{DropFn, TraceFn, VTable, vtable_of};
89

910
pub use self::gc_box::{GcBox, NonTraceable, WeakGcBox};

oscars/src/collectors/mark_sweep/pointers/gc.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,12 @@ impl<T: Trace> Gc<T> {
4444

4545
impl<T: Trace + ?Sized> Gc<T> {
4646
pub(crate) fn as_sized_inner_ptr(&self) -> NonNull<GcBox<NonTraceable>> {
47-
// SAFETY: use `addr_of_mut!` to get a raw pointer without creating
47+
// SAFETY: use `&raw mut` to get a raw pointer without creating
4848
// a `&mut` reference, avoiding Stacked Borrows UB during GC tracing
4949
let raw: *mut ArenaHeapItem<GcBox<NonTraceable>> = self.as_heap_ptr().as_ptr();
5050
// SAFETY: `raw` is non-null because it comes from `as_heap_ptr()`
5151
// `ArenaHeapItem` is `#[repr(transparent)]` so it shares the same address as field 0
52-
unsafe { NonNull::new_unchecked(core::ptr::addr_of_mut!((*raw).0)) }
52+
unsafe { NonNull::new_unchecked(&raw mut (*raw).0) }
5353
}
5454

5555
pub(crate) fn as_heap_ptr(&self) -> NonNull<ArenaHeapItem<GcBox<NonTraceable>>> {

0 commit comments

Comments
 (0)