POSIX Shared Memory Data Structures 1.0
High-performance lock-free data structures for inter-process communication
|
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_type > | acquire_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 |
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.
T | Type of objects in the pool |
TableType | Metadata table type |
Definition at line 23 of file shm_object_pool.h.
using shm_object_pool< T, TableType >::handle_type = uint32_t |
Definition at line 48 of file shm_object_pool.h.
using shm_object_pool< T, TableType >::size_type = size_t |
Definition at line 47 of file shm_object_pool.h.
using shm_object_pool< T, TableType >::value_type = T |
Definition at line 46 of file shm_object_pool.h.
|
inline |
Definition at line 53 of file shm_object_pool.h.
References shm_object_pool< T, TableType >::capacity(), and shm_object_pool< T, TableType >::name().
|
inlinenoexcept |
Acquire an object from the pool.
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().
|
inlinenoexcept |
Batch acquire multiple objects.
count | Number of objects to acquire | |
[out] | handles | Array to store acquired handles |
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.
|
inline |
Acquire and construct an object.
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().
|
inlinenoexcept |
Get pool statistics.
Definition at line 197 of file shm_object_pool.h.
Referenced by shm_object_pool< T, TableType >::full(), shm_object_pool< T, TableType >::num_available(), particle_simulation_example(), and shm_object_pool< T, TableType >::shm_object_pool().
|
inlinenoexcept |
Definition at line 209 of file shm_object_pool.h.
References shm_object_pool< T, TableType >::num_allocated().
|
inlinenoexcept |
Definition at line 213 of file shm_object_pool.h.
References shm_object_pool< T, TableType >::capacity(), and shm_object_pool< T, TableType >::num_allocated().
|
inlinenoexcept |
Definition at line 182 of file shm_object_pool.h.
|
inlinenoexcept |
Definition at line 177 of file shm_object_pool.h.
|
inlinenoexcept |
Check if handle is valid.
Definition at line 190 of file shm_object_pool.h.
|
inlinenoexcept |
Definition at line 229 of file shm_object_pool.h.
Referenced by shm_object_pool< T, TableType >::shm_object_pool().
|
inlinenoexcept |
Definition at line 201 of file shm_object_pool.h.
Referenced by shm_object_pool< T, TableType >::empty(), shm_object_pool< T, TableType >::full(), shm_object_pool< T, TableType >::num_available(), and particle_simulation_example().
|
inlinenoexcept |
Definition at line 205 of file shm_object_pool.h.
References shm_object_pool< T, TableType >::capacity(), and shm_object_pool< T, TableType >::num_allocated().
|
inlinenoexcept |
Definition at line 173 of file shm_object_pool.h.
|
inlinenoexcept |
Access object by handle.
Definition at line 169 of file shm_object_pool.h.
|
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().
|
inlinenoexcept |
Batch release multiple objects.
Definition at line 252 of file shm_object_pool.h.
References shm_object_pool< T, TableType >::release().
|
inlinenoexcept |
Definition at line 225 of file shm_object_pool.h.
|
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.
|
staticconstexpr |
Definition at line 50 of file shm_object_pool.h.
Referenced by shm_object_pool< T, TableType >::acquire(), shm_object_pool< T, TableType >::acquire_batch(), and shm_object_pool< T, TableType >::acquire_construct().