Runtime Support Library is a set of utility C++ classes.
- Microsoft Visual Studio 15 Update 3
- GCC 4.9
This is header only library. Just add rsl/include directory to include paths of your project.
- make a directory to build
mkdir build
cd build
- build tests
cmake "path_to_rsl/rsl/tests"
make
or
cmake -G "Visual Studio 14 Win64" "path_to_rsl/rsl/tests"
open rsl_tests.sln file and build solution
- run tests
./rsl_tests
shared_ptr and unique_ptr classes solved memory managements issues in C++. But we still don't have any utility class to address dangling pointers problem. As many C++ researchers mentioned such class cannot be implemented with zero overhead principle. tsl::track::pointer class is an attemp to make such class with the minimal overhead.
To catch pointer's dangling we need two classes:
- rsl::track::trackable - a class that tracks lifetime of the object or resource.
- rsl::track::pointer - a smart pointer class that will be got notified when corresponding trackable class is going to destroy.
The current implementation is not threadsafe and has the following memory overhead:
static_assert(sizeof(rsl::track::trackable) == sizeof(void*));
static_assert(sizeof(rsl::track::pointer<int>) == 4 * sizeof(void*));
rsl::track::trackable holds only one value - a pointer to a head of a chain of rsl::track::pointer instances. Thus rsl::track::pointer class is a node of the two-directional list. It has pointers to previous and next nodes, pointer to the object it referencing to and one pointer used because rsl::track::pointer has virtual mathods to hide different dangling policies.
To use track pointers for some object A you should have rsl::track::trackable instance that lives the same period of time as the object A. You could achieve this by making rsl::track::trackable part of A or combining A and rsl::track::trackable in larger object.
using namespace rsl::track;
struct A
{
int value;
trackable life_time;
};
A a;
pointer<A> p1(&a, a.life_time);
pointer<int> p2(&a.value, a.life_time);
using namespace rsl::track;
struct A
{
int value;
};
std::pair<A, trackable> a;
pointer<A> p1(&a.first, a.second);
pointer<int> p2(&a.first.value, a.second);
Also rsl library has rsl::track::allocator class that make it easy to use track pointers with standard containers (see tests as examples).
rsl::track::vector<int> v{0, 1, 2};
rsl::track::pointer<int> p1(&v[1], v.get_allocator().get_trackable());
// compact version
auto p2 = get_ptr_at(v, 1)