pub struct JanetGc { /* private fields */ }
Expand description
The Janet Garbage Collector type.
It allows the use of garbage collection operations in the Janet public C API.
Implementations§
source§impl JanetGc
impl JanetGc
sourcepub unsafe fn collect(&self)
pub unsafe fn collect(&self)
Run the garbage collection if there is nothing locking or suspending the garbage
collector, like an active JanetGcLockGuard
or a call to a Janet C API that
locks the GC.
If there is something locking the garbage collection, it simply does a no-op.
§Safety
This function will free all memory allocated with the Janet scratch memory
API and any non-rooted object
that have no reference to a live object (as example, an empty
JanetTable
or JanetArray
)
sourcepub fn lock(&self) -> JanetGcLockGuard
pub fn lock(&self) -> JanetGcLockGuard
Lock the Janet GC and suspend any garbage collection until the guard is dropped.
If there is any attempt to manually trigger the garbage collection while there is
a JanetGcLockGuard
active (or any unsafe call to the Janet C API locking the
GC)
sourcepub fn unlock(guard: JanetGcLockGuard)
pub fn unlock(guard: JanetGcLockGuard)
Immediately drops the guard, and consequently unlocks the Janet GC.
This function is equivalent to calling drop
on the guard but is more
self-documenting. Alternately, the guard will be automatically dropped when it
goes out of scope.
§Example:
use janetrs::JanetGc;
let gc = JanetGc::obtain();
let mut guard = gc.lock();
// do stuff with the Janet GC locked
JanetGc::unlock(guard);
sourcepub fn root(&self, value: Janet) -> JanetGcRootGuard
pub fn root(&self, value: Janet) -> JanetGcRootGuard
Roots the value
to the GC. This prevents the GC from removing the value
and
all of its children in a garbage collection.
The returned guard will unroot
the value
when dropped.
sourcepub fn unroot(guard: JanetGcRootGuard)
pub fn unroot(guard: JanetGcRootGuard)
Immediately drops the guard, and consequently unlocks the Janet GC.
This function is equivalent to calling drop
on the guard but is more
self-documenting. Alternately, the guard will be automatically dropped when it
goes out of scope.