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

an arena of objects of type T in blocks of \( n \) More...

#include <ADS/memory/Arena.h>

Classes

class  Iterator
 the iterator, on constant objects if Const More...

Public types

using value_type = T
 the objects
using size_type = std::size_t
 a number of objects
using difference_type = std::ptrdiff_t
 a distance in objects
using reference = T &
 a reference to an object
using const_reference = const T &
 a constant one
using iterator = Iterator< false >
 the iterator
using const_iterator = Iterator< true >
 the constant iterator

Public member functions

 Arena (std::size_t n=std::size_t(1)<< 12, const A &alloc=A())
 the empty arena whose blocks hold \( n \) objects, taken from alloc
 Arena (const Arena &)=delete
 no copy, since the objects are reached by their addresses
Arena & operator= (const Arena &)=delete
 no copy, since the objects are reached by their addresses
 ~Arena ()
 destroys all the objects and frees the blocks
template<class... Args>
T * make (Args &&... args)
 a new object built from args, whose address never changes in the epoch; with no args the object is value-initialized, T(), in every epoch and on every place, so that an object of trivial type is all zeros whatever the place held in an old epoch
void clear (void) noexcept
 all the objects destroyed and the blocks kept for the next ones, the first of which goes to the first place of the first block: a new epoch, in which no object of the old ones survives
bool fresh (const T *p) const
 whether \( p \) is the address of an object made in the current epoch, in time linear in the blocks: false for a pointer of an old epoch whose place has not been given again, true for one whose place has
std::size_t size (void) const
 the number of objects made in the epoch
std::size_t bytes (void) const
 the memory of the blocks, in bytes, those kept for the epoch included
iterator begin (void)
 the first object made
iterator end (void)
 past the last object made
const_iterator begin (void) const
 the first object made, constant
const_iterator end (void) const
 past the last object made, constant
const_iterator cbegin (void) const
 the first object made, constant
const_iterator cend (void) const
 past the last object made, constant

Detailed description

template<class T, class A = std::allocator< T >>
class ADS::Arena< T, A >

an arena of objects of type T in blocks of \( n \)

Member typedef documentation

◆ const_iterator

template<class T, class A = std::allocator< T >>
using ADS::Arena< T, A >::const_iterator = Iterator< true >

the constant iterator

◆ const_reference

template<class T, class A = std::allocator< T >>
using ADS::Arena< T, A >::const_reference = const T &

a constant one

◆ difference_type

template<class T, class A = std::allocator< T >>
using ADS::Arena< T, A >::difference_type = std::ptrdiff_t

a distance in objects

◆ iterator

template<class T, class A = std::allocator< T >>
using ADS::Arena< T, A >::iterator = Iterator< false >

the iterator

◆ reference

template<class T, class A = std::allocator< T >>
using ADS::Arena< T, A >::reference = T &

a reference to an object

◆ size_type

template<class T, class A = std::allocator< T >>
using ADS::Arena< T, A >::size_type = std::size_t

a number of objects

◆ value_type

template<class T, class A = std::allocator< T >>
using ADS::Arena< T, A >::value_type = T

the objects

Constructor and destructor documentation

◆ Arena() [1/2]

template<class T, class A = std::allocator< T >>
ADS::Arena< T, A >::Arena ( std::size_t n = std::size_t(1)<< 12,
const A & alloc = A() )
explicit

the empty arena whose blocks hold \( n \) objects, taken from alloc

Precondition
\( n \ge 1 \)

◆ Arena() [2/2]

template<class T, class A = std::allocator< T >>
ADS::Arena< T, A >::Arena ( const Arena< T, A > & )
delete

no copy, since the objects are reached by their addresses

◆ ~Arena()

template<class T, class A = std::allocator< T >>
ADS::Arena< T, A >::~Arena ( )

destroys all the objects and frees the blocks

Member function documentation

◆ begin() [1/2]

template<class T, class A = std::allocator< T >>
iterator ADS::Arena< T, A >::begin ( void )
inlinenodiscard

the first object made

◆ begin() [2/2]

template<class T, class A = std::allocator< T >>
const_iterator ADS::Arena< T, A >::begin ( void ) const
inlinenodiscard

the first object made, constant

◆ bytes()

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

the memory of the blocks, in bytes, those kept for the epoch included

◆ cbegin()

template<class T, class A = std::allocator< T >>
const_iterator ADS::Arena< T, A >::cbegin ( void ) const
inlinenodiscard

the first object made, constant

◆ cend()

template<class T, class A = std::allocator< T >>
const_iterator ADS::Arena< T, A >::cend ( void ) const
inlinenodiscard

past the last object made, constant

◆ clear()

template<class T, class A = std::allocator< T >>
void ADS::Arena< T, A >::clear ( void )
noexcept

all the objects destroyed and the blocks kept for the next ones, the first of which goes to the first place of the first block: a new epoch, in which no object of the old ones survives

In pseudocode (beyond the book), \( \beta \) the blocks and \( c \) the index of the block in use:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{procedure}\ \mathrm{clear}() \cr 2 & \quad \textbf{if}\ T \text{ has a destructor}\ \textbf{then} \ \text{destroy the objects of the blocks } 0 , \ldots , c \cr 3 & \quad \textbf{if}\ \beta > 0\ \textbf{then}\ c \gets 0; \ \mathit{used} \gets 0 \cr 4 & \quad \mathit{size} \gets 0 \end{array}} \]

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

◆ end() [1/2]

template<class T, class A = std::allocator< T >>
iterator ADS::Arena< T, A >::end ( void )
inlinenodiscard

past the last object made

◆ end() [2/2]

template<class T, class A = std::allocator< T >>
const_iterator ADS::Arena< T, A >::end ( void ) const
inlinenodiscard

past the last object made, constant

◆ fresh()

template<class T, class A = std::allocator< T >>
bool ADS::Arena< T, A >::fresh ( const T * p) const
nodiscard

whether \( p \) is the address of an object made in the current epoch, in time linear in the blocks: false for a pointer of an old epoch whose place has not been given again, true for one whose place has

◆ make()

template<class T, class A = std::allocator< T >>
template<class... Args>
T * ADS::Arena< T, A >::make ( Args &&... args)

a new object built from args, whose address never changes in the epoch; with no args the object is value-initialized, T(), in every epoch and on every place, so that an object of trivial type is all zeros whatever the place held in an old epoch

In pseudocode (the arena of Hanson, beyond the book), with \( \mathit{used} \) the objects of the block \( c \) in use and \( \beta \) the blocks:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{make}( \mathit{args} ) \cr 2 & \quad \textbf{if}\ \text{there is no block}\ \textbf{or} \ \mathit{used} = n\ \textbf{then} \cr 3 & \qquad \textbf{if}\ c + 1 < \beta\ \textbf{then}\ c \gets c + 1 \quad \triangleright\ \text{a block of an old epoch} \cr 4 & \qquad \textbf{else}\ \text{append a block of } n \text{ places} \text{ from the allocator};\ c \gets \beta - 1 \cr 5 & \qquad \mathit{used} \gets 0 \cr 6 & \quad p \gets \text{the place } \mathit{used} \text{ of the} \text{ block } c;\ \text{build the object in } p \cr 7 & \quad \mathit{used} \gets \mathit{used} + 1;\ \textbf{return}\ p \end{array}} \]

◆ operator=()

template<class T, class A = std::allocator< T >>
Arena & ADS::Arena< T, A >::operator= ( const Arena< T, A > & )
delete

no copy, since the objects are reached by their addresses

◆ size()

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

the number of objects made in the epoch


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