Replace cds::ref/boost::ref with std::ref, remove cds::unref and cds/ref.h header
[libcds.git] / cds / details / allocator.h
1 //$$CDS-header$$
2
3 #ifndef __CDS_DETAILS_ALLOCATOR_H
4 #define __CDS_DETAILS_ALLOCATOR_H
5
6 /*
7     Allocator class for the library. Supports allocating and constructing of objects
8
9     Editions:
10         2008.03.08    Maxim.Khiszinsky    Created
11 */
12
13 #include <type_traits>
14 #include <memory>
15 #include <cds/details/defs.h>
16 #include <cds/user_setup/allocator.h>
17 #include <boost/type_traits/has_trivial_destructor.hpp>
18
19 namespace cds {
20     namespace details {
21
22         /// Extends \p std::allocator interface to provide semantics like operator \p new and \p delete
23         /**
24             The class is the wrapper around underlying \p Alloc class.
25             \p Alloc provides the \p std::allocator interface.
26         */
27         template <typename T, class Alloc = CDS_DEFAULT_ALLOCATOR >
28         class Allocator
29             : public std::conditional<
30                         std::is_same< T, typename Alloc::value_type>::value
31                         , Alloc
32                         , typename Alloc::template rebind<T>::other
33                      >::type
34         {
35         public:
36             /// Underlying allocator type
37             typedef typename std::conditional<
38                 std::is_same< T, typename Alloc::value_type>::value
39                 , Alloc
40                 , typename Alloc::template rebind<T>::other
41             >::type allocator_type;
42
43             /// Element type
44             typedef T   value_type;
45
46             /// Analogue of operator new T(\p src... )
47             template <typename... S>
48             value_type *  New( S const&... src )
49             {
50                 return Construct( allocator_type::allocate(1), src... );
51             }
52
53             /// Analogue of <tt>operator new T( std::forward<Args>(args)... )</tt> (move semantics)
54             template <typename... Args>
55             value_type * MoveNew( Args&&... args )
56             {
57                 return MoveConstruct( allocator_type::allocate(1), std::forward<Args>(args)... );
58             }
59
60             /// Analogue of operator new T[\p nCount ]
61             value_type * NewArray( size_t nCount )
62             {
63                 value_type * p = allocator_type::allocate( nCount );
64                 for ( size_t i = 0; i < nCount; ++i )
65                     Construct( p + i );
66                 return p;
67             }
68
69             /// Analogue of operator new T[\p nCount ].
70             /**
71                 Each item of array of type T is initialized by parameter \p src: T( src )
72             */
73             template <typename S>
74             value_type * NewArray( size_t nCount, S const& src )
75             {
76                 value_type * p = allocator_type::allocate( nCount );
77                 for ( size_t i = 0; i < nCount; ++i )
78                     Construct( p + i, src );
79                 return p;
80             }
81
82 #       if CDS_COMPILER == CDS_COMPILER_INTEL
83             //@cond
84             value_type * NewBlock( size_t nSize )
85             {
86                 return Construct( heap_alloc( nSize ));
87             }
88             //@endcond
89 #       endif
90             /// Allocates block of memory of size at least \p nSize bytes.
91             /**
92                 Internally, the block is allocated as an array of \p void* pointers,
93                 then \p Construct() method is called to initialize \p T.
94
95                 Precondition: <tt> nSize >= sizeof(T) </tt>
96             */
97             template <typename... S>
98             value_type *  NewBlock( size_t nSize, S const&... src )
99             {
100                 return Construct( heap_alloc( nSize ), src... );
101             }
102
103             /// Analogue of operator delete
104             void Delete( value_type * p )
105             {
106                 allocator_type::destroy( p );
107                 allocator_type::deallocate( p, 1 );
108             }
109
110             /// Analogue of operator delete []
111             void Delete( value_type * p, size_t nCount )
112             {
113                  for ( size_t i = 0; i < nCount; ++i )
114                      allocator_type::destroy( p + i );
115                 allocator_type::deallocate( p, nCount );
116             }
117
118 #       if CDS_COMPILER == CDS_COMPILER_INTEL
119             //@cond
120             value_type * Construct( void * p )
121             {
122                 return new( p ) value_type;
123             }
124             //@endcond
125 #       endif
126             /// Analogue of placement operator new( \p p ) T( src... )
127             template <typename... S>
128             value_type * Construct( void * p, S const&... src )
129             {
130                 return new( p ) value_type( src... );
131             }
132
133             /// Analogue of placement <tt>operator new( p ) T( std::forward<Args>(args)... )</tt>
134             template <typename... Args>
135             value_type * MoveConstruct( void * p, Args&&... args )
136             {
137                 return new( p ) value_type( std::forward<Args>(args)... );
138             }
139
140             /// Rebinds allocator to other type \p Q instead of \p T
141             template <typename Q>
142             struct rebind {
143                 typedef Allocator< Q, typename Alloc::template rebind<Q>::other >    other ; ///< Rebinding result
144             };
145
146         private:
147             //@cond
148             void * heap_alloc( size_t nByteSize )
149             {
150                 assert( nByteSize >= sizeof(value_type));
151
152                 size_t const nPtrSize = ( nByteSize + sizeof(void *) - 1 ) / sizeof(void *);
153                 typedef typename allocator_type::template rebind< void * >::other void_allocator;
154                 return void_allocator().allocate( nPtrSize );
155             }
156             //@endcond
157         };
158
159         //@cond
160         namespace {
161             template <class T>
162             static inline void impl_call_dtor(T* p, boost::false_type const&)
163             {
164                 p->T::~T();
165             }
166
167             template <class T>
168             static inline void impl_call_dtor(T* p, boost::true_type const&)
169             {}
170         }
171         //@endcond
172
173         /// Helper function to call destructor of type T
174         /**
175             This function is empty for the type T that has trivial destructor.
176         */
177         template <class T>
178         static inline void call_dtor( T* p )
179         {
180             impl_call_dtor( p, ::boost::has_trivial_destructor<T>() );
181         }
182
183
184         /// Deferral removing of the object of type \p T. Helper class
185         template <typename T, typename Alloc = CDS_DEFAULT_ALLOCATOR>
186         struct deferral_deleter {
187             typedef T           type            ; ///< Type
188             typedef Alloc       allocator_type  ; ///< Allocator for removing
189
190             /// Frees the object \p p
191             /**
192                 Caveats: this function uses temporary object of type \ref cds::details::Allocator to free the node \p p.
193                 So, the node allocator should be stateless. It is standard requirement for \p std::allocator class objects.
194
195                 Do not use this function directly.
196             */
197             static void free( T * p )
198             {
199                 Allocator<T, Alloc> a;
200                 a.Delete( p );
201             }
202         };
203
204     }    // namespace details
205 }    // namespace cds
206
207 #endif    // #ifndef __CDS_DETAILS_ALLOCATOR_H