3 #ifndef CDSLIB_DETAILS_ALIGNED_ALLOCATOR_H
4 #define CDSLIB_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 /// Analogue of operator new T(\p src... )
39 template <typename... S>
40 T * New( size_t nAlign, const S&... src )
42 return Construct( allocator_type::allocate( nAlign, 1), src... );
45 /// Analogue of operator new T[\p nCount ]
46 T * NewArray( size_t nAlign, size_t nCount )
48 T * p = allocator_type::allocate( nAlign, nCount );
49 for ( size_t i = 0; i < nCount; ++i )
54 /// Analogue of operator new T[\p nCount ].
56 Each item of array of type T is initialized by parameter \p src.
59 T * NewArray( size_t nAlign, size_t nCount, const S& src )
61 T * p = allocator_type::allocate( nAlign, nCount );
62 for ( size_t i = 0; i < nCount; ++i )
63 Construct( p + i, src );
67 /// Analogue of operator delete
70 allocator_type::destroy( p );
71 allocator_type::deallocate( p, 1 );
74 /// Analogue of operator delete []
75 void Delete( T * p, size_t nCount )
77 for ( size_t i = 0; i < nCount; ++i )
78 allocator_type::destroy( p + i );
79 allocator_type::deallocate( p, nCount );
82 /// Analogue of placement operator new( \p p ) T( \p src... )
83 template <typename... S>
84 T * Construct( void * p, const S&... src )
86 return new( p ) T( src... );
89 /// Rebinds allocator to other type \p Q instead of \p T
92 typedef AlignedAllocator< Q, typename ALIGNED_ALLOCATOR::template rebind<Q>::other > other ; ///< Rebinding result
96 }} // namespace cds::details
98 #endif // #ifndef CDSLIB_DETAILS_ALIGNED_ALLOCATOR_H