DGC is a c++17 deterministic garbage collection library.
Version: 1.0.
In this context, deterministic garbage collection refers to the following:
Objects are deleted as soon as they are inaccessible, including objects in cycles.
The library only has include files, in the folder 'include'.
If you are interested in using it, please copy and paste the files into your own project.
#include "dgc.hpp"
template <class T> class root_ptrtemplate <class T> class member_ptrobject
The root pointer class is used for:
- global variables
- stack variables
It represents instances of pointers for the root pointer set.
The member pointer class is used for object members.
Objects that wish to be garbage-collected must inherit from class object.
struct Node : public dgc::object {
gdc::member_ptr<Node> left;
gdc::member_ptr<Node> right;
Node() : left(this), right(this) {}
};
gdc::root_ptr<Node> root = new Node();
root->left = new Node();
root->right = new Node();An object instance maintains a list of root/member pointers pointing to it.
Member pointers have an owner property.
Root pointers do not have an owner property.
When a pointer is removed from the set of pointers that point to an object, the graph is scanned backwards, from leaf pointers to the root set, starting from the remaining pointers of that object.
If a pointer has an owner, then the pointers of the owner object are scanned.
If a root pointer is found, then the object is not deleted.
If no root pointers are found, then the object is deleted.
The library is NOT thread safe.
Pointers in STL collections shall be root pointers.
When the object that has the STL collection is deleted, the root pointers in the collection will be deleted, and thus the objects they point to will be deleted if there are no longer accessible from the root set.