ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
PageHeap.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------------*/
2/*---------------------------- File PageHeap.h -----------------------------*/
3/*--------------------------------------------------------------------------*/
59/*--------------------------------------------------------------------------*/
60/*----------------------------- DEFINITIONS --------------------------------*/
61/*--------------------------------------------------------------------------*/
62
63#ifndef __ADS_PageHeap
64 #define __ADS_PageHeap
65/* self-identification: #endif constant */
66
67/*--------------------------------------------------------------------------*/
68/*------------------------------ INCLUDES ----------------------------------*/
69/*--------------------------------------------------------------------------*/
70
71#include "ADS/Contract.h"
73
74#include <cstddef>
75#include <cstdint>
76#include <new>
77#include <vector>
78
79/*--------------------------------------------------------------------------*/
80/*--------------------------- NAMESPACE ------------------------------------*/
81/*--------------------------------------------------------------------------*/
82
83namespace ADS::detail
84{
85
86/*--------------------------------------------------------------------------*/
87/*---------------------------- CLASS PageHeap ------------------------------*/
88/*--------------------------------------------------------------------------*/
89
91template< std::size_t C >
92class alignas( 64 ) PageHeap
93{
94
95/*--------------------------------------------------------------------------*/
96 public:
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;
103
104 static_assert( C * GRAIN <= ( PAGE - HEADER ) / 8 ,
105 "a page holds at least 8 slots of the largest class" );
106
108 struct Free
109 {
111 Free * next;
112 };
113
115 struct Page
116 {
117 Free * free;
118 char * next;
119 char * end;
120 Page * link;
121 void * owner;
122 std::uint32_t c;
123 std::uint32_t used;
124 bool listed;
125 };
126
127 static_assert( sizeof( Page ) <= HEADER );
128
130 explicit PageHeap( void * owner = nullptr );
131
133 PageHeap( const PageHeap & ) = delete;
135 PageHeap & operator=( const PageHeap & ) = delete;
136
138 ~PageHeap();
139
141 [[nodiscard]] static Page * pageOf( void * p ) {
142 return( reinterpret_cast< Page * >(
143 reinterpret_cast< std::uintptr_t >( p ) & ~std::uintptr_t( PAGE - 1 ) ) );
144 }
145
163 [[nodiscard]] void * local( std::size_t c ) {
164 Page * const p = f_class[ c ].current;
165 if( Free * const f = p->free ) [[likely]] {
166 p->free = f->next;
167 ++p->used;
168 ++f_live;
169 return( f );
170 }
171 if( p->next != p->end ) { // a slot never given
172 void * const s = p->next;
173 p->next += ( c + 1 ) * GRAIN;
174 ++p->used;
175 ++f_live;
176 return( s );
177 }
178 return( nullptr );
179 }
180
199 [[nodiscard]] void * more( std::size_t c );
200
217 void give( Page * g , void * p ) {
218 --f_live;
219 if( --g->used == 0 ) { // empty: cut again from its first slot
220 g->free = nullptr;
221 g->next = reinterpret_cast< char * >( g ) + HEADER;
222 }
223 else {
224 Free * const f = static_cast< Free * >( p );
225 f->next = g->free;
226 g->free = f;
227 }
228 if( ! g->listed ) [[unlikely]] { // back among the pages to use
229 g->listed = true;
230 g->link = f_class[ g->c ].avail;
231 f_class[ g->c ].avail = g;
232 }
233 }
234
236 [[nodiscard]] std::size_t live( void ) const { return( f_live ); }
237
239 [[nodiscard]] std::size_t bytes( void ) const {
240 return( f_segments.size() * SEGMENT );
241 }
242
243/*--------------------------------------------------------------------------*/
244 private:
246 struct Class
247 {
248 Page * current;
249 Page * avail = nullptr;
250 };
251
253 inline static Page s_none{ nullptr , nullptr , nullptr , nullptr ,
254 nullptr , 0 , 0 , true };
255
257 Page * fresh( std::size_t c );
258
259 Class f_class[ C ];
260 char * f_page = nullptr;
261 char * f_last = nullptr;
262 std::size_t f_live = 0;
263 void * f_owner;
264 std::vector< char * > f_segments;
265
266 }; // end( class PageHeap )
267
268/*--------------------------------------------------------------------------*/
269/*-------------------------- IMPLEMENTATION --------------------------------*/
270/*--------------------------------------------------------------------------*/
271
272template< std::size_t C >
273PageHeap< C >::PageHeap( void * owner ) : f_owner( owner )
274{
275 for( Class & k : f_class )
276 k.current = &s_none;
277 }
278
279template< std::size_t C >
280PageHeap< C >::~PageHeap()
281{
282 for( char * s : f_segments )
283 HugePageAllocator< char >().deallocate( s , SEGMENT );
284 }
285
286template< std::size_t C >
287typename PageHeap< C >::Page * PageHeap< C >::fresh( std::size_t c )
288{
289 if( f_page == f_last ) { // a new segment
290 if( f_segments.size() == f_segments.capacity() ) // its place first,
291 f_segments.reserve( 2 * f_segments.size() + 1 ); // doubling the room
292 char * s = HugePageAllocator< char >().allocate( SEGMENT );
293 f_segments.push_back( s );
294 f_page = s;
295 f_last = s + SEGMENT;
296 }
297 char * const p = f_page;
298 f_page += 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 ,
302 nullptr ,
303 f_owner , std::uint32_t( c ) , 0 , true } );
304 }
305
306template< std::size_t C >
307void * PageHeap< C >::more( std::size_t c )
308{
309 Class & k = f_class[ c ];
310 for( ; ; ) {
311 Page * const p = k.current;
312 if( Free * f = p->free ) {
313 p->free = f->next;
314 ++p->used;
315 ++f_live;
316 return( f );
317 }
318 if( p->next != p->end ) { // a slot never given
319 void * s = p->next;
320 p->next += ( c + 1 ) * GRAIN;
321 ++p->used;
322 ++f_live;
323 return( s );
324 }
325 if( p != &s_none ) // exhausted: off the list
326 p->listed = false;
327 if( Page * q = k.avail ) {
328 k.avail = q->link;
329 k.current = q;
330 }
331 else
332 k.current = fresh( c );
333 }
334 }
335
336/*--------------------------------------------------------------------------*/
337
338} // end( namespace ADS::detail )
339
340/*--------------------------------------------------------------------------*/
341
342#endif /* PageHeap.h included */
343
344/*--------------------------------------------------------------------------*/
345/*---------------------------- End File PageHeap.h -------------------------*/
346/*--------------------------------------------------------------------------*/
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...