ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
ADS::ConcurrentPoolResource< M > Class Template Reference

pools of the size classes of 8 bytes up to \( M \), a heap per thread, the objects freed by any thread More...

#include <ADS/memory/ConcurrentPool.h>

Public member functions

 ConcurrentPoolResource (void)
 the empty resource, of a new identity
 ConcurrentPoolResource (const ConcurrentPoolResource &)=delete
 no copy, since its objects and allocators point to it
ConcurrentPoolResource & operator= (const ConcurrentPoolResource &)=delete
 no copy, since its objects and allocators point to it
 ~ConcurrentPoolResource ()
 frees the segments and the heaps; the objects still alive are not destroyed
template<std::size_t b, std::size_t a = alignof( std::max_align_t )>
void * allocate (void)
 \( b \) bytes aligned to \( a \), \( b \) and \( a \) known at compile time
template<std::size_t b, std::size_t a = alignof( std::max_align_t )>
void deallocate (void *p)
 the memory \( p \) of allocate< b , a >() given back, by any thread
void * allocate (std::size_t b, std::size_t a=alignof(std::max_align_t))
 \( b \) bytes aligned to \( a \), as allocate< b , a >() above
void deallocate (void *p, std::size_t b, std::size_t a=alignof(std::max_align_t))
 the memory \( p \) of allocate( b , a ) given back, by any thread, as deallocate< b , a >() above
void collect (void)
 the batches of the calling thread go back to their heaps, and the slots that other threads freed into its heap go on its free lists
std::size_t heaps (void) const
 the number of heaps, one per thread that used the resource
std::size_t bytes (void) const
 the memory of the segments, in bytes

Static public member functions

static constexpr bool pooled (std::size_t b, std::size_t a)
 true if a request of \( b \) bytes aligned to \( a \) takes a class, i.e., if \( 1 \le b \le M \) and \( a \le 16 \)
static ConcurrentPoolResource & global (void)
 the resource of the whole program, never destroyed

Static public attributes

static constexpr std::size_t GRAIN = 8
 the step of a class
static constexpr std::size_t ALIGN = 16
 the largest alignment
static constexpr std::size_t MAX = M
 the largest class
static constexpr std::size_t CLASSES = M / GRAIN
 the classes
static constexpr std::size_t SEGMENT = detail::PageHeap< CLASSES >::SEGMENT
 the bytes of a segment of the heaps

Detailed description

template<std::size_t M = 256>
class ADS::ConcurrentPoolResource< M >

pools of the size classes of 8 bytes up to \( M \), a heap per thread, the objects freed by any thread

Constructor and destructor documentation

◆ ConcurrentPoolResource() [1/2]

template<std::size_t M = 256>
ADS::ConcurrentPoolResource< M >::ConcurrentPoolResource ( void )

the empty resource, of a new identity

◆ ConcurrentPoolResource() [2/2]

template<std::size_t M = 256>
ADS::ConcurrentPoolResource< M >::ConcurrentPoolResource ( const ConcurrentPoolResource< M > & )
delete

no copy, since its objects and allocators point to it

◆ ~ConcurrentPoolResource()

template<std::size_t M = 256>
ADS::ConcurrentPoolResource< M >::~ConcurrentPoolResource ( )

frees the segments and the heaps; the objects still alive are not destroyed

Member function documentation

◆ allocate() [1/2]

template<std::size_t M = 256>
void * ADS::ConcurrentPoolResource< M >::allocate ( std::size_t b,
std::size_t a = alignof(std::max_align_t) )
nodiscard

\( b \) bytes aligned to \( a \), as allocate< b , a >() above

◆ allocate() [2/2]

template<std::size_t M = 256>
template<std::size_t b, std::size_t a = alignof( std::max_align_t )>
void * ADS::ConcurrentPoolResource< M >::allocate ( void )
nodiscard

\( b \) bytes aligned to \( a \), \( b \) and \( a \) known at compile time

In pseudocode (the heaps of the threads of Hoard and the batches of snmalloc, beyond the book), \( H \) the heap of the calling thread and \( c \) the class of the request:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{allocate}( b , a ) \cr 2 & \quad \textbf{if}\ b > M\ \textbf{or}\ a > 16 \ \textbf{then return}\ \text{the memory of operator new} \cr 3 & \quad H \gets \text{the cached heap, else the one of this thread on} \text{ the list, else a new one pushed by a CAS} \cr 4 & \quad s \gets H.\mathrm{local}( c );\ \textbf{if}\ s \ne \textbf{nil}\ \textbf{then return}\ s \cr 5 & \quad \text{send the batch of } H \text{ for } c \cr 6 & \quad R \gets \mathrm{exchange}( H.\mathit{returns}_c , \textbf{nil} )\quad \triangleright\ \text{acquire} \cr 7 & \quad \textbf{foreach}\ s \in R\ \textbf{do}\ \text{push } s \text{ on the list of its page} \cr 8 & \quad s \gets H.\mathrm{local}( c );\ \textbf{if}\ s \ne \textbf{nil}\ \textbf{then return}\ s \cr 9 & \quad \textbf{return}\ H.\mathrm{more}( c ) \end{array}} \]

