|
| | 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
|
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
template<class T, class A = HugePageAllocator< T >>
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
template<class T, class A = HugePageAllocator< T >>
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
template<class T, class A = HugePageAllocator< T >>
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
template<class T, class A = HugePageAllocator< T >>
template<class... 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}}
\]