ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
HugePageAllocator.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------------*/
2/*------------------------ File HugePageAllocator.h ------------------------*/
3/*--------------------------------------------------------------------------*/
63/*--------------------------------------------------------------------------*/
64/*----------------------------- DEFINITIONS --------------------------------*/
65/*--------------------------------------------------------------------------*/
66
67#ifndef __ADS_HugePageAllocator
68 #define __ADS_HugePageAllocator
69/* self-identification: #endif constant */
70
71/*--------------------------------------------------------------------------*/
72/*------------------------------ INCLUDES ----------------------------------*/
73/*--------------------------------------------------------------------------*/
74
75#include <cstddef>
76#include <cstdlib>
77#include <limits>
78#include <memory>
79#include <new>
80
81/*--------------------------------------------------------------------------*/
82/*--------------------------- NAMESPACE ------------------------------------*/
83/*--------------------------------------------------------------------------*/
84
85namespace ADS
86{
87
88namespace detail
89{
90
93struct HugeCache
94{
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;
98
100 void * p[ N ];
102 std::size_t r[ N ];
104 int n = 0;
106 std::size_t bytes = 0;
107
109 void * take( std::size_t s ) {
110 for( int i = n ; i-- > 0 ; )
111 if( r[ i ] == s ) {
112 void * q = p[ i ];
113 bytes -= s;
114 p[ i ] = p[ --n ];
115 r[ i ] = r[ n ];
116 return( q );
117 }
118 return( nullptr );
119 }
120
122 bool keep( void * q , std::size_t s ) {
123 if( ( s < LEAST ) || ( n == N ) || ( bytes + s > BYTES ) )
124 return( false );
125 p[ n ] = q;
126 r[ n++ ] = s;
127 bytes += s;
128 return( true );
129 }
130
132 ~HugeCache();
133 };
134
138inline bool & hugeCacheGone( void )
139{
140 thread_local bool gone = false; // no destructor: always there
141 return( gone );
142 }
143
144inline HugeCache::~HugeCache()
145{
146 hugeCacheGone() = true;
147 while( n > 0 ) {
148 --n;
149 ::operator delete( p[ n ] , std::align_val_t( std::size_t( 1 ) << 21 ) );
150 }
151 }
152
154inline HugeCache & hugeCache( void )
155{
156 thread_local HugeCache c;
157 return( c );
158 }
159
160} // end( namespace detail )
161
162/*--------------------------------------------------------------------------*/
163/*----------------------- CLASS HugePageAllocator --------------------------*/
164/*--------------------------------------------------------------------------*/
165
167template< class T >
169{
170
171/*--------------------------------------------------------------------------*/
172 public:
174 using value_type = T;
175
177 static constexpr std::size_t PAGE = std::size_t( 1 ) << 21;
178
180 HugePageAllocator( void ) = default;
181
183 template< class U >
185
203 [[nodiscard]] T * allocate( std::size_t n ) {
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 );
207 if( b < PAGE )
208 return( std::allocator< T >().allocate( n ) );
209 const std::size_t r = ( b + PAGE - 1 ) & ~( PAGE - 1 );
210 if( ! detail::hugeCacheGone() ) // a block kept
211 if( void * q = detail::hugeCache().take( r ) )
212 return( static_cast< T * >( q ) );
213 return(
214 static_cast< T * >( ::operator new( r , std::align_val_t( PAGE ) ) ) );
215 }
216
221 void deallocate( T * p , std::size_t n ) noexcept {
222 const std::size_t b = n * sizeof( T );
223 if( b < PAGE )
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 ) );
228 }
229
231 template< class U >
232 bool operator==( const HugePageAllocator< U > & ) const noexcept {
233 return( true );
234 }
235
236 }; // end( class HugePageAllocator )
237
238/*--------------------------------------------------------------------------*/
239/*----------------------- CLASS ZeroPageAllocator --------------------------*/
240/*--------------------------------------------------------------------------*/
241
244template< class T >
246{
247
248/*--------------------------------------------------------------------------*/
249 public:
250 static_assert( alignof( T ) <= alignof( std::max_align_t ) );
251
253 using value_type = T;
254
256 using is_zeroed = void;
257
259 static constexpr std::size_t PAGE = std::size_t( 1 ) << 21;
260
262 ZeroPageAllocator( void ) = default;
263
265 template< class U >
267
272 [[nodiscard]] T * allocate( std::size_t n ) {
273 if( n >
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 );
277 if( b < PAGE ) {
278 void * q = std::calloc( n ? n : 1 , sizeof( T ) );
279 if( q == nullptr )
280 throw( std::bad_alloc() );
281 return( static_cast< T * >( q ) );
282 }
283 std::size_t r = b + PAGE + sizeof( void * );
284 void * const q = std::calloc( r , 1 );
285 if( q == nullptr )
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 ); // within the PAGE bytes more
290 static_cast< void ** >( a )[ -1 ] = q; // the block, before the place
291 return( static_cast< T * >( a ) );
292 }
293
296 void deallocate( T * p , std::size_t n ) noexcept {
297 if( n * sizeof( T ) < PAGE )
298 std::free( p );
299 else
300 std::free( reinterpret_cast< void ** >( p )[ -1 ] );
301 }
302
304 template< class U >
305 bool operator==( const ZeroPageAllocator< U > & ) const noexcept {
306 return( true );
307 }
308
309 }; // end( class ZeroPageAllocator )
310
311/*--------------------------------------------------------------------------*/
312
313} // end( namespace ADS )
314
315/*--------------------------------------------------------------------------*/
316
317#endif /* HugePageAllocator.h included */
318
319/*--------------------------------------------------------------------------*/
320/*---------------------- End File HugePageAllocator.h ----------------------*/
321/*--------------------------------------------------------------------------*/
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
Definition ADT.h:96