ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
ADS::BoundaryTagAllocator Class Reference

the dynamic storage allocation with boundary tags More...

#include <ADS/memory/Allocators.h>

Public types

enum class  Fit { FIRST , BEST , NEXT }
 the block chosen for a request More...

Public member functions

 BoundaryTagAllocator (long N, Fit fit=Fit::FIRST)
 the memory of \( N \) words, one free block, with the choice fit
long allocate (long n)
 a block of at least \( n \) words, its first word, or \( -1 \)
void free (long a)
 frees the block whose first word is \( a \)
long freeWords (void) const
 the number of the words in free blocks, tags included
long freeBlocks (void) const
 the number of the free blocks

Detailed description

the dynamic storage allocation with boundary tags

Member enumeration documentation

◆ Fit

enum class ADS::BoundaryTagAllocator::Fit
strong

the block chosen for a request

Enumerator
FIRST 

the first one large enough on the list (Algorithm 2.5A)

BEST 

the smallest one large enough

NEXT 

the first one large enough after the last one given

Constructor and destructor documentation

◆ BoundaryTagAllocator()

ADS::BoundaryTagAllocator::BoundaryTagAllocator ( long N,
Fit fit = Fit::FIRST )
explicit

the memory of \( N \) words, one free block, with the choice fit

Precondition
\( N \ge 4 \)

Member function documentation

◆ allocate()

long ADS::BoundaryTagAllocator::allocate ( long n)

a block of at least \( n \) words, its first word, or \( -1 \)

In pseudocode (Algorithm 2.5A of TAOCP with the boundary tags of Algorithm 2.5C, beyond the book), \( s( p ) \) the words of the block at \( p \), two of them its tags:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{allocate}( n ) \cr 2 & \quad s_0 \gets \max( n + 2 , 4 )\quad \triangleright\ \text{the} \text{ tags, and room for the links when free} \cr 3 & \quad p \gets \text{the free block with } s( p ) \ge s_0 \text{,} \text{ first, best or next on the list} \cr 4 & \quad \textbf{if}\ p = \textbf{nil}\ \textbf{then return}\ -1 \cr 5 & \quad \textbf{if}\ s( p ) - s_0 \ge 4\ \textbf{then}\quad \triangleright\ \text{split, the upper part given} \cr 6 & \qquad s( p ) \gets s( p ) - s_0 \text{ in both its tags};\ a \gets p + s( p );\ \mathit{rover} \gets \text{the block after } p \cr 7 & \qquad \text{tag } [ a , a + s_0 ) \text{ in use, of } s_0 \text{ words} \cr 8 & \quad \textbf{else} \cr 9 & \qquad \text{unlink } p \text{ from the list};\ a \gets p; \ \text{tag it in use} \cr 10 & \quad \textbf{return}\ a + 1 \end{array}} \]

Precondition
\( n \ge 1 \)

◆ free()

void ADS::BoundaryTagAllocator::free ( long a)

frees the block whose first word is \( a \)

In pseudocode (Algorithm 2.5C of TAOCP, beyond the book):

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{procedure}\ \mathrm{free}( a ) \cr 2 & \quad p \gets a - 1;\ s \gets s( p ) \cr 3 & \quad \textbf{if}\ \text{the tag at } p - 1 \text{ is free} \ \textbf{then}\quad \triangleright\ \text{the block below} \cr 4 & \qquad q \gets p - s( p - 1 );\ \text{unlink } q;\ s \gets s + s( q );\ p \gets q \cr 5 & \quad \textbf{if}\ p + s < N\ \textbf{and}\ \text{the tag at } p + s \text{ is free}\ \textbf{then}\quad \triangleright\ \text{the block} \text{ above} \cr 6 & \qquad \text{unlink } p + s;\ s \gets s + s( p + s ) \cr 7 & \quad \text{tag } [ p , p + s ) \text{ free};\ \text{link } p \text{ at the head of the list} \end{array}} \]

Precondition
\( a \) was given by allocate() and is in use

◆ freeBlocks()

long ADS::BoundaryTagAllocator::freeBlocks ( void ) const
nodiscard

the number of the free blocks

◆ freeWords()

long ADS::BoundaryTagAllocator::freeWords ( void ) const
nodiscard

the number of the words in free blocks, tags included


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