Remove 'unused vars' warnings
[libcds.git] / cds / memory / pool_allocator.h
1 //$$CDS-header$$
2
3 #ifndef __CDS_MEMORY_POOL_ALLOCATOR_H
4 #define __CDS_MEMORY_POOL_ALLOCATOR_H
5
6 #include <cds/details/defs.h>
7 #include <utility>
8
9 namespace cds { namespace memory {
10
11     ///@defgroup cds_memory_pool Simple memory pool
12
13     /// Pool allocator adapter
14     /**
15         This class is an adapter for an object pool. It gives \p std::allocator interface
16         for the @ref cds_memory_pool "pool".
17
18         Template arguments:
19         - \p T - value type
20         - \p Accessor - a functor to access to pool object. The pool has the following interface:
21             \code
22             template <typename T>
23             class pool {
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
27             };
28             \endcode
29
30         <b>Usage</b>
31
32             Suppose, we have got a pool with interface above. Usually, the pool is a static object:
33             \code
34                 static pool<Foo>     thePool;
35             \endcode
36
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:
39             \code
40                 struct pool_accessor {
41                     typedef typename pool::value_type   value_type;
42
43                     pool& operator()() const
44                     {
45                         return thePool;
46                     }
47                 };
48             \endcode
49
50             Now, <tt>cds::memory::pool_allocator< T, pool_accessor > </tt> can be used instead of \p std::allocator.
51     */
52     template <typename T, typename Accessor>
53     class pool_allocator
54     {
55     //@cond
56     public:
57         typedef Accessor    accessor_type;
58
59         typedef size_t      size_type;
60         typedef ptrdiff_t   difference_type;
61         typedef T*          pointer;
62         typedef const T*    const_pointer;
63         typedef T&          reference;
64         typedef const T&    const_reference;
65         typedef T           value_type;
66
67         template <class U> struct rebind {
68             typedef pool_allocator<U, accessor_type> other;
69         };
70
71     public:
72         pool_allocator() CDS_NOEXCEPT
73         {}
74
75         pool_allocator(const pool_allocator&) CDS_NOEXCEPT
76         {}
77         template <class U> pool_allocator(const pool_allocator<U, accessor_type>&) CDS_NOEXCEPT
78         {}
79         ~pool_allocator()
80         {}
81
82         pointer address(reference x) const CDS_NOEXCEPT
83         {
84             return &x;
85         }
86         const_pointer address(const_reference x) const CDS_NOEXCEPT
87         {
88             return &x;
89         }
90         pointer allocate( size_type n, void const * /*hint*/ = 0)
91         {
92             static_assert( sizeof(value_type) <= sizeof(typename accessor_type::value_type), "Incompatible type" );
93
94             return reinterpret_cast<pointer>( accessor_type()().allocate( n ));
95         }
96         void deallocate(pointer p, size_type n) CDS_NOEXCEPT
97         {
98             accessor_type()().deallocate( reinterpret_cast<typename accessor_type::value_type *>( p ), n );
99         }
100         size_type max_size() const CDS_NOEXCEPT
101         {
102             return size_t(-1) / sizeof(value_type);
103         }
104
105         template <class U, class... Args>
106         void construct(U* p, Args&&... args)
107         {
108             new((void *)p) U( std::forward<Args>(args)...);
109         }
110
111         template <class U>
112         void destroy(U* p)
113         {
114             p->~U();
115         }
116     //@endcond
117     };
118
119 }} // namespace cds::memory
120
121
122 #endif // #ifndef __CDS_MEMORY_POOL_ALLOCATOR_H