|
ADS
Algorithms and Data Structures
|
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 | |
the dynamic storage allocation with boundary tags
|
strong |
|
explicit |
the memory of \( N \) words, one free block, with the choice fit
| 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}} \]
| 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}} \]
|
nodiscard |
the number of the free blocks
|
nodiscard |
the number of the words in free blocks, tags included