ECSM v0.2.0
LinearPool< T, DestroyItems >

Item array with linear memory block. More...

#include <linear-pool.hpp>

Classes

struct  ConstantIterator
 Linear pool constant iterator class. More...
 
struct  Iterator
 Linear pool iterator class. More...
 

Public Member Functions

 LinearPool ()
 Creates a new empty linear pool.
 
 ~LinearPool ()
 Destroys linear pool.
 
template<typename... Args>
ID< T > create (Args &&... args)
 Creates a new item in the pool.
 
void destroy (ID< T > instance)
 Destroys linear pool item.
 
View< T > get (ID< T > instance) const noexcept
 Returns View of the item in the linear pool.
 
View< T > get (const Ref< T > &instance) const noexcept
 Returns View of the item in the linear pool.
 
ID< T > getID (const T *instance) const noexcept
 Returns ID of the item pointer.
 
T * getData () noexcept
 Returns linear pool item memory block.
 
const T * getData () const noexcept
 Returns linear pool constant item memory block.
 
uint32_t getCount () const noexcept
 Returns current created item count.
 
uint32_t getOccupancy () const noexcept
 Returns linear memory used item slots count.
 
void clear (bool destroyItems=DestroyItems)
 Destroys all items in the linear pool.
 
void dispose ()
 Actually destroys items.
 
Iterator begin () noexcept
 Returns an iterator pointing to the first element in the items array.
 
Iterator end () noexcept
 Returns an iterator pointing to the past-the-end element in the items array.
 
ConstantIterator begin () const noexcept
 Returns a constant iterator pointing to the first element in the items array.
 
ConstantIterator end () const noexcept
 Returns a constant iterator pointing to the past-the-end element in the items array.
 

Detailed Description

template<class T, bool DestroyItems>
class ecsm::LinearPool< T, DestroyItems >

Item array with linear memory block.

In a linear pool, a fixed-size block of memory is pre-allocated, and individual items or objects are then allocated from this pool. The linear allocation strategy means that these items are placed sequentially in memory, which can improve cache locality. Cache locality refers to the tendency of accessing nearby memory locations at the same time, which can result in better performance due to the way modern computer architectures use caches.

Template Parameters
Ttype of the item in the linear pool
DestroyItemslinear pool should call destroy() function of the items

Constructor & Destructor Documentation

◆ LinearPool()

template<class T , bool DestroyItems>
LinearPool ( )
inline

Creates a new empty linear pool.

It pre-allocates items array.

◆ ~LinearPool()

template<class T , bool DestroyItems>
~LinearPool ( )
inline

Destroys linear pool.

It destroys all items and deallocates array memory.

Member Function Documentation

◆ create()

template<class T , bool DestroyItems>
template<typename... Args>
ID< T > create ( Args &&... args)
inline

Creates a new item in the pool.

Reallocates linear memory block or reuses free item slots.

Warning
This function can reallocate items memory and invalidate all previous View.
Parameters
argsadditional item arguments or empty
Returns
A new item identifier in the linear pool.

◆ destroy()

template<class T , bool DestroyItems>
void destroy ( ID< T > instance)
inline

Destroys linear pool item.

It puts items to the garbage array, and destroys them after dispose() call.

Parameters
instanceitem identifier in the pool or null
Template Parameters
Ttype of the item in the linear pool

◆ get() [1/2]

template<class T , bool DestroyItems>
View< T > get ( ID< T > instance) const
inlinenoexcept

Returns View of the item in the linear pool.

Warning
Do not store views, use them only in place. Because item memory can be reallocated later.
Parameters
instanceitem identifier in the pool
Template Parameters
Ttype of the item in the linear pool

◆ get() [2/2]

template<class T , bool DestroyItems>
View< T > get ( const Ref< T > & instance) const
inlinenoexcept

Returns View of the item in the linear pool.

Warning
Do not store views, use them only in place. Because item memory can be reallocated later.
Parameters
instanceitem identifier in the pool
Template Parameters
Ttype of the item in the linear pool

◆ getID()

template<class T , bool DestroyItems>
ID< T > getID ( const T * instance) const
inlinenoexcept

Returns ID of the item pointer.

Warning
Use with extreme caution!
Parameters
[in]instancepointer to the item data
Template Parameters
Ttype of the item in the linear pool

◆ getData() [1/2]

template<class T , bool DestroyItems>
T * getData ( )
inlinenoexcept

Returns linear pool item memory block.

Warning
It contains destroyed items too. Use custom code to detect freed items.
Template Parameters
Ttype of the item in the linear pool

◆ getData() [2/2]

template<class T , bool DestroyItems>
const T * getData ( ) const
inlinenoexcept

Returns linear pool constant item memory block.

Warning
It contains destroyed items too. Use custom code to detect freed items.
Template Parameters
Ttype of the item in the linear pool

◆ getCount()

template<class T , bool DestroyItems>
uint32_t getCount ( ) const
inlinenoexcept

Returns current created item count.

Note
This is not an allocated item count.

◆ getOccupancy()

template<class T , bool DestroyItems>
uint32_t getOccupancy ( ) const
inlinenoexcept

Returns linear memory used item slots count.

Warning
This number also contains destroyed items.

◆ clear()

template<class T , bool DestroyItems>
void clear ( bool destroyItems = DestroyItems)
inline

Destroys all items in the linear pool.

Warning
This function deallocates items memory and invalidates all previous View.
Parameters
destroyItemsshould call destroy() function of the items

◆ dispose()

template<class T , bool DestroyItems>
void dispose ( )
inline

Actually destroys items.

See the destroy(). It marks destroyed item memory as free, and can reuse it later.