ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
ObjectPool.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------------*/
2/*--------------------------- File ObjectPool.h ----------------------------*/
3/*--------------------------------------------------------------------------*/
70/*--------------------------------------------------------------------------*/
71/*----------------------------- DEFINITIONS --------------------------------*/
72/*--------------------------------------------------------------------------*/
73
74#ifndef __ADS_ObjectPool
75 #define __ADS_ObjectPool
76/* self-identification: #endif constant */
77
78/*--------------------------------------------------------------------------*/
79/*------------------------------ INCLUDES ----------------------------------*/
80/*--------------------------------------------------------------------------*/
81
82#include "ADS/Contract.h"
83#include "ADS/Portable.h"
85
86#include <algorithm>
87#include <cstddef>
88#include <functional>
89#include <memory>
90#include <new>
91#include <type_traits>
92#include <utility>
93
94/*--------------------------------------------------------------------------*/
95/*--------------------------- NAMESPACE ------------------------------------*/
96/*--------------------------------------------------------------------------*/
97
98namespace ADS
99{
100
101/*--------------------------------------------------------------------------*/
102/*---------------------------- CLASS ObjectPool ----------------------------*/
103/*--------------------------------------------------------------------------*/
104
106template< class T , class A = HugePageAllocator< T > >
108{
109
110/*--------------------------------------------------------------------------*/
111 public:
115 explicit ObjectPool( std::size_t n = std::size_t( 1 ) << 24 ,
116 const A & alloc = A() );
117
119 ObjectPool( const ObjectPool & ) = delete;
121 ObjectPool & operator=( const ObjectPool & ) = delete;
122
125 ObjectPool( ObjectPool && other ) noexcept;
126
129 ObjectPool & operator=( ObjectPool && other ) noexcept;
130
133
135 void swap( ObjectPool & other ) noexcept;
136
150 void adopt( ObjectPool & other ) noexcept;
151
178 template< class... Args >
179 T * make( Args &&... args );
180
193 void free( T * x );
194
197 [[nodiscard]] T * allocate( void );
198
202 void deallocate( T * x );
203
225 void clear( void ) noexcept;
226
230 [[nodiscard]] bool fresh( const T * p ) const;
231
233 [[nodiscard]] std::size_t size( void ) const;
234
236 [[nodiscard]] std::size_t bytes( void ) const;
237
239 using allocator_type = A;
240
242 [[nodiscard]] allocator_type get_allocator( void ) const {
243 return( allocator_type( f_alloc ) );
244 }
245
246/*--------------------------------------------------------------------------*/
247 private:
249 union Slot
250 {
252 Slot * next;
254 alignas( T ) unsigned char object[ sizeof( T ) ];
255 };
256
258 struct Block
259 {
260 Block * prev;
261 std::size_t k;
262 };
263
264 static constexpr std::size_t H =
265 ( sizeof( Block ) + sizeof( Slot ) - 1 ) / sizeof( Slot );
266
267 using SlotAlloc =
268 typename std::allocator_traits< A >::template rebind_alloc< Slot >;
269
271 void release( void ) noexcept;
272
274 Slot * slot( void );
275
277 void refill( void );
278
279 Slot * f_free = nullptr;
280 Slot * f_next = nullptr;
281 Slot * f_end = nullptr;
282 Block * f_last = nullptr;
283 Block * f_reuse = nullptr;
284 std::size_t f_n;
285 std::size_t f_size = 0;
286 ADS_NO_UNIQUE_ADDRESS SlotAlloc f_alloc;
287
288 }; // end( class ObjectPool )
289
290/*--------------------------------------------------------------------------*/
291
292} // end( namespace ADS )
293
294/*--------------------------------------------------------------------------*/
295/*-------------------------- IMPLEMENTATION --------------------------------*/
296/*--------------------------------------------------------------------------*/
297
298#include "ADS/memory/impl/ObjectPool.tpp"
299
300/*--------------------------------------------------------------------------*/
301
302#endif /* ObjectPool.h included */
303
304/*--------------------------------------------------------------------------*/
305/*------------------------- End File ObjectPool.h --------------------------*/
306/*--------------------------------------------------------------------------*/
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...
The primitives of the processor that the compilers spell each in their own way, in one form for all: ...
void free(T *x)
destroys and keeps its slot for the next make()
ObjectPool & operator=(ObjectPool &&other) noexcept
the slots of other, which is left empty, after those of this pool are freed
allocator_type get_allocator(void) const
the allocator of the blocks
Definition ObjectPool.h:242
bool fresh(const T *p) const
whether is a slot given in the current epoch, alive or freed, in time linear in the blocks: false fo...
void adopt(ObjectPool &other) noexcept
the blocks of other taken by this pool, with the objects alive in them, other left empty: the objects...
ObjectPool(ObjectPool &&other) noexcept
the slots of other, which is left empty; its objects stay where they are
std::size_t size(void) const
the number of objects alive
std::size_t bytes(void) const
the memory of the blocks, in bytes
void clear(void) noexcept
all the objects and the free slots dropped, with no destructor called, and the blocks kept for the ne...
A allocator_type
the type of the allocator
Definition ObjectPool.h:239
void swap(ObjectPool &other) noexcept
the slots of this pool and of other exchanged
~ObjectPool()
frees the blocks; the objects still alive are not destroyed
T * allocate(void)
the raw slot of an object, not built, as std::allocator gives it: the caller builds the object in it ...
ObjectPool(const ObjectPool &)=delete
no copy, since its objects are reached by their addresses
T * make(Args &&... args)
a new object built from args, in the slot freed last if any; with no args the object is value-initial...
void deallocate(T *x)
keeps the slot of , whose object is already destroyed (or was never built), for the next make() or al...
ObjectPool(std::size_t n=std::size_t(1)<< 24, const A &alloc=A())
the empty pool whose blocks grow up to slots, taken from alloc
ObjectPool & operator=(const ObjectPool &)=delete
no copy, since its objects are reached by their addresses
#define ADS_NO_UNIQUE_ADDRESS
an empty member that takes no storage, by the attribute that does so (the standard one is accepted bu...
Definition Portable.h:81
Definition ADT.h:96