67#ifndef __ADS_HugePageAllocator
68 #define __ADS_HugePageAllocator
95 static constexpr int N = 16;
96 static constexpr std::size_t BYTES = std::size_t( 64 ) << 20;
97 static constexpr std::size_t LEAST = std::size_t( 8 ) << 20;
106 std::size_t bytes = 0;
109 void * take( std::size_t s ) {
110 for(
int i = n ; i-- > 0 ; )
122 bool keep(
void * q , std::size_t s ) {
123 if( ( s < LEAST ) || ( n == N ) || ( bytes + s > BYTES ) )
138inline bool & hugeCacheGone(
void )
140 thread_local bool gone =
false;
144inline HugeCache::~HugeCache()
146 hugeCacheGone() =
true;
149 ::operator
delete( p[ n ] , std::align_val_t( std::size_t( 1 ) << 21 ) );
154inline HugeCache & hugeCache(
void )
156 thread_local HugeCache c;
177 static constexpr std::size_t
PAGE = std::size_t( 1 ) << 21;
204 if( n > ( std::numeric_limits< std::size_t >::max() -
PAGE ) /
sizeof( T ) )
205 throw( std::bad_array_new_length() );
206 const std::size_t b = n *
sizeof( T );
208 return( std::allocator< T >().
allocate( n ) );
209 const std::size_t r = ( b +
PAGE - 1 ) & ~(
PAGE - 1 );
210 if( ! detail::hugeCacheGone() )
211 if(
void * q = detail::hugeCache().take( r ) )
212 return(
static_cast< T *
>( q ) );
214 static_cast< T *
>( ::operator
new( r , std::align_val_t(
PAGE ) ) ) );
222 const std::size_t b = n *
sizeof( T );
224 std::allocator< T >().deallocate( p , n );
225 else if( detail::hugeCacheGone() ||
226 ! detail::hugeCache().keep( p , ( b +
PAGE - 1 ) & ~(
PAGE - 1 ) ) )
227 ::operator
delete( p , std::align_val_t(
PAGE ) );
250 static_assert(
alignof( T ) <=
alignof( std::max_align_t ) );
259 static constexpr std::size_t
PAGE = std::size_t( 1 ) << 21;
274 ( std::numeric_limits< std::size_t >::max() - 2 *
PAGE ) /
sizeof( T ) )
275 throw( std::bad_array_new_length() );
276 const std::size_t b = n *
sizeof( T );
278 void * q = std::calloc( n ? n : 1 ,
sizeof( T ) );
280 throw( std::bad_alloc() );
281 return(
static_cast< T *
>( q ) );
283 std::size_t r = b +
PAGE +
sizeof(
void * );
284 void *
const q = std::calloc( r , 1 );
286 throw( std::bad_alloc() );
287 void * a =
static_cast< char *
>( q ) +
sizeof(
void * );
288 r -=
sizeof(
void * );
289 std::align(
PAGE , b , a , r );
290 static_cast< void **
>( a )[ -1 ] = q;
291 return(
static_cast< T *
>( a ) );
297 if( n *
sizeof( T ) <
PAGE )
300 std::free(
reinterpret_cast< void **
>( p )[ -1 ] );
bool operator==(const HugePageAllocator< U > &) const noexcept
true: the allocators are all equal
Definition HugePageAllocator.h:232
void deallocate(T *p, std::size_t n) noexcept
the room of objects given back: kept by the thread if it is of 8 MB or more and the cache has room,...
Definition HugePageAllocator.h:221
HugePageAllocator(void)=default
the allocator, which has no state
T * allocate(std::size_t n)
room for objects, on huge pages if they fill one at least
Definition HugePageAllocator.h:203
T value_type
the objects
Definition HugePageAllocator.h:174
static constexpr std::size_t PAGE
the size of a huge page, in bytes
Definition HugePageAllocator.h:177
HugePageAllocator(const HugePageAllocator< U > &) noexcept
the allocator of objects of type T, from that of type U
Definition HugePageAllocator.h:184
void deallocate(T *p, std::size_t n) noexcept
the room of objects given back to the system
Definition HugePageAllocator.h:296
static constexpr std::size_t PAGE
the size of a huge page, in bytes
Definition HugePageAllocator.h:259
T * allocate(std::size_t n)
room for objects, all 0 bits: a block of bytes of std::calloc() if is less than a huge page,...
Definition HugePageAllocator.h:272
bool operator==(const ZeroPageAllocator< U > &) const noexcept
true: the allocators are all equal
Definition HugePageAllocator.h:305
T value_type
the objects
Definition HugePageAllocator.h:253
void is_zeroed
the blocks are all 0 bits [see PointerTable.h]
Definition HugePageAllocator.h:256
ZeroPageAllocator(void)=default
the allocator, which has no state
ZeroPageAllocator(const ZeroPageAllocator< U > &) noexcept
the allocator of objects of type T, from that of type U
Definition HugePageAllocator.h:266