|
ADS
Algorithms and Data Structures
|
The heap of pages of size classes under PoolResource [see PoolAllocator.h] and ConcurrentPoolResource [see ConcurrentPool.h], beyond the book: slots of \( ( c + 1 ) g \) bytes, \( g = 8 \), for the classes \( c = 0 , \dots , C - 1 \), with the free lists sharded by page. More...
#include "ADS/Contract.h"#include "ADS/memory/HugePageAllocator.h"#include <cstddef>#include <cstdint>#include <new>#include <vector>Go to the source code of this file.
Namespaces | |
| namespace | ADS |
The heap of pages of size classes under PoolResource [see PoolAllocator.h] and ConcurrentPoolResource [see ConcurrentPool.h], beyond the book: slots of \( ( c + 1 ) g \) bytes, \( g = 8 \), for the classes \( c = 0 , \dots , C - 1 \), with the free lists sharded by page.
The heap takes segments of \( 2^{21} \) bytes, aligned to their size and advised for the huge pages [see HugePageAllocator.h], and cuts them into pages of \( 2^{16} \) bytes; a page holds the slots of one class after a header of 64 bytes, so that the page of a slot \( p \) is \( p \wedge \neg ( 2^{16} - 1 ) \). Each page has a free list of its own, and the slots never given yet are cut from it in the order of their addresses. A class allocates from its current page, the pop of its free list or the bump of its pointer; when the page is exhausted, the class moves to the next page of its list of the pages with free slots, or to a new page, and the exhausted page leaves the list until a slot of it is freed, which puts it back. A free is the push on the list of the page of the slot, plus the test of a flag; a page counts its slots given and not freed, and the free of its last one empties its list and puts its pointer back at its first slot, so that a page emptied is cut again in the order of its addresses, by the bump. Both operations cost \( O( 1 ) \) time, amortized over the slots of a page for the change of page, and a page of the class \( c \) wastes less than \( ( c + 1 ) g \) bytes at its end besides its header.
So the objects made one after another come from one page, even after frees in a scrambled order: a single free list, as that of ObjectPool [see ObjectPool.h], gives back the slots in the order of the frees, scattered over all the memory, while the lists by page give them back a page at a time, so that the nodes of a container built again are close to each other and the walks over them miss the cache less. The memory goes back to the system only with the heap.
The contracts [see Contract.h]: a slot freed belongs to the heap and is freed once. The operations are defined at the end of the header.