Skip to content
This repository has been archived by the owner on Oct 18, 2022. It is now read-only.
/ rcgc Public archive

A reference-counting, tracing garbage collector in completely safe Rust

License

Notifications You must be signed in to change notification settings

jonas-schievink/rcgc

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

A garbage collector using Rc/Weak

crates.io docs.rs Build Status

This crate implements a pretty simple garbage collector. Its selling point is that it uses no unsafe at all: Instead, it represents references between objects as Weak pointers that are upgraded to Rcs on access.

The GC holds an Rc to every object on its heap and can hand out further Rcs acting as roots. It can then locate rooted objects and perform a tracing garbage collection to free objects with no incoming references (even in the presence of cycles). It also uses the underlying reference counters as a faster way to locate garbage objects: If an object has a weak count of 0 and a strong count of 1, it is only reachable via the GCs own Rc, not via any object reference, and can be freed immediately without having to perform a tracing phase.

If there's a bug in the GC, and an object ends up getting collected while it's still reachable, any access to the object will fail to upgrade the Weak pointer and panic instead of becoming an unsafe use-after-free.

Please refer to the changelog to see what changed in the last releases.

Usage

Start by adding an entry to your Cargo.toml:

[dependencies]
rcgc = "0.1.0"

Then import the crate into your Rust code:

extern crate rcgc;