-
-
Notifications
You must be signed in to change notification settings - Fork 400
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Complete initial layout and refactor
- Loading branch information
Showing
5 changed files
with
141 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,9 @@ | ||
//! Pointers represents the External types returned by the Boa Garbage Collector | ||
|
||
pub mod gc_ptr; | ||
pub mod weak_ptr; | ||
mod gc_ptr; | ||
mod weak_pair; | ||
mod weak_ptr; | ||
|
||
pub use gc_ptr::Gc; | ||
pub use weak_pair::WeakPair; | ||
pub use weak_ptr::WeakGc; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
use crate::{ | ||
finalizer_safe, | ||
internals::Ephemeron, | ||
trace::{Finalize, Trace}, | ||
GcBox, EPHEMERON_QUEUE, | ||
}; | ||
use std::cell::Cell; | ||
use std::ptr::NonNull; | ||
|
||
pub struct WeakPair<K: Trace + ?Sized + 'static, V: Trace + ?Sized, 'static> { | ||
inner_ptr: Cell<NonNull<GcBox<Ephemeron<K, V>>>>, | ||
} | ||
|
||
impl<K: Trace, V: Trace> WeakPair<K, V> { | ||
pub fn new(value: NonNull<GcBox<Ephemeron<K, V>>>) -> Self { | ||
unsafe { | ||
Self { | ||
inner_ptr: Cell::new(NonNull::new_unchecked(value.as_ptr())), | ||
} | ||
} | ||
} | ||
} | ||
|
||
impl<K: Trace + ?Sized, V: Trace> WeakPair<K, V> { | ||
#[inline] | ||
fn inner_ptr(&self) -> *mut GcBox<Ephemeron<K, V>> { | ||
assert!(finalizer_safe()); | ||
|
||
unsafe { self.inner_ptr.get().as_ptr() } | ||
} | ||
|
||
#[inline] | ||
pub fn inner(&self) -> &GcBox<Ephemeron<K, V>> { | ||
unsafe { &*self.inner_ptr() } | ||
} | ||
|
||
#[inline] | ||
pub fn key(&self) -> Option<&K> { | ||
self.inner().key() | ||
} | ||
|
||
#[inline] | ||
pub fn value(&self) -> &V { | ||
self.inner().value() | ||
} | ||
} | ||
|
||
impl<K: Trace, V:Trace> Finalize for WeakPair<K, V> {} | ||
|
||
unsafe impl<K: Trace, V: Trace> Trace for WeakPair<K, V> { | ||
#[inline] | ||
unsafe fn trace(&self) {} | ||
|
||
#[inline] | ||
unsafe fn is_marked_ephemeron(&self) -> bool { | ||
false | ||
} | ||
|
||
#[inline] | ||
unsafe fn weak_trace(&self) { | ||
EPHEMERON_QUEUE.with(|q| { | ||
let mut queue = q.take().expect("queue is initialized by weak_trace"); | ||
queue.push(NonNull::new_unchecked(self.inner_ptr())) | ||
}) | ||
} | ||
|
||
#[inline] | ||
unsafe fn root(&self) {} | ||
|
||
#[inline] | ||
unsafe fn unroot(&self) {} | ||
|
||
#[inline] | ||
fn run_finalizer(&self) { | ||
Finalize::finalize(self) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters