an allocator of small blocks by size classes, for one thread
More...
#include <ADS/memory/SizeClassAllocator.h>
|
| | SizeClassAllocator (void)=default |
| | the allocator with no page
|
| | SizeClassAllocator (const SizeClassAllocator &)=delete |
| | no copy, since its blocks are in its pages
|
| SizeClassAllocator & | operator= (const SizeClassAllocator &)=delete |
| | no copy, since its blocks are in its pages
|
| | SizeClassAllocator (SizeClassAllocator &&other) noexcept |
| | the pages of other, which is left empty
|
| SizeClassAllocator & | operator= (SizeClassAllocator &&other) noexcept |
| | the pages of other, which is left empty, after those of this allocator are freed
|
| | ~SizeClassAllocator () |
| | frees the pages
|
| void * | allocate (std::size_t n) |
| | a block of \( n \) bytes at least, aligned to 16 bytes (8 for the class of 8 bytes)
|
| void | deallocate (void *p, std::size_t n) noexcept |
| | gives back the block \( p \) of a request of \( n \) bytes, pushed on \( F_c \) for its class \( c \)
|
| std::size_t | bytes (void) const |
| | the memory of the pages, in bytes
|
| void | release (void) noexcept |
| | frees all the pages; the blocks given are no longer valid
|
|
| static unsigned | sizeClass (std::size_t n) |
| | the class of a request of \( n \) bytes: 0 for \( n \le 8 \), \( \lceil n / 16 \rceil \) up to 128, and otherwise, with \( 2^e < n \le 2^{e + 1} \), \( 9 + 4 ( e - 7 ) + \lceil ( n - 2^e ) / 2^{e - 2} \rceil - 1 \)
|
| static std::size_t | classSize (unsigned c) |
| | the size of the class \( c \): 8, \( 16 c \) up to 128, and otherwise \( 2^e + j \, 2^{e - 2} \) with \( e = 7 + \lfloor ( c - 9 ) / 4 \rfloor \) and \( j = ( c - 9 ) \bmod 4 + 1 \)
|
|
| static constexpr std::size_t | LARGEST = 32768 |
| | the largest request served by the classes
|
| static constexpr std::size_t | PAGE = std::size_t( 1 ) << 16 |
| | the bytes of a page
|
an allocator of small blocks by size classes, for one thread
◆ SizeClassAllocator() [1/3]
| ADS::SizeClassAllocator::SizeClassAllocator |
( |
void | | ) |
|
|
default |
the allocator with no page
◆ SizeClassAllocator() [2/3]
| ADS::SizeClassAllocator::SizeClassAllocator |
( |
const SizeClassAllocator & | | ) |
|
|
delete |
no copy, since its blocks are in its pages
◆ SizeClassAllocator() [3/3]
| ADS::SizeClassAllocator::SizeClassAllocator |
( |
SizeClassAllocator && | other | ) |
|
|
noexcept |
the pages of other, which is left empty
◆ ~SizeClassAllocator()
| ADS::SizeClassAllocator::~SizeClassAllocator |
( |
| ) |
|
|
inline |
◆ allocate()
| void * ADS::SizeClassAllocator::allocate |
( |
std::size_t | n | ) |
|
|
nodiscard |
a block of \( n \) bytes at least, aligned to 16 bytes (8 for the class of 8 bytes)
In pseudocode (the lists of available blocks of section 2.5 of TAOCP, by size class, beyond the book), \( F_c \) the blocks given back of the class \( c \) and \( [ \mathit{next}_c , \mathit{end}_c ) \) the rest of its page:
\[\class{pseudocode}{\begin{array}{r@{\quad}l}
1 & \textbf{function}\ \mathrm{allocate}( n ) \cr
2 & \quad \textbf{if}\ n > 32768\ \textbf{then return}\ \text{the}
\text{ memory of operator new} \cr
3 & \quad c \gets \mathrm{sizeClass}( n );\ s \gets \mathrm{classSize}(
c ) \cr
4 & \quad \textbf{if}\ F_c \ne \textbf{nil}\ \textbf{then return}
\ \text{the pop of } F_c \cr
5 & \quad \textbf{if}\ \mathit{end}_c - \mathit{next}_c \ge s
\ \textbf{then} \cr
6 & \qquad p \gets \mathit{next}_c;\ \mathit{next}_c \gets
\mathit{next}_c + s;\ \textbf{return}\ p \cr
7 & \quad \text{take a page of } \max( 2^{16} , s + 16 ) \text{ bytes,}
\text{ a header of 16 first} \cr
8 & \quad \textbf{return}\ \text{its first block, the rest in }
[ \mathit{next}_c , \mathit{end}_c )
\end{array}}
\]
- Precondition
- \( n \ge 1 \)
◆ bytes()
| std::size_t ADS::SizeClassAllocator::bytes |
( |
void | | ) |
const |
|
inlinenodiscard |
the memory of the pages, in bytes
◆ classSize()
| std::size_t ADS::SizeClassAllocator::classSize |
( |
unsigned | c | ) |
|
|
staticnodiscard |
the size of the class \( c \): 8, \( 16 c \) up to 128, and otherwise \( 2^e + j \, 2^{e - 2} \) with \( e = 7 + \lfloor ( c - 9 ) / 4 \rfloor \) and \( j = ( c - 9 ) \bmod 4 + 1 \)
◆ deallocate()
| void ADS::SizeClassAllocator::deallocate |
( |
void * | p, |
|
|
std::size_t | n ) |
|
noexcept |
gives back the block \( p \) of a request of \( n \) bytes, pushed on \( F_c \) for its class \( c \)
- Precondition
- \( p \) was given by allocate( n ) of this allocator
◆ operator=() [1/2]
no copy, since its blocks are in its pages
◆ operator=() [2/2]
the pages of other, which is left empty, after those of this allocator are freed
◆ release()
| void ADS::SizeClassAllocator::release |
( |
void | | ) |
|
|
noexcept |
frees all the pages; the blocks given are no longer valid
◆ sizeClass()
| unsigned ADS::SizeClassAllocator::sizeClass |
( |
std::size_t | n | ) |
|
|
staticnodiscard |
the class of a request of \( n \) bytes: 0 for \( n \le 8 \), \( \lceil n / 16 \rceil \) up to 128, and otherwise, with \( 2^e < n \le 2^{e + 1} \), \( 9 + 4 ( e - 7 ) + \lceil ( n - 2^e ) / 2^{e - 2} \rceil - 1 \)
In pseudocode, with no table and no loop:
\[\class{pseudocode}{\begin{array}{r@{\quad}l}
1 & \textbf{function}\ \mathrm{sizeClass}( n ) \cr
2 & \quad \textbf{if}\ n \le 8\ \textbf{then return}\ 0 \cr
3 & \quad \textbf{if}\ n \le 128\ \textbf{then return}
\ \lceil n / 16 \rceil \cr
4 & \quad e \gets \text{the position of the leading bit of } n - 1
\quad \triangleright\ 2^e < n \le 2^{e + 1} \cr
5 & \quad j \gets \lceil ( n - 2^e ) / 2^{e - 2} \rceil
\quad \triangleright\ \text{an addition and a shift} \cr
6 & \quad \textbf{return}\ 9 + 4 ( e - 7 ) + j - 1
\end{array}}
\]
- Precondition
- \( 1 \le n \le 32768 \)
◆ LARGEST
| std::size_t ADS::SizeClassAllocator::LARGEST = 32768 |
|
staticconstexpr |
the largest request served by the classes
◆ PAGE
| std::size_t ADS::SizeClassAllocator::PAGE = std::size_t( 1 ) << 16 |
|
staticconstexpr |
The documentation for this class was generated from the following file: