3 #ifndef __CDS_DETAILS_ALIGNED_ALLOCATOR_H
4 #define __CDS_DETAILS_ALIGNED_ALLOCATOR_H
6 #include <cds/details/defs.h>
7 #include <cds/user_setup/allocator.h>
9 namespace cds { namespace details {
11 /// Allocator for aligned data
13 The class is the wrapper around user-defined aligned allocator.
15 \li \p T is a type to allocate
16 \li \p ALIGNED_ALLOCATOR is an aligned allocator implementation. Default implementation is defined by macro
17 CDS_DEFAULT_ALIGNED_ALLOCATOR from cds/user_setup/allocator.h header file.
19 The \p nAlign parameter of member function specifyes desired aligment of data allocated.
22 When an array allocation is performed the allocator guarantees the alignment for first element of array only.
23 To guarantee the alignment for each element of the array the size of type \p T must be multiple of \p nAlign:
25 sizeof(T) % nAlign == 0
30 , typename ALIGNED_ALLOCATOR = CDS_DEFAULT_ALIGNED_ALLOCATOR
32 class AlignedAllocator: public ALIGNED_ALLOCATOR::template rebind<T>::other
35 /// Underlying aligned allocator type
36 typedef typename ALIGNED_ALLOCATOR::template rebind<T>::other allocator_type;
38 # ifdef CDS_CXX11_VARIADIC_TEMPLATE_SUPPORT
39 /// Analogue of operator new T(\p src... )
40 template <typename... S>
41 T * New( size_t nAlign, const S&... src )
43 return Construct( allocator_type::allocate( nAlign, 1), src... );
47 /// Analogue of operator new T
48 T * New( size_t nAlign )
50 return Construct( allocator_type::allocate(nAlign, 1) );
53 /// Analogue of operator new T(\p src )
55 T * New( size_t nAlign, const S& src )
57 return Construct( allocator_type::allocate( nAlign, 1), src );
60 /// Analogue of operator new T( \p s1, \p s2 )
61 template <typename S1, typename S2>
62 T * New( size_t nAlign, const S1& s1, const S2& s2 )
64 return Construct( allocator_type::allocate( nAlign, 1 ), s1, s2 );
67 /// Analogue of operator new T( \p s1, \p s2, \p s3 )
68 template <typename S1, typename S2, typename S3>
69 T * New( size_t nAlign, const S1& s1, const S2& s2, const S3& s3 )
71 return Construct( allocator_type::allocate(nAlign, 1), s1, s2, s3 );
76 /// Analogue of operator new T[\p nCount ]
77 T * NewArray( size_t nAlign, size_t nCount )
79 T * p = allocator_type::allocate( nAlign, nCount );
80 for ( size_t i = 0; i < nCount; ++i )
85 /// Analogue of operator new T[\p nCount ].
87 Each item of array of type T is initialized by parameter \p src.
90 T * NewArray( size_t nAlign, size_t nCount, const S& src )
92 T * p = allocator_type::allocate( nAlign, nCount );
93 for ( size_t i = 0; i < nCount; ++i )
94 Construct( p + i, src );
98 /// Analogue of operator delete
101 allocator_type::destroy( p );
102 allocator_type::deallocate( p, 1 );
105 /// Analogue of operator delete []
106 void Delete( T * 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 # ifdef CDS_CXX11_VARIADIC_TEMPLATE_SUPPORT
114 /// Analogue of placement operator new( \p p ) T( \p src... )
115 template <typename... S>
116 T * Construct( void * p, const S&... src )
118 return new( p ) T( src... );
121 /// Analogue of placement operator new( \p p ) T
122 T * Construct( void * p )
127 /// Analogue of placement operator new( \p p ) T( \p src )
128 template <typename S>
129 T * Construct( void * p, const S& src )
131 return new( p ) T( src );
134 /// Analogue of placement operator new( \p p ) T( \p s1, \p s2 )
135 template <typename S1, typename S2>
136 T * Construct( void * p, const S1& s1, const S2& s2 )
138 return new( p ) T( s1, s2 );
141 /// Analogue of placement operator new( \p p ) T( \p s1, \p s2, \p s3 )
142 template <typename S1, typename S2, typename S3>
143 T * Construct( void * p, const S1& s1, const S2& s2, const S3& s3 )
145 return new( p ) T( s1, s2, s3 );
149 /// Rebinds allocator to other type \p Q instead of \p T
150 template <typename Q>
152 typedef AlignedAllocator< Q, typename ALIGNED_ALLOCATOR::template rebind<Q>::other > other ; ///< Rebinding result
156 }} // namespace cds::details
158 #endif // #ifndef __CDS_DETAILS_ALIGNED_ALLOCATOR_H