|
Common System 0.2.0
Common interfaces and patterns for system integration
|
Thread-safe object pool that reuses raw storage for expensive objects. More...
#include <object_pool.h>

Public Types | |
| using | value_type = T |
| using | deleter_type = PoolDeleter<T> |
| using | pointer_type = std::unique_ptr<T, deleter_type> |
Public Member Functions | |
| ObjectPool (std::size_t growth=32) | |
| Construct an object pool with the specified growth factor. | |
| template<typename... Args> | |
| pointer_type | acquire (bool *reused, Args &&... args) |
| Acquire an object constructed with the provided arguments. | |
| template<typename... Args> | |
| pointer_type | acquire (Args &&... args) |
| void | release (T *ptr) noexcept |
| Return raw storage to the pool (destructor already run). | |
| void | reserve (std::size_t count) |
Add count additional blocks to the pool. | |
| void | clear () |
| Destroy all cached instances and release memory. | |
| std::size_t | available () const |
| ObjectPool (std::size_t growth=32) | |
| template<typename... Args> | |
| std::unique_ptr< T, std::function< void(T *)> > | acquire (bool *reused, Args &&... args) |
| Acquire an object constructed with the provided arguments. | |
| template<typename... Args> | |
| std::unique_ptr< T, std::function< void(T *)> > | acquire (Args &&... args) |
| void | release (T *ptr) noexcept |
| Return raw storage to the pool (destructor already run). | |
| void | reserve (std::size_t count) |
| Add count additional blocks to the pool. | |
| void | clear () |
| Destroy all cached instances and release memory. | |
| std::size_t | available () const |
Private Types | |
| using | RawPtr = std::unique_ptr<T, detail::RawDelete<T>> |
Private Member Functions | |
| void | allocate_block_unlocked (std::size_t count) |
| void | allocate_block_unlocked (std::size_t count) |
Private Attributes | |
| std::size_t | growth_ |
| std::mutex | mutex_ |
| std::stack< T * > | free_list_ |
| std::vector< RawPtr > | storage_ |
Thread-safe object pool that reuses raw storage for expensive objects.
The pool allocates raw memory once and performs placement-new construction on acquisition. Objects are destructed when released, but the underlying storage is retained for fast reuse.
The pool allocates raw memory once and performs placement-new construction on acquisition. Objects are destructed when released, but the underlying storage is retained for fast reuse.
| T | Object type to pool |
Definition at line 218 of file utils.cppm.
| using kcenon::common::utils::ObjectPool< T >::deleter_type = PoolDeleter<T> |
Definition at line 65 of file object_pool.h.
| using kcenon::common::utils::ObjectPool< T >::pointer_type = std::unique_ptr<T, deleter_type> |
Definition at line 66 of file object_pool.h.
|
exportprivate |
Definition at line 149 of file object_pool.h.
|
export |
Definition at line 64 of file object_pool.h.
|
inlineexplicit |
Construct an object pool with the specified growth factor.
| growth | Number of objects to pre-allocate per expansion (minimum 1). |
Definition at line 72 of file object_pool.h.
|
inlineexplicitexport |
Definition at line 222 of file utils.cppm.
|
inline |
Definition at line 106 of file object_pool.h.
References kcenon::common::utils::ObjectPool< T >::acquire().

|
inlineexport |
Definition at line 258 of file utils.cppm.
References kcenon::common::utils::ObjectPool< T >::acquire().

|
inline |
Acquire an object constructed with the provided arguments.
| reused | Optional pointer that receives whether an existing block was reused. |
| args | Arguments forwarded to the object's constructor. |
Definition at line 82 of file object_pool.h.
References kcenon::common::utils::ObjectPool< T >::allocate_block_unlocked(), kcenon::common::utils::ObjectPool< T >::free_list_, kcenon::common::utils::ObjectPool< T >::growth_, and kcenon::common::utils::ObjectPool< T >::mutex_.
Referenced by kcenon::common::utils::ObjectPool< T >::acquire(), and main().


