ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
ADS::ObjectPool< T, A > Class Template Reference

a pool of objects of type T in blocks of at most \( n \) slots More...

#include <ADS/memory/ObjectPool.h>

Public types

using allocator_type = A
 the type of the allocator

Public member functions

 ObjectPool (std::size_t n=std::size_t(1)<< 24, const A &alloc=A())
 the empty pool whose blocks grow up to \( n \) slots, taken from alloc
 ObjectPool (const ObjectPool &)=delete
 no copy, since its objects are reached by their addresses
ObjectPool & operator= (const ObjectPool &)=delete
 no copy, since its objects are reached by their addresses
 ObjectPool (ObjectPool &&other) noexcept
 the slots of other, which is left empty; its objects stay where they are
ObjectPool & operator= (ObjectPool &&other) noexcept
 the slots of other, which is left empty, after those of this pool are freed
 ~ObjectPool ()
 frees the blocks; the objects still alive are not destroyed
void swap (ObjectPool &other) noexcept
 the slots of this pool and of other exchanged
void adopt (ObjectPool &other) noexcept
 the blocks of other taken by this pool, with the objects alive in them, other left empty: the objects made by other become objects of this pool, to be freed by it, so that a linked structure takes the nodes of another one with no copy (the absorb() of List); in both cases the limit of the blocks becomes the larger of the two; if this pool has no block, it takes the blocks and the free slots of other, as swap( other ) does, else the block chain of other goes under the first block of this pool, in time linear in the blocks of this pool for the walk to it (over its last block, in time linear in the blocks of other, when this pool has blocks kept by clear() not yet used again), and the free slots of other and those it never used are no longer given, until the pool dies or is cleared
template<class... Args>
T * make (Args &&... args)
 a new object built from args, in the slot freed last if any; with no args the object is value-initialized, T(), in every epoch and on every slot, freed or of an old epoch, so that an object of trivial type is all zeros whatever the slot held before
void free (T *x)
 destroys \( x \) and keeps its slot for the next make()
T * allocate (void)
 the raw slot of an object, not built, as std::allocator gives it: the caller builds the object in it (and destroys it before deallocate())
void deallocate (T *x)
 keeps the slot of \( x \), whose object is already destroyed (or was never built), for the next make() or allocate()
void clear (void) noexcept
 all the objects and the free slots dropped, with no destructor called, and the blocks kept for the next objects, from the last block down: a new epoch, in which nothing of the old ones survives
bool fresh (const T *p) const
 whether \( p \) is a slot given in the current epoch, alive or freed, in time linear in the blocks: false for a pointer of an old epoch whose slot has not been given again, true for one whose slot has
std::size_t size (void) const
 the number of objects alive
std::size_t bytes (void) const
 the memory of the blocks, in bytes
allocator_type get_allocator (void) const
 the allocator of the blocks

Detailed description

template<class T, class A = HugePageAllocator< T >>
class ADS::ObjectPool< T, A >

a pool of objects of type T in blocks of at most \( n \) slots

Member typedef documentation

◆ allocator_type

template<class T, class A = HugePageAllocator< T >>
using ADS::ObjectPool< T, A >::allocator_type = A

the type of the allocator

Constructor and destructor documentation

◆ ObjectPool() [1/3]

template<class T, class A = HugePageAllocator< T >>
ADS::ObjectPool< T, A >::ObjectPool ( std::size_t n = std::size_t(1)<< 24,
const A & alloc = A() )
explicit

the empty pool whose blocks grow up to \( n \) slots, taken from alloc

Precondition
\( n \ge 1 \)

◆ ObjectPool() [2/3]

template<class T, class A = HugePageAllocator< T >>
ADS::ObjectPool< T, A >::ObjectPool ( const ObjectPool< T, A > & )
delete

no copy, since its objects are reached by their addresses

◆ ObjectPool() [3/3]

template<class T, class A = HugePageAllocator< T >>
ADS::ObjectPool< T, A >::ObjectPool ( ObjectPool< T, A > && other)
noexcept

the slots of other, which is left empty; its objects stay where they are

◆ ~ObjectPool()

template<class T, class A = HugePageAllocator< T >>
ADS::ObjectPool< T, A >::~ObjectPool ( )

frees the blocks; the objects still alive are not destroyed

Member function documentation

◆ adopt()

template<class T, class A = HugePageAllocator< T >>
void ADS::ObjectPool< T, A >::adopt ( ObjectPool< T, A > & other)
noexcept

the blocks of other taken by this pool, with the objects alive in them, other left empty: the objects made by other become objects of this pool, to be freed by it, so that a linked structure takes the nodes of another one with no copy (the absorb() of List); in both cases the limit of the blocks becomes the larger of the two; if this pool has no block, it takes the blocks and the free slots of other, as swap( other ) does, else the block chain of other goes under the first block of this pool, in time linear in the blocks of this pool for the walk to it (over its last block, in time linear in the blocks of other, when this pool has blocks kept by clear() not yet used again), and the free slots of other and those it never used are no longer given, until the pool dies or is cleared

Precondition
the two allocators are equal

◆ allocate()