◆ bytes()

template<std::size_t M = 256>
std::size_t ADS::ConcurrentPoolResource< M >::bytes ( void ) const
nodiscard

the memory of the segments, in bytes

◆ collect()

template<std::size_t M = 256>
void ADS::ConcurrentPoolResource< M >::collect ( void )

the batches of the calling thread go back to their heaps, and the slots that other threads freed into its heap go on its free lists

In pseudocode (beyond the book):

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{procedure}\ \mathrm{collect}() \cr 2 & \quad \textbf{foreach}\ \text{class } c\ \textbf{do} \cr 3 & \qquad \text{send the batch of } H \text{ for } c \cr 4 & \qquad \textbf{foreach}\ s \in \mathrm{exchange}( H.\mathit{returns}_c , \textbf{nil} )\ \textbf{do}\ \text{push } s \text{ on the list of its page} \end{array}} \]

◆ deallocate() [1/2]

template<std::size_t M = 256>
template<std::size_t b, std::size_t a = alignof( std::max_align_t )>
void ADS::ConcurrentPoolResource< M >::deallocate ( void * p)

the memory \( p \) of allocate< b , a >() given back, by any thread

In pseudocode (the batches of snmalloc, beyond the book), \( H \) the heap of the calling thread and \( B \) its batch for the class \( c \):

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{procedure}\ \mathrm{deallocate}( p ) \cr 2 & \quad g \gets p \wedge \neg ( 2^{16} - 1 );\ O \gets \text{the} \text{ owner of the page } g \cr 3 & \quad \textbf{if}\ O = H\ \textbf{then}\ \text{push } p \text{ on} \text{ the list of } g;\ \textbf{return} \cr 4 & \quad \textbf{if}\ B \text{ goes to a heap other than } O \ \textbf{then}\ \mathrm{send}( B );\ B \gets \text{a batch for } O \cr 5 & \quad \text{push } p \text{ on } B \cr 6 & \quad \textbf{if}\ |B| = 32\ \textbf{then}\ \mathrm{send}( B ) \cr 7 & \textbf{procedure}\ \mathrm{send}( B ) \cr 8 & \quad \textbf{if}\ B = \emptyset\ \textbf{then return} \cr 9 & \quad \textbf{repeat}\ B.\mathit{tail}.\mathit{next} \gets O.\mathit{returns}_c\ \textbf{until}\ \mathrm{CAS}( O.\mathit{returns}_c , B.\mathit{tail}.\mathit{next} , B.\mathit{head} ) \cr 10 & \quad B \gets \emptyset\quad \triangleright\ \text{the CAS with} \text{ release} \end{array}} \]

Precondition
\( p \) was given by allocate< b , a >(), by any thread

◆ deallocate() [2/2]

template<std::size_t M = 256>
void ADS::ConcurrentPoolResource< M >::deallocate ( void * p,
std::size_t b,
std::size_t a = alignof(std::max_align_t) )

the memory \( p \) of allocate( b , a ) given back, by any thread, as deallocate< b , a >() above

Precondition
\( p \) was given by allocate( b , a ), by any thread

◆ global()

template<std::size_t M = 256>
ConcurrentPoolResource & ADS::ConcurrentPoolResource< M >::global ( void )
staticnodiscard

the resource of the whole program, never destroyed

◆ heaps()

template<std::size_t M = 256>
std::size_t ADS::ConcurrentPoolResource< M >::heaps ( void ) const
nodiscard

the number of heaps, one per thread that used the resource

◆ operator=()

template<std::size_t M = 256>
ConcurrentPoolResource & ADS::ConcurrentPoolResource< M >::operator= ( const ConcurrentPoolResource< M > & )
delete

no copy, since its objects and allocators point to it

◆ pooled()

template<std::size_t M = 256>
constexpr bool ADS::ConcurrentPoolResource< M >::pooled ( std::size_t b,
std::size_t a )
inlinestaticnodiscardconstexpr

true if a request of \( b \) bytes aligned to \( a \) takes a class, i.e., if \( 1 \le b \le M \) and \( a \le 16 \)

Member data documentation

◆ ALIGN

template<std::size_t M = 256>
std::size_t ADS::ConcurrentPoolResource< M >::ALIGN = 16
staticconstexpr

the largest alignment

◆ CLASSES

template<std::size_t M = 256>
std::size_t ADS::ConcurrentPoolResource< M >::CLASSES = M / GRAIN
staticconstexpr

the classes

◆ GRAIN

template<std::size_t M = 256>
std::size_t ADS::ConcurrentPoolResource< M >::GRAIN = 8
staticconstexpr

the step of a class

◆ MAX

template<std::size_t M = 256>
std::size_t ADS::ConcurrentPoolResource< M >::MAX = M
staticconstexpr

the largest class

◆ SEGMENT

template<std::size_t M = 256>
std::size_t ADS::ConcurrentPoolResource< M >::SEGMENT = detail::PageHeap< CLASSES >::SEGMENT
staticconstexpr

the bytes of a segment of the heaps


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