64 #define __ADS_PageHeap
91template< std::
size_t C >
92class alignas( 64 ) PageHeap
97 static constexpr std::size_t GRAIN = 8;
99 static constexpr std::size_t PAGE = std::size_t( 1 ) << 16;
101 static constexpr std::size_t SEGMENT = std::size_t( 1 ) << 21;
102 static constexpr std::size_t HEADER = 64;
104 static_assert( C * GRAIN <= ( PAGE - HEADER ) / 8 ,
105 "a page holds at least 8 slots of the largest class" );
127 static_assert(
sizeof( Page ) <= HEADER );
130 explicit PageHeap(
void * owner =
nullptr );
133 PageHeap(
const PageHeap & ) =
delete;
135 PageHeap & operator=(
const PageHeap & ) =
delete;
141 [[nodiscard]]
static Page * pageOf(
void * p ) {
142 return(
reinterpret_cast< Page *
>(
143 reinterpret_cast< std::uintptr_t
>( p ) & ~std::uintptr_t( PAGE - 1 ) ) );
163 [[nodiscard]]
void * local( std::size_t c ) {
164 Page *
const p = f_class[ c ].current;
165 if( Free *
const f = p->free ) [[likely]] {
171 if( p->next != p->end ) {
172 void *
const s = p->next;
173 p->next += ( c + 1 ) * GRAIN;
199 [[nodiscard]]
void * more( std::size_t c );
217 void give( Page * g ,
void * p ) {
219 if( --g->used == 0 ) {
221 g->next =
reinterpret_cast< char *
>( g ) + HEADER;
224 Free *
const f =
static_cast< Free *
>( p );
228 if( ! g->listed ) [[unlikely]] {
230 g->link = f_class[ g->c ].avail;
231 f_class[ g->c ].avail = g;
236 [[nodiscard]] std::size_t live(
void )
const {
return( f_live ); }
239 [[nodiscard]] std::size_t bytes(
void )
const {
240 return( f_segments.size() * SEGMENT );
249 Page * avail =
nullptr;
253 inline static Page s_none{ nullptr , nullptr , nullptr , nullptr ,
254 nullptr , 0 , 0 ,
true };
257 Page * fresh( std::size_t c );
260 char * f_page =
nullptr;
261 char * f_last =
nullptr;
262 std::size_t f_live = 0;
264 std::vector< char * > f_segments;
272template< std::
size_t C >
273PageHeap< C >::PageHeap(
void * owner ) : f_owner( owner )
275 for( Class & k : f_class )
279template< std::
size_t C >
280PageHeap< C >::~PageHeap()
282 for(
char * s : f_segments )
283 HugePageAllocator< char >().deallocate( s , SEGMENT );
286template< std::
size_t C >
287typename PageHeap< C >::Page * PageHeap< C >::fresh( std::size_t c )
289 if( f_page == f_last ) {
290 if( f_segments.size() == f_segments.capacity() )
291 f_segments.reserve( 2 * f_segments.size() + 1 );
292 char * s = HugePageAllocator< char >().allocate( SEGMENT );
293 f_segments.push_back( s );
295 f_last = s + SEGMENT;
297 char *
const p = f_page;
299 const std::size_t b = ( c + 1 ) * GRAIN;
300 return( ::new(
static_cast< void *
>( p ) ) Page{
301 nullptr , p + HEADER , p + HEADER + ( ( PAGE - HEADER ) / b ) * b ,
303 f_owner , std::uint32_t( c ) , 0 ,
true } );
306template< std::
size_t C >
307void * PageHeap< C >::more( std::size_t c )
309 Class & k = f_class[ c ];
311 Page *
const p = k.current;
312 if( Free * f = p->free ) {
318 if( p->next != p->end ) {
320 p->next += ( c + 1 ) * GRAIN;
327 if( Page * q = k.avail ) {
332 k.current = fresh( c );
The contracts of ADS: the preconditions, the postconditions and the invariants of the operations,...
An allocator for the large blocks of a structure, beyond the book: a request of at least one huge pag...