2 This file is a part of libcds - Concurrent Data Structures library
4 (C) Copyright Maxim Khizhinsky (libcds.dev@gmail.com) 2006-2017
6 Source code repo: http://github.com/khizmax/libcds/
7 Download: http://sourceforge.net/projects/libcds/files/
9 Redistribution and use in source and binary forms, with or without
10 modification, are permitted provided that the following conditions are met:
12 * Redistributions of source code must retain the above copyright notice, this
13 list of conditions and the following disclaimer.
15 * Redistributions in binary form must reproduce the above copyright notice,
16 this list of conditions and the following disclaimer in the documentation
17 and/or other materials provided with the distribution.
19 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
23 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
26 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
27 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 #ifndef CDSLIB_DETAILS_ALIGNED_ALLOCATOR_H
32 #define CDSLIB_DETAILS_ALIGNED_ALLOCATOR_H
34 #include <cds/details/defs.h>
35 #include <cds/user_setup/allocator.h>
37 namespace cds { namespace details {
39 /// Allocator for aligned data
41 The class is the wrapper around user-defined aligned allocator.
43 \li \p T is a type to allocate
44 \li \p ALIGNED_ALLOCATOR is an aligned allocator implementation. Default implementation is defined by macro
45 CDS_DEFAULT_ALIGNED_ALLOCATOR from cds/user_setup/allocator.h header file.
47 The \p nAlign parameter of member function specifyes desired aligment of data allocated.
50 When an array allocation is performed the allocator guarantees the alignment for first element of array only.
51 To guarantee the alignment for each element of the array the size of type \p T must be multiple of \p nAlign:
53 sizeof(T) % nAlign == 0
58 , typename ALIGNED_ALLOCATOR = CDS_DEFAULT_ALIGNED_ALLOCATOR
60 class AlignedAllocator: public ALIGNED_ALLOCATOR::template rebind<T>::other
63 /// Underlying aligned allocator type
64 typedef typename ALIGNED_ALLOCATOR::template rebind<T>::other allocator_type;
66 /// Analogue of operator new T(\p src... )
67 template <typename... S>
68 T * New( size_t nAlign, const S&... src )
70 return Construct( allocator_type::allocate( nAlign, 1), src... );
73 /// Analogue of operator new T[\p nCount ]
74 T * NewArray( size_t nAlign, size_t nCount )
76 T * p = allocator_type::allocate( nAlign, nCount );
77 for ( size_t i = 0; i < nCount; ++i )
82 /// Analogue of operator new T[\p nCount ].
84 Each item of array of type T is initialized by parameter \p src.
87 T * NewArray( size_t nAlign, size_t nCount, const S& src )
89 T * p = allocator_type::allocate( nAlign, nCount );
90 for ( size_t i = 0; i < nCount; ++i )
91 Construct( p + i, src );
95 /// Analogue of operator delete
98 allocator_type::destroy( p );
99 allocator_type::deallocate( p, 1 );
102 /// Analogue of operator delete []
103 void Delete( T * p, size_t nCount )
105 for ( size_t i = 0; i < nCount; ++i )
106 allocator_type::destroy( p + i );
107 allocator_type::deallocate( p, nCount );
110 /// Analogue of placement operator new( \p p ) T( \p src... )
111 template <typename... S>
112 T * Construct( void * p, const S&... src )
114 return new( p ) T( src... );
117 /// Rebinds allocator to other type \p Q instead of \p T
118 template <typename Q>
120 typedef AlignedAllocator< Q, typename ALIGNED_ALLOCATOR::template rebind<Q>::other > other ; ///< Rebinding result
124 }} // namespace cds::details
126 #endif // #ifndef CDSLIB_DETAILS_ALIGNED_ALLOCATOR_H