ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
ConcurrentPool.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------------*/
2/*------------------------- File ConcurrentPool.h --------------------------*/
3/*--------------------------------------------------------------------------*/
89/*--------------------------------------------------------------------------*/
90/*----------------------------- DEFINITIONS --------------------------------*/
91/*--------------------------------------------------------------------------*/
92
93#ifndef __ADS_ConcurrentPool
94 #define __ADS_ConcurrentPool
95/* self-identification: #endif constant */
96
97/*--------------------------------------------------------------------------*/
98/*------------------------------ INCLUDES ----------------------------------*/
99/*--------------------------------------------------------------------------*/
100
101#include "ADS/Contract.h"
102#include "ADS/memory/PageHeap.h"
104
105#include <atomic>
106#include <cstddef>
107#include <cstdint>
108#include <new>
109#include <thread>
110#include <vector>
111
112/*--------------------------------------------------------------------------*/
113/*--------------------------- NAMESPACE ------------------------------------*/
114/*--------------------------------------------------------------------------*/
115
116namespace ADS
117{
118
119/*--------------------------------------------------------------------------*/
120/*---------------------- CLASS ConcurrentPoolResource ----------------------*/
121/*--------------------------------------------------------------------------*/
122
125template< std::size_t M = 256 >
127{
128
129 static_assert( ( M >= 16 ) && ( M % 16 == 0 ) && ( M <= 4096 ) ,
130 "the largest class is a multiple of 16 bytes, up to 4096" );
131
132/*--------------------------------------------------------------------------*/
133 public:
134 static constexpr std::size_t GRAIN = 8;
135 static constexpr std::size_t ALIGN = 16;
136 static constexpr std::size_t MAX = M;
137 static constexpr std::size_t CLASSES = M / GRAIN;
139 static constexpr std::size_t SEGMENT = detail::PageHeap< CLASSES >::SEGMENT;
140
143 [[nodiscard]] static constexpr bool pooled( std::size_t b , std::size_t a ) {
144 return( ( b >= 1 ) && ( b <= M ) && ( a <= ALIGN ) );
145 }
146
149
154
158
160 [[nodiscard]] static ConcurrentPoolResource & global( void );
161
187 template< std::size_t b , std::size_t a = alignof( std::max_align_t ) >
188 [[nodiscard]] void * allocate( void );
189
218 template< std::size_t b , std::size_t a = alignof( std::max_align_t ) >
219 void deallocate( void * p );
220
222 [[nodiscard]] void * allocate( std::size_t b ,
223 std::size_t a = alignof( std::max_align_t ) );
224
228 void deallocate( void * p , std::size_t b ,
229 std::size_t a = alignof( std::max_align_t ) );
230
245 void collect( void );
246
248 [[nodiscard]] std::size_t heaps( void ) const;
249
251 [[nodiscard]] std::size_t bytes( void ) const;
252
253/*--------------------------------------------------------------------------*/
254 private:
255 using Pages = detail::PageHeap< CLASSES >;
256 using Free = typename Pages::Free;
257 using Page = typename Pages::Page;
258
260 struct alignas( 64 ) Returns
261 {
263 std::atomic< Free * > top{ nullptr };
264 };
265
266 struct Heap;
267
269 struct Batch
270 {
271 Heap * to = nullptr;
272 Free * head = nullptr;
274 Free * tail = nullptr;
275 std::uint32_t n = 0;
276 };
277
279 static constexpr std::uint32_t BATCH = 32;
280
282 struct Heap
283 {
284 std::thread::id id;
285 Heap * link = nullptr;
286 Pages pages{ this };
287 Batch out[ CLASSES ];
288 Returns returns[ CLASSES ];
289 };
290
292 static void send( Batch & b , std::size_t c );
293
296 [[nodiscard]] static constexpr std::size_t classOf( std::size_t b ,
297 std::size_t a ) {
298 return( ( a > GRAIN ? ( b + 15 ) / 16 * 2 : ( b + 7 ) / 8 ) - 1 );
299 }
300
302 struct Cache
303 {
305 std::uint64_t id = 0;
307 Heap * heap = nullptr;
308 };
309
311 [[nodiscard]] Heap * mine( void );
312
314 Heap * find( void );
315
317 [[nodiscard]] void * take( std::size_t c );
318
320 void * refill( Heap & h , std::size_t c );
321
323 void give( void * p , std::size_t c );
324
325 inline static thread_local Cache s_cache;
326 inline static std::atomic< std::uint64_t > s_ids{ 0 };
327
328 const std::uint64_t f_id;
329 std::atomic< Heap * > f_heaps{ nullptr };
330
331 }; // end( class ConcurrentPoolResource )
332
333/*--------------------------------------------------------------------------*/
334
335} // end( namespace ADS )
336
337/*--------------------------------------------------------------------------*/
338/*-------------------------- IMPLEMENTATION --------------------------------*/
339/*--------------------------------------------------------------------------*/
340
341#include "ADS/memory/impl/ConcurrentPool.tpp"
342
343/*--------------------------------------------------------------------------*/
344
345#endif /* ConcurrentPool.h included */
346
347/*--------------------------------------------------------------------------*/
348/*----------------------- End File ConcurrentPool.h ------------------------*/
349/*--------------------------------------------------------------------------*/
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
Definition ADT.h:96