UraniumCompute 0.1.0
A GPU accelerated parallel task scheduler
UN::ReferenceCounter Class Referencefinal

The reference counter that holds the number of references to the object. More...

#include <ReferenceCounter.h>

Public Member Functions

 ReferenceCounter (IAllocator *pAllocator)
 Create a new reference counter with specified allocator. More...
 
UInt32 AddStrongRef ()
 Add a strong reference to the counter. More...
 
template<class F >
UInt32 ReleaseStrongRef (F &&destroyCallback)
 Remove a strong reference from the counter. More...
 
UInt32 GetStrongRefCount () const
 Get number of strong references.
 

Detailed Description

The reference counter that holds the number of references to the object.

This class holds number of references to the object and a pointer to the allocator that was used to allocate this object. It assumes the following memory layout:

+------------------+ <--- this pointer
--------------------
| Object |
+------------------+
Base class for dynamic reference counted objects.
Definition: Object.h:51
The reference counter that holds the number of references to the object.
Definition: ReferenceCounter.h:41

It will delete this assuming that a single block was used to allocate the object and the counter.
Example (pseudo-code):

class MyObject : Object<IObject> {};
ReferenceCounter* rc = malloc(sizeof(ReferenceCounter) + sizeof(MyObject));
MyObject* obj = rc + sizeof(ReferenceCounter);
// ...
free(rc); // frees memory of both the counter and the object.
ReferenceCounter(IAllocator *pAllocator)
Create a new reference counter with specified allocator.
Definition: ReferenceCounter.h:52

This layout is used for better locality and performance: it groups two allocations into one. The internal reference counting system also supports copying shared pointers using their raw pointers:

Ptr<MyObject> pObj1 = AllocateObject<MyObject>(); // refcount = 1
Ptr<MyObject> pObj2 = pObj1; // refcount = 2 <-- Valid for std::shared_ptr too
Ptr<MyObject> pObj3 = pObj1.Get(); // refcount = 3 <-- Also valid here!
Shared smart pointer implementation that uses reference counting.
Definition: Ptr.h:44
T * Get() const
Get underlying raw pointer.
Definition: Ptr.h:219

Constructor & Destructor Documentation

◆ ReferenceCounter()

UN::ReferenceCounter::ReferenceCounter ( IAllocator pAllocator)
inlineexplicit

Create a new reference counter with specified allocator.

The specified allocator will be used to free memory after the counter reaches zero. This constructor initializes the counter to zero.

Parameters
pAllocator- The allocator to use to free memory.

Member Function Documentation

◆ AddStrongRef()

UInt32 UN::ReferenceCounter::AddStrongRef ( )
inline

Add a strong reference to the counter.

Returns
The new (incremented) number of strong references.

◆ ReleaseStrongRef()

template<class F >
UInt32 UN::ReferenceCounter::ReleaseStrongRef ( F &&  destroyCallback)
inline

Remove a strong reference from the counter.

This function will delete the counter itself if number of references reaches zero.

Parameters
destroyCallback- A function to invoke before deallocation if the counter reached zero. This is typically a lambda that calls object destructor.
Template Parameters
F- Type of callback function.
Returns
The new (decremented) number of strong references.

The documentation for this class was generated from the following file: