POSIX Shared Memory Data Structures 1.0
High-performance lock-free data structures for inter-process communication
Loading...
Searching...
No Matches
shm_object_pool< T, TableType > Class Template Reference

High-performance object pool for shared memory. More...

#include <shm_object_pool.h>

Public Types

using value_type = T
 
using size_type = size_t
 
using handle_type = uint32_t
 

Public Member Functions

template<typename ShmType >
 shm_object_pool (ShmType &shm, std::string_view name, size_t capacity=0)
 
handle_type acquire () noexcept
 Acquire an object from the pool.
 
template<typename... Args>
std::optional< handle_typeacquire_construct (Args &&... args)
 Acquire and construct an object.
 
void release (handle_type handle) noexcept
 Release an object back to the pool.
 
T & operator[] (handle_type handle) noexcept
 Access object by handle.
 
const T & operator[] (handle_type handle) const noexcept
 
T * get (handle_type handle) noexcept
 
const T * get (handle_type handle) const noexcept
 
bool is_valid (handle_type handle) const noexcept
 Check if handle is valid.
 
size_t capacity () const noexcept
 Get pool statistics.
 
size_t num_allocated () const noexcept
 
size_t num_available () const noexcept
 
bool empty () const noexcept
 
bool full () const noexcept
 
std::span< T > unsafe_all_objects () noexcept
 Get view of all objects (including free ones) Use with caution - includes uninitialized objects.
 
std::span< const T > unsafe_all_objects () const noexcept
 
std::string_view name () const noexcept
 
size_t acquire_batch (size_t count, handle_type *handles) noexcept
 Batch acquire multiple objects.
 
void release_batch (std::span< const handle_type > handles) noexcept
 Batch release multiple objects.
 

Static Public Attributes

static constexpr handle_type invalid_handle = NULL_INDEX
 

Detailed Description

template<typename T, typename TableType = shm_table>
requires std::is_trivially_copyable_v<T>
class shm_object_pool< T, TableType >

High-performance object pool for shared memory.

Manages a preallocated pool of objects with O(1) acquire/release. Perfect for simulations with many temporary objects (particles, entities, etc).

Uses a lock-free free list implemented as a stack for fast allocation. Objects are stored contiguously for cache efficiency.

Template Parameters
TType of objects in the pool
TableTypeMetadata table type

Definition at line 23 of file shm_object_pool.h.

Member Typedef Documentation

◆ handle_type

template<typename T , typename TableType = shm_table>
using shm_object_pool< T, TableType >::handle_type = uint32_t

Definition at line 48 of file shm_object_pool.h.

◆ size_type

template<typename T , typename TableType = shm_table>
using shm_object_pool< T, TableType >::size_type = size_t

Definition at line 47 of file shm_object_pool.h.

◆ value_type

template<typename T , typename TableType = shm_table>
using shm_object_pool< T, TableType >::value_type = T

Definition at line 46 of file shm_object_pool.h.

Constructor & Destructor Documentation

◆ shm_object_pool()

template<typename T , typename TableType = shm_table>
template<typename ShmType >
shm_object_pool< T, TableType >::shm_object_pool ( ShmType &  shm,
std::string_view  name,
size_t  capacity = 0 
)
inline

Member Function Documentation

◆ acquire()

template<typename T , typename TableType = shm_table>
handle_type shm_object_pool< T, TableType >::acquire ( )
inlinenoexcept

Acquire an object from the pool.

Returns
Handle to the acquired object, or invalid_handle if pool is full

Definition at line 115 of file shm_object_pool.h.

References shm_object_pool< T, TableType >::invalid_handle.

Referenced by shm_object_pool< T, TableType >::acquire_batch(), and shm_object_pool< T, TableType >::acquire_construct().

◆ acquire_batch()

template<typename T , typename TableType = shm_table>
size_t shm_object_pool< T, TableType >::acquire_batch ( size_t  count,
handle_type handles 
)
inlinenoexcept

Batch acquire multiple objects.

Parameters
countNumber of objects to acquire
[out]handlesArray to store acquired handles
Returns
Number of objects actually acquired

Definition at line 239 of file shm_object_pool.h.

References shm_object_pool< T, TableType >::acquire(), and shm_object_pool< T, TableType >::invalid_handle.

◆ acquire_construct()

template<typename T , typename TableType = shm_table>
template<typename... Args>
std::optional< handle_type > shm_object_pool< T, TableType >::acquire_construct ( Args &&...  args)
inline

Acquire and construct an object.

Returns
Optional containing handle if successful

Definition at line 139 of file shm_object_pool.h.

References shm_object_pool< T, TableType >::acquire(), and shm_object_pool< T, TableType >::invalid_handle.

Referenced by particle_simulation_example().

◆ capacity()

template<typename T , typename TableType = shm_table>
size_t shm_object_pool< T, TableType >::capacity ( ) const
inlinenoexcept

◆ empty()

template<typename T , typename TableType = shm_table>
bool shm_object_pool< T, TableType >::empty ( ) const
inlinenoexcept

◆ full()

template<typename T , typename TableType = shm_table>
bool shm_object_pool< T, TableType >::full ( ) const
inlinenoexcept

◆ get() [1/2]

template<typename T , typename TableType = shm_table>
const T * shm_object_pool< T, TableType >::get ( handle_type  handle) const
inlinenoexcept

Definition at line 182 of file shm_object_pool.h.

◆ get() [2/2]

template<typename T , typename TableType = shm_table>
T * shm_object_pool< T, TableType >::get ( handle_type  handle)
inlinenoexcept

Definition at line 177 of file shm_object_pool.h.

◆ is_valid()

template<typename T , typename TableType = shm_table>
bool shm_object_pool< T, TableType >::is_valid ( handle_type  handle) const
inlinenoexcept

Check if handle is valid.

Definition at line 190 of file shm_object_pool.h.

◆ name()

template<typename T , typename TableType = shm_table>
std::string_view shm_object_pool< T, TableType >::name ( ) const
inlinenoexcept

Definition at line 229 of file shm_object_pool.h.

Referenced by shm_object_pool< T, TableType >::shm_object_pool().

◆ num_allocated()

template<typename T , typename TableType = shm_table>
size_t shm_object_pool< T, TableType >::num_allocated ( ) const
inlinenoexcept

◆ num_available()

template<typename T , typename TableType = shm_table>
size_t shm_object_pool< T, TableType >::num_available ( ) const
inlinenoexcept

◆ operator[]() [1/2]

template<typename T , typename TableType = shm_table>
const T & shm_object_pool< T, TableType >::operator[] ( handle_type  handle) const
inlinenoexcept

Definition at line 173 of file shm_object_pool.h.

◆ operator[]() [2/2]

template<typename T , typename TableType = shm_table>
T & shm_object_pool< T, TableType >::operator[] ( handle_type  handle)
inlinenoexcept

Access object by handle.

Definition at line 169 of file shm_object_pool.h.

◆ release()

template<typename T , typename TableType = shm_table>
void shm_object_pool< T, TableType >::release ( handle_type  handle)
inlinenoexcept

Release an object back to the pool.

Definition at line 151 of file shm_object_pool.h.

Referenced by particle_simulation_example(), and shm_object_pool< T, TableType >::release_batch().

◆ release_batch()

template<typename T , typename TableType = shm_table>
void shm_object_pool< T, TableType >::release_batch ( std::span< const handle_type handles)
inlinenoexcept

Batch release multiple objects.

Definition at line 252 of file shm_object_pool.h.

References shm_object_pool< T, TableType >::release().

◆ unsafe_all_objects() [1/2]

template<typename T , typename TableType = shm_table>
std::span< const T > shm_object_pool< T, TableType >::unsafe_all_objects ( ) const
inlinenoexcept

Definition at line 225 of file shm_object_pool.h.

◆ unsafe_all_objects() [2/2]

template<typename T , typename TableType = shm_table>
std::span< T > shm_object_pool< T, TableType >::unsafe_all_objects ( )
inlinenoexcept

Get view of all objects (including free ones) Use with caution - includes uninitialized objects.

Definition at line 221 of file shm_object_pool.h.

Member Data Documentation

◆ invalid_handle

template<typename T , typename TableType = shm_table>
constexpr handle_type shm_object_pool< T, TableType >::invalid_handle = NULL_INDEX
staticconstexpr

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