summaryrefslogtreecommitdiff
path: root/boost/align/aligned_allocator_adaptor.hpp
blob: f6e0bef8aaff5e493f2d1ed1d366fcadb892150f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/*
 (c) 2014 Glen Joseph Fernandes
 glenjofe at gmail dot com

 Distributed under the Boost Software
 License, Version 1.0.
 http://boost.org/LICENSE_1_0.txt
*/
#ifndef BOOST_ALIGN_ALIGNED_ALLOCATOR_ADAPTOR_HPP
#define BOOST_ALIGN_ALIGNED_ALLOCATOR_ADAPTOR_HPP

#include <boost/config.hpp>
#include <boost/static_assert.hpp>
#include <boost/align/align.hpp>
#include <boost/align/aligned_allocator_adaptor_forward.hpp>
#include <boost/align/alignment_of.hpp>
#include <boost/align/detail/addressof.hpp>
#include <boost/align/detail/is_alignment_constant.hpp>
#include <boost/align/detail/max_align.hpp>
#include <new>

#if !defined(BOOST_NO_CXX11_ALLOCATOR)
#include <memory>
#endif

#if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
#include <utility>
#endif

namespace boost {
    namespace alignment {
        template<class Allocator, std::size_t Alignment>
        class aligned_allocator_adaptor
            : public Allocator {
            BOOST_STATIC_ASSERT(detail::
                is_alignment_constant<Alignment>::value);

#if !defined(BOOST_NO_CXX11_ALLOCATOR)
            typedef std::allocator_traits<Allocator> Traits;

            typedef typename Traits::
                template rebind_alloc<char> CharAlloc;

            typedef typename Traits::
                template rebind_traits<char> CharTraits;

            typedef typename CharTraits::pointer CharPtr;
#else
            typedef typename Allocator::
                template rebind<char>::other CharAlloc;

            typedef typename CharAlloc::pointer CharPtr;
#endif

        public:
#if !defined(BOOST_NO_CXX11_ALLOCATOR)
            typedef typename Traits::value_type value_type;
            typedef typename Traits::size_type size_type;
#else
            typedef typename Allocator::value_type value_type;
            typedef typename Allocator::size_type size_type;
#endif

            typedef value_type* pointer;
            typedef const value_type* const_pointer;
            typedef void* void_pointer;
            typedef const void* const_void_pointer;
            typedef std::ptrdiff_t difference_type;

        private:
            typedef detail::max_align<Alignment,
                detail::max_align<alignment_of<value_type>::value,
                    alignment_of<CharPtr>::value>::value> MaxAlign;

        public:
            template<class U>
            struct rebind {
#if !defined(BOOST_NO_CXX11_ALLOCATOR)
                typedef aligned_allocator_adaptor<typename Traits::
                    template rebind_alloc<U>, Alignment> other;
#else
                typedef aligned_allocator_adaptor<typename Allocator::
                    template rebind<U>::other, Alignment> other;
#endif
            };

#if !defined(BOOST_NO_CXX11_DEFAULTED_FUNCTIONS)
            aligned_allocator_adaptor() = default;
#else
            aligned_allocator_adaptor()
                : Allocator() {
            }
#endif

#if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
            template<class A>
            explicit aligned_allocator_adaptor(A&& alloc)
                BOOST_NOEXCEPT
                : Allocator(std::forward<A>(alloc)) {
            }
#else
            template<class A>
            explicit aligned_allocator_adaptor(const A& alloc)
                BOOST_NOEXCEPT
                : Allocator(alloc) {
            }
#endif

            template<class U>
            aligned_allocator_adaptor(const
                aligned_allocator_adaptor<U, Alignment>& other)
                BOOST_NOEXCEPT
                : Allocator(other.base()) {
            }

            Allocator& base()
                BOOST_NOEXCEPT {
                return static_cast<Allocator&>(*this);
            }

            const Allocator& base() const
                BOOST_NOEXCEPT {
                return static_cast<const Allocator&>(*this);
            }

            pointer allocate(size_type size) {
                std::size_t n1 = size * sizeof(value_type);
                std::size_t n2 = n1 + MaxAlign::value - 1;
                CharAlloc a(base());
                CharPtr p1 = a.allocate(sizeof p1 + n2);
                void* p2 = detail::addressof(*p1) + sizeof p1;
                (void)align(MaxAlign::value, n1, p2, n2);
                void* p3 = static_cast<CharPtr*>(p2) - 1;
                ::new(p3) CharPtr(p1);
                return static_cast<pointer>(p2);
            }

            pointer allocate(size_type size, const_void_pointer hint) {
                std::size_t n1 = size * sizeof(value_type);
                std::size_t n2 = n1 + MaxAlign::value - 1;
                CharPtr h = CharPtr();
                if (hint) {
                    h = *(static_cast<const CharPtr*>(hint) - 1);
                }
                CharAlloc a(base());
#if !defined(BOOST_NO_CXX11_ALLOCATOR)
                CharPtr p1 = CharTraits::allocate(a, sizeof p1 +
                    n2, h);
#else
                CharPtr p1 = a.allocate(sizeof p1 + n2, h);
#endif
                void* p2 = detail::addressof(*p1) + sizeof p1;
                (void)align(MaxAlign::value, n1, p2, n2);
                void* p3 = static_cast<CharPtr*>(p2) - 1;
                ::new(p3) CharPtr(p1);
                return static_cast<pointer>(p2);
            }

            void deallocate(pointer ptr, size_type size) {
                CharPtr* p1 = reinterpret_cast<CharPtr*>(ptr) - 1;
                CharPtr p2 = *p1;
                p1->~CharPtr();
                CharAlloc a(base());
                a.deallocate(p2, size * sizeof(value_type) +
                    MaxAlign::value + sizeof p2);
            }
        };

        template<class A1, class A2, std::size_t Alignment>
        inline bool operator==(const aligned_allocator_adaptor<A1,
            Alignment>& a, const aligned_allocator_adaptor<A2,
            Alignment>& b) BOOST_NOEXCEPT
        {
            return a.base() == b.base();
        }

        template<class A1, class A2, std::size_t Alignment>
        inline bool operator!=(const aligned_allocator_adaptor<A1,
            Alignment>& a, const aligned_allocator_adaptor<A2,
            Alignment>& b) BOOST_NOEXCEPT
        {
            return !(a == b);
        }
    }
}

#endif