ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
PoolAllocator.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------------*/
2/*-------------------------- File PoolAllocator.h --------------------------*/
3/*--------------------------------------------------------------------------*/
67/*--------------------------------------------------------------------------*/
68/*----------------------------- DEFINITIONS --------------------------------*/
69/*--------------------------------------------------------------------------*/
70
71#ifndef __ADS_PoolAllocator
72 #define __ADS_PoolAllocator
73/* self-identification: #endif constant */
74
75/*--------------------------------------------------------------------------*/
76/*------------------------------ INCLUDES ----------------------------------*/
77/*--------------------------------------------------------------------------*/
78
79#include "ADS/Contract.h"
80#include "ADS/memory/PageHeap.h"
81
82#include <cstddef>
83#include <limits>
84#include <memory>
85#include <new>
86#include <type_traits>
87#include <utility>
88
89/*--------------------------------------------------------------------------*/
90/*--------------------------- NAMESPACE ------------------------------------*/
91/*--------------------------------------------------------------------------*/
92
93namespace ADS
94{
95
96namespace detail
97{
98
101[[nodiscard]] void * poolNew( std::size_t b , std::size_t a );
102
105void poolDelete( void * p , std::size_t b , std::size_t a ) noexcept;
106
107} // end( namespace detail )
108
109/*--------------------------------------------------------------------------*/
110/*--------------------------- CLASS PoolResource ---------------------------*/
111/*--------------------------------------------------------------------------*/
112
115template< std::size_t M = 256 >
117{
118
119 static_assert( ( M >= 16 ) && ( M % 16 == 0 ) && ( M <= 4096 ) ,
120 "the largest class is a multiple of 16 bytes, up to 4096" );
121
122/*--------------------------------------------------------------------------*/
123 public:
124 static constexpr std::size_t GRAIN = 8;
125 static constexpr std::size_t ALIGN = 16;
126 static constexpr std::size_t MAX = M;
127 static constexpr std::size_t CLASSES = M / GRAIN;
128
131 [[nodiscard]] static constexpr bool pooled( std::size_t b , std::size_t a ) {
132 return( ( b >= 1 ) && ( b <= M ) && ( a <= ALIGN ) );
133 }
134
137
139 PoolResource( const PoolResource & ) = delete;
141 PoolResource & operator=( const PoolResource & ) = delete;
142
144 ~PoolResource() = default;
145
163 template< std::size_t b , std::size_t a = alignof( std::max_align_t ) >
164 [[nodiscard]] void * allocate( void );
165
169 template< std::size_t b , std::size_t a = alignof( std::max_align_t ) >
170 void deallocate( void * p );
171
173 [[nodiscard]] void * allocate( std::size_t b ,
174 std::size_t a = alignof( std::max_align_t ) );
175
179 void deallocate( void * p , std::size_t b ,
180 std::size_t a = alignof( std::max_align_t ) );
181
183 [[nodiscard]] std::size_t size( void ) const;
184
186 [[nodiscard]] std::size_t bytes( void ) const;
187
188/*--------------------------------------------------------------------------*/
189 private:
192 [[nodiscard]] static constexpr std::size_t classOf( std::size_t b ,
193 std::size_t a ) {
194 return( ( a > GRAIN ? ( b + 15 ) / 16 * 2 : ( b + 7 ) / 8 ) - 1 );
195 }
196
198 [[nodiscard]] void * take( std::size_t c ) {
199 if( void * p = f_heap->local( c ) ) [[likely]]
200 return( p );
201 return( f_heap->more( c ) );
202 }
203
208 std::unique_ptr< detail::PageHeap< CLASSES > > f_heap;
209
210 }; // end( class PoolResource )
211
212/*--------------------------------------------------------------------------*/
213/*--------------------------- CLASS PoolAllocator --------------------------*/
214/*--------------------------------------------------------------------------*/
215
217template< class T , class R = PoolResource<> >
219{
220
221/*--------------------------------------------------------------------------*/
222 public:
224 using value_type = T;
226 using size_type = std::size_t;
228 using difference_type = std::ptrdiff_t;
234 using propagate_on_container_swap = std::true_type;
236 using is_always_equal = std::false_type;
237
239 template< class U >
240 struct rebind
241 {
243 };
244
246 PoolAllocator( R & r ) noexcept : f_r( &r ) { }
247
249 PoolAllocator( void ) noexcept
250 requires requires { R::global(); }
251 : f_r( &R::global() ) { }
252
254 template< class U >
256 : f_r( o.resource() ) { }
257
260 [[nodiscard]] T * allocate( std::size_t n );
261
265 void deallocate( T * p , std::size_t n );
266
268 [[nodiscard]] R * resource( void ) const noexcept { return( f_r ); }
269
271 template< class U >
272 bool operator==( const PoolAllocator< U , R > & o ) const noexcept {
273 return( f_r == o.resource() );
274 }
275
276/*--------------------------------------------------------------------------*/
277 private:
278 R * f_r;
279
280 }; // end( class PoolAllocator )
281
282/*--------------------------------------------------------------------------*/
283
284} // end( namespace ADS )
285
286/*--------------------------------------------------------------------------*/
287/*-------------------------- IMPLEMENTATION --------------------------------*/
288/*--------------------------------------------------------------------------*/
289
290#include "ADS/memory/impl/PoolAllocator.tpp"
291
292/*--------------------------------------------------------------------------*/
293
294#endif /* PoolAllocator.h included */
295
296/*--------------------------------------------------------------------------*/
297/*------------------------ End File PoolAllocator.h ------------------------*/
298/*--------------------------------------------------------------------------*/
The contracts of ADS: the preconditions, the postconditions and the invariants of the operations,...
The heap of pages of size classes under PoolResource [see PoolAllocator.h] and ConcurrentPoolResource...
std::true_type propagate_on_container_swap
the allocator swapped with the container
Definition PoolAllocator.h:234
std::true_type propagate_on_container_copy_assignment
the allocator copied with the container
Definition PoolAllocator.h:230
PoolAllocator(const PoolAllocator< U, R > &o) noexcept
the allocator of the resource of , for objects of type T
Definition PoolAllocator.h:255
PoolAllocator(R &r) noexcept
the allocator of the resource r
Definition PoolAllocator.h:246
std::false_type is_always_equal
two allocators are equal only if they share their resource
Definition PoolAllocator.h:236
std::true_type propagate_on_container_move_assignment
the allocator moved with the container
Definition PoolAllocator.h:232
R * resource(void) const noexcept
the resource
Definition PoolAllocator.h:268
PoolAllocator(void) noexcept
the allocator of the default resource of R, if it has one
Definition PoolAllocator.h:249
void deallocate(T *p, std::size_t n)
the room of objects given back
std::size_t size_type
a number of objects
Definition PoolAllocator.h:226
T * allocate(std::size_t n)
room for objects, the class found at compile time when
bool operator==(const PoolAllocator< U, R > &o) const noexcept
whether the two share their resource
Definition PoolAllocator.h:272
T value_type
the objects
Definition PoolAllocator.h:224
std::ptrdiff_t difference_type
a distance in objects
Definition PoolAllocator.h:228
void * allocate(void)
bytes aligned to , and known at compile time
std::size_t bytes(void) const
the memory of the blocks of the classes, in bytes
PoolResource(const PoolResource &)=delete
no copy, since its objects and allocators point to it
~PoolResource()=default
frees the blocks; the objects still alive are not destroyed
static constexpr std::size_t CLASSES
the classes
Definition PoolAllocator.h:127
std::size_t size(void) const
the number of objects alive in the classes
PoolResource(void)
the empty resource
static constexpr std::size_t ALIGN
the largest alignment
Definition PoolAllocator.h:125
void deallocate(void *p, std::size_t b, std::size_t a=alignof(std::max_align_t))
the memory of allocate( b , a ) given back, to the free list of its page
static constexpr std::size_t MAX
the largest class
Definition PoolAllocator.h:126
void * allocate(std::size_t b, std::size_t a=alignof(std::max_align_t))
bytes aligned to , as allocate< b , a >() above
void deallocate(void *p)
the memory of allocate< b , a >() given back, to the free list of its page
static constexpr bool pooled(std::size_t b, std::size_t a)
true if a request of bytes aligned to takes a class, i.e., if and
Definition PoolAllocator.h:131
PoolResource & operator=(const PoolResource &)=delete
no copy, since its objects and allocators point to it
static constexpr std::size_t GRAIN
the step of a class
Definition PoolAllocator.h:124
Definition ADT.h:96
the allocator of the objects of type U over the same resource
Definition PoolAllocator.h:241
PoolAllocator< U, R > other
the allocator of U
Definition PoolAllocator.h:242