Deluge Firmware 1.3.0
Build date: 2026.02.14
Loading...
Searching...
No Matches
deluge::memory::ObjectPool< T, Alloc > Class Template Reference

A simple object pool implementation. More...

#include <object_pool.h>

Public Types

using value_type = T
 The type of object in the pool.
using pointer_type = std::unique_ptr<T, decltype(&recycle)>
 A managed pointer type for an object from the pool.

Public Member Functions

void resize (size_t n)
 Sets the size of the pool.
size_t capacity () const
 Gets the capacity of the pool.
size_t size () const
 Gets the number of objects in the pool.
void repopulate () noexcept(false)
 Repopulates the pool to its original size.
template<typename... Args>
pointer_type acquire (Args &&... args) noexcept(false)
 Acquires an object from the pool.
void clear ()
 Clears the pool.
constexpr bool empty () const
 Checks if the pool is empty.

Static Public Member Functions

static constexpr ObjectPool & get ()
 Gets the global pool for a given object.
static void recycle (gsl::owner< T * > obj)

Private Attributes

size_t capacity_ = kDefaultSize
std::stack< T *, std::vector< T *, Alloc< T * > > > objects_
Alloc< T > alloc_

Static Private Attributes

static constexpr size_t kDefaultSize = 48

Detailed Description

template<typename T, template< typename > typename Alloc = std::allocator>
class deluge::memory::ObjectPool< T, Alloc >

A simple object pool implementation.

Template Parameters
TThe type of object to pool
AllocThe allocator to use for the pool
Note
ObjectPool instances are thread-local singletons

Member Function Documentation

◆ acquire()

template<typename T, template< typename > typename Alloc = std::allocator>
template<typename... Args>
pointer_type deluge::memory::ObjectPool< T, Alloc >::acquire ( Args &&... args)
inlinenodiscard

Acquires an object from the pool.

Template Parameters
ArgsThe types of arguments to pass to the object's constructor
Parameters
argsThe arguments to pass to the object's constructor
Exceptions
deluge::exception::BAD_ALLOCif memory allocation fails
Returns
A managed pointer to the acquired object

◆ capacity()

template<typename T, template< typename > typename Alloc = std::allocator>
size_t deluge::memory::ObjectPool< T, Alloc >::capacity ( ) const
inlinenodiscard

Gets the capacity of the pool.

Returns
The capacity of the pool

◆ empty()

template<typename T, template< typename > typename Alloc = std::allocator>
bool deluge::memory::ObjectPool< T, Alloc >::empty ( ) const
inlinenodiscardconstexpr

Checks if the pool is empty.

Returns
true if the pool is empty, false otherwise

◆ get()

template<typename T, template< typename > typename Alloc = std::allocator>
constexpr ObjectPool & deluge::memory::ObjectPool< T, Alloc >::get ( )
inlinestaticconstexpr

Gets the global pool for a given object.

Returns
A reference to the pool

◆ recycle()

template<typename T, template< typename > typename Alloc = std::allocator>
void deluge::memory::ObjectPool< T, Alloc >::recycle ( gsl::owner< T * > obj)
inlinestatic

Recycles an object back into the pool

Parameters
objThe object to recycle

◆ repopulate()

template<typename T, template< typename > typename Alloc = std::allocator>
void deluge::memory::ObjectPool< T, Alloc >::repopulate ( )
inline

Repopulates the pool to its original size.

Exceptions
deluge::exception::BAD_ALLOCif memory allocation fails

◆ resize()

template<typename T, template< typename > typename Alloc = std::allocator>
void deluge::memory::ObjectPool< T, Alloc >::resize ( size_t n)
inline

Sets the size of the pool.

Parameters
sizeThe new size of the pool

◆ size()

template<typename T, template< typename > typename Alloc = std::allocator>
size_t deluge::memory::ObjectPool< T, Alloc >::size ( ) const
inlinenodiscard

Gets the number of objects in the pool.

Returns
The number of objects in the pool

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