|
inlineexport |
Acquire an object constructed with the provided arguments.
| reused | Optional pointer that receives whether an existing block was reused. |
| args | Arguments forwarded to the object's constructor. |
Definition at line 232 of file utils.cppm.
References kcenon::common::utils::ObjectPool< T >::allocate_block_unlocked(), kcenon::common::utils::ObjectPool< T >::free_list_, kcenon::common::utils::ObjectPool< T >::growth_, kcenon::common::utils::ObjectPool< T >::mutex_, and kcenon::common::utils::ObjectPool< T >::release().

|
inlineprivate |
Definition at line 151 of file object_pool.h.
References kcenon::common::utils::ObjectPool< T >::free_list_, and kcenon::common::utils::ObjectPool< T >::storage_.
Referenced by kcenon::common::utils::ObjectPool< T >::acquire(), and kcenon::common::utils::ObjectPool< T >::reserve().

|
inlineexportprivate |
Definition at line 303 of file utils.cppm.
References kcenon::common::utils::ObjectPool< T >::free_list_, and kcenon::common::utils::ObjectPool< T >::storage_.
|
inlinenodiscard |
Definition at line 143 of file object_pool.h.
References kcenon::common::utils::ObjectPool< T >::free_list_, and kcenon::common::utils::ObjectPool< T >::mutex_.
Referenced by main().

|
inlinenodiscardexport |
Definition at line 295 of file utils.cppm.
References kcenon::common::utils::ObjectPool< T >::free_list_, and kcenon::common::utils::ObjectPool< T >::mutex_.
|
inline |
Destroy all cached instances and release memory.
Definition at line 137 of file object_pool.h.
References kcenon::common::utils::ObjectPool< T >::free_list_, kcenon::common::utils::ObjectPool< T >::mutex_, and kcenon::common::utils::ObjectPool< T >::storage_.
Referenced by main().

|
inlineexport |
Destroy all cached instances and release memory.
Definition at line 289 of file utils.cppm.
References kcenon::common::utils::ObjectPool< T >::free_list_, kcenon::common::utils::ObjectPool< T >::mutex_, and kcenon::common::utils::ObjectPool< T >::storage_.
|
inlinenoexcept |
Return raw storage to the pool (destructor already run).
Definition at line 113 of file object_pool.h.
References kcenon::common::utils::ObjectPool< T >::free_list_, and kcenon::common::utils::ObjectPool< T >::mutex_.
Referenced by kcenon::common::utils::ObjectPool< T >::acquire().

|
inlineexportnoexcept |
Return raw storage to the pool (destructor already run).
Definition at line 265 of file utils.cppm.
References kcenon::common::utils::ObjectPool< T >::free_list_, and kcenon::common::utils::ObjectPool< T >::mutex_.
|
inline |
Add count additional blocks to the pool.
Definition at line 126 of file object_pool.h.
References kcenon::common::utils::ObjectPool< T >::allocate_block_unlocked(), and kcenon::common::utils::ObjectPool< T >::mutex_.
Referenced by main().


|
inlineexport |
Add count additional blocks to the pool.
Definition at line 278 of file utils.cppm.
References kcenon::common::utils::ObjectPool< T >::allocate_block_unlocked(), and kcenon::common::utils::ObjectPool< T >::mutex_.

|
exportprivate |
Definition at line 161 of file object_pool.h.
Referenced by kcenon::common::utils::ObjectPool< T >::acquire(), kcenon::common::utils::ObjectPool< T >::allocate_block_unlocked(), kcenon::common::utils::ObjectPool< T >::available(), kcenon::common::utils::ObjectPool< T >::clear(), and kcenon::common::utils::ObjectPool< T >::release().
|
exportprivate |
Definition at line 159 of file object_pool.h.
Referenced by kcenon::common::utils::ObjectPool< T >::acquire().
|
mutableexportprivate |
Definition at line 160 of file object_pool.h.
Referenced by kcenon::common::utils::ObjectPool< T >::acquire(), kcenon::common::utils::ObjectPool< T >::available(), kcenon::common::utils::ObjectPool< T >::clear(), kcenon::common::utils::ObjectPool< T >::release(), and kcenon::common::utils::ObjectPool< T >::reserve().
|
exportprivate |
Definition at line 162 of file object_pool.h.
Referenced by kcenon::common::utils::ObjectPool< T >::allocate_block_unlocked(), and kcenon::common::utils::ObjectPool< T >::clear().