3 #ifndef __CDS_DETAILS_ALLOCATOR_H
4 #define __CDS_DETAILS_ALLOCATOR_H
7 Allocator class for the library. Supports allocating and constructing of objects
10 2008.03.08 Maxim.Khiszinsky Created
13 #include <type_traits>
15 #include <cds/details/defs.h>
16 #include <cds/user_setup/allocator.h>
17 #include <boost/type_traits/has_trivial_destructor.hpp>
22 /// Extends \p std::allocator interface to provide semantics like operator \p new and \p delete
24 The class is the wrapper around underlying \p Alloc class.
25 \p Alloc provides the \p std::allocator interface.
27 template <typename T, class Alloc = CDS_DEFAULT_ALLOCATOR >
29 : public std::conditional<
30 std::is_same< T, typename Alloc::value_type>::value
32 , typename Alloc::template rebind<T>::other
36 /// Underlying allocator type
37 typedef typename std::conditional<
38 std::is_same< T, typename Alloc::value_type>::value
40 , typename Alloc::template rebind<T>::other
41 >::type allocator_type;
46 /// Analogue of operator new T(\p src... )
47 template <typename... S>
48 value_type * New( S const&... src )
50 return Construct( allocator_type::allocate(1), src... );
53 # ifdef CDS_EMPLACE_SUPPORT
54 /// Analogue of <tt>operator new T( std::forward<Args>(args)... )</tt> (move semantics)
56 This function is available only for compiler that supports
57 variadic template and move semantics
59 template <typename... Args>
60 value_type * MoveNew( Args&&... args )
62 return MoveConstruct( allocator_type::allocate(1), std::forward<Args>(args)... );
67 /// Analogue of operator new T[\p nCount ]
68 value_type * NewArray( size_t nCount )
70 value_type * p = allocator_type::allocate( nCount );
71 for ( size_t i = 0; i < nCount; ++i )
76 /// Analogue of operator new T[\p nCount ].
78 Each item of array of type T is initialized by parameter \p src: T( src )
81 value_type * NewArray( size_t nCount, S const& src )
83 value_type * p = allocator_type::allocate( nCount );
84 for ( size_t i = 0; i < nCount; ++i )
85 Construct( p + i, src );
89 # if CDS_COMPILER == CDS_COMPILER_INTEL
91 value_type * NewBlock( size_t nSize )
93 return Construct( heap_alloc( nSize ));
97 /// Allocates block of memory of size at least \p nSize bytes.
99 Internally, the block is allocated as an array of \p void* pointers,
100 then \p Construct() method is called to initialize \p T.
102 Precondition: <tt> nSize >= sizeof(T) </tt>
104 template <typename... S>
105 value_type * NewBlock( size_t nSize, S const&... src )
107 return Construct( heap_alloc( nSize ), src... );
110 /// Analogue of operator delete
111 void Delete( value_type * p )
113 allocator_type::destroy( p );
114 allocator_type::deallocate( p, 1 );
117 /// Analogue of operator delete []
118 void Delete( value_type * p, size_t nCount )
120 for ( size_t i = 0; i < nCount; ++i )
121 allocator_type::destroy( p + i );
122 allocator_type::deallocate( p, nCount );
125 # if CDS_COMPILER == CDS_COMPILER_INTEL
127 value_type * Construct( void * p )
129 return new( p ) value_type;
133 /// Analogue of placement operator new( \p p ) T( src... )
134 template <typename... S>
135 value_type * Construct( void * p, S const&... src )
137 return new( p ) value_type( src... );
140 # ifdef CDS_EMPLACE_SUPPORT
141 /// Analogue of placement <tt>operator new( p ) T( std::forward<Args>(args)... )</tt>
143 This function is available only for compiler that supports
144 variadic template and move semantics
146 template <typename... Args>
147 value_type * MoveConstruct( void * p, Args&&... args )
149 return new( p ) value_type( std::forward<Args>(args)... );
153 /// Rebinds allocator to other type \p Q instead of \p T
154 template <typename Q>
156 typedef Allocator< Q, typename Alloc::template rebind<Q>::other > other ; ///< Rebinding result
161 void * heap_alloc( size_t nByteSize )
163 assert( nByteSize >= sizeof(value_type));
165 size_t const nPtrSize = ( nByteSize + sizeof(void *) - 1 ) / sizeof(void *);
166 typedef typename allocator_type::template rebind< void * >::other void_allocator;
167 return void_allocator().allocate( nPtrSize );
175 static inline void impl_call_dtor(T* p, boost::false_type const&)
181 static inline void impl_call_dtor(T* p, boost::true_type const&)
186 /// Helper function to call destructor of type T
188 This function is empty for the type T that has trivial destructor.
191 static inline void call_dtor( T* p )
193 impl_call_dtor( p, ::boost::has_trivial_destructor<T>() );
197 /// Deferral removing of the object of type \p T. Helper class
198 template <typename T, typename Alloc = CDS_DEFAULT_ALLOCATOR>
199 struct deferral_deleter {
200 typedef T type ; ///< Type
201 typedef Alloc allocator_type ; ///< Allocator for removing
203 /// Frees the object \p p
205 Caveats: this function uses temporary object of type \ref cds::details::Allocator to free the node \p p.
206 So, the node allocator should be stateless. It is standard requirement for \p std::allocator class objects.
208 Do not use this function directly.
210 static void free( T * p )
212 Allocator<T, Alloc> a;
217 } // namespace details
220 #endif // #ifndef __CDS_DETAILS_ALLOCATOR_H