2 This file is a part of libcds - Concurrent Data Structures library
4 (C) Copyright Maxim Khizhinsky (libcds.dev@gmail.com) 2006-2016
6 Source code repo: http://github.com/khizmax/libcds/
7 Download: http://sourceforge.net/projects/libcds/files/
9 Redistribution and use in source and binary forms, with or without
10 modification, are permitted provided that the following conditions are met:
12 * Redistributions of source code must retain the above copyright notice, this
13 list of conditions and the following disclaimer.
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.
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.
31 #ifndef CDSLIB_MEMORY_POOL_ALLOCATOR_H
32 #define CDSLIB_MEMORY_POOL_ALLOCATOR_H
34 #include <cds/details/defs.h>
37 namespace cds { namespace memory {
39 ///@defgroup cds_memory_pool Simple memory pool
41 /// Pool allocator adapter
43 This class is an adapter for an object pool. It gives \p std::allocator interface
44 for the @ref cds_memory_pool "pool".
48 - \p Accessor - a functor to access to the pool object. The pool has the following interface:
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
60 Suppose, we have a pool with interface above. Usually, the pool is a static object:
62 static pool<Foo> thePool;
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:
68 struct pool_accessor {
69 typedef typename pool::value_type value_type;
71 pool& operator()() const
78 Now, <tt>cds::memory::pool_allocator< T, pool_accessor > </tt> can be used instead of \p std::allocator.
80 template <typename T, typename Accessor>
85 typedef Accessor accessor_type;
87 typedef size_t size_type;
88 typedef ptrdiff_t difference_type;
90 typedef const T* const_pointer;
92 typedef const T& const_reference;
95 template <class U> struct rebind {
96 typedef pool_allocator<U, accessor_type> other;
100 pool_allocator() CDS_NOEXCEPT
103 pool_allocator(const pool_allocator&) CDS_NOEXCEPT
105 template <class U> pool_allocator(const pool_allocator<U, accessor_type>&) CDS_NOEXCEPT
110 pointer address(reference x) const CDS_NOEXCEPT
114 const_pointer address(const_reference x) const CDS_NOEXCEPT
118 pointer allocate( size_type n, void const * /*hint*/ = 0)
120 static_assert( sizeof(value_type) <= sizeof(typename accessor_type::value_type), "Incompatible type" );
122 return reinterpret_cast<pointer>( accessor_type()().allocate( n ));
124 void deallocate(pointer p, size_type n) CDS_NOEXCEPT
126 accessor_type()().deallocate( reinterpret_cast<typename accessor_type::value_type *>( p ), n );
128 size_type max_size() const CDS_NOEXCEPT
130 return size_t(-1) / sizeof(value_type);
133 template <class U, class... Args>
134 void construct(U* p, Args&&... args)
136 new((void *)p) U( std::forward<Args>(args)...);
147 }} // namespace cds::memory
150 #endif // #ifndef CDSLIB_MEMORY_POOL_ALLOCATOR_H