Skip to content

Instantly share code, notes, and snippets.

@speed1313
Created March 2, 2023 04:20
Show Gist options
  • Save speed1313/dd0e805e2ab9a8edc53be984c2ae2f1f to your computer and use it in GitHub Desktop.
Save speed1313/dd0e805e2ab9a8edc53be984c2ae2f1f to your computer and use it in GitHub Desktop.
seccamp_forum_2023_demo
[package]
name = "demo"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
gomicollector = "0.1.1"
use gomicollector::Heap;
fn main() {
let heap_size = 3;
let mut heap = Heap::<String>::new(heap_size);
// allocate an object
let obj1_id = heap.allocate("obj1".to_string());
let obj1 = heap.get(obj1_id.unwrap());
println!("obj1 allocated {:?}", obj1);
// root -> obj1
heap.root_set.insert(obj1_id.unwrap());
// allocate obj2 which is unreachable from the root set
let obj2_id = heap.allocate("obj2".to_string());
let obj2 = heap.get(obj2_id.unwrap());
println!("obj2 allocated: {:?}", obj2);
let obj3_id = heap.allocate("Obj3".to_string());
let obj3 = heap.get(obj3_id.unwrap());
println!("obj3 allocated: {:?}", obj3);
// root -> obj1 -> obj2
heap.heap[obj1_id.unwrap()].set_head(obj2_id);
// allocate obj4 and check gc collects obj3 memory
println!("obj4 will be allocated");
let _ = heap.allocate(format!("obj4"));
// ojb1 and obj3 is still in the heap because the root points to it.
println!("heap: {:#?}", heap);
println!(
"reachable set: {:?}",
heap.reachable_set()
.iter()
.map(|id| heap.get_data(*id).clone())
.collect::<Vec<String>>()
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment