93#ifndef __ADS_ConcurrentPool
94 #define __ADS_ConcurrentPool
125template< std::
size_t M = 256 >
129 static_assert( ( M >= 16 ) && ( M % 16 == 0 ) && ( M <= 4096 ) ,
130 "the largest class is a multiple of 16 bytes, up to 4096" );
134 static constexpr std::size_t
GRAIN = 8;
135 static constexpr std::size_t
ALIGN = 16;
136 static constexpr std::size_t
MAX = M;
139 static constexpr std::size_t
SEGMENT = detail::PageHeap< CLASSES >::SEGMENT;
143 [[nodiscard]]
static constexpr bool pooled( std::size_t b , std::size_t a ) {
144 return( ( b >= 1 ) && ( b <= M ) && ( a <=
ALIGN ) );
187 template< std::
size_t b , std::
size_t a = alignof( std::max_align_t ) >
218 template< std::
size_t b , std::
size_t a = alignof( std::max_align_t ) >
223 std::size_t a =
alignof( std::max_align_t ) );
229 std::size_t a =
alignof( std::max_align_t ) );
248 [[nodiscard]] std::size_t
heaps(
void )
const;
251 [[nodiscard]] std::size_t
bytes(
void )
const;
255 using Pages = detail::PageHeap< CLASSES >;
256 using Free =
typename Pages::Free;
257 using Page =
typename Pages::Page;
260 struct alignas( 64 ) Returns
263 std::atomic< Free * > top{
nullptr };
272 Free * head =
nullptr;
274 Free * tail =
nullptr;
279 static constexpr std::uint32_t BATCH = 32;
285 Heap * link =
nullptr;
292 static void send( Batch & b , std::size_t c );
296 [[nodiscard]]
static constexpr std::size_t classOf( std::size_t b ,
298 return( ( a >
GRAIN ? ( b + 15 ) / 16 * 2 : ( b + 7 ) / 8 ) - 1 );
305 std::uint64_t
id = 0;
307 Heap * heap =
nullptr;
311 [[nodiscard]] Heap * mine(
void );
317 [[nodiscard]]
void * take( std::size_t c );
320 void * refill( Heap & h , std::size_t c );
323 void give(
void * p , std::size_t c );
325 inline static thread_local Cache s_cache;
326 inline static std::atomic< std::uint64_t > s_ids{ 0 };
328 const std::uint64_t f_id;
329 std::atomic< Heap * > f_heaps{
nullptr };
341#include "ADS/memory/impl/ConcurrentPool.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...
A standard allocator over pools of size classes, beyond the book, so that the containers of the stand...
static constexpr std::size_t CLASSES
the classes
Definition ConcurrentPool.h:137
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 ConcurrentPool.h:143
ConcurrentPoolResource & operator=(const ConcurrentPoolResource &)=delete
no copy, since its objects and allocators point to it
static constexpr std::size_t SEGMENT
the bytes of a segment of the heaps
Definition ConcurrentPool.h:139
ConcurrentPoolResource(const ConcurrentPoolResource &)=delete
no copy, since its objects and allocators point to it
ConcurrentPoolResource(void)
the empty resource, of a new identity
static constexpr std::size_t ALIGN
the largest alignment
Definition ConcurrentPool.h:135
void deallocate(void *p)
the memory of allocate< b , a >() given back, by any thread
~ConcurrentPoolResource()
frees the segments and the heaps; the objects still alive are not destroyed
static constexpr std::size_t MAX
the largest class
Definition ConcurrentPool.h:136
static ConcurrentPoolResource & global(void)
the resource of the whole program, never destroyed
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, by any thread, as deallocate< b , a >() above
void collect(void)
the batches of the calling thread go back to their heaps, and the slots that other threads freed into...
std::size_t bytes(void) const
the memory of the segments, in bytes
static constexpr std::size_t GRAIN
the step of a class
Definition ConcurrentPool.h:134
void * allocate(std::size_t b, std::size_t a=alignof(std::max_align_t))
bytes aligned to , as allocate< b , a >() above
void * allocate(void)
bytes aligned to , and known at compile time
std::size_t heaps(void) const
the number of heaps, one per thread that used the resource