3 #ifndef __CDS_DETAILS_ALLOCATOR_H
4 #define __CDS_DETAILS_ALLOCATOR_H
8 #include <cds/details/defs.h>
9 #include <cds/user_setup/allocator.h>
14 /// Extends \p std::allocator interface to provide semantics like operator \p new and \p delete
16 The class is the wrapper around underlying \p Alloc class.
17 \p Alloc provides the \p std::allocator interface.
19 template <typename T, class Alloc = CDS_DEFAULT_ALLOCATOR >
21 : public std::conditional<
22 std::is_same< T, typename Alloc::value_type>::value
24 , typename Alloc::template rebind<T>::other
28 /// Underlying allocator type
29 typedef typename std::conditional<
30 std::is_same< T, typename Alloc::value_type>::value
32 , typename Alloc::template rebind<T>::other
33 >::type allocator_type;
38 /// Analogue of operator new T(\p src... )
39 template <typename... S>
40 value_type * New( S const&... src )
42 return Construct( allocator_type::allocate(1), src... );
45 /// Analogue of <tt>operator new T( std::forward<Args>(args)... )</tt> (move semantics)
46 template <typename... Args>
47 value_type * MoveNew( Args&&... args )
49 return MoveConstruct( allocator_type::allocate(1), std::forward<Args>(args)... );
52 /// Analogue of operator new T[\p nCount ]
53 value_type * NewArray( size_t nCount )
55 value_type * p = allocator_type::allocate( nCount );
56 for ( size_t i = 0; i < nCount; ++i )
61 /// Analogue of operator new T[\p nCount ].
63 Each item of array of type T is initialized by parameter \p src: T( src )
66 value_type * NewArray( size_t nCount, S const& src )
68 value_type * p = allocator_type::allocate( nCount );
69 for ( size_t i = 0; i < nCount; ++i )
70 Construct( p + i, src );
74 # if CDS_COMPILER == CDS_COMPILER_INTEL
76 value_type * NewBlock( size_t nSize )
78 return Construct( heap_alloc( nSize ));
82 /// Allocates block of memory of size at least \p nSize bytes.
84 Internally, the block is allocated as an array of \p void* pointers,
85 then \p Construct() method is called to initialize \p T.
87 Precondition: <tt> nSize >= sizeof(T) </tt>
89 template <typename... S>
90 value_type * NewBlock( size_t nSize, S const&... src )
92 return Construct( heap_alloc( nSize ), src... );
95 /// Analogue of operator delete
96 void Delete( value_type * p )
98 allocator_type::destroy( p );
99 allocator_type::deallocate( p, 1 );
102 /// Analogue of operator delete []
103 void Delete( value_type * p, size_t nCount )
105 for ( size_t i = 0; i < nCount; ++i )
106 allocator_type::destroy( p + i );
107 allocator_type::deallocate( p, nCount );
110 # if CDS_COMPILER == CDS_COMPILER_INTEL
112 value_type * Construct( void * p )
114 return new( p ) value_type;
118 /// Analogue of placement operator new( \p p ) T( src... )
119 template <typename... S>
120 value_type * Construct( void * p, S const&... src )
122 return new( p ) value_type( src... );
125 /// Analogue of placement <tt>operator new( p ) T( std::forward<Args>(args)... )</tt>
126 template <typename... Args>
127 value_type * MoveConstruct( void * p, Args&&... args )
129 return new( p ) value_type( std::forward<Args>(args)... );
132 /// Rebinds allocator to other type \p Q instead of \p T
133 template <typename Q>
135 typedef Allocator< Q, typename Alloc::template rebind<Q>::other > other ; ///< Rebinding result
140 void * heap_alloc( size_t nByteSize )
142 assert( nByteSize >= sizeof(value_type));
144 size_t const nPtrSize = ( nByteSize + sizeof(void *) - 1 ) / sizeof(void *);
145 typedef typename allocator_type::template rebind< void * >::other void_allocator;
146 return void_allocator().allocate( nPtrSize );
151 /// Deferral removing of the object of type \p T. Helper class
152 template <typename T, typename Alloc = CDS_DEFAULT_ALLOCATOR>
153 struct deferral_deleter {
154 typedef T type ; ///< Type
155 typedef Alloc allocator_type ; ///< Allocator for removing
157 /// Frees the object \p p
159 Caveats: this function uses temporary object of type \ref cds::details::Allocator to free the node \p p.
160 So, the node allocator should be stateless. It is standard requirement for \p std::allocator class objects.
162 Do not use this function directly.
164 static void free( T * p )
166 Allocator<T, Alloc> a;
171 } // namespace details
174 #endif // #ifndef __CDS_DETAILS_ALLOCATOR_H