template<class T, class A = HugePageAllocator< T >>
T * ADS::ObjectPool< T, A >::allocate ( void )
nodiscard

the raw slot of an object, not built, as std::allocator gives it: the caller builds the object in it (and destroys it before deallocate())

◆ bytes()

template<class T, class A = HugePageAllocator< T >>
std::size_t ADS::ObjectPool< T, A >::bytes ( void ) const
nodiscard

the memory of the blocks, in bytes

◆ clear()

template<class T, class A = HugePageAllocator< T >>
void ADS::ObjectPool< T, A >::clear ( void )
noexcept

all the objects and the free slots dropped, with no destructor called, and the blocks kept for the next objects, from the last block down: a new epoch, in which nothing of the old ones survives

In pseudocode (beyond the book), \( R \) the blocks kept and not yet used again in the epoch, linked from the last one to the first:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{procedure}\ \mathrm{clear}() \cr 2 & \quad F \gets \textbf{nil};\ \mathit{size} \gets 0 \cr 3 & \quad \textbf{if}\ \text{there is a block}\ \textbf{then} \ [ \mathit{next} , \mathit{end} ) \gets \text{the slots of the} \text{ last block};\ R \gets \text{the block before it} \end{array}} \]

and make() takes, when \( F \) is empty and \( \mathit{next} = \mathit{end} \), the slots of the block \( R \) (and \( R \) moves to the block before it) while \( R \ne \textbf{nil} \), a new block otherwise

Precondition
no other thread accesses an object of the pool: each of its accesses happens before the call

◆ deallocate()

template<class T, class A = HugePageAllocator< T >>
void ADS::ObjectPool< T, A >::deallocate ( T * x)

keeps the slot of \( x \), whose object is already destroyed (or was never built), for the next make() or allocate()

Precondition
\( x \) was given by this pool

◆ free()

template<class T, class A = HugePageAllocator< T >>
void ADS::ObjectPool< T, A >::free ( T * x)

destroys \( x \) and keeps its slot for the next make()

In pseudocode (the list AVAIL of section 2.2.3 of TAOCP, beyond the book):

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{procedure}\ \mathrm{free}( x ) \cr 2 & \quad \text{destroy the object } x;\ x.\mathit{next} \gets F;\ F \gets x \end{array}} \]

Precondition
\( x \) was made by this pool and is alive

◆ fresh()

template<class T, class A = HugePageAllocator< T >>
bool ADS::ObjectPool< T, A >::fresh ( const T * p) const
nodiscard

whether \( p \) is a slot given in the current epoch, alive or freed, in time linear in the blocks: false for a pointer of an old epoch whose slot has not been given again, true for one whose slot has

◆ get_allocator()

template<class T, class A = HugePageAllocator< T >>
allocator_type ADS::ObjectPool< T, A >::get_allocator ( void ) const
inlinenodiscard

the allocator of the blocks

◆ make()

template<class T, class A = HugePageAllocator< T >>
template<class... Args>
T * ADS::ObjectPool< T, A >::make ( Args &&... args)

a new object built from args, in the slot freed last if any; with no args the object is value-initialized, T(), in every epoch and on every slot, freed or of an old epoch, so that an object of trivial type is all zeros whatever the slot held before

In pseudocode (the list AVAIL of section 2.2.3 of TAOCP, beyond the book), \( F \) the free slots and \( [ \mathit{next} , \mathit{end} ) \) the slots never used of the last block, of \( k \) slots:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{make}( \mathit{args} ) \cr 2 & \quad \textbf{if}\ F \ne \textbf{nil}\ \textbf{then}\ s \gets F;\ F \gets s.\mathit{next} \cr 3 & \quad \textbf{else} \cr 4 & \qquad \textbf{if}\ \mathit{next} = \mathit{end}\ \textbf{then} \ \text{take a block of } \min( 2 k , n ) \text{ slots, } 1 \text{ the} \text{ first time} \cr 5 & \qquad s \gets \mathit{next};\ \mathit{next} \gets \mathit{next} + 1 \cr 6 & \quad \text{build the object in } s \text{ (on an exception, push} \text{ } s \text{ on } F \text{ again)} \cr 7 & \quad \textbf{return}\ s \end{array}} \]

◆ operator=() [1/2]

template<class T, class A = HugePageAllocator< T >>
ObjectPool & ADS::ObjectPool< T, A >::operator= ( const ObjectPool< T, A > & )
delete

no copy, since its objects are reached by their addresses

◆ operator=() [2/2]

template<class T, class A = HugePageAllocator< T >>
ObjectPool & ADS::ObjectPool< T, A >::operator= ( ObjectPool< T, A > && other)
noexcept

the slots of other, which is left empty, after those of this pool are freed

◆ size()

template<class T, class A = HugePageAllocator< T >>
std::size_t ADS::ObjectPool< T, A >::size ( void ) const
nodiscard

the number of objects alive

◆ swap()

template<class T, class A = HugePageAllocator< T >>
void ADS::ObjectPool< T, A >::swap ( ObjectPool< T, A > & other)
noexcept

the slots of this pool and of other exchanged


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