71#ifndef __ADS_PoolAllocator
72 #define __ADS_PoolAllocator
101[[nodiscard]]
void * poolNew( std::size_t b , std::size_t a );
105void poolDelete(
void * p , std::size_t b , std::size_t a )
noexcept;
115template< std::
size_t M = 256 >
119 static_assert( ( M >= 16 ) && ( M % 16 == 0 ) && ( M <= 4096 ) ,
120 "the largest class is a multiple of 16 bytes, up to 4096" );
124 static constexpr std::size_t
GRAIN = 8;
125 static constexpr std::size_t
ALIGN = 16;
126 static constexpr std::size_t
MAX = M;
131 [[nodiscard]]
static constexpr bool pooled( std::size_t b , std::size_t a ) {
132 return( ( b >= 1 ) && ( b <= M ) && ( a <=
ALIGN ) );
163 template< std::
size_t b , std::
size_t a = alignof( std::max_align_t ) >
169 template< std::
size_t b , std::
size_t a = alignof( std::max_align_t ) >
174 std::size_t a =
alignof( std::max_align_t ) );
180 std::size_t a =
alignof( std::max_align_t ) );
183 [[nodiscard]] std::size_t
size(
void )
const;
186 [[nodiscard]] std::size_t
bytes(
void )
const;
192 [[nodiscard]]
static constexpr std::size_t classOf( std::size_t b ,
194 return( ( a >
GRAIN ? ( b + 15 ) / 16 * 2 : ( b + 7 ) / 8 ) - 1 );
198 [[nodiscard]]
void * take( std::size_t c ) {
199 if(
void * p = f_heap->local( c ) ) [[likely]]
201 return( f_heap->more( c ) );
208 std::unique_ptr< detail::PageHeap< CLASSES > > f_heap;
217template<
class T ,
class R = PoolResource<> >
250 requires requires { R::global(); }
251 : f_r( &R::global() ) { }
256 : f_r( o.resource() ) { }
268 [[nodiscard]] R *
resource(
void )
const noexcept {
return( f_r ); }
273 return( f_r == o.resource() );
290#include "ADS/memory/impl/PoolAllocator.tpp"
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
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