Move libcds 1.6.0 from SVN
[libcds.git] / cds / details / aligned_allocator.h
1 //$$CDS-header$$
2
3 #ifndef __CDS_DETAILS_ALIGNED_ALLOCATOR_H
4 #define __CDS_DETAILS_ALIGNED_ALLOCATOR_H
5
6 #include <cds/details/defs.h>
7 #include <cds/user_setup/allocator.h>
8
9 namespace cds { namespace details {
10
11     /// Allocator for aligned data
12     /**
13         The class is the wrapper around user-defined aligned allocator.
14         Template parameters:
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.
18
19         The \p nAlign parameter of member function specifyes desired aligment of data allocated.
20
21         \par Note
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:
24         \code
25         sizeof(T) % nAlign == 0
26         \endcode
27     */
28     template <
29         typename T
30         , typename ALIGNED_ALLOCATOR = CDS_DEFAULT_ALIGNED_ALLOCATOR
31     >
32     class AlignedAllocator: public ALIGNED_ALLOCATOR::template rebind<T>::other
33     {
34     public:
35         /// Underlying aligned allocator type
36         typedef typename ALIGNED_ALLOCATOR::template rebind<T>::other   allocator_type;
37
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 )
42         {
43             return Construct( allocator_type::allocate( nAlign, 1), src... );
44         }
45 #   else
46         //@cond
47         /// Analogue of operator new T
48         T *  New( size_t nAlign )
49         {
50             return Construct( allocator_type::allocate(nAlign, 1) );
51         }
52
53         /// Analogue of operator new T(\p src )
54         template <typename S>
55         T *  New( size_t nAlign, const S& src )
56         {
57             return Construct( allocator_type::allocate( nAlign, 1), src );
58         }
59
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 )
63         {
64             return Construct( allocator_type::allocate( nAlign, 1 ), s1, s2 );
65         }
66
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 )
70         {
71             return Construct( allocator_type::allocate(nAlign, 1), s1, s2, s3 );
72         }
73         //@endcond
74 #   endif
75
76         /// Analogue of operator new T[\p nCount ]
77         T * NewArray( size_t nAlign, size_t nCount )
78         {
79             T * p = allocator_type::allocate( nAlign, nCount );
80             for ( size_t i = 0; i < nCount; ++i )
81                 Construct( p + i );
82             return p;
83         }
84
85         /// Analogue of operator new T[\p nCount ].
86         /**
87             Each item of array of type T is initialized by parameter \p src.
88         */
89         template <typename S>
90         T * NewArray( size_t nAlign, size_t nCount, const S& src )
91         {
92             T * p = allocator_type::allocate( nAlign, nCount );
93             for ( size_t i = 0; i < nCount; ++i )
94                 Construct( p + i, src );
95             return p;
96         }
97
98         /// Analogue of operator delete
99         void Delete( T * p )
100         {
101             allocator_type::destroy( p );
102             allocator_type::deallocate( p, 1 );
103         }
104
105         /// Analogue of operator delete []
106         void Delete( T * p, size_t nCount )
107         {
108             for ( size_t i = 0; i < nCount; ++i )
109                 allocator_type::destroy( p + i );
110             allocator_type::deallocate( p, nCount );
111         }
112
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 )
117         {
118             return new( p ) T( src... );
119         }
120 #   else
121         /// Analogue of placement operator new( \p p ) T
122         T * Construct( void * p )
123         {
124             return new( p ) T;
125         }
126
127         /// Analogue of placement operator new( \p p ) T( \p src )
128         template <typename S>
129         T * Construct( void * p, const S& src )
130         {
131             return new( p ) T( src );
132         }
133
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 )
137         {
138             return new( p ) T( s1, s2 );
139         }
140
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 )
144         {
145             return new( p ) T( s1, s2, s3 );
146         }
147 #   endif
148
149         /// Rebinds allocator to other type \p Q instead of \p T
150         template <typename Q>
151         struct rebind {
152             typedef AlignedAllocator< Q, typename ALIGNED_ALLOCATOR::template rebind<Q>::other >    other ; ///< Rebinding result
153         };
154     };
155
156 }} // namespace cds::details
157
158 #endif // #ifndef __CDS_DETAILS_ALIGNED_ALLOCATOR_H