Implicitly redirect cmdhelp includes to corresponding JSON files
[lsnes.git] / src / library / gc.cpp
blob8538badf62fd6a9ba2aae03e605b5418186a98b0
1 #include "gc.hpp"
2 #include <list>
3 #include <iostream>
4 #include <set>
6 namespace GC
8 namespace
10 std::set<item*>* gc_items;
13 item::item()
15 if(!gc_items) gc_items = new std::set<item*>;
16 gc_items->insert(this);
17 root_count = 1;
20 item::~item()
22 gc_items->erase(this);
25 void item::mark_root()
27 root_count++;
30 void item::unmark_root()
32 if(root_count) root_count--;
35 void item::do_gc()
37 if(!gc_items) return;
38 for(auto i : *gc_items)
39 i->reachable = false;
40 for(auto i : *gc_items) {
41 if(i->root_count) {
42 i->mark();
45 for(auto i = gc_items->begin(); i != gc_items->end();) {
46 if(!(*i)->reachable) {
47 auto ptr = i;
48 i++;
49 delete *ptr;
50 } else
51 i++;
55 void item::mark()
57 bool was_reachable = reachable;
58 reachable = true;
59 if(!was_reachable) {
60 trace();