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 /// Analogue of <tt>operator new T( std::forward<Args>(args)... )</tt> (move semantics)
54 template <typename... Args>
55 value_type * MoveNew( Args&&... args )
57 return MoveConstruct( allocator_type::allocate(1), std::forward<Args>(args)... );
60 /// Analogue of operator new T[\p nCount ]
61 value_type * NewArray( size_t nCount )
63 value_type * p = allocator_type::allocate( nCount );
64 for ( size_t i = 0; i < nCount; ++i )
69 /// Analogue of operator new T[\p nCount ].
71 Each item of array of type T is initialized by parameter \p src: T( src )
74 value_type * NewArray( size_t nCount, S const& src )
76 value_type * p = allocator_type::allocate( nCount );
77 for ( size_t i = 0; i < nCount; ++i )
78 Construct( p + i, src );
82 # if CDS_COMPILER == CDS_COMPILER_INTEL
84 value_type * NewBlock( size_t nSize )
86 return Construct( heap_alloc( nSize ));
90 /// Allocates block of memory of size at least \p nSize bytes.
92 Internally, the block is allocated as an array of \p void* pointers,
93 then \p Construct() method is called to initialize \p T.
95 Precondition: <tt> nSize >= sizeof(T) </tt>
97 template <typename... S>
98 value_type * NewBlock( size_t nSize, S const&... src )
100 return Construct( heap_alloc( nSize ), src... );
103 /// Analogue of operator delete
104 void Delete( value_type * p )
106 allocator_type::destroy( p );
107 allocator_type::deallocate( p, 1 );
110 /// Analogue of operator delete []
111 void Delete( value_type * p, size_t nCount )
113 for ( size_t i = 0; i < nCount; ++i )
114 allocator_type::destroy( p + i );
115 allocator_type::deallocate( p, nCount );
118 # if CDS_COMPILER == CDS_COMPILER_INTEL
120 value_type * Construct( void * p )
122 return new( p ) value_type;
126 /// Analogue of placement operator new( \p p ) T( src... )
127 template <typename... S>
128 value_type * Construct( void * p, S const&... src )
130 return new( p ) value_type( src... );
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 )
137 return new( p ) value_type( std::forward<Args>(args)... );
140 /// Rebinds allocator to other type \p Q instead of \p T
141 template <typename Q>
143 typedef Allocator< Q, typename Alloc::template rebind<Q>::other > other ; ///< Rebinding result
148 void * heap_alloc( size_t nByteSize )
150 assert( nByteSize >= sizeof(value_type));
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 );
162 static inline void impl_call_dtor(T* p, boost::false_type const&)
168 static inline void impl_call_dtor(T* p, boost::true_type const&)
173 /// Helper function to call destructor of type T
175 This function is empty for the type T that has trivial destructor.
178 static inline void call_dtor( T* p )
180 impl_call_dtor( p, ::boost::has_trivial_destructor<T>() );
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
190 /// Frees the object \p p
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.
195 Do not use this function directly.
197 static void free( T * p )
199 Allocator<T, Alloc> a;
204 } // namespace details
207 #endif // #ifndef __CDS_DETAILS_ALLOCATOR_H