|
ADS
Algorithms and Data Structures
|
An allocator for the large blocks of a structure, beyond the book: a request of at least one huge page (2 MB) is aligned to a huge page and rounded up to whole ones, so that a system which backs the large blocks of memory by huge pages (the transparent huge pages of Linux under the policy always, the default of several distributions) can give the block whole huge pages; a smaller request goes to std::allocator. More...
#include <cstddef>#include <cstdlib>#include <limits>#include <memory>#include <new>Go to the source code of this file.
Classes | |
| class | ADS::HugePageAllocator< T > |
| an allocator of objects of type T, on huge pages for the large blocks More... | |
| class | ADS::ZeroPageAllocator< T > |
| an allocator of objects of type T on blocks of 0s, aligned to a huge page if they fill one at least [see the file] More... | |
Namespaces | |
| namespace | ADS |
An allocator for the large blocks of a structure, beyond the book: a request of at least one huge page (2 MB) is aligned to a huge page and rounded up to whole ones, so that a system which backs the large blocks of memory by huge pages (the transparent huge pages of Linux under the policy always, the default of several distributions) can give the block whole huge pages; a smaller request goes to std::allocator.
A structure whose nodes are reached in no useful order, e.g., the trees of a heap, then misses the TLB once per 2 MB instead of once per 4 KB page, and its memory is faulted in a huge page at a time; the price is up to one huge page more of address space per block, which is not touched, so that it costs no memory. Neither the standard library nor Boost nor hwloc can ask the system for huge pages, so that the allocator does not ask: where the policy gives them only on request (madvise), the blocks are only aligned, on pages of 4 KB.
A block of 8 MB or more given back is kept by its thread, up to 16 blocks and 64 MB in all, and given again to the next request of the same size, as the size-class allocators keep their freed spans (Berger et al.; Leijen et al.): its pages are already faulted in, so that a structure that is built and destroyed again and again, or that grows through the same sizes (a vector that doubles, a pool whose blocks double), pays the faults once; a smaller block goes back to the allocator of the system, which serves it again from its heap, and the blocks kept go back to the system when the thread ends. A block given again holds what its last owner left in it, as any memory of an allocator.
The allocators are all equal, and stateless.
ZeroPageAllocator gives blocks of 0s by std::calloc(), a large block aligned to a huge page within one huge page more: the C libraries take a large block from the system as new pages, which the system gives as 0s at their first access and does not write before, so that a structure whose empty state is all 0 bits (the places of PointerTable, which then builds nothing over them [see PointerTable.h]) writes no 0 over its blocks and faults in only the pages it touches. Its blocks are not kept, and a request smaller than a huge page is a plain std::calloc() (for the types whose alignment that of std::max_align_t covers).