|
ADS
Algorithms and Data Structures
|
An allocator of small blocks by size classes, beyond the book: the scheme of the allocators of today (mimalloc, jemalloc, tcmalloc), for one thread, the size of a block given back by its owner (sized deallocation, as std::allocator does), so that a block needs no header. More...
#include "ADS/Contract.h"#include <bit>#include <cstddef>#include <cstdint>#include <new>#include <utility>#include "ADS/memory/impl/SizeClassAllocator.tpp"Go to the source code of this file.
Classes | |
| class | ADS::SizeClassAllocator |
| an allocator of small blocks by size classes, for one thread More... | |
Namespaces | |
| namespace | ADS |
An allocator of small blocks by size classes, beyond the book: the scheme of the allocators of today (mimalloc, jemalloc, tcmalloc), for one thread, the size of a block given back by its owner (sized deallocation, as std::allocator does), so that a block needs no header.
A request of \( n \) bytes, \( 1 \le n \le 32768 \), goes to the least class not below it: 8, the multiples of 16 up to 128, then four classes per power of 2 (160, 192, 224, 256, 320, ...), i.e., the sizes \( 2^e + j \, 2^{e - 2} \) for \( j = 1 , \ldots , 4 \) and \( 7 \le e \le 14 \), so that past 128 bytes the waste is below \( 2^{e-2} \), i.e., below 20% of the block, at least \( 1.25 \cdot 2^e \), and below 25% of the request, more than \( 2^e \) (below 16 bytes up to 128), and the class is found by a shift and a count of the leading zeros, with no table and no loop. Each class has a free list of the blocks given back, the last one first (whose memory is still in the cache), and a page of 64 KB (the size of the largest block, if larger) whose rest is cut a block at a time; a request takes the first free block, or the next one of the page, or a new page, in \( O( 1 ) \) time, and a release puts the block at the head of the list of its class, in \( O( 1 ) \). The pages go back to the system with the allocator (or by release()): memory is not returned while the allocator lives, as with a pool, but the blocks given back serve any later request of their class. A request of more than 32768 bytes goes to operator new.
The contracts [see Contract.h]: a request is of at least one byte, and deallocate() gets a block of this allocator with the size of its request. The operations are defined in SizeClassAllocator.tpp.