Crates.io | jrsonnet-gcmodule |
lib.rs | jrsonnet-gcmodule |
version | |
source | src |
created_at | 2022-06-05 11:05:09.7326 |
updated_at | 2024-12-02 20:40:00.883242 |
description | Cyclic garbage collection inspired by CPython's gc implementation. |
homepage | |
repository | https://github.com/CertainLach/gcmodule |
max_upload_size | |
id | 600151 |
Cargo.toml error: | TOML parse error at line 21, column 1 | 21 | autolib = false | ^^^^^^^ unknown field `autolib`, expected one of `name`, `version`, `edition`, `authors`, `description`, `readme`, `license`, `repository`, `homepage`, `documentation`, `build`, `resolver`, `links`, `default-run`, `default_dash_run`, `rust-version`, `rust_dash_version`, `rust_version`, `license-file`, `license_dash_file`, `license_file`, `licenseFile`, `license_capital_file`, `forced-target`, `forced_dash_target`, `autobins`, `autotests`, `autoexamples`, `autobenches`, `publish`, `metadata`, `keywords`, `categories`, `exclude`, `include` |
size | 0 |
This is fork of https://github.com/quark-zju/gcmodule package, which implements several features for usage in jrsonnet
Garbage collection inspired by CPython's implementation.
This library provides a type Cc<T>
. It provides shared reference-counting pointer, similar to stdlib Rc<T>
. Unlike Rc
, reference cycles in Cc
can be collected.
If all values can be freed by just reference-counting, the collector used by this library does not take extra memory. This is different from some other implementations, which require manual collection to free the extra memory used by the collector.
use gcmodule::{Cc, Trace};
use std::cell::RefCell;
type List = Cc<RefCell<Vec<Box<dyn Trace>>>>;
let a: List = Default::default();
let b: List = Default::default();
a.borrow_mut().push(Box::new(b.clone()));
b.borrow_mut().push(Box::new(a.clone())); // Form a cycle.
drop(a);
drop(b); // Internal values are not dropped due to the cycle.
gcmodule::collect_thread_cycles(); // Internal values are dropped.
For customized structures, they need to implement the Trace
interface. That can be done by #[derive(Trace)]
.
use gcmodule::{Cc, Trace};
use std::cell::RefCell;
#[derive(Trace, Default)]
struct List(RefCell<Vec<Box<dyn Trace>>>);
{
let a: List = Default::default();
let b: List = Default::default();
a.borrow_mut().push(Box::new(b.clone()));
b.borrow_mut().push(Box::new(a.clone()));
}
assert_eq!(gcmodule::collect_thread_cycles(), 2); // 2 values are collected.
Refer to the documentation for more examples and technical details.
Cc<T>
and Trace
are similar, or even compatible.gcmodule
is conceptually simpler. There is no need for the "colors" concept.gcmodule
provides ThreadedCc<T>
for multi-thread environment.bacon-rajan-cc
requires manual collection to release GC metadata (but not the tracked object) even if the reference count logically drops to 0. See this commit message for some details.rcgc
takes a novel approach - the collector holds strong references while everywhere else uses weak references.rcgc
requires manual collection to release actual objects even if the reference count of objects (logically) drops to 0.