issue#11: cds: changed __CDS_ guard prefix to CDSLIB_ for all .h files
[libcds.git] / cds / details / aligned_allocator.h
1 //$$CDS-header$$
2
3 #ifndef CDSLIB_DETAILS_ALIGNED_ALLOCATOR_H
4 #define CDSLIB_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         /// Analogue of operator new T(\p src... )
39         template <typename... S>
40         T *  New( size_t nAlign, const S&... src )
41         {
42             return Construct( allocator_type::allocate( nAlign, 1), src... );
43         }
44
45         /// Analogue of operator new T[\p nCount ]
46         T * NewArray( size_t nAlign, size_t nCount )
47         {
48             T * p = allocator_type::allocate( nAlign, nCount );
49             for ( size_t i = 0; i < nCount; ++i )
50                 Construct( p + i );
51             return p;
52         }
53
54         /// Analogue of operator new T[\p nCount ].
55         /**
56             Each item of array of type T is initialized by parameter \p src.
57         */
58         template <typename S>
59         T * NewArray( size_t nAlign, size_t nCount, const S& src )
60         {
61             T * p = allocator_type::allocate( nAlign, nCount );
62             for ( size_t i = 0; i < nCount; ++i )
63                 Construct( p + i, src );
64             return p;
65         }
66
67         /// Analogue of operator delete
68         void Delete( T * p )
69         {
70             allocator_type::destroy( p );
71             allocator_type::deallocate( p, 1 );
72         }
73
74         /// Analogue of operator delete []
75         void Delete( T * p, size_t nCount )
76         {
77             for ( size_t i = 0; i < nCount; ++i )
78                 allocator_type::destroy( p + i );
79             allocator_type::deallocate( p, nCount );
80         }
81
82         /// Analogue of placement operator new( \p p ) T( \p src... )
83         template <typename... S>
84         T * Construct( void * p, const S&... src )
85         {
86             return new( p ) T( src... );
87         }
88
89         /// Rebinds allocator to other type \p Q instead of \p T
90         template <typename Q>
91         struct rebind {
92             typedef AlignedAllocator< Q, typename ALIGNED_ALLOCATOR::template rebind<Q>::other >    other ; ///< Rebinding result
93         };
94     };
95
96 }} // namespace cds::details
97
98 #endif // #ifndef CDSLIB_DETAILS_ALIGNED_ALLOCATOR_H