3 #ifndef CDSLIB_MEMORY_POOL_ALLOCATOR_H
4 #define CDSLIB_MEMORY_POOL_ALLOCATOR_H
6 #include <cds/details/defs.h>
9 namespace cds { namespace memory {
11 ///@defgroup cds_memory_pool Simple memory pool
13 /// Pool allocator adapter
15 This class is an adapter for an object pool. It gives \p std::allocator interface
16 for the @ref cds_memory_pool "pool".
20 - \p Accessor - a functor to access to pool object. The pool has the following interface:
24 typedef T value_type ; // Object type maintained by pool
25 T * allocate( size_t n ) ; // Allocate an array of object of type T
26 void deallocate( T * p, size_t n ) ; // Deallocate the array p of size n
32 Suppose, we have got a pool with interface above. Usually, the pool is a static object:
34 static pool<Foo> thePool;
37 The \p %pool_allocator gives \p std::allocator interface for the pool.
38 It is needed to declare an <i>accessor</i> functor to access to \p thePool:
40 struct pool_accessor {
41 typedef typename pool::value_type value_type;
43 pool& operator()() const
50 Now, <tt>cds::memory::pool_allocator< T, pool_accessor > </tt> can be used instead of \p std::allocator.
52 template <typename T, typename Accessor>
57 typedef Accessor accessor_type;
59 typedef size_t size_type;
60 typedef ptrdiff_t difference_type;
62 typedef const T* const_pointer;
64 typedef const T& const_reference;
67 template <class U> struct rebind {
68 typedef pool_allocator<U, accessor_type> other;
72 pool_allocator() CDS_NOEXCEPT
75 pool_allocator(const pool_allocator&) CDS_NOEXCEPT
77 template <class U> pool_allocator(const pool_allocator<U, accessor_type>&) CDS_NOEXCEPT
82 pointer address(reference x) const CDS_NOEXCEPT
86 const_pointer address(const_reference x) const CDS_NOEXCEPT
90 pointer allocate( size_type n, void const * /*hint*/ = 0)
92 static_assert( sizeof(value_type) <= sizeof(typename accessor_type::value_type), "Incompatible type" );
94 return reinterpret_cast<pointer>( accessor_type()().allocate( n ));
96 void deallocate(pointer p, size_type n) CDS_NOEXCEPT
98 accessor_type()().deallocate( reinterpret_cast<typename accessor_type::value_type *>( p ), n );
100 size_type max_size() const CDS_NOEXCEPT
102 return size_t(-1) / sizeof(value_type);
105 template <class U, class... Args>
106 void construct(U* p, Args&&... args)
108 new((void *)p) U( std::forward<Args>(args)...);
119 }} // namespace cds::memory
122 #endif // #ifndef CDSLIB_MEMORY_POOL_ALLOCATOR_H