fixed adding file problem
[c11concurrency-benchmarks.git] / gdax-orderbook-hpp / demo / dependencies / libcds-2.3.2 / cds / details / aligned_allocator.h
1 /*
2     This file is a part of libcds - Concurrent Data Structures library
3
4     (C) Copyright Maxim Khizhinsky (libcds.dev@gmail.com) 2006-2017
5
6     Source code repo: http://github.com/khizmax/libcds/
7     Download: http://sourceforge.net/projects/libcds/files/
8
9     Redistribution and use in source and binary forms, with or without
10     modification, are permitted provided that the following conditions are met:
11
12     * Redistributions of source code must retain the above copyright notice, this
13       list of conditions and the following disclaimer.
14
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.
18
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.
29 */
30
31 #ifndef CDSLIB_DETAILS_ALIGNED_ALLOCATOR_H
32 #define CDSLIB_DETAILS_ALIGNED_ALLOCATOR_H
33
34 #include <cds/details/defs.h>
35 #include <cds/user_setup/allocator.h>
36
37 namespace cds { namespace details {
38
39     /// Allocator for aligned data
40     /**
41         The class is the wrapper around user-defined aligned allocator.
42         Template parameters:
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.
46
47         The \p nAlign parameter of member function specifyes desired aligment of data allocated.
48
49         \par Note
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:
52         \code
53         sizeof(T) % nAlign == 0
54         \endcode
55     */
56     template <
57         typename T
58         , typename ALIGNED_ALLOCATOR = CDS_DEFAULT_ALIGNED_ALLOCATOR
59     >
60     class AlignedAllocator: public ALIGNED_ALLOCATOR::template rebind<T>::other
61     {
62     public:
63         /// Underlying aligned allocator type
64         typedef typename ALIGNED_ALLOCATOR::template rebind<T>::other   allocator_type;
65
66         /// Analogue of operator new T(\p src... )
67         template <typename... S>
68         T *  New( size_t nAlign, const S&... src )
69         {
70             return Construct( allocator_type::allocate( nAlign, 1), src... );
71         }
72
73         /// Analogue of operator new T[\p nCount ]
74         T * NewArray( size_t nAlign, size_t nCount )
75         {
76             T * p = allocator_type::allocate( nAlign, nCount );
77             for ( size_t i = 0; i < nCount; ++i )
78                 Construct( p + i );
79             return p;
80         }
81
82         /// Analogue of operator new T[\p nCount ].
83         /**
84             Each item of array of type T is initialized by parameter \p src.
85         */
86         template <typename S>
87         T * NewArray( size_t nAlign, size_t nCount, const S& src )
88         {
89             T * p = allocator_type::allocate( nAlign, nCount );
90             for ( size_t i = 0; i < nCount; ++i )
91                 Construct( p + i, src );
92             return p;
93         }
94
95         /// Analogue of operator delete
96         void Delete( T * p )
97         {
98             allocator_type::destroy( p );
99             allocator_type::deallocate( p, 1 );
100         }
101
102         /// Analogue of operator delete []
103         void Delete( T * p, size_t nCount )
104         {
105             for ( size_t i = 0; i < nCount; ++i )
106                 allocator_type::destroy( p + i );
107             allocator_type::deallocate( p, nCount );
108         }
109
110         /// Analogue of placement operator new( \p p ) T( \p src... )
111         template <typename... S>
112         T * Construct( void * p, const S&... src )
113         {
114             return new( p ) T( src... );
115         }
116
117         /// Rebinds allocator to other type \p Q instead of \p T
118         template <typename Q>
119         struct rebind {
120             typedef AlignedAllocator< Q, typename ALIGNED_ALLOCATOR::template rebind<Q>::other >    other ; ///< Rebinding result
121         };
122     };
123
124 }} // namespace cds::details
125
126 #endif // #ifndef CDSLIB_DETAILS_ALIGNED_ALLOCATOR_H