3 #ifndef CDSLIB_DETAILS_ALLOCATOR_H
4 #define CDSLIB_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;
35 /// \p true if underlined allocator is \p std::allocator, \p false otherwise
36 static CDS_CONSTEXPR bool const c_bStdAllocator = std::is_same< allocator_type, std::allocator<T>>::value;
41 /// Analogue of operator new T(\p src... )
42 template <typename... S>
43 value_type * New( S const&... src )
45 return Construct( allocator_type::allocate(1), src... );
48 /// Analogue of <tt>operator new T( std::forward<Args>(args)... )</tt> (move semantics)
49 template <typename... Args>
50 value_type * MoveNew( Args&&... args )
52 return MoveConstruct( allocator_type::allocate(1), std::forward<Args>(args)... );
55 /// Analogue of operator new T[\p nCount ]
56 value_type * NewArray( size_t nCount )
58 value_type * p = allocator_type::allocate( nCount );
59 for ( size_t i = 0; i < nCount; ++i )
64 /// Analogue of operator new T[\p nCount ].
66 Each item of array of type T is initialized by parameter \p src: T( src )
69 value_type * NewArray( size_t nCount, S const& src )
71 value_type * p = allocator_type::allocate( nCount );
72 for ( size_t i = 0; i < nCount; ++i )
73 Construct( p + i, src );
77 # if CDS_COMPILER == CDS_COMPILER_INTEL
79 value_type * NewBlock( size_t nSize )
81 return Construct( heap_alloc( nSize ));
85 /// Allocates block of memory of size at least \p nSize bytes.
87 Internally, the block is allocated as an array of \p void* pointers,
88 then \p Construct() method is called to initialize \p T.
90 Precondition: <tt> nSize >= sizeof(T) </tt>
92 template <typename... S>
93 value_type * NewBlock( size_t nSize, S const&... src )
95 return Construct( heap_alloc( nSize ), src... );
98 /// Analogue of operator delete
99 void Delete( value_type * p )
101 allocator_type::destroy( p );
102 allocator_type::deallocate( p, 1 );
105 /// Analogue of operator delete []
106 void Delete( value_type * p, size_t nCount )
108 for ( size_t i = 0; i < nCount; ++i )
109 allocator_type::destroy( p + i );
110 allocator_type::deallocate( p, nCount );
113 # if CDS_COMPILER == CDS_COMPILER_INTEL
115 value_type * Construct( void * p )
117 return new( p ) value_type;
121 /// Analogue of placement operator new( \p p ) T( src... )
122 template <typename... S>
123 value_type * Construct( void * p, S const&... src )
125 value_type * pv = new( p ) value_type( src... );
129 /// Analogue of placement <tt>operator new( p ) T( std::forward<Args>(args)... )</tt>
130 template <typename... Args>
131 value_type * MoveConstruct( void * p, Args&&... args )
133 value_type * pv = new( p ) value_type( std::forward<Args>(args)... );
137 /// Rebinds allocator to other type \p Q instead of \p T
138 template <typename Q>
140 typedef Allocator< Q, typename Alloc::template rebind<Q>::other > other ; ///< Rebinding result
145 void * heap_alloc( size_t nByteSize )
147 assert( nByteSize >= sizeof(value_type));
149 size_t const nPtrSize = ( nByteSize + sizeof(void *) - 1 ) / sizeof(void *);
150 typedef typename allocator_type::template rebind< void * >::other void_allocator;
151 return void_allocator().allocate( nPtrSize );
156 /// Deferral removing of the object of type \p T. Helper class
157 template <typename T, typename Alloc = CDS_DEFAULT_ALLOCATOR>
158 struct deferral_deleter {
159 typedef T type ; ///< Type
160 typedef Alloc allocator_type ; ///< Allocator for removing
162 /// Frees the object \p p
164 Caveats: this function uses temporary object of type \ref cds::details::Allocator to free the node \p p.
165 So, the node allocator should be stateless. It is standard requirement for \p std::allocator class objects.
167 Do not use this function directly.
169 static void free( T * p )
171 Allocator<type, allocator_type> a;
176 } // namespace details
179 #endif // #ifndef CDSLIB_DETAILS_ALLOCATOR_H