Foundation
Loading...
Searching...
No Matches
Classes | Public Member Functions | Private Member Functions | Private Attributes | List of all members
Foundation::Core::AtomicPool< T > Class Template Reference

Atomic, bounded object pool of fixed allocation sizes. Being a sibling to AtomicStack - key differences being how objects are allocated in a fixed-size arena and is never freed back to the system. Deallocation returns objects to the pool for reuse. That - and you get stable pointers to objects directly. More...

#include <AtomicPool.hpp>

Classes

struct  Node
 
struct  PTag
 

Public Member Functions

 AtomicPool ()=default
 
 AtomicPool (size_t size, Allocator *alloc)
 
template<typename... Args>
T * Construct (Args &&... args)
 Constructs an object of type T in the pool with the given arguments.
 
void Destruct (T *ptr)
 Destructs the object pointed to by ptr and returns it to the pool.
 
size_t Index (T *ptr) const
 Returns the index of the given pointer in the pool.
 
TAt (size_t index)
 Returns the pointer at the given index in the pool.
 
void Collect ()
 Destruct all allocated objects in the pool, collecting garbage.
 
 ~AtomicPool ()
 

Private Member Functions

NodeAllocateNode ()
 
void DeallocateNode (Node *node)
 

Private Attributes

NodemNodes {nullptr}
 
size_t mSize {0}
 
Atomic< PTagmHead {}
 
ScopedArena mArena
 

Detailed Description

template<typename T>
class Foundation::Core::AtomicPool< T >

Atomic, bounded object pool of fixed allocation sizes. Being a sibling to AtomicStack - key differences being how objects are allocated in a fixed-size arena and is never freed back to the system. Deallocation returns objects to the pool for reuse. That - and you get stable pointers to objects directly.

Note
This implementation is thread-safe for allocations from multiple threads.
Template Parameters
TType of object to allocate, must be default constructible.

Constructor & Destructor Documentation

◆ AtomicPool() [1/2]

template<typename T >
Foundation::Core::AtomicPool< T >::AtomicPool ( )
default

◆ AtomicPool() [2/2]

template<typename T >
Foundation::Core::AtomicPool< T >::AtomicPool ( size_t  size,
Allocator alloc 
)
inline

◆ ~AtomicPool()

template<typename T >
Foundation::Core::AtomicPool< T >::~AtomicPool ( )
inline

Member Function Documentation

◆ AllocateNode()

template<typename T >
Node * Foundation::Core::AtomicPool< T >::AllocateNode ( )
inlineprivate

◆ At()

template<typename T >
T * Foundation::Core::AtomicPool< T >::At ( size_t  index)
inline

Returns the pointer at the given index in the pool.

Returns
Pointer to object of type T, or nullptr if index is out of bounds.

◆ Collect()

template<typename T >
void Foundation::Core::AtomicPool< T >::Collect ( )
inline

Destruct all allocated objects in the pool, collecting garbage.

◆ Construct()

template<typename T >
template<typename... Args>
T * Foundation::Core::AtomicPool< T >::Construct ( Args &&...  args)
inline

Constructs an object of type T in the pool with the given arguments.

Returns
Pointer to the constructed object, or nullptr if the pool is exhausted.
Note
User NOT use delete to free the returned pointer as it is managed by the pool. The destructor of T should NOT be manually called - handle both with Deallocate.

◆ DeallocateNode()

template<typename T >
void Foundation::Core::AtomicPool< T >::DeallocateNode ( Node node)
inlineprivate

◆ Destruct()

template<typename T >
void Foundation::Core::AtomicPool< T >::Destruct ( T ptr)
inline

Destructs the object pointed to by ptr and returns it to the pool.

◆ Index()

template<typename T >
size_t Foundation::Core::AtomicPool< T >::Index ( T ptr) const
inline

Returns the index of the given pointer in the pool.

Returns
Index of range [0, mSize). -1

Member Data Documentation

◆ mArena

template<typename T >
ScopedArena Foundation::Core::AtomicPool< T >::mArena
private

◆ mHead

template<typename T >
Atomic<PTag> Foundation::Core::AtomicPool< T >::mHead {}
private

◆ mNodes

template<typename T >
Node* Foundation::Core::AtomicPool< T >::mNodes {nullptr}
private

◆ mSize

template<typename T >
size_t Foundation::Core::AtomicPool< T >::mSize {0}
private

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