Added copyright and license
[libcds.git] / cds / memory / pool_allocator.h
1 /*
2     This file is a part of libcds - Concurrent Data Structures library
3
4     (C) Copyright Maxim Khizhinsky (libcds.dev@gmail.com) 2006-2016
5
6     Source code repo: http://github.com/khizmax/libcds/
7     Download: http://sourceforge.net/projects/libcds/files/
8     
9     Redistribution and use in source and binary forms, with or without
10     modification, are permitted provided that the following conditions are met:
11
12     * Redistributions of source code must retain the above copyright notice, this
13       list of conditions and the following disclaimer.
14
15     * Redistributions in binary form must reproduce the above copyright notice,
16       this list of conditions and the following disclaimer in the documentation
17       and/or other materials provided with the distribution.
18
19     THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20     AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21     IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22     DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
23     FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24     DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25     SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
26     CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
27     OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28     OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.     
29 */
30
31 #ifndef CDSLIB_MEMORY_POOL_ALLOCATOR_H
32 #define CDSLIB_MEMORY_POOL_ALLOCATOR_H
33
34 #include <cds/details/defs.h>
35 #include <utility>
36
37 namespace cds { namespace memory {
38
39     ///@defgroup cds_memory_pool Simple memory pool
40
41     /// Pool allocator adapter
42     /**
43         This class is an adapter for an object pool. It gives \p std::allocator interface
44         for the @ref cds_memory_pool "pool".
45
46         Template arguments:
47         - \p T - value type
48         - \p Accessor - a functor to access to the pool object. The pool has the following interface:
49             \code
50             template <typename T>
51             class pool {
52                 typedef T value_type    ;   // Object type maintained by pool
53                 T * allocate( size_t n )            ;   // Allocate an array of object of type T
54                 void deallocate( T * p, size_t n )  ;   // Deallocate the array p of size n
55             };
56             \endcode
57
58         <b>Usage</b>
59
60             Suppose, we have a pool with interface above. Usually, the pool is a static object:
61             \code
62                 static pool<Foo>     thePool;
63             \endcode
64
65             The \p %pool_allocator gives \p std::allocator interface for the pool.
66             It is needed to declare an <i>accessor</i> functor to access to \p thePool:
67             \code
68                 struct pool_accessor {
69                     typedef typename pool::value_type   value_type;
70
71                     pool& operator()() const
72                     {
73                         return thePool;
74                     }
75                 };
76             \endcode
77
78             Now, <tt>cds::memory::pool_allocator< T, pool_accessor > </tt> can be used instead of \p std::allocator.
79     */
80     template <typename T, typename Accessor>
81     class pool_allocator
82     {
83     //@cond
84     public:
85         typedef Accessor    accessor_type;
86
87         typedef size_t      size_type;
88         typedef ptrdiff_t   difference_type;
89         typedef T*          pointer;
90         typedef const T*    const_pointer;
91         typedef T&          reference;
92         typedef const T&    const_reference;
93         typedef T           value_type;
94
95         template <class U> struct rebind {
96             typedef pool_allocator<U, accessor_type> other;
97         };
98
99     public:
100         pool_allocator() CDS_NOEXCEPT
101         {}
102
103         pool_allocator(const pool_allocator&) CDS_NOEXCEPT
104         {}
105         template <class U> pool_allocator(const pool_allocator<U, accessor_type>&) CDS_NOEXCEPT
106         {}
107         ~pool_allocator()
108         {}
109
110         pointer address(reference x) const CDS_NOEXCEPT
111         {
112             return &x;
113         }
114         const_pointer address(const_reference x) const CDS_NOEXCEPT
115         {
116             return &x;
117         }
118         pointer allocate( size_type n, void const * /*hint*/ = 0)
119         {
120             static_assert( sizeof(value_type) <= sizeof(typename accessor_type::value_type), "Incompatible type" );
121
122             return reinterpret_cast<pointer>( accessor_type()().allocate( n ));
123         }
124         void deallocate(pointer p, size_type n) CDS_NOEXCEPT
125         {
126             accessor_type()().deallocate( reinterpret_cast<typename accessor_type::value_type *>( p ), n );
127         }
128         size_type max_size() const CDS_NOEXCEPT
129         {
130             return size_t(-1) / sizeof(value_type);
131         }
132
133         template <class U, class... Args>
134         void construct(U* p, Args&&... args)
135         {
136             new((void *)p) U( std::forward<Args>(args)...);
137         }
138
139         template <class U>
140         void destroy(U* p)
141         {
142             p->~U();
143         }
144     //@endcond
145     };
146
147 }} // namespace cds::memory
148
149
150 #endif // #ifndef CDSLIB_MEMORY_POOL_ALLOCATOR_H