Skip to content

axilmar/dgc

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Deterministic Garbage Collection (DGC)

DGC is a c++17 deterministic garbage collection library.

Version: 1.0.

'Deterministic garbage collection' definition

In this context, deterministic garbage collection refers to the following:

Objects are deleted as soon as they are inaccessible, including objects in cycles.

Include files

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.

Master header

#include "dgc.hpp"

Classes

  1. template <class T> class root_ptr
  2. template <class T> class member_ptr
  3. object

The root_ptr class

The root pointer class is used for:

  1. global variables
  2. stack variables

It represents instances of pointers for the root pointer set.

The member_ptr class

The member pointer class is used for object members.

The object class

Objects that wish to be garbage-collected must inherit from class object.

Example

	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();

How it works

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.

Thread safety

The library is NOT thread safe.

Pointers in STL collections

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.

About

Deterministic gc for c++17.

Resources

Stars

Watchers

Forks

Releases

Packages

Contributors

Languages